'fill', 'height' => 'fill']; public float $zoom = 0; public float $panX = 0; public float $panY = 0; private string $shownPart = ''; private bool $stroking = false; private float $grabX = 0; private float $grabY = 0; /** the pixel under the pointer (-1 none), shown in the status line */ public int $hoverX = -1; public int $hoverY = -1; /** where the pointer was last seen by the form (view pixels): the wheel zooms around it */ public float $aimX = -1; public float $aimY = -1; public const MAX_ZOOM = 40; private function editor(): ?SkinEditorForm { return $this->form instanceof SkinEditorForm ? $this->form : null; } protected function content(Theme $t, float $maxWidth, string $breakpoint): array { return [$maxWidth, 300]; } /** The zoom that shows the whole piece. */ public function fit(): void { $editor = $this->editor(); if ($editor === null || $this->w <= 0) { return; } [$iw, $ih] = $editor->document()->size($editor->part); $this->zoom = max(1, floor(min(($this->w - 16) / $iw, ($this->h - 16) / $ih))); $this->zoom = min($this->zoom, 24); $this->panX = ($this->w - $iw * $this->zoom) / 2; $this->panY = ($this->h - $ih * $this->zoom) / 2; } /** @return array{0: int, 1: int} the pixel of the piece under a view point */ public function pixelAt(float $px, float $py): array { return [(int)floor(($px - $this->x - $this->panX) / max(1, $this->zoom)), (int)floor(($py - $this->y - $this->panY) / max(1, $this->zoom))]; } public function update(float $dt): void { $editor = $this->editor(); if ($editor === null) { return; } if ($this->shownPart !== $editor->part || $this->zoom <= 0) { $this->shownPart = $editor->part; $this->fit(); } $engine = $this->form?->engineOrNull(); if ($engine !== null) { $m = $engine->input->getMouse(); [$this->hoverX, $this->hoverY] = $this->contains($m->x, $m->y) ? $this->pixelAt($m->x, $m->y) : [-1, -1]; } } public function render(Renderer $r, Theme $t): void { $editor = $this->editor(); $r->fillRect($this->x, $this->y, $this->w, $this->h, '#404040'); if ($editor === null) { return; } [$iw, $ih] = $editor->document()->size($editor->part); $z = max(1, $this->zoom); $x0 = $this->x + $this->panX; $y0 = $this->y + $this->panY; // the transparent pixels show a chequerboard, cut to the widget $cx0 = max($x0, $this->x); $cy0 = max($y0, $this->y); $cx1 = min($x0 + $iw * $z, $this->x + $this->w); $cy1 = min($y0 + $ih * $z, $this->y + $this->h); if ($cx1 <= $cx0 || $cy1 <= $cy0) { return; } $r->fillRect($cx0, $cy0, $cx1 - $cx0, $cy1 - $cy0, '#c8c8c8'); $step = 8; for ($yy = $cy0; $yy < $cy1; $yy += $step) { for ($xx = $cx0 + ((int)(($yy - $cy0) / $step) % 2) * $step; $xx < $cx1; $xx += 2 * $step) { $r->fillRect($xx, $yy, min($step, $cx1 - $xx), min($step, $cy1 - $yy), '#9a9a9a'); } } // the picture: the part of it inside the widget $sdl = $this->form?->sdlOrNull(); $file = $editor->previewFile($editor->part); $name = Picture::texture($sdl, $file); if ($name !== null && $sdl !== null) { $tex = $sdl->getTexture($name); $sx = ($cx0 - $x0) / $z; $sy = ($cy0 - $y0) / $z; $sw = ($cx1 - $cx0) / $z; $sh = ($cy1 - $cy0) / $z; $r->copy($tex['texture'], [(int)floor($sx), (int)floor($sy), (int)ceil($sw), (int)ceil($sh)], $x0 + floor($sx) * $z, $y0 + floor($sy) * $z, ceil($sw) * $z, ceil($sh) * $z); } // the grid when the pixels are big if ($z >= 6) { for ($i = 0; $i <= $iw; $i++) { $gx = $x0 + $i * $z; if ($gx >= $cx0 && $gx <= $cx1) { $r->fillRect($gx, $cy0, 1, $cy1 - $cy0, [0, 0, 0, 50]); } } for ($j = 0; $j <= $ih; $j++) { $gy = $y0 + $j * $z; if ($gy >= $cy0 && $gy <= $cy1) { $r->fillRect($cx0, $gy, $cx1 - $cx0, 1, [0, 0, 0, 50]); } } } if ($this->hoverX >= 0 && $this->hoverX < $iw && $this->hoverY >= 0 && $this->hoverY < $ih && $editor->tool !== 'main') { $r->drawRect($x0 + $this->hoverX * $z, $y0 + $this->hoverY * $z, $z, $z, '#ff8000', max(1, (int)($z / 8))); } } public function pointerDown(Pointer $p): void { $editor = $this->editor(); if ($editor === null) { return; } if ($editor->tool === 'main') { [$this->grabX, $this->grabY] = [$p->x - $this->panX, $p->y - $this->panY]; return; } [$px, $py] = $this->pixelAt($p->x, $p->y); if ($editor->tool === 'pipette') { $editor->pick($px, $py); return; } $this->stroking = true; $editor->document()->checkpoint($editor->part); $this->paint($px, $py); } public function pointerDrag(Pointer $p): void { $editor = $this->editor(); if ($editor === null) { return; } if ($editor->tool === 'main') { [$this->panX, $this->panY] = [$p->x - $this->grabX, $p->y - $this->grabY]; return; } if ($this->stroking && in_array($editor->tool, ['crayon', 'gomme'], true)) { [$px, $py] = $this->pixelAt($p->x, $p->y); $this->paint($px, $py); } } public function pointerUp(Pointer $p, bool $inside): void { $editor = $this->editor(); if ($this->stroking && $editor !== null) { $editor->document()->endStroke($editor->part); } $this->stroking = false; $editor?->refresh(); } private function paint(int $px, int $py): void { $editor = $this->editor(); if ($editor === null) { return; } $doc = $editor->document(); match ($editor->tool) { 'pot' => $doc->fill($editor->part, $px, $py, $editor->color), 'gomme' => $doc->plot($editor->part, $px, $py, ''), default => $doc->plot($editor->part, $px, $py, $editor->color), }; } public function wheel(float $notches): bool { if ($notches == 0) { return false; } [$x, $y] = $this->contains($this->aimX, $this->aimY) ? [$this->aimX, $this->aimY] : $this->mouse(); $this->zoomBy($notches > 0 ? 1 : -1, $x, $y); return true; } /** @return array{0: float, 1: float} the mouse of the engine, else the middle */ private function mouse(): array { $m = $this->form?->engineOrNull()?->input->getMouse(); return $m !== null && $this->contains($m->x, $m->y) ? [(float)$m->x, (float)$m->y] : [$this->x + $this->w / 2, $this->y + $this->h / 2]; } /** One step closer (+1) or further (-1); the pixel under the view point ($x, $y; else the middle) stays under it. */ public function zoomBy(int $direction, ?float $x = null, ?float $y = null): void { $x ??= $this->x + $this->w / 2; $y ??= $this->y + $this->h / 2; $old = max(1, $this->zoom); $this->zoom = max(1, min(self::MAX_ZOOM, $old + $direction * max(1, floor($old / 4)))); $this->panX = ($x - $this->x) - (($x - $this->x - $this->panX) / $old) * $this->zoom; $this->panY = ($y - $this->y) - (($y - $this->y - $this->panY) / $old) * $this->zoom; } }