Skip to content

Commit

Permalink
try to update the player colour based on mouse position (currently no…
Browse files Browse the repository at this point in the history
…t working)
  • Loading branch information
togglebyte committed Nov 14, 2023
1 parent 1d80f84 commit fa56922
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
16 changes: 12 additions & 4 deletions examples/tilemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
};
});
}));

Expand Down
16 changes: 16 additions & 0 deletions src/tick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -62,6 +69,7 @@ impl Tick {
keep_running: true,
frame: 0,
input: InputState::default(),
mouse: None,
}),
period: tick_every,
sync: Condvar::new(),
Expand Down Expand Up @@ -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>,
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/widgets/tilemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}"));
}

Expand Down

0 comments on commit fa56922

Please sign in to comment.