-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfxobject.cpp
126 lines (103 loc) · 2.69 KB
/
gfxobject.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
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
#include "gfxobject.hpp"
namespace TerraNova {
GFXObject::GFXObject(SDL_Renderer* ren, const File::Path& spritePath,
const int x, const int y, const bool selectable): ren(ren),
selectable(selectable) {
layout.x = x;
layout.y = y;
ChangeSprite(spritePath);
}
void GFXObject::ChangeSprite(File::Path spritePath){
sprite = GFXManager::RequestSprite(spritePath);
sprite->MakeDefaultSize(layout);
if (selectable) {
spritePath.remove_filename();
spritePath /= "sprite_selected";
selectedSprite = GFXManager::RequestSprite(spritePath);
}
}
void GFXObject::Render() const{
if (!visible) return;
sprite->RenderTo(ren, layout);
if (selected) selectedSprite->RenderTo(ren, layout);
}
int GFXObject::Select(){
if(selectable){
selected = true;
}
return static_cast<int>(SELECTED);
}
void GFXObject::Deselect(){
selected = false;
}
std::string GFXObject::HoverText() const{
return "This object has no specialized hover text.";
}
void GFXObject::MoveTo(int x, int y){
/*if(x < 0) x = 0;
if(y < 0) y = 0;
layout.x = std::min(x, SCREEN_WIDTH - layout.w);
layout.y = std::min(y, SCREEN_HEIGHT - layout.h);*/
layout.x = x;
layout.y = y;
}
// this takes an entire SDL_Rect but only uses the positions, not the sizes
void GFXObject::MoveTo(SDL_Rect newLayout){
/*if(newLayout.x < 0) newLayout.x = 0;
if(newLayout.y < 0) newLayout.y = 0;
layout.x = std::min(newLayout.x, SCREEN_WIDTH - layout.w);
layout.y = std::min(newLayout.y, SCREEN_HEIGHT - layout.h);*/
MoveTo(newLayout.x, newLayout.y);
}
void GFXObject::Resize(int w, int h){
if(w < 0) w = 0;
if(h < 0) h = 0;
layout.w = w;
layout.h = h;
}
// this takes an entire SDL_Rect but only uses the sizes, not the positions
void GFXObject::Resize(SDL_Rect newLayout){
/*if(newLayout.w < 0) newLayout.w = 0;
if(newLayout.h < 0) newLayout.h = 0;
layout.w = newLayout.w;
layout.h = newLayout.h;*/
Resize(newLayout.w, newLayout.h);
}
int GFXObject::X() const{
return layout.x;
}
int GFXObject::Y() const{
return layout.y;
}
int GFXObject::W() const{
return layout.w;
}
int GFXObject::H() const{
return layout.h;
}
const SDL_Rect& GFXObject::Layout() const{
return layout;
}
int GFXObject::LeftEdge() const{
return layout.x;
}
int GFXObject::RightEdge() const{
return layout.x + layout.w - 1;
}
int GFXObject::TopEdge() const{
return layout.y;
}
int GFXObject::BottomEdge() const{
return layout.y + layout.h - 1;
}
bool GFXObject::InsideQ(const int x, const int y) {
if(!visible) return false;
if(x < layout.x || y < layout.y || x >= layout.x + layout.w ||
y >= layout.y + layout.h) return false;
return true;
}
GFXObject* GFXObject::SelectAt(const int x, const int y) {
if (InsideQ(x, y)) return this;
return nullptr;
}
} // namespace TerraNova