From f76cab4129d0a8ed3ea8582de7c5ffc3b5a2b79e Mon Sep 17 00:00:00 2001 From: RustyStriker Date: Thu, 4 Aug 2022 17:50:21 +0300 Subject: [PATCH] GRID SNAP! --- README.md | 8 ++++---- src/lib.rs | 26 +++++++++++++++++++++++++- src/main.rs | 10 ++++++---- src/modify.rs | 5 +++-- src/ui.rs | 28 ++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 061c08e..3afe4de 100644 --- a/README.md +++ b/README.md @@ -16,13 +16,13 @@ Eventually, this will be a level editor... But until then, I guess it's just fun - [x] Show/Hide images - [ ] Move/Drag images around - [ ] Control images Z value(so we could reorder them) - - [ ] Delete images - - [ ] Name images + - [x] Delete images + - [x] Name images - [ ] Duplicate images(and also shapes maybe?)? - [x] Show hide shapes - [ ] Control shape Z value -- [ ] Snap to grid -- [ ] Change grid size +- [x] Snap to grid +- [x] Change grid size - [ ] Show/Hide grid(also make a visible grid in the first place) - [ ] Save? (maybe just import and export directly?) - [ ] Export diff --git a/src/lib.rs b/src/lib.rs index cd7093c..b61f1c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,11 +7,35 @@ pub mod create; pub mod helpers; pub mod modify; pub mod ui; -pub use ui::{action_bar_sys, shape_tree_sys}; pub use modify::modify_sys; pub use create::create_sys; pub use helpers::*; +#[derive(Debug, Clone)] +pub struct SnapGrid { + pub width: f32, + pub height: f32, + pub snap: bool, + pub offset: Vec2, +} +impl SnapGrid { + pub fn snap_to_grid(&self, v: Vec2) -> Vec2 { + if self.snap { + let w = v - self.offset; + let snapped = Vec2::new(w.x - w.x % self.width, w.y - w.y % self.height); + + snapped + self.offset + } + else { + v + } + } +} +impl Default for SnapGrid { + fn default() -> Self { + Self { width: 15.0, height: 15.0, snap: false, offset: Vec2::ZERO } + } +} #[derive(Clone, Debug)] pub struct PointSize(pub f32); diff --git a/src/main.rs b/src/main.rs index cc880fa..924cd35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,7 @@ fn main() { .insert_resource(ButtonsColors::default()) .insert_resource(UiState::default()) .insert_resource(PointSize(6.0)) + .insert_resource(SnapGrid::default()) ; app @@ -40,13 +41,14 @@ fn main() { .add_system(modify_sys.with_run_criteria(|state: Res, mut ec: ResMut| if !ec.ctx_mut().is_pointer_over_area() && state.current_action == Action::Modify { ShouldRun::Yes } else { ShouldRun::No } )) - .add_system(action_bar_sys) - .add_system(shape_tree_sys) + .add_system(ui::action_bar_sys) + .add_system(ui::shape_tree_sys) .add_system(show_hide_points) .add_system(color_points) .add_system(drag_camera_sys) .add_system(zoom_camera_sys) .add_system(scale_points) + .add_system(ui::grid_window_sys) ; app.run(); @@ -78,8 +80,8 @@ fn zoom_camera_sys( for mut op in cam.iter_mut() { op.scale += scroll ; - if op.scale <= 0.0 { - op.scale = 0.1; + if op.scale <= 0.05 { + op.scale = 0.05; } } } diff --git a/src/modify.rs b/src/modify.rs index 800e69d..15c3e5a 100644 --- a/src/modify.rs +++ b/src/modify.rs @@ -15,6 +15,7 @@ pub fn modify_sys( gtransforms: Query<&GlobalTransform>, q_cam: Query<(&Camera, &GlobalTransform), With>, shapes: Query<(Entity, &ShapeData)>, + snap: Res, ) { let scale = scale.get_single().map(|s| s.scale).unwrap_or(1.0); let mouse_pos = get_mouse_pos(&q_cam, &wnds); @@ -51,7 +52,7 @@ pub fn modify_sys( // Middle of a drag :D if let Some(ce) = sd.center && ce == pe { if let Ok(mut t) = transforms.get_mut(sd.main_shape) { - t.translation = mouse_pos.extend(t.translation.z); + t.translation = snap.snap_to_grid(mouse_pos).extend(t.translation.z); } } else if let Ok(mut t) = transforms.get_mut(pe) { @@ -59,7 +60,7 @@ pub fn modify_sys( let gt = gtransforms.get(pe).unwrap(); let rot = gt.to_scale_rotation_translation().1; let ang = rot.to_euler(EulerRot::XYZ).2; - let delta = Mat2::from_angle(-ang) * (mouse_pos - gt.translation().xy()); + let delta = Mat2::from_angle(-ang) * (snap.snap_to_grid(mouse_pos) - gt.translation().xy()); t.translation += delta.extend(0.0); // We need to recalculate the center, and update the points to be the new relevant point from the center let center_offset = calc_shape_center_offset(&transforms, sd); diff --git a/src/ui.rs b/src/ui.rs index 99fc16c..693ee56 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -76,6 +76,34 @@ pub fn action_bar_sys( }); } +pub fn grid_window_sys( + mut grid: ResMut, + mut egui_ctx: ResMut, +) { + egui::Window::new("Snap Grid") + .default_pos((200.0, 20.0)) + .title_bar(true) + .resizable(false) + .show(egui_ctx.ctx_mut(), |ui| { + ui.checkbox(&mut grid.snap, "Snap Enabled"); + ui.label("Grid size:"); + ui.horizontal(|hui| { + hui.label("Width:"); + hui.add(egui::DragValue::new(&mut grid.width)); + hui.label("Height:"); + hui.add(egui::DragValue::new(&mut grid.height)); + }); + ui.horizontal(|hui| { + hui.label("Offset:"); + hui.label("X:"); + hui.add(egui::DragValue::new(&mut grid.offset.x)); + hui.label("Y:"); + hui.add(egui::DragValue::new(&mut grid.offset.y)); + }); + + }); +} + pub fn shape_tree_sys( mut name_change: Local>, mut coms: Commands,