Files
UnleashedRecomp-hedge-dev/UnleashedRecomp/gpu/imgui_snapshot.cpp
Skyth (Asilkan) c0897dd507 Options menu, achievements, and installer wizard. (#19)
* Implemented guest-to-host function pointers (WIP)

Co-Authored-By: Skyth (Asilkan) <19259897+blueskythlikesclouds@users.noreply.github.com>

* function: support more types for function pointers

* Initial options menu implementation.

* Improve options menu visuals.

* Draw fade on borders, center tabs better.

* Adjust line sizes, fix tab text centering.

* Adjust padding & text sizes.

* Fix bar dark gradient effect.

* api: ported BlueBlur headers and misc. research

* Fix config name padding not getting scaled at different resolutions.

* config: use string_view, added method to get value pointer

* config: use std::map for reverse enum template

* Draw config options manually instead of looping through them.

* config: implemented name and enum localisation

* config_detail: move implementation to cpp, relocate sources

* Implemented accessing options menu via pause and title screen

* config: replace MSAA with AntiAliasing enum

* options_menu: implemented info panel and text marquee (see TODOs)

* Draw selection triangles.

* Supersample fonts to 2K.

* Implement options menu navigation.

* Fix duplicate triangles when selecting options.

* Draw scroll bar.

* Adjust scroll bar padding.

* Further scroll bar padding adjustments.

* Draw outer container as an outline.

* Improve marquee text scrolling.

* CTitleMenu: fix options menu re-entering on A press whilst visible

* Make procedural grid pattern more accurate.

* Add enum & bool editing.

* Update English localisation

* Fix input state mapping.

* options_menu: hide menu on Y hold

* CHudPause: fix crash when opening options menu from village/lab

* Implement float slider.

* options_menu: round res scale description resolution

* options_menu: use config callbacks after setting items

* api: fix GameObject layout

* camera_patches: implemented camera X/Y invert

* options_menu: fix buffered A press selecting first option upon entry

* config_locale: update description for Battle Music

* config: added Allow Background Input option

* options_menu: move ATOC option below Anti-Aliasing

* options_menu: only draw header/footer fade in stages

* Handle real-time modifications of some video config values.

* Converge increments only when holding the left/right button.

* Add sound effects to options menu.

* Change some sounds used in options menu.

* Give the final decide sound to bool toggling.

* Add option select animation.

* options_menu: only play slider sound between min/max range

* Apply category select animation.

* config: rename Controls category to Input

* Implement intro transition animation for options menu.

* audio_patches: implemented music volume

* Implement FPS slider.

* Prevent ImGui from displaying OS cursor.

* Fade container brackets during intro transition.

* player_patches: added penalty to Unleash Cancel

* config_locale: update English localisation

* player_patches: ensure Unleash gauge penalty doesn't dip into negatives

* options_menu: fix being unable to press A at least once after opening the menu

* CTitleMenu: added open/close sounds to the options menu

* audio_patches: implemented Music and SE volume

* api: update research

* Implemented music volume attenuation for Windows 10+

* api: fix score offset

* Add an interval between consecutive playbacks of the slider sound effect in fastIncrement mode

* config: implemented enum descriptions

* options_menu: fit thumbnail rect to grid, remove menu hide input

* options_menu: fix description wrap width

* camera_patches: fix FOV at narrow aspect ratios

mobile gaming is back on the menu

* options_menu: implemented greyed out options and localisation

* options_menu: allow providing reasons for greyed out options

* audio_patches: check if Windows version is supported

* Update PowerRecomp submodule

* api: more research

* options_menu: forget selected item upon opening

* options_menu: restrict XButtonHoming to title and world map

* window: always hide mouse cursor

The options menu doesn't accept mouse input, so there's not really any point to showing the cursor at all.

* Animate category tab background  from the center.

* Fix clip rect in info panel not getting popped at all times.

* Expose texture loader in "video.h".

* config: use final names and descriptions, label options to be moved to exports

* options_menu: implemented Voice Language (and some misc. clean-up)

* Move Voice Language patch to resident_patches

* config: added Aspect Ratio option (to be implemented)

* options_menu: implemented Subtitles

* Remove triple buffering from options menu, turn it to an enum.

* window: hide mouse cursor on controller input for windowed mode

* window: show window dimensions on title bar when resizing window

* api: update research

* Accept functions directly in GuestToHostFunction & add memory range asserts.

* Add guest_stack_var, improve shared_ptr implementation.

* Handle float/double arguments properly in GuestToHostFunction.

* CHudPause_patches: allocate options strings on stack

* api: update research

* guest_stack_var: allow creation without constructing underlying type

* memory: make assertions lenient towards nullptr

* api: include guest_stack_var in SWA.inl

* audio_patches: don't worry about it

* Implemented achievement overlay (WIP)

* Implemented achievements menu (WIP)

* Clean-up, improved animation and layouts

* options_menu: fix naming convention

* achievements_overlay: implemented queue and hermite interpolation

* achievements_menu: implemented animations and improved navigation

* achievements_menu: improve animation accuracy

* achievements_menu: added timestamps

* achievement_data: added checksum and format verification

* achievement_menu: improved outro animation

* achievement_menu: added total unlocked achievements

* achievement_menu: update sprite animation

* Update resources submodule

* Add installer wizard.

* Skip drawing scanlines when height is 0.

* Tweak install screen to better match the original

* Added arrow circle to installer's header

* Move icon header generation to resources submodule

* Added missing animations and tweaked other ones for installer

* Improve detection for DLC only mode. Add template for message prompts.

* Add language picker.

* window: update icon resources

* Added file_to_c

* Fixes to conversion.

* Implemented message window

* achievement_menu: use selection cursor texture

* Update embedded resources

* Implemented message window

* Merge branch 'bin2c' into options-menu

* Update embedded resources

* Framework for max width for buttons.

* Update embedded resources

* Use textures for pause menu containers

* audio_patches: check if Windows major version is >=10

Just in case.

* installer_wizard: use integer outline for button text

* Added arrow circle spinning animation during installation screen

* achievement_menu: fix timestamp and scroll bar padding

* achievement_overlay: fix achievement name padding

* installer_wizard: fix arrow circle spinning animation misaligning

* Add Scale and Origin to ImGui shaders. Change text to be squashed.

* message_window: implemented mouse input

* installer_wizard: implemented message windows

* achievement_menu: start marquee before timestamp margin

* Fix message box flow.

* message_window: use pause container texture

* Add extra condition for starting the installer.

* message_window: only accept mouse click if option is selected

* Implemented safer way to check if the game is loaded

* Add queued update when using files pickers.

* installer_wizard: implement localisation

* installer_wizard: use enum for localisation

* message_window: fix visibility persisting after window closes

* Fix arrow circle animation and added pulse animation

* Come back check space.

* Implement ZSTD compression in file_to_c.

* Add fade-in/out to installation icons and sleep after hitting 100%

* Implement ImGui font atlas caching.

* Controller navigation.

* Implemented button guide

* CTitleStateMenu: fix start button opening old options menu

* Update resources submodule

* imgui_snapshot: check if game is loaded before accessing XDBF

* message_window: added button guide

* options_menu: increase button guide side margins

* video: disable imgui.ini creation

* Use IM_DELETE for deleting the existing font atlas.

* Remove redundant FlushViewport call.

* Fix ImGui callbacks leaking memory.

* Replace unique_ptr reference arguments with raw pointers.

* Specialize description for resolution scale by reference.

---------

Co-authored-by: Hyper <34012267+hyperbx@users.noreply.github.com>
Co-authored-by: PTKay <jp_moura99@outlook.com>
Co-authored-by: Dario <dariosamo@gmail.com>
2024-12-06 18:52:06 +03:00

273 lines
8.8 KiB
C++

#include "imgui_snapshot.h"
#include <locale/locale.h>
#include <res/font/im_font_atlas.bin.h>
#include <user/config.h>
#include <decompressor.h>
#include <kernel/xdbf.h>
#include <app.h>
void ImDrawDataSnapshot::Clear()
{
for (int n = 0; n < Cache.GetMapSize(); n++)
if (ImDrawDataSnapshotEntry* entry = Cache.TryGetMapData(n))
IM_DELETE(entry->OurCopy);
Cache.Clear();
DrawData.Clear();
}
void ImDrawDataSnapshot::SnapUsingSwap(ImDrawData* src, double current_time)
{
ImDrawData* dst = &DrawData;
IM_ASSERT(src != dst && src->Valid);
// Copy all fields except CmdLists[]
ImVector<ImDrawList*> backup_draw_list;
backup_draw_list.swap(src->CmdLists);
IM_ASSERT(src->CmdLists.Data == NULL);
*dst = *src;
backup_draw_list.swap(src->CmdLists);
// Swap and mark as used
for (ImDrawList* src_list : src->CmdLists)
{
ImDrawDataSnapshotEntry* entry = GetOrAddEntry(src_list);
if (entry->OurCopy == NULL)
{
entry->SrcCopy = src_list;
entry->OurCopy = IM_NEW(ImDrawList)(src_list->_Data);
}
IM_ASSERT(entry->SrcCopy == src_list);
entry->SrcCopy->CmdBuffer.swap(entry->OurCopy->CmdBuffer); // Cheap swap
entry->SrcCopy->IdxBuffer.swap(entry->OurCopy->IdxBuffer);
entry->SrcCopy->VtxBuffer.swap(entry->OurCopy->VtxBuffer);
entry->SrcCopy->CmdBuffer.reserve(entry->OurCopy->CmdBuffer.Capacity); // Preserve bigger size to avoid reallocs for two consecutive frames
entry->SrcCopy->IdxBuffer.reserve(entry->OurCopy->IdxBuffer.Capacity);
entry->SrcCopy->VtxBuffer.reserve(entry->OurCopy->VtxBuffer.Capacity);
entry->LastUsedTime = current_time;
dst->CmdLists.push_back(entry->OurCopy);
}
// Cleanup unused data
const double gc_threshold = current_time - MemoryCompactTimer;
for (int n = 0; n < Cache.GetMapSize(); n++)
if (ImDrawDataSnapshotEntry* entry = Cache.TryGetMapData(n))
{
if (entry->LastUsedTime > gc_threshold)
continue;
IM_DELETE(entry->OurCopy);
Cache.Remove(GetDrawListID(entry->SrcCopy), entry);
}
};
template<typename T1, typename T2>
void ImFontAtlasSnapshot::SnapPointer(size_t offset, const T1& value, const T2& ptr, size_t count)
{
if (ptr != nullptr && count != 0)
{
if (!objects.contains(ptr))
{
constexpr size_t ALIGN = alignof(std::remove_pointer_t<T2>);
constexpr size_t SIZE = sizeof(std::remove_pointer_t<T2>);
size_t ptrOffset = (data.size() + ALIGN - 1) & ~(ALIGN - 1);
data.resize(ptrOffset + SIZE * count);
memcpy(&data[ptrOffset], ptr, SIZE * count);
for (size_t i = 0; i < count; i++)
{
size_t curPtrOffset = ptrOffset + SIZE * i;
objects[&ptr[i]] = curPtrOffset;
Traverse(curPtrOffset, ptr[i]);
}
}
size_t fieldOffset = offset + (reinterpret_cast<ptrdiff_t>(&ptr) - reinterpret_cast<ptrdiff_t>(&value));
*reinterpret_cast<size_t*>(&data[fieldOffset]) = objects[ptr];
offsets.push_back(fieldOffset);
}
}
template<typename T>
void ImFontAtlasSnapshot::Traverse(size_t offset, const T& value)
{
if constexpr (std::is_pointer_v<T>)
{
SnapPointer(offset, value, value, 1);
}
else if constexpr (std::is_same_v<T, ImFontAtlas>)
{
SnapPointer(offset, value, value.ConfigData.Data, value.ConfigData.Size);
SnapPointer(offset, value, value.CustomRects.Data, value.CustomRects.Size);
SnapPointer(offset, value, value.Fonts.Data, value.Fonts.Size);
}
else if constexpr (std::is_same_v<T, ImFont>)
{
SnapPointer(offset, value, value.IndexAdvanceX.Data, value.IndexAdvanceX.Size);
SnapPointer(offset, value, value.IndexLookup.Data, value.IndexLookup.Size);
SnapPointer(offset, value, value.Glyphs.Data, value.Glyphs.Size);
SnapPointer(offset, value, value.FallbackGlyph, 1);
SnapPointer(offset, value, value.ContainerAtlas, 1);
SnapPointer(offset, value, value.ConfigData, value.ConfigDataCount);
}
else if constexpr (std::is_same_v<T, ImFontAtlasCustomRect>)
{
SnapPointer(offset, value, value.Font, 1);
}
else if constexpr (std::is_same_v<T, ImFontConfig>)
{
SnapPointer(offset, value, value.GlyphRanges, value.GlyphRanges != nullptr ? wcslen(reinterpret_cast<const wchar_t*>(value.GlyphRanges)) + 1 : 0);
SnapPointer(offset, value, value.DstFont, 1);
}
}
template<typename T>
size_t ImFontAtlasSnapshot::Snap(const T& value)
{
size_t offset = (data.size() + alignof(T) - 1) & ~(alignof(T) - 1);
data.resize(offset + sizeof(T));
memcpy(&data[offset], &value, sizeof(T));
objects[&value] = offset;
Traverse(offset, value);
return offset;
}
struct ImFontAtlasSnapshotHeader
{
uint32_t imguiVersion;
uint32_t dataOffset;
uint32_t offsetCount;
uint32_t offsetsOffset;
};
void ImFontAtlasSnapshot::Snap()
{
data.resize(sizeof(ImFontAtlasSnapshotHeader));
size_t dataOffset = Snap(*ImGui::GetIO().Fonts);
size_t offsetsOffset = data.size();
std::sort(offsets.begin(), offsets.end());
data.insert(data.end(),
reinterpret_cast<uint8_t*>(offsets.data()),
reinterpret_cast<uint8_t*>(offsets.data() + offsets.size()));
auto header = reinterpret_cast<ImFontAtlasSnapshotHeader*>(data.data());
header->imguiVersion = IMGUI_VERSION_NUM;
header->dataOffset = dataOffset;
header->offsetCount = offsets.size();
header->offsetsOffset = offsetsOffset;
}
static std::unique_ptr<uint8_t[]> g_imFontAtlas;
ImFontAtlas* ImFontAtlasSnapshot::Load()
{
g_imFontAtlas = decompressZstd(g_im_font_atlas, g_im_font_atlas_uncompressed_size);
auto header = reinterpret_cast<ImFontAtlasSnapshotHeader*>(g_imFontAtlas.get());
assert(header->imguiVersion == IMGUI_VERSION_NUM && "ImGui version mismatch, the font atlas needs to be regenerated!");
auto offsetTable = reinterpret_cast<uint32_t*>(g_imFontAtlas.get() + header->offsetsOffset);
for (size_t i = 0; i < header->offsetCount; i++)
{
*reinterpret_cast<size_t*>(g_imFontAtlas.get() + (*offsetTable)) += reinterpret_cast<size_t>(g_imFontAtlas.get());
++offsetTable;
}
return reinterpret_cast<ImFontAtlas*>(g_imFontAtlas.get() + header->dataOffset);
}
static void GetGlyphs(std::set<ImWchar>& glyphs, const std::string_view& value)
{
const char* cur = value.data();
while (cur < value.data() + value.size())
{
unsigned int c;
cur += ImTextCharFromUtf8(&c, cur, value.data() + value.size());
glyphs.emplace(c);
}
}
static std::vector<ImWchar> g_glyphRanges;
void ImFontAtlasSnapshot::GenerateGlyphRanges()
{
std::vector<std::string_view> localeStrings;
for (auto& config : Config::Definitions)
config->GetLocaleStrings(localeStrings);
std::set<ImWchar> glyphs;
for (size_t i = 0x20; i <= 0xFF; i++)
glyphs.emplace(i);
for (auto& localeString : localeStrings)
GetGlyphs(glyphs, localeString);
for (auto& [name, locale] : g_locale)
{
for (auto& [language, value] : locale)
GetGlyphs(glyphs, value);
}
for (auto& [language, locale] : g_bool_locale)
{
for (auto& [value, nameAndDesc] : locale)
{
GetGlyphs(glyphs, std::get<0>(nameAndDesc));
GetGlyphs(glyphs, std::get<1>(nameAndDesc));
}
}
if (g_isGameLoaded)
{
for (size_t i = XDBF_LANGUAGE_ENGLISH; i <= XDBF_LANGUAGE_ITALIAN; i++)
{
auto achievements = g_xdbfWrapper.GetAchievements(static_cast<EXDBFLanguage>(i));
for (auto& achievement : achievements)
{
GetGlyphs(glyphs, achievement.Name);
GetGlyphs(glyphs, achievement.UnlockedDesc);
GetGlyphs(glyphs, achievement.LockedDesc);
}
}
}
for (auto glyph : glyphs)
{
if (g_glyphRanges.empty() || (g_glyphRanges.back() + 1) != glyph)
{
g_glyphRanges.push_back(glyph);
g_glyphRanges.push_back(glyph);
}
else
{
g_glyphRanges.back() = glyph;
}
}
g_glyphRanges.push_back(0);
}
ImFont* ImFontAtlasSnapshot::GetFont(const char* name, float size)
{
auto fontAtlas = ImGui::GetIO().Fonts;
for (auto& configData : fontAtlas->ConfigData)
{
if (strstr(configData.Name, name) != nullptr && abs(configData.SizePixels - size) < 0.001f)
{
assert(configData.DstFont != nullptr);
return configData.DstFont;
}
}
#ifdef ENABLE_IM_FONT_ATLAS_SNAPSHOT
assert(false && "Unable to locate equivalent font in the atlas file.");
#endif
return fontAtlas->AddFontFromFileTTF(name, size, nullptr, g_glyphRanges.data());
}