Completed
Pull Request — master (#127)
by
unknown
02:42
created

uiButton::removeChildren()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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