GRID SNAP!
This commit is contained in:
parent
10b120512f
commit
f76cab4129
5 changed files with 66 additions and 11 deletions
|
@ -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
|
||||
|
|
26
src/lib.rs
26
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);
|
||||
|
|
10
src/main.rs
10
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<UiState>, mut ec: ResMut<EguiContext>|
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ pub fn modify_sys(
|
|||
gtransforms: Query<&GlobalTransform>,
|
||||
q_cam: Query<(&Camera, &GlobalTransform), With<MainCamera>>,
|
||||
shapes: Query<(Entity, &ShapeData)>,
|
||||
snap: Res<SnapGrid>,
|
||||
) {
|
||||
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);
|
||||
|
|
28
src/ui.rs
28
src/ui.rs
|
@ -76,6 +76,34 @@ pub fn action_bar_sys(
|
|||
});
|
||||
}
|
||||
|
||||
pub fn grid_window_sys(
|
||||
mut grid: ResMut<SnapGrid>,
|
||||
mut egui_ctx: ResMut<EguiContext>,
|
||||
) {
|
||||
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<Option<(Entity, String)>>,
|
||||
mut coms: Commands,
|
||||
|
|
Loading…
Reference in a new issue