Skip to content

Commit 8c9a251

Browse files
committed
feat(settings): use treenode for sections
1 parent d239a78 commit 8c9a251

File tree

1 file changed

+121
-95
lines changed

1 file changed

+121
-95
lines changed

src/utils/settings.hpp

+121-95
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ static void OnRegisterOverlay(reshade::api::effect_runtime* runtime) {
227227
bool any_change = false;
228228
std::string last_section;
229229
std::string last_group;
230+
bool open_section = true;
231+
bool open_node = false;
232+
bool has_indent = false;
230233
for (auto* setting : *settings) {
231234
int styles_pushed = 0;
232235
if (setting->tint.has_value()) {
@@ -243,7 +246,10 @@ static void OnRegisterOverlay(reshade::api::effect_runtime* runtime) {
243246
ImGuiCol_Button,
244247
ImGuiCol_ButtonActive,
245248
ImGuiCol_ButtonHovered,
246-
ImGuiCol_TextSelectedBg
249+
ImGuiCol_TextSelectedBg,
250+
ImGuiCol_Header,
251+
ImGuiCol_HeaderHovered,
252+
ImGuiCol_HeaderActive,
247253
};
248254
for (const auto style : styles) {
249255
auto style_rgb = ImGui::GetStyleColorVec4(style);
@@ -257,115 +263,135 @@ static void OnRegisterOverlay(reshade::api::effect_runtime* runtime) {
257263
}
258264

259265
if (last_section != setting->section) {
260-
ImGui::SeparatorText(setting->section.c_str());
261266
last_section.assign(setting->section);
262-
}
263267

264-
if (!last_group.empty() && !setting->group.empty() && last_group == setting->group) {
265-
ImGui::SameLine();
266-
}
267-
last_group = setting->group;
268+
if (open_node) {
269+
// TreePop will call unindent
270+
ImGui::Indent();
271+
ImGui::TreePop();
272+
}
273+
open_node = ImGui::TreeNodeEx(
274+
setting->section.c_str(),
275+
ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_SpanFullWidth);
276+
if (open_node) {
277+
ImGui::Unindent();
278+
}
268279

269-
const bool is_disabled = preset_index == 0
270-
|| (setting->is_enabled != nullptr
271-
&& !setting->is_enabled());
272-
if (is_disabled) {
273-
ImGui::BeginDisabled();
280+
open_section = open_node;
274281
}
275-
bool changed = false;
276282

277-
switch (setting->value_type) {
278-
case SettingValueType::FLOAT:
279-
changed |= ImGui::SliderFloat(
280-
setting->label.c_str(),
281-
&setting->value,
282-
setting->min,
283-
setting->max,
284-
setting->format.c_str());
285-
break;
286-
case SettingValueType::INTEGER:
287-
changed |= ImGui::SliderInt(
288-
setting->label.c_str(),
289-
&setting->value_as_int,
290-
setting->min,
291-
setting->GetMax(),
292-
setting->labels.empty()
293-
? setting->format.c_str()
294-
: setting->labels.at(setting->value_as_int).c_str(),
295-
ImGuiSliderFlags_NoInput);
296-
break;
297-
case SettingValueType::BOOLEAN:
298-
changed |= ImGui::SliderInt(
299-
setting->label.c_str(),
300-
&setting->value_as_int,
301-
0,
302-
1,
303-
setting->labels.empty()
304-
? ((setting->value_as_int == 0) ? "Off" : "On") // NOLINT(readability-avoid-nested-conditional-operator)
305-
: setting->labels.at(setting->value_as_int).c_str(),
306-
ImGuiSliderFlags_NoInput);
307-
break;
308-
case SettingValueType::BUTTON: {
309-
changed |= ImGui::Button(setting->label.c_str());
283+
if (open_section) {
284+
if (!last_group.empty() && !setting->group.empty() && last_group == setting->group) {
285+
ImGui::SameLine();
310286
}
311-
}
312-
if (changed) {
313-
setting->on_change();
314-
}
315-
if (!setting->tooltip.empty()) {
316-
ImGui::SetItemTooltip("%s", setting->tooltip.c_str());
317-
}
318287

319-
if (preset_index != 0
320-
&& setting->can_reset
321-
&& setting->value_type != SettingValueType::BUTTON) {
322-
ImGui::SameLine();
323-
const bool is_using_default = (setting->GetValue() == setting->default_value);
324-
ImGui::BeginDisabled(is_using_default);
325-
ImGui::PushID(&setting->default_value);
326-
if (is_using_default) {
327-
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(ImColor::HSV(0, 0, 0.6f)));
328-
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(ImColor::HSV(0, 0, 0.7f)));
329-
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(ImColor::HSV(0, 0, 0.8f)));
288+
last_group = setting->group;
289+
290+
const bool is_disabled = preset_index == 0
291+
|| (setting->is_enabled != nullptr
292+
&& !setting->is_enabled());
293+
if (is_disabled) {
294+
ImGui::BeginDisabled();
330295
}
331-
auto* font = ImGui::GetFont();
332-
auto old_scale = font->Scale;
333-
auto previous_font_size = ImGui::GetFontSize();
334-
font->Scale *= 0.75f;
335-
ImGui::PushFont(font);
336-
auto current_font_size = ImGui::GetFontSize();
337-
338-
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, current_font_size * 2);
339-
340-
ImVec2 cursor_pos = ImGui::GetCursorPos();
341-
cursor_pos.y += (previous_font_size / 2.f) - (current_font_size / 2.f);
342-
ImGui::SetCursorPos(cursor_pos);
343-
344-
if (ImGui::Button(reinterpret_cast<const char*>(ICON_FK_UNDO))) {
345-
setting->Set(setting->default_value);
346-
changed = true;
296+
bool changed = false;
297+
298+
switch (setting->value_type) {
299+
case SettingValueType::FLOAT:
300+
changed |= ImGui::SliderFloat(
301+
setting->label.c_str(),
302+
&setting->value,
303+
setting->min,
304+
setting->max,
305+
setting->format.c_str());
306+
break;
307+
case SettingValueType::INTEGER:
308+
changed |= ImGui::SliderInt(
309+
setting->label.c_str(),
310+
&setting->value_as_int,
311+
setting->min,
312+
setting->GetMax(),
313+
setting->labels.empty()
314+
? setting->format.c_str()
315+
: setting->labels.at(setting->value_as_int).c_str(),
316+
ImGuiSliderFlags_NoInput);
317+
break;
318+
case SettingValueType::BOOLEAN:
319+
changed |= ImGui::SliderInt(
320+
setting->label.c_str(),
321+
&setting->value_as_int,
322+
0,
323+
1,
324+
setting->labels.empty()
325+
? ((setting->value_as_int == 0) ? "Off" : "On") // NOLINT(readability-avoid-nested-conditional-operator)
326+
: setting->labels.at(setting->value_as_int).c_str(),
327+
ImGuiSliderFlags_NoInput);
328+
break;
329+
case SettingValueType::BUTTON: {
330+
changed |= ImGui::Button(setting->label.c_str());
331+
}
332+
}
333+
if (changed) {
334+
setting->on_change();
335+
}
336+
if (!setting->tooltip.empty()) {
337+
ImGui::SetItemTooltip("%s", setting->tooltip.c_str());
347338
}
348339

349-
if (is_using_default) {
350-
ImGui::PopStyleColor(3);
340+
if (preset_index != 0
341+
&& setting->can_reset
342+
&& setting->value_type != SettingValueType::BUTTON) {
343+
ImGui::SameLine();
344+
const bool is_using_default = (setting->GetValue() == setting->default_value);
345+
ImGui::BeginDisabled(is_using_default);
346+
ImGui::PushID(&setting->default_value);
347+
if (is_using_default) {
348+
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(ImColor::HSV(0, 0, 0.6f)));
349+
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(ImColor::HSV(0, 0, 0.7f)));
350+
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(ImColor::HSV(0, 0, 0.8f)));
351+
}
352+
auto* font = ImGui::GetFont();
353+
auto old_scale = font->Scale;
354+
auto previous_font_size = ImGui::GetFontSize();
355+
font->Scale *= 0.75f;
356+
ImGui::PushFont(font);
357+
auto current_font_size = ImGui::GetFontSize();
358+
359+
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, current_font_size * 2);
360+
361+
ImVec2 cursor_pos = ImGui::GetCursorPos();
362+
cursor_pos.y += (previous_font_size / 2.f) - (current_font_size / 2.f);
363+
ImGui::SetCursorPos(cursor_pos);
364+
365+
if (ImGui::Button(reinterpret_cast<const char*>(ICON_FK_UNDO))) {
366+
setting->Set(setting->default_value);
367+
changed = true;
368+
}
369+
370+
if (is_using_default) {
371+
ImGui::PopStyleColor(3);
372+
}
373+
font->Scale = old_scale;
374+
ImGui::PopFont();
375+
ImGui::PopStyleVar();
376+
ImGui::PopID();
377+
ImGui::EndDisabled();
351378
}
352-
font->Scale = old_scale;
353-
ImGui::PopFont();
354-
ImGui::PopStyleVar();
355-
ImGui::PopID();
356-
ImGui::EndDisabled();
357-
}
358379

359-
if (changed) {
360-
setting->Write();
361-
any_change = true;
362-
}
363-
if (is_disabled) {
364-
ImGui::EndDisabled();
380+
if (changed) {
381+
setting->Write();
382+
any_change = true;
383+
}
384+
if (is_disabled) {
385+
ImGui::EndDisabled();
386+
}
365387
}
366-
367388
ImGui::PopStyleColor(styles_pushed);
368389
}
390+
if (open_node) {
391+
ImGui::Indent();
392+
ImGui::TreePop();
393+
}
394+
369395
if (!changed_preset && any_change) {
370396
switch (preset_index) {
371397
case 1:

0 commit comments

Comments
 (0)