国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【rust/egui】(四)看看template的app.rs:update以及組件TopBottomPanel&Button

這篇具有很好參考價(jià)值的文章主要介紹了【rust/egui】(四)看看template的app.rs:update以及組件TopBottomPanel&Button。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

說(shuō)在前面

  • rust新手,egui沒(méi)啥找到啥教程,這里自己記錄下學(xué)習(xí)過(guò)程
  • 環(huán)境:windows11 22H2
  • rust版本:rustc 1.71.1
  • egui版本:0.22.0
  • eframe版本:0.22.0
  • 上一篇:這里

update

  • update實(shí)際上還是eframe::App的特征,并非egui的內(nèi)容。其官方注解如下:
    fn update(&mut self, ctx: &Context, frame: &mut Frame)
    Called each time the UI needs repainting, 
    which may be many times per second.
    
    update函數(shù)會(huì)在需要重繪ui(或者其他)的時(shí)候被調(diào)用,一秒可能會(huì)調(diào)用多次
    (稍微看了下源代碼,可能是事件驅(qū)動(dòng)調(diào)用?)
  • 我們可以在該函數(shù)里加個(gè)日志看看調(diào)用情況:
    [2023-08-20T07:44:02Z ERROR demo_app::app] update
    [2023-08-20T07:44:02Z ERROR demo_app::app] update
    [2023-08-20T07:44:02Z ERROR demo_app::app] update
    [2023-08-20T07:44:02Z ERROR demo_app::app] update
    [2023-08-20T07:44:02Z ERROR demo_app::app] update
    [2023-08-20T07:44:07Z ERROR demo_app::app] update
    [2023-08-20T07:44:07Z ERROR demo_app::app] update
    
    可以看到,當(dāng)我們不進(jìn)行任何操作(鼠標(biāo)、鍵盤(pán)均不輸入)時(shí),是沒(méi)有任何輸出的,當(dāng)按住任意一個(gè)按鍵后,日志開(kāi)始瘋狂輸出,這也印證了事件驅(qū)動(dòng)的猜想。
  • 其他內(nèi)容本文暫未深入

TopBottomPanel

  • 接下來(lái)正式開(kāi)始接觸egui的內(nèi)容,首先是:

    #[cfg(not(target_arch = "wasm32"))] // 非wasm才有
    egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
        // 頂部的panel通常用于菜單欄
        egui::menu::bar(ui, |ui| {
            ui.menu_button("File", |ui| {
                if ui.button("Quit").clicked() {
                    _frame.close();
                }
            });
        });
    });
    
  • 看看這里面是些什么,首先是top(),其實(shí)現(xiàn)如下:

    pub fn top(id: impl Into<Id>) -> Self {
        Self::new(TopBottomSide::Top, id)
    }
    // id需要是全局唯一的, e.g. Id::new("my_top_panel").
    

    參數(shù)id:需要實(shí)現(xiàn)Into<Id>特征,并且需要全局唯一,那我要是不唯一怎么辦,比如把下面的SidePanel也改成一樣的:

    egui::SidePanel::left("top_panel")
    

    運(yùn)行后出現(xiàn)報(bào)錯(cuò) (錯(cuò)誤提示還挺全) 【rust/egui】(四)看看template的app.rs:update以及組件TopBottomPanel&Button,Rust,rust,命令模式,開(kāi)發(fā)語(yǔ)言

  • 在函數(shù)實(shí)現(xiàn)中,實(shí)際上還是調(diào)用的new方法,傳入位置的枚舉TopBottomSide::Top

  • 當(dāng)然我們也可以調(diào)用bottom()方法,對(duì)應(yīng)枚舉TopBottomSide::Bottom
    【rust/egui】(四)看看template的app.rs:update以及組件TopBottomPanel&Button,Rust,rust,命令模式,開(kāi)發(fā)語(yǔ)言

  • new方法的實(shí)現(xiàn)如下:

    pub fn new(side: TopBottomSide, id: impl Into<Id>) -> Self {
        Self {
            side,
            id: id.into(), // 調(diào)用into方法,轉(zhuǎn)為Id類(lèi)型
            frame: None,
            resizable: false, // 下面是一些控制參數(shù)
            show_separator_line: true,
            default_height: None,
            height_range: 20.0..=f32::INFINITY,
        }
    }
    
  • 緊跟top()的是show()方法:

    pub fn show<R>(
        self,
        ctx: &Context,
        add_contents: impl FnOnce(&mut Ui) -> R
    ) -> InnerResponse<R>
    

    參數(shù)add_contentsFnOnce閉包,僅執(zhí)行一次,在我們的應(yīng)用中,閉包中添加了一個(gè)簡(jiǎn)單的菜單欄:

    // 添加菜單欄
    egui::menu::bar(ui, |ui| {
        ui.menu_button("File", |ui| {
        	if ui.button("Quit").clicked() {
            	_frame.close();
            }
        });
    });
    
  • 注意:上面說(shuō)的執(zhí)行一次,是說(shuō)此次update調(diào)用中執(zhí)行一次

  • 我們繼續(xù)深入下TopBottomPanel的定義:

    pub struct TopBottomPanel {
        side: TopBottomSide,
        id: Id,
        frame: Option<Frame>,
        resizable: bool,
        show_separator_line: bool,
        default_height: Option<f32>,
        height_range: RangeInclusive<f32>,
    }
    

    其實(shí)是可以修改一些樣式的,比如高度:

    egui::TopBottomPanel::top("top_panel").min_height(100.0).show(...
    

    【rust/egui】(四)看看template的app.rs:update以及組件TopBottomPanel&Button,Rust,rust,命令模式,開(kāi)發(fā)語(yǔ)言文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-668002.html

menu::bar

  • TopBottomPanel中,我們使用bar()函數(shù)添加了一個(gè)菜單欄,其函數(shù)定義如下:
    pub fn bar<R>(
        ui: &mut Ui,
        add_contents: impl FnOnce(&mut Ui) -> R
    ) -> InnerResponse<R>
    
    同樣使用FnOnce閉包來(lái)添加一些額外的元素
  • 菜單欄組件在TopBottomPanel::top中的展示效果最好,當(dāng)然也可以放在Window中。
    The menu bar goes well in a TopBottomPanel::top, 
    but can also be placed in a Window. In the latter case you may want to wrap it in Frame.
    
    【rust/egui】(四)看看template的app.rs:update以及組件TopBottomPanel&Button,Rust,rust,命令模式,開(kāi)發(fā)語(yǔ)言
    放到bottom會(huì)蓋住菜單(File):
    【rust/egui】(四)看看template的app.rs:update以及組件TopBottomPanel&Button,Rust,rust,命令模式,開(kāi)發(fā)語(yǔ)言

menu::menu_button

  • bar()的回調(diào)中,我們添加了一個(gè)下拉按鈕
    pub fn menu_button<R>(
        ui: &mut Ui,
        title: impl Into<WidgetText>,
        add_contents: impl FnOnce(&mut Ui) -> R
    ) -> InnerResponse<Option<R>>
    Construct a top level menu in a menu bar.
    
    似乎menu_button最好包在menu bar
  • 同時(shí)也使用了FnOnce閉包添加了一個(gè)按鈕:
    ui.menu_button("File", |ui| {
    	if ui.button("Quit").clicked() {
            _frame.close();
        }
    });
    
  • 其實(shí)我們還可以在menu_button中添加一個(gè)子menu_button
    ui.menu_button("File", |ui| {
        if ui.button("Quit").clicked() {
            _frame.close();
        }
        ui.menu_button("QuitMenu", |ui| {
            if ui.button("Quit").clicked() {
                _frame.close();
            }
        });
    });
    
    效果如圖
    【rust/egui】(四)看看template的app.rs:update以及組件TopBottomPanel&Button,Rust,rust,命令模式,開(kāi)發(fā)語(yǔ)言
  • 如果menu_button直接放在panel中會(huì)怎樣呢?
    【rust/egui】(四)看看template的app.rs:update以及組件TopBottomPanel&Button,Rust,rust,命令模式,開(kāi)發(fā)語(yǔ)言
    其實(shí)也是可以的,只是效果不是很好,對(duì)比一下(上圖是放在panel中,下圖是放在bar中的效果):
    【rust/egui】(四)看看template的app.rs:update以及組件TopBottomPanel&Button,Rust,rust,命令模式,開(kāi)發(fā)語(yǔ)言

Ui::button

  • 上面我們已經(jīng)接觸到了文本按鈕,其定義如下:
    pub fn button(&mut self, text: impl Into<WidgetText>) -> Response
    
  • 實(shí)際上是一個(gè)簡(jiǎn)單的封裝函數(shù):
    Button::new(text).ui(self)
    
  • 通常的用法是:
    if ui.button("Click me").clicked() {}
    
  • 現(xiàn)在我們進(jìn)一步看看Button的定義:
    pub struct Button {
        text: WidgetText,
        shortcut_text: WidgetText,
        wrap: Option<bool>,
        /// None means default for interact
        fill: Option<Color32>,
        stroke: Option<Stroke>,
        sense: Sense,
        small: bool,
        frame: Option<bool>,
        min_size: Vec2,
        rounding: Option<Rounding>,
        image: Option<widgets::Image>,
    }
    
  • 是有一些參數(shù)可以設(shè)置的,那我們?cè)鯓犹砑右粋€(gè)不一樣的按鈕呢?
    if ui
        .add(egui::Button::new("q")
        	// .fill(Color32::GOLD)
        	.min_size(egui::Vec2 { x: 20.0, y: 100.0 }))
        .clicked()
    {
        _frame.close();
    }
    
    【rust/egui】(四)看看template的app.rs:update以及組件TopBottomPanel&Button,Rust,rust,命令模式,開(kāi)發(fā)語(yǔ)言
    【rust/egui】(四)看看template的app.rs:update以及組件TopBottomPanel&Button,Rust,rust,命令模式,開(kāi)發(fā)語(yǔ)言
  • ui.button()、ui.add()返回的都是Response,它可以讓我們知道ui元素是否被點(diǎn)擊、拖拽,進(jìn)而做出對(duì)應(yīng)的處理;例如點(diǎn)擊事件:
    pub fn clicked(&self) -> bool {
        self.clicked[PointerButton::Primary as usize]
    }
    
    其大致流程是:鼠標(biāo)點(diǎn)擊事件被eframe捕獲,由egui計(jì)算與整個(gè)ui的交互結(jié)果,例如哪些元素被點(diǎn)擊到了,點(diǎn)擊結(jié)果存儲(chǔ)到Response.clicked數(shù)組中,我們只需訪問(wèn)即可。
    clicked存儲(chǔ)了五種點(diǎn)擊事件
    pub enum PointerButton {
        /// The primary mouse button is usually the left one.
        /// 通常是鼠標(biāo)左鍵
        Primary = 0,
    
        /// The secondary mouse button is usually the right one,
        /// and most often used for context menus or other optional things.
        /// 通常是鼠標(biāo)右鍵
        Secondary = 1,
    
        /// The tertiary mouse button is usually the middle mouse button (e.g. clicking the scroll wheel).
        /// 通常是鼠標(biāo)中鍵
        Middle = 2,
    
        /// The first extra mouse button on some mice. In web typically corresponds to the Browser back button.
        Extra1 = 3,
    
        /// The second extra mouse button on some mice. In web typically corresponds to the Browser forward button.
        Extra2 = 4,
    }
    

eframe::Frame::close

  • 調(diào)用該函數(shù)會(huì)通知eframe關(guān)閉應(yīng)用,調(diào)用后應(yīng)用不會(huì)立即關(guān)閉,而是在該幀結(jié)束的時(shí)候關(guān)閉
  • 同時(shí),如果crate::run_native后面還有代碼的話,也會(huì)繼續(xù)執(zhí)行:
    let ret = eframe::run_native(
        "demo app",
        native_options,
        Box::new(|cc| Box::new(demo_app::TemplateApp::new(cc))),
    );
    
    log::error!("end");
    
    ret
    

參考

  • Button
  • menu_button
  • bar
  • TopBottomPanel
  • update

到了這里,關(guān)于【rust/egui】(四)看看template的app.rs:update以及組件TopBottomPanel&Button的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【rust/egui】(二)看看template的main函數(shù):日志輸出以及eframe run_native

    【rust/egui】(二)看看template的main函數(shù):日志輸出以及eframe run_native

    rust新手,egui沒(méi)啥找到啥教程,這里自己記錄下學(xué)習(xí)過(guò)程 環(huán)境:windows11 22H2 rust版本:rustc 1.71.1 egui版本:0.22.0 eframe版本:0.22.0 上一篇:這里 首先讓我們看看 main.rs 中有些什么 在 eframe 中使用的日志庫(kù)為 log 以及 env_logger ,其日志等級(jí)有5個(gè): 我們可以在main函數(shù)中添加測(cè)試一

    2024年02月13日
    瀏覽(22)
  • 【rust/egui】(一)從編譯運(yùn)行template開(kāi)始

    【rust/egui】(一)從編譯運(yùn)行template開(kāi)始

    rust新手,egui沒(méi)啥找到啥教程,這里自己記錄下學(xué)習(xí)過(guò)程 環(huán)境:windows11 22H2 rust版本:rustc 1.71.1 egui版本:0.22.0 eframe版本:0.22.0 rust windows安裝參考:這里 本文默認(rèn)讀者已安裝相關(guān)環(huán)境(git、vscode等) egui github demo online 關(guān)于 immediate mode wikipedia microsoft learn 先 clone 下這個(gè)項(xiàng)目(也可

    2024年02月13日
    瀏覽(19)
  • RUST egui部署到github

    接上文,當(dāng)用trunk serve編譯部署后,工程目錄下就會(huì)有一個(gè)dist目錄,這個(gè)目錄就是用來(lái)部署用的。 :) 創(chuàng)建一個(gè)github repo,這個(gè)repo的名稱(chēng)有固定格式要求,就是你自己的用戶(hù)名+github.io,比如我的用戶(hù)名是crazyskady,那么這個(gè)repo就叫crazyskady.github.io 部署就簡(jiǎn)單了,先把自己的

    2024年03月21日
    瀏覽(19)
  • [Rust GUI]eframe(egui框架)代碼示例

    [Rust GUI]eframe(egui框架)代碼示例

    你可以使用egui的其他綁定,例如:egui-miniquad,bevy_egui,egui_sdl2_gl 等。 egui庫(kù)相當(dāng)于核心庫(kù),需要借助eframe框架寫(xiě)界面。 eframe使用egui_glow渲染,而egui_glow需要opengl2.0+。 1、訪問(wèn)微軟官網(wǎng)下載生成工具 2、勾選這個(gè) 3、對(duì)比勾選細(xì)節(jié) 4、點(diǎn)擊安裝 5、安裝完成 6、關(guān)閉 Visual Studio

    2024年02月08日
    瀏覽(22)
  • Rust圖形界面:從零開(kāi)始創(chuàng)建eGUi項(xiàng)目

    Rust圖形界面:從零開(kāi)始創(chuàng)建eGUi項(xiàng)目

    egui系列:初步 首先,用cargo創(chuàng)建一個(gè)新項(xiàng)目,并添加eframe 盡管默認(rèn)創(chuàng)建的項(xiàng)目只實(shí)現(xiàn)了輸出Hello world功能,但添加了eframe庫(kù),所以下載需要一點(diǎn)時(shí)間。 創(chuàng)建成功后,直接把下面的代碼寫(xiě)入main.rs文件中,這些代碼來(lái)自egui的hello_world示例。 然后運(yùn)行cargo run,結(jié)果如下所示 在e

    2024年02月01日
    瀏覽(23)
  • 【rust/egui】(十)使用painter繪制一些圖形—connections

    【rust/egui】(十)使用painter繪制一些圖形—connections

    rust新手,egui沒(méi)啥找到啥教程,這里自己記錄下學(xué)習(xí)過(guò)程 環(huán)境:windows11 22H2 rust版本:rustc 1.71.1 egui版本:0.22.0 eframe版本:0.22.0 上一篇:這里 在上一節(jié)我們使用 painter 繪制了一個(gè)可以拖拽的小方塊,現(xiàn)在我們來(lái)用 painter 將兩個(gè)小方塊連接起來(lái),類(lèi)似這種: 首先我們需要在我們

    2024年02月09日
    瀏覽(25)
  • 【Rust日?qǐng)?bào)】2023-02-14 Rust GUI 框架對(duì)比: Tauri vs Iced vs egui

    【Rust日?qǐng)?bào)】2023-02-14 Rust GUI 框架對(duì)比: Tauri vs Iced vs egui

    Rust GUI 框架對(duì)比: Tauri vs Iced vs egui Tauri:使用系統(tǒng)的 webview 來(lái)渲染 HTML/JS 的前端。你可以選擇任何前端框架。后臺(tái)是用Rust編寫(xiě)的,可以通過(guò)內(nèi)置的方法與前臺(tái)通信。 Iced: 受 Elm 啟發(fā)的(響應(yīng)式)GUI庫(kù)。在桌面上使用 wgpu 進(jìn)行渲染;實(shí)驗(yàn)性的web后端創(chuàng)建DOM進(jìn)行渲染。所有代碼

    2024年02月02日
    瀏覽(21)
  • 【rust/egui】(八)使用panels給你的應(yīng)用劃分功能區(qū)塊

    【rust/egui】(八)使用panels給你的應(yīng)用劃分功能區(qū)塊

    rust新手,egui沒(méi)啥找到啥教程,這里自己記錄下學(xué)習(xí)過(guò)程 環(huán)境:windows11 22H2 rust版本:rustc 1.71.1 egui版本:0.22.0 eframe版本:0.22.0 上一篇:這里 panel 是ui上的一塊區(qū)域,比如我們打開(kāi)CSDN的markdown編輯器,它大致上可以劃分成四(五)塊 (當(dāng)然實(shí)際實(shí)現(xiàn)上這四塊區(qū)域可能不是并列的

    2024年02月09日
    瀏覽(19)
  • k8s helm ingress-nginx Error: template: ingress-nginx/templates/controller-role “update-status“

    k8s helm ingress-nginx Error: template: ingress-nginx/templates/controller-role “update-status“

    k8s,使用helm包管理器安裝ingress-nginx時(shí),安裝文件出錯(cuò) helm 版本:Version:“v3.2.3” k8s 版本:Kubernetes v1.23.6 ingress-nginx chart version:4.9.0 app version:1.9.5 前面都按網(wǎng)上的教程做,沒(méi)有什么大問(wèn)題 最后一步執(zhí)行安裝命令 命令執(zhí)行失敗,報(bào)錯(cuò) 問(wèn)題可能出現(xiàn)在對(duì).Values.controller.extraArg

    2024年03月20日
    瀏覽(28)
  • 【rust/bevy】從game template開(kāi)始

    【rust/bevy】從game template開(kāi)始

    操作系統(tǒng):win11 rust版本:rustc 1.77.0-nightly bevy版本:0.12 rust安裝 這里 windows 下建議使用 msvc 版本 bevy 安裝 這里 clone代碼 運(yùn)行 結(jié)果 template中的例子是2d的,我們稍微修改下 首先增加一個(gè) CameraController ,代碼在 bevy 的例程中也可以找到 再添加一個(gè) SceneSetup ,用于初始化場(chǎng)景和相

    2024年01月18日
    瀏覽(65)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包