forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene_file.cpp
325 lines (274 loc) · 9.6 KB
/
scene_file.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
/*
* 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 <algorithm>
#include <sstream>
#include <vector>
#include "baseui.h"
#include "cache.h"
#include <lcf/data.h>
#include "game_system.h"
#include "game_party.h"
#include "input.h"
#include <lcf/lsd/reader.h>
#include "player.h"
#include "scene_file.h"
#include "bitmap.h"
#include <lcf/reader_util.h>
#include "output.h"
#ifdef EMSCRIPTEN
# include <emscripten.h>
# include "platform/emscripten/interface.h"
#endif
constexpr int arrow_animation_frames = 20;
Scene_File::Scene_File(std::string message) :
message(message) {
}
std::unique_ptr<Sprite> Scene_File::MakeBorderSprite(int y) {
int border_height = 8;
auto bitmap = Bitmap::Create(MENU_WIDTH, border_height, Cache::System()->GetBackgroundColor());
auto sprite = std::unique_ptr<Sprite>(new Sprite());
sprite->SetVisible(true);
sprite->SetZ(Priority_Window + 1);
sprite->SetBitmap(bitmap);
sprite->SetX(Player::menu_offset_x);
sprite->SetY(y);
return sprite;
}
std::unique_ptr<Sprite> Scene_File::MakeArrowSprite(bool down) {
int sprite_width = 8;
int sprite_height = 8;
Rect rect = Rect(40, (down ? 16 : sprite_height), 16, sprite_height);
auto bitmap = Bitmap::Create(*(Cache::System()), rect);
auto sprite = std::unique_ptr<Sprite>(new Sprite());
sprite->SetVisible(false);
sprite->SetZ(Priority_Window + 2);
sprite->SetBitmap(bitmap);
sprite->SetX((MENU_WIDTH / 2) - sprite_width + Player::menu_offset_x);
sprite->SetY(down ? Player::screen_height - sprite_height : 32);
return sprite;
}
void Scene_File::CreateHelpWindow() {
help_window.reset(new Window_Help(Player::menu_offset_x, 0, MENU_WIDTH, 32));
help_window->SetText(message);
help_window->SetZ(Priority_Window + 1);
}
void Scene_File::PopulatePartyFaces(Window_SaveFile& win, int /* id */, lcf::rpg::Save& savegame) {
win.SetParty(savegame.title);
win.SetHasSave(true);
}
void Scene_File::UpdateLatestTimestamp(int id, lcf::rpg::Save& savegame) {
if (savegame.title.timestamp > latest_time) {
latest_time = savegame.title.timestamp;
latest_slot = id;
}
}
void Scene_File::PopulateSaveWindow(Window_SaveFile& win, int id) {
// Try to access file
std::stringstream ss;
ss << "Save" << (id <= 8 ? "0" : "") << (id + 1) << ".lsd";
std::string file = fs.FindFile(ss.str());
if (!file.empty()) {
// File found
auto save_stream = FileFinder::Save().OpenInputStream(file);
if (!save_stream) {
Output::Debug("Save {} read error", file);
win.SetCorrupted(true);
return;
}
std::unique_ptr<lcf::rpg::Save> savegame = lcf::LSD_Reader::Load(save_stream, Player::encoding);
if (savegame) {
PopulatePartyFaces(win, id, *savegame);
UpdateLatestTimestamp(id, *savegame);
} else {
Output::Debug("Save {} corrupted", file);
win.SetCorrupted(true);
}
}
}
void Scene_File::Start() {
CreateHelpWindow();
border_top = Scene_File::MakeBorderSprite(32);
// Refresh File Finder Save Folder
fs = FileFinder::Save();
for (int i = 0; i < Utils::Clamp<int32_t>(lcf::Data::system.easyrpg_max_savefiles, 3, 99); i++) {
std::shared_ptr<Window_SaveFile>
w(new Window_SaveFile(Player::menu_offset_x, 40 + i * 64, MENU_WIDTH, 64));
w->SetIndex(i);
w->SetZ(Priority_Window);
PopulateSaveWindow(*w, i);
w->Refresh();
file_windows.push_back(w);
}
border_bottom = Scene_File::MakeBorderSprite(Player::screen_height - 8);
up_arrow = Scene_File::MakeArrowSprite(false);
down_arrow = Scene_File::MakeArrowSprite(true);
index = latest_slot;
top_index = std::max(0, index - 2);
RefreshWindows();
for (auto& fw: file_windows) {
fw->Update();
}
std::vector<std::string> commands;
#ifdef EMSCRIPTEN
commands.emplace_back("Download Savegame");
commands.emplace_back("Upload Savegame");
#endif
extra_commands_window = std::make_unique<Window_Command>(commands);
extra_commands_window->SetZ(Priority_Window + 100);
extra_commands_window->SetVisible(false);
}
void Scene_File::RefreshWindows() {
for (int i = 0; i < (int)file_windows.size(); i++) {
Window_SaveFile *w = file_windows[i].get();
w->SetY(40 + (i - top_index) * 64);
w->SetActive(i == index);
w->Refresh();
}
}
void Scene_File::Refresh() {
for (int i = 0; i < Utils::Clamp<int32_t>(lcf::Data::system.easyrpg_max_savefiles, 3, 99); i++) {
Window_SaveFile *w = file_windows[i].get();
PopulateSaveWindow(*w, i);
w->Refresh();
}
}
void Scene_File::vUpdate() {
UpdateArrows();
if (IsWindowMoving()) {
for (auto& fw: file_windows) {
fw->Update();
}
return;
}
if (HandleExtraCommandsWindow()) {
return;
}
if (Input::IsTriggered(Input::CANCEL)) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cancel));
Scene::Pop();
} else if (Input::IsTriggered(Input::DECISION)) {
if (IsSlotValid(index)) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Decision));
Action(index);
}
else {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Buzzer));
}
} else if (Input::IsTriggered(Input::SHIFT)) {
#ifdef EMSCRIPTEN
extra_commands_window->SetX(SCREEN_TARGET_WIDTH - extra_commands_window->GetWidth() - 8);
extra_commands_window->SetY(file_windows[index]->GetY() + 8);
extra_commands_window->SetItemEnabled(0, file_windows[index]->IsValid() && file_windows[index]->HasParty());
extra_commands_window->SetVisible(true);
return;
#endif
}
int old_top_index = top_index;
int old_index = index;
int max_index = static_cast<int>(file_windows.size()) - 1;
if (Input::IsRepeated(Input::DOWN) || Input::IsTriggered(Input::SCROLL_DOWN)) {
if (Input::IsTriggered(Input::DOWN) || Input::IsTriggered(Input::SCROLL_DOWN)
|| index < max_index) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cursor));
index = (index + 1) % file_windows.size();
}
//top_index = std::max(top_index, index - 3 + 1);
}
if (Input::IsRepeated(Input::UP) || Input::IsTriggered(Input::SCROLL_UP)) {
if (Input::IsTriggered(Input::UP) || Input::IsTriggered(Input::SCROLL_UP)
|| index >= 1) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cursor));
index = (index + max_index) % file_windows.size();
}
//top_index = std::min(top_index, index);
}
if (Input::IsRepeated(Input::PAGE_DOWN) && index < max_index) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cursor));
index = (index + 3 <= max_index) ? index + 3 : max_index;
}
if (Input::IsRepeated(Input::PAGE_UP) && index >= 1) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cursor));
index = (index > 3) ? index - 3 : 0;
}
if (index > top_index + 2) {
MoveFileWindows((top_index + 2 - index) * 64, 7);
top_index = std::max(top_index, index - 3 + 1);
}
else if (index < top_index) {
MoveFileWindows((top_index - index) * 64, 7);
top_index = std::min(top_index, index);
}
//top_index = std::min(top_index, std::max(top_index, index - 3 + 1));
if (top_index != old_top_index || index != old_index)
RefreshWindows();
for (auto& fw: file_windows) {
fw->Update();
}
}
bool Scene_File::IsWindowMoving() const {
for (auto& fw: file_windows) {
if (fw->IsMovementActive()) {
return true;
}
}
return false;
}
void Scene_File::MoveFileWindows(int dy, int dt) {
for (auto& fw: file_windows) {
fw->InitMovement(fw->GetX(), fw->GetY(), fw->GetX(), fw->GetY() + dy, dt);
}
}
void Scene_File::UpdateArrows() {
int max_index = static_cast<int>(file_windows.size()) - 1;
bool show_up_arrow = (top_index > 0);
bool show_down_arrow = (top_index < max_index - 2);
if (show_up_arrow || show_down_arrow) {
arrow_frame = (arrow_frame + 1) % (arrow_animation_frames * 2);
}
bool arrow_visible = (arrow_frame < arrow_animation_frames);
up_arrow->SetVisible(show_up_arrow && arrow_visible);
down_arrow->SetVisible(show_down_arrow && arrow_visible);
}
bool Scene_File::HandleExtraCommandsWindow() {
if (!extra_commands_window->IsVisible()) {
return false;
}
extra_commands_window->Update();
#ifdef EMSCRIPTEN
if (Input::IsTriggered(Input::DECISION)) {
if (extra_commands_window->GetIndex() == 0) {
// Download
if (!extra_commands_window->IsItemEnabled(0)) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Buzzer));
return true;
}
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Decision));
Emscripten_Interface::DownloadSavegame(index + 1);
extra_commands_window->SetVisible(false);
} else {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Decision));
Emscripten_Interface::UploadSavegame(index + 1);
extra_commands_window->SetVisible(false);
}
} else if (Input::IsTriggered(Input::CANCEL)) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cancel));
extra_commands_window->SetVisible(false);
}
#endif
return true;
}