Skip to content

Commit

Permalink
merge indicators into one
Browse files Browse the repository at this point in the history
connecting line, drawing circels, and selection are now one entity,
updated in one system

this also adds the color of this indicator as a resource that's saved
and loaded from scenes

this closes #173
  • Loading branch information
tomara-x committed Jun 2, 2024
1 parent fbdd289 commit 05dd0e4
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 99 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,12 @@ some ops make a circle do things to its targets. like `process`, `del_targets`,
- when color changes, set the default drawing color
- `highlight_color`
- when color changes, set the highlight color (the outline around selected entities)
- `selection_color`
- when color changes, set the color of the selection lasso circle (and draw mode indicator)
- `indicator_color`
- when color changes, set the color of the selecting/drawing/connecting indicator
- `connection_color`
- when color changes, set the color of connection arrows
- `connection_width`
- when this circle's num changes, set the width of the connection arrows
- `connecting_line_color`
- when color changes, set the connect mode indicator
- `command_color`
- when color changes, set color of the command line text
- `text_size`
Expand All @@ -429,6 +427,9 @@ some ops make a circle do things to its targets. like `process`, `del_targets`,
- `n -> 6` : prefilter threshold (default: 0)
- `n -> 7` : prefilter threshold softness (default: 0)

all of these except for the tonemapping and bloom settings are saved inside the scene file (so deleting the circle after changing that setting is fine)
but for persistent change to bloom/tonemapping you have to leave the circles with the input values attached to them in the scene

</p>
</details>

Expand Down
44 changes: 4 additions & 40 deletions src/circles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,31 +124,6 @@ pub fn transform_highlights(
}
}

pub fn draw_drawing_circle(
id: Res<SelectionCircle>,
mut trans_query: Query<&mut Transform>,
mut meshes: ResMut<Assets<Mesh>>,
mesh_ids: Query<&Mesh2dHandle>,
mouse_button_input: Res<ButtonInput<MouseButton>>,
cursor: Res<CursorInfo>,
default_verts: Res<DefaultDrawVerts>,
) {
if mouse_button_input.pressed(MouseButton::Left)
&& !mouse_button_input.just_pressed(MouseButton::Left) {
let v = default_verts.0;
trans_query.get_mut(id.0).unwrap().translation = cursor.i.extend(1.);
let Mesh2dHandle(mesh_id) = mesh_ids.get(id.0).unwrap();
let mesh = meshes.get_mut(mesh_id).unwrap();
*mesh = RegularPolygon::new(cursor.i.distance(cursor.f).max(0.1), v).into();
}
if mouse_button_input.just_released(MouseButton::Left) {
trans_query.get_mut(id.0).unwrap().translation = Vec3::Z;
let Mesh2dHandle(mesh_id) = mesh_ids.get(id.0).unwrap();
let mesh = meshes.get_mut(mesh_id).unwrap();
*mesh = Triangle2d::default().into();
}
}

pub fn update_selection(
mut commands: Commands,
mouse_button_input: Res<ButtonInput<MouseButton>>,
Expand All @@ -158,11 +133,8 @@ pub fn update_selection(
cursor: Res<CursorInfo>,
keyboard_input: Res<ButtonInput<KeyCode>>,
mut top_clicked_circle: Local<Option<(Entity, f32)>>,
id: Res<SelectionCircle>,
mut trans_query: Query<&mut Transform, Without<Vertices>>,
mut meshes: ResMut<Assets<Mesh>>,
mesh_ids: Query<&Mesh2dHandle>,
order_query: Query<(), With<Order>>, // non-hole circle
mut clicked_on_space: ResMut<ClickedOnSpace>,
) {
if keyboard_input.pressed(KeyCode::Space) { return; }
let shift = keyboard_input.any_pressed([KeyCode::ShiftLeft, KeyCode::ShiftRight]);
Expand All @@ -182,6 +154,7 @@ pub fn update_selection(
}
}
if let Some(top) = *top_clicked_circle {
clicked_on_space.0 = false;
if !selected.contains(top.0) {
if shift { commands.entity(top.0).insert(Selected); }
else {
Expand All @@ -194,17 +167,7 @@ pub fn update_selection(
commands.entity(top.0).remove::<Selected>();
}
}
} else if mouse_button_input.pressed(MouseButton::Left) && top_clicked_circle.is_none() {
trans_query.get_mut(id.0).unwrap().translation = ((cursor.i+cursor.f)/2.).extend(100.);
let Mesh2dHandle(mesh_id) = mesh_ids.get(id.0).unwrap();
let mesh = meshes.get_mut(mesh_id).unwrap();
*mesh = Rectangle::from_corners(cursor.f, cursor.i).into();
}
if mouse_button_input.just_released(MouseButton::Left) {
trans_query.get_mut(id.0).unwrap().translation = Vec3::Z;
let Mesh2dHandle(mesh_id) = mesh_ids.get(id.0).unwrap();
let mesh = meshes.get_mut(mesh_id).unwrap();
*mesh = Triangle2d::default().into();
} else if mouse_button_input.just_released(MouseButton::Left) {
if top_clicked_circle.is_none() {
if !shift {
for entity in selected.iter() {
Expand All @@ -227,6 +190,7 @@ pub fn update_selection(
}
}
}
clicked_on_space.0 = true;
*top_clicked_circle = None;
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,7 @@ impl DragModes {
}

#[derive(Resource)]
pub struct SelectionCircle(pub Entity);

#[derive(Resource)]
pub struct ConnectingLine(pub Entity);
pub struct Indicator(pub Entity);

#[derive(Resource, Reflect, Default)]
#[reflect(Resource)]
Expand Down Expand Up @@ -255,6 +252,10 @@ pub struct SystemClipboard(pub ClipboardContext);
#[reflect(Resource)]
pub struct Version(pub String);

#[derive(Resource, Reflect, Default)]
#[reflect(Resource)]
pub struct IndicatorColor(pub Color);

#[derive(Resource, Default)]
pub struct PolygonHandles(pub Vec<Option<Mesh2dHandle>>);

Expand All @@ -267,6 +268,9 @@ pub struct DacCircles(pub Vec<Entity>);
#[derive(Resource)]
pub struct ConnectionMat(pub Handle<ColorMaterial>);

#[derive(Resource)]
pub struct ClickedOnSpace(pub bool);

// -------------------- events --------------------
#[derive(Event, Default)]
pub struct OrderChange;
Expand Down
23 changes: 0 additions & 23 deletions src/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,29 +208,6 @@ pub fn update_connection_arrows(
}
}

pub fn draw_connecting_arrow(
mouse_button_input: Res<ButtonInput<MouseButton>>,
cursor: Res<CursorInfo>,
keyboard_input: Res<ButtonInput<KeyCode>>,
id: Res<ConnectingLine>,
mut trans_query: Query<&mut Transform>,
connection_width: Res<ConnectionWidth>,
) {
if mouse_button_input.pressed(MouseButton::Left)
&& !mouse_button_input.just_pressed(MouseButton::Left)
&& !keyboard_input.pressed(KeyCode::Space) {
let perp = (cursor.i - cursor.f).perp();
*trans_query.get_mut(id.0).unwrap() = Transform {
translation: ((cursor.i + cursor.f) / 2.).extend(100.),
scale: Vec3::new(connection_width.0, cursor.f.distance(cursor.i), 1.),
rotation: Quat::from_rotation_z(perp.to_angle()),
}
}
if mouse_button_input.just_released(MouseButton::Left) {
*trans_query.get_mut(id.0).unwrap() = Transform::default();
}
}

pub fn connect_targets(
mut commands: Commands,
query: Query<(Entity, &Transform, &Vertices), With<Order>>,
Expand Down
88 changes: 70 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use bevy::{
render::view::RenderLayers,
window::FileDragAndDrop::DroppedFile,
ecs::system::SystemParam,
sprite::Mesh2dHandle,
prelude::*
};

Expand Down Expand Up @@ -74,8 +75,10 @@ fn main() {
.insert_resource(ConnectionColor(Color::hsla(0., 1., 1., 0.7)))
.insert_resource(ConnectionWidth(4.))
.insert_resource(CommandColor(Color::hsla(0., 0., 0.7, 1.)))
.insert_resource(IndicatorColor(Color::hsla(0., 1., 0.5, 0.3)))
.insert_resource(DefaultLT((0, 0)))
.insert_resource(TextSize(0.1))
.insert_resource(ClickedOnSpace(true))
.insert_resource(SystemClipboard(ClipboardContext::new().unwrap()))
.insert_resource(Msaa::Sample4)
.insert_resource(Version(format!("{} {}", env!("CARGO_PKG_VERSION"), env!("COMMIT_HASH"))))
Expand All @@ -92,13 +95,13 @@ fn main() {
.add_systems(Update, paste_scene.run_if(on_event::<PasteCommand>()))
.add_systems(Update, post_load)
.add_systems(Update, file_drag_and_drop)
.add_systems(Update, update_indicator)
.init_resource::<DragModes>()
// cursor
.insert_resource(CursorInfo::default())
.add_systems(Update, update_cursor_info)
// circles
.add_systems(Update, spawn_circles.run_if(in_state(Mode::Draw)))
.add_systems(Update, draw_drawing_circle.run_if(in_state(Mode::Draw)))
.add_systems(Update, update_selection.after(update_cursor_info).run_if(in_state(Mode::Edit)))
.add_systems(Update, move_selected.after(update_selection).run_if(in_state(Mode::Edit)))
.add_systems(Update, update_color.after(update_selection).run_if(in_state(Mode::Edit)))
Expand All @@ -125,7 +128,6 @@ fn main() {
.add_systems(Update, connect_targets)
.add_systems(Update, target.run_if(in_state(Mode::Connect)))
.add_systems(PreUpdate, update_connection_arrows)
.add_systems(Update, draw_connecting_arrow.run_if(in_state(Mode::Connect)))
// order
.init_resource::<Queue>()
.init_resource::<LoopQueue>()
Expand Down Expand Up @@ -162,6 +164,7 @@ fn main() {
.register_type::<ConnectionColor>()
.register_type::<ConnectionWidth>()
.register_type::<CommandColor>()
.register_type::<IndicatorColor>()
.register_type::<TextSize>()
.register_type::<Version>()
.register_type::<Holes>()
Expand All @@ -179,6 +182,7 @@ fn setup(
mut materials: ResMut<Assets<ColorMaterial>>,
command_color: Res<CommandColor>,
connection_color: Res<ConnectionColor>,
indicator_color: Res<IndicatorColor>,
) {
// camera
commands.spawn((
Expand Down Expand Up @@ -236,29 +240,17 @@ fn setup(
));
});

// selection / drawing circle
// selecting / drawing / connecting indicator
let id = commands.spawn((
ColorMesh2dBundle {
mesh: meshes.add(Triangle2d::default()).into(),
material: materials.add(ColorMaterial::from(Color::hsla(0., 1., 0.5, 0.3))),
material: materials.add(ColorMaterial::from(indicator_color.0)),
transform: Transform::from_translation(Vec3::Z),
..default()
},
Col(Color::hsla(0., 1., 0.5, 0.3)),
Col(indicator_color.0),
)).id();
commands.insert_resource(SelectionCircle(id));

// connecting line
let id = commands.spawn((
ColorMesh2dBundle {
mesh: meshes.add(Triangle2d::default()).into(),
material: materials.add(ColorMaterial::from(Color::hsla(0., 1., 0.5, 0.3))),
transform: Transform::default(),
..default()
},
Col(Color::hsla(0., 1., 0.5, 0.3)),
)).id();
commands.insert_resource(ConnectingLine(id));
commands.insert_resource(Indicator(id));

// arrow mesh
commands.insert_resource(ArrowHandle(meshes.add(Triangle2d::default()).into()));
Expand All @@ -281,6 +273,59 @@ fn toggle_pan(
}
}

fn update_indicator(
mode: Res<State<Mode>>,
id: Res<Indicator>,
mut trans_query: Query<&mut Transform>,
mouse_button_input: Res<ButtonInput<MouseButton>>,
cursor: Res<CursorInfo>,
keyboard_input: Res<ButtonInput<KeyCode>>,
connection_width: Res<ConnectionWidth>,
default_verts: Res<DefaultDrawVerts>,
mut meshes: ResMut<Assets<Mesh>>,
mesh_ids: Query<&Mesh2dHandle>,
clicked_on_space: Res<ClickedOnSpace>,
) {
if mouse_button_input.pressed(MouseButton::Left)
&& !mouse_button_input.just_pressed(MouseButton::Left)
&& !keyboard_input.pressed(KeyCode::Space) {
if *mode.get() == Mode::Edit && clicked_on_space.0 {
let Mesh2dHandle(mesh_id) = mesh_ids.get(id.0).unwrap();
let mesh = meshes.get_mut(mesh_id).unwrap();
*mesh = Rectangle::from_corners(cursor.f, cursor.i).into();
*trans_query.get_mut(id.0).unwrap() = Transform {
translation: ((cursor.i+cursor.f)/2.).extend(100.),
..default()
};
} else if *mode.get() == Mode::Draw {
let v = default_verts.0;
let Mesh2dHandle(mesh_id) = mesh_ids.get(id.0).unwrap();
let mesh = meshes.get_mut(mesh_id).unwrap();
*mesh = RegularPolygon::new(cursor.i.distance(cursor.f).max(0.1), v).into();
*trans_query.get_mut(id.0).unwrap() = Transform {
translation: cursor.i.extend(100.),
..default()
};
} else if *mode.get() == Mode::Connect {
let Mesh2dHandle(mesh_id) = mesh_ids.get(id.0).unwrap();
let mesh = meshes.get_mut(mesh_id).unwrap();
*mesh = Triangle2d::default().into();
let perp = (cursor.i - cursor.f).perp();
*trans_query.get_mut(id.0).unwrap() = Transform {
translation: ((cursor.i + cursor.f) / 2.).extend(100.),
scale: Vec3::new(connection_width.0, cursor.f.distance(cursor.i), 1.),
rotation: Quat::from_rotation_z(perp.to_angle()),
}
}
}
if mouse_button_input.just_released(MouseButton::Left) {
*trans_query.get_mut(id.0).unwrap() = Transform::default();
let Mesh2dHandle(mesh_id) = mesh_ids.get(id.0).unwrap();
let mesh = meshes.get_mut(mesh_id).unwrap();
*mesh = Triangle2d::default().into();
}
}

fn save_scene(world: &mut World) {
let mut save_events = world.resource_mut::<Events<SaveCommand>>();
let events: Vec<SaveCommand> = save_events.drain().collect();
Expand All @@ -307,6 +352,7 @@ fn save_scene(world: &mut World) {
.allow_resource::<ConnectionWidth>()
.allow_resource::<ClearColor>()
.allow_resource::<CommandColor>()
.allow_resource::<IndicatorColor>()
.allow_resource::<TextSize>()
.allow_resource::<Version>()
.extract_entities(query.iter(world))
Expand Down Expand Up @@ -392,6 +438,8 @@ struct MoreParams<'w, 's> {
arrow_handle: Res<'w, ArrowHandle>,
connection_mat: Res<'w, ConnectionMat>,
selected_query: Query<'w, 's, Entity, With<Selected>>,
indicator_color: Res<'w, IndicatorColor>,
indicator_id: Res<'w, Indicator>,
}

fn post_load(
Expand All @@ -410,12 +458,16 @@ fn post_load(
scene_spawner: Res<SceneSpawner>,
mut polygon_handles: ResMut<PolygonHandles>,
more: MoreParams,
mut indicator_color_query: Query<&mut Col, Without<Vertices>>,
) {
for (scene_id, instance_id) in scenes.iter() {
if scene_spawner.instance_is_ready(**instance_id) {
for e in more.selected_query.iter() {
commands.entity(e).remove::<Selected>();
}
// update indicator color
let indicator_color = more.indicator_color.0;
indicator_color_query.get_mut(more.indicator_id.0).unwrap().0 = indicator_color;
// update connection material from color resource
let mat_id = &more.connection_mat.0;
materials.get_mut(mat_id).unwrap().color = more.connection_color.0;
Expand Down
15 changes: 5 additions & 10 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ pub struct Access<'w, 's> {
connection_color: ResMut<'w, ConnectionColor>,
connection_width: ResMut<'w, ConnectionWidth>,
arrow_query: Query<'w, 's, &'static ConnectionArrow>,
selection_circle: Res<'w, SelectionCircle>,
connecting_line: Res<'w, ConnectingLine>,
indicator: Res<'w, Indicator>,
indicator_color: ResMut<'w, IndicatorColor>,
command_line_text: Query<'w, 's, &'static mut Text, With<CommandText>>,
command_color: ResMut<'w, CommandColor>,
dac_change_event: EventWriter<'w, DacChange>,
Expand Down Expand Up @@ -689,10 +689,11 @@ pub fn process(
if color.is_changed() {
access.highlight_color.0 = color.0;
}
} else if op == "selection_color" {
} else if op == "indicator_color" {
let color = access.col_query.get_mut(*id).unwrap();
if color.is_changed() {
let id = access.selection_circle.0;
access.indicator_color.0 = color.0;
let id = access.indicator.0;
access.col_query.get_mut(id).unwrap().0 = color.0;
}
} else if op == "connection_color" {
Expand All @@ -702,12 +703,6 @@ pub fn process(
let mat_id = &access.connection_mat.0;
access.materials.get_mut(mat_id).unwrap().color = color.0;
}
} else if op == "connecting_line_color" {
let color = access.col_query.get_mut(*id).unwrap();
if color.is_changed() {
let id = access.connecting_line.0;
access.col_query.get_mut(id).unwrap().0 = color.0;
}
} else if op == "command_color" {
let color = access.col_query.get_mut(*id).unwrap();
if color.is_changed() {
Expand Down

0 comments on commit 05dd0e4

Please sign in to comment.