Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
Add mouse_input/moved_this_frame
Browse files Browse the repository at this point in the history
  • Loading branch information
darthdeus committed Apr 29, 2024
1 parent ca87707 commit 4bd0bcb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@
- Upgraded `wgpu: 0.18 -> 0.19.3, winit: 0.28 -> 0.29, egui: 0.24 -> 0.26`.
- Made `wgpu::PowerPreference` configurable & default to `None`, with the hope of fixing a potential
issue where users have their dedicated GPU disabled on a dual GPU setup.
- Add option to override `max_texture_dimension_2d` in `GameConfig`, this is useful for games that want to support
- Added option to override `max_texture_dimension_2d` in `GameConfig`, this is useful for games that want to support
resolutions higher than 4K. It's not entirely clear to me whether we can safely just raise this and still work
on all machines, hence why it becomes an optional override. We're doing some more testing on this in our
game, and if it'll be safe we'll also change the default.
- Added `mouse_input_this_frame()` and `mouse_moved_this_frame()` for determining whether the mouse had any input
in the current frame, or whether it was moved.

# v0.3.0

Expand Down
10 changes: 10 additions & 0 deletions comfy-core/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ pub fn screen_height() -> f32 {
GLOBAL_STATE.borrow().screen_size.y
}

/// Returns true if there was any mouse input during the current frame.
pub fn mouse_input_this_frame() -> bool {
GLOBAL_STATE.borrow().mouse_input_this_frame
}

/// Returns true if the mouse was moved during the current frame.
pub fn mouse_moved_this_frame() -> bool {
GLOBAL_STATE.borrow().mouse_moved_this_frame
}

pub fn screenshake(timer: f32, amount: f32) {
let mut camera = main_camera_mut();

Expand Down
4 changes: 3 additions & 1 deletion comfy-core/src/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ pub fn inc_assets_loaded(newly_loaded_count: usize) {
ASSETS_LOADED.fetch_add(newly_loaded_count, Ordering::SeqCst);
}


pub fn frame_time() -> f32 {
f32::from_bits(FRAME_TIME.load(Ordering::SeqCst))
}
Expand Down Expand Up @@ -100,6 +99,9 @@ pub struct GlobalState {
pub mouse_rel: IVec2,
pub mouse_world: Vec2,

pub mouse_moved_this_frame: bool,
pub mouse_input_this_frame: bool,

pub mouse_locked: bool,
pub cursor_hidden: bool,

Expand Down
14 changes: 12 additions & 2 deletions comfy/src/game_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ pub async fn run_comfy_main_async(
global_state.mouse_just_pressed.clear();
global_state.mouse_just_released.clear();
global_state.mouse_wheel = (0.0, 0.0);
global_state.mouse_input_this_frame = false;
global_state.mouse_moved_this_frame = false;

engine
.renderer
Expand Down Expand Up @@ -251,11 +253,15 @@ pub async fn run_comfy_main_async(

match event {
WindowEvent::CursorMoved { position, .. } => {
GLOBAL_STATE.borrow_mut().mouse_position =
let mut global_state = GLOBAL_STATE.borrow_mut();

global_state.mouse_input_this_frame = true;
global_state.mouse_moved_this_frame = true;
global_state.mouse_position =
vec2(position.x as f32, position.y as f32);
}

WindowEvent::MouseInput { state, button, .. } => {
WindowEvent::MouseInput { state, button, .. } => {\
let quad_button = match button {
winit::event::MouseButton::Left => {
MouseButton::Left
Expand All @@ -279,6 +285,8 @@ pub async fn run_comfy_main_async(

let mut global_state = GLOBAL_STATE.borrow_mut();

global_state.mouse_input_this_frame = true;

match state {
ElementState::Pressed => {
global_state
Expand All @@ -305,6 +313,8 @@ pub async fn run_comfy_main_async(
WindowEvent::MouseWheel { delta, .. } => {
let mut global_state = GLOBAL_STATE.borrow_mut();

global_state.mouse_input_this_frame = true;

match delta {
MouseScrollDelta::LineDelta(x, y) => {
global_state.mouse_wheel = (*x, *y);
Expand Down

0 comments on commit 4bd0bcb

Please sign in to comment.