<?phpnamespace App\Gui;use App\Core\WinZik;use JumPHP\Gui\Pointer;/** * One of the windows of 2001 drawn on the desktop (Form1 the player, Form2 the play list, Form4 the compact bar, * Form5 the lyrics): a skinned rectangle without a title bar, moved by dragging its background. Coordinates are * desktop pixels (= the pixels of the skin); the Desktop widget zooms them. */abstract class SkinWindow{ public float $x = 0; public float $y = 0; /** the control pressed (its id), '' = none */ protected string $pressed = ''; public function __construct(protected WinZik $wz, protected Desktop $desk) { } abstract public function id(): string; abstract public function width(): float; abstract public function height(): float; abstract public function visible(): bool; /** Draws itself; the canvas' origin is already the window's corner. */ abstract public function render(Canvas $c): void; /** * How often the desktop version draws it again when nothing happens to it (seconds): what moves on it by itself. * A click, a key, the pointer over it: drawn at once anyway. */ public function redrawEvery(): float { return 0.25; } /** * The controls: id => [x, y, w, h] in the window. * @return array<string, array{0: float, 1: float, 2: float, 3: float}> */ public function controls(): array { return []; } public function contains(float $dx, float $dy): bool { return $this->visible() && $dx >= $this->x && $dx < $this->x + $this->width() && $dy >= $this->y && $dy < $this->y + $this->height(); } /** The control under a point of the window, '' = the background. */ public function controlAt(float $x, float $y): string { foreach ($this->controls() as $id => [$cx, $cy, $cw, $ch]) { if ($x >= $cx && $x < $cx + $cw && $y >= $cy && $y < $cy + $ch) { return $id; } } return ''; } /** A press in the window: true when a control took it, false = the window is dragged. */ public function press(float $x, float $y, Pointer $p): bool { $this->pressed = $this->controlAt($x, $y); return $this->pressed !== ''; } /** The control pressed, '' = none. */ public function pressedControl(): string { return $this->pressed; } /** A control whose press is a drag of its own (a slider): the others let the window move when the pointer goes. */ public function dragsItself(string $control): bool { return false; } /** The press becomes a move of the window: the control forgets it. */ public function cancelPress(): void { $this->pressed = ''; } /** The pointer moves while pressed on a control. */ public function drag(float $x, float $y): void { } /** Released: a button is clicked when the pointer is still on it. */ public function release(float $x, float $y, bool $double): void { $id = $this->pressed; $this->pressed = ''; if ($id !== '' && $this->controlAt($x, $y) === $id) { \App\Native\NativeShell::trace('clicked ' . $this->id() . '.' . $id . ($double ? ' (double)' : '')); $this->clicked($id, $double); } elseif ($id !== '') { \App\Native\NativeShell::trace('released off ' . $this->id() . '.' . $id . ' at ' . round($x) . ',' . round($y)); } } /** A button of the window was clicked. */ protected function clicked(string $id, bool $double): void { } /** the control under the pointer ('' = none, the background) and since when (Desktop::$clock) */ protected string $hovered = ''; protected float $hoveredSince = 0.0; /** Where the pointer is over the window (-1, -1: away), each frame: the control under it lights up. */ public function hover(float $x, float $y): void { $id = $x < 0 ? '' : $this->controlAt($x, $y); if ($id !== $this->hovered) { $this->hovered = $id; $this->hoveredSince = $this->desk->clock; } } /** The control under the pointer, '' = none. */ public function hoveredControl(): string { return $this->hovered; } /** What a control does, in a few words, when it is not obvious ('' = nothing to say): the window shows it itself. */ public function tip(string $control): string { return ''; } /** The tip of the control under the pointer once it has stayed on it a moment, '' = none (or a control held). */ public function hint(float $after = 0.6): string { if ($this->hovered === '' || $this->pressed !== '' || $this->desk->clock - $this->hoveredSince < $after) { return ''; } return $this->tip($this->hovered); } public function wheel(float $notches): bool { return false; } public function update(float $dt): void { } /** The pressed button drawn pushed in (a dark veil over it); the button under the pointer, lit (a light veil). */ protected function pushed(Canvas $c, string $id): void { $controls = $this->controls(); if (!isset($controls[$id])) { return; } [$x, $y, $w, $h] = $controls[$id]; if ($this->pressed === $id) { $c->fill($x, $y, $w, $h, [0, 0, 0, 70]); } elseif ($this->pressed === '' && $this->hovered === $id) { $c->fill($x, $y, $w, $h, [255, 255, 255, 40]); } } /** Where it is, saved in the settings. */ public function remember(): void { $this->wz->ui[$this->id()]['x'] = (int)round($this->x); $this->wz->ui[$this->id()]['y'] = (int)round($this->y); $this->wz->touch(); } public function restore(): void { $this->x = (float)($this->wz->ui[$this->id()]['x'] ?? 0); $this->y = (float)($this->wz->ui[$this->id()]['y'] ?? 0); }}