added gui, but does not show up
parent
466119907a
commit
ac46d9b0d8
|
@ -0,0 +1,97 @@
|
|||
use bevy_ecs::prelude::*;
|
||||
use egui::Key;
|
||||
use winit::event;
|
||||
use std::sync::Arc;
|
||||
use egui_glow::EguiGlow;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct Gui
|
||||
{
|
||||
egui_ctx: EguiGlow,
|
||||
menus: MenuManager,
|
||||
window: Arc<winit::window::Window>,
|
||||
}
|
||||
|
||||
impl Gui
|
||||
{
|
||||
pub fn new(ctx: EguiGlow, window: Arc<winit::window::Window>) -> Self
|
||||
{
|
||||
let mut menus = MenuManager::new();
|
||||
|
||||
menus.add(Menu::Hello);
|
||||
Self
|
||||
{
|
||||
egui_ctx: ctx,
|
||||
menus,
|
||||
window
|
||||
}
|
||||
}
|
||||
|
||||
pub fn passEvent(&mut self, event: &event::WindowEvent) -> bool
|
||||
{
|
||||
self.egui_ctx.on_event(event).consumed
|
||||
}
|
||||
|
||||
pub fn run(&mut self, world: &mut World)
|
||||
{
|
||||
let mut to_be_removed = Vec::new();
|
||||
|
||||
self.egui_ctx.run(&self.window, |ctx|
|
||||
{
|
||||
for (key, value) in &mut self.menus.0.iter_mut()
|
||||
{
|
||||
match value
|
||||
{
|
||||
Menu::Hello => if hellotest(ctx)
|
||||
{
|
||||
to_be_removed.push(*key);
|
||||
},
|
||||
Menu::Second => ()
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub enum Menu
|
||||
{
|
||||
Hello,
|
||||
Second
|
||||
}
|
||||
|
||||
fn hellotest(ctx: &egui::Context) -> bool
|
||||
{
|
||||
let mut open = true;
|
||||
egui::Window::new("hello there")
|
||||
.open(&mut open)
|
||||
.show(ctx, |ui| {
|
||||
if ui.button("hello").clicked()
|
||||
{
|
||||
println!("hai");
|
||||
}
|
||||
});
|
||||
|
||||
!open
|
||||
}
|
||||
|
||||
struct MenuManager(pub HashMap<u8, Menu>, u8);
|
||||
|
||||
impl MenuManager
|
||||
{
|
||||
pub fn new() -> Self
|
||||
{
|
||||
Self
|
||||
{
|
||||
0: HashMap::new(),
|
||||
1: 0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add(&mut self, new: Menu)
|
||||
{
|
||||
self.0.insert(self.1, new);
|
||||
self.1 = self.1 + 1;
|
||||
}
|
||||
}
|
49
src/main.rs
49
src/main.rs
|
@ -4,11 +4,13 @@ use std::sync::Arc;
|
|||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use egui_glow::EguiGlow;
|
||||
use glow::HasContext;
|
||||
|
||||
|
||||
use bevy_ecs::prelude::*;
|
||||
|
||||
use gui::Gui;
|
||||
use winit::{event_loop::EventLoopBuilder, window::WindowBuilder};
|
||||
use winit::event::{Event, WindowEvent};
|
||||
|
||||
|
@ -19,6 +21,7 @@ use glutin_winit::{self, GlWindow};
|
|||
use glutin::display::GetGlDisplay;
|
||||
|
||||
pub mod renderer;
|
||||
pub mod gui;
|
||||
fn main() {
|
||||
|
||||
// NOTE initialization code here
|
||||
|
@ -95,7 +98,7 @@ fn main() {
|
|||
|
||||
|
||||
// current Window Reference Counter
|
||||
let window = Rc::new(window.unwrap());
|
||||
let window = Arc::new(window.unwrap());
|
||||
|
||||
|
||||
let attrs = window.build_surface_attributes(<_>::default());
|
||||
|
@ -118,6 +121,10 @@ fn main() {
|
|||
})
|
||||
});
|
||||
|
||||
let mut gui = Gui::new(
|
||||
EguiGlow::new(&event_loop, Arc::clone(&gl), None),
|
||||
window.clone()
|
||||
);
|
||||
|
||||
unsafe {
|
||||
gl.disable(glow::BLEND);
|
||||
|
@ -148,24 +155,28 @@ fn main() {
|
|||
match event
|
||||
{
|
||||
Event::WindowEvent { event, .. } => {
|
||||
match event
|
||||
|
||||
if gui.passEvent(&event) == false
|
||||
{
|
||||
WindowEvent::CloseRequested => {
|
||||
println!("closing gracefully");
|
||||
control_flow.set_exit();
|
||||
},
|
||||
WindowEvent::Resized(size) => {
|
||||
if size.width !=0 && size.height !=0 {
|
||||
gl_surface.resize(
|
||||
&gl_context,
|
||||
std::num::NonZeroU32::new(size.width).unwrap(),
|
||||
std::num::NonZeroU32::new(size.height).unwrap()
|
||||
);
|
||||
}
|
||||
},
|
||||
// TODO to be implemented later
|
||||
_ => ()
|
||||
};
|
||||
match event
|
||||
{
|
||||
WindowEvent::CloseRequested => {
|
||||
println!("closing gracefully");
|
||||
control_flow.set_exit();
|
||||
},
|
||||
WindowEvent::Resized(size) => {
|
||||
if size.width !=0 && size.height !=0 {
|
||||
gl_surface.resize(
|
||||
&gl_context,
|
||||
std::num::NonZeroU32::new(size.width).unwrap(),
|
||||
std::num::NonZeroU32::new(size.height).unwrap()
|
||||
);
|
||||
}
|
||||
},
|
||||
// TODO to be implemented later
|
||||
_ => ()
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -180,6 +191,8 @@ fn main() {
|
|||
}
|
||||
|
||||
schedule.run(&mut world);
|
||||
|
||||
gui.run(&mut world);
|
||||
gl_surface.swap_buffers(&gl_context).unwrap();
|
||||
},
|
||||
|
||||
|
|
Loading…
Reference in New Issue