1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace eXpansion\Framework\Gui\Layouts; |
4
|
|
|
|
5
|
|
|
use FML\Controls\Control; |
6
|
|
|
use FML\Controls\Frame; |
7
|
|
|
use FML\Elements\Format; |
8
|
|
|
use FML\Script\Features\ScriptFeature; |
9
|
|
|
use FML\Types\Container; |
10
|
|
|
use FML\Types\Renderable; |
11
|
|
|
use FML\Types\ScriptFeatureable; |
12
|
|
|
|
13
|
|
|
class LayoutLine implements Renderable, ScriptFeatureable, Container |
14
|
|
|
{ |
15
|
|
|
protected $frameClasses = []; |
16
|
|
|
|
17
|
|
|
protected $frameId = null; |
18
|
|
|
|
19
|
|
|
/** @var float */ |
20
|
|
|
protected $width = 0.; |
21
|
|
|
|
22
|
|
|
/** @var float */ |
23
|
|
|
protected $height = 0.; |
24
|
|
|
|
25
|
|
|
/** @var Control[] */ |
26
|
|
|
protected $elements = []; |
27
|
|
|
|
28
|
|
|
/** @var float */ |
29
|
|
|
private $margin = 2.; |
30
|
|
|
/** |
31
|
|
|
* @var float |
32
|
|
|
*/ |
33
|
|
|
protected $startX = 0.; |
34
|
|
|
/** |
35
|
|
|
* @var float |
36
|
|
|
*/ |
37
|
|
|
protected $startY = 0.; |
38
|
|
|
|
39
|
|
|
/** @var string */ |
40
|
|
|
protected $hAlign = "left"; |
41
|
|
|
|
42
|
|
|
/** @var string */ |
43
|
|
|
protected $vAlign = "top"; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* layoutLine constructor. |
47
|
|
|
* @param float $startX |
48
|
|
|
* @param float $startY |
49
|
|
|
* @param object[] $elements |
50
|
|
|
* @param float $margin |
51
|
|
|
* @throws \Exception |
52
|
|
|
*/ |
53
|
|
|
public function __construct($startX, $startY, $elements = [], $margin = 0.) |
54
|
|
|
{ |
55
|
|
|
if (!is_array($elements)) { |
56
|
|
|
throw new \Exception('not an array'); |
57
|
|
|
} |
58
|
|
|
$this->margin = $margin; |
59
|
|
|
$this->elements = $elements; |
60
|
|
|
$this->startX = $startX; |
61
|
|
|
$this->startY = $startY; |
62
|
|
|
$sizeY = 0; |
63
|
|
View Code Duplication |
foreach ($this->elements as $idx => $element) { |
|
|
|
|
64
|
|
|
$this->width += $element->getWidth() + $this->margin; |
65
|
|
|
if ((abs($element->getY()) + $element->getHeight()) > $sizeY) { |
66
|
|
|
$sizeY = abs($element->getY()) + $element->getHeight(); |
67
|
|
|
$this->setHeight($sizeY); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Render the XML element |
75
|
|
|
* |
76
|
|
|
* @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered |
77
|
|
|
* @return \DOMElement |
78
|
|
|
*/ |
79
|
|
|
public function render(\DOMDocument $domDocument) |
80
|
|
|
{ |
81
|
|
|
$frame = new Frame(); |
82
|
|
|
$frame->setId($this->frameId); |
83
|
|
|
$frame->setAlign($this->hAlign, $this->vAlign); |
84
|
|
|
$frame->setPosition($this->startX, $this->startY); |
85
|
|
|
$frame->addClasses($this->frameClasses); |
86
|
|
|
$sizeY = 0; |
87
|
|
|
$sizeX = 0; |
88
|
|
|
/** @var Control $oldElement */ |
89
|
|
|
$oldElement = null; |
90
|
|
|
foreach ($this->elements as $idx => $element) { |
91
|
|
|
|
92
|
|
|
if ($idx === 0) { |
93
|
|
|
$start = $this->getRelativeStartPosition($element); |
94
|
|
|
} else { |
95
|
|
|
$start = $this->getStartPosition($oldElement) + $this->margin; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($oldElement) { |
99
|
|
|
|
100
|
|
|
if ($oldElement->getHorizontalAlign() == "center" && $element->getHorizontalAlign() == "center") { |
101
|
|
|
$element->setX($start + $oldElement->getWidth() + ($element->getWidth() / 2)); |
102
|
|
View Code Duplication |
} elseif ($oldElement->getHorizontalAlign() == "left" && $element->getHorizontalAlign() == "center") { |
|
|
|
|
103
|
|
|
$element->setX($start + $oldElement->getWidth() + ($element->getWidth() / 2)); |
104
|
|
|
} elseif ($oldElement->getHorizontalAlign() == "center" && $element->getHorizontalAlign() == "left") { |
105
|
|
|
$element->setX($start + $oldElement->getWidth()); |
106
|
|
View Code Duplication |
} elseif ($oldElement->getHorizontalAlign() == "center" && $element->getHorizontalAlign() == "right") { |
|
|
|
|
107
|
|
|
$element->setX($start + $oldElement->getWidth() + $element->getWidth()); |
108
|
|
|
} elseif ($oldElement->getHorizontalAlign() == "right" && $element->getHorizontalAlign() == "right") { |
109
|
|
|
$element->setX($start + $element->getWidth()); |
110
|
|
View Code Duplication |
} elseif ($oldElement->getHorizontalAlign() == "right" && $element->getHorizontalAlign() == "center") { |
|
|
|
|
111
|
|
|
$element->setX($start + ($element->getWidth() / 2)); |
112
|
|
|
} elseif ($oldElement->getHorizontalAlign() == "left" && $element->getHorizontalAlign() == "right") { |
113
|
|
|
$element->setX($start + $oldElement->getWidth() + $element->getWidth()); |
114
|
|
|
} elseif ($oldElement->getHorizontalAlign() == "right" && $element->getHorizontalAlign() == "left") { |
115
|
|
|
$element->setX($start); |
116
|
|
|
} else { |
117
|
|
|
$element->setX($start + $oldElement->getWidth()); |
118
|
|
|
} |
119
|
|
|
} else { |
120
|
|
|
$element->setX($start); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ((abs($element->getY()) + $element->getHeight()) > $sizeY) { |
124
|
|
|
$sizeY = abs($element->getY()) + $element->getHeight(); |
125
|
|
|
$this->setHeight($sizeY); |
126
|
|
|
} |
127
|
|
|
$frame->addChild($element); |
128
|
|
|
$oldElement = $element; |
129
|
|
|
$sizeX += $element->getWidth() + $this->margin; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$this->setWidth($sizeX); |
133
|
|
|
|
134
|
|
|
return $frame->render($domDocument); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param Control $element |
139
|
|
|
* @return double |
140
|
|
|
*/ |
141
|
|
|
private function getRelativeStartPosition($element) |
142
|
|
|
{ |
143
|
|
|
if (is_null($element)) { |
144
|
|
|
return 0; |
145
|
|
|
} |
146
|
|
|
switch ($element->getHorizontalAlign()) { |
147
|
|
|
case "left": |
148
|
|
|
return 0; |
149
|
|
|
case "center": |
150
|
|
|
return 0.5 * $element->getWidth(); |
151
|
|
|
case "right": |
152
|
|
|
return $element->getWidth(); |
153
|
|
|
default: |
154
|
|
|
return 0; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param Control $element |
160
|
|
|
* @return float|int |
161
|
|
|
*/ |
162
|
|
|
private function getStartPosition($element) |
163
|
|
|
{ |
164
|
|
|
if (is_null($element)) { |
165
|
|
|
return 0; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
switch ($element->getHorizontalAlign()) { |
169
|
|
|
case "center": |
170
|
|
|
return $element->getX() - (0.5 * $element->getWidth()); |
171
|
|
|
default: |
172
|
|
|
return $element->getX(); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get the Script Features |
178
|
|
|
* |
179
|
|
|
* @return ScriptFeature[] |
180
|
|
|
*/ |
181
|
|
View Code Duplication |
public function getScriptFeatures() |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
$features = []; |
184
|
|
|
foreach ($this->elements as $element) { |
185
|
|
|
if ($element instanceof ScriptFeatureable) { |
186
|
|
|
$features[] = $element->getScriptFeatures(); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return ScriptFeature::collect($features); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param mixed $startX |
195
|
|
|
* @return LayoutLine |
196
|
|
|
*/ |
197
|
|
|
public function setX($startX) |
198
|
|
|
{ |
199
|
|
|
$this->startX = $startX; |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param mixed $startY |
206
|
|
|
* @return LayoutLine |
207
|
|
|
*/ |
208
|
|
|
public function setY($startY) |
209
|
|
|
{ |
210
|
|
|
$this->startY = $startY; |
211
|
|
|
|
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return float |
217
|
|
|
*/ |
218
|
|
|
public function getX() |
219
|
|
|
{ |
220
|
|
|
return $this->startX; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return double |
225
|
|
|
*/ |
226
|
|
|
public function getY() |
227
|
|
|
{ |
228
|
|
|
return $this->startY; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @return float |
233
|
|
|
*/ |
234
|
|
|
public function getWidth() |
235
|
|
|
{ |
236
|
|
|
return $this->width; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param float $width |
241
|
|
|
*/ |
242
|
|
|
public function setWidth($width) |
243
|
|
|
{ |
244
|
|
|
$this->width = $width; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return float |
249
|
|
|
*/ |
250
|
|
|
public function getHeight() |
251
|
|
|
{ |
252
|
|
|
return $this->height; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param float $height |
257
|
|
|
*/ |
258
|
|
|
public function setHeight($height) |
259
|
|
|
{ |
260
|
|
|
$this->height = $height; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @param Renderable $element |
265
|
|
|
*/ |
266
|
|
|
public function addChild(Renderable $element) |
267
|
|
|
{ |
268
|
|
|
$this->elements[] = $element; |
269
|
|
|
$this->width += $element->getWidth() + $this->margin; |
|
|
|
|
270
|
|
|
if ((abs($element->getY()) + $element->getHeight()) > $this->getHeight()) { |
|
|
|
|
271
|
|
|
$this->setHeight(abs($element->getY()) + $element->getHeight()); |
|
|
|
|
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function getChildren() |
276
|
|
|
{ |
277
|
|
|
return $this->elements; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
|
281
|
|
|
public function addClass($class) |
282
|
|
|
{ |
283
|
|
|
$this->frameClasses [] = $class; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Add a new child |
288
|
|
|
* |
289
|
|
|
* @api |
290
|
|
|
* @param Renderable $child Child Control to add |
291
|
|
|
* @return void |
292
|
|
|
* @deprecated Use addChild() |
293
|
|
|
* @see Container::addChild() |
294
|
|
|
*/ |
295
|
|
|
public function add(Renderable $child) |
296
|
|
|
{ |
297
|
|
|
// do nothing |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Add new children |
302
|
|
|
* |
303
|
|
|
* @api |
304
|
|
|
* @param Renderable[] $children Child Controls to add |
305
|
|
|
* @return void |
306
|
|
|
*/ |
307
|
|
|
public function addChildren(array $children) |
308
|
|
|
{ |
309
|
|
|
foreach ($children as $child) { |
310
|
|
|
$this->addChild($child); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Remove all children |
316
|
|
|
* |
317
|
|
|
* @api |
318
|
|
|
* @return static |
319
|
|
|
*/ |
320
|
|
|
public function removeAllChildren() |
321
|
|
|
{ |
322
|
|
|
$this->width = 0; |
|
|
|
|
323
|
|
|
$this->height = 0; |
|
|
|
|
324
|
|
|
$this->elements = []; |
325
|
|
|
|
326
|
|
|
return $this; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Remove all children |
331
|
|
|
* |
332
|
|
|
* @api |
333
|
|
|
* @return void |
334
|
|
|
* @deprecated Use removeAllChildren() |
335
|
|
|
* @see Container::removeAllChildren() |
336
|
|
|
*/ |
337
|
|
|
public function removeChildren() |
338
|
|
|
{ |
339
|
|
|
// do nothing |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Get the Format |
344
|
|
|
* |
345
|
|
|
* @api |
346
|
|
|
* @param bool $createIfEmpty If the format should be created if it doesn't exist yet |
347
|
|
|
* @return void |
348
|
|
|
* @deprecated Use Style |
349
|
|
|
* @see Style |
350
|
|
|
*/ |
351
|
|
|
public function getFormat($createIfEmpty = true) |
352
|
|
|
{ |
353
|
|
|
|
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Set the Format |
358
|
|
|
* |
359
|
|
|
* @api |
360
|
|
|
* @param Format $format New Format |
361
|
|
|
* @return void |
362
|
|
|
* @deprecated Use Style |
363
|
|
|
* @see Style |
364
|
|
|
*/ |
365
|
|
|
public function setFormat(Format $format = null) |
366
|
|
|
{ |
367
|
|
|
|
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
public function setAlign($hAling = "left", $vAlign = "top") |
371
|
|
|
{ |
372
|
|
|
$this->hAlign = $hAling; |
373
|
|
|
$this->vAlign = $vAlign; |
374
|
|
|
|
375
|
|
|
return $this; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
|
379
|
|
|
public function setPosition($x, $y) |
380
|
|
|
{ |
381
|
|
|
$this->setX($x); |
382
|
|
|
$this->setY($y); |
383
|
|
|
|
384
|
|
|
return $this; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* @return null|string |
389
|
|
|
*/ |
390
|
|
|
public function getId() |
391
|
|
|
{ |
392
|
|
|
return $this->frameId; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* @param null|string $frameId |
397
|
|
|
* @return LayoutLine |
398
|
|
|
*/ |
399
|
|
|
public function setId($frameId) |
400
|
|
|
{ |
401
|
|
|
$this->frameId = $frameId; |
402
|
|
|
|
403
|
|
|
return $this; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* @return string |
408
|
|
|
*/ |
409
|
|
|
public function getHorizontalAlign(): string |
410
|
|
|
{ |
411
|
|
|
return $this->hAlign; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* @param string $hAlign |
416
|
|
|
* @return LayoutLine |
417
|
|
|
*/ |
418
|
|
|
public function setHorizontalAlign(string $hAlign) |
419
|
|
|
{ |
420
|
|
|
$this->hAlign = $hAlign; |
421
|
|
|
|
422
|
|
|
return $this; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
} |
426
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.