Completed
Branch master (3d25b6)
by De Cramer
02:34
created

Window::getChildren()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace eXpansion\Framework\Core\Model\Gui;
4
5
use eXpansion\Framework\Core\Exceptions\Gui\MissingCloseActionException;
6
use eXpansion\Framework\Core\Model\UserGroups\Group;
7
use FML\Controls\Frame;
8
use FML\Controls\Label;
9
use FML\Controls\Quad;
10
use FML\Controls\Quads\Quad_Bgs1;
11
use FML\Controls\Quads\Quad_Bgs1InRace;
12
use FML\Elements\Format;
13
use FML\Types\Container;
14
use FML\Types\Renderable;
15
16
class Window extends Manialink implements Container
17
{
18
    /** @var \FML\ManiaLink  */
19
    protected $manialink;
20
21
    /** @var Label  */
22
    protected $closeButton;
23
24
    /** @var Frame  */
25
    protected $contentFrame;
26
27 3
    public function __construct(
28
        Group $group,
29
        ManiaScriptFactory $windowManiaScriptFactory,
30
        $name,
31
        $sizeX,
32
        $sizeY,
33
        $posX = null,
34
        $posY = null
35
    ) {
36 3
        parent::__construct($group, $name, $sizeX, $sizeY, $posX, $posY);
37
38 3
        $titleHeight = 5.5;
39 3
        $closeButtonWidth = 9.5;
40 3
        $titlebarColor = "3afe";
41
42
        // Manialink containing everything
43 3
        $this->manialink = new \FML\ManiaLink();
44 3
        $this->manialink->setId($this->getId())
45 3
                        ->setName($name)
46 3
                        ->setVersion(\FML\ManiaLink::VERSION_3);
47 3
        $windowFrame = new Frame('Window');
48 3
        $windowFrame->setPosition($posX, $posY);
49 3
        $this->manialink->addChild($windowFrame);
50
51
        // Title bar & title.
52 3
        $titleLabel = new Label();
53 3
        $titleLabel->setPosition(3, -$titleHeight/3 - 1)
54 3
            ->setAlign(Label::LEFT, Label::CENTER2)
55 3
            ->setText($name)
56 3
            ->setTextColor('fff')
57 3
            ->setTextSize(2)
58 3
            ->setTextFont('RajdhaniMono');
59 3
        $windowFrame->addChild($titleLabel);
60
61 3
        $titleBar = new Quad();
62 3
        $titleBar->setSize($sizeX, 0.33)
63 3
            ->setPosition(0, -$titleHeight)
64 3
            ->setBackgroundColor('fff');
65 3
        $windowFrame->addChild($titleBar);
66
67 3
        $titleBar = new Quad();
68 3
        $titleBar->setSize($sizeX / 4, 0.5)
69 3
            ->setPosition(0, -$titleHeight)
70 3
            ->setBackgroundColor('fff');
71 3
        $windowFrame->addChild($titleBar);
72
73 3
        $titleBar = new Quad('Title');
74 3
        $titleBar->setSize($sizeX - $closeButtonWidth, $titleHeight)
75 3
            ->setBackgroundColor($titlebarColor)
76 3
            ->setScriptEvents(true);
77 3
        $windowFrame->addChild($titleBar);
78
79 3
        $this->closeButton = new Label('Close');
80 3
        $this->closeButton->setSize($closeButtonWidth, $titleHeight)
81 3
            ->setPosition($sizeX - $closeButtonWidth + ($closeButtonWidth / 2), -$titleHeight / 2)
82 3
            ->setAlign(Label::CENTER, Label::CENTER2)
83 3
            ->setText("✖")
84 3
            ->setTextColor('fff')
85 3
            ->setTextSize(2)
86 3
            ->setTextFont('OswaldMono')
87 3
            ->setScriptEvents(true)
88 3
            ->setAreaFocusColor($titlebarColor);
89 3
        $windowFrame->addChild($this->closeButton);
90
91
        //body
92 3
        $body = new Quad_Bgs1();
93 3
        $body->setSize($sizeX, $sizeY - $titleHeight)
94 3
            ->setPosition(0, -$titleHeight)
95 3
            ->setSubStyle(Quad_Bgs1::SUBSTYLE_BgWindow3);
96 3
        $windowFrame->addChild($body);
97
98 3
        $body = new Quad_Bgs1InRace();
99 3
        $body->setSize($sizeX + 10, $sizeY + 10)
100 3
            ->setPosition(-5, 5)
101 3
            ->setSubStyle(Quad_Bgs1InRace::SUBSTYLE_BgButtonShadow);
102 3
        $windowFrame->addChild($body);
103
104
        // Add maniascript for window handling.
105 3
        $this->manialink->addChild($windowManiaScriptFactory->createScript(['']));
106
107
        // Frame to handle the content of the window.
108 3
        $this->contentFrame = new Frame();
109 3
        $this->contentFrame->setPosition(2, -$titleHeight - 2);
110 3
        $windowFrame->addChild($this->contentFrame);
111 3
    }
112
113
    /**
114
     * Set action to close the window.
115
     *
116
     * @param $actionId
117
     */
118 1
    public function setCloseAction($actionId)
119
    {
120 1
        $this->closeButton->setDataAttributes(['action' => $actionId]);
121 1
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126 2
    public function getXml()
127
    {
128 2
        if (empty($this->closeButton->getDataAttribute('action')))
129
        {
130 1
            throw new MissingCloseActionException("Close action is missing for window. Check if you are using the proper factory.");
131
        }
132
133 1
        return $this->manialink->__toString();
134
    }
135
136
    /**
137
     * Get the children
138
     *
139
     * @api
140
     * @return Renderable[]
141
     */
142 1
    public function getChildren()
143
    {
144 1
        return $this->contentFrame->getChildren();
145
    }
146
147
    /**
148
     * Add a new child
149
     *
150
     * @api
151
     *
152
     * @param Renderable $child Child Control to add
153
     *
154
     * @return static
155
     */
156 1
    public function addChild(Renderable $child)
157
    {
158 1
        $this->contentFrame->addChild($child);
159
160 1
        return $this;
161
    }
162
163
    /**
164
     * Add a new child
165
     *
166
     * @api
167
     *
168
     * @param Renderable $child Child Control to add
169
     *
170
     * @return static
171
     * @deprecated Use addChild()
172
     * @see        Container::addChild()
173
     */
174 1
    public function add(Renderable $child)
175
    {
176 1
        $this->contentFrame->addChild($child);
177
178 1
        return $this;
179
    }
180
181
    /**
182
     * Add new children
183
     *
184
     * @api
185
     *
186
     * @param Renderable[] $children Child Controls to add
187
     *
188
     * @return static
189
     */
190 1
    public function addChildren(array $children)
191
    {
192 1
        $this->contentFrame->addChildren($children);
193
194 1
        return $this;
195
    }
196
197
    /**
198
     * Remove all children
199
     *
200
     * @api
201
     * @return static
202
     */
203 1
    public function removeAllChildren()
204
    {
205 1
        $this->contentFrame->removeAllChildren();
206
207 1
        return $this;
208
    }
209
210
    /**
211
     * Remove all children
212
     *
213
     * @api
214
     * @return static
215
     * @deprecated Use removeAllChildren()
216
     * @see        Container::removeAllChildren()
217
     */
218 1
    public function removeChildren()
219
    {
220 1
        $this->contentFrame->removeAllChildren();
221
222 1
        return $this;
223
    }
224
225
    /**
226
     * Get the Format
227
     *
228
     * @api
229
     *
230
     * @param bool $createIfEmpty If the format should be created if it doesn't exist yet
231
     *
232
     * @return Format
233
     * @deprecated Use Style
234
     * @see        Style
235
     */
236 1
    public function getFormat($createIfEmpty = true)
237
    {
238 1
        return $this->contentFrame->getFormat($createIfEmpty);
0 ignored issues
show
Deprecated Code introduced by
The method FML\Controls\Frame::getFormat() has been deprecated with message: Use Style

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
239
    }
240
241
    /**
242
     * Set the Format
243
     *
244
     * @api
245
     *
246
     * @param Format $format New Format
247
     *
248
     * @return static
249
     * @deprecated Use Style
250
     * @see        Style
251
     */
252 1
    public function setFormat(Format $format = null) {
253 1
        return $this->contentFrame->setFormat($format);
0 ignored issues
show
Deprecated Code introduced by
The method FML\Controls\Frame::setFormat() has been deprecated with message: Use Style

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
254
    }
255
}
256