EN
apps/winzik / src / Gui / SkinPreview.php · commit 6576d6f
<?phpnamespace App\Gui;use App\Form\SkinEditorForm;use App\Model\SkinParts;use JumPHP\Gui\Picture;use JumPHP\Gui\Pointer;use JumPHP\Gui\Theme;use JumPHP\Gui\Widget;use JumPHP\Renderer\Renderer;/** * The window being skinned, with the pieces where they go (Picture1(0..2) of EDT): the piece being drawn is framed * in orange, the pieces changed in this skin have a green dot (Shape5), a click on a piece chooses it. */final class SkinPreview extends Widget{    public const DEFAULTS = ['width' => 'fill', 'height' => 'fill'];    private function editor(): ?SkinEditorForm    {        return $this->form instanceof SkinEditorForm ? $this->form : null;    }    protected function content(Theme $t, float $maxWidth, string $breakpoint): array    {        return [$maxWidth, 200];    }    /** @return array{0: float, 1: float, 2: float} where the window is drawn and its zoom */    private function placement(): array    {        $editor = $this->editor();        $window = $editor === null ? 'main' : $editor->window;        [, , $w, $h] = SkinParts::WINDOWS[$window];        $z = max(0.5, min(floor(min(($this->w - 8) / $w, ($this->h - 8) / $h) * 2) / 2, 6));        return [$this->x + ($this->w - $w * $z) / 2, $this->y + ($this->h - $h * $z) / 2, $z];    }    public function render(Renderer $r, Theme $t): void    {        $r->fillRect($this->x, $this->y, $this->w, $this->h, '#008080');        $editor = $this->editor();        if ($editor === null) {            return;        }        [$ox, $oy, $z] = $this->placement();        $window = $editor->window;        [, $background, $w, $h] = SkinParts::WINDOWS[$window];        $this->draw($r, $editor, $background, $ox, $oy, $w, $h, $z);        foreach (SkinParts::PARTS[$window] as $part => [$pw, $ph, $px, $py]) {            $shown = !in_array($part, SkinParts::HIDDEN_IN_PREVIEW, true) || $part === $editor->part;            if ($shown) {                $this->draw($r, $editor, $part, $ox + $px * $z, $oy + $py * $z, $pw, $ph, $z);            }        }        foreach (array_merge([$background], array_keys(SkinParts::PARTS[$window])) as $part) {            if ($editor->document()->isModified($part)) {                [$px, $py] = $part === $background ? [0, 0] : [SkinParts::PARTS[$window][$part][2], SkinParts::PARTS[$window][$part][3]];                $r->fillCircle($ox + $px * $z + 3, $oy + $py * $z + 3, 3, '#00ff00');            }        }        if ($editor->part !== $background && isset(SkinParts::PARTS[$window][$editor->part])) {            [$pw, $ph, $px, $py] = SkinParts::PARTS[$window][$editor->part];            $r->drawRect($ox + $px * $z - 2, $oy + $py * $z - 2, $pw * $z + 4, $ph * $z + 4, '#ff8000', 2);        } else {            $r->drawRect($ox - 3, $oy - 3, $w * $z + 6, $h * $z + 6, '#ff8000', 2);        }    }    private function draw(Renderer $r, SkinEditorForm $editor, string $part, float $x, float $y, float $w, float $h, float $z): void    {        $sdl = $this->form?->sdlOrNull();        $name = Picture::texture($sdl, $editor->previewFile($part));        if ($name === null || $sdl === null) {            return;        }        [$iw, $ih] = $editor->document()->size($part);        $r->copy($sdl->getTexture($name)['texture'], [0, 0, (int)min($iw, $w), (int)min($ih, $h)], $x, $y, min($iw, $w) * $z, min($ih, $h) * $z);    }    /** The piece under a view point ('' = the background, null = outside). */    public function partAt(float $vx, float $vy): ?string    {        $editor = $this->editor();        if ($editor === null) {            return null;        }        [$ox, $oy, $z] = $this->placement();        $x = ($vx - $ox) / $z;        $y = ($vy - $oy) / $z;        [, $background, $w, $h] = SkinParts::WINDOWS[$editor->window];        if ($x < 0 || $y < 0 || $x >= $w || $y >= $h) {            return null;        }        $found = $background;        foreach (SkinParts::PARTS[$editor->window] as $part => [$pw, $ph, $px, $py]) {            $shown = !in_array($part, SkinParts::HIDDEN_IN_PREVIEW, true) || $part === $editor->part;            if ($shown && $x >= $px && $x < $px + $pw && $y >= $py && $y < $py + $ph) {                $found = $part;    // the last one drawn is on top            }        }        return $found;    }    protected function click(Pointer $p): void    {        $part = $this->partAt($p->x, $p->y);        if ($part !== null) {            $this->editor()?->selectPart($part);        }    }}