ZH
apps/winzik / src / Form / ControlsForm.php · commit 6576d6f
<?phpnamespace App\Form;use App\Core\WinZik;use JumPHP\Gui\Button;use JumPHP\Gui\CheckBox;use JumPHP\Gui\ComboBox;use JumPHP\Gui\Label;use JumPHP\Gui\Radio;/** * Form11 of 2001: the action of each axis and button of the joystick (Son +, Son -, Morceau suivant...), the * joystick number, and a label that turns red while its button is held (Timer1). Saved when it closes. * * @property Radio $pad1 * @property Radio $pad2 * @property Label $padName * @property Label $lxp * @property ComboBox $xp * @property Label $lxm * @property ComboBox $xm * @property Label $lyp * @property ComboBox $yp * @property Label $lym * @property ComboBox $ym * @property Label $lb1 * @property ComboBox $b1 * @property Label $lb2 * @property ComboBox $b2 * @property Label $lb3 * @property ComboBox $b3 * @property Label $lb4 * @property ComboBox $b4 * @property CheckBox $enabled * @property Button $ok */final class ControlsForm extends WinZikDialog{    public const INPUTS = ['xp', 'xm', 'yp', 'ym', 'b1', 'b2', 'b3', 'b4'];    protected function loaded(array $params = []): void    {        foreach (self::INPUTS as $input) {            $this->$input->items = WinZik::ACTIONS;            $this->$input->selected = (int)($this->wz->controls[$input] ?? 0);        }        $pad = (int)($this->wz->options['pad'] ?? 0);        $this->pad1->checked = $pad === 0;        $this->pad2->checked = $pad === 1;        $this->enabled->checked = (bool)($this->wz->options['joystick'] ?? false);    }    protected function tick(float $dt): void    {        parent::tick($dt);        $pad = $this->pad2->checked ? 1 : 0;        $gamepad = $this->engine->input->getGamepad();        $this->padName->text = $gamepad->isConnected($pad) ? (string)$gamepad->name($pad) : 'aucune manette branchée';        $lx = $gamepad->axis($pad, 'lx');        $ly = $gamepad->axis($pad, 'ly');        $held = [            'xp' => $lx > 0.5, 'xm' => $lx < -0.5, 'yp' => $ly < -0.5, 'ym' => $ly > 0.5,            'b1' => $gamepad->isButtonHeld($pad, 'a'), 'b2' => $gamepad->isButtonHeld($pad, 'b'),            'b3' => $gamepad->isButtonHeld($pad, 'x'), 'b4' => $gamepad->isButtonHeld($pad, 'y'),        ];        foreach ($held as $input => $on) {            $label = 'l' . $input;            $this->$label->color = $on ? '#ff4040' : '';      // QBColor(12) while it is held        }    }    public function onOkClick(Button $sender): void    {        foreach (self::INPUTS as $input) {            $this->wz->controls[$input] = max(0, (int)$this->$input->selected);        }        $this->wz->options['pad'] = $this->pad2->checked ? 1 : 0;        $this->wz->options['joystick'] = (bool)$this->enabled->checked;        $this->wz->save();        $this->close();    }}