SENSORS
This commit is contained in:
1 parent
c5aa0a02e2
commit
78ea738b8e
8 files changed
+222
-52
No files matched your search
+7
-4
@@ -1,3 +1,4 @@
|
||||
use gui::Ui;
|
||||
use winit::{
|
||||
application::ApplicationHandler,
|
||||
event::WindowEvent,
|
||||
@@ -9,7 +10,7 @@ use super::Client;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct App {
|
||||
client: Option<Client>,
|
||||
client: Option<(Client, Ui<Client>)>,
|
||||
}
|
||||
|
||||
impl App {
|
||||
@@ -25,12 +26,14 @@ impl ApplicationHandler for App {
|
||||
let window = event_loop
|
||||
.create_window(Window::default_attributes())
|
||||
.unwrap();
|
||||
let client = Client::new(window.into());
|
||||
self.client = Some(client);
|
||||
let (ui, ids) = Client::create_ui();
|
||||
let client = Client::new(window.into(), ids);
|
||||
self.client = Some((client, ui));
|
||||
}
|
||||
}
|
||||
|
||||
fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
|
||||
self.client.as_mut().unwrap().event(event, event_loop);
|
||||
let (client, ui) = self.client.as_mut().unwrap();
|
||||
client.event(event, event_loop, ui);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
use gui::{SenseCtx, SenseTrigger, Vec2};
|
||||
use winit::event::WindowEvent;
|
||||
|
||||
use crate::testing::Client;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Input {
|
||||
size: Vec2,
|
||||
mouse_pos: Vec2,
|
||||
mouse_pressed: bool,
|
||||
mouse_just_pressed: bool,
|
||||
}
|
||||
|
||||
impl Input {
|
||||
pub fn event(&mut self, event: &WindowEvent) {
|
||||
self.mouse_just_pressed = false;
|
||||
match event {
|
||||
WindowEvent::Resized(size) => {
|
||||
self.size = Vec2::new(size.width as f32, size.height as f32)
|
||||
}
|
||||
WindowEvent::CursorMoved { position, .. } => {
|
||||
self.mouse_pos = Vec2::new(position.x as f32, position.y as f32)
|
||||
}
|
||||
WindowEvent::MouseInput { state, button, .. } => match button {
|
||||
winit::event::MouseButton::Left => {
|
||||
if state.is_pressed() {
|
||||
if !self.mouse_pressed {
|
||||
self.mouse_just_pressed = true;
|
||||
} else {
|
||||
self.mouse_just_pressed = false;
|
||||
}
|
||||
self.mouse_pressed = true;
|
||||
} else {
|
||||
self.mouse_pressed = false;
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
fn active(&mut self, trigger: &SenseTrigger) -> bool {
|
||||
let region = trigger.shape.to_screen(self.size);
|
||||
if !region.contains(self.mouse_pos) {
|
||||
return false;
|
||||
}
|
||||
match trigger.sense {
|
||||
gui::Sense::Click => self.mouse_just_pressed,
|
||||
gui::Sense::Hover => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SenseCtx for Client {
|
||||
fn active(&mut self, trigger: &SenseTrigger) -> bool {
|
||||
self.input.active(trigger)
|
||||
}
|
||||
}
|
||||
+32
-20
@@ -5,39 +5,44 @@ use gui::*;
|
||||
use render::Renderer;
|
||||
use winit::{event::WindowEvent, event_loop::ActiveEventLoop, window::Window};
|
||||
|
||||
use crate::testing::input::Input;
|
||||
|
||||
mod app;
|
||||
mod input;
|
||||
mod render;
|
||||
|
||||
pub fn main() {
|
||||
App::run();
|
||||
}
|
||||
|
||||
struct Data {
|
||||
x: u32,
|
||||
}
|
||||
|
||||
pub struct Client {
|
||||
renderer: Renderer,
|
||||
ui: Ui<Data>,
|
||||
input: Input,
|
||||
ui: UiIds,
|
||||
}
|
||||
|
||||
pub struct UiIds {
|
||||
test: WidgetId<Span>,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub fn new(window: Arc<Window>) -> Self {
|
||||
let renderer = Renderer::new(window);
|
||||
pub fn create_ui() -> (Ui<Self>, UiIds) {
|
||||
let mut ui = Ui::new();
|
||||
let test = ui.id();
|
||||
let rect = Rect {
|
||||
color: UiColor::WHITE,
|
||||
radius: 20.0,
|
||||
thickness: 0.0,
|
||||
inner_radius: 0.0,
|
||||
};
|
||||
let mut ui = Ui::new();
|
||||
let test = ui.id();
|
||||
ui.set_base(
|
||||
(
|
||||
(
|
||||
rect.color(UiColor::BLUE)
|
||||
.sense(|d: &mut Data| println!("{}", d.x)),
|
||||
.sense(Sense::Click, |id, ui, client| {
|
||||
println!("hello!");
|
||||
ui[id].color.a -= 1;
|
||||
}),
|
||||
(
|
||||
rect.color(UiColor::RED).center((100.0, 100.0)),
|
||||
(
|
||||
@@ -75,26 +80,33 @@ impl Client {
|
||||
.span(Dir::DOWN, [3, 1, 1])
|
||||
.pad(10),
|
||||
);
|
||||
Self { renderer, ui, test }
|
||||
(ui, UiIds { test })
|
||||
}
|
||||
|
||||
pub fn event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop) {
|
||||
pub fn new(window: Arc<Window>, ui: UiIds) -> Self {
|
||||
let renderer = Renderer::new(window);
|
||||
Self {
|
||||
renderer,
|
||||
ui,
|
||||
input: Input::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop, ui: &mut Ui<Self>) {
|
||||
self.input.event(&event);
|
||||
ui.run_sensors(self);
|
||||
match event {
|
||||
WindowEvent::CloseRequested => event_loop.exit(),
|
||||
WindowEvent::RedrawRequested => {
|
||||
let primitives = self.ui.update(&mut Data { x: 39 });
|
||||
let primitives = ui.update(self);
|
||||
self.renderer.update(primitives);
|
||||
self.renderer.draw()
|
||||
}
|
||||
WindowEvent::Resized(size) => self.renderer.resize(&size),
|
||||
WindowEvent::KeyboardInput { event, .. } => {
|
||||
if event.state.is_pressed() {
|
||||
let child = self.ui.add(Rect::new(Color::YELLOW)).erase_type();
|
||||
self.ui[&self.test].children.push((child, fixed(20.0)));
|
||||
self.renderer.window().request_redraw();
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
if ui.needs_redraw() {
|
||||
self.renderer.window().request_redraw();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user