mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Star tool: the points field sets the next star's points, 3 to 20
The Star tool's Sides field edited the Polygon tool's sides, and every star drag drew 5 points. The Star tool now has its own star_points option (5 to start and after Reset Tool), shown in the field and used by the drag. A value past 20 becomes 20 and a value under 3 becomes 3, each with the alert "An integer between 3 and 20 is required. Closest value inserted.". Fixes #16867.
This commit is contained in:
1 parent
e22facf45b
commit
fcd6eed991
5 files changed
+212
-6
No files matched your search
@@ -162,6 +162,9 @@ pub struct ToolOptions {
|
||||
#[serde(rename = "useAlignedRendering", alias = "align_edges")]
|
||||
pub align_edges: bool,
|
||||
pub polygon_sides: u32,
|
||||
/// The Star tool's points for the next star (#16867): its own, apart from the Polygon tool's
|
||||
/// [`Self::polygon_sides`]. 3 to 20; 5 to start and after Reset Tool.
|
||||
pub star_points: u32,
|
||||
/// The Polygon tool's corner radius in px for the next polygon (#13302): its own, not the
|
||||
/// Rectangle's [`Self::corner_radius`]. 0 draws sharp corners.
|
||||
pub polygon_corner_radius: f32,
|
||||
@@ -508,6 +511,7 @@ impl Default for ToolOptions {
|
||||
corner_radius: 0.0,
|
||||
align_edges: true,
|
||||
polygon_sides: 5,
|
||||
star_points: 5,
|
||||
polygon_corner_radius: 0.0,
|
||||
line_weight: 1.0,
|
||||
arrow_start: false,
|
||||
|
||||
@@ -146,14 +146,15 @@ fn paint_edit(stroke: bool, change: &PaintChange, width: f32) -> Value {
|
||||
/// and `ShapeBar` reports an edit only in the pass that read the input, so a second pass in the
|
||||
/// same frame sees the stored result and writes nothing.
|
||||
/// The bar's number fields: the next shape's, or the selected rectangle's own radius when the bar edits it.
|
||||
fn tool_settings(o: &crate::state::ToolOptions, live: Option<&photocraft_doc::LiveShape>, rect_target: bool) -> ShapeToolSettings {
|
||||
fn tool_settings(o: &crate::state::ToolOptions, kind: ShapeKind, live: Option<&photocraft_doc::LiveShape>, rect_target: bool) -> ShapeToolSettings {
|
||||
let mut settings = ShapeToolSettings::default();
|
||||
settings.fill_on = o.shape_fill;
|
||||
settings.corner_radius = match live {
|
||||
Some(photocraft_doc::LiveShape::Rect { radii, .. }) if rect_target => radii[0],
|
||||
_ => f64::from(o.corner_radius),
|
||||
};
|
||||
settings.sides = o.polygon_sides;
|
||||
// The Star tool's points are its own, apart from the Polygon tool's sides (#16867).
|
||||
settings.sides = if kind == ShapeKind::Star { o.star_points } else { o.polygon_sides };
|
||||
settings.line_weight = f64::from(o.line_weight);
|
||||
settings.stroke_width = f64::from(o.stroke_width);
|
||||
settings
|
||||
@@ -173,6 +174,55 @@ fn tool_extras(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The points a Star tool's star may have: whole numbers 3 to 20, as the reference's options bar (#16867).
|
||||
const STAR_POINTS: std::ops::RangeInclusive<f64> = 3.0..=20.0;
|
||||
|
||||
/// The star points a Sides change of `n` sets (`None`: keep them), and whether it was a value past 20 the user is
|
||||
/// told about (the reference's "An integer between 3 and 20 is required" alert). The framework's Sides cell holds
|
||||
/// 3 to 100 and reports values past that itself; this covers 21 to 100 until its Star cell holds 3 to 20.
|
||||
/// - A proposal past 20 while the gesture runs (`committed` false) keeps the points: a clamped value here would end
|
||||
/// the field's gesture before it commits.
|
||||
/// - The committed value is clamped. `shown` is the value the field showed: a committed 21 from 20 is an arrow step
|
||||
/// past the end, which stops there without an alert, as the field's own ends do.
|
||||
fn star_points(n: u32, shown: u32, committed: bool) -> (Option<u32>, bool) {
|
||||
let (low, high) = (*STAR_POINTS.start() as u32, *STAR_POINTS.end() as u32);
|
||||
if n <= high {
|
||||
return (Some(n.max(low)), false);
|
||||
}
|
||||
if !committed {
|
||||
return (None, false);
|
||||
}
|
||||
let stepped_past_end = n == high + 1 && shown == high;
|
||||
(Some(high), !stepped_past_end)
|
||||
}
|
||||
|
||||
/// Stores the Star tool's points from a Sides change of `n` ([`star_points`]); true when it was a value past 20 the
|
||||
/// user is told about.
|
||||
fn set_star_points(app: &mut PhotocraftApp, n: u32, shown: u32, committed: bool) -> bool {
|
||||
let (points, alert) = star_points(n, shown, committed);
|
||||
if let Some(points) = points {
|
||||
app.ui.tool_options.star_points = points;
|
||||
}
|
||||
alert
|
||||
}
|
||||
|
||||
/// The range alert of the bar's cell, with a star's count past the shared Sides range (3 to 100) naming the star's own.
|
||||
fn star_range(kind: ShapeKind, mut range: craft::ui::text::RangeAlert) -> craft::ui::text::RangeAlert {
|
||||
if kind == ShapeKind::Star {
|
||||
range.range = STAR_POINTS;
|
||||
range.kept = range.kept.clamp(*STAR_POINTS.start(), *STAR_POINTS.end());
|
||||
}
|
||||
range
|
||||
}
|
||||
|
||||
/// The alert of a star points value typed past 20: the text fields' whole-number range sentence over 3 to 20.
|
||||
fn star_points_range_alert(title: String, env: &Env) -> Alert<'static> {
|
||||
let words = env.words::<craft::ui::text::TextWords>();
|
||||
let units = env.units();
|
||||
let (low, high) = (units.number(*STAR_POINTS.start(), 0..=0), units.number(*STAR_POINTS.end(), 0..=0));
|
||||
Alert::new(title, words.range_integer.fill(&[("low", low.as_str()), ("high", high.as_str())]))
|
||||
}
|
||||
|
||||
/// The title of the alert for a number typed outside its range.
|
||||
fn range_title(kind: ShapeKind) -> String {
|
||||
match kind {
|
||||
@@ -213,7 +263,7 @@ pub(crate) fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool
|
||||
// selection tools, whose Radius is its own.
|
||||
let rect_target = matches!(tool, Tool::PathSelection | Tool::DirectSelection)
|
||||
&& matches!(target.as_ref().and_then(|s| s.live.as_ref()), Some(photocraft_doc::LiveShape::Rect { .. }));
|
||||
let settings = tool_settings(o, target.as_ref().and_then(|s| s.live.as_ref()), rect_target);
|
||||
let settings = tool_settings(o, kind, target.as_ref().and_then(|s| s.live.as_ref()), rect_target);
|
||||
let style = stroke.as_ref().map_or_else(|| o.shape_stroke.clone(), StrokeOptions::from);
|
||||
let locked = target.as_ref().is_some_and(|s| {
|
||||
app.session.active().is_some_and(|st| {
|
||||
@@ -245,6 +295,7 @@ pub(crate) fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool
|
||||
|
||||
let mut recent: Option<[f32; 3]> = None;
|
||||
let mut remembered: Vec<Fill> = Vec::new();
|
||||
let mut star_points_alert = false;
|
||||
for event in &out.events {
|
||||
let Some(change) = proposal(event) else { continue };
|
||||
if matches!(event, EditEvent::Cancel { .. }) && already_shown(change, &appearance, &settings) {
|
||||
@@ -313,6 +364,7 @@ pub(crate) fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool
|
||||
edit = Some(json!({"radii": r}));
|
||||
}
|
||||
}
|
||||
ShapeChange::Sides(n) if kind == ShapeKind::Star => star_points_alert |= set_star_points(app, *n, settings.sides, committed),
|
||||
ShapeChange::Sides(n) => app.ui.tool_options.polygon_sides = *n,
|
||||
ShapeChange::Weight(w) => app.ui.tool_options.line_weight = *w as f32,
|
||||
_ => {}
|
||||
@@ -350,8 +402,11 @@ pub(crate) fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool
|
||||
if let Some((which, gesture)) = out.screen_pick {
|
||||
crate::screen_picker::hold(app, ui.ctx(), ("shape-bar", which), gesture);
|
||||
}
|
||||
if let Some(range) = out.out_of_range {
|
||||
let alert = Alert::range(range_title(kind), &range, &Env::of(ui));
|
||||
let alert = match out.out_of_range {
|
||||
Some(range) => Some(Alert::range(range_title(kind), &star_range(kind, range), &Env::of(ui))),
|
||||
None => star_points_alert.then(|| star_points_range_alert(range_title(kind), &Env::of(ui))),
|
||||
};
|
||||
if let Some(alert) = alert {
|
||||
// An `Err` is the alert that was not opened because one is already up.
|
||||
let _ = app.framework.dialogs.open_unless_open(HostedAlert::new(alert));
|
||||
}
|
||||
@@ -493,6 +548,10 @@ mod rectangle_radius_target_tests;
|
||||
#[path = "polygon_sides_alert_tests.rs"]
|
||||
mod polygon_sides_alert_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "star_points_field_tests.rs"]
|
||||
mod star_points_field_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "polygon_corner_radius_field_tests.rs"]
|
||||
mod polygon_corner_radius_field_tests;
|
||||
@@ -0,0 +1,142 @@
|
||||
//! #16867: the Star tool's number-of-points field sets the next star's points, whole numbers 3 to 20 (a value typed
|
||||
//! past either end becomes the closest one with the reference's "An integer between 3 and 20 is required. Closest
|
||||
//! value inserted." alert), 5 to start and after Reset Tool, and kept apart from the Polygon tool's sides.
|
||||
|
||||
use egui::accesskit::Role;
|
||||
use egui::vec2;
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::{NodeT, Queryable};
|
||||
use photocraft_doc::{LayerContent, LiveShape};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::state::Tool;
|
||||
|
||||
const ALERT: &str = "An integer between 3 and 20 is required. Closest value inserted.";
|
||||
|
||||
/// The options bar of `tool`, with the dialog host that draws the alerts.
|
||||
fn bar(tool: Tool) -> Harness<'static, Box<PhotocraftApp>> {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 800, "height": 600})).unwrap();
|
||||
app.ui.tool = tool;
|
||||
let mut h = Harness::builder().with_context(app.ctx().clone()).with_size(vec2(1800.0, 300.0)).with_step_dt(1.0 / 60.0).build_ui_state(
|
||||
move |ui, app: &mut Box<PhotocraftApp>| {
|
||||
crate::panels::options_bar(app, ui);
|
||||
crate::dialogs::run(app, ui);
|
||||
},
|
||||
app,
|
||||
);
|
||||
h.run_steps(4);
|
||||
h
|
||||
}
|
||||
|
||||
/// Types `text` into the Sides field, then Tabs out of it.
|
||||
fn type_sides(h: &mut Harness<'static, Box<PhotocraftApp>>, text: &str) {
|
||||
h.query_all_by_role(Role::SpinButton).find(|n| n.accesskit_node().label().as_deref() == Some("Sides")).expect("the Sides field").click();
|
||||
h.run_steps(1);
|
||||
h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
|
||||
h.event(egui::Event::Text(text.into()));
|
||||
h.run_steps(1);
|
||||
h.key_press(egui::Key::Tab);
|
||||
h.run_steps(3);
|
||||
}
|
||||
|
||||
/// The messages of the open alerts.
|
||||
fn alerts(h: &Harness<'static, Box<PhotocraftApp>>) -> Vec<String> {
|
||||
h.state().framework.dialogs.open_dialogs().iter().filter_map(|d| d.fields.as_ref()?.get("message")?.as_str().map(str::to_string)).collect()
|
||||
}
|
||||
|
||||
/// A Star tool drag from 400,300 to 501,401 on the canvas: the live shape it makes, and its anchor count.
|
||||
fn drag_star(app: &mut PhotocraftApp) -> (LiveShape, usize) {
|
||||
use crate::canvas::testing;
|
||||
app.ui.tool = Tool::Star;
|
||||
app.sync_views();
|
||||
testing::press(app, [400.0, 300.0], egui::Modifiers::NONE);
|
||||
testing::move_to(app, [450.0, 350.0], egui::Modifiers::NONE);
|
||||
testing::move_to(app, [501.0, 401.0], egui::Modifiers::NONE);
|
||||
testing::release(app, [501.0, 401.0], egui::Modifiers::NONE);
|
||||
let st = app.session.active().expect("a document");
|
||||
let layer = st.doc.layer(st.active_layer.expect("an active layer")).expect("the layer");
|
||||
let LayerContent::Shape(shape) = &layer.content else { panic!("the drag makes a shape layer, got {:?}", layer.name) };
|
||||
let live = shape.live.clone().expect("a live star");
|
||||
let anchors = photocraft_vector::shapes::live_path(&live).subpaths.iter().map(|s| s.knots.len()).sum();
|
||||
(live, anchors)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_field_sets_the_next_stars_points() {
|
||||
let mut h = bar(Tool::Star);
|
||||
type_sides(&mut h, "8");
|
||||
assert_eq!(h.state().ui.tool_options.star_points, 8);
|
||||
assert!(alerts(&h).is_empty(), "8 is in range");
|
||||
let (live, anchors) = drag_star(h.state_mut());
|
||||
assert!(matches!(live, LiveShape::Polygon { sides: 8, .. }), "an 8-point star, got {live:?}");
|
||||
assert_eq!(anchors, 16, "8 points give 16 anchors");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn points_past_the_range_take_the_closest_value_with_the_3_to_20_alert() {
|
||||
for (typed, kept) in [("25", 20), ("2", 3), ("21", 20), ("100", 20), ("1000", 20), ("0", 3)] {
|
||||
let mut h = bar(Tool::Star);
|
||||
type_sides(&mut h, typed);
|
||||
assert_eq!(h.state().ui.tool_options.star_points, kept, "{typed} keeps {kept}");
|
||||
assert_eq!(alerts(&h), vec![ALERT.to_string()], "{typed}: one alert with the star's range");
|
||||
h.get_by_label("OK").click();
|
||||
h.run_steps(4);
|
||||
assert!(alerts(&h).is_empty(), "{typed}: OK closes the alert");
|
||||
assert_eq!(h.state().ui.tool_options.star_points, kept, "{typed}: the value stays after OK");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn points_in_range_are_kept_without_an_alert() {
|
||||
for (typed, kept) in [("3", 3), ("6", 6), ("20", 20)] {
|
||||
let mut h = bar(Tool::Star);
|
||||
type_sides(&mut h, typed);
|
||||
assert_eq!(h.state().ui.tool_options.star_points, kept, "{typed}");
|
||||
assert!(alerts(&h).is_empty(), "{typed} is in range");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn star_points_and_polygon_sides_are_kept_apart() {
|
||||
let mut h = bar(Tool::Star);
|
||||
type_sides(&mut h, "8");
|
||||
assert_eq!((h.state().ui.tool_options.star_points, h.state().ui.tool_options.polygon_sides), (8, 5), "the star's points leave the sides");
|
||||
|
||||
let mut h = bar(Tool::Polygon);
|
||||
type_sides(&mut h, "30");
|
||||
assert_eq!((h.state().ui.tool_options.polygon_sides, h.state().ui.tool_options.star_points), (30, 5), "the sides leave the star's points");
|
||||
assert!(alerts(&h).is_empty(), "30 sides are in the Polygon tool's range");
|
||||
let (live, _) = drag_star(h.state_mut());
|
||||
assert!(matches!(live, LiveShape::Polygon { sides: 5, .. }), "the next star keeps 5 points, got {live:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_field_shows_the_stars_points() {
|
||||
let mut h = bar(Tool::Star);
|
||||
h.state_mut().ui.tool_options.polygon_sides = 9;
|
||||
h.state_mut().ui.tool_options.star_points = 7;
|
||||
h.run_steps(2);
|
||||
let shown =
|
||||
h.query_all_by_role(Role::SpinButton).find(|n| n.accesskit_node().label().as_deref() == Some("Sides")).and_then(|n| n.accesskit_node().numeric_value());
|
||||
assert_eq!(shown, Some(7.0), "the Star tool's field shows its points, not the Polygon tool's sides");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reset_tool_gives_5_points() {
|
||||
let mut o = crate::state::ToolOptions::default();
|
||||
assert_eq!(o.star_points, 5, "5 to start");
|
||||
o.star_points = 12;
|
||||
crate::tools::tool_reset::reset_options(&mut o, Tool::Star);
|
||||
assert_eq!(o.star_points, 5, "Reset Tool gives 5");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_arrow_step_past_20_stops_there_without_an_alert() {
|
||||
assert_eq!(super::star_points(21, 20, true), (Some(20), false), "a step from 20 stops at 20");
|
||||
assert_eq!(super::star_points(21, 8, true), (Some(20), true), "21 typed over 8 alerts");
|
||||
assert_eq!(super::star_points(40, 20, false), (None, false), "a running gesture past 20 keeps the points");
|
||||
assert_eq!(super::star_points(12, 5, true), (Some(12), false), "in range");
|
||||
assert_eq!(super::star_points(12, 5, false), (Some(12), false), "in range while the gesture runs");
|
||||
}
|
||||
@@ -299,6 +299,7 @@ pub fn option_keys(tool: Tool) -> &'static [&'static str] {
|
||||
corner_radius,
|
||||
align_edges,
|
||||
polygon_sides,
|
||||
star_points,
|
||||
polygon_corner_radius,
|
||||
line_weight
|
||||
)
|
||||
|
||||
@@ -59,7 +59,7 @@ fn configure(app: &PhotocraftApp, state: &mut VectorTools) -> Which {
|
||||
if super::shapes::is_shape_tool(tool) {
|
||||
let (sides, ratio) = match tool {
|
||||
Tool::Triangle => (3, 1.0),
|
||||
Tool::Star => (5, 0.5),
|
||||
Tool::Star => (o.star_points.clamp(3, 20), 0.5),
|
||||
_ => (o.polygon_sides, 1.0),
|
||||
};
|
||||
let mut options = ShapeDragOptions::new(super::kind_of(tool))
|
||||
|
||||
Reference in new issue
Block a user