Skip to content

Commit f5a88f7

Browse files
committed
Some hot-reload fixes
1 parent d97935e commit f5a88f7

8 files changed

Lines changed: 74 additions & 141 deletions

File tree

mod-src/include/ui.h

Lines changed: 0 additions & 118 deletions
This file was deleted.

mod-src/src/demo.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ void SetupUI() {
1616
chk.cvar = "gSkipIntro";
1717
chk.opts.checkbox.tooltip = "Skip the intro cutscene.";
1818
chk.opts.checkbox.default_val = true;
19-
C_AddMenuEntry("My Mod", "gMyModSidebar");
20-
C_AddSidebarEntry("My Mod", "General", 1);
21-
C_AddWidget("My Mod", "General", 1, "Draw Mod Hi", &chk);
19+
C_AddSidebarEntry("My Mod", 1);
20+
C_AddWidget("My Mod", 1, "Draw Mod Hi", &chk);
2221
}
2322

2423
void OnFrameUpdate(IEvent* event) {
@@ -40,6 +39,8 @@ MOD_INIT() {
4039
}
4140

4241
MOD_EXIT() {
42+
C_RemoveSidebarEntry("My Mod");
43+
4344
UNREGISTER_LISTENER(GameFrameUpdate, gFrameUpdateListenerID);
4445
UNREGISTER_LISTENER(RenderGamePost, gRenderGamePostListenerID);
4546
}

src/port/api/ui.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,26 @@ static UIWidgets::LabelPositions MapLabel(C_LabelPos pos) {
1717
return UIWidgets::LabelPositions::Near;
1818
}
1919

20-
extern "C" HM_API void C_AddMenuEntry(const char* label, const char* cvar) {
21-
GhostshipGui::mGhostshipMenu->AddMenuEntry(label, cvar);
20+
// extern "C" void C_AddMenuEntry(const char* label, const char* cvar) {
21+
// GhostshipGui::mGhostshipMenu->AddMenuEntry(label, cvar);
22+
// }
23+
24+
extern "C" void C_AddSidebarEntry(const char* label, int column) {
25+
GhostshipGui::mGhostshipMenu->AddSidebarEntry("Mods", label, column);
2226
}
2327

24-
extern "C" HM_API void C_AddSidebarEntry(const char* menu, const char* label, int column) {
25-
GhostshipGui::mGhostshipMenu->AddSidebarEntry(menu, label, column);
28+
// extern "C" void C_RemoveMenuEntry(const char* entryName) {
29+
// GhostshipGui::mGhostshipMenu->RemoveMenuEntry(entryName);
30+
// }
31+
32+
extern "C" void C_RemoveSidebarEntry(const char* sidebarName) {
33+
GhostshipGui::mGhostshipMenu->RemoveSidebarEntry("Mods", sidebarName);
2634
}
2735

28-
extern "C" HM_API void C_AddWidget(const char* section, const char* sidebar, int column, const char* label, C_WidgetConfig* config) {
36+
extern "C" void C_AddWidget(const char* sidebar, int column, const char* label, C_WidgetConfig* config) {
2937
if (!config) return;
3038

31-
WidgetPath path = { section, sidebar, static_cast<SectionColumns>(column) };
39+
WidgetPath path = { "Mods", sidebar, static_cast<SectionColumns>(column) };
3240

3341
// 1. Begin the Widget Builder Chain
3442
WidgetInfo& widget = GhostshipGui::mGhostshipMenu->AddWidget(path, label, static_cast<WidgetType>(config->type))

src/port/api/ui.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,7 @@
33
#include <stdint.h>
44

55
#ifdef __cplusplus
6-
#define EXTERN_C extern "C"
7-
#else
8-
#define EXTERN_C
9-
#endif
10-
11-
#if defined(__clang__)
12-
#define HM_API __attribute__((optnone))
13-
#elif defined(__GNUC__)
14-
#define HM_API __attribute__((optimize("O0")))
15-
#else
16-
#define HM_API
6+
extern "C" {
177
#endif
188

199
// ---------------------------------------------------------
@@ -121,6 +111,12 @@ typedef struct {
121111
// ---------------------------------------------------------
122112
// 4. The Wrapper Function
123113
// ---------------------------------------------------------
124-
EXTERN_C void C_AddMenuEntry(const char* label, const char* cvar);
125-
EXTERN_C void C_AddSidebarEntry(const char* menu, const char* label, int column);
126-
EXTERN_C void C_AddWidget(const char* section, const char* sidebar, int column, const char* label, C_WidgetConfig* config);
114+
// void C_AddMenuEntry(const char* label, const char* cvar);
115+
void C_AddSidebarEntry(const char* label, int column);
116+
void C_AddWidget(const char* sidebar, int column, const char* label, C_WidgetConfig* config);
117+
118+
// void C_RemoveMenuEntry(const char* entryName);
119+
void C_RemoveSidebarEntry(const char* sidebarName);
120+
#ifdef __cplusplus
121+
}
122+
#endif

src/port/ui/GhostshipMenu.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,28 @@ void GhostshipMenu::AddSidebarEntry(std::string sectionName, std::string sidebar
6060
menuEntries.at(sectionName).sidebarOrder.push_back(sidebarName);
6161
}
6262

63+
void GhostshipMenu::RemoveMenuEntry(std::string entryName) {
64+
assert(!entryName.empty());
65+
if (menuEntries.contains(entryName)) {
66+
menuEntries.at(entryName).markForDelete = true;
67+
}
68+
}
69+
70+
void GhostshipMenu::RemoveSidebarEntry(std::string sectionName, std::string sidebarName) {
71+
assert(!sectionName.empty());
72+
assert(!sidebarName.empty());
73+
auto& sidebars = menuEntries.at(sectionName).sidebars;
74+
// Delete directly
75+
if (sidebars.contains(sidebarName)) {
76+
sidebars.erase(sidebarName);
77+
auto& sidebarOrder = menuEntries.at(sectionName).sidebarOrder;
78+
sidebarOrder.erase(std::remove(sidebarOrder.begin(), sidebarOrder.end(), sidebarName), sidebarOrder.end());
79+
} else {
80+
// Mark for deletion if not found, will be deleted when encountered in Draw()
81+
menuEntries.at(sectionName).sidebars.at(sidebarName).markForDelete = true;
82+
}
83+
}
84+
6385
WidgetInfo& GhostshipMenu::AddWidget(WidgetPath& pathInfo, std::string widgetName, WidgetType widgetType) {
6486
assert(!widgetName.empty()); // Must be unique
6587
assert(menuEntries.contains(pathInfo.sectionName)); // Section/header must already exist
@@ -126,6 +148,7 @@ void GhostshipMenu::InitElement() {
126148
AddMenuRando();
127149
AddMenuAchievements();
128150
AddMenuDevTools();
151+
AddMenuEntry("Mods", CVAR_SETTING("Menu.ModsSidebarSection"));
129152

130153
if (CVarGetInteger(CVAR_SETTING("Menu.SidebarSearch"), 0)) {
131154
InsertSidebarSearch();

src/port/ui/GhostshipMenu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class GhostshipMenu : public Ship::Menu {
1818
void Draw() override;
1919

2020
void AddSidebarEntry(std::string sectionName, std::string sidbarName, uint32_t columnCount);
21+
void RemoveMenuEntry(std::string entryName);
22+
void RemoveSidebarEntry(std::string sectionName, std::string sidebarName);
2123
WidgetInfo& AddWidget(WidgetPath& pathInfo, std::string widgetName, WidgetType widgetType);
2224
void AddMenuSettings();
2325
void AddMenuEnhancements();

src/port/ui/Menu.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,11 @@ void Menu::DrawElement() {
818818
const char* sidebarCvar = menuEntries.at(headerIndex).sidebarCvar;
819819

820820
std::string sectionIndex = CVarGetString(sidebarCvar, "");
821+
822+
if (sectionIndex.empty()) {
823+
sectionIndex = menuEntries.at("Settings").sidebarOrder.at(0);
824+
}
825+
821826
if (!sidebar->contains(sectionIndex)) {
822827
sectionIndex = menuEntries.at(headerIndex).sidebarOrder.at(0);
823828
}
@@ -937,5 +942,19 @@ void Menu::DrawElement() {
937942
freshOpen = false;
938943
}
939944
ImGui::End();
945+
946+
if(menuEntries.contains(headerIndex) && menuEntries.at(headerIndex).markForDelete) {
947+
menuEntries.erase(headerIndex);
948+
std::erase(menuOrder, headerIndex);
949+
return;
950+
}
951+
952+
if(sidebar->contains(sectionIndex) && sidebar->at(sectionIndex).markForDelete) {
953+
sidebar->erase(sectionIndex);
954+
menuEntries.at(headerIndex).sidebarOrder.erase(
955+
std::remove(menuEntries.at(headerIndex).sidebarOrder.begin(), menuEntries.at(headerIndex).sidebarOrder.end(),
956+
sectionIndex),
957+
menuEntries.at(headerIndex).sidebarOrder.end());
958+
}
940959
}
941960
} // namespace Ship

src/port/ui/MenuTypes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ struct disabledInfo {
256256
struct SidebarEntry {
257257
uint32_t columnCount;
258258
std::vector<std::vector<WidgetInfo>> columnWidgets;
259+
bool markForDelete = false;
259260
};
260261

261262
// Contains entries for what's listed in the header at the top, including the name displayed on the top bar (label),
@@ -266,6 +267,7 @@ struct MainMenuEntry {
266267
const char* sidebarCvar;
267268
std::unordered_map<std::string, SidebarEntry> sidebars = {};
268269
std::vector<std::string> sidebarOrder = {};
270+
bool markForDelete = false;
269271
};
270272

271273
static const std::unordered_map<Ship::AudioBackend, const char*> audioBackendsMap = {

0 commit comments

Comments
 (0)