forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_selectable.cpp
240 lines (215 loc) · 6.9 KB
/
window_selectable.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
/*
* 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 "window_selectable.h"
#include "game_system.h"
#include "input.h"
#include "util_macro.h"
#include "bitmap.h"
constexpr int arrow_animation_frames = 20;
// Constructor
Window_Selectable::Window_Selectable(int ix, int iy, int iwidth, int iheight) :
Window_Base(ix, iy, iwidth, iheight) { }
void Window_Selectable::CreateContents() {
int w = std::max(0, width - border_x * 2);
int h = std::max(0, std::max(height - border_y * 2, GetRowMax() * menu_item_height));
SetContents(Bitmap::Create(w, h));
}
// Properties
int Window_Selectable::GetIndex() const {
return index;
}
void Window_Selectable::SetIndex(int nindex) {
index = min(nindex, item_max - 1);
if (active && help_window != NULL) {
UpdateHelp();
}
UpdateCursorRect();
}
int Window_Selectable::GetColumnMax() const {
return column_max;
}
void Window_Selectable::SetColumnMax(int ncolmax) {
column_max = ncolmax;
}
int Window_Selectable::GetRowMax() const {
return (item_max + column_max - 1) / column_max;
}
int Window_Selectable::GetTopRow() const {
return oy / menu_item_height;
}
void Window_Selectable::SetTopRow(int row) {
if (row < 0) row = 0;
if (row > GetRowMax() - 1) row = GetRowMax() - 1;
SetOy(row * menu_item_height);
}
int Window_Selectable::GetPageRowMax() const {
return (height - border_y * 2) / menu_item_height;
}
int Window_Selectable::GetPageItemMax() {
return GetPageRowMax() * column_max;
}
Rect Window_Selectable::GetItemRect(int index) {
Rect rect = Rect();
rect.width = (width / column_max - 16);
rect.x = (index % column_max * (rect.width + 16));
rect.height = menu_item_height - 4;
rect.y = index / column_max * menu_item_height + menu_item_height / 8;
return rect;
}
Window_Help* Window_Selectable::GetHelpWindow() {
return help_window;
}
void Window_Selectable::SetHelpWindow(Window_Help* nhelp_window) {
help_window = nhelp_window;
if (active && help_window != NULL) {
UpdateHelp();
}
}
void Window_Selectable::UpdateHelp() {
if (UpdateHelpFn && help_window != nullptr) {
UpdateHelpFn(*help_window, index);
}
}
// Update Cursor Rect
void Window_Selectable::UpdateCursorRect() {
int cursor_width = 0;
int x = 0;
if (index < 0) {
SetCursorRect(Rect());
return;
}
int row = index / column_max;
if (row < GetTopRow()) {
SetTopRow(row);
} else if (row > GetTopRow() + (GetPageRowMax() - 1)) {
SetTopRow(row - (GetPageRowMax() - 1));
}
cursor_width = (width / column_max - 16) + 8;
x = (index % column_max * (cursor_width + 8)) - 4;
int y = index / column_max * menu_item_height - oy;
SetCursorRect(Rect(x, y, cursor_width, menu_item_height));
}
void Window_Selectable::UpdateArrows() {
bool show_up_arrow = (GetTopRow() > 0);
bool show_down_arrow = (GetTopRow() < (GetRowMax() - GetPageRowMax()));
if (show_up_arrow || show_down_arrow) {
arrow_frame = (arrow_frame + 1) % (arrow_animation_frames * 2);
}
bool arrow_visible = (arrow_frame < arrow_animation_frames);
SetUpArrow(show_up_arrow && arrow_visible);
SetDownArrow(show_down_arrow && arrow_visible);
}
// Update
void Window_Selectable::Update() {
Window_Base::Update();
if (active && item_max > 0 && index >= 0) {
if (scroll_dir != 0) {
scroll_progress++;
SetOy(GetOy() + (menu_item_height * scroll_progress / 4 - menu_item_height * (scroll_progress - 1) / 4) * scroll_dir);
UpdateArrows();
if (scroll_progress < 4) {
return;
} else {
scroll_dir = 0;
scroll_progress = 0;
if (active && help_window != NULL) {
UpdateHelp();
}
UpdateCursorRect();
}
}
int old_index = index;
auto move_down = [&]() {
if (index < item_max - column_max || column_max == 1 ) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cursor));
index = (index + column_max) % item_max;
}
};
if (Input::IsTriggered(Input::DOWN) || Input::IsTriggered(Input::SCROLL_DOWN)) {
move_down();
} else if (Input::IsRepeated(Input::DOWN)) {
if (endless_scrolling || (index + column_max) % item_max > index) {
move_down();
}
}
auto move_up = [&]() {
if (index >= column_max || column_max == 1) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cursor));
index = (index - column_max + item_max) % item_max;
}
};
if (Input::IsTriggered(Input::UP) || Input::IsTriggered(Input::SCROLL_UP)) {
move_up();
} else if (Input::IsRepeated(Input::UP)) {
if (endless_scrolling || (index - column_max + item_max) % item_max < index) {
move_up();
}
}
// page up/down is limited to selectables with one column
if (column_max == 1) {
if (Input::IsRepeated(Input::PAGE_DOWN) && index < item_max - 1) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cursor));
int new_pos = index + GetPageRowMax();
index = (new_pos <= item_max - 1) ? new_pos : item_max - 1;
}
if (Input::IsRepeated(Input::PAGE_UP) && index > 0) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cursor));
int new_pos = index - GetPageRowMax();
index = (new_pos >= 0) ? new_pos : 0;
}
}
if (Input::IsRepeated(Input::RIGHT)) {
if (column_max >= wrap_limit && index < item_max - 1) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cursor));
index += 1;
}
}
if (Input::IsRepeated(Input::LEFT)) {
if (column_max >= wrap_limit && index > 0) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cursor));
index -= 1;
}
}
if (std::abs(index - old_index) <= column_max) {
int row = index / column_max;
if (row < GetTopRow() && old_index < item_max - 1) {
scroll_dir = -1;
return;
} else if (row > GetTopRow() + (GetPageRowMax() - 1) && old_index > 0) {
scroll_dir = 1;
return;
}
}
}
if (active && help_window != NULL) {
UpdateHelp();
}
UpdateCursorRect();
UpdateArrows();
}
// Set endless scrolling state
void Window_Selectable::SetEndlessScrolling(bool state) {
endless_scrolling = state;
}
// Set menu item height
void Window_Selectable::SetMenuItemHeight(int height) {
menu_item_height = height;
}
void Window_Selectable::SetSingleColumnWrapping(bool wrap) {
wrap_limit = wrap ? 1 : 2;
}