mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 05:14:24 +00:00
Digit keys set a canvas tool's opacity: DigitKeys pair rule in craft-behavior, the OpacityDigit command set (1…0, Shift+1…Shift+0) in ui-canvas
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
//! [`DigitKeys`]: Photoshop's number keys for a tool's opacity. 1 sets 10 % … 9 sets 90 %, 0 sets 100 %; two digits
|
||||
//! typed within [`DigitKeys::TWO_DIGITS`] seconds set the exact value (4 then 5 is 45 %, 0 then 0 is 0 %). The second
|
||||
//! value's keys (Shift with a digit) set the tool's second value the same way: Flow for a brush, Fill for a layer.
|
||||
|
||||
/// What a number key sets: the percentage and which value.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
#[non_exhaustive]
|
||||
pub struct DigitEntry {
|
||||
/// 0..=100.
|
||||
pub percent: f64,
|
||||
/// The tool's second value (Flow, Fill) rather than its opacity.
|
||||
pub second: bool,
|
||||
}
|
||||
|
||||
/// The number keys' state between presses: the first digit of a pair. The app keeps one.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||
pub struct DigitKeys {
|
||||
first: Option<(u8, bool, f64)>,
|
||||
}
|
||||
|
||||
impl DigitKeys {
|
||||
/// Seconds within which a second digit makes an exact value.
|
||||
pub const TWO_DIGITS: f64 = 0.8;
|
||||
|
||||
/// One press of `digit` (0..=9) for the second value or not, at `now` seconds: 10 × the digit (0: 100) on its own,
|
||||
/// or the two-digit value when it follows a first digit for the same value in time. `None` for a digit over 9.
|
||||
pub fn press(&mut self, digit: u8, second: bool, now: f64) -> Option<DigitEntry> {
|
||||
if digit > 9 {
|
||||
return None;
|
||||
}
|
||||
let percent = match self.first.take() {
|
||||
Some((first, was_second, at)) if was_second == second && (0.0..Self::TWO_DIGITS).contains(&(now - at)) => f64::from(first * 10 + digit),
|
||||
_ => {
|
||||
self.first = Some((digit, second, now));
|
||||
if digit == 0 { 100.0 } else { f64::from(digit) * 10.0 }
|
||||
}
|
||||
};
|
||||
Some(DigitEntry { percent, second })
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ pub mod codec;
|
||||
pub mod columns;
|
||||
pub mod command;
|
||||
pub mod cursor;
|
||||
pub mod digit_keys;
|
||||
pub mod divider;
|
||||
pub mod doc_key;
|
||||
pub mod dock;
|
||||
@@ -70,6 +71,7 @@ pub use codec::{Caret, Codec, NotAValue, NumberCodec, PlainText, decimals_for_st
|
||||
pub use columns::{ColumnKey, ColumnWidth, GridSection, GridView};
|
||||
pub use command::{CommandFlags, pick_instance, resolve_flags};
|
||||
pub use cursor::{Cursor, CursorBadge, CursorIcon, CursorPriority, IconId, PaintedCursor};
|
||||
pub use digit_keys::{DigitEntry, DigitKeys};
|
||||
pub use doc_key::DocKey;
|
||||
pub use dock::DockSide;
|
||||
pub use edit_history::EditHistory;
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
//! The number keys' pair rule.
|
||||
|
||||
use craft_behavior::DigitKeys;
|
||||
|
||||
#[test]
|
||||
fn one_digit_sets_tens_and_two_quick_digits_set_the_exact_value() {
|
||||
let mut keys = DigitKeys::default();
|
||||
let mut press = |digit, second, now| keys.press(digit, second, now).map(|entry| (entry.percent, entry.second));
|
||||
assert_eq!(press(4, false, 0.0), Some((40.0, false)));
|
||||
assert_eq!(press(5, false, 0.5), Some((45.0, false)), "a second digit in time");
|
||||
assert_eq!(press(5, false, 0.6), Some((50.0, false)), "a third digit starts again");
|
||||
assert_eq!(press(3, false, 2.0), Some((30.0, false)), "too late for a pair");
|
||||
assert_eq!(press(0, true, 2.1), Some((100.0, true)), "a digit for the other value starts again; 0 is 100 %");
|
||||
assert_eq!(press(0, true, 2.2), Some((0.0, true)), "0 then 0 is 0 %");
|
||||
assert_eq!(press(10, false, 3.0), None);
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
mod canvas_view;
|
||||
mod cells;
|
||||
mod digit_keys;
|
||||
mod dock;
|
||||
mod dock_apply;
|
||||
mod fit;
|
||||
|
||||
@@ -52,7 +52,9 @@ mod raster;
|
||||
pub use raster::{DetailPlan, RasterDisplay, RasterDisplayOutput, RasterStyle, snap_to_texels};
|
||||
|
||||
// l4d-space
|
||||
mod opacity_keys;
|
||||
mod snap;
|
||||
pub use opacity_keys::OpacityDigit;
|
||||
pub(crate) use rulers::LABEL_STEP_MIN;
|
||||
pub use rulers::{NUDGE, NUDGE_FAR};
|
||||
pub use snap::{Snap, canvas_snap, snap_suspended, snap_tolerance};
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
//! Photoshop's number keys for a canvas tool's opacity, as the [`OpacityDigit`] command set: 1 to 9 set 10 % to 90 %
|
||||
//! and 0 sets 100 %, two digits typed quickly set the exact value, and Shift with a digit sets the tool's second value
|
||||
//! (Flow for a brush, Fill for a layer). The app routes the fired commands to [`OpacityDigit::entry`] with its
|
||||
//! [`DigitKeys`] while the current tool has an opacity, and sets the value the entry names; which value that is and its
|
||||
//! undo step stay the app's.
|
||||
|
||||
use crate::runtime::commands::CommandSet as _;
|
||||
use crate::runtime::commands::{FirePhase, Fired};
|
||||
use craft_behavior::{DigitEntry, DigitKeys};
|
||||
|
||||
/// The number keys of a canvas tool's opacity and second value.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, craft_ui_macros::CommandSet)]
|
||||
#[command(prefix = "toolOpacity")]
|
||||
#[non_exhaustive]
|
||||
pub enum OpacityDigit {
|
||||
/// 1: the opacity at 10 %, or the second digit of an exact value.
|
||||
#[command(label = "Opacity 1", default = "1")]
|
||||
OpacityOne,
|
||||
/// 2: the opacity at 20 %, or the second digit of an exact value.
|
||||
#[command(label = "Opacity 2", default = "2")]
|
||||
OpacityTwo,
|
||||
/// 3: the opacity at 30 %, or the second digit of an exact value.
|
||||
#[command(label = "Opacity 3", default = "3")]
|
||||
OpacityThree,
|
||||
/// 4: the opacity at 40 %, or the second digit of an exact value.
|
||||
#[command(label = "Opacity 4", default = "4")]
|
||||
OpacityFour,
|
||||
/// 5: the opacity at 50 %, or the second digit of an exact value.
|
||||
#[command(label = "Opacity 5", default = "5")]
|
||||
OpacityFive,
|
||||
/// 6: the opacity at 60 %, or the second digit of an exact value.
|
||||
#[command(label = "Opacity 6", default = "6")]
|
||||
OpacitySix,
|
||||
/// 7: the opacity at 70 %, or the second digit of an exact value.
|
||||
#[command(label = "Opacity 7", default = "7")]
|
||||
OpacitySeven,
|
||||
/// 8: the opacity at 80 %, or the second digit of an exact value.
|
||||
#[command(label = "Opacity 8", default = "8")]
|
||||
OpacityEight,
|
||||
/// 9: the opacity at 90 %, or the second digit of an exact value.
|
||||
#[command(label = "Opacity 9", default = "9")]
|
||||
OpacityNine,
|
||||
/// 0: the opacity at 100 %, or the second digit of an exact value.
|
||||
#[command(label = "Opacity 0", default = "0")]
|
||||
OpacityZero,
|
||||
/// Shift+1: the second value at 10 %, or the second digit of an exact value.
|
||||
#[command(label = "Flow or Fill 1", default = "Shift+1")]
|
||||
SecondOne,
|
||||
/// Shift+2: the second value at 20 %, or the second digit of an exact value.
|
||||
#[command(label = "Flow or Fill 2", default = "Shift+2")]
|
||||
SecondTwo,
|
||||
/// Shift+3: the second value at 30 %, or the second digit of an exact value.
|
||||
#[command(label = "Flow or Fill 3", default = "Shift+3")]
|
||||
SecondThree,
|
||||
/// Shift+4: the second value at 40 %, or the second digit of an exact value.
|
||||
#[command(label = "Flow or Fill 4", default = "Shift+4")]
|
||||
SecondFour,
|
||||
/// Shift+5: the second value at 50 %, or the second digit of an exact value.
|
||||
#[command(label = "Flow or Fill 5", default = "Shift+5")]
|
||||
SecondFive,
|
||||
/// Shift+6: the second value at 60 %, or the second digit of an exact value.
|
||||
#[command(label = "Flow or Fill 6", default = "Shift+6")]
|
||||
SecondSix,
|
||||
/// Shift+7: the second value at 70 %, or the second digit of an exact value.
|
||||
#[command(label = "Flow or Fill 7", default = "Shift+7")]
|
||||
SecondSeven,
|
||||
/// Shift+8: the second value at 80 %, or the second digit of an exact value.
|
||||
#[command(label = "Flow or Fill 8", default = "Shift+8")]
|
||||
SecondEight,
|
||||
/// Shift+9: the second value at 90 %, or the second digit of an exact value.
|
||||
#[command(label = "Flow or Fill 9", default = "Shift+9")]
|
||||
SecondNine,
|
||||
/// Shift+0: the second value at 100 %, or the second digit of an exact value.
|
||||
#[command(label = "Flow or Fill 0", default = "Shift+0")]
|
||||
SecondZero,
|
||||
}
|
||||
|
||||
impl OpacityDigit {
|
||||
/// The digit the command types and whether it is for the second value.
|
||||
pub const fn digit(self) -> (u8, bool) {
|
||||
match self {
|
||||
OpacityDigit::OpacityOne => (1, false),
|
||||
OpacityDigit::OpacityTwo => (2, false),
|
||||
OpacityDigit::OpacityThree => (3, false),
|
||||
OpacityDigit::OpacityFour => (4, false),
|
||||
OpacityDigit::OpacityFive => (5, false),
|
||||
OpacityDigit::OpacitySix => (6, false),
|
||||
OpacityDigit::OpacitySeven => (7, false),
|
||||
OpacityDigit::OpacityEight => (8, false),
|
||||
OpacityDigit::OpacityNine => (9, false),
|
||||
OpacityDigit::OpacityZero => (0, false),
|
||||
OpacityDigit::SecondOne => (1, true),
|
||||
OpacityDigit::SecondTwo => (2, true),
|
||||
OpacityDigit::SecondThree => (3, true),
|
||||
OpacityDigit::SecondFour => (4, true),
|
||||
OpacityDigit::SecondFive => (5, true),
|
||||
OpacityDigit::SecondSix => (6, true),
|
||||
OpacityDigit::SecondSeven => (7, true),
|
||||
OpacityDigit::SecondEight => (8, true),
|
||||
OpacityDigit::SecondNine => (9, true),
|
||||
OpacityDigit::SecondZero => (0, true),
|
||||
}
|
||||
}
|
||||
|
||||
/// What `fired` sets, through the pair state `keys`, at `now` seconds (the pass time): `None` for another command,
|
||||
/// a release, or a held key's repeat, which types no second digit.
|
||||
pub fn entry(keys: &mut DigitKeys, fired: &Fired, now: f64) -> Option<DigitEntry> {
|
||||
if fired.phase != FirePhase::Press {
|
||||
return None;
|
||||
}
|
||||
let (digit, second) = OpacityDigit::from_ref(&fired.command)?.digit();
|
||||
keys.press(digit, second, now)
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@ mod crop;
|
||||
|
||||
mod navigate;
|
||||
|
||||
mod opacity_keys;
|
||||
|
||||
mod pasteboard;
|
||||
|
||||
mod raster;
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
//! The number keys of a tool's opacity: a digit key fires its `OpacityDigit` command, which sets the opacity, and
|
||||
//! Shift with it the second value.
|
||||
|
||||
use craft_behavior::DigitKeys;
|
||||
use craft_ui::canvas::OpacityDigit;
|
||||
use craft_ui::runtime::frame::take_output;
|
||||
use craft_ui::testing::input::key_press;
|
||||
use craft_ui::testing::{Frames, trial};
|
||||
use egui::{Key, Modifiers, Ui};
|
||||
|
||||
#[trial]
|
||||
fn a_digit_key_sets_the_opacity_and_shift_with_it_the_second_value() {
|
||||
let mut show = |_: &mut Ui| {};
|
||||
let mut frames = Frames::new();
|
||||
frames.idle(2, &mut show);
|
||||
let mut keys = DigitKeys::default();
|
||||
let mut press = |frames: &mut Frames, key: Key, modifiers: Modifiers| {
|
||||
frames.frame(vec![key_press(key, modifiers)], &mut show);
|
||||
let now = frames.ctx.input(|input| input.time);
|
||||
let entries: Vec<_> = take_output(&frames.ctx).fired.iter().filter_map(|fired| OpacityDigit::entry(&mut keys, fired, now)).collect();
|
||||
assert_eq!(entries.len(), 1, "{key:?} with {modifiers:?} makes one entry");
|
||||
(entries[0].percent, entries[0].second)
|
||||
};
|
||||
assert_eq!(press(&mut frames, Key::Num4, Modifiers::NONE), (40.0, false));
|
||||
assert_eq!(press(&mut frames, Key::Num5, Modifiers::NONE), (45.0, false), "a second digit at once makes the exact value");
|
||||
assert_eq!(press(&mut frames, Key::Num0, Modifiers::SHIFT), (100.0, true));
|
||||
}
|
||||
Reference in New Issue
Block a user