NL
apps/winzik / src / Gui / EqWindow.php · commit 6576d6f
<?phpnamespace App\Gui;use App\Model\Equalizer;use JumPHP\Gui\Pointer;/** * The equalizer window (258 x 116, beside the player): ON, AUTO, PRESETS, the curve of the bands, the preamp and the 10 * bands from 60 Hz to 16 kHz (-12..+12 dB). Drawn in the colours of the skin (the skins of 2001 had no equalizer). */final class EqWindow extends SkinWindow{    public const WIDTH = 258;    public const HEIGHT = 116;    private const TRACK_TOP = 40;    private const TRACK_H = 56;    public function id(): string    {        return 'eq';    }    public function width(): float    {        return self::WIDTH;    }    public function height(): float    {        return self::HEIGHT;    }    public function visible(): bool    {        $ui = $this->wz->ui;        return $ui['eq']['visible'] && !$ui['compact']['active'] && !$ui['lyricsOnly'] && !$ui['tray'];    }    /** x of a slider: 0 = the preamp, 1..10 the bands */    public static function sliderX(int $i): float    {        return $i === 0 ? 14 : 66 + ($i - 1) * 18.6;    }    public function controls(): array    {        $c = ['close' => [240.0, 1.0, 15.0, 11.0], 'on' => [6.0, 16.0, 26.0, 12.0], 'auto' => [34.0, 16.0, 32.0, 12.0], 'presets' => [204.0, 16.0, 48.0, 12.0],            'curve' => [70.0, 15.0, 130.0, 20.0]];      // the little screen of the curve: a double click flattens it        for ($i = 0; $i <= 10; $i++) {            $c['s' . $i] = [self::sliderX($i) - 4, (float)(self::TRACK_TOP - 4), 13.0, (float)(self::TRACK_H + 8)];        }        return $c;    }    public function tip(string $control): string    {        return match ($control) {            'on' => $this->wz->eq->on ? 'Clic : égaliseur arrêté' : 'Clic : égaliseur en marche',            'auto' => 'AUTO : un réglage retenu par morceau',            'presets' => 'Réglages tout prêts',            'curve' => 'Double-clic : tout remettre à 0 dB',            default => '',        };    }    /** The slider pressed or under the pointer (0 = the preamp, 1..10 the bands), null: none. */    private function slider(): ?int    {        $id = $this->pressed !== '' ? $this->pressed : $this->hovered;        return preg_match('/^s(\d+)$/', $id, $m) === 1 ? (int)$m[1] : null;    }    /**     * What the stripe at the top says: the slider pressed or under the pointer, its band and its value ("3 kHz : +4,5 dB");     * the pointer a moment on a button, what it does; else the window's name.     */    public function caption(): string    {        $i = $this->slider();        if ($i !== null) {            $eq = $this->wz->eq;            $db = $i === 0 ? $eq->preamp : $eq->bands[$i - 1];            $band = $i === 0 ? 'PREAMP' : Equalizer::BANDS[$i - 1];            $band = str_ends_with($band, 'K') ? substr($band, 0, -1) . ' kHz' : ($i === 0 ? $band : $band . ' Hz');            return $band . ' : ' . str_replace('.', ',', sprintf('%+.1f dB', $db));        }        return $this->hint() ?: 'WINZIK EQUALIZER';    }    /** The wheel over a slider: 1 dB a notch (up: louder). */    public function wheel(float $notches): bool    {        $i = $this->slider();        if ($i === null || $notches == 0.0) {            return false;        }        $eq = $this->wz->eq;        $step = ($notches > 0 ? 1 : -1) * max(1, (int)round(abs($notches)));        if ($i === 0) {            $eq->preamp = Equalizer::clamp($eq->preamp + $step);        } else {            $eq->bands[$i - 1] = Equalizer::clamp($eq->bands[$i - 1] + $step);        }        $this->wz->eqChanged();        return true;    }    private function knobY(float $db): float    {        return self::TRACK_TOP + (Equalizer::MAX - $db) / (Equalizer::MAX - Equalizer::MIN) * (self::TRACK_H - 6);    }    private function dbAt(float $y): float    {        $t = ($y - self::TRACK_TOP - 3) / (self::TRACK_H - 6);        return Equalizer::clamp(Equalizer::MAX - $t * (Equalizer::MAX - Equalizer::MIN));    }    public function render(Canvas $c): void    {        $eq = $this->wz->eq;        $skin = $this->wz->skin;        $colors = $skin->colors;        $pal = $skin->palette();        // dressed by the skin: its case, the title stripe of its player, its close button, its sliders' knob        $c->fill(0, 0, self::WIDTH, self::HEIGHT, $pal['case']);        $size = $c->imageSize('main');        if ($size !== null) {            $c->image('main', 0, 0, 60, 14, [0, 0, 60, 14]);            $c->image('main', 60, 0, self::WIDTH - 120, 14, [60, 0, 20, 14]);            $c->image('main', self::WIDTH - 60, 0, 60, 14, [$size[0] - 60, 0, 60, 14]);        }        $c->fill(70, 2, 118, 10, [0, 0, 0, 170]);        $c->text($c->fit($this->caption(), 116, 7.5), 129, 2, 7.5, $colors['title'], 'center');        if (!$c->image('close', 240, 1, 15, 12)) {            $c->bevel(240, 1, 15, 11);            $c->text('x', 247.5, 1, 8, '#000000', 'center');        }        $this->pushed($c, 'close');        $c->frame(0, 0, self::WIDTH, self::HEIGHT, $colors['barGreenLeft']);        $this->toggle($c, 6, 16, 26, 'ON', $eq->on, $pal);        $this->toggle($c, 34, 16, 32, 'AUTO', $eq->auto, $pal);        $this->pushed($c, 'on');        $this->pushed($c, 'auto');        $c->bevel(204, 16, 48, 12, true, $pal['face']);        $c->text('PRESETS', 228, 18, 7, $pal['ink'], 'center');        $this->pushed($c, 'presets');        // the curve of the bands, in a little screen of the skin        $c->fill(70, 15, 130, 20, $pal['list']);        $c->fill(70, 25, 130, 1, [255, 255, 255, 40]);        $prev = null;        foreach ($eq->bands as $i => $db) {            $x = 74 + $i * 13.5;            $y = 25 - $db / 12 * 8;            if ($prev !== null) {                for ($k = 0; $k <= 8; $k++) {                    $c->fill($prev[0] + ($x - $prev[0]) * $k / 8, $prev[1] + ($y - $prev[1]) * $k / 8, 1.5, 1.5, $colors['barGreenLeft']);                }            }            $prev = [$x, $y];        }        // the scale and the sliders        $c->text('+12db', 40, self::TRACK_TOP - 2, 6, $pal['caseInk']);        $c->text('+0db', 42, self::TRACK_TOP + self::TRACK_H / 2 - 5, 6, $pal['caseInk']);        $c->text('-12db', 40, self::TRACK_TOP + self::TRACK_H - 8, 6, $pal['caseInk']);        for ($i = 0; $i <= 10; $i++) {            $db = $i === 0 ? $eq->preamp : $eq->bands[$i - 1];            $x = self::sliderX($i);            $c->fill($x, self::TRACK_TOP, 5, self::TRACK_H, $pal['list']);            // the slider is coloured like the bargraph: green (low) to red (high)            $level = ($db - Equalizer::MIN) / (Equalizer::MAX - Equalizer::MIN);            $fill = $level < 0.5 ? $colors['barGreenLeft'] : ($level < 0.8 ? $colors['barOrangeLeft'] : $colors['barRedLeft']);            $ky = $this->knobY($db);            $c->fill($x + 1, $ky + 3, 3, self::TRACK_TOP + self::TRACK_H - $ky - 3, $eq->on ? $fill : '#404050');            if (!$c->image('bpos', $x - 3, $ky, 11, 6)) {                $c->bevel($x - 3, $ky, 11, 6, true, $pal['face']);            }            if ($this->slider() === $i) {                $c->fill($x - 3, $ky, 11, 6, [255, 255, 255, $this->pressed !== '' ? 70 : 40]);     // the knob under the pointer, lit            }            $label = $i === 0 ? 'PREAMP' : Equalizer::BANDS[$i - 1];            $c->text($label, $x + 2.5, self::TRACK_TOP + self::TRACK_H + 3, 6, $pal['caseInk'], 'center');        }        $c->fill(56, self::TRACK_TOP, 1, self::TRACK_H, [128, 128, 128, 120]);    }    /** @param array{case: string, list: string, face: string, ink: string, caseInk: string} $pal */    private function toggle(Canvas $c, float $x, float $y, float $w, string $label, bool $on, array $pal): void    {        $c->bevel($x, $y, $w, 12, !$on, $pal['face']);        $c->fill($x + 3, $y + 4, 4, 4, $on ? $this->wz->skin->colors['barGreenLeft'] : '#204020');        $c->text($label, $x + 9, $y + 2, 7, $pal['ink']);    }    public function dragsItself(string $control): bool    {        return str_starts_with($control, 's');    }    public function press(float $x, float $y, Pointer $p): bool    {        if (!parent::press($x, $y, $p)) {            return false;        }        $this->drag($x, $y);        return true;    }    public function drag(float $x, float $y): void    {        if (!str_starts_with($this->pressed, 's')) {            return;        }        $i = (int)substr($this->pressed, 1);        $db = $this->dbAt($y);        if ($i === 0) {            $this->wz->eq->preamp = $db;        } else {            $this->wz->eq->bands[$i - 1] = $db;        }        $this->wz->eqChanged();    }    public function release(float $x, float $y, bool $double): void    {        if (str_starts_with($this->pressed, 's')) {            if ($double) {                // a double click puts the slider back to 0 dB                $i = (int)substr($this->pressed, 1);                $i === 0 ? $this->wz->eq->preamp = 0.0 : $this->wz->eq->bands[$i - 1] = 0.0;                $this->wz->eqChanged();            }            $this->pressed = '';            return;        }        parent::release($x, $y, $double);    }    protected function clicked(string $id, bool $double): void    {        $eq = $this->wz->eq;        match ($id) {            'close' => $this->wz->toggleEq(),            'on' => $eq->on = !$eq->on,            'auto' => $eq->auto = !$eq->auto,            'presets' => $this->desk->openPresets($this->x + 204, $this->y + 28),            'curve' => $double ? $this->flatten() : null,            default => null,        };        $this->wz->eqChanged();    }    /** Every band and the preamp back to 0 dB. */    private function flatten(): void    {        $eq = $this->wz->eq;        $eq->preamp = 0.0;        $eq->bands = array_fill(0, count($eq->bands), 0.0);        $this->wz->notice('Égaliseur remis à plat', 2);    }}