Completed
Pull Request — master (#95)
by
unknown
02:41
created

uiCheckbox::render()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 40
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.8571
c 0
b 0
f 0
ccs 0
cts 32
cp 0
cc 3
eloc 30
nc 1
nop 1
crap 12
1
<?php
2
3
namespace eXpansion\Framework\Gui\Components;
4
5
use FML\Controls\Entry;
6
use FML\Controls\Frame;
7
use FML\Controls\Labels\Label_Text;
8
use FML\Script\Features\ScriptFeature;
9
use FML\Script\Script;
10
use FML\Script\ScriptLabel;
11
use FML\Types\ScriptFeatureable;
12
13
class uiCheckbox extends abstractUiElement implements ScriptFeatureable
14
{
15
    /**
16
     * @var string
17
     */
18
    private $text;
19
    /**
20
     * @var string
21
     */
22
    private $name;
23
    /**
24
     * @var bool
25
     */
26
    private $checked = false;
27
    /**
28
     * @var bool
29
     */
30
    private $disabled = false;
31
32
    private $width = 30;
33
    private $height = 6;
34
35
36
    /**
37
     * uiCheckbox constructor.
38
     * @param string $text
39
     * @param string $name
40
     * @param bool $checked
41
     * @param bool $disabled
42
     */
43
    public function __construct($text, $name, $checked = false, $disabled = false)
44
    {
45
        $this->text = $text;
46
        $this->name = $name;
47
        $this->checked = $checked;
48
        $this->disabled = $disabled;
49
50
    }
51
52
53
    /**
54
     * Render the XML element
55
     *
56
     * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered
57
     * @return \DOMElement
58
     *
59
     *
60
     *
61
     * <frame pos="40 0" class="uiContainer uiCheckbox" data-checked="0" data-disabled="0">
62
     * <entry pos="45 -3" z-index="0" size="6 6" textemboss="1" text="1" textsize="3" valign="center2" halign="center" textformat="Basic" name="checkbox" scriptevents="1" hidden="0"/>
63
     * <label pos="3.5 -2" z-index="0" size="6 6" textemboss="1" textsize="4" text="✔" valign="center2" halign="center" class="uiElement uiChecked" scriptevents="1" focusareacolor1="0000" focusareacolor2="0000" hidden="1"/>
64
     * <label pos="3 -3" z-index="0" size="6 6" textemboss="1" textsize="4" text="⬜" valign="center2" halign="center" class="uiElement" scriptevents="1" focusareacolor1="0000" focusareacolor2="0000"/>
65
     * <label pos="6 -2.5" z-index="0" size="43 5" text="Selected Checkbox" textsize="2" scriptevents="1" focusareacolor1="0000" focusareacolor2="0000" valign="center" class="uiElement"/>
66
     * </frame>
67
     */
68
    public function render(\DOMDocument $domDocument)
69
    {
70
        $containerFrame = new Frame();
71
        $containerFrame->setPosition($this->posX, $this->posY)
72
            ->setZ($this->posZ)
73
            ->addClasses(['uiContainer', 'uiCheckbox'])
74
            ->addDataAttribute('checked', $this->isChecked() ? "1" : "0")
75
            ->addDataAttribute('disabled', $this->isDisabled() ? "1" : "0");
76
77
78
        $entry = new Entry();
79
        $entry->setPosition(900, 900)
80
            ->setName($this->name);
81
82
        $checkedBackground = new uiLabel('⬜');
83
        $checkedBackground->setTextSize(4)
84
            ->setAlign('center', 'center2')
85
            ->setSize(6, 6)
86
            ->setPosition(3, -3);
87
        $checkedBackground->setScriptEvents(true)
88
            ->addClass('uiCheckboxElement');
89
90
        $checkedLabel = clone $checkedBackground;
91
        $checkedLabel->setText('✔')->setPosition(3.5, -2)->setScale(0);
92
93
        $label = new uiLabel();
94
        $label->setTranslate(false)
95
            ->setAlign("left", "center2")
96
            ->setPosition(6, -3)
97
            ->setSize($this->width - 6, $this->height)
98
            ->setText($this->getText());
99
100
        $containerFrame->addChild($entry);
101
        $containerFrame->addChild($label);
102
        $containerFrame->addChild($checkedLabel);
103
        $containerFrame->addChild($checkedBackground);
104
105
106
        return $containerFrame->render($domDocument);
107
    }
108
109
110
    /**
111
     * @return string
112
     */
113
    public function getText()
114
    {
115
        return $this->text;
116
    }
117
118
    /**
119
     * @param string $text
120
     */
121
    public function setText($text)
122
    {
123
        $this->text = $text;
124
    }
125
126
    /**
127
     * @param string $name
128
     * @return uiCheckbox
129
     */
130
    public function setName($name)
131
    {
132
        $this->name = $name;
133
134
        return $this;
135
    }
136
137
    /**
138
     * @return bool
139
     */
140
    public function isChecked()
141
    {
142
        return $this->checked;
143
    }
144
145
    /**
146
     * @param bool $checked
147
     */
148
    public function setChecked($checked)
149
    {
150
        $this->checked = $checked;
151
    }
152
153
    /**
154
     * @return bool
155
     */
156
    public function isDisabled()
157
    {
158
        return $this->disabled;
159
    }
160
161
    /**
162
     * @param bool $disabled
163
     */
164
    public function setDisabled($disabled)
165
    {
166
        $this->disabled = $disabled;
167
    }
168
169
170
    /**
171
     * Prepare the given Script for rendering by adding the needed Labels, etc.
172
     *
173
     * @param Script $script Script to prepare
174
     * @return static
175
     */
176
    public function prepare(Script $script)
177
    {
178
        $script->addScriptFunction("uiCheckboxFunctions", $this->getScriptRenderCheckbox());
179
        $script->addCustomScriptLabel(ScriptLabel::MouseClick, $this->getScriptMouseClick());
180
        $script->addCustomScriptLabel(ScriptLabel::OnInit, $this->getScriptInit());
181
    }
182
183
    private function getScriptInit()
184
    {
185
        return /** language=textmate  prefix=#RequireContext\n */
186
            <<<'EOD'
187
            Page.GetClassChildren ("uiContainer", Page.MainFrame, True);
188
            foreach (frame in Page.GetClassChildren_Result) {
189
                if (frame.HasClass("uiCheckbox")) {
190
                    uiRenderCheckbox((frame as CMlFrame));				
191
                }								
192
            }
193
EOD;
194
    }
195
196
    /**
197
     * @return string
198
     */
199
    private function getScriptMouseClick()
200
    {
201
        return /** language=textmate  prefix=#RequireContext\n */
202
            <<<'EOD'
203
            if (Event.Control.HasClass("uiCheckboxElement") ) {
204
                if (Event.Control.Parent.HasClass("uiCheckbox")) {									
205
                        uiToggleCheckbox(Event.Control.Parent);	
206
                }
207
            }																		
208
EOD;
209
    }
210
211
212
    /**
213
     * @return string
214
     */
215
    private function getScriptRenderCheckbox()
216
    {
217
        return /** @lang textmate */
218
            <<<'EOD'
219
  
220
          
221
Void uiRenderCheckbox(CMlFrame frame) {
222
    declare uiControl = frame.Controls[2];
223
	if (frame.DataAttributeGet("checked") == "1") {
224
       AnimMgr.Add(uiControl, "<frame scale=\"1.\" />", 250, CAnimManager::EAnimManagerEasing::BackOut);            
225
    } else {
226
       AnimMgr.Add(uiControl, "<frame scale=\"0.\" />", 100, CAnimManager::EAnimManagerEasing::BackIn);
227
	}
228
        declare CMlEntry entry = (frame.Controls[0] as CMlEntry);
229
	    entry.Value = frame.DataAttributeGet("checked") ;  
230
}	
231
232
Void uiToggleCheckbox(CMlFrame frame) { 
233
	if  (frame.DataAttributeGet("checked") == "1") {
234
		frame.DataAttributeSet("checked", "0");
235
	} else {
236
		frame.DataAttributeSet("checked", "1");			
237
	}
238
	uiRenderCheckbox(frame);			
239
}
240
241
EOD;
242
    } // end of getScriptRenderCheckbox
243
244
    /**
245
     * Get the Script Features
246
     *
247
     * @return ScriptFeature[]
248
     */
249
    public function getScriptFeatures()
250
    {
251
        return ScriptFeature::collect($this);
252
    }
253
254
    public function setSize($x, $y)
255
    {
256
        $this->width = $x;
257
        $this->height = $y;
258
    }
259
260
    public function getHeight()
261
    {
262
        return $this->height;
263
    }
264
265
    public function getWidth()
266
    {
267
        return $this->width;
268
    }
269
}
270