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

Scrollbar::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 5
1
<?php
2
3
namespace eXpansion\Framework\Gui\Components;
4
5
use FML\Controls\Frame;
6
use FML\Controls\Label;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, eXpansion\Framework\Gui\Components\Label.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use FML\Controls\Quad;
8
use FML\Script\Script;
9
use FML\Types\Renderable;
10
11
class Scrollbar extends AbstractUiElement implements Renderable
12
{
13
14
    const AXIS_X = "X";
15
    const AXIS_Y = "Y";
16
17
    /**
18
     * @var string
19
     */
20
    private $axis;
21
    /**
22
     * @var float
23
     */
24
    private $length;
25
    /**
26
     * @var float
27
     */
28
    private $scrollbarSize;
29
30
    /**
31
     * uiScrollbar constructor.
32
     * @param string $axis
33
     * @param float  $posX
34
     * @param float  $posY
35
     * @param        $scrollbarSize
36
     * @param float  $length
37
     */
38
    public function __construct($axis, $posX, $posY, $scrollbarSize, $length)
39
    {
40
41
        $this->axis = $axis;
42
        $this->setPosition($posX, $posY);
43
        $this->length = $length;
44
        if ($this->axis == "X") {
45
            $this->setWidth($length);
46
            $this->setHeight(5.);
47
        } else {
48
            $this->setHeight($length);
49
            $this->setWidth(5.);
50
        }
51
        $this->scrollbarSize = $scrollbarSize;
52
    }
53
54
    /**
55
     * Render the XML element
56
     *
57
     * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered
58
     * @return \DOMElement
59
     */
60
    public function render(\DOMDocument $domDocument)
61
    {
62
        if ($this->axis == self::AXIS_X) {
63
            $frame = $this->getScrollbarX();
64
        } else {
65
            $frame = $this->getScrollbarY();
66
        }
67
68
        return $frame->render($domDocument);
69
    }
70
71 View Code Duplication
    private function getScrollbarX()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        $frame = new Frame();
74
        $frame->setPosition($this->posX, $this->posY)
75
            ->addClass('uiScrollbarControl');
76
77
        $scrollbar = new Quad();
78
        $scrollbar->setPosition(5, 0)
79
            ->setSize($this->scrollbarSize, 5)
80
            ->setBackgroundColor('fffa')
81
            ->setFocusBackgroundColor("ffff")
82
            ->addClass('uiScrollbar')
83
            ->addDataAttribute('axis', $this->axis)
84
            ->setAlign('left', 'bottom')
85
            ->setScriptEvents(true);
86
87
        $leftLabel = new Label();
88
        $leftLabel->setPosition(0, 0)
89
            ->setSize(5, 5)
90
            ->setText("⏴")
91
            ->setScriptEvents(true)
92
            ->setAreaColor('aaa')
93
            ->setAreaFocusColor('777')
94
            ->setAlign("left", "bottom");
95
96
        $rightLabel = new Label();
97
        $rightLabel->setPosition($this->length, 0)
98
            ->setSize(5, 5)
99
            ->setText("⏵")
100
            ->setScriptEvents(true)
101
            ->setAreaColor('aaa')
102
            ->setAreaFocusColor('777')
103
            ->setAlign("right", "bottom");
104
105
        $background = new Quad();
106
        $background->setSize($this->length, 5)
107
            ->setAlign("left", "bottom")
108
            ->setBackgroundColor("000a");
109
110
        $frame->addChild($scrollbar);
111
        $frame->addChild($leftLabel);
112
        $frame->addChild($rightLabel);
113
        $frame->addChild($background);
114
115
        return $frame;
116
117
    }
118
119 View Code Duplication
    private function getScrollbarY()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121
        $frame = new Frame();
122
        $frame->setPosition($this->posX, $this->posY)
123
            ->addClass('uiScrollbarControl');
124
125
126
        $scrollbar = new Quad();
127
        $scrollbar->setPosition(0, -5)
128
            ->setSize(5, $this->scrollbarSize)
129
            ->setBackgroundColor('fffa')
130
            ->setFocusBackgroundColor('ffff')
131
            ->addClass('uiScrollbar')
132
            ->addDataAttribute('axis', $this->axis)
133
            ->setAlign('right', 'top')
134
            ->setScriptEvents(true);
135
136
        $topLabel = new Label();
137
        $topLabel->setPosition(0, 0)
138
            ->setSize(5, 5)
139
            ->setText("⏶")
140
            ->setScriptEvents(true)
141
            ->setAreaColor('aaa')
142
            ->setAreaFocusColor('777')
143
            ->setAlign("right", "top");
144
145
        $bottomLabel = new Label();
146
        $bottomLabel->setPosition(0, -$this->length)
147
            ->setSize(5, 5)
148
            ->setText("⏷")
149
            ->setScriptEvents(true)
150
            ->setAreaColor('aaa')
151
            ->setAreaFocusColor('777')
152
            ->setAlign("right", "bottom");
153
154
        $background = new Quad();
155
        $background->setSize(5, $this->length)
156
            ->setAlign("right", "top")
157
            ->setBackgroundColor("000a");
158
159
        $frame->addChild($scrollbar);
160
        $frame->addChild($topLabel);
161
        $frame->addChild($bottomLabel);
162
        $frame->addChild($background);
163
164
        return $frame;
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 void
172
     */
173
    public function prepare(Script $script)
174
    {
175
        // do nothing
176
    }
177
178
179
}
180