-
Notifications
You must be signed in to change notification settings - Fork 5
/
LayoutMenus.cpp
218 lines (196 loc) · 6.32 KB
/
LayoutMenus.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "LayoutMenus.h"
#include "SDL_include.h"
#include <sstream>
#include <iomanip>
using namespace widgets;
LayoutMenus::LayoutMenus(const int &x, const int &y, const int &width, const int &height, SDL_Texture *newtexture, SDL_Renderer *renderer, const bool& mini)
{
rect.x = x;
rect.y = y;
rect.w = width;
rect.h = height;
texture = newtexture;
visible = false;
SDL_Color color;
color.a = 255;
color.b = 0;
color.g = 0;
color.r = 0;
std::string xmlpath = "assets//xml//layout_menu_config.xml";
std::string xmltype = "layout";
hxmlFile = new rapidxml::file<>(xmlpath.c_str()); // Default template is char
hdoc.parse<0>(hxmlFile->data());
hpRoot = hdoc.first_node();
if (!mini){
pNode = hpRoot->last_node("config")->first_node(xmltype.c_str());
}
else{
pNode = hpRoot->first_node("config")->first_node(xmltype.c_str());
}
sNode = pNode->first_node("slots");
nNode = pNode->first_node("slot_name");
dNode = pNode->first_node("slot_description");
for (int i = 0; i < 3; ++i){
slots.push_back(MeguButton(rect.x + atoi(sNode->first_node("pos")->first_node("x")->value()),
rect.y + i * atoi(sNode->first_node("offset")->first_node("y")->value()) + atoi(sNode->first_node("pos")->first_node("y")->value()),
atoi(sNode->first_node("pos")->first_node("w")->value()),
atoi(sNode->first_node("pos")->first_node("h")->value()), NULL, false));
slots_name.push_back(TtfWrapper(rect.x + atoi(nNode->first_node("pos")->first_node("x")->value())
, rect.y + i * atoi(nNode->first_node("offset")->first_node("y")->value()) + atoi(nNode->first_node("pos")->first_node("y")->value())
, 255, 255, 255, SDL_BLENDMODE_BLEND, 0, color
, atoi(nNode->first_node("font_size")->value())
, nNode->first_node("font")->value()
, 300
, false)
);
slots_description.push_back(TtfWrapper(rect.x + atoi(dNode->first_node("pos")->first_node("x")->value())
, rect.y + i * atoi(dNode->first_node("offset")->first_node("y")->value()) + atoi(dNode->first_node("pos")->first_node("y")->value())
, 255, 255, 255, SDL_BLENDMODE_BLEND, 0, color
, atoi(dNode->first_node("font_size")->value())
, nNode->first_node("font")->value()
, 300
, false)
);
}
marker = loadTexture(pNode->first_node("marker")->first_node("texture")->value(), renderer);
marker_rect.w = atoi(pNode->first_node("marker")->first_node("size")->first_node("w")->value());
marker_rect.h = atoi(pNode->first_node("marker")->first_node("size")->first_node("h")->value());
marker_visible = false;
for (rapidxml::xml_node<> *bNode = pNode->first_node("button"); bNode; bNode = bNode->next_sibling("button"))
{
controls.push_back(MeguButton(
rect.x + atoi(bNode->first_node("pos")->first_node("x")->value())
, rect.y + atoi(bNode->first_node("pos")->first_node("y")->value())
, atoi(bNode->first_node("pos")->first_node("w")->value())
, atoi(bNode->first_node("pos")->first_node("h")->value())
, NULL
, true)
);
}
int index = 0;
current_page = 0;
//Load image at specified path
}
void LayoutMenus::RenderButtons(SDL_Renderer *renderer)
{
if (visible){
if (marker_visible){
SDL_RenderCopy(renderer, marker, NULL, &marker_rect);
}
}
}
void LayoutMenus::setViewPage(const int &page, SDL_Renderer *renderer){
if (save){
if (page >= 0){
current_page = page;
updateSlots(renderer);
}
}
else{
std::cerr << "layout list size: " << layout_list.size() << std::endl;
std::cerr << "page: " << page << std::endl;
std::cerr << "page limit: " << (layout_list.size() - 1) / 3 << std::endl;
if (page <= (layout_list.size() - 1) / 3 && page >= 0){
current_page = page;
updateSlots(renderer);
}
}
}
LayoutMenus::~LayoutMenus()
{
}
void LayoutMenus::parseFiles(const std::vector<std::string> &layout_friend_list){
layout_list.clear();
int i = 1;
for (std::vector<std::string>::const_iterator it = layout_friend_list.begin(); it != layout_friend_list.end(); ++it){
std::ifstream file;
std::string filename = "gamedata/house/";
file_data f;
std::stringstream ss;
std::string s;
std::cerr << *it << std::endl;
ss << std::setw(3) << std::setfill('0') << i;
s= "SAV" + ss.str() + ".house";
ss.clear();
while (s != *it){
std::cerr << "s " << s << " " << *it << std::endl;
f.enabled = false;
layout_list.push_back(f);
i= i+1;
std::stringstream ss1;
ss1.clear();
ss1 << std::setw(3) << std::setfill('0') << i;
s.clear();
s = "SAV" + ss1.str() + ".house";
}
file.open(filename + *it);
if (file.is_open()){
f.path = filename;
file >> f.img_path;
f.enabled = true;
layout_list.push_back(f);
file.close();
}
++i;
}
}
void LayoutMenus::updateSlots(SDL_Renderer *renderer){
//clean up first
for (int i = 0; i < 3; ++i){
slots[i].changeObject(NULL);
slots[i].setEnable(false);
slots_name[i].free();
slots_name[i].setVisible(false);
slots_description[i].free();
slots_description[i].setVisible(false);
}
for (int i = 0; i < 3; ++i){
int curr_index = current_page * 3 + i;
SDL_Texture * texture = NULL;
if (curr_index < layout_list.size()){
int w, h;
texture = loadTexture("gamedata/house/preview/" + layout_list.at(curr_index).img_path, renderer);
std::cerr << "layout list: " << layout_list.at(curr_index).img_path << std::endl;
std::cerr << layout_list.at(curr_index).img_path << std::endl;
}
std::cerr << "current index: " << curr_index << std::endl;
slots[i].changeDisplayObject(texture, marker_rect.w, marker_rect.h, curr_index + 1);
if (save){
slots[i].setEnable(true);
}
else{
if (layout_list.at(curr_index).enabled){
slots[i].setEnable(true);
}
else{
slots[i].setEnable(false);
}
}
}
}
SDL_Texture* LayoutMenus::loadTexture(std::string path, SDL_Renderer *renderer)
{
//The final texture
SDL_Texture* newTexture = NULL;
//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load(path.c_str());
if (loadedSurface == NULL)
{
printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
}
else
{
//Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
if (newTexture == NULL)
{
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
//Get rid of old loaded surface
SDL_FreeSurface(loadedSurface);
}
return newTexture;
}
void LayoutMenus::setMode(const bool &mode){
save = mode;
}