Completed
Pull Request — master (#51)
by De Cramer
02:45
created

Window::setFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 1
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
        // Frame to handle the content of the window.
52 3
        $this->contentFrame = new Frame();
53 3
        $this->contentFrame->setPosition(2, -$titleHeight - 2);
54 3
        $this->contentFrame->setSize($sizeX - 4, $sizeY -$titleHeight - 4);
55 3
        $windowFrame->addChild($this->contentFrame);
56
57
        // Title bar & title.
58 3
        $titleLabel = new Label();
59 3
        $titleLabel->setPosition(3, -$titleHeight/3 - 1)
60 3
            ->setAlign(Label::LEFT, Label::CENTER2)
61 3
            ->setText($name)
62 3
            ->setTextColor('fff')
63 3
            ->setTextSize(2)
64 3
            ->setTextFont('RajdhaniMono');
65 3
        $windowFrame->addChild($titleLabel);
66
67 3
        $titleBar = new Quad();
68 3
        $titleBar->setSize($sizeX, 0.33)
69 3
            ->setPosition(0, -$titleHeight)
70 3
            ->setBackgroundColor('fff');
71 3
        $windowFrame->addChild($titleBar);
72
73 3
        $titleBar = new Quad();
74 3
        $titleBar->setSize($sizeX / 4, 0.5)
75 3
            ->setPosition(0, -$titleHeight)
76 3
            ->setBackgroundColor('fff');
77 3
        $windowFrame->addChild($titleBar);
78
79 3
        $titleBar = new Quad('Title');
80 3
        $titleBar->setSize($sizeX - $closeButtonWidth, $titleHeight)
81 3
            ->setBackgroundColor($titlebarColor)
82 3
            ->setScriptEvents(true);
83 3
        $windowFrame->addChild($titleBar);
84
85 3
        $this->closeButton = new Label('Close');
86 3
        $this->closeButton->setSize($closeButtonWidth, $titleHeight)
87 3
            ->setPosition($sizeX - $closeButtonWidth + ($closeButtonWidth / 2), -$titleHeight / 2)
88 3
            ->setAlign(Label::CENTER, Label::CENTER2)
89 3
            ->setText("✖")
90 3
            ->setTextColor('fff')
91 3
            ->setTextSize(2)
92 3
            ->setTextFont('OswaldMono')
93 3
            ->setScriptEvents(true)
94 3
            ->setAreaFocusColor($titlebarColor);
95 3
        $windowFrame->addChild($this->closeButton);
96
97
        //body
98 3
        $body = new Quad_Bgs1();
99 3
        $body->setSize($sizeX, $sizeY - $titleHeight)
100 3
            ->setPosition(0, -$titleHeight)
101 3
            ->setSubStyle(Quad_Bgs1::SUBSTYLE_BgWindow3);
102 3
        $windowFrame->addChild($body);
103
104 3
        $body = new Quad_Bgs1InRace();
105 3
        $body->setSize($sizeX + 10, $sizeY + 10)
106 3
            ->setPosition(-5, 5)
107 3
            ->setSubStyle(Quad_Bgs1InRace::SUBSTYLE_BgButtonShadow);
108 3
        $windowFrame->addChild($body);
109
110
        // Add maniascript for window handling.
111 3
        $this->manialink->addChild($windowManiaScriptFactory->createScript(['']));
112 3
    }
113
114
    /**
115
     * Set action to close the window.
116
     *
117
     * @param $actionId
118
     */
119 1
    public function setCloseAction($actionId)
120
    {
121 1
        $this->closeButton->setDataAttributes(['action' => $actionId]);
122 1
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127 2
    public function getXml()
128
    {
129 2
        if (empty($this->closeButton->getDataAttribute('action')))
130
        {
131 1
            throw new MissingCloseActionException("Close action is missing for window. Check if you are using the proper factory.");
132
        }
133
134 1
        return $this->manialink->__toString();
135
    }
136
137
    /**
138
     * Get the children
139
     *
140
     * @api
141
     * @return Renderable[]
142
     */
143 1
    public function getChildren()
144
    {
145 1
        return $this->contentFrame->getChildren();
146
    }
147
148
    /**
149
     * Add a new child
150
     *
151
     * @api
152
     *
153
     * @param Renderable $child Child Control to add
154
     *
155
     * @return static
156
     */
157 1
    public function addChild(Renderable $child)
158
    {
159 1
        $this->contentFrame->addChild($child);
160
161 1
        return $this;
162
    }
163
164
    /**
165
     * Add a new child
166
     *
167
     * @api
168
     *
169
     * @param Renderable $child Child Control to add
170
     *
171
     * @return static
172
     * @deprecated Use addChild()
173
     * @see        Container::addChild()
174
     */
175 1
    public function add(Renderable $child)
176
    {
177 1
        $this->contentFrame->addChild($child);
178
179 1
        return $this;
180
    }
181
182
    /**
183
     * Add new children
184
     *
185
     * @api
186
     *
187
     * @param Renderable[] $children Child Controls to add
188
     *
189
     * @return static
190
     */
191 1
    public function addChildren(array $children)
192
    {
193 1
        $this->contentFrame->addChildren($children);
194
195 1
        return $this;
196
    }
197
198
    /**
199
     * Remove all children
200
     *
201
     * @api
202
     * @return static
203
     */
204 1
    public function removeAllChildren()
205
    {
206 1
        $this->contentFrame->removeAllChildren();
207
208 1
        return $this;
209
    }
210
211
    /**
212
     * Remove all children
213
     *
214
     * @api
215
     * @return static
216
     * @deprecated Use removeAllChildren()
217
     * @see        Container::removeAllChildren()
218
     */
219 1
    public function removeChildren()
220
    {
221 1
        $this->contentFrame->removeAllChildren();
222
223 1
        return $this;
224
    }
225
226
    /**
227
     * Get the Format
228
     *
229
     * @api
230
     *
231
     * @param bool $createIfEmpty If the format should be created if it doesn't exist yet
232
     *
233
     * @return Format
234
     * @deprecated Use Style
235
     * @see        Style
236
     */
237 1
    public function getFormat($createIfEmpty = true)
238
    {
239 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...
240
    }
241
242
    /**
243
     * Set the Format
244
     *
245
     * @api
246
     *
247
     * @param Format $format New Format
248
     *
249
     * @return static
250
     * @deprecated Use Style
251
     * @see        Style
252
     */
253 1
    public function setFormat(Format $format = null) {
254 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...
255
    }
256
257
    /**
258
     * @return Frame
259
     */
260
    public function getContentFrame()
261
    {
262
        return $this->contentFrame;
263
    }
264
}
265