Completed
Pull Request — master (#161)
by
unknown
02:51
created

uiButton::getScriptFeatures()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace eXpansion\Framework\Gui\Components;
4
5
use FML\Controls\Frame;
6
use FML\Controls\Quad;
7
use FML\Elements\Format;
8
use FML\Script\Features\ScriptFeature;
9
use FML\Script\Script;
10
use FML\Script\ScriptLabel;
11
use FML\Types\Container;
12
use FML\Types\Renderable;
13
use FML\Types\ScriptFeatureable;
14
15
class uiButton extends abstractUiElement implements ScriptFeatureable, Container
16
{
17
    const TYPE_DECORATED = "decorated";
18
    const TYPE_DEFAULT = "default";
19
    const COLOR_DEFAULT = "aaa";
20
    const COLOR_SUCCESS = "0d0";
21
    const COLOR_WARNING = "d00";
22
    const COLOR_PRIMARY = "3af";
23
    const COLOR_SECONDARY = "000";
24
    /** @var  uiLabel */
25
    protected $buttonLabel;
26
    protected $type;
27
    protected $textColor = "eee";
28
    protected $backColor = self::COLOR_DEFAULT;
29
    protected $focusColor = "bbb";
30
    protected $borderColor = "fff";
31
    protected $translate = false;
32
33
    protected $action = null;
34
    protected $text = "button";
35
    protected $scale = 1.;
36
37
    public function __construct($text = "button", $type = self::TYPE_DEFAULT)
38
    {
39
        $this->setHorizontalAlign("center");
40
        $this->setVerticalAlign("center");
41
42
        $this->text = $text;
43
        $this->type = $type;
44
        $this->setSize(18, 5);
45
        $this->buttonLabel = new uiLabel("", uiLabel::TYPE_TITLE);
46
        $this->buttonLabel->addClass('uiButtonElement');
47
    }
48
49
50
    /**
51
     * Render the XML element
52
     *
53
     * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered
54
     * @return \DOMElement
55
     *
56
     * <frame pos="64 -35" class="uiContainer uiButton">
57
     * <label size="26 9" data-color="fff" text="Cancel" class="button noAnim"  textprefix=" " opacity="1" halign="center" valign="center" focusareacolor1="0000" focusareacolor2="d00" scriptevents="1" translate="0" textsize="2"/>
58
     * <quad size="26 9" style="Bgs1" colorize="d00" substyle="BgColorContour" class="button" halign="center" valign="center" pos="0 0"/>
59
     * </frame>
60
     */
61
    public function render(\DOMDocument $domDocument)
62
    {
63
        $buttonFrame = new Frame();
64
        $buttonFrame->setAlign("center", "center")
65
            ->setPosition($this->posX + ($this->width / 2), $this->posY - ($this->height / 2), $this->posZ)
66
            ->addClasses(['uiContainer', 'uiButton'])
67
            ->addDataAttribute("action", $this->action)
68
            ->setScale($this->scale);
69
70
        foreach ($this->_dataAttributes as $name => $value) {
71
            $buttonFrame->addDataAttribute($name, $value);
72
        }
73
74
        if ($this->type == self::TYPE_DECORATED) {
75
            $quad = new Quad();
76
            $this->backColor = 0000;
77
            $quad->setStyles("Bgs1", "BgColorContour")
78
                ->setColorize($this->borderColor)
79
                ->setSize($this->width, $this->height)
80
                //->setPosition(-$this->width / 2, $this->height / 2)
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
81
                ->setAlign("center", "center2");
82
            $buttonFrame->addChild($quad);
83
        }
84
85
        $this->buttonLabel->setSize($this->width, $this->height)
86
            ->setText($this->getText())
87
            ->setTextSize(1)
88
            ->setScriptEvents(true)
89
            ->setAreaColor($this->backColor)
90
            ->setAreaFocusColor($this->focusColor)
91
            ->setTextColor($this->textColor)
92
93
            ->setAlign("center", "center2");
94
95
        if ($this->translate) {
96
            $this->buttonLabel->setTextId($this->getText());
97
        }
98
99
        $this->buttonLabel->setDataAttributes($this->_dataAttributes);
100
        $this->buttonLabel->addClasses($this->_classes);
101
102
        $buttonFrame->addChild($this->buttonLabel);
103
104
105
        return $buttonFrame->render($domDocument);
106
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getText()
113
    {
114
        return $this->text;
115
    }
116
117
    /**
118
     * @param string $text
119
     * @return uiButton
120
     */
121
    public function setText($text)
122
    {
123
        $this->text = $text;
124
125
        return $this;
126
    }
127
128
    /**
129
     * Get the Script Features
130
     *
131
     * @return ScriptFeature[]
132
     */
133
    public function getScriptFeatures()
134
    {
135
        return ScriptFeature::collect($this);
136
    }
137
138
    /**
139
     * Prepare the given Script for rendering by adding the needed Labels, etc.
140
     *
141
     * @param Script $script Script to prepar
142
     * @return void
143
     */
144
    public function prepare(Script $script)
145
    {
146
        $script->addCustomScriptLabel(ScriptLabel::MouseClick, $this->getScriptMouseClick());
147
        $script->addScriptFunction("", $this->getScriptFunction());
148
    }
149
150
    protected function getScriptMouseClick()
151
    {
152
        return /** language=textmate  prefix=#RequireContext\n */
153
            <<<'EOD'
154
            if (Event.Control.HasClass("uiButtonElement") ) {            
155
                TriggerButtonClick(Event.Control);                             
156
            }
157
EOD;
158
    }
159
160
    protected function getScriptFunction()
161
    {
162
        return /** language=textmate  prefix=#RequireContext\n */
163
            <<<'EOD'
164
       
165
            Void TriggerButtonClick(CMlControl Control) {                
166
                 if (Control.Parent.HasClass("uiButton")) {
167
                    Control.Parent.RelativeScale = 0.75;
168
                    AnimMgr.Add(Control.Parent, "<elem scale=\"1.\" />", 200, CAnimManager::EAnimManagerEasing::QuadIn); 
169
                    TriggerPageAction(Control.Parent.DataAttributeGet("action"));
170
                 }                                
171
            }
172
            
173
            Void TriggerButtonClick(Text ControlId) {
174
                declare Control <=> Page.GetFirstChild(ControlId);
175
                TriggerButtonClick(Control);
176
            }
177
178
179
EOD;
180
    }
181
182
183
    /**
184
     * @return string
185
     */
186
    public function getType()
187
    {
188
        return $this->type;
189
    }
190
191
    /**
192
     * @param string $type
193
     * @return uiButton
194
     */
195
    public function setType($type)
196
    {
197
        $this->type = $type;
198
199
        return $this;
200
    }
201
202
    /**
203
     * @return string
204
     */
205
    public function getTextColor()
206
    {
207
        return $this->textColor;
208
    }
209
210
    /**
211
     * @param string $textColor
212
     * @return uiButton
213
     */
214
    public function setTextColor($textColor)
215
    {
216
        $this->textColor = $textColor;
217
218
        return $this;
219
    }
220
221
    /**
222
     * @return string
223
     */
224
    public function getBackgroundColor()
225
    {
226
        return $this->backColor;
227
    }
228
229
    /**
230
     * @param string $backColor
231
     * @return uiButton
232
     */
233
    public function setBackgroundColor($backColor)
234
    {
235
        $this->backColor = $backColor;
236
237
        return $this;
238
    }
239
240
    /**
241
     * @return string
242
     */
243
    public function getBorderColor()
244
    {
245
        return $this->borderColor;
246
    }
247
248
    /**
249
     * @param string $borderColor
250
     * @return uiButton
251
     */
252
    public function setBorderColor($borderColor)
253
    {
254
        $this->borderColor = $borderColor;
255
256
        return $this;
257
    }
258
259
    /**
260
     * @return null
261
     */
262
    public function getAction()
263
    {
264
        return $this->action;
265
    }
266
267
    /**
268
     * @param null $action
269
     * @return uiButton
270
     */
271
    public function setAction($action)
272
    {
273
        $this->action = $action;
274
275
        return $this;
276
    }
277
278
    /**
279
     * @return string
280
     */
281
    public function getFocusColor()
282
    {
283
        return $this->focusColor;
284
    }
285
286
    /**
287
     * @param string $focusColor
288
     * @return uiButton
289
     */
290
    public function setFocusColor($focusColor)
291
    {
292
        $this->focusColor = $focusColor;
293
294
        return $this;
295
    }
296
297
    /**
298
     * @return float
299
     */
300
    public function getScale()
301
    {
302
        return $this->scale;
303
    }
304
305
    /**
306
     * @param float $scale
307
     * @return uiButton
308
     */
309
    public function setScale($scale)
310
    {
311
        $this->scale = $scale;
312
313
        return $this;
314
    }
315
316
    /**
317
     * @return bool
318
     */
319
    public function getTranslate()
320
    {
321
        return $this->translate;
322
323
    }
324
325
    /**
326
     * @param bool $translate
327
     * @return uiButton
328
     */
329
    public function setTranslate($translate = true)
330
    {
331
        if ($translate) {
332
            $this->buttonLabel->setTextId($this->getText());
333
        } else {
334
            $this->buttonLabel->setText($this->getText());
335
        }
336
337
        $this->buttonLabel->setTranslate($translate);
338
339
        return $this;
340
    }
341
342
    /**
343
     * Get the children
344
     *
345
     * @api
346
     * @return Renderable[]
347
     */
348
    public function getChildren()
349
    {
350
        return [$this->buttonLabel];
351
    }
352
353
    /**
354
     * Add a new child
355
     *
356
     * @api
357
     * @param Renderable $child Child Control to add
358
     * @deprecated
359
     * @return void
360
     */
361
    public function addChild(Renderable $child)
362
    {
363
364
    }
365
366
    /**
367
     * Add a new child
368
     *
369
     * @api
370
     * @param Renderable $child Child Control to add
371
     * @return void
372
     * @deprecated Use addChild()
373
     * @see        Container::addChild()
374
     */
375
    public function add(Renderable $child)
376
    {
377
378
    }
379
380
    /**
381
     * Add new children
382
     *
383
     * @api
384
     * @param Renderable[] $children Child Controls to add
385
     * @return void
386
     */
387
    public function addChildren(array $children)
388
    {
389
390
    }
391
392
    /**
393
     * Remove all children
394
     *
395
     * @api
396
     * @return void
397
     */
398
    public function removeAllChildren()
399
    {
400
401
    }
402
403
    /**
404
     * Remove all children
405
     *
406
     * @api
407
     * @return void
408
     * @deprecated Use removeAllChildren()
409
     * @see        Container::removeAllChildren()
410
     */
411
    public function removeChildren()
412
    {
413
414
    }
415
416
    /**
417
     * Get the Format
418
     *
419
     * @api
420
     * @param bool $createIfEmpty If the format should be created if it doesn't exist yet
421
     * @return void
422
     * @deprecated Use Style
423
     * @see        Style
424
     */
425
    public function getFormat($createIfEmpty = true)
426
    {
427
428
    }
429
430
    /**
431
     * Set the Format
432
     *
433
     * @api
434
     * @param Format $format New Format
435
     * @return void
436
     * @deprecated Use Style
437
     * @see        Style
438
     */
439
    public function setFormat(Format $format = null)
440
    {
441
442
    }
443
444
    /**
445
     * @return string|null
446
     */
447
    public function getId()
448
    {
449
        return $this->buttonLabel->getId();
450
    }
451
452
    /**
453
     * @param null $id
454
     * @return uiButton
455
     */
456
    public function setId($id)
457
    {
458
        $this->buttonLabel->setId($id);
459
460
        return $this;
461
    }
462
}
463