|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\CLImate\TerminalObject\Dynamic\Animation; |
|
4
|
|
|
|
|
5
|
|
|
use League\CLImate\Decorator\Parser\ParserImporter; |
|
6
|
|
|
use League\CLImate\TerminalObject\Helper\StringLength; |
|
7
|
|
|
use League\CLImate\Util\UtilImporter; |
|
8
|
|
|
|
|
9
|
|
|
class Keyframe |
|
10
|
|
|
{ |
|
11
|
|
|
use StringLength, ParserImporter, UtilImporter; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Get the enter keyframes for the desired direction |
|
15
|
|
|
* |
|
16
|
|
|
* @param array $lines |
|
17
|
|
|
* @param string $direction |
|
18
|
|
|
* |
|
19
|
|
|
* @return array |
|
20
|
|
|
*/ |
|
21
|
40 |
|
public function enterFrom($lines, $direction) |
|
22
|
|
|
{ |
|
23
|
40 |
|
return array_reverse($this->exitTo($lines, $direction)); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get the exit keyframes for the desired direction |
|
28
|
|
|
* |
|
29
|
|
|
* @param array $lines |
|
30
|
|
|
* @param string $direction |
|
31
|
|
|
* |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
56 |
|
public function exitTo($lines, $direction) |
|
35
|
|
|
{ |
|
36
|
56 |
|
$lines = $this->adjustLines($lines, $direction); |
|
37
|
56 |
|
$line_method = $this->getLineMethod($direction); |
|
38
|
|
|
|
|
39
|
56 |
|
$direction_keyframes = $this->getDirectionFrames($direction, $lines, $line_method); |
|
40
|
|
|
|
|
41
|
56 |
|
$keyframes = array_fill(0, 4, $lines); |
|
42
|
56 |
|
$keyframes = array_merge($keyframes, $direction_keyframes); |
|
43
|
56 |
|
$keyframes[] = array_fill(0, count($lines), ''); |
|
44
|
|
|
|
|
45
|
56 |
|
return $keyframes; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get scroll keyframes |
|
50
|
|
|
* |
|
51
|
|
|
* @param array $lines |
|
52
|
|
|
* @param string $enter_from |
|
53
|
|
|
* @param string $exit_to |
|
54
|
|
|
* |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
20 |
|
public function scroll($lines, $enter_from, $exit_to) |
|
58
|
|
|
{ |
|
59
|
20 |
|
$keyframes = $this->enterFrom($lines, $enter_from); |
|
60
|
20 |
|
$keyframes = array_merge($keyframes, $this->exitTo($lines, $exit_to)); |
|
61
|
20 |
|
$keyframes = array_unique($keyframes, SORT_REGULAR); |
|
62
|
20 |
|
$keyframes[] = reset($keyframes); |
|
63
|
|
|
|
|
64
|
20 |
|
return $keyframes; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get the line parser for the direction |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $direction |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
56 |
|
protected function getLineMethod($direction) |
|
74
|
|
|
{ |
|
75
|
56 |
|
return 'current' . ucwords(strtolower($direction)) . 'Line'; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Adjust the array of lines if necessary |
|
80
|
|
|
* |
|
81
|
|
|
* @param array $lines |
|
82
|
|
|
* @param string $direction |
|
83
|
|
|
* |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
56 |
|
protected function adjustLines(array $lines, $direction) |
|
87
|
|
|
{ |
|
88
|
56 |
|
$adjust_method = 'adjust' . ucwords(strtolower($direction)) . 'Lines'; |
|
89
|
|
|
|
|
90
|
56 |
|
if (method_exists($this, $adjust_method)) { |
|
91
|
28 |
|
return $this->$adjust_method($lines); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
28 |
|
return $lines; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Pad the array of lines for "right" animation |
|
99
|
|
|
* |
|
100
|
|
|
* @param array $lines |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
20 |
|
protected function adjustRightLines(array $lines) |
|
104
|
|
|
{ |
|
105
|
20 |
|
return $this->padArray($lines, $this->util->width()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Pad the array of lines for "left" animation |
|
110
|
|
|
* |
|
111
|
|
|
* @param array $lines |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
20 |
|
protected function adjustLeftLines(array $lines) |
|
115
|
|
|
{ |
|
116
|
20 |
|
return $this->padArray($lines, $this->maxStrLen($lines)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Get the keyframes appropriate for the animation direction |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $direction |
|
123
|
|
|
* @param array $lines |
|
124
|
|
|
* @param string $line_method |
|
125
|
|
|
* |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
56 |
|
protected function getDirectionFrames($direction, array $lines, $line_method) |
|
129
|
|
|
{ |
|
130
|
|
|
$mapping = [ |
|
131
|
56 |
|
'exitHorizontalFrames' => ['left', 'right'], |
|
132
|
|
|
'exitVerticalFrames' => ['top', 'bottom'], |
|
133
|
|
|
]; |
|
134
|
|
|
|
|
135
|
56 |
|
foreach ($mapping as $method => $directions) { |
|
136
|
56 |
|
if (in_array($direction, $directions)) { |
|
137
|
56 |
|
return $this->$method($lines, $line_method); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
// Fail gracefully, simply return an array |
|
142
|
|
|
return []; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Create horizontal exit animation keyframes for the art |
|
147
|
|
|
* |
|
148
|
|
|
* @param array $lines |
|
149
|
|
|
* @param string $line_method |
|
150
|
|
|
* |
|
151
|
|
|
* @return array |
|
152
|
|
|
*/ |
|
153
|
28 |
|
protected function exitHorizontalFrames(array $lines, $line_method) |
|
154
|
|
|
{ |
|
155
|
28 |
|
$keyframes = []; |
|
156
|
28 |
|
$length = mb_strlen($lines[0]); |
|
157
|
|
|
|
|
158
|
28 |
|
for ($i = $length; $i > 0; $i--) { |
|
159
|
28 |
|
$keyframes[] = $this->getHorizontalKeyframe($lines, $i, $line_method, $length); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
28 |
|
return $keyframes; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Get the keyframe for a horizontal animation |
|
167
|
|
|
* |
|
168
|
|
|
* @param array $lines |
|
169
|
|
|
* @param int $frame_number |
|
170
|
|
|
* @param string $line_method |
|
171
|
|
|
* @param int $length |
|
172
|
|
|
* |
|
173
|
|
|
* @return array |
|
174
|
|
|
*/ |
|
175
|
28 |
|
protected function getHorizontalKeyframe(array $lines, $frame_number, $line_method, $length) |
|
176
|
|
|
{ |
|
177
|
28 |
|
$keyframe = []; |
|
178
|
|
|
|
|
179
|
28 |
|
foreach ($lines as $line) { |
|
180
|
28 |
|
$keyframe[] = $this->$line_method($line, $frame_number, $length); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
28 |
|
return $keyframe; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Create vertical exit animation keyframes for the art |
|
188
|
|
|
* |
|
189
|
|
|
* @param array $lines |
|
190
|
|
|
* @param string $line_method |
|
191
|
|
|
* |
|
192
|
|
|
* @return array |
|
193
|
|
|
*/ |
|
194
|
28 |
|
protected function exitVerticalFrames(array $lines, $line_method) |
|
195
|
|
|
{ |
|
196
|
28 |
|
$keyframes = []; |
|
197
|
28 |
|
$line_count = count($lines); |
|
198
|
|
|
|
|
199
|
28 |
|
for ($i = $line_count - 1; $i >= 0; $i--) { |
|
200
|
28 |
|
$keyframes[] = $this->$line_method($lines, $line_count, $i); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
28 |
|
return $keyframes; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Get the current line as it is exiting left |
|
208
|
|
|
* |
|
209
|
|
|
* @param string $line |
|
210
|
|
|
* @param int $frame_number |
|
211
|
|
|
* |
|
212
|
|
|
* @return string |
|
213
|
|
|
*/ |
|
214
|
20 |
|
protected function currentLeftLine($line, $frame_number) |
|
215
|
|
|
{ |
|
216
|
20 |
|
return mb_substr($line, -$frame_number); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Get the current line as it is exiting right |
|
222
|
|
|
* |
|
223
|
|
|
* @param string $line |
|
224
|
|
|
* @param int $frame_number |
|
225
|
|
|
* @param int $length |
|
226
|
|
|
* |
|
227
|
|
|
* @return string |
|
228
|
|
|
*/ |
|
229
|
20 |
|
protected function currentRightLine($line, $frame_number, $length) |
|
230
|
|
|
{ |
|
231
|
20 |
|
return str_repeat(' ', $length - $frame_number) . mb_substr($line, 0, $frame_number); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Slice off X number of lines from the bottom and fill the rest with empty strings |
|
236
|
|
|
* |
|
237
|
|
|
* @param array $lines |
|
238
|
|
|
* @param integer $total_lines |
|
239
|
|
|
* @param integer $current |
|
240
|
|
|
* |
|
241
|
|
|
* @return array |
|
242
|
|
|
*/ |
|
243
|
16 |
|
protected function currentTopLine($lines, $total_lines, $current) |
|
244
|
|
|
{ |
|
245
|
16 |
|
$keyframe = array_slice($lines, -$current, $current); |
|
246
|
|
|
|
|
247
|
16 |
|
return array_merge($keyframe, array_fill(0, $total_lines - $current, '')); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Slice off X number of lines from the top and fill the rest with empty strings |
|
252
|
|
|
* |
|
253
|
|
|
* @param array $lines |
|
254
|
|
|
* @param integer $total_lines |
|
255
|
|
|
* @param integer $current |
|
256
|
|
|
* |
|
257
|
|
|
* @return array |
|
258
|
|
|
*/ |
|
259
|
20 |
|
protected function currentBottomLine($lines, $total_lines, $current) |
|
260
|
|
|
{ |
|
261
|
20 |
|
$keyframe = array_fill(0, $total_lines - $current, ''); |
|
262
|
|
|
|
|
263
|
20 |
|
return array_merge($keyframe, array_slice($lines, 0, $current)); |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|