forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_base.cpp
345 lines (287 loc) · 9.96 KB
/
window_base.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* 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 <iomanip>
#include <sstream>
#include "window_base.h"
#include "cache.h"
#include <lcf/data.h>
#include "game_system.h"
#include "bitmap.h"
#include "font.h"
#include "player.h"
Window_Base::Window_Base(int x, int y, int width, int height, Drawable::Flags flags)
: Window(flags)
{
SetWindowskin(Cache::SystemOrBlack());
SetX(x);
SetY(y);
SetWidth(width);
SetHeight(height);
SetStretch(Main_Data::game_system->GetMessageStretch() == lcf::rpg::System::Stretch_stretch);
SetZ(Priority_Window);
}
bool Window_Base::InitMovement(int old_x, int old_y, int new_x, int new_y, int duration) {
current_frame = 0;
old_position[0] = old_x;
old_position[1] = old_y;
new_position[0] = new_x;
new_position[1] = new_y;
SetX(old_position[0]);
SetY(old_position[1]);
total_frames = 0;
if (old_x != new_x || old_y != new_y) {
total_frames = duration;
return true;
}
return false;
}
bool Window_Base::IsMovementActive() {
return total_frames > 0 && current_frame <= total_frames;
}
void Window_Base::Update() {
Window::Update();
if (IsSystemGraphicUpdateAllowed()) {
SetWindowskin(Cache::SystemOrBlack());
SetStretch(Main_Data::game_system->GetMessageStretch() == lcf::rpg::System::Stretch_stretch);
}
UpdateMovement();
}
void Window_Base::UpdateMovement() {
if (!IsMovementActive()) {
return;
}
current_frame++;
if (IsMovementActive()) {
SetX(old_position[0] + (new_position[0] - old_position[0]) * current_frame / total_frames);
SetY(old_position[1] + (new_position[1] - old_position[1]) * current_frame / total_frames);
} else {
SetX(new_position[0]);
SetY(new_position[1]);
}
}
void Window_Base::OnFaceReady(FileRequestResult* result, int face_index, int cx, int cy, bool flip) {
BitmapRef faceset = Cache::Faceset(result->file);
Rect src_rect(
(face_index % 4) * 48,
face_index / 4 * 48,
48,
48
);
if (flip) {
contents->FlipBlit(cx, cy, *faceset, src_rect, true, false, Opacity::Opaque());
}
else {
contents->Blit(cx, cy, *faceset, src_rect, 255);
}
}
// All these functions assume that the input is valid
void Window_Base::DrawFace(StringView face_name, int face_index, int cx, int cy, bool flip) {
if (face_name.empty()) { return; }
FileRequestAsync* request = AsyncHandler::RequestFile("FaceSet", face_name);
request->SetGraphicFile(true);
face_request_ids.push_back(request->Bind(&Window_Base::OnFaceReady, this, face_index, cx, cy, flip));
request->Start();
}
void Window_Base::DrawActorFace(const Game_Actor& actor, int cx, int cy) {
DrawFace(actor.GetFaceName(), actor.GetFaceIndex(), cx, cy);
}
void Window_Base::DrawActorName(const Game_Battler& actor, int cx, int cy) const {
contents->TextDraw(cx, cy, Font::ColorDefault, actor.GetName());
}
void Window_Base::DrawActorTitle(const Game_Actor& actor, int cx, int cy) const {
contents->TextDraw(cx, cy, Font::ColorDefault, actor.GetTitle());
}
void Window_Base::DrawActorClass(const Game_Actor& actor, int cx, int cy) const {
contents->TextDraw(cx, cy, Font::ColorDefault, actor.GetClassName());
}
void Window_Base::DrawActorLevel(const Game_Actor& actor, int cx, int cy) const {
// Draw LV-String
contents->TextDraw(cx, cy, 1, lcf::Data::terms.lvl_short);
// Draw Level of the Actor
contents->TextDraw(cx + (lcf::Data::system.easyrpg_max_level >= 100 ? 30 : 24), cy, Font::ColorDefault, std::to_string(actor.GetLevel()), Text::AlignRight);
}
void Window_Base::DrawActorState(const Game_Battler& actor, int cx, int cy) const {
// Unit has Normal state if no state is set
const lcf::rpg::State* state = actor.GetSignificantState();
if (!state) {
contents->TextDraw(cx, cy, Font::ColorDefault, lcf::Data::terms.normal_status);
} else {
contents->TextDraw(cx, cy, state->color, state->name);
}
}
void Window_Base::DrawActorExp(const Game_Actor& actor, int cx, int cy) const {
// Draw EXP-String
int width = 7;
if (actor.MaxExpValue() < 1000000) {
width = 6;
contents->TextDraw(cx, cy, 1, lcf::Data::terms.exp_short);
}
// Current Exp of the Actor
// ------/------
std::stringstream ss;
ss << std::setfill(' ') << std::setw(width) << actor.GetExpString();
// Delimiter
ss << '/';
// Exp for Level up
ss << std::setfill(' ') << std::setw(width) << actor.GetNextExpString();
contents->TextDraw(cx + (width == 6 ? 12 : 0), cy, Font::ColorDefault, ss.str(), Text::AlignLeft);
}
void Window_Base::DrawActorHp(const Game_Battler& actor, int cx, int cy, int digits, bool draw_max) const {
// Draw HP-String
contents->TextDraw(cx, cy, 1, lcf::Data::terms.hp_short);
// Draw Current HP of the Actor
cx += 12;
// Color: 0 okay, 4 critical, 5 dead
int color = GetValueFontColor(actor.GetHp(), actor.GetMaxHp(), true);
auto dx = digits * 6;
contents->TextDraw(cx + dx, cy, color, std::to_string(actor.GetHp()), Text::AlignRight);
if (!draw_max)
return;
// Draw the /
cx += dx;
contents->TextDraw(cx, cy, Font::ColorDefault, "/");
// Draw Max Hp
cx += 6;
contents->TextDraw(cx + dx, cy, Font::ColorDefault, std::to_string(actor.GetMaxHp()), Text::AlignRight);
}
void Window_Base::DrawActorSp(const Game_Battler& actor, int cx, int cy, int digits, bool draw_max) const {
// Draw SP-String
contents->TextDraw(cx, cy, 1, lcf::Data::terms.sp_short);
// Draw Current SP of the Actor
cx += 12;
// Color: 0 okay, 4 critical/empty
int color = GetValueFontColor(actor.GetSp(), actor.GetMaxSp(), false);
auto dx = digits * 6;
contents->TextDraw(cx + dx, cy, color, std::to_string(actor.GetSp()), Text::AlignRight);
if (!draw_max)
return;
// Draw the /
cx += dx;
contents->TextDraw(cx, cy, Font::ColorDefault, "/");
// Draw Max Sp
cx += 6;
contents->TextDraw(cx + dx, cy, Font::ColorDefault, std::to_string(actor.GetMaxSp()), Text::AlignRight);
}
void Window_Base::DrawActorParameter(const Game_Battler& actor, int cx, int cy, int type) const {
StringView name;
int value;
switch (type) {
case 0:
name = lcf::Data::terms.attack;
value = actor.GetAtk();
break;
case 1:
name = lcf::Data::terms.defense;
value = actor.GetDef();
break;
case 2:
name = lcf::Data::terms.spirit;
value = actor.GetSpi();
break;
case 3:
name = lcf::Data::terms.agility;
value = actor.GetAgi();
break;
default:
return;
}
// Draw Term
contents->TextDraw(cx, cy, 1, name);
// Draw Value
contents->TextDraw(cx + 78, cy, Font::ColorDefault, std::to_string(value), Text::AlignRight);
}
void Window_Base::DrawEquipmentType(const Game_Actor& actor, int cx, int cy, int type) const {
StringView name;
switch (type) {
case 0:
name = lcf::Data::terms.weapon;
break;
case 1:
if (actor.HasTwoWeapons()) {
name = lcf::Data::terms.weapon;
} else {
name = lcf::Data::terms.shield;
}
break;
case 2:
name = lcf::Data::terms.armor;
break;
case 3:
name = lcf::Data::terms.helmet;
break;
case 4:
name = lcf::Data::terms.accessory;
break;
default:
return;
}
contents->TextDraw(cx, cy, 1, name);
}
void Window_Base::DrawItemName(const lcf::rpg::Item& item, int cx, int cy, bool enabled) const {
int color = enabled ? Font::ColorDefault : Font::ColorDisabled;
contents->TextDraw(cx, cy, color, item.name);
}
void Window_Base::DrawSkillName(const lcf::rpg::Skill& skill, int cx, int cy, bool enabled) const {
int color = enabled ? Font::ColorDefault : Font::ColorDisabled;
contents->TextDraw(cx, cy, color, skill.name);
}
void Window_Base::DrawCurrencyValue(int money, int cx, int cy) const {
// This function draws right aligned because of the dynamic with of the
// gold output (cx and cy define the right border)
std::stringstream gold;
gold << money;
Rect gold_text_size = Text::GetSize(*Font::Default(), lcf::Data::terms.gold);
contents->TextDraw(cx, cy, 1, lcf::Data::terms.gold, Text::AlignRight);
contents->TextDraw(cx - gold_text_size.width, cy, Font::ColorDefault, gold.str(), Text::AlignRight);
}
void Window_Base::DrawGauge(const Game_Battler& actor, int cx, int cy, int alpha) const {
BitmapRef system2 = Cache::System2();
if (!system2) {
return;
}
bool full = actor.IsAtbGaugeFull();
// Which gauge (0 - 2)
int gauge_y = 32 + 2 * 16;
// Three components of the gauge
Rect gauge_left(0, gauge_y, 16, 16);
Rect gauge_center(16, gauge_y, 16, 16);
Rect gauge_right(32, gauge_y, 16, 16);
Rect dst_rect(cx + 16, cy, 25, 16);
contents->Blit(cx + 0, cy, *system2, gauge_left, alpha);
contents->Blit(cx + 16 + 25, cy, *system2, gauge_right, alpha);
contents->StretchBlit(dst_rect, *system2, gauge_center, alpha);
const auto atb = actor.GetAtbGauge();
const auto gauge_w = 25 * atb / actor.GetMaxAtbGauge();
if (gauge_w > 0) {
// Full or not full bar
Rect gauge_bar(full ? 64 : 48, gauge_y, 16, 16);
Rect bar_rect(cx + 16, cy, gauge_w, 16);
contents->StretchBlit(bar_rect, *system2, gauge_bar, alpha);
}
}
void Window_Base::DrawActorHpValue(const Game_Battler& actor, int cx, int cy) const {
contents->TextDraw(cx, cy, GetValueFontColor(actor.GetHp(), actor.GetMaxHp(), true), std::to_string(actor.GetHp()), Text::AlignRight);
}
void Window_Base::DrawActorSpValue(const Game_Battler& actor, int cx, int cy) const {
contents->TextDraw(cx, cy, GetValueFontColor(actor.GetSp(), actor.GetMaxSp(), false), std::to_string(actor.GetSp()), Text::AlignRight);
}
int Window_Base::GetValueFontColor(int have, int max, bool can_knockout) const {
if (can_knockout && have == 0) return Font::ColorKnockout;
if (max > 0 && (have <= max / 4)) return Font::ColorCritical;
return Font::ColorDefault;
}