ES
games/rob-revisited / src / Revisited / Ui / Wheel.php · commit f9a1307
<?phpnamespace App\Revisited\Ui;/** * The mouse wheel turned into whole rows: fractions (a trackpad, a browser's small deltas) are kept and added up instead * of being rounded away, so many small turns scroll as far as a few big ones; a change of direction drops what was * left of the other way. Used by the scrolling lists of the screens (Screens, SaveScreen, ChestView). */final class Wheel{    private float $acc = 0.0;    /** Rows to scroll this frame for that wheel delta (SDL: > 0 up): negative = toward the top. */    public function steps(float $wheel): int    {        if ($wheel == 0.0 || !is_finite($wheel)) {            return 0;        }        if ($this->acc * $wheel < 0) {            $this->acc = 0.0;        }        $this->acc += $wheel;        $n = (int) ($this->acc > 0 ? floor($this->acc + 1e-9) : ceil($this->acc - 1e-9));        $this->acc -= $n;        return -$n;    }    public function reset(): void    {        $this->acc = 0.0;    }    /** A first row clamped to [0, rows - visible]. */    public static function clamp(int $top, int $rows, int $visible): int    {        return max(0, min($top, max(0, $rows - $visible)));    }}