Skip to content

Commit ad78699

Browse files
committed
Dot grid checkbox and remove prefixed Color functions
1 parent 64836e7 commit ad78699

File tree

4 files changed

+59
-116
lines changed

4 files changed

+59
-116
lines changed

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

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use crate::messages::portfolio::document::overlays::utility_types::OverlayContex
33
use crate::messages::portfolio::document::utility_types::misc::{GridSnapping, GridType};
44
use crate::messages::prelude::*;
55
use glam::DVec2;
6-
use graphene_core::renderer::Quad;
76
use graphene_core::raster::color::Color;
7+
use graphene_core::renderer::Quad;
88

99
fn grid_overlay_rectangular(document: &DocumentMessageHandler, overlay_context: &mut OverlayContext, spacing: DVec2) {
1010
let origin = document.snapping_state.grid.origin;
@@ -34,7 +34,11 @@ fn grid_overlay_rectangular(document: &DocumentMessageHandler, overlay_context:
3434
} else {
3535
DVec2::new(secondary_pos, primary_end)
3636
};
37-
overlay_context.line(document_to_viewport.transform_point2(start), document_to_viewport.transform_point2(end), Some(&grid_color.rgb_hex_prefixed()));
37+
overlay_context.line(
38+
document_to_viewport.transform_point2(start),
39+
document_to_viewport.transform_point2(end),
40+
Some(&("#".to_owned() + &grid_color.rgb_hex())),
41+
);
3842
}
3943
}
4044
}
@@ -62,7 +66,11 @@ fn grid_overlay_isometric(document: &DocumentMessageHandler, overlay_context: &m
6266
let x_pos = (((min_x - origin.x) / spacing).ceil() + line_index as f64) * spacing + origin.x;
6367
let start = DVec2::new(x_pos, min_y);
6468
let end = DVec2::new(x_pos, max_y);
65-
overlay_context.line(document_to_viewport.transform_point2(start), document_to_viewport.transform_point2(end), Some(&grid_color.rgb_hex_prefixed()));
69+
overlay_context.line(
70+
document_to_viewport.transform_point2(start),
71+
document_to_viewport.transform_point2(end),
72+
Some(&("#".to_owned() + &grid_color.rgb_hex())),
73+
);
6674
}
6775

6876
for (tan, multiply) in [(tan_a, -1.), (tan_b, 1.)] {
@@ -76,7 +84,11 @@ fn grid_overlay_isometric(document: &DocumentMessageHandler, overlay_context: &m
7684
let y_pos = (((inverse_project(&min_y) - origin.y) / spacing).ceil() + line_index as f64) * spacing + origin.y;
7785
let start = DVec2::new(min_x, project(&DVec2::new(min_x, y_pos)));
7886
let end = DVec2::new(max_x, project(&DVec2::new(max_x, y_pos)));
79-
overlay_context.line(document_to_viewport.transform_point2(start), document_to_viewport.transform_point2(end), Some(&grid_color.rgb_hex_prefixed()));
87+
overlay_context.line(
88+
document_to_viewport.transform_point2(start),
89+
document_to_viewport.transform_point2(end),
90+
Some(&("#".to_owned() + &grid_color.rgb_hex())),
91+
);
8092
}
8193
}
8294
}
@@ -99,17 +111,27 @@ fn grid_overlay_dot(document: &DocumentMessageHandler, overlay_context: &mut Ove
99111
let x_pos = (((min_x - origin.x) / spacing.x).ceil() + line_index as f64) * spacing.x + origin.x;
100112
for line_index in 0..=((max_y - min_y) / spacing.y).ceil() as i32 {
101113
let y_pos = (((min_y - origin.y) / spacing.y).ceil() + line_index as f64) * spacing.y + origin.y;
102-
let circle_pos = DVec2::new(x_pos, y_pos);
103-
overlay_context.circle(document_to_viewport.transform_point2(circle_pos), 1., Some(&grid_color.rgb_hex_prefixed()), Some(&grid_color.rgb_hex_prefixed()));
114+
let circle_pos = DVec2::new(x_pos, y_pos);
115+
overlay_context.circle(
116+
document_to_viewport.transform_point2(circle_pos),
117+
1.,
118+
Some(&("#".to_owned() + &grid_color.rgb_hex())),
119+
Some(&("#".to_owned() + &grid_color.rgb_hex())),
120+
);
104121
}
105-
}
122+
}
106123
}
107124

108125
pub fn grid_overlay(document: &DocumentMessageHandler, overlay_context: &mut OverlayContext) {
109126
match document.snapping_state.grid.grid_type {
110-
GridType::Rectangle { spacing } => grid_overlay_rectangular(document, overlay_context, spacing),
127+
GridType::Rectangle { spacing } => {
128+
if document.snapping_state.grid.dot_display {
129+
grid_overlay_dot(document, overlay_context, spacing)
130+
} else {
131+
grid_overlay_rectangular(document, overlay_context, spacing)
132+
}
133+
}
111134
GridType::Isometric { y_axis_spacing, angle_a, angle_b } => grid_overlay_isometric(document, overlay_context, y_axis_spacing, angle_a, angle_b),
112-
GridType::Dot { spacing } => grid_overlay_dot(document, overlay_context, spacing),
113135
}
114136
}
115137

@@ -141,6 +163,14 @@ pub fn overlay_options(grid: &GridSnapping) -> Vec<LayoutGroup> {
141163
}
142164
})
143165
};
166+
let update_display = |grid, update: fn(&mut GridSnapping) -> Option<&mut bool>| {
167+
update_val::<CheckboxInput>(grid, move |grid, val| {
168+
if let Some(update) = update(grid) {
169+
*update = val.checked;
170+
}
171+
})
172+
};
173+
144174
widgets.push(LayoutGroup::Row {
145175
widgets: vec![
146176
TextLabel::new("Origin").table_align(true).widget_holder(),
@@ -171,15 +201,11 @@ pub fn overlay_options(grid: &GridSnapping) -> Vec<LayoutGroup> {
171201
RadioEntryData::new("isometric")
172202
.label("Isometric")
173203
.on_update(update_val(grid, |grid, _| grid.grid_type = GridType::ISOMETRIC)),
174-
RadioEntryData::new("dot")
175-
.label("Dot")
176-
.on_update(update_val(grid, |grid, _| grid.grid_type = GridType::DOT)),
177204
])
178205
.min_width(200)
179206
.selected_index(Some(match grid.grid_type {
180207
GridType::Rectangle { .. } => 0,
181208
GridType::Isometric { .. } => 1,
182-
GridType::Dot { .. } => 2,
183209
}))
184210
.widget_holder(),
185211
],
@@ -237,38 +263,25 @@ pub fn overlay_options(grid: &GridSnapping) -> Vec<LayoutGroup> {
237263
.widget_holder(),
238264
],
239265
});
240-
},
241-
GridType::Dot { spacing } => widgets.push(LayoutGroup::Row {
266+
}
267+
}
268+
match grid.grid_type {
269+
GridType::Rectangle { spacing } => widgets.push(LayoutGroup::Row {
242270
widgets: vec![
243-
TextLabel::new("Spacing").table_align(true).widget_holder(),
271+
TextLabel::new("Dot display").table_align(true).widget_holder(),
244272
Separator::new(SeparatorType::Unrelated).widget_holder(),
245-
NumberInput::new(Some(spacing.x))
246-
.label("X")
247-
.unit(" px")
248-
.min(0.)
249-
.min_width(98)
250-
.on_update(update_origin(grid, |grid| grid.grid_type.dot_spacing().map(|spacing| &mut spacing.x)))
251-
.widget_holder(),
252-
Separator::new(SeparatorType::Related).widget_holder(),
253-
NumberInput::new(Some(spacing.y))
254-
.label("Y")
255-
.unit(" px")
256-
.min(0.)
257-
.min_width(98)
258-
.on_update(update_origin(grid, |grid| grid.grid_type.dot_spacing().map(|spacing| &mut spacing.y)))
259-
.widget_holder(),
273+
CheckboxInput::new(grid.dot_display).on_update(update_display(grid, |grid| Some(&mut grid.dot_display))).widget_holder(),
260274
],
261275
}),
276+
GridType::Isometric { y_axis_spacing, angle_a, angle_b } => {}
262277
}
263-
264-
widgets.push(LayoutGroup::Row{
265-
widgets: vec![
278+
widgets.push(LayoutGroup::Row {
279+
widgets: vec![
266280
TextLabel::new("Color").table_align(true).widget_holder(),
267281
Separator::new(SeparatorType::Unrelated).widget_holder(),
268-
ColorButton::new(Some(grid.grid_color))
269-
.on_update(update_color(grid, |grid| Some(&mut grid.grid_color)))
270-
.widget_holder(),
271-
]});
282+
ColorButton::new(Some(grid.grid_color)).on_update(update_color(grid, |grid| Some(&mut grid.grid_color))).widget_holder(),
283+
],
284+
});
272285

273286
widgets
274287
}

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use glam::DVec2;
2-
use std::fmt;
31
use crate::consts::COLOR_OVERLAY_GRAY;
2+
use glam::DVec2;
43
use graphene_core::raster::Color;
4+
use std::{fmt, path::Display};
55

66
#[repr(transparent)]
77
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize, specta::Type)]
@@ -92,7 +92,11 @@ impl Default for SnappingState {
9292
grid: GridSnapping {
9393
origin: DVec2::ZERO,
9494
grid_type: GridType::RECTANGLE,
95-
grid_color: Color::from_rgb_str_prefixed(COLOR_OVERLAY_GRAY.to_uppercase().as_str()).expect("color"),
95+
grid_color: COLOR_OVERLAY_GRAY
96+
.strip_prefix("#")
97+
.and_then(|value| Color::from_rgb_str(value))
98+
.expect("Should create Color from prefixed hex string"),
99+
dot_display: false,
96100
},
97101
tolerance: 8.,
98102
artboards: true,
@@ -147,7 +151,6 @@ pub struct PointSnapping {
147151
pub enum GridType {
148152
Rectangle { spacing: DVec2 },
149153
Isometric { y_axis_spacing: f64, angle_a: f64, angle_b: f64 },
150-
Dot { spacing: DVec2 },
151154
}
152155
impl GridType {
153156
pub const RECTANGLE: Self = GridType::Rectangle { spacing: DVec2::ONE };
@@ -156,7 +159,6 @@ impl GridType {
156159
angle_a: 30.,
157160
angle_b: 30.,
158161
};
159-
pub const DOT: Self = GridType::Dot { spacing: DVec2::ONE };
160162
pub fn rect_spacing(&mut self) -> Option<&mut DVec2> {
161163
match self {
162164
Self::Rectangle { spacing } => Some(spacing),
@@ -181,18 +183,13 @@ impl GridType {
181183
_ => None,
182184
}
183185
}
184-
pub fn dot_spacing(&mut self) -> Option<&mut DVec2> {
185-
match self {
186-
Self::Dot { spacing } => Some(spacing),
187-
_ => None,
188-
}
189-
}
190186
}
191187
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq)]
192188
pub struct GridSnapping {
193189
pub origin: DVec2,
194190
pub grid_type: GridType,
195191
pub grid_color: Color,
192+
pub dot_display: bool,
196193
}
197194
impl GridSnapping {
198195
// Double grid size until it takes up at least 10px.

editor/src/messages/tool/common_functionality/snapping/grid_snapper.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ impl GridSnapper {
9393
match snap_data.document.snapping_state.grid.grid_type {
9494
GridType::Rectangle { spacing } => self.get_snap_lines_rectangular(document_point, snap_data, spacing),
9595
GridType::Isometric { y_axis_spacing, angle_a, angle_b } => self.get_snap_lines_isometric(document_point, snap_data, y_axis_spacing, angle_a, angle_b),
96-
GridType::Dot { spacing } => self.get_snap_lines_rectangular(document_point, snap_data, spacing),
9796
}
9897
}
9998

node-graph/gcore/src/raster/color.rs

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -749,24 +749,6 @@ impl Color {
749749
(self.a() * 255.) as u8,
750750
)
751751
}
752-
/// Return an 8-character RGBA hex string (with a # prefix).
753-
///
754-
/// # Examples
755-
/// ```
756-
/// use graphene_core::raster::color::Color;
757-
/// let color = Color::from_rgba8_srgb(0x52, 0x67, 0xFA, 0x61).to_gamma_srgb();
758-
/// assert_eq!("#3240A261", color.rgba_hex_prefixed())
759-
/// ```
760-
#[cfg(feature = "std")]
761-
pub fn rgba_hex_prefixed(&self) -> String {
762-
format!(
763-
"#{:02X?}{:02X?}{:02X?}{:02X?}",
764-
(self.r() * 255.) as u8,
765-
(self.g() * 255.) as u8,
766-
(self.b() * 255.) as u8,
767-
(self.a() * 255.) as u8,
768-
)
769-
}
770752

771753
/// Return a 6-character RGB hex string (without a # prefix).
772754
/// ```
@@ -779,17 +761,6 @@ impl Color {
779761
format!("{:02X?}{:02X?}{:02X?}", (self.r() * 255.) as u8, (self.g() * 255.) as u8, (self.b() * 255.) as u8)
780762
}
781763

782-
/// Return a 6-character RGB hex string (with a # prefix).
783-
/// ```
784-
/// use graphene_core::raster::color::Color;
785-
/// let color = Color::from_rgba8_srgb(0x52, 0x67, 0xFA, 0x61).to_gamma_srgb();
786-
/// assert_eq!("#3240A2", color.rgb_hex_prefixed())
787-
/// ```
788-
#[cfg(feature = "std")]
789-
pub fn rgb_hex_prefixed(&self) -> String {
790-
format!("#{:02X?}{:02X?}{:02X?}", (self.r() * 255.) as u8, (self.g() * 255.) as u8, (self.b() * 255.) as u8)
791-
}
792-
793764
/// Return the all components as a u8 slice, first component is red, followed by green, followed by blue, followed by alpha.
794765
///
795766
/// # Examples
@@ -860,26 +831,6 @@ impl Color {
860831
Some(Color::from_rgba8_srgb(r, g, b, a))
861832
}
862833

863-
/// Creates a color from a 8-character RGBA hex string (with a # prefix).
864-
///
865-
/// # Examples
866-
/// ```
867-
/// use graphene_core::raster::color::Color;
868-
/// let color = Color::from_rgba_str_prefixed("#7C67FA61").unwrap();
869-
/// ```
870-
pub fn from_rgba_str_prefixed(color_str: &str) -> Option<Color> {
871-
if color_str.len() != 9 {
872-
return None;
873-
}
874-
let color_str = &color_str[1..];
875-
let r = u8::from_str_radix(&color_str[0..2], 16).ok()?;
876-
let g = u8::from_str_radix(&color_str[2..4], 16).ok()?;
877-
let b = u8::from_str_radix(&color_str[4..6], 16).ok()?;
878-
let a = u8::from_str_radix(&color_str[6..8], 16).ok()?;
879-
880-
Some(Color::from_rgba8_srgb(r, g, b, a))
881-
}
882-
883834
/// Creates a color from a 6-character RGB hex string (without a # prefix).
884835
/// ```
885836
/// use graphene_core::raster::color::Color;
@@ -896,23 +847,6 @@ impl Color {
896847
Some(Color::from_rgb8_srgb(r, g, b))
897848
}
898849

899-
/// Creates a color from a 6-character RGB hex string (with a # prefix).
900-
/// ```
901-
/// use graphene_core::raster::color::Color;
902-
/// let color = Color::from_rgb_str_prefixed("7C67FA").unwrap();
903-
/// ```
904-
pub fn from_rgb_str_prefixed(color_str: &str) -> Option<Color> {
905-
if color_str.len() != 7 {
906-
return None;
907-
}
908-
let color_str = &color_str[1..];
909-
let r = u8::from_str_radix(&color_str[0..2], 16).ok()?;
910-
let g = u8::from_str_radix(&color_str[2..4], 16).ok()?;
911-
let b = u8::from_str_radix(&color_str[4..6], 16).ok()?;
912-
913-
Some(Color::from_rgb8_srgb(r, g, b))
914-
}
915-
916850
/// Linearly interpolates between two colors based on t.
917851
///
918852
/// T must be between 0 and 1.

0 commit comments

Comments
 (0)