Skip to content

Commit 05071ba

Browse files
Feature gate camera work because jank
1 parent a20e6c2 commit 05071ba

File tree

6 files changed

+30
-9
lines changed

6 files changed

+30
-9
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "norm"
2+
name = "normeditor"
33
authors = ["Matthew Kim"]
44
version = "0.1.1"
55
edition = "2021"
@@ -50,6 +50,7 @@ codegen-units = 1
5050

5151
[features]
5252
time = []
53+
camera = []
5354

5455
[dependencies]
5556
crc32fast = "1.4.2"

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ cargo r --release ./tests/obama.png
2525
### Additional Scripts
2626

2727
```bash
28+
# Run the renderer with an experimental camera
29+
cargo r --features camera -- ./tests/obama.png
2830

2931
# Profile the decoder
3032
cargo b --release && samply record ./target/release/norm_decode_png ./tests/reagan.png

src/renderer/app_state.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::{
22
image::grammar::Image,
33
renderer::{
4-
camera::Camera,
54
draw_uniform::DrawUniform,
65
effect_pipeline::EffectPipeline,
76
feature_uniform::{FeatureUniform, TransformAction},
@@ -12,10 +11,17 @@ use crate::{
1211
shape_uniform::{CircleData, ShapeUniform, MAX_CIRCLES},
1312
},
1413
};
14+
15+
#[cfg(feature = "camera")]
16+
use crate::renderer::camera::Camera;
17+
18+
#[cfg(feature = "camera")]
19+
use winit::event::MouseScrollDelta;
20+
1521
use anyhow::Result;
1622
use winit::{
1723
dpi::PhysicalSize,
18-
event::{ElementState, Event, KeyEvent, Modifiers, MouseButton, MouseScrollDelta, WindowEvent},
24+
event::{ElementState, Event, KeyEvent, Modifiers, MouseButton, WindowEvent},
1925
event_loop::EventLoop,
2026
keyboard::{KeyCode, PhysicalKey},
2127
window::{CursorIcon, Window, WindowBuilder},
@@ -31,6 +37,7 @@ pub struct AppState<'a> {
3137

3238
pub feature_uniform: FeatureUniform,
3339
pub draw_uniform: DrawUniform,
40+
#[cfg(feature = "camera")]
3441
pub camera: Camera,
3542
pub mouse_state: MouseState,
3643
pub editor_state: EditorState,
@@ -63,6 +70,7 @@ impl<'a> AppState<'a> {
6370
let draw_uniform_resource =
6471
gpu_allocator.create_uniform_resource("draw_uniform", draw_uniform)?;
6572

73+
#[cfg(feature = "camera")]
6674
let camera = Camera::new();
6775

6876
let shape_render_texture =
@@ -151,6 +159,7 @@ impl<'a> AppState<'a> {
151159
size,
152160
feature_uniform,
153161
draw_uniform,
162+
#[cfg(feature = "camera")]
154163
camera,
155164
mouse_state,
156165
editor_state,
@@ -188,17 +197,16 @@ impl<'a> AppState<'a> {
188197
// note to future self: we are probably due for a refactor
189198
// right now, application state, inputs, windowing are all coupled together
190199
// it would be good to be able to reuse certain commands for instance, think about cut (cmd + x)
191-
let mut super_key_pressed = if cfg!(target_os = "macos") {
200+
let super_key_pressed = if cfg!(target_os = "macos") {
192201
self.modifiers.state().super_key()
193202
} else {
194203
self.modifiers.state().control_key()
195204
};
196205

197-
let mut draw_mode = draw_uniform.crosshair();
198-
199206
// some global rules
200207
// maybe keep a stack of actions?
201-
draw_mode = if super_key_pressed { false } else { draw_mode };
208+
209+
let draw_mode = if super_key_pressed { false } else { draw_uniform.crosshair() };
202210

203211
match event {
204212
WindowEvent::MouseInput { state, button, .. } => {
@@ -208,7 +216,9 @@ impl<'a> AppState<'a> {
208216
.set_pressed(matches!(state, ElementState::Pressed));
209217

210218
// camera panning
211-
if super_key_pressed && !draw_mode {
219+
#[cfg(feature = "camera")]
220+
if super_key_pressed {
221+
dbg!("pressed");
212222
self.window.set_cursor_icon(CursorIcon::Grab);
213223

214224
match (prev_state, self.mouse_state.pressed()) {
@@ -304,6 +314,7 @@ impl<'a> AppState<'a> {
304314
}
305315
}
306316
}
317+
#[cfg(feature = "camera")]
307318
WindowEvent::MouseWheel { delta, .. } => {
308319
match delta {
309320
MouseScrollDelta::LineDelta(_, y) => {
@@ -335,6 +346,7 @@ impl<'a> AppState<'a> {
335346
WindowEvent::CursorMoved { position, .. } => {
336347
let (x, y) = (position.x as f32, position.y as f32);
337348

349+
#[cfg(feature = "camera")]
338350
if super_key_pressed && self.mouse_state.pressed() {
339351
if let Some((start_x, start_y)) = self.mouse_state.camera_pan_start() {
340352
let delta_x = (x - start_x) / (self.size.width as f32) * 2.0;
@@ -482,6 +494,7 @@ impl<'a> AppState<'a> {
482494
}
483495
}
484496
}
497+
#[cfg(feature = "camera")]
485498
(KeyCode::KeyR, ElementState::Pressed) => {
486499
self.camera.reset();
487500
}
@@ -503,6 +516,7 @@ impl<'a> AppState<'a> {
503516
);
504517

505518
// Update camera in draw uniform
519+
#[cfg(feature = "camera")]
506520
self.draw_uniform
507521
.update_camera(self.camera.view_projection_matrix());
508522

src/renderer/draw_uniform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl DrawUniform {
3030
}
3131
}
3232

33-
pub(crate) fn update_camera(&mut self, view_proj: [[f32; 4]; 4]) {
33+
pub(crate) const fn _update_camera(&mut self, view_proj: [[f32; 4]; 4]) {
3434
self.camera_view_proj = view_proj;
3535
}
3636

src/renderer/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub(crate) use texture::*;
33
pub(crate) use vertex::*;
44

55
mod app_state;
6+
#[cfg(feature = "camera")]
67
mod camera;
78
mod compute_effect;
89
mod draw_uniform;

src/renderer/mouse_state.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub struct MouseState {
66
position_x: f32,
77
position_y: f32,
88
start_drag: Option<(f32, f32)>,
9+
#[cfg(feature = "camera")]
910
camera_pan_start: Option<(f32, f32)>,
1011
selected_shape: Option<usize>, // index to the shape stack
1112
clipboard_shape: Option<Circle>, // index to the shape stack
@@ -72,10 +73,12 @@ impl MouseState {
7273
self.clipboard_shape = element_id;
7374
}
7475

76+
#[cfg(feature = "camera")]
7577
pub(crate) const fn camera_pan_start(&self) -> Option<(f32, f32)> {
7678
self.camera_pan_start
7779
}
7880

81+
#[cfg(feature = "camera")]
7982
pub(crate) const fn set_camera_pan_start(&mut self, position: Option<(f32, f32)>) {
8083
self.camera_pan_start = position;
8184
}

0 commit comments

Comments
 (0)