sense specific buttons
This commit is contained in:
1 parent
21aa2b3501
commit
b48acccb8d
8 files changed
+167
-107
No files matched your search
+22
-17
@@ -1,31 +1,40 @@
|
||||
use ui::layout::{CursorState, Vec2};
|
||||
use winit::event::WindowEvent;
|
||||
use winit::event::{MouseButton, WindowEvent};
|
||||
|
||||
use crate::testing::Client;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Input {
|
||||
mouse_pos: Vec2,
|
||||
mouse_pressed: bool,
|
||||
mouse_exists: bool,
|
||||
cursor: CursorState,
|
||||
}
|
||||
|
||||
impl Input {
|
||||
pub fn event(&mut self, event: &WindowEvent) {
|
||||
match event {
|
||||
WindowEvent::CursorMoved { position, .. } => {
|
||||
self.mouse_pos = Vec2::new(position.x as f32, position.y as f32);
|
||||
self.mouse_exists = true;
|
||||
self.cursor.pos = Vec2::new(position.x as f32, position.y as f32);
|
||||
self.cursor.exists = true;
|
||||
}
|
||||
WindowEvent::MouseInput { state, button, .. } => match button {
|
||||
winit::event::MouseButton::Left => {
|
||||
self.mouse_pressed = state.is_pressed();
|
||||
WindowEvent::MouseInput { state, button, .. } => {
|
||||
let buttons = &mut self.cursor.buttons;
|
||||
let pressed = state.is_pressed();
|
||||
match button {
|
||||
MouseButton::Left => buttons.left.update(pressed),
|
||||
MouseButton::Right => buttons.right.update(pressed),
|
||||
MouseButton::Middle => buttons.middle.update(pressed),
|
||||
_ => (),
|
||||
}
|
||||
_ => (),
|
||||
},
|
||||
}
|
||||
WindowEvent::CursorLeft { .. } => {
|
||||
self.cursor.exists = false;
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn end_frame(&mut self) {
|
||||
self.cursor.buttons.end_frame();
|
||||
}
|
||||
}
|
||||
|
||||
impl Client {
|
||||
@@ -34,11 +43,7 @@ impl Client {
|
||||
(size.width, size.height).into()
|
||||
}
|
||||
|
||||
pub fn cursor_state(&self) -> CursorState {
|
||||
CursorState {
|
||||
pos: self.input.mouse_pos,
|
||||
exists: self.input.mouse_exists,
|
||||
pressed: self.input.mouse_pressed,
|
||||
}
|
||||
pub fn cursor_state(&self) -> &CursorState {
|
||||
&self.input.cursor
|
||||
}
|
||||
}
|
||||
+8
-8
@@ -3,7 +3,6 @@ use std::sync::Arc;
|
||||
use app::App;
|
||||
use cosmic_text::Family;
|
||||
use render::Renderer;
|
||||
use senses::*;
|
||||
use ui::prelude::*;
|
||||
use winit::{event::WindowEvent, event_loop::ActiveEventLoop, window::Window};
|
||||
|
||||
@@ -73,7 +72,7 @@ impl Client {
|
||||
|
||||
let add_button = rect(Color::LIME)
|
||||
.radius(30)
|
||||
.on(PRESS_START, move |ctx: &mut Client, _| {
|
||||
.on(Sense::click(), move |ctx: &mut Client, _| {
|
||||
let child = ctx
|
||||
.ui
|
||||
.add(image(include_bytes!("assets/sungals.png")).center())
|
||||
@@ -85,7 +84,7 @@ impl Client {
|
||||
|
||||
let del_button = rect(Color::RED)
|
||||
.radius(30)
|
||||
.on(PRESS_START, move |ctx: &mut Client, _| {
|
||||
.on(Sense::click(), move |ctx: &mut Client, _| {
|
||||
ctx.ui[span_add].children.pop();
|
||||
})
|
||||
.size(150)
|
||||
@@ -124,7 +123,7 @@ impl Client {
|
||||
texts,
|
||||
text_edit("add")
|
||||
.font_size(30)
|
||||
.id_on(PRESS_START, |id, client: &mut Client, ctx| {
|
||||
.id_on(Sense::click(), |id, client: &mut Client, ctx| {
|
||||
client.ui.text(id).select(ctx.cursor, ctx.size);
|
||||
client.selected = Some(id.clone());
|
||||
})
|
||||
@@ -135,14 +134,14 @@ impl Client {
|
||||
|
||||
let switch_button = |color, to, label| {
|
||||
let rect = rect(color)
|
||||
.id_on(PRESS_START, move |id, ctx: &mut Client, _| {
|
||||
.id_on(Sense::click(), move |id, ctx: &mut Client, _| {
|
||||
ctx.ui[main].inner.set_static(to);
|
||||
ctx.ui[id].color = color.add_rgb(-0.2);
|
||||
})
|
||||
.edit_on(HOVER_START | PRESS_END, move |r, _| {
|
||||
.edit_on(Sense::HoverStart | Sense::unclick(), move |r, _| {
|
||||
r.color = color.add_rgb(0.4);
|
||||
})
|
||||
.edit_on(HOVER_END, move |r, _| {
|
||||
.edit_on(Sense::HoverEnd, move |r, _| {
|
||||
r.color = color;
|
||||
});
|
||||
(rect, text(label).font_size(30)).stack()
|
||||
@@ -185,7 +184,7 @@ impl Client {
|
||||
|
||||
pub fn event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop) {
|
||||
self.input.event(&event);
|
||||
let cursor_state = self.cursor_state();
|
||||
let cursor_state = self.cursor_state().clone();
|
||||
let window_size = self.window_size();
|
||||
run_sensors(self, &cursor_state, window_size);
|
||||
match event {
|
||||
@@ -221,6 +220,7 @@ impl Client {
|
||||
if self.ui.needs_redraw() {
|
||||
self.renderer.window().request_redraw();
|
||||
}
|
||||
self.input.end_frame();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user