Skip to content

Commit

Permalink
feat: handle mouse wheel events (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
suspistew committed Jan 21, 2025
1 parent 0798177 commit d24c330
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/core/components/maths/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ impl Transform {
self.scale = scale
}


/// Change the scale value to a new one.
pub fn get_scale(&self) -> f32{
self.scale
}

/// Change the z value in the local translation coordinates.
pub fn set_z(&mut self, z: usize) {
let z_diff = self.global_translation.z - self.local_translation.z;
Expand Down
4 changes: 4 additions & 0 deletions src/core/resources/inputs/inputs_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ impl InputsController {
self.mouse.add_click_event(event);
}

pub(crate) fn reset_mouse_wheel(&mut self) {
self.mouse.reset_mouse_wheel();
}

pub(crate) fn add_keyboard_event(&mut self, event: KeyboardEvent) {
self.keyboard.add_keyboard_event(event);
}
Expand Down
9 changes: 9 additions & 0 deletions src/core/resources/inputs/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ impl Mouse {
self.click_events.push(event);
}

pub(crate) fn reset_mouse_wheel(&mut self) {
if self.buttons_pressed.contains(&MouseButton::WheelUp) {
self.buttons_pressed.remove(&MouseButton::WheelUp);
}
if self.buttons_pressed.contains(&MouseButton::WheelDown) {
self.buttons_pressed.remove(&MouseButton::WheelDown);
}
}

pub(crate) fn clear_events(&mut self) {
self.click_events.clear();
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/resources/inputs/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub enum MouseButton {
Middle,
Back,
Forward,
WheelUp,
WheelDown,
Other(u16),
}

Expand Down
3 changes: 3 additions & 0 deletions src/graphics/components/tiles/tilemap.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{collections::HashMap, ops::Range};

use hecs::Entity;
use log::info;
use serde::{Deserialize, Serialize};
use wgpu::{util::BufferInitDescriptor, PrimitiveTopology};

Expand Down Expand Up @@ -218,6 +219,8 @@ impl Tilemap {
let entry = world.entry_mut::<&mut Sprite>(tile);
if let Ok(sprite) = entry {
sprite.set_tile_nb(new_tile_nb);
} else {
let _r = world.add_components(tile, (Sprite::new(new_tile_nb),));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ fn prepare_buffer_update_for_tilemap(renderer: &mut Scion2DPreRenderer, data: &m
let mut offset_y = 0.;
let mut offset_z = 0;

/*
x 20
y 10
*/
if isometric {
offset_x = -1. * tile.position.x() as f32 * t.offset_x_multiplier_x() + tile.position.y() as f32 * t.offset_x_multiplier_y() - (tile.position.z() as f32 * t.offset_x_multiplier_z());
offset_y = -1. * (tile.position.y() as f32 * t.offset_y_multiplier_y() + tile.position.x() as f32 * t.offset_y_multiplier_x()) - (tile.position.z() as f32 * t.offset_y_multiplier_z());
Expand Down
17 changes: 16 additions & 1 deletion src/graphics/windowing/window_event_handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::core::components::maths::camera::Camera;
use winit::event::WindowEvent;
use winit::event::{MouseScrollDelta, WindowEvent};
use winit::keyboard::Key;

use crate::core::resources::inputs::mouse::MouseEvent;
Expand Down Expand Up @@ -53,6 +53,21 @@ pub fn handle_window_event(runner: &mut ScionRunner) -> Vec<RendererEvent> {
};
runner.game_data.inputs().add_click_event(m_event);
}
WindowEvent::MouseWheel { delta, .. } => {

match delta {
MouseScrollDelta::LineDelta(_x, y) => {
if y > 0. {
runner.game_data.inputs().add_click_event(MouseEvent{ button: MouseButton::WheelUp, state: InputState::Pressed });
} else if y < 0. {
runner.game_data.inputs().add_click_event(MouseEvent{ button: MouseButton::WheelDown, state: InputState::Pressed });
} else {
runner.game_data.inputs().reset_mouse_wheel();
}
}
MouseScrollDelta::PixelDelta(_) => {}
}
}
WindowEvent::CursorMoved { device_id: _, position, .. } => {
let dpi_factor = runner
.window
Expand Down

0 comments on commit d24c330

Please sign in to comment.