Cotton code
92 lines · 4.3 KB · RustRaw
//! THE CONTEST (owner, 15 Nov 2026: "ink search anywhere on the chat"): on//! a surface that scrolls under the finger, whose a stroke is. A deck gives//! every travelling stroke to the ink, because nothing under it follows the//! finger; Messages' stream of chats scrolls up and down, so there a stroke//! is contested, and these are the rules a shell holds it to. A surface the//! alphabet does not name (`surfaces`) is not contested: every deck, list//! and reader reads its ink exactly as before.//!//! - The scroll is never held back: it follows the finger from its first//!   move, with no wait. A stroke that stays straight is the scroll's and is//!   never ink, so a lone upright line (an l, an i, a 1) written over the//!   messages scrolls them; it is written on the chat's head, where nothing//!   scrolls, or after another letter.//! - A stroke that BENDS is the ink's (`claims`): once the narrower side of//!   its box is `bend` points and `bend_ratio` of the longer, it is drawn//!   from where it began, the scroll stands down and goes back to where it//!   was when the finger came down. No letter but a straight line stays//!   that narrow. A wide stroke that turns back on itself is the ink's too,//!   once it has travelled `turn_back` times its box's width: a flat//!   scratch-out never bends that much, and nothing scrolls sideways, so a//!   stroke doubling back across the chat is never the scroll's. A scroll//!   that turns back up and down is still the scroll's.//! - While a letter is in progress every stroke is the ink's at once (the//!   bar of a T, the dot of an i), until the letter is read.//! - A stroke that never bent is read at the lift (`at_lift`): a level one//!   (`swipe_level`: its rise no more than that part of its run) as long as//!   `swipe` is the step to the next chat (leftward) or the one before, a//!   plain cut; with letters written, a level one shorter than the//!   alphabet's `swipe_at_once` is the ink's, the stroke that takes a//!   letter back or clears them (`letters`); any other is the scroll's.use serde::{Deserialize, Serialize};use super::{Knobs, Point};/// A contested surface's knobs (`alphabet.json`'s `surfaces`), in points.#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]#[serde(deny_unknown_fields)]pub struct Contest {    /// The narrower side of a stroke's box at which it is the ink's.    pub bend: f64,    /// And the part of the longer side it must be.    pub bend_ratio: f64,    /// How many times its box's width a stroke wider than tall has travelled    /// when it is the ink's: it turned back on itself.    pub turn_back: f64,    /// How far a level stroke runs before it steps to the next chat.    pub swipe: f64,    /// The most a level stroke rises, as a part of its run.    pub swipe_level: f64,}/// What a stroke that never bent comes to at the lift.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum Lifted {    /// The next chat (1) or the one before (-1), at once.    Step(i8),    /// The ink's: it edits the letters written.    Ink,    /// The scroll's, or nobody's.    Nothing,}impl Contest {    /// Whether a stroke whose box is `width` by `height` so far, `travel`    /// points along, is the ink's: it has bent, or it is wide and has    /// turned back on itself.    pub fn claims(&self, width: f64, height: f64, travel: f64) -> bool {        let (narrow, long) = if width < height { (width, height) } else { (height, width) };        (narrow >= self.bend && narrow >= self.bend_ratio * long) || (width > height && width >= self.bend && travel >= self.turn_back * width)    }    /// A stroke that never bent, lifted: from where it began to where it    /// ended, `lettered` when letters are written on the surface.    pub fn at_lift(&self, from: Point, to: Point, lettered: bool, knobs: &Knobs) -> Lifted {        let (run, rise) = (to.x - from.x, (to.y - from.y).abs());        if rise > self.swipe_level * run.abs() {            return Lifted::Nothing;        }        if lettered && run.abs() < knobs.swipe_at_once {            return Lifted::Ink;        }        if run.abs() >= self.swipe {            return Lifted::Step(if run < 0.0 { 1 } else { -1 });        }        Lifted::Nothing    }}#[cfg(test)]#[path = "contest_tests.rs"]mod tests;