-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubwindow.cpp
More file actions
61 lines (51 loc) · 1.63 KB
/
subwindow.cpp
File metadata and controls
61 lines (51 loc) · 1.63 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
#include "subwindow.hpp"
namespace TerraNova {
Subwindow::Subwindow(SDL_Renderer* ren, const std::string spriteFile,
const int x, const int y) : UIElement(ren, spriteFile, x, y) {
layout.x -= layout.w/2;
layout.y -= layout.h/2;
}
void Subwindow::AddCaption(const std::string& caption) {
AddText(caption, MakeSDLRect(W()/2, SUBWINDOW_BUTTON_SPACING, W(), H()));
if (textSprite != nullptr) layout.y -= textLayout.h/2;
}
void Subwindow::AddButton(std::unique_ptr<Button> toAdd) {
layout.x += layout.w/2;
layout.w = std::max(layout.w, toAdd->W() + 2*SUBWINDOW_BUTTON_SPACING);
layout.x -= layout.w/2;
//layout.y -= toAdd->H()/2;
buttons.push_back(std::move(toAdd));
int bottomY = MoveButtonsTo(layout.x, layout.y);
layout.h = bottomY - Y();
}
void Subwindow::Render() const {
UIElement::Render();
for (auto& button : buttons) button->Render();
}
void Subwindow::MoveTo(int x, int y) {
UIElement::MoveTo(x, y);
MoveButtonsTo(x, y);
}
void Subwindow::MoveTo(SDL_Rect newLayout) {
MoveTo(newLayout.x, newLayout.y);
}
// return the location of the bottom of the last button, plus one BUTTON_SPACING
int Subwindow::MoveButtonsTo(int x, int y) {
int currentY = layout.y + 2*SUBWINDOW_BUTTON_SPACING;
if (textSprite != nullptr) currentY += SUBWINDOW_BUTTON_SPACING;
for (auto& button : buttons) {
button->MoveTo(layout.x + layout.w/2 - button->W()/2, currentY);
currentY += SUBWINDOW_BUTTON_SPACING + button->H();
}
return currentY;
}
GFXObject* Subwindow::SelectAt(const int x, const int y) {
for (auto& button : buttons) {
if (button->InsideQ(x, y)) {
button->Click();
return nullptr;
}
}
return nullptr;
}
} // namespace TerraNova