Skip to content

Commit 2c15149

Browse files
authored
Merge pull request #12 from LykenSol/mouse
Clean up mouse support.
2 parents 9d0ba8f + a1d1556 commit 2c15149

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

shaders/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,20 @@ pub fn fs(constants: &ShaderConstants, mut frag_coord: Vec2) -> Vec4 {
6161
);
6262
let time = constants.time;
6363
let mouse = vec4(
64-
constants.last_lmb_down_x / COLS as f32,
65-
constants.last_lmb_down_y / ROWS as f32,
66-
constants.last_click_x / COLS as f32
64+
constants.drag_end_x / COLS as f32,
65+
constants.drag_end_y / ROWS as f32,
66+
constants.drag_start_x / COLS as f32
6767
* if constants.mouse_left_pressed {
6868
1.0
6969
} else {
7070
-1.0
7171
},
72-
constants.last_click_y / ROWS as f32 * if constants.mouse_clicked { 1.0 } else { -1.0 },
72+
constants.drag_start_y / ROWS as f32
73+
* if constants.mouse_left_clicked {
74+
1.0
75+
} else {
76+
-1.0
77+
},
7378
);
7479

7580
let col = (frag_coord.x / resolution.x) as usize;

shared/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ pub struct ShaderConstants {
2121
pub width: u32,
2222
pub height: u32,
2323
pub time: f32,
24-
pub last_lmb_down_x: f32,
25-
pub last_lmb_down_y: f32,
26-
pub last_click_x: f32,
27-
pub last_click_y: f32,
24+
pub cursor_x: f32,
25+
pub cursor_y: f32,
26+
pub drag_start_x: f32,
27+
pub drag_start_y: f32,
28+
pub drag_end_x: f32,
29+
pub drag_end_y: f32,
2830
pub mouse_left_pressed: bool,
29-
pub mouse_clicked: bool,
31+
pub mouse_left_clicked: bool,
3032
}
3133

3234
pub fn saturate(x: f32) -> f32 {

src/main.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ async fn run(
101101
.map(|surface| device.create_swap_chain(&surface, &sc_desc));
102102

103103
let start = std::time::Instant::now();
104-
let mut mouse_left_pressed = false;
105104
let (mut cursor_x, mut cursor_y) = (0.0, 0.0);
106-
let (mut last_lmb_down_x, mut last_lmb_down_y) = (0.0, 0.0);
107-
let mut mouse_clicked = false;
108-
let (mut last_click_x, mut last_click_y) = (0.0, 0.0);
105+
let (mut drag_start_x, mut drag_start_y) = (0.0, 0.0);
106+
let (mut drag_end_x, mut drag_end_y) = (0.0, 0.0);
107+
let mut mouse_left_pressed = false;
108+
let mut mouse_left_clicked = false;
109109

110110
event_loop.run(move |event, _, control_flow| {
111111
// Have the closure take ownership of the resources.
@@ -162,14 +162,16 @@ async fn run(
162162
width: window.inner_size().width,
163163
height: window.inner_size().height,
164164
time: start.elapsed().as_secs_f32(),
165-
last_lmb_down_x,
166-
last_lmb_down_y,
167-
last_click_x,
168-
last_click_y,
165+
cursor_x,
166+
cursor_y,
167+
drag_start_x,
168+
drag_start_y,
169+
drag_end_x,
170+
drag_end_y,
169171
mouse_left_pressed,
170-
mouse_clicked,
172+
mouse_left_clicked,
171173
};
172-
mouse_clicked = false;
174+
mouse_left_clicked = false;
173175
rpass.set_pipeline(&render_pipeline);
174176
rpass.set_push_constants(wgpu::ShaderStage::all(), 0, unsafe {
175177
any_as_u32_slice(&push_constants)
@@ -207,11 +209,11 @@ async fn run(
207209
} => {
208210
mouse_left_pressed = state == ElementState::Pressed;
209211
if mouse_left_pressed {
210-
last_lmb_down_x = cursor_x;
211-
last_lmb_down_y = cursor_y;
212-
last_click_x = cursor_x;
213-
last_click_y = cursor_y;
214-
mouse_clicked = true;
212+
drag_start_x = cursor_x;
213+
drag_start_y = cursor_y;
214+
drag_end_x = cursor_x;
215+
drag_end_y = cursor_y;
216+
mouse_left_clicked = true;
215217
}
216218
}
217219
Event::WindowEvent {
@@ -221,8 +223,8 @@ async fn run(
221223
cursor_x = position.x as f32;
222224
cursor_y = position.y as f32;
223225
if mouse_left_pressed {
224-
last_lmb_down_x = cursor_x;
225-
last_lmb_down_y = cursor_y;
226+
drag_end_x = cursor_x;
227+
drag_end_y = cursor_y;
226228
}
227229
}
228230
_ => {}

0 commit comments

Comments
 (0)