summaryrefslogtreecommitdiff
path: root/include/SSVOpenHexagon/Core/MenuGame.hpp
blob: 2143f0ce65a172df80f34309761f134721904ad6 (plain)
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
// Copyright (c) 2013-2020 Vittorio Romeo
// License: Academic Free License ("AFL") v. 3.0
// AFL License page: https://opensource.org/licenses/AFL-3.0

#pragma once

#include "SSVOpenHexagon/Core/HexagonDialogBox.hpp"

#include "SSVOpenHexagon/Data/StyleData.hpp"
#include "SSVOpenHexagon/Data/LevelData.hpp"
#include "SSVOpenHexagon/Data/LevelStatus.hpp"

#include "SSVOpenHexagon/Utils/Clock.hpp"
#include "SSVOpenHexagon/Utils/FastVertexVector.hpp"
#include "SSVOpenHexagon/Utils/LuaWrapper.hpp"
#include "SSVOpenHexagon/Utils/UniquePtr.hpp"

#include <SSVStart/Camera/Camera.hpp>

#include <SSVMenuSystem/SSVMenuSystem.hpp>

#include <SSVUtils/Core/Common/Frametime.hpp>

#include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Text.hpp>
#include <SFML/Graphics/Texture.hpp>

#include <SFML/System/Vector2.hpp>

#include <array>
#include <chrono>
#include <cctype>
#include <functional>
#include <memory>
#include <optional>
#include <string_view>
#include <string>
#include <utility>
#include <vector>

namespace ssvs {
class GameWindow;
}

namespace ssvs::Input {
class Trigger;
}

namespace hg {

class HGAssets;
class Audio;
class HexagonGame;
class HexagonClient;
class LeaderboardCache;
class ProfileData;

struct PackData;
struct PackInfo;
struct LoadInfo;

namespace Steam {
class steam_manager;
}

namespace Discord {
class discord_manager;
}

enum class States
{
    ETLPNewBoot,
    LoadingScreen,
    EpilepsyWarning,
    SLPSelectBoot,
    SMain,
    LevelSelection,
    MOpts,
    MOnline,
    SLPSelect,
    ETLPNew
};

class MenuGame
{
public:
    //---------------------------------------
    // Hexagon game callbacks (to avoid physical dependency)
    std::function<void(const ssvs::Input::Trigger&, int)> fnHGTriggerRefresh;

    std::function<void(
        const std::string&, const std::string&, bool, float, bool)>
        fnHGNewGame;

    std::function<void()> fnHGUpdateRichPresenceCallbacks;

private:
    //---------------------------------------
    // Classes

    Steam::steam_manager& steamManager;
    Discord::discord_manager& discordManager;
    HGAssets& assets;
    sf::Font& openSquare;
    sf::Font& openSquareBold;
    Audio& audio;
    ssvs::GameState game;
    ssvs::GameWindow& window;
    HexagonClient& hexagonClient;
    HexagonDialogBox dialogBox;
    Utils::UniquePtr<LeaderboardCache> leaderboardCache;

    Lua::LuaContext lua;
    std::vector<std::string> execScriptPackPathContext;
    const PackData* currentPack;

    //---------------------------------------
    // Initialization

    void initAssets();
    void initInput();
    void initLua();
    void initMenus();
    void playLocally();

    [[nodiscard]] std::pair<const unsigned int, const unsigned int>
    pickRandomMainMenuBackgroundStyle();

    //---------------------------------------
    // Assets

    static constexpr std::array<const char*, 3> creditsIds{
        "creditsBar2.png", "creditsBar2b.png", "creditsBar2c.png"};

    sf::Sprite titleBar;
    sf::Sprite creditsBar1;
    sf::Sprite creditsBar2;
    sf::Sprite epilepsyWarning;


    //---------------------------------------
    // Online status bar

    sf::Sprite sOnline;
    sf::RectangleShape rsOnlineStatus;
    sf::Text txtOnlineStatus;

    void initOnlineIcons();

    //---------------------------------------
    // Text Entering

    std::vector<char> enteredChars;

    [[nodiscard]] bool isEnteringText() const noexcept;

    //---------------------------------------
    // Cameras
    ssvs::Camera backgroundCamera;
    ssvs::Camera overlayCamera;

    bool mustRefresh;

    //---------------------------------------
    // Navigation

    bool wasFocusHeld;
    bool focusHeld;
    float wheelProgress;
    float touchDelay;
    States state;
    int packChangeDirection;

    void leftAction();
    void rightAction();
    void upAction();
    void downAction();
    void okAction();
    void eraseAction();
    void exitAction();

    void changePackTo(const int idx);
    void changePack();
    void changePackQuick(const int direction);
    void changePackAction(const int direction);

    [[nodiscard]] ssvms::Menu* getCurrentMenu() noexcept;
    [[nodiscard]] bool isInMenu() noexcept;
    void ignoreInputsAfterMenuExec();

    //---------------------------------------
    // State changes

    void changeStateTo(const States mState);

    //---------------------------------------
    // Update

    LevelStatus levelStatus;
    int ignoreInputs;

    void update(ssvu::FT mFT);
    void setIndex(int mIdx);
    void refreshCamera();
    void reloadAssets(const bool reloadEntirePack);
    void setIgnoreAllInputs(const unsigned int presses);

    //---------------------------------------
    // Drawing

    float w;
    float h;
    int scrollbarOffset{0};
    bool fourByThree{false};
    ssvms::Menu welcomeMenu;
    ssvms::Menu mainMenu;
    ssvms::Menu optionsMenu;
    ssvms::Menu onlineMenu;
    ssvms::Menu profileSelectionMenu;
    const LevelData* levelData;
    StyleData styleData;

    // The height of the font is an important value necessary to properly
    // draw all the menus and it only changes when the resolution does.
    // So we only calculate it on boot and when the res changes and store it.
    struct MenuFont
    {
        sf::Text font;
        float height;

        void updateHeight();
    };

    MenuFont txtVersion;
    MenuFont txtProf;
    MenuFont txtLoadBig;
    MenuFont txtLoadSmall;
    MenuFont txtRandomTip;
    MenuFont txtMenuBig;
    MenuFont txtMenuSmall;
    MenuFont txtProfile;
    MenuFont txtInstructionsBig;
    MenuFont txtInstructionsMedium;
    MenuFont txtInstructionsSmall;
    MenuFont txtEnteringText;
    MenuFont txtSelectionBig;
    MenuFont txtSelectionMedium;
    MenuFont txtSelectionSmall;
    MenuFont txtSelectionScore;
    MenuFont txtSelectionRanked;
    sf::Color menuTextColor;
    sf::Color menuQuadColor;
    sf::Color menuSelectionColor;
    sf::Color dialogBoxTextColor;
    Utils::FastVertexVectorTris menuBackgroundTris;
    Utils::FastVertexVectorQuads menuQuads;

    // Mouse control
    HRTimePoint lastMouseClick{};
    bool mouseHovering{false};
    bool mouseWasPressed{false};
    bool mousePressed{false};
    bool mustFavorite{false};
    bool mustPlay{false};
    std::optional<int> mustChangeIndexTo;
    std::optional<int> mustChangePackIndexTo;
    std::optional<int> mustUseMenuItem;
    bool mouseCursorVisible{true};
    sf::Vector2i lastMouseMovedPosition{};

    std::string strBuf;

    void playSelectedLevel();

    void setMouseCursorVisible(const bool x);

    [[nodiscard]] bool isMouseCursorVisible() const;

    [[nodiscard]] bool overlayMouseOverlap(
        const sf::Vector2f& mins, const sf::Vector2f& maxs) const;

    [[nodiscard]] bool overlayMouseOverlapAndUpdateHover(
        const sf::Vector2f& mins, const sf::Vector2f& maxs);

    [[nodiscard]] sf::Color mouseOverlapColor(
        const bool mouseOverlap, const sf::Color& c) const;

    [[nodiscard]] bool mouseLeftRisingEdge() const;

    void draw();
    void render(sf::Drawable& mDrawable);

    // Helper functions
    [[nodiscard]] float getFPSMult() const;

    void drawGraphics();

    void drawOnlineStatus();

    void adjustMenuOffset(const bool resetMenuOffset);

    [[nodiscard]] float calcMenuOffset(float& offset, const float maxOffset,
        const bool revertOffset, const bool speedUp = false);

    void calcMenuItemOffset(float& offset, bool selected);

    void createQuad(
        const sf::Color& color, float x1, float x2, float y1, float y2);

    void createQuad(const sf::Color& color, const sf::Vector2f& mins,
        const sf::Vector2f& maxs);

    void createQuad(const sf::Color& color, const sf::FloatRect& rect);

    void createQuadTrapezoid(const sf::Color& color, float x1, float x2,
        float x3, float y1, float y2, bool left);

    [[nodiscard]] std::pair<int, int> getScrollbarNotches(
        const int size, const int maxSize) const;

    void drawScrollbar(const float totalHeight, const int size,
        const int notches, const float x, const float y,
        const sf::Color& color);

    void drawMainSubmenus(
        const std::vector<std::unique_ptr<ssvms::Category>>& subMenus,
        const float indent);

    void drawSubmenusSmall(
        const std::vector<std::unique_ptr<ssvms::Category>>& subMenus,
        const float indent);

    // Load menu
    LoadInfo& loadInfo;
    std::array<std::string_view, 2> randomTip;
    float hexagonRotation;
    void drawLoadResults();

    // Main menu
    float menuHalfHeight;

    void drawMainMenu(
        ssvms::Category& mSubMenu, float baseIndent, const bool revertOffset);

    // Options menu
    void drawOptionsSubmenus(
        ssvms::Category& mSubMenu, float baseIndent, const bool revertOffset);

    // Profiles Menu
    std::string formatSurvivalTime(ProfileData* data);
    void drawProfileSelection(const float xOffset, const bool revertOffset);
    void drawProfileSelectionBoot();

    // Entering text menu
    float enteringTextOffset;
    void drawEnteringText(const float xOffset, const bool revertOffset);
    void drawEnteringTextBoot();

    // Level selection menu
    enum class PackChange
    {
        Rest,
        Folding,
        Stretching
    };

    enum class Label
    {
        LevelName,
        PackName,
        PackAuthor,
        MusicName,
        MusicAuthor,
        MusicAlbum,
        ScrollsSize
    };

    // To keep data of the regular level selection and favorites separated.
    struct LevelDrawer
    {
        int packIdx{0};
        int currentIndex{0};

        // Pointer to avoid heavy copy loads.
        const std::vector<std::string>* levelDataIds;

        float XOffset{0.f};   // to make the menu slide in/out
        float YOffset{0.f};   // to scroll up and down the menu
        float YScrollTo{0.f}; // height list must scroll to show current item
        std::vector<float> lvlOffsets; // xOffset of the single level labels

        bool isFavorites{false};
    };

    bool isLevelFavorite;
    std::vector<std::string> favoriteLevelDataIds;
    LevelDrawer lvlSlct;
    LevelDrawer favSlct;
    LevelDrawer* lvlDrawer;

    void changeFavoriteLevelsToProfile();
    [[nodiscard]] bool isFavoriteLevels() const;
    [[nodiscard]] std::size_t getSelectablePackInfosSize() const;
    [[nodiscard]] const PackInfo& getNthSelectablePackInfo(const std::size_t i);

    int diffMultIdx{0};
    bool firstLevelSelection{true};
    PackChange packChangeState{PackChange::Rest};
    float namesScroll[static_cast<int>(Label::ScrollsSize)]{0};
    std::vector<std::string> levelDescription;
    float textToQuadBorder{0.f};
    float slctFrameSize{0.f};
    float packLabelHeight{0.f};
    float levelLabelHeight{0.f};
    float packChangeOffset{0.f}; // level list yOffset when being fold
    float levelDetailsOffset{0.f};
    static inline constexpr float baseScrollSpeed{30.f};
    float scrollSpeed{baseScrollSpeed};

    // Login at startup
    bool mustShowLoginAtStartup{true};
    void openLoginDialogBoxAndStartLoginProcess();

    // First timer tips
    bool showFirstTimeTips{false};
    bool mustShowFTTMainMenu{true};
    bool mustShowFTTLevelSelect{true};
    bool mustShowFTTDeathTips{true};
    float dialogBoxDelay{0.f};

    void addRemoveFavoriteLevel();
    void switchToFromFavoriteLevels();

    // Visual effects
    float difficultyBumpEffect{0.f};
    static inline constexpr float difficultyBumpEffectMax{24.f};

    void adjustLevelsOffset();
    void updateLevelSelectionDrawingParameters();

    [[nodiscard]] float getLevelSelectionHeight() const;
    [[nodiscard]] float getLevelListHeight() const;

    void calcScrollSpeed();
    void calcLevelChangeScroll(const int dir);
    void calcPackChangeScrollFold(const float mLevelListHeight);
    void calcPackChangeScrollStretch(const float mLevelListHeight);
    void quickPackFoldStretch();
    void scrollLevelListToTargetY(ssvu::FT mFT);

    void checkWindowTopScroll(
        const float scroll, std::function<void(const float)> action);

    void checkWindowBottomScroll(
        const float scroll, std::function<void(const float)> action);

    void scrollName(std::string& text, float& scroller);

    void scrollNameRightBorder(std::string& text, const std::string key,
        sf::Text& font, float& scroller, float border);

    void scrollNameRightBorder(
        std::string& text, sf::Text& font, float& scroller, const float border);

    void resetNamesScrolls();

    void resetLevelNamesScrolls();

    [[nodiscard]] float getMaximumTextWidth() const;

    void formatLevelDescription();

    void drawLevelSelectionRightSide(
        LevelDrawer& drawer, const bool revertOffset);

    void drawLevelSelectionLeftSide(
        LevelDrawer& drawer, const bool revertOffset);

    // Text rendering
    void renderText(
        const std::string& mStr, sf::Text& mText, const sf::Vector2f& mPos);

    void renderText(const std::string& mStr, sf::Text& mText,
        const sf::Vector2f& mPos, const sf::Color& mColor);

    void renderText(const std::string& mStr, sf::Text& mText,
        const unsigned int mSize, const sf::Vector2f& mPos);

    void renderText(const std::string& mStr, sf::Text& mText,
        const unsigned int mSize, const sf::Vector2f& mPos,
        const sf::Color& mColor);

    // Text rendering centered
    void renderTextCentered(
        const std::string& mStr, sf::Text& mText, const sf::Vector2f& mPos);

    void renderTextCentered(const std::string& mStr, sf::Text& mText,
        const sf::Vector2f& mPos, const sf::Color& mColor);

    void renderTextCentered(const std::string& mStr, sf::Text& mText,
        const unsigned int mSize, const sf::Vector2f& mPos);

    void renderTextCentered(const std::string& mStr, sf::Text& mText,
        const unsigned int mSize, const sf::Vector2f& mPos,
        const sf::Color& mColor);

    // Text rendering centered with an offset
    void renderTextCenteredOffset(const std::string& mStr, sf::Text& mText,
        const sf::Vector2f& mPos, const float xOffset);

    void renderTextCenteredOffset(const std::string& mStr, sf::Text& mText,
        const sf::Vector2f& mPos, const float xOffset, const sf::Color& mColor);

    //---------------------------------------
    // Misc / Unused

    std::string scoresMessage;
    float exitTimer{0}, currentCreditsId{0};
    bool mustTakeScreenshot{false};
    std::string currentLeaderboard, enteredStr, leaderboardString;

    void runLuaFile(const std::string& mFileName);
    void changeResolutionTo(unsigned int mWidth, unsigned int mHeight);
    void playSoundOverride(const std::string& assetId);

    [[nodiscard]] float getWindowWidth() const noexcept;

    [[nodiscard]] float getWindowHeight() const noexcept;

    //---------------------------------------
    // Input boxes

    enum class DialogInputState
    {
        Nothing,

        Registration_EnteringUsername,
        Registration_EnteringPassword,
        Registration_EnteringPasswordConfirm,

        Login_EnteringUsername,
        Login_EnteringPassword,

        DeleteAccount_EnteringPassword,
    };

    DialogInputState dialogInputState{DialogInputState::Nothing};

    std::string registrationUsername;
    std::string registrationPassword;
    std::string registrationPasswordConfirm;
    std::string loginUsername;
    std::string loginPassword;
    std::string deleteAccountPassword;

    void showDialogBox(const std::string& msg);
    void showInputDialogBox(const std::string& msg);
    void showInputDialogBoxNice(const std::string& title,
        const std::string& inputType, const std::string& extra = "");
    void showInputDialogBoxNiceWithDefault(const std::string& title,
        const std::string& inputType, const std::string& def,
        const std::string& extra = "");

public:
    MenuGame(Steam::steam_manager& mSteamManager,
        Discord::discord_manager& mDiscordManager, HGAssets& mAssets,
        Audio& mAudio, ssvs::GameWindow& mGameWindow,
        HexagonClient& mHexagonClient);

    ~MenuGame();

    void init(bool mErrored);
    void init(bool mErrored, const std::string& pack, const std::string& level);

    bool loadCommandLineLevel(
        const std::string& pack, const std::string& level);

    [[nodiscard]] ssvs::GameState& getGame() noexcept;

    void returnToLevelSelection();

    void refreshBinds();
};

} // namespace hg