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

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