try to update the player colour based on mouse position (currently not working)

This commit is contained in:
Togglebit 2023-11-14 08:51:57 +01:00
parent 1d80f8467c
commit fa56922594
3 changed files with 32 additions and 4 deletions

View file

@ -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,
};
});
}));

View file

@ -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<Px>) {
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<F>(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<Key>,
pub mouse: Mouse,
}
#[derive(Debug, Default)]
pub struct Mouse {
pub pos: Point<Px>,
}
#[derive(Debug)]
struct TickData {
state: Mutex<TickState>,
@ -136,6 +151,7 @@ struct TickState {
keep_running: bool,
frame: usize,
input: InputState,
mouse: Option<Mouse>,
}
fn tick_loop<F>(data: &TickData, mut tick: F)

View file

@ -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:?}"));
}