Skip to content

Commit 13e2a3c

Browse files
committed
Refactor code and polish things
1 parent 8ec5d6c commit 13e2a3c

File tree

4 files changed

+107
-58
lines changed

4 files changed

+107
-58
lines changed

editor/src/consts.rs

-2
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,12 @@ pub const SCALE_EFFECT: f64 = 0.5;
108108

109109
// COLORS
110110
pub const COLOR_OVERLAY_BLUE: &str = "#00a8ff";
111-
pub const COLOR_OVERLAY_BLUE_TRANSLUCENT: &str = "#00a8ff77";
112111
pub const COLOR_OVERLAY_YELLOW: &str = "#ffc848";
113112
pub const COLOR_OVERLAY_GREEN: &str = "#63ce63";
114113
pub const COLOR_OVERLAY_RED: &str = "#ef5454";
115114
pub const COLOR_OVERLAY_GRAY: &str = "#cccccc";
116115
pub const COLOR_OVERLAY_WHITE: &str = "#ffffff";
117116
pub const COLOR_OVERLAY_LABEL_BACKGROUND: &str = "#000000cc";
118-
pub const COLOR_OVERLAY_TRANSPARENT: &str = "#ffffff00";
119117

120118
// DOCUMENT
121119
pub const DEFAULT_DOCUMENT_NAME: &str = "Untitled Document";

editor/src/messages/portfolio/document/overlays/utility_types.rs

+40-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use super::utility_functions::overlay_canvas_context;
22
use crate::consts::{
3-
COLOR_OVERLAY_BLUE, COLOR_OVERLAY_BLUE_TRANSLUCENT, COLOR_OVERLAY_GREEN, COLOR_OVERLAY_RED, COLOR_OVERLAY_TRANSPARENT, COLOR_OVERLAY_WHITE, COLOR_OVERLAY_YELLOW, MANIPULATOR_GROUP_MARKER_SIZE,
4-
PIVOT_CROSSHAIR_LENGTH, PIVOT_CROSSHAIR_THICKNESS,
3+
COLOR_OVERLAY_BLUE, COLOR_OVERLAY_GREEN, COLOR_OVERLAY_RED, COLOR_OVERLAY_WHITE, COLOR_OVERLAY_YELLOW, MANIPULATOR_GROUP_MARKER_SIZE, PIVOT_CROSSHAIR_LENGTH, PIVOT_CROSSHAIR_THICKNESS,
54
};
65
use crate::consts::{COMPASS_ROSE_ARROW_SIZE, COMPASS_ROSE_HOVER_RING_DIAMETER, COMPASS_ROSE_MAIN_RING_DIAMETER, COMPASS_ROSE_PIVOT_DIAMETER};
76
use crate::messages::prelude::Message;
@@ -295,9 +294,15 @@ impl OverlayContext {
295294

296295
pub fn draw_scale(&mut self, start: DVec2, scale: f64, radius: f64, text: &str) {
297296
let sign = scale.signum();
297+
let mut fill_color = graphene_std::Color::from_rgb_str(crate::consts::COLOR_OVERLAY_WHITE.strip_prefix('#').unwrap())
298+
.unwrap()
299+
.with_alpha(0.05)
300+
.rgba_hex();
301+
fill_color.insert(0, '#');
302+
let fill_color = Some(fill_color.as_str());
298303
self.line(start + DVec2::X * radius * sign, start + DVec2::X * (radius * scale), None);
299-
self.circle(start, radius, Some(COLOR_OVERLAY_TRANSPARENT), None);
300-
self.circle(start, radius * scale.abs(), Some(COLOR_OVERLAY_TRANSPARENT), None);
304+
self.circle(start, radius, fill_color, None);
305+
self.circle(start, radius * scale.abs(), fill_color, None);
301306
self.text(
302307
text,
303308
COLOR_OVERLAY_BLUE,
@@ -343,29 +348,6 @@ impl OverlayContext {
343348

344349
let old_line_width = self.render_context.line_width();
345350

346-
if show_hover_ring {
347-
self.render_context.set_line_width((COMPASS_ROSE_HOVER_RING_DIAMETER - COMPASS_ROSE_MAIN_RING_DIAMETER) / 2.);
348-
self.render_context.begin_path();
349-
self.render_context
350-
.arc(position.x, position.y, (COMPASS_ROSE_MAIN_RING_DIAMETER + COMPASS_ROSE_HOVER_RING_DIAMETER) / 4., 0., TAU)
351-
.expect("Failed to draw outer circle");
352-
self.render_context.set_stroke_style_str(COLOR_OVERLAY_BLUE_TRANSLUCENT);
353-
self.render_context.stroke();
354-
}
355-
356-
self.render_context.set_line_width(2.);
357-
self.render_context.begin_path();
358-
self.render_context
359-
.arc(position.x, position.y, (COMPASS_ROSE_MAIN_RING_DIAMETER + 1.) / 2., 0., TAU)
360-
.expect("Failed to draw inner circle");
361-
self.render_context.set_stroke_style_str(COLOR_OVERLAY_BLUE);
362-
self.render_context.stroke();
363-
364-
self.render_context.set_line_width(1.);
365-
self.render_context.set_stroke_style_str(COLOR_OVERLAY_BLUE);
366-
self.render_context.set_fill_style_str(COLOR_OVERLAY_BLUE);
367-
368-
// Draw 4 arrows at cardinal directions
369351
let ring_radius = (COMPASS_ROSE_MAIN_RING_DIAMETER + 1.) / 2.;
370352

371353
for i in 0..4 {
@@ -374,7 +356,7 @@ impl OverlayContext {
374356
let perpendicular = DVec2::from_angle(base_angle + TAU / 4.);
375357
let color = if i % 2 == 0 { COLOR_OVERLAY_RED } else { COLOR_OVERLAY_GREEN };
376358

377-
let base = DVec2::new(x, y) + direction * ring_radius;
359+
let base = DVec2::new(x, y) + direction * (ring_radius - COMPASS_ROSE_ARROW_SIZE * 0.1);
378360

379361
self.render_context.begin_path();
380362

@@ -389,8 +371,38 @@ impl OverlayContext {
389371
self.render_context.close_path();
390372
self.render_context.set_fill_style_str(color);
391373
self.render_context.fill();
374+
self.render_context.set_stroke_style_str(color);
375+
self.render_context.stroke();
392376
}
393377

378+
if show_hover_ring {
379+
let mut fill_color = graphene_std::Color::from_rgb_str(crate::consts::COLOR_OVERLAY_BLUE.strip_prefix('#').unwrap())
380+
.unwrap()
381+
.with_alpha(0.3)
382+
.rgba_hex();
383+
fill_color.insert(0, '#');
384+
385+
self.render_context.set_line_width((COMPASS_ROSE_HOVER_RING_DIAMETER - COMPASS_ROSE_MAIN_RING_DIAMETER) / 2.);
386+
self.render_context.begin_path();
387+
self.render_context
388+
.arc(position.x, position.y, (COMPASS_ROSE_MAIN_RING_DIAMETER + COMPASS_ROSE_HOVER_RING_DIAMETER) / 4., 0., TAU)
389+
.expect("Failed to draw outer circle");
390+
self.render_context.set_stroke_style_str(fill_color.as_str());
391+
self.render_context.stroke();
392+
}
393+
394+
self.render_context.set_line_width(2.);
395+
self.render_context.begin_path();
396+
self.render_context
397+
.arc(position.x, position.y, (COMPASS_ROSE_MAIN_RING_DIAMETER + 1.) / 2., 0., TAU)
398+
.expect("Failed to draw inner circle");
399+
self.render_context.set_stroke_style_str(COLOR_OVERLAY_BLUE);
400+
self.render_context.stroke();
401+
402+
self.render_context.set_line_width(1.);
403+
self.render_context.set_stroke_style_str(COLOR_OVERLAY_BLUE);
404+
self.render_context.set_fill_style_str(COLOR_OVERLAY_BLUE);
405+
394406
self.render_context.set_line_width(old_line_width);
395407

396408
self.end_dpi_aware_transform();

editor/src/messages/tool/common_functionality/pivot.rs

+37-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::messages::prelude::*;
99

1010
use glam::{DAffine2, DVec2};
1111
use std::collections::VecDeque;
12-
use std::f64::consts::TAU;
12+
use std::f64::consts::FRAC_PI_2;
1313

1414
#[derive(Clone, Debug, PartialEq)]
1515
pub enum CompassRoseState {
@@ -21,13 +21,41 @@ pub enum CompassRoseState {
2121
None,
2222
}
2323

24+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
25+
pub enum Axis {
26+
#[default]
27+
None,
28+
X,
29+
Y,
30+
}
31+
32+
impl Axis {
33+
pub fn is_constraint(&self) -> bool {
34+
matches!(self, Self::X | Self::Y)
35+
}
36+
}
37+
2438
impl CompassRoseState {
2539
pub fn can_grab(&self) -> bool {
2640
matches!(self, Self::HoverRing | Self::AxisX | Self::AxisY)
2741
}
42+
2843
pub fn is_pivot(&self) -> bool {
2944
matches!(self, Self::Pivot)
3045
}
46+
47+
pub fn is_ring(&self) -> bool {
48+
matches!(self, Self::HoverRing | Self::MainRing)
49+
}
50+
51+
pub fn axis_type(&self) -> Axis {
52+
match self {
53+
CompassRoseState::AxisX => Axis::X,
54+
CompassRoseState::AxisY => Axis::Y,
55+
CompassRoseState::HoverRing => Axis::None,
56+
_ => unreachable!(),
57+
}
58+
}
3159
}
3260

3361
#[derive(Clone, Debug)]
@@ -63,6 +91,10 @@ impl Pivot {
6391
layer_transform * bounds_transform
6492
}
6593

94+
pub fn get_position(&self) -> Option<DVec2> {
95+
self.pivot
96+
}
97+
6698
/// Recomputes the pivot position and transform.
6799
fn recalculate_pivot(&mut self, document: &DocumentMessageHandler) {
68100
let selected_nodes = document.network_interface.selected_nodes(&[]).unwrap();
@@ -103,9 +135,8 @@ impl Pivot {
103135
}
104136
}
105137

106-
pub fn update_pivot(&mut self, document: &DocumentMessageHandler, overlay_context: &mut OverlayContext, angle: f64, mouse_position: DVec2) {
138+
pub fn update_pivot(&mut self, document: &DocumentMessageHandler, overlay_context: &mut OverlayContext, angle: f64, show_hover_ring: bool) {
107139
self.recalculate_pivot(document);
108-
let show_hover_ring = matches!(self.is_over(mouse_position, angle), CompassRoseState::HoverRing | CompassRoseState::MainRing);
109140
if let Some(pivot) = self.pivot {
110141
overlay_context.pivot(pivot, angle, show_hover_ring);
111142
}
@@ -146,21 +177,21 @@ impl Pivot {
146177
}
147178

148179
/// Answers if the pointer is currently positioned over the pivot.
149-
pub fn is_over(&self, mouse: DVec2, angle: f64) -> CompassRoseState {
180+
pub fn compass_rose_state(&self, mouse: DVec2, angle: f64) -> CompassRoseState {
150181
match self.pivot {
151182
None => CompassRoseState::None,
152183
Some(pivot) => {
153184
let distance_squared = mouse.distance_squared(pivot);
154185
let ring_radius = (COMPASS_ROSE_MAIN_RING_DIAMETER + 1.) / 2.;
155186

156187
for i in 0..4 {
157-
let base_angle = i as f64 * TAU / 4.0 + angle;
188+
let base_angle = i as f64 * FRAC_PI_2 + angle;
158189
let direction = DVec2::from_angle(base_angle);
159190

160191
let arrow_base = pivot + direction * ring_radius;
161192
let arrow_tip = arrow_base + direction * COMPASS_ROSE_ARROW_SIZE;
162193

163-
let perp = DVec2::from_angle(base_angle + TAU / 4.) * COMPASS_ROSE_ARROW_SIZE / 2.;
194+
let perp = direction.perp() * COMPASS_ROSE_ARROW_SIZE / 2.;
164195
let side1 = arrow_base + perp;
165196
let side2 = arrow_base - perp;
166197

editor/src/messages/tool/tool_messages/select_tool.rs

+30-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(clippy::too_many_arguments)]
22

33
use super::tool_prelude::*;
4-
use crate::consts::{DRAG_DIRECTION_MODE_DETERMINATION_THRESHOLD, ROTATE_INCREMENT, SELECTION_TOLERANCE};
4+
use crate::consts::{COLOR_OVERLAY_GREEN, COLOR_OVERLAY_RED, DRAG_DIRECTION_MODE_DETERMINATION_THRESHOLD, ROTATE_INCREMENT, SELECTION_TOLERANCE};
55
use crate::messages::input_mapper::utility_types::input_mouse::ViewportPosition;
66
use crate::messages::portfolio::document::graph_operation::utility_types::TransformIn;
77
use crate::messages::portfolio::document::overlays::utility_types::OverlayContext;
@@ -12,7 +12,7 @@ use crate::messages::portfolio::document::utility_types::nodes::SelectedNodes;
1212
use crate::messages::portfolio::document::utility_types::transformation::Selected;
1313
use crate::messages::preferences::SelectionMode;
1414
use crate::messages::tool::common_functionality::graph_modification_utils::{get_text, is_layer_fed_by_node_of_name};
15-
use crate::messages::tool::common_functionality::pivot::{CompassRoseState, Pivot};
15+
use crate::messages::tool::common_functionality::pivot::{Axis, Pivot};
1616
use crate::messages::tool::common_functionality::shape_editor::SelectionShapeType;
1717
use crate::messages::tool::common_functionality::snapping::{self, SnapCandidatePoint, SnapData, SnapManager};
1818
use crate::messages::tool::common_functionality::transformation_cage::*;
@@ -270,14 +270,6 @@ impl ToolTransition for SelectTool {
270270
}
271271
}
272272

273-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
274-
pub enum Axis {
275-
#[default]
276-
None,
277-
X,
278-
Y,
279-
}
280-
281273
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
282274
enum SelectToolFsmState {
283275
Ready { selection: NestedSelectionBehavior },
@@ -555,8 +547,30 @@ impl Fsm for SelectToolFsmState {
555547
.map(|bounds| transform * Quad::from_box(bounds))
556548
.map_or(0., |quad| (quad.top_left() - quad.top_right()).to_angle());
557549
let mouse_position = input.mouse.position;
550+
let compass_rose_state = tool_data.pivot.compass_rose_state(mouse_position, angle);
551+
let show_hover_ring = compass_rose_state.is_ring();
558552
// Update pivot
559-
tool_data.pivot.update_pivot(document, &mut overlay_context, angle, mouse_position);
553+
tool_data.pivot.update_pivot(document, &mut overlay_context, angle, show_hover_ring);
554+
if let SelectToolFsmState::Dragging { axis } = self {
555+
if axis.is_constraint() {
556+
let e0 = tool_data
557+
.bounding_box_manager
558+
.as_ref()
559+
.map(|man| man.transform * Quad::from_box(man.bounds))
560+
.map_or(DVec2::X, |quad| (quad.top_left() - quad.top_right()).normalize_or(DVec2::X));
561+
562+
let origin = tool_data.pivot.get_position().unwrap_or(tool_data.drag_start);
563+
let (direction, color) = match axis {
564+
Axis::X => (e0, COLOR_OVERLAY_RED),
565+
Axis::Y => (e0.perp(), COLOR_OVERLAY_GREEN),
566+
_ => unreachable!(),
567+
};
568+
569+
let viewport_diagonal = input.viewport_bounds.size().length();
570+
571+
overlay_context.line(origin - direction * viewport_diagonal, origin + direction * viewport_diagonal, Some(color));
572+
}
573+
}
560574

561575
// Check if the tool is in selection mode
562576
if let Self::Drawing { selection_shape } = self {
@@ -693,11 +707,11 @@ impl Fsm for SelectToolFsmState {
693707
.as_ref()
694708
.map(|man| man.transform * Quad::from_box(man.bounds))
695709
.map_or(0., |quad| (quad.top_left() - quad.top_right()).to_angle());
696-
let compass_ross_state = tool_data.pivot.is_over(input.mouse.position, angle);
710+
let compass_rose_state = tool_data.pivot.compass_rose_state(input.mouse.position, angle);
697711

698712
let state =
699713
// Dragging the pivot
700-
if compass_ross_state.is_pivot() {
714+
if compass_rose_state.is_pivot() {
701715
responses.add(DocumentMessage::StartTransaction);
702716

703717
// tool_data.snap_manager.start_snap(document, input, document.bounding_boxes(), true, true);
@@ -775,7 +789,7 @@ impl Fsm for SelectToolFsmState {
775789
SelectToolFsmState::RotatingBounds
776790
}
777791
// Dragging the selected layers around to transform them
778-
else if compass_ross_state.can_grab() || intersection.is_some_and(|intersection| selected.iter().any(|selected_layer| intersection.starts_with(*selected_layer, document.metadata()))) {
792+
else if compass_rose_state.can_grab() || intersection.is_some_and(|intersection| selected.iter().any(|selected_layer| intersection.starts_with(*selected_layer, document.metadata()))) {
779793
responses.add(DocumentMessage::StartTransaction);
780794

781795
if input.keyboard.key(select_deepest) || tool_data.nested_selection_behavior == NestedSelectionBehavior::Deepest {
@@ -787,13 +801,7 @@ impl Fsm for SelectToolFsmState {
787801
tool_data.layers_dragging = selected;
788802

789803
tool_data.get_snap_candidates(document, input);
790-
let axis = match compass_ross_state{
791-
CompassRoseState::AxisX => Axis::X,
792-
CompassRoseState::AxisY => Axis::Y,
793-
CompassRoseState::HoverRing => Axis::None,
794-
_ => unreachable!()
795-
};
796-
804+
let axis = compass_rose_state.axis_type();
797805
SelectToolFsmState::Dragging{axis}
798806
}
799807
// Dragging a selection box
@@ -1042,7 +1050,7 @@ impl Fsm for SelectToolFsmState {
10421050
.map(|man| man.transform * Quad::from_box(man.bounds))
10431051
.map_or(0., |quad| (quad.top_left() - quad.top_right()).to_angle());
10441052
// Dragging the pivot overrules the other operations
1045-
if tool_data.pivot.is_over(input.mouse.position, angle).is_pivot() {
1053+
if tool_data.pivot.compass_rose_state(input.mouse.position, angle).is_pivot() {
10461054
cursor = MouseCursorIcon::Move;
10471055
}
10481056

0 commit comments

Comments
 (0)