Completed
Push — master ( 206125...47c10a )
by De Cramer
13s
created

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