Button::setTextColor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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