Completed
Pull Request — master (#141)
by
unknown
02:37
created

uiButton::getFocusColor()   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
    }
143
144
    protected function getScriptMouseClick()
145
    {
146
        return /** language=textmate  prefix=#RequireContext\n */
147
            <<<'EOD'
148
            if (Event.Control.HasClass("uiButtonElement") ) {
149
                if (Event.Control.Parent.HasClass("uiButton")) {
150
                      Event.Control.Parent.RelativeScale = 0.75;
151
                      AnimMgr.Add(Event.Control.Parent, "<elem scale=\"1.\" />", 200, CAnimManager::EAnimManagerEasing::QuadIn); 
152
                      TriggerPageAction(Event.Control.Parent.DataAttributeGet("action"));
153
                }                
154
            }
155
EOD;
156
    }
157
158
    /**
159
     * @return string
160
     */
161
    public function getType()
162
    {
163
        return $this->type;
164
    }
165
166
    /**
167
     * @param string $type
168
     */
169
    public function setType($type)
170
    {
171
        $this->type = $type;
172
173
        return $this;
174
    }
175
176
    /**
177
     * @return string
178
     */
179
    public function getTextColor()
180
    {
181
        return $this->textColor;
182
    }
183
184
    /**
185
     * @param string $textColor
186
     */
187
    public function setTextColor($textColor)
188
    {
189
        $this->textColor = $textColor;
190
191
        return $this;
192
    }
193
194
    /**
195
     * @return string
196
     */
197
    public function getBackgroundColor()
198
    {
199
        return $this->backColor;
200
    }
201
202
    /**
203
     * @param string $backColor
204
     */
205
    public function setBackgroundColor($backColor)
206
    {
207
        $this->backColor = $backColor;
208
209
        return $this;
210
    }
211
212
    /**
213
     * @return string
214
     */
215
    public function getBorderColor()
216
    {
217
        return $this->borderColor;
218
    }
219
220
    /**
221
     * @param string $borderColor
222
     */
223
    public function setBorderColor($borderColor)
224
    {
225
        $this->borderColor = $borderColor;
226
227
        return $this;
228
    }
229
230
    /**
231
     * @return null
232
     */
233
    public function getAction()
234
    {
235
        return $this->action;
236
    }
237
238
    /**
239
     * @param null $action
240
     */
241
    public function setAction($action)
242
    {
243
        $this->action = $action;
244
245
        return $this;
246
    }
247
248
    /**
249
     * @return string
250
     */
251
    public function getFocusColor()
252
    {
253
        return $this->focusColor;
254
    }
255
256
    /**
257
     * @param string $focusColor
258
     */
259
    public function setFocusColor($focusColor)
260
    {
261
        $this->focusColor = $focusColor;
262
263
        return $this;
264
    }
265
266
    /**
267
     * @return float
268
     */
269
    public function getScale()
270
    {
271
        return $this->scale;
272
    }
273
274
    /**
275
     * @param float $scale
276
     */
277
    public function setScale($scale)
278
    {
279
        $this->scale = $scale;
280
281
        return $this;
282
    }
283
284
    /**
285
     * @return bool
286
     */
287
    public function getTranslate()
288
    {
289
        return $this->translate;
290
291
    }
292
293
    /**
294
     * @param bool $translate
295
     */
296
    public function setTranslate($translate = true)
297
    {
298
        if ($translate) {
299
            $this->buttonLabel->setTextId($this->getText());
300
        } else {
301
            $this->buttonLabel->setText($this->getText());
302
        }
303
304
        $this->buttonLabel->setTranslate($translate);
305
306
        return $this;
307
    }
308
309
    /**
310
     * Get the children
311
     *
312
     * @api
313
     * @return Renderable[]
314
     */
315
    public function getChildren()
316
    {
317
        return [$this->buttonLabel];
318
    }
319
320
    /**
321
     * Add a new child
322
     *
323
     * @api
324
     * @param Renderable $child Child Control to add
325
     * @deprecated
326
     * @return static
327
     */
328
    public function addChild(Renderable $child)
329
    {
330
        // TODO: Implement addChild() method.
331
    }
332
333
    /**
334
     * Add a new child
335
     *
336
     * @api
337
     * @param Renderable $child Child Control to add
338
     * @return static
339
     * @deprecated Use addChild()
340
     * @see        Container::addChild()
341
     */
342
    public function add(Renderable $child)
343
    {
344
        // TODO: Implement add() method.
345
    }
346
347
    /**
348
     * Add new children
349
     *
350
     * @api
351
     * @param Renderable[] $children Child Controls to add
352
     * @return static
353
     */
354
    public function addChildren(array $children)
355
    {
356
        // TODO: Implement addChildren() method.
357
    }
358
359
    /**
360
     * Remove all children
361
     *
362
     * @api
363
     * @return static
364
     */
365
    public function removeAllChildren()
366
    {
367
        // TODO: Implement removeAllChildren() method.
368
    }
369
370
    /**
371
     * Remove all children
372
     *
373
     * @api
374
     * @return static
375
     * @deprecated Use removeAllChildren()
376
     * @see        Container::removeAllChildren()
377
     */
378
    public function removeChildren()
379
    {
380
        // TODO: Implement removeChildren() method.
381
    }
382
383
    /**
384
     * Get the Format
385
     *
386
     * @api
387
     * @param bool $createIfEmpty If the format should be created if it doesn't exist yet
388
     * @return Format
389
     * @deprecated Use Style
390
     * @see        Style
391
     */
392
    public function getFormat($createIfEmpty = true)
393
    {
394
        // TODO: Implement getFormat() method.
395
    }
396
397
    /**
398
     * Set the Format
399
     *
400
     * @api
401
     * @param Format $format New Format
402
     * @return static
403
     * @deprecated Use Style
404
     * @see        Style
405
     */
406
    public function setFormat(Format $format = null)
407
    {
408
        // TODO: Implement setFormat() method.
409
    }
410
411
    /**
412
     * @return null
413
     */
414
    public function getId()
415
    {
416
        return $this->buttonLabel->getId();
417
    }
418
419
    /**
420
     * @param null $id
421
     */
422
    public function setId($id)
423
    {
424
        $this->buttonLabel->setId($id);
425
    }
426
}
427