refactor events; watch prep work
This commit is contained in:
@@ -19,7 +19,10 @@ use vulkano::{
|
||||
sync::GpuFuture,
|
||||
};
|
||||
use wgui::{
|
||||
event::{MouseButton, MouseDownEvent, MouseMotionEvent, MouseUpEvent, MouseWheelEvent},
|
||||
event::{
|
||||
EventListenerCollection, MouseButton, MouseDownEvent, MouseMotionEvent, MouseUpEvent,
|
||||
MouseWheelEvent,
|
||||
},
|
||||
gfx::WGfx,
|
||||
renderer_vk::{self},
|
||||
};
|
||||
@@ -29,7 +32,7 @@ use winit::{
|
||||
keyboard::{KeyCode, PhysicalKey},
|
||||
};
|
||||
|
||||
use crate::testbed::{testbed_dashboard::TestbedDashboard, testbed_generic::TestbedGeneric};
|
||||
use crate::testbed::testbed_generic::TestbedGeneric;
|
||||
|
||||
mod assets;
|
||||
mod profiler;
|
||||
@@ -53,11 +56,12 @@ fn init_logging() {
|
||||
.init();
|
||||
}
|
||||
|
||||
fn load_testbed() -> anyhow::Result<Box<dyn Testbed>> {
|
||||
fn load_testbed(
|
||||
listeners: &mut EventListenerCollection<(), ()>,
|
||||
) -> anyhow::Result<Box<dyn Testbed>> {
|
||||
let name = std::env::var("TESTBED").unwrap_or_default();
|
||||
Ok(match name.as_str() {
|
||||
"dashboard" => Box::new(TestbedDashboard::new()?),
|
||||
"" => Box::new(TestbedGeneric::new()?),
|
||||
"" => Box::new(TestbedGeneric::new(listeners)?),
|
||||
_ => Box::new(TestbedAny::new(&name)?),
|
||||
})
|
||||
}
|
||||
@@ -92,7 +96,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
let mut scale = window.scale_factor() as f32;
|
||||
|
||||
let mut testbed = load_testbed()?;
|
||||
let mut listeners = EventListenerCollection::default();
|
||||
|
||||
let mut testbed = load_testbed(&mut listeners)?;
|
||||
|
||||
let mut mouse = Vec2::ZERO;
|
||||
|
||||
@@ -119,19 +125,27 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
} => match delta {
|
||||
MouseScrollDelta::LineDelta(x, y) => testbed
|
||||
.layout()
|
||||
.push_event(&wgui::event::Event::MouseWheel(MouseWheelEvent {
|
||||
shift: Vec2::new(x, y),
|
||||
pos: mouse / scale,
|
||||
device: 0,
|
||||
}))
|
||||
.push_event(
|
||||
&listeners,
|
||||
&wgui::event::Event::MouseWheel(MouseWheelEvent {
|
||||
shift: Vec2::new(x, y),
|
||||
pos: mouse / scale,
|
||||
device: 0,
|
||||
}),
|
||||
(&mut (), &mut ()),
|
||||
)
|
||||
.unwrap(),
|
||||
MouseScrollDelta::PixelDelta(pos) => testbed
|
||||
.layout()
|
||||
.push_event(&wgui::event::Event::MouseWheel(MouseWheelEvent {
|
||||
shift: Vec2::new(pos.x as f32 / 5.0, pos.y as f32 / 5.0),
|
||||
pos: mouse / scale,
|
||||
device: 0,
|
||||
}))
|
||||
.push_event(
|
||||
&listeners,
|
||||
&wgui::event::Event::MouseWheel(MouseWheelEvent {
|
||||
shift: Vec2::new(pos.x as f32 / 5.0, pos.y as f32 / 5.0),
|
||||
pos: mouse / scale,
|
||||
device: 0,
|
||||
}),
|
||||
(&mut (), &mut ()),
|
||||
)
|
||||
.unwrap(),
|
||||
},
|
||||
Event::WindowEvent {
|
||||
@@ -142,20 +156,28 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
if matches!(state, winit::event::ElementState::Pressed) {
|
||||
testbed
|
||||
.layout()
|
||||
.push_event(&wgui::event::Event::MouseDown(MouseDownEvent {
|
||||
pos: mouse / scale,
|
||||
button: MouseButton::Left,
|
||||
device: 0,
|
||||
}))
|
||||
.push_event(
|
||||
&listeners,
|
||||
&wgui::event::Event::MouseDown(MouseDownEvent {
|
||||
pos: mouse / scale,
|
||||
button: MouseButton::Left,
|
||||
device: 0,
|
||||
}),
|
||||
(&mut (), &mut ()),
|
||||
)
|
||||
.unwrap();
|
||||
} else {
|
||||
testbed
|
||||
.layout()
|
||||
.push_event(&wgui::event::Event::MouseUp(MouseUpEvent {
|
||||
pos: mouse / scale,
|
||||
button: MouseButton::Left,
|
||||
device: 0,
|
||||
}))
|
||||
.push_event(
|
||||
&listeners,
|
||||
&wgui::event::Event::MouseUp(MouseUpEvent {
|
||||
pos: mouse / scale,
|
||||
button: MouseButton::Left,
|
||||
device: 0,
|
||||
}),
|
||||
(&mut (), &mut ()),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
@@ -167,10 +189,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
mouse = vec2(position.x as _, position.y as _);
|
||||
testbed
|
||||
.layout()
|
||||
.push_event(&wgui::event::Event::MouseMotion(MouseMotionEvent {
|
||||
pos: mouse / scale,
|
||||
device: 0,
|
||||
}))
|
||||
.push_event(
|
||||
&listeners,
|
||||
&wgui::event::Event::MouseMotion(MouseMotionEvent {
|
||||
pos: mouse / scale,
|
||||
device: 0,
|
||||
}),
|
||||
(&mut (), &mut ()),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
Event::WindowEvent {
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
use wgui::layout::Layout;
|
||||
|
||||
pub mod testbed_any;
|
||||
pub mod testbed_dashboard;
|
||||
pub mod testbed_generic;
|
||||
|
||||
pub trait Testbed {
|
||||
fn update(&mut self, width: f32, height: f32, timestep_alpha: f32) -> anyhow::Result<()>;
|
||||
fn layout(&mut self) -> &mut Layout;
|
||||
fn update(&mut self, width: f32, height: f32, timestep_alpha: f32) -> anyhow::Result<()>;
|
||||
fn layout(&mut self) -> &mut Layout;
|
||||
}
|
||||
|
||||
@@ -3,26 +3,26 @@ use glam::Vec2;
|
||||
use wgui::layout::Layout;
|
||||
|
||||
pub struct TestbedAny {
|
||||
pub layout: Layout,
|
||||
pub layout: Layout,
|
||||
}
|
||||
|
||||
impl TestbedAny {
|
||||
pub fn new(name: &str) -> anyhow::Result<Self> {
|
||||
let path = format!("gui/{name}.xml");
|
||||
let (layout, _state) = wgui::parser::new_layout_from_assets(Box::new(assets::Asset {}), &path)?;
|
||||
Ok(Self { layout })
|
||||
}
|
||||
pub fn new(name: &str) -> anyhow::Result<Self> {
|
||||
let path = format!("gui/{name}.xml");
|
||||
let (layout, _state) =
|
||||
wgui::parser::new_layout_from_assets(Box::new(assets::Asset {}), &path)?;
|
||||
Ok(Self { layout })
|
||||
}
|
||||
}
|
||||
|
||||
impl Testbed for TestbedAny {
|
||||
fn update(&mut self, width: f32, height: f32, timestep_alpha: f32) -> anyhow::Result<()> {
|
||||
self
|
||||
.layout
|
||||
.update(Vec2::new(width, height), timestep_alpha)?;
|
||||
Ok(())
|
||||
}
|
||||
fn update(&mut self, width: f32, height: f32, timestep_alpha: f32) -> anyhow::Result<()> {
|
||||
self.layout
|
||||
.update(Vec2::new(width, height), timestep_alpha)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn layout(&mut self) -> &mut Layout {
|
||||
&mut self.layout
|
||||
}
|
||||
fn layout(&mut self) -> &mut Layout {
|
||||
&mut self.layout
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
use crate::{assets, testbed::Testbed};
|
||||
use glam::Vec2;
|
||||
use wgui::layout::Layout;
|
||||
|
||||
pub struct TestbedDashboard {
|
||||
pub layout: Layout,
|
||||
}
|
||||
|
||||
impl TestbedDashboard {
|
||||
pub fn new() -> anyhow::Result<Self> {
|
||||
const XML_PATH: &str = "gui/dashboard.xml";
|
||||
let (layout, _state) =
|
||||
wgui::parser::new_layout_from_assets(Box::new(assets::Asset {}), XML_PATH)?;
|
||||
Ok(Self { layout })
|
||||
}
|
||||
}
|
||||
|
||||
impl Testbed for TestbedDashboard {
|
||||
fn update(&mut self, width: f32, height: f32, timestep_alpha: f32) -> anyhow::Result<()> {
|
||||
self
|
||||
.layout
|
||||
.update(Vec2::new(width, height), timestep_alpha)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn layout(&mut self) -> &mut Layout {
|
||||
&mut self.layout
|
||||
}
|
||||
}
|
||||
@@ -2,98 +2,98 @@ use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
use glam::{Mat4, Vec2};
|
||||
use wgui::{
|
||||
drawing::{self},
|
||||
event::EventListener,
|
||||
layout::{Layout, WidgetID},
|
||||
renderer_vk::text::TextStyle,
|
||||
drawing::{self},
|
||||
event::{EventListenerCollection, EventListenerKind},
|
||||
layout::{Layout, WidgetID},
|
||||
renderer_vk::text::TextStyle,
|
||||
};
|
||||
|
||||
use crate::{assets, testbed::Testbed};
|
||||
|
||||
pub struct TestbedGeneric {
|
||||
pub layout: Layout,
|
||||
rot: f32,
|
||||
widget_id: Rc<RefCell<Option<WidgetID>>>,
|
||||
pub layout: Layout,
|
||||
rot: f32,
|
||||
widget_id: Rc<RefCell<Option<WidgetID>>>,
|
||||
}
|
||||
|
||||
impl TestbedGeneric {
|
||||
pub fn new() -> anyhow::Result<Self> {
|
||||
const XML_PATH: &str = "gui/testbed.xml";
|
||||
pub fn new(listeners: &mut EventListenerCollection<(), ()>) -> anyhow::Result<Self> {
|
||||
const XML_PATH: &str = "gui/testbed.xml";
|
||||
|
||||
let (mut layout, res) =
|
||||
wgui::parser::new_layout_from_assets(Box::new(assets::Asset {}), XML_PATH)?;
|
||||
let (mut layout, res) =
|
||||
wgui::parser::new_layout_from_assets(Box::new(assets::Asset {}), XML_PATH)?;
|
||||
|
||||
use wgui::components::button;
|
||||
let my_div_parent = res.require_by_id("my_div_parent")?;
|
||||
// create some buttons for testing
|
||||
for i in 0..4 {
|
||||
let n = i as f32 / 4.0;
|
||||
button::construct(
|
||||
&mut layout,
|
||||
my_div_parent,
|
||||
button::Params {
|
||||
text: "I'm a button!",
|
||||
color: drawing::Color::new(1.0 - n, n * n, n, 1.0),
|
||||
..Default::default()
|
||||
},
|
||||
)?;
|
||||
}
|
||||
use wgui::components::button;
|
||||
let my_div_parent = res.require_by_id("my_div_parent")?;
|
||||
// create some buttons for testing
|
||||
for i in 0..4 {
|
||||
let n = i as f32 / 4.0;
|
||||
button::construct(
|
||||
&mut layout,
|
||||
my_div_parent,
|
||||
button::Params {
|
||||
text: "I'm a button!",
|
||||
color: drawing::Color::new(1.0 - n, n * n, n, 1.0),
|
||||
..Default::default()
|
||||
},
|
||||
)?;
|
||||
}
|
||||
|
||||
let button = button::construct(
|
||||
&mut layout,
|
||||
my_div_parent,
|
||||
button::Params {
|
||||
text: "Click me!!",
|
||||
color: drawing::Color::new(0.2, 0.2, 0.2, 1.0),
|
||||
size: Vec2::new(256.0, 64.0),
|
||||
text_style: TextStyle {
|
||||
size: Some(30.0),
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
)?;
|
||||
let button = button::construct(
|
||||
&mut layout,
|
||||
my_div_parent,
|
||||
button::Params {
|
||||
text: "Click me!!",
|
||||
color: drawing::Color::new(0.2, 0.2, 0.2, 1.0),
|
||||
size: Vec2::new(256.0, 64.0),
|
||||
text_style: TextStyle {
|
||||
size: Some(30.0),
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
)?;
|
||||
|
||||
let widget_id = Rc::new(RefCell::new(None));
|
||||
let widget_id = Rc::new(RefCell::new(None));
|
||||
|
||||
let wid = widget_id.clone();
|
||||
layout.add_event_listener(
|
||||
button.body,
|
||||
EventListener::MouseRelease(Box::new(move |data, _| {
|
||||
button.set_text(data, "Congratulations!");
|
||||
*wid.borrow_mut() = Some(data.widget_id);
|
||||
})),
|
||||
);
|
||||
let wid = widget_id.clone();
|
||||
listeners.add(
|
||||
button.body,
|
||||
EventListenerKind::MouseRelease,
|
||||
Box::new(move |data, (), ()| {
|
||||
button.set_text(data, "Congratulations!");
|
||||
*wid.borrow_mut() = Some(data.widget_id);
|
||||
}),
|
||||
);
|
||||
|
||||
Ok(Self {
|
||||
layout,
|
||||
rot: 0.0,
|
||||
widget_id,
|
||||
})
|
||||
}
|
||||
Ok(Self {
|
||||
layout,
|
||||
rot: 0.0,
|
||||
widget_id,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Testbed for TestbedGeneric {
|
||||
fn update(&mut self, width: f32, height: f32, timestep_alpha: f32) -> anyhow::Result<()> {
|
||||
if let Some(widget_id) = *self.widget_id.borrow() {
|
||||
self.rot += 0.01;
|
||||
fn update(&mut self, width: f32, height: f32, timestep_alpha: f32) -> anyhow::Result<()> {
|
||||
if let Some(widget_id) = *self.widget_id.borrow() {
|
||||
self.rot += 0.01;
|
||||
|
||||
let a = self.layout.widget_map.get(widget_id).unwrap();
|
||||
let mut widget = a.lock().unwrap();
|
||||
widget.data.transform = Mat4::IDENTITY
|
||||
* Mat4::from_rotation_y(-self.rot)
|
||||
* Mat4::from_rotation_x(self.rot * 0.25)
|
||||
* Mat4::from_rotation_z(-self.rot * 0.1);
|
||||
let a = self.layout.widget_map.get(widget_id).unwrap();
|
||||
let mut widget = a.lock().unwrap();
|
||||
widget.data.transform = Mat4::IDENTITY
|
||||
* Mat4::from_rotation_y(-self.rot)
|
||||
* Mat4::from_rotation_x(self.rot * 0.25)
|
||||
* Mat4::from_rotation_z(-self.rot * 0.1);
|
||||
|
||||
self.layout.needs_redraw = true;
|
||||
}
|
||||
self.layout.needs_redraw = true;
|
||||
}
|
||||
|
||||
self
|
||||
.layout
|
||||
.update(Vec2::new(width, height), timestep_alpha)?;
|
||||
Ok(())
|
||||
}
|
||||
self.layout
|
||||
.update(Vec2::new(width, height), timestep_alpha)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn layout(&mut self) -> &mut Layout {
|
||||
&mut self.layout
|
||||
}
|
||||
}
|
||||
fn layout(&mut self) -> &mut Layout {
|
||||
&mut self.layout
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user