Skip to content

One keyboard and mouse for a Mac and a Windows PC over the local network, in either direction.

4.1 KB
Raw Blame
"""Selvedge: tension opens a woven seam; the threads stay continuous and mend behind you."""

import math
from core.i18n import N_

from .effects import Effect, clamp, ease_out
from .fx_material import stroke, surface


def _cloth(pen, s, level, departing):
    duration = (0.48, 0.64, 0.78)[level]
    t = s.since
    if t is not None and t >= duration:
        return
    pushing = departing and t is None
    p = clamp(s.pressure) if pushing else 1.0
    u = 0.0 if pushing else clamp(t / duration)
    alpha = clamp(p * 5) if pushing else 1 - clamp((u - 0.55) / 0.45)
    tension = 0.55 if s.reduced else p
    opening = 0.0
    if not pushing:
        if s.reduced:
            alpha = 1 - u
        elif departing:
            opening = math.sin(math.pi * ease_out(u)) * 29
            tension = 1 - ease_out(u)
        else:
            opening = 24 * (1 - ease_out(clamp(u / 0.65)))
            tension = 0.65 * (1 - ease_out(u))
    at = surface(pen, s, departing)
    span = (48, 88, 132)[level]
    rows = (2, 4, 6)[level]
    samples = (8, 10, 12)[level]
    palette = s.palette
    # The warp stays anchored at the wall while the weft tightens across it.
    if level:
        pen.beginPath()
        for i in range(-8 if level == 2 else -5, 9 if level == 2 else 6):
            x = i * 14
            envelope = max(0, 1 - abs(x) / span)
            y = (5 + (rows - 1) * 4 + 13 * tension * envelope)
            pen.moveTo(*at(x, 2))
            pen.lineTo(*at(x + math.copysign(opening * envelope, x or 1), y))
        stroke(pen, palette[1 % len(palette)], 1.0, alpha * 0.62, s.dark)
    for pigment, colour in enumerate(palette):
        pen.beginPath()
        for row in range(rows):
            if row % len(palette) != pigment:
                continue
            def yarn(x):
                envelope = max(0, 1 - (x / span) ** 2)
                lift = (10 + level * 5) * tension * envelope
                split = opening * envelope * (1 if row % 2 else -0.12)
                return max(2, 7 + row * 4 + lift + split)
            pen.moveTo(*at(-span, yarn(-span)))
            step = 2 * span / samples
            for j in range(samples):
                a, b = -span + j * step, -span + (j + 1) * step
                # One cubic per half weave replaces a polyline's dense sampled geometry.
                weave = (3 if level < 2 else 7) * (1 - 0.65 * tension) * (-1 if (j + row) % 2 else 1)
                c1, c2 = a + step / 3, b - step / 3
                pen.bezierCurveTo(*at(c1, max(2, yarn(c1) + weave)),
                                  *at(c2, max(2, yarn(c2) + weave)), *at(b, yarn(b)))
        stroke(pen, colour, 1.8 if level == 0 else 1.6, alpha, s.dark)
    if level:
        # Short overpasses hide the warp at alternating crossings, making an actual weave.
        pen.beginPath()
        for i in range(-4, 5):
            x = i * 14
            envelope = max(0, 1 - (x / span) ** 2)
            y = (9 + (10 + level * 5) * tension * envelope) + opening * envelope
            pen.moveTo(*at(x - 2, y - 1))
            pen.lineTo(*at(x + 2, y + 1))
        stroke(pen, palette[-1], 2.1, alpha * 0.85, s.dark)


def _make(id_, name, level, blurb):
    return Effect(id_, name, ("quiet", "medium", "showpiece")[level], blurb,
                  lambda pen, s: _cloth(pen, s, level, True),
                  lambda pen, s: _cloth(pen, s, level, False),
                  (0.48, 0.64, 0.78)[level], (0.48, 0.64, 0.78)[level])


EFFECTS = [
    _make("thread", N_("Thread"), 0, N_("Two fine yarns tighten, part for the pointer and stitch the seam closed behind it.")),
    _make("weave", N_("Weave"), 1, N_("A woven border gathers tension, opens between its strands and gently mends on arrival.")),
    _make("jacquard", N_("Jacquard"), 2, N_("A diamond-patterned textile tightens across the edge, opens its weave and settles behind you.")),
]

PACKS = {
    "woad": (N_("Woad"), ["#8277b3", "#7268a4", "#c3b9df"]),
    "saffron": (N_("Saffron"), ["#d48238", "#976025", "#efc18a"]),
    "flax": (N_("Flax"), ["#cfb885", "#8a9c84", "#efdfb8"]),
    "madder": (N_("Madder"), ["#c45b68", "#873d57", "#e5b09b"]),
    "tide": (N_("Tide"), ["#58a9a3", "#547498", "#b1d1c1"]),
}