Skip to content

Commit

Permalink
Add hide selected
Browse files Browse the repository at this point in the history
  • Loading branch information
Liby99 committed Oct 10, 2019
1 parent 2dbd14b commit d415e6a
Show file tree
Hide file tree
Showing 19 changed files with 262 additions and 41 deletions.
8 changes: 8 additions & 0 deletions src/components/hidden.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use specs::prelude::*;

#[derive(Debug, Copy, Clone, Default)]
pub struct Hidden;

impl Component for Hidden {
type Storage = NullStorage<Self>;
}
4 changes: 3 additions & 1 deletion src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ mod line;
mod circle;
mod rectangle;
mod selected;
mod hidden;

pub use point::*;
pub use line::*;
pub use circle::*;
pub use rectangle::*;
pub use selected::*;
pub use selected::*;
pub use hidden::*;
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fn main() {
.with(interactions::select::SeldeAllViaKeyboard, "selde_all_via_keyboard", &[])
.with(interactions::remove::RemoveSelectedViaDelete, "remove_selected_via_delete", &[])
.with(interactions::create::line::AbortCreateLineViaKeyboard, "abort_create_line_via_keyboard", &[])
.with(interactions::hide::HideSelectedViaKeyboard, "hide_selected_via_keyboard", &[])
.with(interactions::history::UndoViaKeyboard, "undo_via_keyboard", &[])
.with(interactions::history::RedoViaKeyboard, "redo_via_keyboard", &[])

Expand All @@ -56,6 +57,7 @@ fn main() {
// Geometry action handlers
.with(geometry_actions::SeldeAllHandler::default(), "selde_all_handler", &["selde_all_via_keyboard", "selde_via_mouse"])
.with(geometry_actions::RemoveSelectedHandler::default(), "remove_selected_handler", &["remove_selected_via_delete", "dependency_graph_cache"])
.with(geometry_actions::HideSelectedHandler::default(), "hide_selected_handler", &["hide_selected_via_keyboard"])

// Geometry helpers
.with(interactions::create::point::SnapPointSystem, "snap_point_system", &["spatial_hash_cache", "tool_state_manager", "viewport_state_manager"])
Expand Down Expand Up @@ -84,6 +86,7 @@ fn main() {
.with(geometry_systems::InsertHistoryGeometry::default(), "insert_history_geometry", &["sketch_history_action_handler"])

// Remove systems
.with(geometry_systems::HideHandler::default(), "hide_handler", &["hide_selected_handler"])
.with(geometry_systems::RemoveHandler::default(), "remove_handler", &["remove_selected_handler", "sketch_history_action_handler"])

// History caching
Expand Down
2 changes: 2 additions & 0 deletions src/resources/events/geometry_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub enum GeometryAction {
DeselectAll,
DeselectAllExcept(Entity),
RemoveSelected,
HideSelected,
UnhideAll,
DrawParallelOnSelected,
DrawPerpendicularOnSelected,
DrawMidpointOnSelected,
Expand Down
18 changes: 18 additions & 0 deletions src/resources/events/sketch_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub enum SketchEvent {
Insert(Entity, SketchGeometry, bool), // the last bool represents "is_by_history"
Remove(Entity, SketchGeometry, bool), // the last bool represents "is_by_history"
MovePoint(Entity, MovePoint),
Hide(Entity, bool),
Unhide(Entity, bool),
}

impl SketchEvent {
Expand All @@ -29,6 +31,22 @@ impl SketchEvent {
pub fn remove_by_history(ent: Entity, sketch_geom: SketchGeometry) -> Self {
SketchEvent::Remove(ent, sketch_geom, true)
}

pub fn hide(ent: Entity) -> Self {
SketchEvent::Hide(ent, false)
}

pub fn hide_by_history(ent: Entity) -> Self {
SketchEvent::Hide(ent, true)
}

pub fn unhide(ent: Entity) -> Self {
SketchEvent::Unhide(ent, false)
}

pub fn unhide_by_history(ent: Entity) -> Self {
SketchEvent::Unhide(ent, true)
}
}

#[derive(Debug, Clone, Copy)]
Expand Down
4 changes: 3 additions & 1 deletion src/resources/sketch_history.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use specs::prelude::*;
use super::events::SketchGeometry;

Expand All @@ -21,6 +21,8 @@ impl Default for SketchHistory {
pub enum SketchHistoryEvent {
RemoveMany(HashMap<Entity, SketchGeometry>),
InsertMany(HashMap<Entity, SketchGeometry>),
HideMany(HashSet<Entity>),
UnhideMany(HashSet<Entity>),
// Update(Entity, SketchGeometry, SketchGeometry), // Entity, old, new
}

Expand Down
6 changes: 5 additions & 1 deletion src/systems/cache_managers/dependency_graph_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ impl<'a> System<'a> for DependencyGraphCache {
SketchGeometry::Circle(sym_circle, _) => add_circle(&mut dependency_graph, entity, sym_circle),
},
SketchEvent::Remove(entity, _, _) => dependency_graph.remove(entity),
SketchEvent::Select(_) | SketchEvent::Deselect(_) | SketchEvent::MovePoint(_, _) => (),
SketchEvent::Select(_) |
SketchEvent::Deselect(_) |
SketchEvent::MovePoint(_, _) |
SketchEvent::Hide(_, _) |
SketchEvent::Unhide(_, _) => (),
}
}
} else {
Expand Down
55 changes: 34 additions & 21 deletions src/systems/cache_managers/sketch_history_action_handler.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::collections::{HashMap, HashSet};
use specs::prelude::*;
use crate::{
resources::{
events::{
HistoryAction, HistoryActionReader, HistoryActionChannel,
SketchEvent, SketchEventChannel,
SketchEvent, SketchEventChannel, SketchGeometry,
},
SketchHistory, SketchHistoryEvent,
},
Expand Down Expand Up @@ -42,32 +43,20 @@ impl<'a> System<'a> for SketchHistoryActionHandler {
HistoryAction::Undo => {
if let Some(history_event) = sketch_history.undo() {
match history_event {
SketchHistoryEvent::InsertMany(insertions) => {
for (inserted_entity, inserted_geometry) in insertions {
sketch_event_channel.single_write(SketchEvent::remove_by_history(*inserted_entity, *inserted_geometry));
}
},
SketchHistoryEvent::RemoveMany(removals) => {
for (removed_entity, removed_geometry) in removals {
sketch_event_channel.single_write(SketchEvent::insert_by_history(*removed_entity, *removed_geometry));
}
},
SketchHistoryEvent::InsertMany(insertions) => remove(&mut sketch_event_channel, insertions),
SketchHistoryEvent::RemoveMany(removals) => insert(&mut sketch_event_channel, removals),
SketchHistoryEvent::HideMany(entities) => unhide(&mut sketch_event_channel, entities),
SketchHistoryEvent::UnhideMany(entities) => hide(&mut sketch_event_channel, entities),
}
}
},
HistoryAction::Redo => {
if let Some(history_event) = sketch_history.redo() {
match history_event {
SketchHistoryEvent::InsertMany(insertions) => {
for (inserted_entity, inserted_geometry) in insertions {
sketch_event_channel.single_write(SketchEvent::insert_by_history(*inserted_entity, *inserted_geometry));
}
},
SketchHistoryEvent::RemoveMany(removals) => {
for (removed_entity, removed_geometry) in removals {
sketch_event_channel.single_write(SketchEvent::remove_by_history(*removed_entity, *removed_geometry));
}
},
SketchHistoryEvent::InsertMany(insertions) => insert(&mut sketch_event_channel, insertions),
SketchHistoryEvent::RemoveMany(removals) => remove(&mut sketch_event_channel, removals),
SketchHistoryEvent::HideMany(entities) => hide(&mut sketch_event_channel, entities),
SketchHistoryEvent::UnhideMany(entities) => unhide(&mut sketch_event_channel, entities),
}
}
},
Expand All @@ -76,4 +65,28 @@ impl<'a> System<'a> for SketchHistoryActionHandler {
}
}
}
}

fn remove(sketch_event_channel: &mut SketchEventChannel, entities: &HashMap<Entity, SketchGeometry>) {
for (entity, geometry) in entities {
sketch_event_channel.single_write(SketchEvent::remove_by_history(*entity, *geometry));
}
}

fn insert(sketch_event_channel: &mut SketchEventChannel, entities: &HashMap<Entity, SketchGeometry>) {
for (entity, geometry) in entities {
sketch_event_channel.single_write(SketchEvent::insert_by_history(*entity, *geometry));
}
}

fn hide(sketch_event_channel: &mut SketchEventChannel, entities: &HashSet<Entity>) {
for entity in entities {
sketch_event_channel.single_write(SketchEvent::hide_by_history(*entity));
}
}

fn unhide(sketch_event_channel: &mut SketchEventChannel, entities: &HashSet<Entity>) {
for entity in entities {
sketch_event_channel.single_write(SketchEvent::unhide_by_history(*entity));
}
}
26 changes: 25 additions & 1 deletion src/systems/cache_managers/sketch_history_cache.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use specs::prelude::*;
use crate::{
resources::{
Expand All @@ -11,6 +11,8 @@ enum Event {
None,
Insert(HashMap<Entity, SketchGeometry>),
Remove(HashMap<Entity, SketchGeometry>),
Hide(HashSet<Entity>),
Unhide(HashSet<Entity>),
}

pub struct SketchHistoryCache {
Expand Down Expand Up @@ -64,6 +66,26 @@ impl<'a> System<'a> for SketchHistoryCache {
curr_event = Event::Remove(removals);
}
},
SketchEvent::Hide(entity, false) => {
if let Event::Hide(entities) = &mut curr_event {
entities.insert(*entity);
} else {
push_event(curr_event, &mut sketch_history);
let mut entities = HashSet::new();
entities.insert(*entity);
curr_event = Event::Hide(entities);
}
},
SketchEvent::Unhide(entity, false) => {
if let Event::Unhide(entities) = &mut curr_event {
entities.insert(*entity);
} else {
push_event(curr_event, &mut sketch_history);
let mut entities = HashSet::new();
entities.insert(*entity);
curr_event = Event::Unhide(entities);
}
},
_ => (),
}
}
Expand All @@ -78,5 +100,7 @@ fn push_event(event: Event, history: &mut SketchHistory) {
Event::None => (),
Event::Insert(insertions) => history.push(SketchHistoryEvent::InsertMany(insertions)),
Event::Remove(removals) => history.push(SketchHistoryEvent::RemoveMany(removals)),
Event::Hide(entities) => history.push(SketchHistoryEvent::HideMany(entities)),
Event::Unhide(entities) => history.push(SketchHistoryEvent::UnhideMany(entities)),
}
}
29 changes: 22 additions & 7 deletions src/systems/cache_managers/spatial_hash_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,35 @@ impl<'a> System<'a> for SpatialHashCache {
let dependents = dependency_graph.get_all_dependents(entity);
for dependent in dependents {
table.remove_from_all(dependent);
if let Some(point) = points.get(dependent) {
table.insert_point(dependent, *point, &*vp);
} else if let Some(line) = lines.get(dependent) {
table.insert_line(dependent, *line, &*vp);
} else if let Some(circle) = circles.get(dependent) {
table.insert_circle(dependent, *circle, &*vp);
}
insert_entity(dependent, &*vp, &mut table, &points, &lines, &circles);
}
},
SketchEvent::Hide(entity, _) => table.remove_from_all(*entity),
SketchEvent::Unhide(entity, _) => {
insert_entity(*entity, &*vp, &mut table, &points, &lines, &circles);
}
}
}
} else {
panic!("[spatial_hash_cache] No sketch event reader id");
}
}
}
}

fn insert_entity<'a>(
entity: Entity,
viewport: &Viewport,
table: &mut SpatialHashTable<Entity>,
points: &ReadStorage<'a, Point>,
lines: &ReadStorage<'a, Line>,
circles: &ReadStorage<'a, Circle>,
) {
if let Some(point) = points.get(entity) {
table.insert_point(entity, *point, viewport);
} else if let Some(line) = lines.get(entity) {
table.insert_line(entity, *line, &*viewport);
} else if let Some(circle) = circles.get(entity) {
table.insert_circle(entity, *circle, viewport);
}
}
48 changes: 48 additions & 0 deletions src/systems/geometry_actions/hide_selected_handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use specs::prelude::*;
use crate::{
resources::events::{
GeometryAction, GeometryActionChannel, GeometryActionReader,
SketchEvent, SketchEventChannel,
},
components::{Selected},
};

pub struct HideSelectedHandler {
geometry_action_reader: Option<GeometryActionReader>,
}

impl Default for HideSelectedHandler {
fn default() -> Self {
Self { geometry_action_reader: None }
}
}

impl<'a> System<'a> for HideSelectedHandler {
type SystemData = (
Entities<'a>,
Read<'a, GeometryActionChannel>,
Write<'a, SketchEventChannel>,
ReadStorage<'a, Selected>,
);

fn setup(&mut self, world: &mut World) {
Self::SystemData::setup(world);
self.geometry_action_reader = Some(world.fetch_mut::<GeometryActionChannel>().register_reader());
}

fn run(&mut self, (entities, geometry_action_channel, mut sketch_event_channel, selected): Self::SystemData) {
if let Some(reader_id) = &mut self.geometry_action_reader {
for event in geometry_action_channel.read(reader_id) {
match event {
GeometryAction::HideSelected => {
for (entity, _) in (&entities, &selected).join() {
sketch_event_channel.single_write(SketchEvent::hide(entity));
}
break;
},
_ => ()
}
}
}
}
}
3 changes: 3 additions & 0 deletions src/systems/geometry_actions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
mod remove_selected_handler;
pub use remove_selected_handler::*;

mod hide_selected_handler;
pub use hide_selected_handler::*;

mod selde_all_handler;
pub use selde_all_handler::*;

Expand Down
Loading

0 comments on commit d415e6a

Please sign in to comment.