Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to egui v0.25.0 and miniquad v0.4 #65

Merged
merged 10 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ include = ["../LICENSE-APACHE", "../LICENSE-MIT", "**/*.rs", "Cargo.toml"]

[dependencies]
bytemuck = "1.9"
egui = { version = "0.22.0", features = ["bytemuck"] }
miniquad = { version = "0.3.12" }
egui = { version = "0.25", features = ["bytemuck"] }
miniquad = { version = "=0.4.0" }
quad-url = "0.1"

[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand All @@ -31,7 +31,7 @@ quad-rand = "0.2.1"
copypasta = "0.8.1"

[dev-dependencies]
egui_demo_lib = { version = "0.22.0", default-features = false }
egui_demo_lib = { version = "0.25", default-features = false }
glam = "0.22.0"

[profile.release]
Expand Down
110 changes: 57 additions & 53 deletions examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,54 @@ struct Stage {
show_egui_demo_windows: bool,
egui_demo_windows: egui_demo_lib::DemoWindows,
color_test: egui_demo_lib::ColorTest,
pixels_per_point: f32,
prev_egui_zoom_factor: f32,
zoom_factor: f32,
mq_ctx: Box<dyn mq::RenderingBackend>,
}

impl Stage {
fn new(ctx: &mut mq::Context) -> Self {
fn new() -> Self {
let mut mq_ctx = mq::window::new_rendering_backend();

Self {
egui_mq: egui_mq::EguiMq::new(ctx),
egui_mq: egui_mq::EguiMq::new(&mut *mq_ctx),
show_egui_demo_windows: true,
egui_demo_windows: Default::default(),
color_test: Default::default(),
pixels_per_point: ctx.dpi_scale(),
prev_egui_zoom_factor: 1.0,
zoom_factor: 1.0,
mq_ctx,
}
}
}

impl mq::EventHandler for Stage {
fn update(&mut self, _ctx: &mut mq::Context) {}
fn update(&mut self) {}

fn draw(&mut self, mq_ctx: &mut mq::Context) {
mq_ctx.clear(Some((1., 1., 1., 1.)), None, None);
mq_ctx.begin_default_pass(mq::PassAction::clear_color(0.0, 0.0, 0.0, 1.0));
mq_ctx.end_render_pass();
fn draw(&mut self) {
self.mq_ctx.clear(Some((1., 1., 1., 1.)), None, None);
self.mq_ctx
.begin_default_pass(mq::PassAction::clear_color(0.0, 0.0, 0.0, 1.0));
self.mq_ctx.end_render_pass();

let dpi_scale = mq_ctx.dpi_scale();
let dpi_scale = mq::window::dpi_scale();

// Run the UI code:
self.egui_mq.run(mq_ctx, |_mq_ctx, egui_ctx| {
self.egui_mq.run(&mut *self.mq_ctx, |_mq_ctx, egui_ctx| {
if self.show_egui_demo_windows {
self.egui_demo_windows.ui(egui_ctx);
}

// zoom factor could have been changed by the user in egui using Ctrl/Cmd and -/+/0,
// but it could also be in the middle of being changed by us using the slider. So we
// only allow egui's zoom to override our zoom if the egui zoom is different from what
// we saw last time (meaning the user has changed it).
let curr_egui_zoom = egui_ctx.zoom_factor();
if self.prev_egui_zoom_factor != curr_egui_zoom {
self.zoom_factor = curr_egui_zoom;
}
self.prev_egui_zoom_factor = curr_egui_zoom;

egui::Window::new("egui ❤ miniquad").show(egui_ctx, |ui| {
egui::widgets::global_dark_light_mode_buttons(ui);
ui.checkbox(&mut self.show_egui_demo_windows, "Show egui demo windows");
Expand All @@ -44,13 +61,24 @@ impl mq::EventHandler for Stage {
ui.label("Physical pixels per each logical 'point':");
ui.label(format!("native: {:.2}", dpi_scale));
ui.label(format!("egui: {:.2}", ui.ctx().pixels_per_point()));
ui.label("Current zoom factor:");
ui.add(
egui::Slider::new(&mut self.pixels_per_point, 0.75..=3.0).logarithmic(true),
egui::Slider::new(&mut self.zoom_factor, 0.75..=3.0).logarithmic(true),
)
.on_hover_text("Physical pixels per logical point");
.on_hover_text("Override egui zoom factor manually (changes effective pixels per point)");
if ui.button("Reset").clicked() {
self.pixels_per_point = dpi_scale;
self.zoom_factor = 1.0;
}

ui.label("By default, egui allows zooming with\nCtrl/Cmd and +/-/0");
// Creating a checkbox that directly mutates the egui context's options causes a
// freeze so we copy the state out, possibly mutate it with the checkbox, and
// then copy it back in.
let mut zoom_with_keyboard = egui_ctx.options(|o| o.zoom_with_keyboard);
ui.checkbox(&mut zoom_with_keyboard, "Allow egui zoom with keyboard");
egui_ctx.options_mut(|o|
o.zoom_with_keyboard = zoom_with_keyboard
);
});

#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -61,9 +89,9 @@ impl mq::EventHandler for Stage {
}
});

// Don't change scale while dragging the slider
// Don't change zoom while dragging the slider
if !egui_ctx.is_using_pointer() {
egui_ctx.set_pixels_per_point(self.pixels_per_point);
egui_ctx.set_zoom_factor(self.zoom_factor);
}

egui::Window::new("Color Test").show(egui_ctx, |ui| {
Expand All @@ -77,62 +105,38 @@ impl mq::EventHandler for Stage {

// Draw things behind egui here

self.egui_mq.draw(mq_ctx);
self.egui_mq.draw(&mut *self.mq_ctx);

// Draw things in front of egui here

mq_ctx.commit_frame();
self.mq_ctx.commit_frame();
}

fn mouse_motion_event(&mut self, _: &mut mq::Context, x: f32, y: f32) {
fn mouse_motion_event(&mut self, x: f32, y: f32) {
self.egui_mq.mouse_motion_event(x, y);
}

fn mouse_wheel_event(&mut self, _: &mut mq::Context, dx: f32, dy: f32) {
fn mouse_wheel_event(&mut self, dx: f32, dy: f32) {
self.egui_mq.mouse_wheel_event(dx, dy);
}

fn mouse_button_down_event(
&mut self,
ctx: &mut mq::Context,
mb: mq::MouseButton,
x: f32,
y: f32,
) {
self.egui_mq.mouse_button_down_event(ctx, mb, x, y);
fn mouse_button_down_event(&mut self, mb: mq::MouseButton, x: f32, y: f32) {
self.egui_mq.mouse_button_down_event(mb, x, y);
}

fn mouse_button_up_event(
&mut self,
ctx: &mut mq::Context,
mb: mq::MouseButton,
x: f32,
y: f32,
) {
self.egui_mq.mouse_button_up_event(ctx, mb, x, y);
fn mouse_button_up_event(&mut self, mb: mq::MouseButton, x: f32, y: f32) {
self.egui_mq.mouse_button_up_event(mb, x, y);
}

fn char_event(
&mut self,
_ctx: &mut mq::Context,
character: char,
_keymods: mq::KeyMods,
_repeat: bool,
) {
fn char_event(&mut self, character: char, _keymods: mq::KeyMods, _repeat: bool) {
self.egui_mq.char_event(character);
}

fn key_down_event(
&mut self,
ctx: &mut mq::Context,
keycode: mq::KeyCode,
keymods: mq::KeyMods,
_repeat: bool,
) {
self.egui_mq.key_down_event(ctx, keycode, keymods);
fn key_down_event(&mut self, keycode: mq::KeyCode, keymods: mq::KeyMods, _repeat: bool) {
self.egui_mq.key_down_event(keycode, keymods);
}

fn key_up_event(&mut self, _ctx: &mut mq::Context, keycode: mq::KeyCode, keymods: mq::KeyMods) {
fn key_up_event(&mut self, keycode: mq::KeyCode, keymods: mq::KeyMods) {
self.egui_mq.key_up_event(keycode, keymods);
}
}
Expand All @@ -150,5 +154,5 @@ fn main() {
window_height: 1024,
..Default::default()
};
mq::start(conf, |mut ctx| Box::new(Stage::new(&mut ctx)));
mq::start(conf, || Box::new(Stage::new()));
}
Loading
Loading