#pragma once #include "anm2.h" #include "font.h" #include "kage_graphics.h" #include "persistent_game_data.h" #include #include #include #include #include #include #include #include #include constexpr int POPUP_BUTTON_COUNT = 2; constexpr int POPUP_RESULT_NONE = (int)0xfefefefe; constexpr int POPUP_LINE_TEXT = -1; constexpr int POPUP_NO_SELECTION = -1; constexpr float POPUP_SIZE_FIT = -1; constexpr uint32_t POPUP_TEXT_ARGB = 0xff372b2d; constexpr uint32_t POPUP_INACTIVE_ARGB = 0xffd3d3d3; constexpr uint32_t POPUP_WHITE_ARGB = 0xffffffff; // How achievement popups show (OptionsConfig::popupsMode). enum PopupsMode : int { POPUPS_OFF, POPUPS_LARGE, POPUPS_SMALL }; // Where the achievement overlay is in showing its current achievement. enum OverlayState : int { OVERLAY_HIDDEN, OVERLAY_APPEARING, OVERLAY_SHOWN, OVERLAY_DISAPPEARING }; // Whether a reset leaves the popup buttons below the screen or slides them in. enum ButtonsReset { BUTTONS_STAY_HIDDEN, BUTTONS_SLIDE_IN }; // What a popup draws besides its frame and text (MessagePopup::flags). enum PopupFlag : uint8_t { POPUP_DIM_SCREEN = 1 << 0, POPUP_SHADOW = 1 << 1, POPUP_BLOCK_MENUS = 1 << 2 }; // How a popup comes and goes. enum PopupAnimation : int { POPUP_ANIMATION_NONE, POPUP_ANIMATION_BOUNCE, POPUP_ANIMATION_FADE }; // The frame image a popup draws with. enum PopupStyle : int { POPUP_STYLE_WHITE, POPUP_STYLE_BEIGE }; // Where a line sits across its popup. enum PopupAlign : int { POPUP_ALIGN_LEFT, POPUP_ALIGN_CENTER, POPUP_ALIGN_RIGHT }; // Where a popup is in its life. enum PopupState : int { POPUP_NEW, POPUP_SHOWING, POPUP_SHOWN, POPUP_HIDING }; // Whether closing a popup waits until it has shown for its minimum time. enum PopupClose { CLOSE_UNFORCED, CLOSE_FORCED }; struct Popup; KColor kcolor_argb(uint32_t argb); using PopupCallback = void (*)(Popup& popup, void* context); using PopupCloseCallback = void (*)(int* result, void* userData); // A line of a popup: a text, or a choice when it has an id, which is the result choosing it closes the popup with. // A line with an inactive id shows only while the popup shows inactive lines, in its inactive color. // The game never gives one. struct PopupLine { KColor color = kcolor_argb(POPUP_TEXT_ARGB); Vector offset{}; std::string text; int id = POPUP_LINE_TEXT; int inactiveId = POPUP_LINE_TEXT; PopupAlign align = POPUP_ALIGN_CENTER; }; // Destruction (00420710) is the default. // What a popup shows and does (the descriptor PopupQueue::push copies): its lines, frame, placement and animation, // the callbacks it runs each frame, after drawing and when it closes, and the result backing out gives (-1 for none). // A size of -1 fits the lines, the width wrapping at maxWidthFraction of the screen. struct MessagePopup { KColor color = kcolor_argb(POPUP_WHITE_ARGB); KColor inactiveColor = kcolor_argb(POPUP_INACTIVE_ARGB); Vector offset{}; Vector size{}; std::vector entries; std::vector lines; Font* font = nullptr; PopupCallback renderCallback = nullptr; PopupCallback updateCallback = nullptr; PopupCloseCallback closeCallback = nullptr; void* closeUserData = nullptr; void* renderContext = nullptr; void* updateContext = nullptr; float animationSpeed = 1; float lineHeightScale = 1; float maxWidthFraction = 0.5f; float paddingX = 25; float paddingY = 15; PopupAnimation animation = POPUP_ANIMATION_BOUNCE; PopupStyle style = POPUP_STYLE_WHITE; int cancelResult = -1; int initialSelection = POPUP_NO_SELECTION; int layer = 3; int priority = 2; uint32_t minDisplayTime = 0; uint8_t flags = POPUP_DIM_SCREEN | POPUP_SHADOW | POPUP_BLOCK_MENUS; bool isInactiveShown = true; MessagePopup() = default; MessagePopup(Font* textFont, PopupCloseCallback callback, void* userData, Vector popupSize); void line_add(const PopupLine& line); void message_add(const char* header, const char* message); void ok_add(const char* message); void yes_no_add(const char* message); }; // A value tweened linearly over a duration in ms, from the target it last had to a new one. // The original leaves the start and duration unset until a tween starts, so a popup without an animation reads 1 ms. template struct PopupLerp { T from{}; T to{}; uint64_t startTime = 0; uint32_t duration = 1; void set(T value); void start(T target, uint32_t ms); float progress() const; T get() const; bool is_done() const; }; // Destruction (009c3410, 009c2d00) and copying the descriptor's entries (009c3070) are the defaults. // A popup on screen: its descriptor (lines laid out), where it is in showing, the line selected, the result waiting // for its minimum display time, and the tweens of its position, scale and alpha. struct Popup { MessagePopup desc; KAGE::Graphics::ImagePtr frameImage; PopupLerp position; PopupLerp scale; PopupLerp alpha; uint64_t fadeStartTime = 0; uint64_t showTime = 0; float backgroundAlpha = 0; PopupState state = POPUP_NEW; int animationStep = 0; int id = 0; int result = POPUP_RESULT_NONE; int selectedLine = POPUP_NO_SELECTION; inline static int nextId = 0; explicit Popup(const MessagePopup& source); Vector get_center() const; Vector get_size() const; uint64_t get_elapsed_ms() const; bool Update(bool isFirstInQueue); void set_state(PopupState newState); void show(); bool hide(); void close(int& closeResult, PopupClose mode); void select_move(int step); void build_lines(); void render_frame(KColor tint, Vector shift) const; void render() const; }; // Destruction (009c3290) is the default. // The popups on screen, kept by descending priority so the last one is on top and takes the input. // The lock lets callbacks reach the queue while it updates or draws. class PopupQueue { public: std::list> popups; std::recursive_mutex mutex; void Update(); void render_layer(int layer); Popup* find(int popupId); int push(const MessagePopup& desc); void close(int popupId, int result); bool is_blocking(); }; // One of the two button prompts in the bottom corners: its sprite, how far it has slid below its place toward its // target, the frame of its label and the action it shows the binding of. struct PopupButton { ANM2 sprite; Vector offset{}; float slideOffset = 0; float targetSlideOffset = 0; int buttonAction = 0; int textFrame = 0; bool isVisible = false; }; // The back and confirm button prompts (Manager's PopupButtons), drawn over everything with the active controller's // key names, and a callback run after them. struct PopupButtons { std::array buttons; void (*callback)(PopupButtons& popupButtons, void* context) = nullptr; void* callbackContext = nullptr; float scale = 1; void Init(); void reset(ButtonsReset mode = BUTTONS_STAY_HIDDEN); void load_graphics(); void Update(); void Render(); Vector get_position(int index) const; Vector get_label_position(int index) const; }; // An achievement as achievements.xml gives it: its text and its picture's path. struct AchievementEntry { std::string gfx; std::string name; int id = 0; }; // Destruction (009314c0) is the default. // The achievement unlocked popup: the achievements queued to show, each in turn appearing, shown for 90 frames (or // until confirm is held) and disappearing. Small popups show at a third of the size in the corner, without the paper. class AchievementOverlay { public: std::array entries; std::deque queue; ANM2 sprite; OverlayState state = OVERLAY_HIDDEN; int currentEntry = 0; int frame = 0; bool isPopupBlocked = false; void load_graphics(); bool load_xml(std::string_view path); void Update(); void Render(); }; int active_controller_get(); std::vector text_wrap_lines(std::string_view text, Font& font, float maxWidth);