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

uiCheckbox::prepare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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