<?phpnamespace App\Mobile;use JumPHP\Renderer\Renderer;use JumPHP\Renderer\Sdl;use JumPHP\Util\Color;/** * The icons of the phone version, drawn in PHP (GD) on a grid of 24 x 24 units, four times larger then reduced (smooth * edges), one texture per name / size / colour (SDL can not tint a texture: each colour is its own picture). */final class Icons{ /** @var array<string, string> key => texture name */ private array $made = []; private int $k = 1; public function __construct(private ?Sdl $sdl) { } /** Draws an icon $px pixels wide, its top-left at x / y. */ public function draw(Renderer $r, string $name, float $x, float $y, float $px, Color|string $color): void { $size = max(4, (int)round($px)); $c = Color::of($color); $key = "$name|$size|" . $c->key(); if (!isset($this->made[$key])) { if ($this->sdl === null) { return; } $texture = 'mobile-icon-' . md5($key); try { $this->sdl->createTextureFromGd($texture, $this->picture($name, $size, $c)); } catch (\Throwable) { return; } $this->made[$key] = $texture; } $t = $this->sdl?->getTexture($this->made[$key])['texture'] ?? null; if ($t !== null) { $r->copy($t, null, round($x), round($y), $size, $size); } } /** The picture of an icon (tests can look at it). */ public function picture(string $name, int $size, Color $c): \GdImage { $this->k = 4 * $size; $big = imagecreatetruecolor($this->k, $this->k); imagealphablending($big, false); imagefill($big, 0, 0, imagecolorallocatealpha($big, 0, 0, 0, 127)); imagealphablending($big, true); $ink = imagecolorallocatealpha($big, $c->r, $c->g, $c->b, 127 - intdiv($c->a, 2)); $this->shape($big, $ink, $name); $small = imagecreatetruecolor($size, $size); imagealphablending($small, false); imagesavealpha($small, true); imagefill($small, 0, 0, imagecolorallocatealpha($small, 0, 0, 0, 127)); imagecopyresampled($small, $big, 0, 0, 0, 0, $size, $size, $this->k, $this->k); return $small; } /** grid units (0..24) -> pixels of the big picture */ private function u(float $v): int { return (int)round($v * $this->k / 24); } /** @param list<float> $points x, y, x, y... in grid units */ private function poly(\GdImage $im, int $ink, array $points): void { imagefilledpolygon($im, array_map(fn (float $v) => $this->u($v), $points), $ink); } private function dot(\GdImage $im, int $ink, float $x, float $y, float $radius): void { imagefilledellipse($im, $this->u($x), $this->u($y), $this->u(2 * $radius), $this->u(2 * $radius), $ink); } /** A thick line with round ends. */ private function line(\GdImage $im, int $ink, float $x1, float $y1, float $x2, float $y2, float $w = 2.0): void { $dx = $x2 - $x1; $dy = $y2 - $y1; $len = max(0.001, sqrt($dx * $dx + $dy * $dy)); $nx = -$dy / $len * $w / 2; $ny = $dx / $len * $w / 2; $this->poly($im, $ink, [$x1 + $nx, $y1 + $ny, $x2 + $nx, $y2 + $ny, $x2 - $nx, $y2 - $ny, $x1 - $nx, $y1 - $ny]); $this->dot($im, $ink, $x1, $y1, $w / 2); $this->dot($im, $ink, $x2, $y2, $w / 2); } /** @param list<float> $points a line through these points */ private function path(\GdImage $im, int $ink, array $points, float $w = 2.0): void { for ($i = 0; $i + 3 < count($points); $i += 2) { $this->line($im, $ink, $points[$i], $points[$i + 1], $points[$i + 2], $points[$i + 3], $w); } } private function ring(\GdImage $im, int $ink, float $x, float $y, float $radius, float $w = 2.0): void { $steps = 40; for ($i = 0; $i < $steps; $i++) { $a = 2 * M_PI * $i / $steps; $b = 2 * M_PI * ($i + 1) / $steps; $this->line($im, $ink, $x + cos($a) * $radius, $y + sin($a) * $radius, $x + cos($b) * $radius, $y + sin($b) * $radius, $w); } } private function arc(\GdImage $im, int $ink, float $x, float $y, float $radius, float $from, float $to, float $w = 2.0): void { $steps = 16; for ($i = 0; $i < $steps; $i++) { $a = deg2rad($from + ($to - $from) * $i / $steps); $b = deg2rad($from + ($to - $from) * ($i + 1) / $steps); $this->line($im, $ink, $x + cos($a) * $radius, $y + sin($a) * $radius, $x + cos($b) * $radius, $y + sin($b) * $radius, $w); } } /** @return list<float> a five-pointed star */ private static function star(float $cx, float $cy, float $outer, float $inner): array { $p = []; for ($i = 0; $i < 10; $i++) { $a = -M_PI / 2 + $i * M_PI / 5; $r = $i % 2 === 0 ? $outer : $inner; $p[] = $cx + cos($a) * $r; $p[] = $cy + sin($a) * $r; } return $p; } private function shape(\GdImage $im, int $ink, string $name): void { switch ($name) { case 'play': $this->poly($im, $ink, [7, 4.5, 20, 12, 7, 19.5]); break; case 'pause': $this->poly($im, $ink, [6, 5, 10, 5, 10, 19, 6, 19]); $this->poly($im, $ink, [14, 5, 18, 5, 18, 19, 14, 19]); break; case 'next': $this->poly($im, $ink, [4.5, 5.5, 15, 12, 4.5, 18.5]); $this->poly($im, $ink, [16.5, 5.5, 19.5, 5.5, 19.5, 18.5, 16.5, 18.5]); break; case 'prev': $this->poly($im, $ink, [19.5, 5.5, 9, 12, 19.5, 18.5]); $this->poly($im, $ink, [4.5, 5.5, 7.5, 5.5, 7.5, 18.5, 4.5, 18.5]); break; case 'more': foreach ([5, 12, 19] as $y) { $this->dot($im, $ink, 12, $y, 2.1); } break; case 'shuffle': $this->path($im, $ink, [3.5, 7, 8, 7, 15, 17, 19, 17]); $this->path($im, $ink, [3.5, 17, 8, 17, 15, 7, 19, 7]); $this->poly($im, $ink, [18, 3.5, 22, 7, 18, 10.5]); $this->poly($im, $ink, [18, 13.5, 22, 17, 18, 20.5]); break; case 'repeat': $this->path($im, $ink, [5, 13, 5, 8, 17, 8]); $this->poly($im, $ink, [16, 4.5, 20.5, 8, 16, 11.5]); $this->path($im, $ink, [19, 11, 19, 16, 7, 16]); $this->poly($im, $ink, [8, 12.5, 3.5, 16, 8, 19.5]); break; case 'volume': case 'mute': $this->poly($im, $ink, [3, 9, 7.5, 9, 12.5, 4.5, 12.5, 19.5, 7.5, 15, 3, 15]); if ($name === 'volume') { $this->arc($im, $ink, 12.5, 12, 4, -50, 50, 2); $this->arc($im, $ink, 12.5, 12, 8, -55, 55, 2); } else { $this->line($im, $ink, 15.5, 9, 21, 15, 2); $this->line($im, $ink, 21, 9, 15.5, 15, 2); } break; case 'note': $this->dot($im, $ink, 8, 17.5, 3.4); $this->line($im, $ink, 10.6, 17.5, 10.6, 5, 2.2); $this->poly($im, $ink, [9.5, 3.8, 19, 6, 19, 10, 9.5, 7.8]); $this->dot($im, $ink, 16.5, 15.5, 3); $this->line($im, $ink, 18.8, 15.5, 18.8, 7, 2.2); break; case 'list': foreach ([6, 12, 18] as $y) { $this->line($im, $ink, 4, $y, 20, $y, 2.2); } break; case 'queue': foreach ([6, 11] as $y) { $this->line($im, $ink, 4, $y, 16, $y, 2); } $this->line($im, $ink, 4, 16, 11, 16, 2); $this->line($im, $ink, 17, 13, 17, 21, 2); $this->line($im, $ink, 13, 17, 21, 17, 2); break; case 'plus': $this->line($im, $ink, 12, 4.5, 12, 19.5, 2.4); $this->line($im, $ink, 4.5, 12, 19.5, 12, 2.4); break; case 'search': $this->ring($im, $ink, 10.5, 10.5, 6, 2.2); $this->line($im, $ink, 15, 15, 20, 20, 2.6); break; case 'close': $this->line($im, $ink, 6, 6, 18, 18, 2.2); $this->line($im, $ink, 18, 6, 6, 18, 2.2); break; case 'back': $this->line($im, $ink, 19, 12, 5.5, 12, 2.2); $this->path($im, $ink, [11, 6, 5, 12, 11, 18], 2.2); break; case 'chevron': $this->path($im, $ink, [9, 5.5, 15.5, 12, 9, 18.5], 2.2); break; case 'up': $this->path($im, $ink, [5.5, 15, 12, 8.5, 18.5, 15], 2.2); break; case 'down': $this->path($im, $ink, [5.5, 9, 12, 15.5, 18.5, 9], 2.2); break; case 'star': $this->poly($im, $ink, self::star(12, 12.8, 10, 4.2)); break; case 'starOutline': $p = self::star(12, 12.8, 9.2, 3.9); $p[] = $p[0]; $p[] = $p[1]; $this->path($im, $ink, $p, 1.7); break; case 'info': $this->ring($im, $ink, 12, 12, 9, 2); $this->line($im, $ink, 12, 11, 12, 17, 2.2); $this->dot($im, $ink, 12, 7.5, 1.4); break; case 'folder': $this->poly($im, $ink, [3, 6, 3, 19, 21, 19, 21, 8.5, 11.5, 8.5, 9.5, 6]); break; case 'trash': $this->line($im, $ink, 4, 6.5, 20, 6.5, 2); $this->path($im, $ink, [9, 6, 9.5, 3.8, 14.5, 3.8, 15, 6], 1.8); $this->poly($im, $ink, [6, 8.5, 18, 8.5, 16.8, 20.5, 7.2, 20.5]); break; case 'eq': foreach ([[6, 9], [12, 15], [18, 7]] as [$x, $knob]) { $this->line($im, $ink, $x, 4, $x, 20, 1.8); $this->dot($im, $ink, $x, $knob, 2.8); } break; case 'palette': $this->ring($im, $ink, 12, 12, 8.5, 2); foreach ([[8, 9], [12, 7], [16, 9], [16.5, 13.5]] as [$x, $y]) { $this->dot($im, $ink, $x, $y, 1.6); } break; case 'gear': for ($i = 0; $i < 8; $i++) { $a = $i * M_PI / 4; $this->line($im, $ink, 12 + cos($a) * 6, 12 + sin($a) * 6, 12 + cos($a) * 9.5, 12 + sin($a) * 9.5, 3.4); } $this->ring($im, $ink, 12, 12, 5.5, 3); break; case 'check': $this->path($im, $ink, [5, 12.5, 10, 17.5, 19.5, 7], 2.6); break; case 'lyrics': $this->poly($im, $ink, [4, 4, 20, 4, 20, 16, 9, 16, 5, 20, 5, 16, 4, 16]); break; case 'snow': for ($i = 0; $i < 3; $i++) { $a = M_PI / 2 + $i * M_PI / 3; [$c, $s] = [cos($a), sin($a)]; $this->line($im, $ink, 12 - $c * 9.5, 12 - $s * 9.5, 12 + $c * 9.5, 12 + $s * 9.5, 1.8); foreach ([-1, 1] as $end) { $bx = 12 + $end * $c * 6; $by = 12 + $end * $s * 6; foreach ([-1, 1] as $side) { $b = $a + $side * M_PI / 4 + ($end < 0 ? M_PI : 0); $this->line($im, $ink, $bx, $by, $bx + cos($b) * 3.2, $by + sin($b) * 3.2, 1.6); } } } break; case 'phone': $this->path($im, $ink, [7, 3, 17, 3, 17, 21, 7, 21, 7, 3], 2); $this->dot($im, $ink, 12, 18, 1.2); break; case 'mic': $this->poly($im, $ink, [9, 3.5, 15, 3.5, 15, 12.5, 9, 12.5]); $this->dot($im, $ink, 12, 3.8, 3); $this->dot($im, $ink, 12, 12.2, 3); $this->arc($im, $ink, 12, 11, 6.2, 0, 180, 1.9); $this->line($im, $ink, 12, 17.5, 12, 20.5, 1.9); $this->line($im, $ink, 8.5, 20.8, 15.5, 20.8, 1.9); break; case 'youtube': // a screen with a play triangle in it $this->path($im, $ink, [4.5, 5, 19.5, 5, 21.5, 7, 21.5, 17, 19.5, 19, 4.5, 19, 2.5, 17, 2.5, 7, 4.5, 5], 2); $this->poly($im, $ink, [9.5, 8.5, 16, 12, 9.5, 15.5]); break; case 'spotify': $this->ring($im, $ink, 12, 12, 9, 2); $this->arc($im, $ink, 12, 19, 9, 235, 305, 2); $this->arc($im, $ink, 12, 20, 7.5, 240, 300, 1.8); $this->arc($im, $ink, 12, 21, 6, 245, 295, 1.6); break; case 'store': $this->poly($im, $ink, [4, 8, 20, 8, 18.5, 20.5, 5.5, 20.5]); $this->arc($im, $ink, 12, 8, 4, 180, 360, 1.9); break; case 'window': $this->path($im, $ink, [3.5, 5, 20.5, 5, 20.5, 19, 3.5, 19, 3.5, 5], 1.9); $this->poly($im, $ink, [3.5, 5, 20.5, 5, 20.5, 9, 3.5, 9]); break; case 'save': $this->path($im, $ink, [4, 4, 17, 4, 20, 7, 20, 20, 4, 20, 4, 4], 1.9); $this->poly($im, $ink, [7.5, 4, 15.5, 4, 15.5, 9, 7.5, 9]); $this->ring($im, $ink, 12, 14.5, 2.5, 1.8); break; case 'tag': $this->path($im, $ink, [3.5, 3.5, 11.5, 3.5, 20.5, 12.5, 12.5, 20.5, 3.5, 11.5, 3.5, 3.5], 1.9); $this->dot($im, $ink, 8, 8, 1.7); break; case 'download': $this->line($im, $ink, 12, 3.5, 12, 14.5, 2.2); $this->path($im, $ink, [7, 10, 12, 15, 17, 10], 2.2); $this->line($im, $ink, 4.5, 20, 19.5, 20, 2.2); break; case 'retry': $this->arc($im, $ink, 12, 12, 7.5, -60, 230, 2.1); $this->poly($im, $ink, [15, 2.5, 20.5, 6.5, 14.5, 9.5]); break; case 'film': $this->path($im, $ink, [3.5, 5, 20.5, 5, 20.5, 19, 3.5, 19, 3.5, 5], 1.9); $this->poly($im, $ink, [10, 9, 15.5, 12, 10, 15]); break; case 'fullscreen': $this->path($im, $ink, [4, 9, 4, 4, 9, 4], 2.1); $this->path($im, $ink, [15, 4, 20, 4, 20, 9], 2.1); $this->path($im, $ink, [20, 15, 20, 20, 15, 20], 2.1); $this->path($im, $ink, [9, 20, 4, 20, 4, 15], 2.1); break; case 'rotate': $this->path($im, $ink, [6, 3.5, 13, 3.5, 13, 20.5, 6, 20.5, 6, 3.5], 1.8); $this->arc($im, $ink, 13, 12, 7, -70, 70, 1.8); $this->poly($im, $ink, [18.5, 16, 21.5, 19, 17, 20]); break; case 'paste': $this->path($im, $ink, [6, 5.5, 4.5, 5.5, 4.5, 21, 19.5, 21, 19.5, 5.5, 18, 5.5], 1.9); $this->poly($im, $ink, [8.5, 3, 15.5, 3, 15.5, 7.5, 8.5, 7.5]); $this->line($im, $ink, 8, 12, 16, 12, 1.7); $this->line($im, $ink, 8, 16, 14, 16, 1.7); break; case 'edit': $this->poly($im, $ink, [15.5, 4, 20, 8.5, 9, 19.5, 4.5, 19.5, 4.5, 15]); break; case 'logout': $this->path($im, $ink, [11, 4, 4.5, 4, 4.5, 20, 11, 20], 1.9); $this->line($im, $ink, 9.5, 12, 20, 12, 2); $this->path($im, $ink, [16, 8, 20, 12, 16, 16], 2); break; case 'stats': $this->poly($im, $ink, [4, 13, 7.5, 13, 7.5, 20, 4, 20]); $this->poly($im, $ink, [10.25, 7, 13.75, 7, 13.75, 20, 10.25, 20]); $this->poly($im, $ink, [16.5, 3.5, 20, 3.5, 20, 20, 16.5, 20]); break; case 'cloud': // a cloud: three puffs on a flat bottom $this->dot($im, $ink, 7.5, 14, 4.5); $this->dot($im, $ink, 12.5, 10.5, 6); $this->dot($im, $ink, 17, 14, 4.5); $this->poly($im, $ink, [7.5, 12, 17, 12, 17, 18.5, 7.5, 18.5]); break; case 'computer': $this->path($im, $ink, [3, 4.5, 21, 4.5, 21, 16, 3, 16, 3, 4.5], 1.9); $this->line($im, $ink, 12, 16, 12, 19.5, 2); $this->line($im, $ink, 7.5, 20, 16.5, 20, 2); break; case 'copy': $this->path($im, $ink, [8.5, 8.5, 20, 8.5, 20, 20.5, 8.5, 20.5, 8.5, 8.5], 1.9); $this->path($im, $ink, [15.5, 5.5, 15.5, 3.5, 4, 3.5, 4, 15.5, 6, 15.5], 1.9); break; case 'warning': $this->path($im, $ink, [12, 3.5, 21.5, 20, 2.5, 20, 12, 3.5], 2); $this->line($im, $ink, 12, 9.5, 12, 14, 2.2); $this->dot($im, $ink, 12, 17, 1.3); break; case 'user': $this->ring($im, $ink, 12, 8, 4, 2); $this->arc($im, $ink, 12, 21, 7.5, 180, 360, 2); break; case 'target': // « où ça joue » : a ring, a dot, four ticks $this->ring($im, $ink, 12, 12, 6.5, 2); $this->dot($im, $ink, 12, 12, 2.2); $this->line($im, $ink, 12, 2, 12, 5.5, 2); $this->line($im, $ink, 12, 18.5, 12, 22, 2); $this->line($im, $ink, 2, 12, 5.5, 12, 2); $this->line($im, $ink, 18.5, 12, 22, 12, 2); break; case 'clock': // lyrics with times (synchronised): a dial $this->ring($im, $ink, 12, 12, 8.5, 2.2); $this->line($im, $ink, 12, 12, 12, 6.8, 2.2); $this->line($im, $ink, 12, 12, 15.6, 14, 2.2); break; case 'translate': // a translation: a big A and the lines of another script beside it $this->path($im, $ink, [2.5, 19, 7.5, 5, 12.5, 19], 2.2); $this->line($im, $ink, 4.6, 14, 10.4, 14, 2); $this->line($im, $ink, 14, 7, 22, 7, 2); $this->line($im, $ink, 18, 4.5, 18, 7, 2); $this->path($im, $ink, [15.5, 10, 18, 15, 21.5, 19.5], 2); $this->path($im, $ink, [20.5, 10, 18, 15, 14.5, 19.5], 2); break; case 'key': $this->ring($im, $ink, 7.5, 12, 4, 2); $this->line($im, $ink, 11.5, 12, 21, 12, 2); $this->line($im, $ink, 18, 12, 18, 16, 2); $this->line($im, $ink, 21, 12, 21, 15, 2); break; default: $this->dot($im, $ink, 12, 12, 4); } }}