2个不稳定版本

0.2.0 2022年9月8日
0.1.0 2022年8月25日

#9 in #glfw-window

MIT 许可证

44KB
542

项目已废弃

最近我对即时模式的GUI并不感兴趣,所以我将不得不停止在这个项目上投入时间。如果你想,可以fork它。

checkmate

Egui工具包

egui的依赖项

Egui是一个小巧的UI库,你可以将其嵌入到你的App中。它需要App提供鼠标/键盘等输入。一旦它处理了输入,egui就会为你提供你需要绘制在显示屏上的数据。

egui_backend是一个将输入要求抽象为WindowBackend特质并输出绘图内容的GfxBackend的crate。这允许你为特定的库(如winit、glfw、wgpu、vulkan等)实现这些特质,并且任何用户都可以轻松地重用这些后端。

大多数App开发者只需要关注实现UserApp特质,并选择他们想要使用的后端。如果你不使用高级功能,那么你可以通过几行代码就轻松地交换后端。

// This is the user struct where we can store any data we want along with the window and gfx backends.
pub struct App {
    pub frame_count: usize,
    pub egui_context: egui::Context,
    pub glow_backend: GlowBackend,
    pub glfw_backend: GlfwBackend,
}
// we need to implement this trait for our struct
impl UserApp for App {
    // we can make it generic, but didn't want to complicate the example.
    type UserGfxBackend = GlowBackend;
    type UserWindowBackend = GlfwBackend;
    // The function which is used by some default fn impl of this trait
    // allows us access to window/gfx backends mutably at the same time
    fn get_all(
        &mut self,
    ) -> (
        &mut Self::UserWindowBackend,
        &mut Self::UserGfxBackend,
        &egui::Context,
    ) {
        (
            &mut self.glfw_backend,
            &mut self.glow_backend,
            &self.egui_context,
        )
    }
    // here, you put your gui code, which will be run every frame.
    fn gui_run(&mut self) {
        let egui_context = self.egui_context.clone();
        egui::Window::new("egui window").show(&egui_context, |ui| {
            ui.label(format!("frame number: {}", self.frame_count));
        });
    }
}

pub fn fake_main() {
    // create window backend with default config. here you can set initial flags like transparency or selecting opengl vs vulkan etc..
    let mut glfw_backend = GlfwBackend::new(Default::default(), BackendConfig::default());
    // creating gfx backend. It uses Window backend to load things like fn pointers or window handle for swapchain etc.. behind the scenes.
    let glow_backend = GlowBackend::new(&mut glfw_backend, Default::default());
    // initialize app state
    let app = App {
    frame_count: 0,
    egui_context: Default::default(),
    glow_backend,
    glfw_backend,
    };
    // enter event loop. Now, the `gui_run` method from trait impl will be called every frame. read docs for more info
    <App as UserApp>::UserWindowBackend::run_event_loop(app);
}

依赖项

~6–14MB
~126K SLoC