-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathContainer.cpp
99 lines (90 loc) · 3.39 KB
/
Container.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include"Container.h"
Container::Container(const FloatRect & rectangle, Control & ctrl, const Vector2f & windowSize)
:
bounds(rectangle),
control(ctrl),
containerView(FloatRect(0, 0, rectangle.width, rectangle.height)),
controlView(FloatRect(0, 0, rectangle.width - scrollBarSize, rectangle.height)),
scroll({ rectangle.width - scrollBarSize,0 }, { (float)scrollBarSize,rectangle.height }, controlView, { control.getGlobalBounds().width, control.getGlobalBounds().height })
{
float ratioX = (1.0 / windowSize.x);
float ratioY = (1.0 / windowSize.y);
containerView.setViewport(FloatRect({ ratioX * rectangle.left , ratioY * rectangle.top, ratioX * rectangle.width,ratioY * rectangle.height }));
controlView.setViewport(FloatRect({ ratioX * rectangle.left , ratioY * rectangle.top, ratioX * (rectangle.width - scrollBarSize),ratioY * rectangle.height }));
control.setPosition({ 0, 0 });
}
void Container::resize(const FloatRect & rectangle, const Vector2f & windowSize)
{
bounds = rectangle;
containerView.reset(FloatRect(0, 0, rectangle.width, rectangle.height));
controlView.reset(FloatRect(0, 0, rectangle.width - scrollBarSize, rectangle.height));
float ratioX = (1.0 / windowSize.x);
float ratioY = (1.0 / windowSize.y);
containerView.setViewport(FloatRect({ ratioX * rectangle.left , ratioY * rectangle.top, ratioX * rectangle.width,ratioY * rectangle.height }));
controlView.setViewport(FloatRect({ ratioX * rectangle.left , ratioY * rectangle.top, ratioX * (rectangle.width - scrollBarSize),ratioY * rectangle.height }));
scroll.setSettings({ rectangle.width - scrollBarSize,0 }, { (float)scrollBarSize,rectangle.height });
control.setPosition({ 0, 0 });
}
bool Container::contains(const Vector2f & point)
{
return bounds.contains(point);
}
Control * Container::handleEvent(RenderWindow & window, Event event, Vector2i mousePos)
{
if (event.type == Event::MouseWheelMoved)
{
float wheel = event.mouseWheel.delta * -1;
scroll.scroll(wheel);
return nullptr;
}
Vector2f containerRelative = window.mapPixelToCoords(mousePos, containerView);
if (scroll.contains(containerRelative) && event.type == Event::MouseButtonPressed)
{
if (scroll.onBar(containerRelative))
{
Window & w = window;
Vector2f current = containerRelative;
while (Mouse::isButtonPressed(Mouse::Button::Left))
{
current = window.mapPixelToCoords(Mouse::getPosition(window), containerView);
}
scroll.scroll((current.y - containerRelative.y) / 10);
return nullptr;
}
else if (scroll.onScrollUpButton(containerRelative))
{
scroll.scrollUpButtonClick();
return nullptr;
}
else if (scroll.onScrollDownButton(containerRelative))
{
scroll.scrollDownButtonClick();
return nullptr;
}
return nullptr;
}
else
{
Vector2f controlRelative = window.mapPixelToCoords(mousePos, controlView);
Control * ptr = control.handleEvent(event, controlRelative);
return ptr;
}
return nullptr;
}
void Container::draw(RenderTarget& target, RenderStates states) const
{
const View & temp = target.getView();
control.setPosition({ 0,0 });
target.setView(containerView);
if (control.getGlobalBounds().height > bounds.height)
{
target.draw(scroll);
}
target.setView(controlView);
target.draw(control);
target.setView(temp);
}
void Container::updateControlSize(const Vector2f & size)
{
scroll.updateTargetSize(size);
}