1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AlecRabbit\Spinner\Core\Driver; |
6
|
|
|
|
7
|
|
|
use AlecRabbit\Spinner\Contract\IDeltaTimer; |
8
|
|
|
use AlecRabbit\Spinner\Core\Builder\Contract\ISequenceStateBuilder; |
9
|
|
|
use AlecRabbit\Spinner\Core\Contract\IRenderer; |
10
|
|
|
use AlecRabbit\Spinner\Core\Contract\ISequenceState; |
11
|
|
|
use AlecRabbit\Spinner\Core\Contract\ISpinner; |
12
|
|
|
use AlecRabbit\Spinner\Core\Output\Contract\ISequenceStateWriter; |
13
|
|
|
|
14
|
|
|
final class Renderer implements IRenderer |
15
|
|
|
{ |
16
|
|
|
private ISequenceState $state; |
17
|
|
|
|
18
|
|
|
public function __construct( |
19
|
|
|
private readonly ISequenceStateWriter $stateWriter, |
20
|
|
|
private readonly ISequenceStateBuilder $stateBuilder, |
21
|
|
|
private readonly IDeltaTimer $deltaTimer, |
22
|
|
|
) { |
23
|
|
|
$this->state = $this->createState(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
private function createState( |
27
|
|
|
string $sequence = '', |
28
|
|
|
int $width = 0, |
29
|
|
|
int $previousWidth = 0 |
30
|
|
|
): ISequenceState { |
31
|
|
|
return $this->stateBuilder |
32
|
|
|
->withSequence($sequence) |
33
|
|
|
->withWidth($width) |
34
|
|
|
->withPreviousWidth($previousWidth) |
35
|
|
|
->build() |
36
|
|
|
; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function initialize(): void |
40
|
|
|
{ |
41
|
|
|
$this->stateWriter->initialize(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function render(ISpinner $spinner, ?float $dt = null): void |
45
|
|
|
{ |
46
|
|
|
$frame = |
47
|
|
|
$spinner->getFrame( |
48
|
|
|
$dt ?? $this->deltaTimer->getDelta() |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$this->state = $this->createState( |
52
|
|
|
$frame->getSequence(), |
53
|
|
|
$frame->getWidth(), |
54
|
|
|
$this->state->getWidth(), |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
$this->stateWriter->write($this->state); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function finalize(?string $finalMessage = null): void |
61
|
|
|
{ |
62
|
|
|
$this->stateWriter->finalize($finalMessage); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function erase(ISpinner $spinner): void |
66
|
|
|
{ |
67
|
|
|
$this->stateWriter->erase($this->state); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|