-
Notifications
You must be signed in to change notification settings - Fork 804
/
gmenu.cpp
383 lines (343 loc) · 9.75 KB
/
gmenu.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/**
* @file gmenu.cpp
*
* Implementation of the in-game navigation and interaction.
*/
#include "gmenu.h"
#include <algorithm>
#include <cstdint>
#include <optional>
#include "DiabloUI/ui_flags.hpp"
#include "control.h"
#include "controls/axis_direction.h"
#include "controls/controller_motion.h"
#include "engine.h"
#include "engine/clx_sprite.hpp"
#include "engine/load_cel.hpp"
#include "engine/render/clx_render.hpp"
#include "engine/render/text_render.hpp"
#include "options.h"
#include "stores.h"
#include "utils/language.h"
#include "utils/ui_fwd.h"
namespace devilution {
namespace {
// Width of the slider menu item, including the label.
constexpr int SliderItemWidth = 490;
// Horizontal dimensions of the slider value
constexpr int SliderValueBoxLeft = 16 + SliderItemWidth / 2;
constexpr int SliderValueBoxWidth = 287;
constexpr int SliderValueBorderWidth = 2;
constexpr int SliderValueLeft = SliderValueBoxLeft + SliderValueBorderWidth;
constexpr int SliderValueWidth = SliderValueBoxWidth - 2 * SliderValueBorderWidth;
constexpr int SliderValueHeight = 29;
constexpr int SliderValuePaddingTop = 10;
constexpr int SliderMarkerWidth = 27;
constexpr int SliderFillMin = SliderMarkerWidth / 2;
constexpr int SliderFillMax = SliderValueWidth - SliderMarkerWidth / 2 - 1;
constexpr int GMenuTop = 117;
constexpr int GMenuItemHeight = 45;
OptionalOwnedClxSpriteList optbar_cel;
OptionalOwnedClxSpriteList PentSpin_cel;
OptionalOwnedClxSpriteList option_cel;
OptionalOwnedClxSpriteList sgpLogo;
bool isDraggingSlider;
TMenuItem *sgpCurrItem;
int LogoAnim_tick;
uint8_t LogoAnim_frame;
void (*gmenu_current_option)();
int sgCurrentMenuIdx;
void GmenuUpDown(bool isDown)
{
if (sgpCurrItem == nullptr) {
return;
}
isDraggingSlider = false;
int i = sgCurrentMenuIdx;
if (sgCurrentMenuIdx != 0) {
while (i != 0) {
i--;
if (isDown) {
sgpCurrItem++;
if (sgpCurrItem->fnMenu == nullptr)
sgpCurrItem = &sgpCurrentMenu[0];
} else {
if (sgpCurrItem == sgpCurrentMenu)
sgpCurrItem = &sgpCurrentMenu[sgCurrentMenuIdx];
sgpCurrItem--;
}
if (sgpCurrItem->enabled()) {
if (i != 0)
PlaySFX(SfxID::MenuMove);
return;
}
}
}
}
void GmenuLeftRight(bool isRight)
{
if (!sgpCurrItem->isSlider())
return;
uint16_t step = sgpCurrItem->sliderStep();
if (isRight) {
if (step == sgpCurrItem->sliderSteps())
return;
step++;
} else {
if (step == 0)
return;
step--;
}
sgpCurrItem->setSliderStep(step);
sgpCurrItem->fnMenu(false);
}
int GmenuGetLineWidth(TMenuItem *pItem)
{
if (pItem->isSlider())
return SliderItemWidth;
return GetLineWidth(_(pItem->pszStr), GameFont46, 2);
}
void GmenuDrawMenuItem(const Surface &out, TMenuItem *pItem, int y)
{
int w = GmenuGetLineWidth(pItem);
if (pItem->isSlider()) {
int uiPositionX = GetUIRectangle().position.x;
ClxDraw(out, { SliderValueBoxLeft + uiPositionX, y + 40 }, (*optbar_cel)[0]);
const uint16_t step = pItem->dwFlags & 0xFFF;
const uint16_t steps = std::max<uint16_t>(pItem->sliderSteps(), 2);
const uint16_t pos = SliderFillMin + step * (SliderFillMax - SliderFillMin) / steps;
SDL_Rect rect = MakeSdlRect(SliderValueLeft + uiPositionX, y + SliderValuePaddingTop, pos, SliderValueHeight);
SDL_FillRect(out.surface, &rect, 205);
ClxDraw(out, { SliderValueLeft + pos - SliderMarkerWidth / 2 + uiPositionX, y + SliderValuePaddingTop + SliderValueHeight - 1 }, (*option_cel)[0]);
}
int x = (gnScreenWidth - w) / 2;
UiFlags style = pItem->enabled() ? UiFlags::ColorGold : UiFlags::ColorBlack;
DrawString(out, _(pItem->pszStr), Point { x, y },
{ .flags = style | UiFlags::FontSize46, .spacing = 2 });
if (pItem == sgpCurrItem) {
const ClxSprite sprite = (*PentSpin_cel)[PentSpn2Spin()];
ClxDraw(out, { x - 54, y + 51 }, sprite);
ClxDraw(out, { x + 4 + w, y + 51 }, sprite);
}
}
void GameMenuMove()
{
static AxisDirectionRepeater repeater;
const AxisDirection moveDir = repeater.Get(GetLeftStickOrDpadDirection(false));
if (moveDir.x != AxisDirectionX_NONE)
GmenuLeftRight(moveDir.x == AxisDirectionX_RIGHT);
if (moveDir.y != AxisDirectionY_NONE)
GmenuUpDown(moveDir.y == AxisDirectionY_DOWN);
}
bool GmenuMouseIsOverSlider()
{
int uiPositionX = GetUIRectangle().position.x;
if (MousePosition.x < SliderValueLeft + uiPositionX) {
return false;
}
if (MousePosition.x >= SliderValueLeft + SliderValueWidth + uiPositionX) {
return false;
}
return true;
}
int GmenuGetSliderFill()
{
return std::clamp(MousePosition.x - SliderValueLeft - GetUIRectangle().position.x, SliderFillMin, SliderFillMax);
}
} // namespace
TMenuItem *sgpCurrentMenu;
void gmenu_draw_pause(const Surface &out)
{
if (leveltype != DTYPE_TOWN)
RedBack(out);
if (sgpCurrentMenu == nullptr) {
DrawString(out, _("Pause"), { { 0, 0 }, { gnScreenWidth, GetMainPanel().position.y } },
{ .flags = UiFlags::FontSize46 | UiFlags::ColorGold | UiFlags::AlignCenter | UiFlags::VerticalCenter, .spacing = 2 });
}
}
void FreeGMenu()
{
sgpLogo = std::nullopt;
PentSpin_cel = std::nullopt;
option_cel = std::nullopt;
optbar_cel = std::nullopt;
}
void gmenu_init_menu()
{
LogoAnim_frame = 0;
sgpCurrentMenu = nullptr;
sgpCurrItem = nullptr;
gmenu_current_option = nullptr;
sgCurrentMenuIdx = 0;
isDraggingSlider = false;
if (HeadlessMode)
return;
if (gbIsHellfire)
sgpLogo = LoadCel("data\\hf_logo3", 430);
else
sgpLogo = LoadCel("data\\diabsmal", 296);
PentSpin_cel = LoadCel("data\\pentspin", 48);
option_cel = LoadCel("data\\option", SliderMarkerWidth);
optbar_cel = LoadCel("data\\optbar", SliderValueBoxWidth);
}
bool gmenu_is_active()
{
return sgpCurrentMenu != nullptr;
}
void gmenu_set_items(TMenuItem *pItem, void (*gmFunc)())
{
PauseMode = 0;
isDraggingSlider = false;
sgpCurrentMenu = pItem;
gmenu_current_option = gmFunc;
if (gmenu_current_option != nullptr) {
gmenu_current_option();
}
sgCurrentMenuIdx = 0;
if (sgpCurrentMenu != nullptr) {
for (int i = 0; sgpCurrentMenu[i].fnMenu != nullptr; i++) {
sgCurrentMenuIdx++;
}
}
// BUGFIX: OOB access when sgCurrentMenuIdx is 0; should be set to NULL instead. (fixed)
sgpCurrItem = sgCurrentMenuIdx > 0 ? &sgpCurrentMenu[sgCurrentMenuIdx - 1] : nullptr;
GmenuUpDown(true);
if (sgpCurrentMenu == nullptr)
SaveOptions();
}
void gmenu_draw(const Surface &out)
{
if (sgpCurrentMenu != nullptr) {
GameMenuMove();
if (gmenu_current_option != nullptr)
gmenu_current_option();
if (gbIsHellfire) {
const uint32_t ticks = SDL_GetTicks();
if ((int)(ticks - LogoAnim_tick) > 25) {
++LogoAnim_frame;
if (LogoAnim_frame >= 16)
LogoAnim_frame = 0;
LogoAnim_tick = ticks;
}
}
int uiPositionY = GetUIRectangle().position.y;
const ClxSprite sprite = (*sgpLogo)[LogoAnim_frame];
ClxDraw(out, { (gnScreenWidth - sprite.width()) / 2, 102 + uiPositionY }, sprite);
int y = 110 + uiPositionY;
TMenuItem *i = sgpCurrentMenu;
if (sgpCurrentMenu->fnMenu != nullptr) {
while (i->fnMenu != nullptr) {
GmenuDrawMenuItem(out, i, y);
i++;
y += GMenuItemHeight;
}
}
}
}
bool gmenu_presskeys(SDL_Keycode vkey)
{
if (sgpCurrentMenu == nullptr)
return false;
switch (vkey) {
case SDLK_KP_ENTER:
case SDLK_RETURN:
if (sgpCurrItem->enabled()) {
PlaySFX(SfxID::MenuMove);
sgpCurrItem->fnMenu(true);
}
break;
case SDLK_ESCAPE:
PlaySFX(SfxID::MenuMove);
gmenu_set_items(nullptr, nullptr);
break;
case SDLK_SPACE:
return false;
case SDLK_LEFT:
GmenuLeftRight(false);
break;
case SDLK_RIGHT:
GmenuLeftRight(true);
break;
case SDLK_UP:
GmenuUpDown(false);
break;
case SDLK_DOWN:
GmenuUpDown(true);
break;
default:
break;
}
return true;
}
bool gmenu_on_mouse_move()
{
if (!isDraggingSlider)
return false;
const uint16_t step = sgpCurrItem->sliderSteps() * (GmenuGetSliderFill() - SliderFillMin) / (SliderFillMax - SliderFillMin);
sgpCurrItem->setSliderStep(step);
sgpCurrItem->fnMenu(false);
return true;
}
bool gmenu_left_mouse(bool isDown)
{
if (!isDown) {
if (isDraggingSlider) {
isDraggingSlider = false;
return true;
}
return false;
}
if (sgpCurrentMenu == nullptr) {
return false;
}
const Point uiPosition = GetUIRectangle().position;
if (MousePosition.y >= GetMainPanel().position.y) {
return false;
}
if (MousePosition.y - (GMenuTop + uiPosition.y) < 0) {
return true;
}
int i = (MousePosition.y - (GMenuTop + uiPosition.y)) / GMenuItemHeight;
if (i >= sgCurrentMenuIdx) {
return true;
}
TMenuItem *pItem = &sgpCurrentMenu[i];
if (!pItem->enabled()) {
return true;
}
int w = GmenuGetLineWidth(pItem);
uint16_t screenWidth = GetScreenWidth();
if (MousePosition.x < screenWidth / 2 - w / 2) {
return true;
}
if (MousePosition.x > screenWidth / 2 + w / 2) {
return true;
}
sgpCurrItem = pItem;
PlaySFX(SfxID::MenuMove);
if (pItem->isSlider()) {
isDraggingSlider = GmenuMouseIsOverSlider();
gmenu_on_mouse_move();
} else {
sgpCurrItem->fnMenu(true);
}
return true;
}
void gmenu_slider_set(TMenuItem *pItem, int min, int max, int value)
{
assert(pItem);
uint16_t nSteps = std::max<uint16_t>(pItem->sliderSteps(), 2);
pItem->setSliderStep(((max - min - 1) / 2 + (value - min) * nSteps) / (max - min));
}
int gmenu_slider_get(TMenuItem *pItem, int min, int max)
{
uint16_t step = pItem->sliderStep();
uint16_t steps = std::max<uint16_t>(pItem->sliderSteps(), 2);
return min + (step * (max - min) + (steps - 1) / 2) / steps;
}
void gmenu_slider_steps(TMenuItem *pItem, int steps)
{
pItem->dwFlags &= 0xFF000FFF;
pItem->setSliderSteps(steps);
}
} // namespace devilution