mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 05:14:24 +00:00
Step 8 borrows the declared parts instead of copying them: Decls keeps the sensing core's PartDecls beside their extras. sense_root records its shapes from an iterator and moves its widget list into the arena. The arena swaps and clears its per-pass lists. Member decls and shapes, read samples and the value and reset part lists sit inline. A real read takes no copy of the open measuring runs. Adds a trial: a control whose size follows its value reruns the frame after its own edit.
156 lines
4.6 KiB
Rust
156 lines
4.6 KiB
Rust
//! A value component for the trials: a track that scrubs an `f32`, written with the derives.
|
|
|
|
use craft_behavior::{CancelReason, EditEvent, EditEvents, GesturePhase, PartKey, PressPhase, Sense};
|
|
use craft_ui::contract::{Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, MeasureCx, PartsCx, Pointer as _, Role, Styled as _};
|
|
use craft_ui::style::{ComponentStyle, Flags, StyleCx};
|
|
use egui::{Pos2, Rect, Vec2};
|
|
use std::cell::Cell;
|
|
|
|
const TRACK: PartKey = PartKey::new("track");
|
|
|
|
/// A style with no look: the trials read events and values, not pixels.
|
|
#[derive(Clone, Debug, Default, PartialEq, ComponentStyle)]
|
|
#[non_exhaustive]
|
|
pub struct PlainStyle {
|
|
#[style(metrics)]
|
|
metrics: craft_behavior::Metrics,
|
|
}
|
|
|
|
/// Where the component sits and what a track position means: the value is the pointer's offset from the left edge.
|
|
pub const AT: Rect = Rect { min: Pos2::new(10.0, 10.0), max: Pos2::new(110.0, 30.0) };
|
|
|
|
/// Values the trials start from and drag to, named so the assertions compare relations. A drag along the track ends at
|
|
/// `DRAGGED`; the held drag of the mixed-composite trial stops at `HELD`.
|
|
pub const INITIAL: f32 = 10.0;
|
|
|
|
pub const APP_VALUE: f32 = 3.0;
|
|
|
|
pub const CAP: f32 = 30.0;
|
|
|
|
pub const DRAGGED: f32 = 50.0;
|
|
|
|
pub const HELD: f32 = 45.0;
|
|
|
|
const TOLERANCE: f32 = 1.5;
|
|
|
|
/// A value within pixel rounding of `expected`.
|
|
pub fn near(value: f32, expected: f32) -> bool {
|
|
(value - expected).abs() < TOLERANCE
|
|
}
|
|
|
|
/// What the component recorded while it ran.
|
|
#[derive(Default)]
|
|
#[non_exhaustive]
|
|
pub struct Seen {
|
|
pub flags: Cell<Flags>,
|
|
pub shown: Cell<Option<f32>>,
|
|
pub phase: Cell<Option<GesturePhase>>,
|
|
}
|
|
|
|
/// Scrubs a value by dragging along its track; a click without a drag does nothing.
|
|
#[derive(craft_ui::contract::Component)]
|
|
pub struct Scrub<'a> {
|
|
#[common]
|
|
common: Common,
|
|
value: Bind<'a, f32>,
|
|
seen: Option<&'a Seen>,
|
|
/// The measured width follows the value.
|
|
grows: bool,
|
|
}
|
|
|
|
impl<'a> Scrub<'a> {
|
|
pub fn new(value: Bind<'a, f32>) -> Self {
|
|
Scrub { common: Common::new(), value, seen: None, grows: false }
|
|
}
|
|
|
|
pub fn seen(mut self, seen: &'a Seen) -> Self {
|
|
self.seen = Some(seen);
|
|
self
|
|
}
|
|
|
|
pub fn grows(mut self) -> Self {
|
|
self.grows = true;
|
|
self
|
|
}
|
|
}
|
|
|
|
impl Component for Scrub<'_> {
|
|
const ROLE: Role = Role::SLIDER;
|
|
type Style = PlainStyle;
|
|
type Output = EditResponse<f32>;
|
|
|
|
fn style(_: &StyleCx<'_>, _: ScrubStyleKey) -> PlainStyle {
|
|
PlainStyle::default()
|
|
}
|
|
|
|
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
|
|
let grown = if self.grows { cx.value().copied().unwrap_or_default() } else { 0.0 };
|
|
AT.size() + Vec2::new(grown, 0.0)
|
|
}
|
|
|
|
fn sense(&self) -> Sense {
|
|
Sense::CLICK | Sense::FOCUS
|
|
}
|
|
|
|
fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) {
|
|
cx.part(TRACK, rect).sense(Sense::CLICK | Sense::DRAG).pointer_only("the track is dragged with the pointer");
|
|
}
|
|
|
|
fn bind(&mut self) -> Option<BindMut<'_, f32>> {
|
|
Some(self.value.reborrow())
|
|
}
|
|
|
|
fn update(&mut self, cx: &mut Cx<'_, Self>) {
|
|
let left = cx.rect.left();
|
|
let phase = cx.part(TRACK).press.phase;
|
|
match phase {
|
|
PressPhase::DragStart { pos, .. } => {
|
|
cx.begin();
|
|
cx.propose(pos.x - left);
|
|
}
|
|
PressPhase::Drag { pos, .. } => cx.propose(pos.x - left),
|
|
PressPhase::Release { .. } if cx.gesture().is_some() => cx.commit(),
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
fn paint(&mut self, _: &mut craft_ui::contract::PaintUi<'_>, cx: &Cx<'_, Self>) {
|
|
if let Some(seen) = self.seen {
|
|
seen.flags.set(cx.state().flags);
|
|
seen.shown.set(cx.value().copied());
|
|
seen.phase.set(cx.gesture());
|
|
}
|
|
}
|
|
|
|
fn finish(self, cx: Cx<'_, Self>) -> EditResponse<f32> {
|
|
cx.edit_response()
|
|
}
|
|
}
|
|
|
|
/// What a response's events were, for the gesture trials.
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
#[non_exhaustive]
|
|
pub enum Ev {
|
|
Begin,
|
|
Update(f32),
|
|
Commit(f32),
|
|
Cancel(CancelReason),
|
|
Set(f32),
|
|
}
|
|
|
|
pub fn ev_of(events: &EditEvents<f32>) -> Vec<Ev> {
|
|
events
|
|
.iter()
|
|
.filter_map(|event| {
|
|
Some(match event {
|
|
EditEvent::Begin { .. } => Ev::Begin,
|
|
EditEvent::Update { value, .. } => Ev::Update(*value),
|
|
EditEvent::Commit { after, .. } => Ev::Commit(*after),
|
|
EditEvent::Cancel { reason, .. } => Ev::Cancel(*reason),
|
|
EditEvent::Set { after, .. } => Ev::Set(*after),
|
|
_ => return None,
|
|
})
|
|
})
|
|
.collect()
|
|
}
|