-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollPanel.cpp
More file actions
132 lines (108 loc) · 2.81 KB
/
ScrollPanel.cpp
File metadata and controls
132 lines (108 loc) · 2.81 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include"ScrollPanel.h"
ScrollPanel::ScrollPanel()
{
scrollSpeed = 1.0f;
margin = 0.7;
offset = { 0 };
offSet = &offset;
color = { 0,0,0,0 };
type = SCROLL;
}
void ScrollPanel::setPanelSize(float w, float h)
{
offSet->x = w;
offSet->y = h;
}
void ScrollPanel::setPanelSize(int x, int y, float w, float h)
{
this->setPos(x, y);
offSet->x = w;
offSet->y = h;
}
void ScrollPanel::setScrollSpeed(float speed)
{
scrollSpeed = speed;
}
void ScrollPanel::setScrollSpeed(int speed)
{
scrollSpeed = static_cast<int>(speed);
}
void ScrollPanel::setColor(unsigned char r, unsigned char g, unsigned char b,unsigned char a)
{
color = { r,g,b,a };
}
void ScrollPanel::setScrollLimit(float topMargin,float bottomMargin)
{
sort(objectList.begin(), objectList.end(), [](const std::unique_ptr<Object>& a, const std::unique_ptr<Object>& b)
{
return a->currentPosY() < b->currentPosY();
});
}
void ScrollPanel::addObjectList(Object* obj)
{
objectList.emplace_back(std::unique_ptr<Object>(obj));
}
void ScrollPanel::Update()
{
}
void ScrollPanel::moveObjects(float wheel)
{
float scrolled = wheel * scrollSpeed;
auto topObj = objectList.begin();
float topPos = (*topObj)->currentPosY();
auto bottomObj = objectList.end() - 1;
float bottomPos = (*bottomObj)->getBottom();
if (wheel != 0)
{
#ifdef _Debug
cout << wheel << endl;
#endif
}
if ((pos->y + offSet->y) * margin < bottomPos && wheel > 0)
{
(*bottomObj)->setPos((*bottomObj)->currentPosX(), (*bottomObj)->currentPosY() - scrolled);
for (auto itr = objectList.begin(); itr != objectList.end() - 1; ++itr)
{
(*itr)->setPos((*itr)->currentPosX(), (*itr)->currentPosY() - scrolled);
#ifdef _Debug
cout << (*itr)->currentPosY() - scrolled << endl;
#endif
}
}
else if(pos->y * (1 - margin) > topPos&& wheel < 0)
{
(*topObj)->setPos((*topObj)->currentPosX(), (*topObj)->currentPosY() - wheel * scrollSpeed);
for (auto itr = objectList.begin() + 1; itr != objectList.end(); ++itr)
{
(*itr)->setPos((*itr)->currentPosX(), (*itr)->currentPosY() - wheel * scrollSpeed);
}
}
}
bool ScrollPanel::hitCheck()
{
Mouse* mouse = nullptr;
mouse = Mouse::GetInstance();
if (mouse->mx > this->pos->x && mouse->mx < this->pos->x + offSet->x)
{
if (mouse->my > this->pos->y && mouse->my < this->pos->y + offSet->y)
{
moveObjects(mouse->wheel);
for (auto itr = objectList.begin(); itr != objectList.end(); ++itr)
{
(*itr)->hitCheckScroll(pos, offSet);
}
return true;
}
}
return false;
}
void ScrollPanel::drawObjects(SDL_Renderer* gRenderer)
{
SDL_SetRenderDrawColor(gRenderer, color.r, color.g, color.b, color.a);
SDL_Rect rect = { pos->x, pos->y, offSet->x, offSet->y};
SDL_RenderFillRect(gRenderer, &rect);
for (auto itr = objectList.begin(); itr != objectList.end(); ++itr)
{
(*itr)->drawObjectsScroll(gRenderer, pos,offSet);
}
}