50 lines
1.1 KiB
PHP
50 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace NoccyLabs\Spinner;
|
|
|
|
|
|
class Spinner
|
|
{
|
|
private static $defaultStyle = Style\BrailleDotsStyle::class;
|
|
|
|
private static $defaultFps = 10;
|
|
|
|
private array $frames = [];
|
|
|
|
private int $frame = 0;
|
|
|
|
private float $lastAdvance = 0;
|
|
|
|
private float $advanceIv = 0;
|
|
|
|
public static function setDefaultStyle(string $class)
|
|
{
|
|
$inst = new $class;
|
|
if (!($inst instanceof Style\StyleInterface)) {
|
|
throw new \DomainException();
|
|
}
|
|
self::$defaultStyle = $class;
|
|
}
|
|
|
|
public function __construct(?string $style=null, ?int $fps=null)
|
|
{
|
|
$style = $style??self::$defaultStyle;
|
|
$fps = $fps??self::$defaultFps;
|
|
|
|
$this->advanceIv = 1.0 / $fps;
|
|
|
|
$style = new $style();
|
|
$this->frames = $style->getFrames();
|
|
}
|
|
|
|
public function __toString()
|
|
{
|
|
$now = microtime(true);
|
|
if ($this->lastAdvance + $this->advanceIv < $now) {
|
|
$this->frame = ($this->frame + 1) % count($this->frames);
|
|
$this->lastAdvance = $now;
|
|
}
|
|
return $this->frames[$this->frame];
|
|
}
|
|
}
|