Completed
Pull Request — master (#95)
by
unknown
03:20
created

uiButton::getHeight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 4
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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\Script\Features\ScriptFeature;
9
use FML\Script\Script;
10
use FML\Script\ScriptLabel;
11
use FML\Types\Renderable;
12
use FML\Types\ScriptFeatureable;
13
14
class uiButton extends abstractUiElement implements ScriptFeatureable
15
{
16
    private $type;
17
18
    private $textColor = "eee";
19
    private $backColor = self::COLOR_DEFAULT;
20
    private $focusColor = "bbb";
21
    private $borderColor = "fff";
22
23
    private $action = null;
24
    private $text = "button";
25
26
    const TYPE_DECORATED = "decorated";
27
    const TYPE_DEFAULT = "default";
28
29
    const COLOR_DEFAULT = "aaa";
30
    const COLOR_SUCCESS = "0d0";
31
    const COLOR_WARNING = "d00";
32
    const COLOR_PRIMARY = "3af";
33
    const COLOR_SECONDARY = "000";
34
35
    public function __construct($text = "button", $type = self::TYPE_DEFAULT)
36
    {
37
        $this->text = $text;
38
        $this->type = $type;
39
        $this->setSize(26, 8);
40
    }
41
42
43
    /**
44
     * Render the XML element
45
     *
46
     * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered
47
     * @return \DOMElement
48
     *
49
     * <frame pos="64 -35" class="uiContainer uiButton">
50
     * <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"/>
51
     * <quad size="26 9" style="Bgs1" colorize="d00" substyle="BgColorContour" class="button" halign="center" valign="center" pos="0 0"/>
52
     * </frame>
53
     */
54
    public function render(\DOMDocument $domDocument)
55
    {
56
        $buttonFrame = new Frame();
57
        $buttonFrame->setAlign("center", "center")
58
            ->setPosition($this->posX + ($this->width / 2), $this->posY - ($this->height / 2), $this->posZ)
59
            ->addClasses(['uiContainer', 'uiButton'])
60
            ->addDataAttribute("action", $this->action);
61
62
        if ($this->type == self::TYPE_DECORATED) {
63
            $quad = new Quad();
64
            $this->backColor = null;
65
            $quad->setStyles("Bgs1", "BgColorContour")
66
                ->setColorize($this->borderColor)
67
                ->setSize($this->width, $this->height)
68
                //->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...
69
                ->setAlign("center", "center2");
70
            $buttonFrame->addChild($quad);
71
        }
72
73
        $label = new uiLabel($this->getText(), uiLabel::TYPE_TITLE);
74
        $label->setSize($this->width, $this->height)
75
            ->setScriptEvents(true)
76
            ->setAreaColor($this->backColor)
77
            ->setAreaFocusColor($this->focusColor)
78
            ->setTextColor($this->textColor)
79
            ->addClass('uiButtonElement')
80
            ->setAlign("center", "center2");
81
        //->setPosition(-$this->width / 2, $this->height / 2);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
82
83
        $buttonFrame->addChild($label);
84
85
86
        return $buttonFrame->render($domDocument);
87
88
    }
89
90
    /**
91
     * Get the Script Features
92
     *
93
     * @return ScriptFeature[]
94
     */
95
    public function getScriptFeatures()
96
    {
97
        return ScriptFeature::collect($this);
98
    }
99
100
    /**
101
     * Prepare the given Script for rendering by adding the needed Labels, etc.
102
     *
103
     * @param Script $script Script to prepar
104
     * @return void
105
     */
106
    public function prepare(Script $script)
107
    {
108
        $script->addCustomScriptLabel(ScriptLabel::MouseClick, $this->getScriptMouseClick());
109
    }
110
111
    private function getScriptMouseClick()
112
    {
113
        return /** language=textmate  prefix=#RequireContext\n */
114
            <<<'EOD'
115
            if (Event.Control.HasClass("uiButtonElement") ) {
116
                if (Event.Control.Parent.HasClass("uiButton")) {
117
                      Event.Control.Parent.RelativeScale = 0.75;
118
                      AnimMgr.Add(Event.Control.Parent, "<elem scale=\"1.\" />", 200, CAnimManager::EAnimManagerEasing::QuadIn); 
119
                      TriggerPageAction(Event.Control.Parent.DataAttributeGet("action"));
120
                }                
121
            }
122
EOD;
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getType()
129
    {
130
        return $this->type;
131
    }
132
133
    /**
134
     * @param string $type
135
     */
136
    public function setType($type)
137
    {
138
        $this->type = $type;
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    public function getTextColor()
145
    {
146
        return $this->textColor;
147
    }
148
149
    /**
150
     * @param string $textColor
151
     */
152
    public function setTextColor($textColor)
153
    {
154
        $this->textColor = $textColor;
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    public function getBackgroundColor()
161
    {
162
        return $this->backColor;
163
    }
164
165
    /**
166
     * @param string $backColor
167
     */
168
    public function setBackgroundColor($backColor)
169
    {
170
        $this->backColor = $backColor;
171
    }
172
173
    /**
174
     * @return string
175
     */
176
    public function getBorderColor()
177
    {
178
        return $this->borderColor;
179
    }
180
181
    /**
182
     * @param string $borderColor
183
     */
184
    public function setBorderColor($borderColor)
185
    {
186
        $this->borderColor = $borderColor;
187
    }
188
189
    /**
190
     * @return null
191
     */
192
    public function getAction()
193
    {
194
        return $this->action;
195
    }
196
197
    /**
198
     * @param null $action
199
     */
200
    public function setAction($action)
201
    {
202
        $this->action = $action;
203
    }
204
205
    /**
206
     * @return string
207
     */
208
    public function getFocusColor()
209
    {
210
        return $this->focusColor;
211
    }
212
213
    /**
214
     * @param string $focusColor
215
     */
216
    public function setFocusColor($focusColor)
217
    {
218
        $this->focusColor = $focusColor;
219
    }
220
221
    /**
222
     * @return string
223
     */
224
    public function getText()
225
    {
226
        return $this->text;
227
    }
228
229
    /**
230
     * @param string $text
231
     */
232
    public function setText($text)
233
    {
234
        $this->text = $text;
235
    }
236
237
}
238