Completed
Pull Request — master (#270)
by
unknown
03:25
created

Checkbox::getScriptMouseClick()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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