Skip to content

Commit

Permalink
Save settings
Browse files Browse the repository at this point in the history
  • Loading branch information
kynex7510 committed Jun 13, 2024
1 parent 5cd6af1 commit ffce6bd
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 29 deletions.
52 changes: 46 additions & 6 deletions src/DevTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,53 @@
#include <Geode/loader/Mod.hpp>
#include "ImGui.hpp"

template<>
struct matjson::Serialize<Settings> {
static Settings from_json(const matjson::Value& value) {
return Settings {
.GDInWindow = value["game_in_window"].as_bool(),
.attributesInTree = value["attributes_in_tree"].as_bool(),
.alwaysHighlight = value["always_highlight"].as_bool(),
.highlightLayouts = value["highlight_layouts"].as_bool(),
.arrowExpand = value["arrow_expand"].as_bool(),
.orderChildren = value["order_children"].as_bool(),
.advancedSettings = value["advanced_settings"].as_bool(),
.showMemoryViewer = value["show_memory_viewer"].as_bool(),
.theme = value["theme"].as_string(),
};
}

static matjson::Value to_json(const Settings& settings) {
auto obj = matjson::Object();
obj["game_in_window"] = settings.GDInWindow;
obj["attributes_in_tree"] = settings.attributesInTree;
obj["always_highlight"] = settings.alwaysHighlight;
obj["highlight_layouts"] = settings.highlightLayouts;
obj["arrow_expand"] = settings.arrowExpand;
obj["order_children"] = settings.orderChildren;
obj["advanced_settings"] = settings.advancedSettings;
obj["show_memory_viewer"] = settings.showMemoryViewer;
obj["theme"] = settings.theme;
return obj;
}

static bool is_json(matjson::Value const& val) {
return val.is_object();
}
};

$on_mod(DataSaved) { DevTools::get()->saveSettings(); }

DevTools* DevTools::get() {
static auto inst = new DevTools;
static auto inst = new DevTools();
return inst;
}

void DevTools::loadSettings() { m_settings = Mod::get()->getSavedValue<Settings>("settings"); }
void DevTools::saveSettings() { Mod::get()->setSavedValue("settings", m_settings); }

bool DevTools::shouldPopGame() const {
return m_visible && m_GDInWindow;
return m_visible && m_settings.GDInWindow;
}

bool DevTools::pausedGame() const {
Expand All @@ -29,7 +69,7 @@ bool DevTools::isSetup() const {
}

bool DevTools::shouldOrderChildren() const {
return m_orderChildren;
return m_settings.orderChildren;
}

CCNode* DevTools::getSelectedNode() const {
Expand Down Expand Up @@ -89,7 +129,7 @@ void DevTools::drawPages() {
&DevTools::drawSettings
);

if (m_advancedSettings) {
if (m_settings.advancedSettings) {
this->drawPage(
U8STR(FEATHER_SETTINGS " Advanced Settings###devtools/advanced/settings"),
&DevTools::drawAdvancedSettings
Expand All @@ -116,15 +156,15 @@ void DevTools::drawPages() {
);
}

if (m_showMemoryViewer) {
if (m_settings.showMemoryViewer) {
this->drawPage("Memory viewer", &DevTools::drawMemory);
}
}

void DevTools::draw(GLRenderCtx* ctx) {
if (m_visible) {
if (m_reloadTheme) {
applyTheme(m_theme);
applyTheme(m_settings.theme);
m_reloadTheme = false;
}

Expand Down
26 changes: 17 additions & 9 deletions src/DevTools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,27 @@ enum class HighlightMode {
Layout,
};

struct Settings {
bool GDInWindow = true;
bool attributesInTree = false;
bool alwaysHighlight = true;
bool highlightLayouts = false;
bool arrowExpand = false;
bool orderChildren = true;
bool advancedSettings = false;
bool showMemoryViewer = false;
std::string theme = DARK_THEME;
};

class DevTools {
protected:
bool m_visible = false;
bool m_setup = false;
bool m_reloadTheme = true;
bool m_GDInWindow = true;
bool m_attributesInTree = false;
bool m_alwaysHighlight = true;
bool m_shouldRelayout = false;
bool m_highlightLayouts = false;
bool m_arrowExpand = false;
bool m_advancedSettings = false;
bool m_showModGraph = false;
bool m_pauseGame = false;
bool m_orderChildren = true;
bool m_showMemoryViewer = false;
std::string m_theme = DARK_THEME;
Settings m_settings;
ImGuiID m_dockspaceID;
ImFont* m_defaultFont = nullptr;
ImFont* m_smallFont = nullptr;
Expand Down Expand Up @@ -71,8 +75,12 @@ class DevTools {

bool hasExtension(const std::string& ext) const;

DevTools() { loadSettings(); }

public:
static DevTools* get();
void loadSettings();
void saveSettings();

bool shouldUseGDWindow() const;

Expand Down
2 changes: 1 addition & 1 deletion src/pages/GeometryDash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ void DevTools::drawGD(GLRenderCtx* gdCtx) {
ImGui::IsWindowHovered() &&
getGDWindowRect().Contains(ImGui::GetMousePos());

if (m_highlightLayouts) {
if (m_settings.highlightLayouts) {
this->drawLayoutHighlights(CCDirector::get()->getRunningScene());
}

Expand Down
20 changes: 10 additions & 10 deletions src/pages/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,50 @@ void DevTools::drawSettings() {

// TODO: fix this option as it hasnt worked in a while lol
#if 0
ImGui::Checkbox("GD in Window", &m_GDInWindow);
ImGui::Checkbox("GD in Window", &m_settings.GDInWindow);
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip("Show GD inside a window when DevTools are open");
}
ImGui::Checkbox("Attributes in Tree", &m_attributesInTree);
ImGui::Checkbox("Attributes in Tree", &m_settings.attributesInTree);
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip("Show node attributes in the Tree");
}
#endif
ImGui::Checkbox("Highlight Nodes", &m_alwaysHighlight);
ImGui::Checkbox("Highlight Nodes", &m_settings.alwaysHighlight);
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip(
"Always highlight nodes when hovered in the Tree. "
"When disabled, you can highlight by pressing Shift."
);
}
ImGui::Checkbox("Highlight Layouts", &m_highlightLayouts);
ImGui::Checkbox("Highlight Layouts", &m_settings.highlightLayouts);
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip(
"Highlights the borders of all layouts applied to nodes"
);
}
ImGui::Checkbox("Arrow to Expand", &m_arrowExpand);
ImGui::Checkbox("Arrow to Expand", &m_settings.arrowExpand);
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip(
"If enabled, expanding nodes in the Tree only works with the arrow. "
"Makes selecting nodes less annoying."
);
}
ImGui::Checkbox("Order Node Children", &m_orderChildren);
ImGui::Checkbox("Order Node Children", &m_settings.orderChildren);
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip(
"When enabled (default behavior) node children are sorted by Z Order.\n"
"When disabled, children have the same order they do during init functions (maybe).\n"
"As a side effect to disabling this, things may render incorrectly."
);
}
ImGui::Checkbox("Advanced Settings", &m_advancedSettings);
ImGui::Checkbox("Advanced Settings", &m_settings.advancedSettings);
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip(
"Shows advanced settings. Mostly useful only for development of Geode itself."
);
}
ImGui::Checkbox("Show Memory Viewer", &m_showMemoryViewer);
ImGui::Checkbox("Show Memory Viewer", &m_settings.showMemoryViewer);
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip(
"Shows the memory viewer window."
Expand Down Expand Up @@ -167,11 +167,11 @@ void DevTools::drawSettings() {
ImGui::Separator();

ImGui::Text("Theme");
static auto SELECTED = static_cast<int>(getThemeIndex(m_theme));
static auto SELECTED = static_cast<int>(getThemeIndex(m_settings.theme));
if (ImGui::Combo("##devtools/theme", &SELECTED,
(ranges::join(getThemeOptions(), std::string(1, '\0')) + '\0').c_str()
)) {
m_theme = getThemeAtIndex(SELECTED);
m_settings.theme = getThemeAtIndex(SELECTED);
m_reloadTheme = true;
}
if (ImGui::IsItemHovered()) {
Expand Down
6 changes: 3 additions & 3 deletions src/pages/Tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void DevTools::drawTreeBranch(CCNode* node, size_t index) {
{
flags |= ImGuiTreeNodeFlags_Leaf;
}
if (m_arrowExpand)
if (m_settings.arrowExpand)
{
flags |= ImGuiTreeNodeFlags_OpenOnArrow;
}
Expand All @@ -58,11 +58,11 @@ void DevTools::drawTreeBranch(CCNode* node, size_t index) {
DevTools::get()->selectNode(node);
selected = true;
}
if (ImGui::IsItemHovered() && (m_alwaysHighlight || ImGui::IsKeyDown(ImGuiKey_ModShift))) {
if (ImGui::IsItemHovered() && (m_settings.alwaysHighlight || ImGui::IsKeyDown(ImGuiKey_ModShift))) {
DevTools::get()->highlightNode(node, HighlightMode::Hovered);
}
if (expanded) {
if (m_attributesInTree) {
if (m_settings.attributesInTree) {
this->drawNodeAttributes(node);
}
size_t i = 0;
Expand Down

0 comments on commit ffce6bd

Please sign in to comment.