egui-tetra
egui-tetra is a library that helps integrate egui, an immediate mode GUI library, with Tetra, a 2D game framework.
Usage
The easiest way to use egui-tetra is to make your main state struct implement
egui-tetra's [State
] trait instead of Tetra's. This will
give you access to a ui
callback where you can do your GUI
rendering.
use std::error::Error;
use egui_tetra::egui;
struct MainState;
impl egui_tetra::State<Box<dyn Error>> for MainState {
fn ui(
&mut self,
ctx: &mut tetra::Context,
egui_ctx: &egui::CtxRef,
) -> Result<(), Box<dyn Error>> {
egui::Window::new("hi!").show(egui_ctx, |ui| {
ui.label("Hello world!");
});
Ok(())
}
fn update(
&mut self,
ctx: &mut tetra::Context,
egui_ctx: &egui::CtxRef,
) -> Result<(), Box<dyn Error>> {
/// Your update code here
Ok(())
}
fn draw(
&mut self,
ctx: &mut tetra::Context,
egui_ctx: &egui::CtxRef,
) -> Result<(), Box<dyn Error>> {
/// Your drawing code here
Ok(())
}
fn event(
&mut self,
ctx: &mut tetra::Context,
egui_ctx: &egui::CtxRef,
event: tetra::Event,
) -> Result<(), Box<dyn Error>> {
/// Your event handling code here
Ok(())
}
}
When running the Tetra Context
, wrap your state
struct in a [StateWrapper
] to make it compatible with Tetra's
State
trait.
# use std::error::Error;
# use egui_tetra::egui;
#
# struct MainState;
#
# impl egui_tetra::State<Box<dyn Error>> for MainState {
# fn ui(
# &mut self,
# ctx: &mut tetra::Context,
# egui_ctx: &egui::CtxRef,
# ) -> Result<(), Box<dyn Error>> {
# egui::Window::new("hi!").show(egui_ctx, |ui| {
# ui.label("Hello world!");
# });
# Ok(())
# }
#
# fn update(
# &mut self,
# ctx: &mut tetra::Context,
# egui_ctx: &egui::CtxRef,
# ) -> Result<(), Box<dyn Error>> {
# Ok(())
# }
#
# fn draw(
# &mut self,
# ctx: &mut tetra::Context,
# egui_ctx: &egui::CtxRef,
# ) -> Result<(), Box<dyn Error>> {
# Ok(())
# }
#
# fn event(
# &mut self,
# ctx: &mut tetra::Context,
# egui_ctx: &egui::CtxRef,
# event: tetra::Event,
# ) -> Result<(), Box<dyn Error>> {
# Ok(())
# }
# }
#
fn main() -> Result<(), Box<dyn Error>> {
tetra::ContextBuilder::new("example", 800, 600)
.build()?
.run(|_| Ok(egui_tetra::StateWrapper::new(MainState)))
}
If you need more control, you can use [EguiWrapper
] and manually
hook up egui to Tetra's callbacks.