mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 02:32:33 +00:00
ui: add connected-square reference point presentation
This commit is contained in:
@@ -26,4 +26,4 @@ pub use tree_picker::{TreeEntry, TreePicker, TreePickerStyle};
|
|||||||
pub use unit_select::{UnitSelect, UnitSelectOutput, UnitSelectStyle};
|
pub use unit_select::{UnitSelect, UnitSelectOutput, UnitSelectStyle};
|
||||||
|
|
||||||
pub use craft_behavior::Anchor9;
|
pub use craft_behavior::Anchor9;
|
||||||
pub use reference_point::{ReferencePoint, ReferencePointStyle};
|
pub use reference_point::{PointPresentation, ReferencePoint, ReferencePointStyle};
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
//! `ReferencePoint`: the 3 by 3 picker of a box's nine points. A leaf with a part per point, one roving group over a
|
//! `ReferencePoint`: the 3 by 3 picker of a box's nine points. A leaf with a part per point, one roving group over a
|
||||||
//! grid: one Tab stop, the arrows move the focus over the grid in both directions and stop at its edges, Home and End
|
//! grid: one Tab stop, the arrows move the focus over the grid in both directions and stop at its edges, Home and End
|
||||||
//! go to the first and last point, and Space, Enter or a click picks the focused one.
|
//! go to the first and last point, and Space, Enter or a click picks the focused one. A `PointPresentation` picks how a
|
||||||
|
//! point is marked: a dot, or a square joined to the squares beside it.
|
||||||
|
|
||||||
use super::words::PickerWords;
|
use super::words::PickerWords;
|
||||||
use crate::contract::{
|
use crate::contract::{
|
||||||
Access, Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, Input, MeasureCx, Name, PaintUi, PartsCx, Props as _, Role, SetPosition,
|
Access, Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, Input, MeasureCx, Name, PaintUi, PartsCx, Props as _, Role, SetPosition,
|
||||||
Styled as _, WordSetCx as _,
|
Styled as _, WordSetCx as _,
|
||||||
};
|
};
|
||||||
use crate::primitives::{PaintShape, StrokeWidth};
|
use crate::primitives::{Draw, PaintShape, StrokeWidth};
|
||||||
use crate::runtime::env::Label;
|
use crate::runtime::env::Label;
|
||||||
use crate::runtime::focus::{ActivateKeys, KeyUse};
|
use crate::runtime::focus::{ActivateKeys, KeyUse};
|
||||||
use crate::style::{ComponentStyle, Flags, FocusLook, Stateful, States, StyleColor, StyleCx, SurfaceKind, SurfaceLook, UiTokens};
|
use crate::style::{ComponentStyle, Flags, FocusLook, Stateful, States, StyleColor, StyleCx, SurfaceKind, SurfaceLook, UiTokens};
|
||||||
@@ -25,6 +26,10 @@ const GRID: usize = 3;
|
|||||||
/// The side of the dot every point shows; the large picker draws the picked one twice as wide.
|
/// The side of the dot every point shows; the large picker draws the picked one twice as wide.
|
||||||
const DOT: f32 = 3.5;
|
const DOT: f32 = 3.5;
|
||||||
|
|
||||||
|
/// The side of a point's square in the connected-squares presentation, as a share of its cell: the observed proxy's
|
||||||
|
/// squares leave about a third of the pitch for the line that joins them.
|
||||||
|
const SQUARE: f32 = 0.7;
|
||||||
|
|
||||||
/// The arrow's tip, from the cell's centre, as a share of the arrow's extent.
|
/// The arrow's tip, from the cell's centre, as a share of the arrow's extent.
|
||||||
const ARROW_TIP: f32 = 0.5;
|
const ARROW_TIP: f32 = 0.5;
|
||||||
/// The arrow's tail, behind the cell's centre, as a share of the arrow's extent.
|
/// The arrow's tail, behind the cell's centre, as a share of the arrow's extent.
|
||||||
@@ -36,6 +41,18 @@ const ARROW_HEAD: f32 = 3.5 / 12.0;
|
|||||||
/// The head's half width, as a share of the arrow's extent.
|
/// The head's half width, as a share of the arrow's extent.
|
||||||
const ARROW_HEAD_HALF_WIDTH: f32 = 3.5 / 12.0;
|
const ARROW_HEAD_HALF_WIDTH: f32 = 3.5 / 12.0;
|
||||||
|
|
||||||
|
/// How a reference point picker marks its nine points.
|
||||||
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
|
||||||
|
#[non_exhaustive]
|
||||||
|
pub enum PointPresentation {
|
||||||
|
/// A dot at each point, the picked point's dot twice as wide: the default.
|
||||||
|
#[default]
|
||||||
|
Dots,
|
||||||
|
/// A square at each point, each joined to the squares beside it by a line, so the nine points read as the box's
|
||||||
|
/// grid. The picked point's square is filled; the others are outlined.
|
||||||
|
ConnectedSquares,
|
||||||
|
}
|
||||||
|
|
||||||
/// The style of a reference point picker.
|
/// The style of a reference point picker.
|
||||||
#[derive(Clone, Debug, PartialEq, ComponentStyle)]
|
#[derive(Clone, Debug, PartialEq, ComponentStyle)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
@@ -52,23 +69,28 @@ pub struct ReferencePointStyle {
|
|||||||
dot: f32,
|
dot: f32,
|
||||||
/// The side of the picked point's dot.
|
/// The side of the picked point's dot.
|
||||||
picked_dot: f32,
|
picked_dot: f32,
|
||||||
|
/// The side of one point's square: the connected-squares presentation's mark.
|
||||||
|
square: f32,
|
||||||
/// The dots' corner radius.
|
/// The dots' corner radius.
|
||||||
dot_radius: egui::CornerRadius,
|
dot_radius: egui::CornerRadius,
|
||||||
/// The picked point's dot.
|
/// The picked point's dot, or the fill of its square.
|
||||||
ink: Stateful<StyleColor>,
|
ink: Stateful<StyleColor>,
|
||||||
/// The side of the square one growth arrow fits in.
|
/// The side of the square one growth arrow fits in.
|
||||||
arrow: f32,
|
arrow: f32,
|
||||||
/// The unpicked dots and the growth arrows.
|
/// The unpicked dots and squares, the lines between the squares and the growth arrows.
|
||||||
arrow_ink: Stateful<StyleColor>,
|
arrow_ink: Stateful<StyleColor>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The 3 by 3 picker of a box's nine points: the point a transform, a resize or a watermark works about. The nine
|
/// The 3 by 3 picker of a box's nine points: the point a transform, a resize or a watermark works about. The nine
|
||||||
/// points are one Tab stop; the arrows move the focus and stop at the grid's edges, and Space, Enter or a click picks
|
/// points are one Tab stop; the arrows move the focus and stop at the grid's edges, and Space, Enter or a click picks
|
||||||
/// the focused point. The arrows are directions in the box, so they do not mirror in a right-to-left interface and
|
/// the focused point. The arrows are directions in the box, so they do not mirror in a right-to-left interface and
|
||||||
/// neither does the grid. Every point shows a muted dot, the pick's twice as wide in the accent colour, and the cell
|
/// neither does the grid. A point shows a muted dot, the pick's twice as wide in the accent colour, and the cell under
|
||||||
/// under the pointer takes the hover fill. A mixed value picks no point. With [`growth`](ReferencePoint::growth) the
|
/// the pointer takes the hover fill; a mixed value picks no point. The
|
||||||
/// cells next to the pick show arrows toward where the box grows. The [`compact`](ReferencePoint::compact) picker is
|
/// [`presentation`](ReferencePoint::presentation) picks that mark instead: the square of
|
||||||
/// the size of an options bar's icon, its pick's dot as wide as the others, with no arrows.
|
/// [`PointPresentation::ConnectedSquares`], the nine joined into the box's grid, with the pick's square filled and the
|
||||||
|
/// others outlined. With [`growth`](ReferencePoint::growth) the cells next to the pick show arrows toward where the
|
||||||
|
/// box grows, over whatever the presentation marks them with. The [`compact`](ReferencePoint::compact) picker is the
|
||||||
|
/// size of an options bar's icon, its pick's dot as wide as the others, with no arrows.
|
||||||
#[derive(crate::contract::Component, crate::contract::Props)]
|
#[derive(crate::contract::Component, crate::contract::Props)]
|
||||||
#[component(exempt(case = "rtl", reason = "The nine points are positions in a box, not reading order: the left point stays on the left."))]
|
#[component(exempt(case = "rtl", reason = "The nine points are positions in a box, not reading order: the left point stays on the left."))]
|
||||||
pub struct ReferencePoint<'a> {
|
pub struct ReferencePoint<'a> {
|
||||||
@@ -83,12 +105,16 @@ pub struct ReferencePoint<'a> {
|
|||||||
#[style_key]
|
#[style_key]
|
||||||
#[prop(flag)]
|
#[prop(flag)]
|
||||||
compact: bool,
|
compact: bool,
|
||||||
|
/// How the nine points are marked: a dot each, or squares joined into the box's grid.
|
||||||
|
#[style_key]
|
||||||
|
#[prop]
|
||||||
|
presentation: PointPresentation,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ReferencePoint<'a> {
|
impl<'a> ReferencePoint<'a> {
|
||||||
/// A picker named `label` over `value`.
|
/// A picker named `label` over `value`.
|
||||||
pub fn new(label: impl Into<Label>, value: Bind<'a, Anchor9>) -> Self {
|
pub fn new(label: impl Into<Label>, value: Bind<'a, Anchor9>) -> Self {
|
||||||
ReferencePoint { common: Common::new(), value, growth: None, compact: false }.label(label)
|
ReferencePoint { common: Common::new(), value, growth: None, compact: false, presentation: PointPresentation::Dots }.label(label)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The direction of the arrow `cell` shows when the pick is `anchor`, in cell steps (each -1, 0 or 1): away from
|
/// The direction of the arrow `cell` shows when the pick is `anchor`, in cell steps (each -1, 0 or 1): away from
|
||||||
@@ -120,6 +146,56 @@ impl<'a> ReferencePoint<'a> {
|
|||||||
Rect::from_min_size(rect.min + vec2(column as f32 * cell.x, row as f32 * cell.y), cell)
|
Rect::from_min_size(rect.min + vec2(column as f32 * cell.x, row as f32 * cell.y), cell)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The line joining the square of the cell `at` to the square `step` cells beside it (across or down) in the
|
||||||
|
/// square's own half side `half`: from the one's edge to the other's. `None` where the grid has no such square, or
|
||||||
|
/// where the two squares meet.
|
||||||
|
fn link(rect: Rect, at: usize, step: (usize, usize), half: f32) -> Option<(Pos2, Pos2)> {
|
||||||
|
let (column, row) = Anchor9::ALL[at].cell();
|
||||||
|
let (next_column, next_row) = (column + step.0, row + step.1);
|
||||||
|
if next_column >= GRID || next_row >= GRID {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let along = vec2(step.0 as f32, step.1 as f32);
|
||||||
|
let (from, to) = (Self::cell_rect(rect, at).center(), Self::cell_rect(rect, next_row * GRID + next_column).center());
|
||||||
|
let (start, end) = (from + along * half, to - along * half);
|
||||||
|
((end - start).dot(along) > 0.0).then_some((start, end))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The box's grid, under the marks: every square joined to the squares beside it, from the one's edge to the
|
||||||
|
/// other's.
|
||||||
|
fn paint_links(draw: &Draw<'_>, cx: &Cx<'_, Self>) {
|
||||||
|
let half = cx.style.square / 2.0;
|
||||||
|
let line = cx.resolve(&cx.style.arrow_ink);
|
||||||
|
for at in 0..Anchor9::ALL.len() {
|
||||||
|
for step in [(1, 0), (0, 1)] {
|
||||||
|
if let Some((from, to)) = Self::link(cx.rect, at, step, half) {
|
||||||
|
draw.stroke(&PaintShape::Path(vec![from, to]), StrokeWidth::Thin, line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The mark of the point at `cell`: a dot, or the square the presentation gives it, the pick's filled.
|
||||||
|
fn paint_mark(draw: &Draw<'_>, cx: &Cx<'_, Self>, cell: Rect, presentation: PointPresentation, picked: bool) {
|
||||||
|
match presentation {
|
||||||
|
PointPresentation::Dots => {
|
||||||
|
let (side, ink) = if picked { (cx.style.picked_dot, &cx.style.ink) } else { (cx.style.dot, &cx.style.arrow_ink) };
|
||||||
|
let dot = Rect::from_center_size(cell.center(), Vec2::splat(side));
|
||||||
|
draw.fill(&PaintShape::Rect(dot, cx.style.dot_radius), cx.resolve(ink));
|
||||||
|
}
|
||||||
|
PointPresentation::ConnectedSquares => {
|
||||||
|
// The pick is the filled square; the others are outlines, as the observed proxy draws them.
|
||||||
|
let square = Rect::from_center_size(cell.center(), Vec2::splat(cx.style.square));
|
||||||
|
let ink = if picked { &cx.style.ink } else { &cx.style.arrow_ink };
|
||||||
|
if picked {
|
||||||
|
draw.fill(&PaintShape::Rect(square, egui::CornerRadius::ZERO), cx.resolve(ink));
|
||||||
|
} else {
|
||||||
|
draw.stroke(&PaintShape::Rect(square, egui::CornerRadius::ZERO), StrokeWidth::Thin, cx.resolve(ink));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn name_of(point: Anchor9, words: &PickerWords) -> &Label {
|
fn name_of(point: Anchor9, words: &PickerWords) -> &Label {
|
||||||
match point {
|
match point {
|
||||||
Anchor9::TopLeft => &words.point_top_left,
|
Anchor9::TopLeft => &words.point_top_left,
|
||||||
@@ -149,6 +225,7 @@ impl Component for ReferencePoint<'_> {
|
|||||||
let edge = Stateful::new(cx.color(colors.line.control_edge));
|
let edge = Stateful::new(cx.color(colors.line.control_edge));
|
||||||
let disabled = cx.color(colors.text.disabled);
|
let disabled = cx.color(colors.text.disabled);
|
||||||
let look = |rest: StyleColor| Stateful::new(rest).off(States::new(rest, rest, rest, disabled));
|
let look = |rest: StyleColor| Stateful::new(rest).off(States::new(rest, rest, rest, disabled));
|
||||||
|
let cell = if key.compact { cx.tokens.size.icon.md / GRID as f32 } else { cx.tokens.size.control.md };
|
||||||
ReferencePointStyle {
|
ReferencePointStyle {
|
||||||
metrics: Metrics::new(),
|
metrics: Metrics::new(),
|
||||||
surface: SurfaceLook::new(Stateful::new(StyleColor(sunken.fill)))
|
surface: SurfaceLook::new(Stateful::new(StyleColor(sunken.fill)))
|
||||||
@@ -156,9 +233,10 @@ impl Component for ReferencePoint<'_> {
|
|||||||
.radius(sunken.radius)
|
.radius(sunken.radius)
|
||||||
.border(edge, cx.tokens.stroke.thin),
|
.border(edge, cx.tokens.stroke.thin),
|
||||||
focus: cx.focus_look(),
|
focus: cx.focus_look(),
|
||||||
cell: if key.compact { cx.tokens.size.icon.md / GRID as f32 } else { cx.tokens.size.control.md },
|
cell,
|
||||||
dot: DOT,
|
dot: DOT,
|
||||||
picked_dot: if key.compact { DOT } else { DOT * 2.0 },
|
picked_dot: if key.compact { DOT } else { DOT * 2.0 },
|
||||||
|
square: cell * SQUARE,
|
||||||
dot_radius: UiTokens::corner(cx.tokens.shape.radius.sm),
|
dot_radius: UiTokens::corner(cx.tokens.shape.radius.sm),
|
||||||
ink: look(cx.color(colors.accent.text)),
|
ink: look(cx.color(colors.accent.text)),
|
||||||
arrow: cx.tokens.size.icon.sm,
|
arrow: cx.tokens.size.icon.sm,
|
||||||
@@ -213,10 +291,12 @@ impl Component for ReferencePoint<'_> {
|
|||||||
let current = Self::position_of(cx.value());
|
let current = Self::position_of(cx.value());
|
||||||
debug_assert!(!(self.compact && self.growth.is_some()), "the compact picker shows no growth arrows");
|
debug_assert!(!(self.compact && self.growth.is_some()), "the compact picker shows no growth arrows");
|
||||||
let draw = ui.draw();
|
let draw = ui.draw();
|
||||||
|
if self.presentation == PointPresentation::ConnectedSquares {
|
||||||
|
Self::paint_links(&draw, cx);
|
||||||
|
}
|
||||||
for (at, point) in Anchor9::ALL.iter().enumerate() {
|
for (at, point) in Anchor9::ALL.iter().enumerate() {
|
||||||
let cell = Self::cell_rect(cx.rect, at);
|
let cell = Self::cell_rect(cx.rect, at);
|
||||||
let (side, ink) = if current == Some(at) { (cx.style.picked_dot, &cx.style.ink) } else { (cx.style.dot, &cx.style.arrow_ink) };
|
Self::paint_mark(&draw, cx, cell, self.presentation, current == Some(at));
|
||||||
draw.fill(&PaintShape::Rect(Rect::from_center_size(cell.center(), Vec2::splat(side)), cx.style.dot_radius), cx.resolve(ink));
|
|
||||||
if let Some(growth) = self.growth
|
if let Some(growth) = self.growth
|
||||||
&& let Some(anchor) = current.map(|pick| Anchor9::ALL[pick])
|
&& let Some(anchor) = current.map(|pick| Anchor9::ALL[pick])
|
||||||
&& let Some((dx, dy)) = Self::arrow(anchor, *point, growth)
|
&& let Some((dx, dy)) = Self::arrow(anchor, *point, growth)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
//! Stories of `ReferencePoint`.
|
//! Stories of `ReferencePoint`.
|
||||||
|
|
||||||
use super::ReferencePoint;
|
use super::{PointPresentation, ReferencePoint};
|
||||||
use crate::contract::{Bind, Show as _, Validity};
|
use crate::contract::{Bind, Show as _, Validity};
|
||||||
use crate::runtime::env::Message;
|
use crate::runtime::env::Message;
|
||||||
use crate::testing::{Drive, Fixture, StoryData, StoryOutput, story};
|
use crate::testing::{Drive, Fixture, StoryData, StoryOutput, story};
|
||||||
@@ -43,3 +43,25 @@ fn reference_point_growth(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
|||||||
fn reference_point_compact(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
fn reference_point_compact(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||||
ReferencePoint::new("Reference point", Bind::bound(data.value("compact", Anchor9::Centre))).compact().show(ui).into()
|
ReferencePoint::new("Reference point", Bind::bound(data.value("compact", Anchor9::Centre))).compact().show(ui).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The nine points as squares joined into the box's grid: the pick's square filled, the others outlined.
|
||||||
|
#[story(ReferencePoint<'static>, covers = [], drive = Drive::Custom(pick_top_left))]
|
||||||
|
fn reference_point_connected_squares(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||||
|
let value = Bind::bound(data.value("squares", Anchor9::Centre));
|
||||||
|
ReferencePoint::new("Reference point", value).presentation(PointPresentation::ConnectedSquares).show(ui).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The squares keep the growth arrows: the box grows both ways, so every square beside the pick frames an arrow
|
||||||
|
/// pointing away from it.
|
||||||
|
#[story(ReferencePoint<'static>, covers = [], drive = Drive::Never)]
|
||||||
|
fn reference_point_connected_squares_growth(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||||
|
let value = Bind::bound(data.value("squares_growing", Anchor9::Centre));
|
||||||
|
ReferencePoint::new("Reference point", value).presentation(PointPresentation::ConnectedSquares).growth((1.0, 1.0)).show(ui).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The squares at the options bar's size: the presentation is the mark a point shows, whatever the picker sizes at.
|
||||||
|
#[story(ReferencePoint<'static>, covers = [], drive = Drive::Never)]
|
||||||
|
fn reference_point_connected_squares_compact(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||||
|
let value = Bind::bound(data.value("squares_compact", Anchor9::BottomLeft));
|
||||||
|
ReferencePoint::new("Reference point", value).presentation(PointPresentation::ConnectedSquares).compact().show(ui).into()
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
mod combo_list;
|
mod combo_list;
|
||||||
mod date;
|
mod date;
|
||||||
|
mod reference_point;
|
||||||
mod select_list;
|
mod select_list;
|
||||||
mod select_steps;
|
mod select_steps;
|
||||||
mod select_width;
|
mod select_width;
|
||||||
|
|||||||
@@ -0,0 +1,224 @@
|
|||||||
|
//! `ReferencePoint` marks its nine points with the dot the picker has always shown; asked for a presentation, it marks
|
||||||
|
//! them with the squares of the box's grid, joined to their neighbours, and keeps every part, cell, name and pick.
|
||||||
|
|
||||||
|
use craft_behavior::Anchor9;
|
||||||
|
use craft_ui::contract::{Bind, Show as _};
|
||||||
|
use craft_ui::pickers::{PointPresentation, ReferencePoint};
|
||||||
|
use craft_ui::testing::Fixture;
|
||||||
|
use craft_ui::testing::focus::AccessActionKind;
|
||||||
|
use craft_ui::testing::trial;
|
||||||
|
use egui::epaint::RectShape;
|
||||||
|
use egui::{Key, Modifiers, Pos2, Rect, Shape, Ui};
|
||||||
|
use std::cell::Cell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
/// Three points on each side of the picker's grid.
|
||||||
|
const GRID: usize = 3;
|
||||||
|
|
||||||
|
/// The centre point's place among the nine, which the fixtures pick.
|
||||||
|
const CENTRE: usize = 4;
|
||||||
|
|
||||||
|
/// The slack a painted shape is compared with: the rounding of the layout.
|
||||||
|
const ROUNDING: f32 = 0.5;
|
||||||
|
|
||||||
|
/// The nine points' names, in the order the picker declares them: row by row from the top left.
|
||||||
|
const NAMES: [&str; GRID * GRID] = ["Top left", "Top", "Top right", "Left", "Center", "Right", "Bottom left", "Bottom", "Bottom right"];
|
||||||
|
|
||||||
|
/// The picker a trial shows, holding the point it edits and the mark it shows it with.
|
||||||
|
struct Picker {
|
||||||
|
point: Cell<Anchor9>,
|
||||||
|
presentation: PointPresentation,
|
||||||
|
compact: bool,
|
||||||
|
growth: Option<(f64, f64)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Picker {
|
||||||
|
/// The picker as the library shows it, the centre picked.
|
||||||
|
fn dots() -> Self {
|
||||||
|
Picker { point: Cell::new(Anchor9::Centre), presentation: PointPresentation::Dots, compact: false, growth: None }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn squares() -> Self {
|
||||||
|
Picker { presentation: PointPresentation::ConnectedSquares, ..Picker::dots() }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The picker the canvas grows both ways about, so every point beside the centre has an arrow.
|
||||||
|
fn grown(self) -> Self {
|
||||||
|
Picker { growth: Some((1.0, 1.0)), ..self }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compact(self) -> Self {
|
||||||
|
Picker { compact: true, ..self }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn show(&self, ui: &mut Ui) {
|
||||||
|
let mut point = self.point.get();
|
||||||
|
let mut picker = ReferencePoint::new("Reference point", Bind::bound(&mut point)).presentation(self.presentation);
|
||||||
|
if self.compact {
|
||||||
|
picker = picker.compact();
|
||||||
|
}
|
||||||
|
if let Some(growth) = self.growth {
|
||||||
|
picker = picker.growth(growth);
|
||||||
|
}
|
||||||
|
picker.show(ui);
|
||||||
|
self.point.set(point);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A settled fixture showing `picker`, which the caller keeps to read the point back.
|
||||||
|
fn settled(picker: &Rc<Picker>) -> Fixture {
|
||||||
|
let shown = picker.clone();
|
||||||
|
let mut fixture = Fixture::new(move |ui| shown.show(ui));
|
||||||
|
fixture.settle();
|
||||||
|
fixture
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The nine points' cells, as the picker reports them to a screen reader: also the names and the count it keeps.
|
||||||
|
fn cells(fixture: &Fixture) -> Vec<Rect> {
|
||||||
|
NAMES.iter().map(|name| fixture.by_label(name).unwrap_or_else(|| panic!("no `{name}` point")).rect).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The mark a point shows: the shape drawn inside its cell that is smaller than the cell and as tall as it is wide.
|
||||||
|
fn mark<'a>(shapes: &'a [Shape], cell: Rect) -> Option<&'a RectShape> {
|
||||||
|
shapes.iter().find_map(|shape| match shape {
|
||||||
|
Shape::Rect(rect)
|
||||||
|
if cell.contains_rect(rect.rect) && rect.rect.width() < cell.width() && (rect.rect.width() - rect.rect.height()).abs() <= ROUNDING =>
|
||||||
|
{
|
||||||
|
Some(rect)
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The mark of each of the nine points, in the order of [`NAMES`].
|
||||||
|
fn marks<'a>(shapes: &'a [Shape], cells: &[Rect]) -> Vec<&'a RectShape> {
|
||||||
|
cells.iter().map(|cell| mark(shapes, *cell).unwrap_or_else(|| panic!("no mark in the cell at {:?}", cell.center()))).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The lines a frame painted, as the two points each joins.
|
||||||
|
fn lines(shapes: &[Shape]) -> Vec<[Pos2; 2]> {
|
||||||
|
shapes
|
||||||
|
.iter()
|
||||||
|
.filter_map(|shape| match shape {
|
||||||
|
Shape::LineSegment { points, .. } => Some(*points),
|
||||||
|
Shape::Path(path) if path.points.len() == 2 => Some([path.points[0], path.points[1]]),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The lines the squares presentation draws: the point of each cell joined to the point on its right and to the one
|
||||||
|
/// below it, from one square's edge to the other's.
|
||||||
|
fn links(marks: &[&RectShape]) -> Vec<[Pos2; 2]> {
|
||||||
|
let mut links = Vec::new();
|
||||||
|
for (at, mark) in marks.iter().enumerate() {
|
||||||
|
let (column, row) = (at % GRID, at / GRID);
|
||||||
|
if column + 1 < GRID {
|
||||||
|
links.push([mark.rect.right_center(), marks[at + 1].rect.left_center()]);
|
||||||
|
}
|
||||||
|
if row + 1 < GRID {
|
||||||
|
links.push([mark.rect.center_bottom(), marks[at + GRID].rect.center_top()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
links
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The lines the grid joins its squares with: two per row and two per column.
|
||||||
|
const GRID_LINES: usize = 2 * GRID * (GRID - 1);
|
||||||
|
|
||||||
|
fn same(first: Pos2, second: Pos2) -> bool {
|
||||||
|
(first - second).length() <= ROUNDING
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether `painted` holds a line joining the two points `wanted` joins, either way round.
|
||||||
|
fn joins(painted: &[[Pos2; 2]], wanted: [Pos2; 2]) -> bool {
|
||||||
|
painted.iter().any(|line| (same(line[0], wanted[0]) && same(line[1], wanted[1])) || (same(line[0], wanted[1]) && same(line[1], wanted[0])))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[trial]
|
||||||
|
fn the_default_presentation_marks_the_nine_points_with_dots_and_joins_none_of_them() {
|
||||||
|
let picker = Rc::new(Picker::dots());
|
||||||
|
let mut fixture = settled(&picker);
|
||||||
|
let cells = cells(&fixture);
|
||||||
|
assert_eq!(cells.len(), GRID * GRID, "a cell for each point of the box");
|
||||||
|
let shapes = fixture.frame_shapes();
|
||||||
|
let marks = marks(&shapes, &cells);
|
||||||
|
let painted = lines(&shapes);
|
||||||
|
assert!(painted.is_empty(), "the default presentation joins nothing: {painted:?}");
|
||||||
|
for (at, mark) in marks.iter().enumerate() {
|
||||||
|
assert!(cells[at].contains_rect(mark.rect), "the dot of the point at {at} stays in its cell");
|
||||||
|
assert!(same(mark.rect.center(), cells[at].center()), "the dot of the point at {at} sits at the centre of its cell");
|
||||||
|
assert!(mark.fill.a() > 0, "the dot of the point at {at} is filled");
|
||||||
|
assert!(mark.stroke.is_empty(), "the dot of the point at {at} is not outlined");
|
||||||
|
}
|
||||||
|
assert!(marks[CENTRE].rect.width() > marks[0].rect.width(), "the pick's dot is the wider one");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[trial]
|
||||||
|
fn the_squares_presentation_outlines_the_points_and_joins_them_into_the_boxs_grid() {
|
||||||
|
let picker = Rc::new(Picker::squares());
|
||||||
|
let mut fixture = settled(&picker);
|
||||||
|
let cells = cells(&fixture);
|
||||||
|
let shapes = fixture.frame_shapes();
|
||||||
|
let marks = marks(&shapes, &cells);
|
||||||
|
for (at, mark) in marks.iter().enumerate() {
|
||||||
|
assert!(cells[at].contains_rect(mark.rect), "the square of the point at {at} stays in its cell");
|
||||||
|
assert!(same(mark.rect.center(), cells[at].center()), "the square of the point at {at} sits at the centre of its cell");
|
||||||
|
assert_eq!(mark.corner_radius, egui::CornerRadius::ZERO, "the square of the point at {at} has square corners");
|
||||||
|
let picked = at == CENTRE;
|
||||||
|
assert_eq!(mark.fill.a() > 0, picked, "the pick's square is filled and the rest are not, at {at}");
|
||||||
|
assert_eq!(!mark.stroke.is_empty(), !picked, "the rest are outlined and the pick is not, at {at}");
|
||||||
|
}
|
||||||
|
let painted = lines(&shapes);
|
||||||
|
let wanted = links(&marks);
|
||||||
|
assert_eq!(painted.len(), GRID_LINES, "the squares are joined to the neighbours they have: {painted:?}");
|
||||||
|
assert_eq!(wanted.len(), GRID_LINES, "and the joins wanted are the same number");
|
||||||
|
for line in &wanted {
|
||||||
|
assert!(joins(&painted, *line), "a line joins {line:?}, of {painted:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[trial]
|
||||||
|
fn the_presentation_changes_the_mark_and_leaves_every_cell_and_the_size_alone() {
|
||||||
|
let dots = Rc::new(Picker::dots());
|
||||||
|
let squares = Rc::new(Picker::squares());
|
||||||
|
let (dot_fixture, square_fixture) = (settled(&dots), settled(&squares));
|
||||||
|
assert_eq!(cells(&dot_fixture), cells(&square_fixture), "the nine parts and the cells they sense keep their places");
|
||||||
|
assert_eq!(dot_fixture.shown(), square_fixture.shown(), "and the picker takes the size it took before");
|
||||||
|
let compact_dots = Rc::new(Picker::dots().compact());
|
||||||
|
let compact_squares = Rc::new(Picker::squares().compact());
|
||||||
|
let (small_dots, mut small_squares) = (settled(&compact_dots), settled(&compact_squares));
|
||||||
|
assert_eq!(small_dots.shown(), small_squares.shown(), "the compact picker keeps its icon size too");
|
||||||
|
assert!(small_squares.shown().width() < square_fixture.shown().width(), "and is the smaller picker");
|
||||||
|
assert_eq!(cells(&small_squares).len(), GRID * GRID, "the compact picker keeps its nine points");
|
||||||
|
let shapes = small_squares.frame_shapes();
|
||||||
|
assert_eq!(lines(&shapes).len(), GRID_LINES, "the compact squares are joined as the large ones are");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[trial]
|
||||||
|
fn the_growth_arrows_are_shown_beside_the_pick_in_either_presentation() {
|
||||||
|
let arrows = |picker: Picker| {
|
||||||
|
let picker = Rc::new(picker.grown());
|
||||||
|
let mut fixture = settled(&picker);
|
||||||
|
lines(&fixture.frame_shapes()).len()
|
||||||
|
};
|
||||||
|
let neighbours = GRID * GRID - 1;
|
||||||
|
assert_eq!(arrows(Picker::dots()), neighbours, "the dots presentation shows one arrow per point beside the centre pick");
|
||||||
|
assert_eq!(arrows(Picker::squares()), neighbours + GRID_LINES, "the squares presentation keeps those arrows beside its joins");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[trial]
|
||||||
|
fn a_press_on_a_square_picks_its_point_and_space_picks_the_focused_one() {
|
||||||
|
let picker = Rc::new(Picker::squares());
|
||||||
|
let mut fixture = settled(&picker);
|
||||||
|
let corner = fixture.by_label("Bottom right").expect("the bottom right square");
|
||||||
|
fixture.click(&corner);
|
||||||
|
fixture.settle();
|
||||||
|
assert_eq!(picker.point.get(), Anchor9::BottomRight, "a press on a square picks its point");
|
||||||
|
let corner = fixture.by_label("Top left").expect("the top left square");
|
||||||
|
fixture.access(AccessActionKind::Focus, &corner);
|
||||||
|
fixture.settle();
|
||||||
|
fixture.key(Key::Space, Modifiers::NONE);
|
||||||
|
fixture.settle();
|
||||||
|
assert_eq!(picker.point.get(), Anchor9::TopLeft, "Space picks the square the focus is on");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user