Skip to content

Commit

Permalink
Merge pull request #39 from caspark/master
Browse files Browse the repository at this point in the history
Upgrade to egui v0.25 and macroquad v0.4.4
  • Loading branch information
optozorax authored Feb 3, 2024
2 parents 5ff0fb7 + fe69fe2 commit 62f1a3e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 56 deletions.
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "MIT/Apache-2.0"
homepage = "https://github.com/optozorax/egui-macroquad"
repository = "https://github.com/optozorax/egui-macroquad"
description = "Bindings between egui and macroquad"
readme="README.md"
readme = "README.md"
categories = ["gui", "game-development"]
keywords = ["gui", "imgui", "immediate", "portable", "gamedev"]
include = [
Expand All @@ -18,16 +18,17 @@ include = [
]

[dependencies]
egui = "0.21.0"
egui-miniquad = "0.14.0"
macroquad = { version="0.3.25", default-features=false }
egui = "0.25.0"
# use a fork of egui-miniquad that's been updated to support egui 0.25.0 until https://github.com/not-fl3/egui-miniquad/pull/65 is merged
egui-miniquad = { git = 'https://github.com/caspark/egui-miniquad.git', rev="4fb6d4c4f3c1ed2114c5cf80f8ce967f5301e318" }
macroquad = { version="0.4.4", default-features=false }

[features]
default = ["audio"]
audio = ["macroquad/audio"]

[dev-dependencies]
egui_demo_lib = "0.21.0"
egui_demo_lib = "0.25.0"

[profile.release]
opt-level = 2
84 changes: 33 additions & 51 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//! // Draw things before egui
//!
//! egui_macroquad::draw();
//!
//!
//! // Draw things after egui
//!
//! next_frame().await;
Expand All @@ -50,7 +50,10 @@ use miniquad as mq;
pub use egui;
pub use macroquad;

struct Egui(EguiMq, usize);
struct Egui {
egui_mq: EguiMq,
input_subscriber_id: usize,
}

// Global variable and global functions because it's more like macroquad way
static mut EGUI: Option<Egui> = None;
Expand All @@ -68,24 +71,27 @@ fn get_egui() -> &'static mut Egui {

impl Egui {
fn new() -> Self {
Self(
EguiMq::new(unsafe { get_internal_gl() }.quad_context),
macroquad::input::utils::register_input_subscriber(),
)
Self {
egui_mq: EguiMq::new(unsafe { get_internal_gl() }.quad_context),
input_subscriber_id: macroquad::input::utils::register_input_subscriber(),
}
}

fn ui<F: FnOnce(&mut mq::Context, &egui::Context)>(&mut self, f: F) {
fn ui<F>(&mut self, f: F)
where
F: FnOnce(&mut dyn mq::RenderingBackend, &egui::Context),
{
let gl = unsafe { get_internal_gl() };
macroquad::input::utils::repeat_all_miniquad_input(self, self.1);
macroquad::input::utils::repeat_all_miniquad_input(self, self.input_subscriber_id);

self.0.run(gl.quad_context, f);
self.egui_mq.run(gl.quad_context, f);
}

fn draw(&mut self) {
let mut gl = unsafe { get_internal_gl() };
// Ensure that macroquad's shapes are not goint to be lost, and draw them now
gl.flush();
self.0.draw(&mut gl.quad_context);
self.egui_mq.draw(gl.quad_context);
}
}

Expand All @@ -96,7 +102,7 @@ pub fn ui<F: FnOnce(&egui::Context)>(f: F) {

/// Configure egui without beginning or ending a frame.
pub fn cfg<F: FnOnce(&egui::Context)>(f: F) {
f(get_egui().0.egui_ctx());
f(get_egui().egui_mq.egui_ctx());
}

/// Draw egui ui. Must be called after `ui` and once per frame.
Expand All @@ -105,59 +111,35 @@ pub fn draw() {
}

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

fn draw(&mut self, _ctx: &mut mq::Context) {}
fn draw(&mut self) {}

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

fn mouse_wheel_event(&mut self, _ctx: &mut mq::Context, dx: f32, dy: f32) {
self.0.mouse_wheel_event(dx, dy);
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.0.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.0.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,
) {
self.0.char_event(character);
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.0.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) {
self.0.key_up_event(keycode, keymods);
fn key_up_event(&mut self, keycode: mq::KeyCode, keymods: mq::KeyMods) {
self.egui_mq.key_up_event(keycode, keymods);
}
}

0 comments on commit 62f1a3e

Please sign in to comment.