RU
apps/winzik / src / Gui / JogDial.php · commit 6576d6f
<?phpnamespace App\Gui;use JumPHP\Gui\Pointer;use JumPHP\Gui\Theme;use JumPHP\Gui\Widget;use JumPHP\Renderer\Renderer;/** * An endless slider (a jog wheel): pressed, the value runs as long as it is held, faster the farther the pointer is from * the middle (left: down, right: up); a short click on one side: one step. No end: any whole number. Its ridges turn * while it runs. A double click: back to 0. The pointer over it: its ring lit. Event: change (int $value), at each new * value (what it moves is moved at once). * * @property int $value */final class JogDial extends Widget{    public const DEFAULTS = ['value' => 0, 'min' => -2000, 'max' => 2000];    private bool $held = false;    private float $pointerX = 0.0;    private float $heldFor = 0.0;    private float $carry = 0.0;    private float $turn = 0.0;    private int $atPress = 0;    public static function events(): array    {        return ['change'];    }    protected function content(Theme $t, float $maxWidth, string $breakpoint): array    {        return [$t->px(150), $t->px(30)];    }    /** -1 .. 1: where the pointer is, from the middle (a dead zone in the middle: it stops) */    private function lean(): float    {        $half = max(1.0, $this->w / 2);        $d = ($this->pointerX - ($this->x + $half)) / $half;        return abs($d) < 0.12 ? 0.0 : max(-1.0, min(1.0, $d));    }    public function render(Renderer $r, Theme $t): void    {        $h = $this->h;        $t->box($r, $this->x, $this->y, $this->w, $h, $this->held ? $t->color('accent') : ($this->hovered && $this->isEnabled() ? $t->color('accent')->withAlpha(150) : $t->color('track')), $h / 2);        $inner = $t->px(2);        $t->box($r, $this->x + $inner, $this->y + $inner, $this->w - 2 * $inner, $h - 2 * $inner, $t->color('surface'), ($h - 2 * $inner) / 2);        // its ridges, turning with the value        $gap = $t->px(7);        $ridge = $t->color('muted')->withAlpha(140);        $offset = fmod($this->turn, $gap);        for ($x = $this->x + $h / 2 + $offset; $x < $this->x + $this->w - $h / 2; $x += $gap) {            $r->fillRect($x, $this->y + $h * 0.25, max(1.0, $t->px(1.5)), $h * 0.5, $ridge);        }        // its arrows (drawn: a font may not have them) and its value        $font = $t->font(null, true);        $text = $t->color('text');        $a = $h * 0.22;        $cy = $this->y + $h / 2;        for ($i = 0; $i < (int)ceil($a); $i++) {            $len = ($i + 1) * 2;        // the tip outside: the column next to the edge is the shortest            $r->fillRect($this->x + $h * 0.3 + $i, $cy - $len / 2, 1, $len, $text);                     // left, pointing left            $r->fillRect($this->x + $this->w - $h * 0.3 - $i - 1, $cy - $len / 2, 1, $len, $text);      // right, pointing right        }        $label = (string)$this->props['value'];        $lw = $font->width($label, 1) + $t->px(10);        $t->box($r, $this->x + ($this->w - $lw) / 2, $this->y + $inner * 2, $lw, $h - $inner * 4, $t->color('surface'), $t->px(4));        $font->draw($r, $label, $this->x + $this->w / 2, $this->y + ($h - $font->height()) / 2, 1, $text, 'center');    }    public function pointerDown(Pointer $p): void    {        if (!$this->isEnabled()) {            return;        }        $this->held = true;        $this->pointerX = $p->x;        $this->heldFor = 0.0;        $this->carry = 0.0;        $this->atPress = (int)$this->props['value'];    }    public function pointerDrag(Pointer $p): void    {        $this->pointerX = $p->x;    }    public function pointerUp(Pointer $p, bool $inside): void    {        if ($this->held && $inside && $p->double) {            // a double click: back to 0 (its first click made a step: undone too)            $this->held = false;            $this->setValue(0);            return;        }        if ($this->held && $this->heldFor < 0.3 && (int)$this->props['value'] === $this->atPress) {            // a short click: one step to its side            $side = $this->pointerX < $this->x + $this->w / 2 ? -1 : 1;            $this->setValue((int)$this->props['value'] + $side);        }        $this->held = false;    }    /** Each frame: held, it runs (1 a second near the middle, up to 120 at the ends). */    public function update(float $dt): void    {        if (!$this->held) {            return;        }        $this->heldFor += $dt;        if ($this->heldFor < 0.3) {            return;     // a click, not a hold        }        $lean = $this->lean();        $speed = ($lean < 0 ? -1 : 1) * (1 + 119 * $lean * $lean);        if ($lean === 0.0) {            return;        }        $this->carry += $speed * $dt;        $steps = (int)$this->carry;        if ($steps !== 0) {            $this->carry -= $steps;            $this->setValue((int)$this->props['value'] + $steps);        }    }    public function setValue(int $value): void    {        $value = max((int)$this->props['min'], min((int)$this->props['max'], $value));        if ($value !== (int)$this->props['value']) {            $this->turn += ($value - (int)$this->props['value']) * 2;            $this->props['value'] = $value;            $this->emit('change', $value);        }    }    /** The mouse wheel over it: one notch, one step (up: +1); with Shift, ten. It is not the form that scrolls then. */    public function wheel(float $notches): bool    {        if (!$this->isEnabled() || $notches == 0.0) {            return false;        }        $shift = function_exists('SDL_GetModState') && (\SDL_GetModState() & 0x0003) !== 0;        $this->setValue((int)$this->props['value'] + ($notches > 0 ? 1 : -1) * ($shift ? 10 : 1) * max(1, (int)round(abs($notches))));        return true;    }    public function keys(Pointer $p): bool    {        $used = false;        foreach ($p->keys as $key) {            if ($key === Pointer::LEFT || $key === Pointer::DOWN) {                $this->setValue((int)$this->props['value'] - 1);                $used = true;            } elseif ($key === Pointer::RIGHT || $key === Pointer::UP) {                $this->setValue((int)$this->props['value'] + 1);                $used = true;            }        }        return $used;    }    public function focusable(): bool    {        return $this->isEnabled();    }}