add imgui menu

This commit is contained in:
Michael Zhang 2022-01-12 20:39:52 -06:00
parent 917f90defa
commit 205f78fd84
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
4 changed files with 91 additions and 17 deletions

Binary file not shown.

View file

@ -23,6 +23,9 @@ impl EventHandler for Game {
) -> GameResult { ) -> GameResult {
self.mouse_pos = (x, y); self.mouse_pos = (x, y);
self.imgui.update_mouse_pos(x, y); self.imgui.update_mouse_pos(x, y);
if self.imgui.want_capture_mouse() {
return Ok(());
}
Ok(()) Ok(())
} }
@ -34,6 +37,10 @@ impl EventHandler for Game {
y: f32, y: f32,
) -> GameResult { ) -> GameResult {
self.imgui.update_mouse_down(btn); self.imgui.update_mouse_down(btn);
if self.imgui.want_capture_mouse() {
return Ok(());
}
// TODO: figure out if the UI handled anything, and then whether or not to keep going into // TODO: figure out if the UI handled anything, and then whether or not to keep going into
// letting the rest of the code handle the mouse press or not // letting the rest of the code handle the mouse press or not
match btn { match btn {
@ -85,6 +92,11 @@ impl EventHandler for Game {
} }
fn mouse_wheel_event(&mut self, _: &mut Context, x: f32, y: f32) -> GameResult { fn mouse_wheel_event(&mut self, _: &mut Context, x: f32, y: f32) -> GameResult {
self.imgui.update_scroll(x, y);
if self.imgui.want_capture_mouse() {
return Ok(());
}
self.seek_by_steps(-y as i32); self.seek_by_steps(-y as i32);
Ok(()) Ok(())
} }
@ -92,6 +104,9 @@ impl EventHandler for Game {
fn key_up_event(&mut self, _: &mut Context, keycode: KeyCode, keymods: KeyMods) -> GameResult { fn key_up_event(&mut self, _: &mut Context, keycode: KeyCode, keymods: KeyMods) -> GameResult {
use KeyCode::*; use KeyCode::*;
self.imgui.update_key_up(keycode, keymods); self.imgui.update_key_up(keycode, keymods);
if self.imgui.want_capture_keyboard() {
return Ok(());
}
match keycode { match keycode {
Space => self.toggle_playing(), Space => self.toggle_playing(),
@ -114,6 +129,9 @@ impl EventHandler for Game {
) -> GameResult { ) -> GameResult {
use KeyCode::*; use KeyCode::*;
self.imgui.update_key_down(keycode, keymods); self.imgui.update_key_down(keycode, keymods);
if self.imgui.want_capture_keyboard() {
return Ok(());
}
self.keymap.insert(keycode); self.keymap.insert(keycode);
match keycode { match keycode {

View file

@ -22,7 +22,7 @@ use ggez::{
Context, Context,
}; };
use image::io::Reader as ImageReader; use image::io::Reader as ImageReader;
use imgui::Window; use imgui::{Window, MenuItem};
use imgui_winit_support::WinitPlatform; use imgui_winit_support::WinitPlatform;
use libosu::{ use libosu::{
beatmap::Beatmap, beatmap::Beatmap,
@ -417,19 +417,6 @@ impl Game {
self.draw_seeker(ctx)?; self.draw_seeker(ctx)?;
let mut show = true;
self.imgui.render(ctx, 1.0, |ui| {
// Window
Window::new("Hello world")
.size([300.0, 600.0], imgui::Condition::FirstUseEver)
.position([50.0, 50.0], imgui::Condition::FirstUseEver)
.build(&ui, || {
// Your window stuff here!
ui.text("Hi from this label!");
});
ui.show_demo_window(&mut show);
});
// draw whatever tool user is using // draw whatever tool user is using
let (mx, my) = self.mouse_pos; let (mx, my) = self.mouse_pos;
let pos_x = (mx - PLAYFIELD_BOUNDS.x) / PLAYFIELD_BOUNDS.w * 512.0; let pos_x = (mx - PLAYFIELD_BOUNDS.x) / PLAYFIELD_BOUNDS.w * 512.0;
@ -533,6 +520,54 @@ impl Game {
_ => {} _ => {}
} }
let mut show = true;
self.imgui.render(ctx, 1.0, |ui| {
if let Some(menu_bar) = ui.begin_main_menu_bar() {
if let Some(menu) = ui.begin_menu("File") {
MenuItem::new("Save <C-s>").build(ui);
MenuItem::new("Create Difficulty").build(ui);
ui.separator();
MenuItem::new("Revert to Saved <C-l>").build(ui);
MenuItem::new("Export...").build(ui);
ui.separator();
MenuItem::new("Open Song Folder").build(ui);
MenuItem::new("Exit <Esc>").build(ui);
menu.end();
}
if let Some(menu) = ui.begin_menu("Edit") {
menu.end();
}
if let Some(menu) = ui.begin_menu("View") {
menu.end();
}
if let Some(menu) = ui.begin_menu("Compose") {
menu.end();
}
if let Some(menu) = ui.begin_menu("Design") {
menu.end();
}
if let Some(menu) = ui.begin_menu("Timing") {
menu.end();
}
if let Some(menu) = ui.begin_menu("Web") {
menu.end();
}
if let Some(menu) = ui.begin_menu("Help") {
menu.end();
}
menu_bar.end();
}
// Window
// Window::new("Hello world")
// .size([300.0, 600.0], imgui::Condition::FirstUseEver)
// .position([50.0, 50.0], imgui::Condition::FirstUseEver)
// .build(&ui, || {
// // Your window stuff here!
// ui.text("Hi from this label!");
// });
// ui.show_demo_window(&mut show);
});
graphics::present(ctx)?; graphics::present(ctx)?;
self.frame += 1; self.frame += 1;
if self.is_playing { if self.is_playing {

View file

@ -27,7 +27,7 @@ use ggez::Context;
use gfx_core::{handle::RenderTargetView, memory::Typed}; use gfx_core::{handle::RenderTargetView, memory::Typed};
use gfx_device_gl; use gfx_device_gl;
use imgui::{Context as ImContext, Key, Ui, Window}; use imgui::{Context as ImContext, FontId, Key, Ui, Window, FontSource};
use imgui_gfx_renderer::*; use imgui_gfx_renderer::*;
use std::time::Instant; use std::time::Instant;
@ -44,6 +44,7 @@ struct MouseState {
pub struct ImGuiWrapper { pub struct ImGuiWrapper {
pub imgui: ImContext, pub imgui: ImContext,
pub renderer: Renderer<gfx_core::format::Rgba8, gfx_device_gl::Resources>, pub renderer: Renderer<gfx_core::format::Rgba8, gfx_device_gl::Resources>,
font: FontId,
last_frame: Instant, last_frame: Instant,
mouse_state: MouseState, mouse_state: MouseState,
} }
@ -55,6 +56,13 @@ impl ImGuiWrapper {
imgui.set_ini_filename(None); imgui.set_ini_filename(None);
let (factory, gfx_device, _, _, _) = graphics::gfx_objects(ctx); let (factory, gfx_device, _, _, _) = graphics::gfx_objects(ctx);
// Font
let font = imgui.fonts().add_font(&[FontSource::TtfData {
data: include_bytes!("../resources/Roboto-Regular.ttf"),
size_pixels: 16.0,
config: None,
}]);
// Shaders // Shaders
let shaders = { let shaders = {
let version = gfx_device.get_info().shading_language; let version = gfx_device.get_info().shading_language;
@ -74,10 +82,10 @@ impl ImGuiWrapper {
}; };
// Renderer // Renderer
let mut renderer = Renderer::init(&mut imgui, &mut *factory, shaders).unwrap(); let renderer = Renderer::init(&mut imgui, &mut *factory, shaders).unwrap();
{ {
let mut io = imgui.io_mut(); let io = imgui.io_mut();
io[Key::Tab] = KeyCode::Tab as _; io[Key::Tab] = KeyCode::Tab as _;
io[Key::LeftArrow] = KeyCode::Left as _; io[Key::LeftArrow] = KeyCode::Left as _;
io[Key::RightArrow] = KeyCode::Right as _; io[Key::RightArrow] = KeyCode::Right as _;
@ -106,6 +114,7 @@ impl ImGuiWrapper {
Self { Self {
imgui, imgui,
renderer, renderer,
font,
last_frame: Instant::now(), last_frame: Instant::now(),
mouse_state: MouseState::default(), mouse_state: MouseState::default(),
} }
@ -130,7 +139,9 @@ impl ImGuiWrapper {
self.imgui.io_mut().delta_time = delta_s; self.imgui.io_mut().delta_time = delta_s;
let ui = self.imgui.frame(); let ui = self.imgui.frame();
let font = ui.push_font(self.font);
run_ui(&ui); run_ui(&ui);
font.pop();
// Render // Render
let (factory, _, encoder, _, render_target) = graphics::gfx_objects(ctx); let (factory, _, encoder, _, render_target) = graphics::gfx_objects(ctx);
@ -214,4 +225,14 @@ impl ImGuiWrapper {
self.mouse_state.wheel += y; self.mouse_state.wheel += y;
self.mouse_state.wheel_h += x; self.mouse_state.wheel_h += x;
} }
pub fn want_capture_mouse(&self) -> bool {
let io = self.imgui.io();
io.want_capture_mouse
}
pub fn want_capture_keyboard(&self) -> bool {
let io = self.imgui.io();
io.want_capture_keyboard
}
} }