forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_varlist.cpp
220 lines (210 loc) · 6.57 KB
/
window_varlist.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
219
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
// Headers
#include <sstream>
#include <iomanip>
#include "window_varlist.h"
#include "game_switches.h"
#include "game_variables.h"
#include "game_strings.h"
#include "bitmap.h"
#include <lcf/data.h>
#include "output.h"
#include <lcf/reader_util.h>
#include "game_party.h"
#include "game_map.h"
Window_VarList::Window_VarList(std::vector<std::string> commands) :
Window_Command(commands, 224, 10) {
SetX(0);
SetY(32);
SetHeight(176);
SetWidth(224);
}
Window_VarList::~Window_VarList() {
}
void Window_VarList::Refresh() {
contents->Clear();
for (int i = 0; i < 10; i++) {
DrawItemValue(i);
}
}
void Window_VarList::DrawItemValue(int index){
if (!DataIsValid(first_var+index)) {
return;
}
switch (mode) {
case eSwitch:
{
auto value = Main_Data::game_switches->Get(first_var + index);
auto font = (!value) ? Font::ColorCritical : Font::ColorDefault;
DrawItem(index, Font::ColorDefault);
contents->TextDraw(GetWidth() - 16, 16 * index + 2, font, value ? "[ON]" : "[OFF]", Text::AlignRight);
}
break;
case eVariable:
{
auto value = Main_Data::game_variables->Get(first_var + index);
auto font = (value < 0) ? Font::ColorCritical : Font::ColorDefault;
DrawItem(index, Font::ColorDefault);
contents->TextDraw(GetWidth() - 16, 16 * index + 2, font, std::to_string(value), Text::AlignRight);
}
break;
case eItem:
{
auto value = Main_Data::game_party->GetItemCount(first_var + index);
auto font = (value == 0) ? Font::ColorCritical : Font::ColorDefault;
DrawItem(index, Font::ColorDefault);
contents->TextDraw(GetWidth() - 16, 16 * index + 2, font, std::to_string(value), Text::AlignRight);
}
break;
case eTroop:
case eMap:
case eHeal:
case eCommonEvent:
case eMapEvent:
{
DrawItem(index, Font::ColorDefault);
contents->TextDraw(GetWidth() - 16, 16 * index + 2, Font::ColorDefault, "", Text::AlignRight);
}
break;
case eLevel:
{
auto value = Main_Data::game_party->GetActors()[first_var + index - 1]->GetLevel();
DrawItem(index, Font::ColorDefault);
contents->TextDraw(GetWidth() - 16, 16 * index + 2, Font::ColorDefault, std::to_string(value), Text::AlignRight);
}
break;
case eString:
{
auto value = ToString(Main_Data::game_strings->Get(first_var + index));
DrawItem(index, Font::ColorDefault);
if (value.empty()) {
contents->TextDraw(34, 16 * index + 2, Font::ColorDisabled, "undefined", Text::AlignLeft);
} else {
size_t pos = 0;
while ((pos = value.find("\n", pos)) != std::string::npos) {
value.replace(pos, 1, "\\n");
pos += 3;
}
if (value.length() > 28) {
value = value.substr(0, 25) + "...";
}
contents->TextDraw(34, 16 * index + 2, Font::ColorDefault, value, Text::AlignLeft);
}
}
break;
case eNone:
break;
}
}
void Window_VarList::UpdateList(int first_value){
static std::stringstream ss;
first_var = first_value;
int map_idx = 0;
if (mode == eMap) {
auto iter = std::lower_bound(lcf::Data::treemap.maps.begin(), lcf::Data::treemap.maps.end(), first_value,
[](const lcf::rpg::MapInfo& l, int r) { return l.ID < r; });
map_idx = iter - lcf::Data::treemap.maps.begin();
}
for (int i = 0; i < 10; i++){
if (!DataIsValid(first_var+i)) {
continue;
}
ss.str("");
ss << std::setfill('0') << std::setw(4) << (first_value + i) << ": ";
switch (mode) {
case eSwitch:
ss << Main_Data::game_switches->GetName(first_value + i);
break;
case eVariable:
ss << Main_Data::game_variables->GetName(first_value + i);
break;
case eItem:
ss << lcf::ReaderUtil::GetElement(lcf::Data::items, first_value+i)->name;
break;
case eTroop:
ss << lcf::ReaderUtil::GetElement(lcf::Data::troops, first_value+i)->name;
break;
case eMap:
if (map_idx < static_cast<int>(lcf::Data::treemap.maps.size())) {
auto& map = lcf::Data::treemap.maps[map_idx];
if (map.ID == first_value + i) {
ss << map.name;
++map_idx;
}
}
break;
case eHeal:
if (first_value + i == 1) {
ss << "Party";
} else {
auto* actor = Main_Data::game_party->GetActors()[first_value + i-2];
ss << actor->GetName() << " " << actor->GetHp() << " / " << actor->GetMaxHp();
}
break;
case eLevel:
if (first_value + i >= 1) {
auto* actor = Main_Data::game_party->GetActors()[first_value + i-1];
ss << actor->GetName();
}
break;
case eCommonEvent:
ss << lcf::ReaderUtil::GetElement(lcf::Data::commonevents, first_value+i)->name;
break;
case eMapEvent:
ss << Game_Map::GetEvent(first_value+i)->GetName();
break;
case eString:
default:
break;
}
this->SetItemText(i, ss.str());
}
}
void Window_VarList::SetMode(Mode mode, int max_length_strings) {
this->mode = mode;
this->max_length_strings = max_length_strings;
SetVisible((mode != eNone));
Refresh();
}
bool Window_VarList::DataIsValid(int range_index) {
switch (mode) {
case eSwitch:
return Main_Data::game_switches->IsValid(range_index);
case eVariable:
return Main_Data::game_variables->IsValid(range_index);
case eItem:
return range_index > 0 && range_index <= static_cast<int>(lcf::Data::items.size());
case eTroop:
return range_index > 0 && range_index <= static_cast<int>(lcf::Data::troops.size());
case eMap:
return range_index > 0 && range_index <= (lcf::Data::treemap.maps.size() > 0 ? lcf::Data::treemap.maps.back().ID : 0);
case eHeal:
return range_index > 0 && range_index <= static_cast<int>(Main_Data::game_party->GetActors().size()) + 1;
case eLevel:
return range_index > 0 && range_index <= static_cast<int>(Main_Data::game_party->GetActors().size());
case eCommonEvent:
return range_index > 0 && range_index <= static_cast<int>(lcf::Data::commonevents.size());
case eMapEvent:
return Game_Map::GetEvent(range_index) != nullptr;
case eString:
return range_index > 0 && range_index <= max_length_strings;
default:
break;
}
return false;
}