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