diff --git a/examples/tilemap.rs b/examples/tilemap.rs index 79a5ee4..7dbfec1 100644 --- a/examples/tilemap.rs +++ b/examples/tilemap.rs @@ -46,8 +46,7 @@ fn main() -> gooey::Result { id: myself, }) .tick(Tick::times_per_second(60, move |elapsed, input| { - // get mouse cursor position and subsequently get the object under - // the mouse + // get mouse cursor position and subsequently get the object under the cursor let mut direction = Point::new(0., 0.); if input.keys.contains(&Key::ArrowDown) { @@ -65,11 +64,20 @@ fn main() -> gooey::Result { let one_second_movement = direction * TILE_SIZE.0 as f32; + let cursor_pos = input.mouse.pos; + layers.map_mut(|layers| { - layers.1[myself].position += Point::new( + let pos = &mut layers.1[myself].position; + *pos += Point::new( one_second_movement.x * elapsed.as_secs_f32(), one_second_movement.y * elapsed.as_secs_f32(), - ) + ); + + let rect = Rect::new(*pos, Size::new(16., 16.)); + let color = match rect.cast().contains(cursor_pos) { + true => Color::RED, + false => Color::BLUE, + }; }); })); diff --git a/src/tick.rs b/src/tick.rs index d57ed02..b1978da 100644 --- a/src/tick.rs +++ b/src/tick.rs @@ -5,6 +5,8 @@ use std::time::{Duration, Instant}; use kludgine::app::winit::event::KeyEvent; use kludgine::app::winit::keyboard::Key; +use kludgine::figures::Point; +use kludgine::figures::units::Px; use crate::context::WidgetContext; use crate::value::Dynamic; @@ -48,6 +50,11 @@ impl Tick { } } + pub fn set_cursor_position(&self, pos: Point) { + let mut state = self.data.state(); + state.input.mouse.pos = pos; + } + /// Returns a new tick that invokes `tick`, aiming to repeat at the given /// duration. pub fn new(tick_every: Duration, tick: F) -> Self @@ -62,6 +69,7 @@ impl Tick { keep_running: true, frame: 0, input: InputState::default(), + mouse: None, }), period: tick_every, sync: Condvar::new(), @@ -110,8 +118,15 @@ impl Tick { pub struct InputState { /// A collection of all keys currently pressed. pub keys: HashSet, + pub mouse: Mouse, } +#[derive(Debug, Default)] +pub struct Mouse { + pub pos: Point, +} + + #[derive(Debug)] struct TickData { state: Mutex, @@ -136,6 +151,7 @@ struct TickState { keep_running: bool, frame: usize, input: InputState, + mouse: Option, } fn tick_loop(data: &TickData, mut tick: F) diff --git a/src/widgets/tilemap.rs b/src/widgets/tilemap.rs index adf75af..f662e7d 100644 --- a/src/widgets/tilemap.rs +++ b/src/widgets/tilemap.rs @@ -135,6 +135,10 @@ where let zoom = self.zoom; let world = tilemap::translate_coordinates(local, offset, scale, zoom, size); + if let Some(tick) = &self.tick { + tick.set_cursor_position(world); + } + self.debug_output.as_ref().unwrap().set(format!("world: {world:?} | local: {local:?}")); }