From 173d3f4bc7f2977b9f1ba924525163563d4574f3 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 19 Jan 2024 01:09:10 -0600 Subject: [PATCH] [olympic/studio] Add PushButton, PopupBtns, ComboBox, and IDStackItem to ImGui utils --- .../modlib/include/studio/imguiutil.hpp | 30 ++++++++++++++ src/olympic/studio/modlib/src/imguiutil.cpp | 41 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/olympic/studio/modlib/include/studio/imguiutil.hpp b/src/olympic/studio/modlib/include/studio/imguiutil.hpp index d72d9205..f92ea7fb 100644 --- a/src/olympic/studio/modlib/include/studio/imguiutil.hpp +++ b/src/olympic/studio/modlib/include/studio/imguiutil.hpp @@ -2,12 +2,42 @@ * Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved. */ +#pragma once + #include #include namespace studio::ig { +inline constexpr auto BtnSz = ImVec2{50, 22}; + +template +class IDStackItem { + private: + T m_id; + public: + explicit IDStackItem(T id) noexcept: m_id(id) { + ImGui::PushID(m_id); + } + ~IDStackItem() noexcept { + ImGui::PopID(); + } +}; + void centerNextWindow(turbine::Context &ctx) noexcept; +bool PushButton(ox::CStringView lbl, ImVec2 const&btnSz = BtnSz) noexcept; + +void PopupBtns(float popupWidth, bool &popupOpen); + +/** + * + * @param lbl + * @param list + * @param selectedIdx + * @return true if new value selected, false otherwise + */ +bool ComboBox(ox::CStringView lbl, ox::SpanView list, size_t &selectedIdx) noexcept; + } \ No newline at end of file diff --git a/src/olympic/studio/modlib/src/imguiutil.cpp b/src/olympic/studio/modlib/src/imguiutil.cpp index f4e00900..e52f3153 100644 --- a/src/olympic/studio/modlib/src/imguiutil.cpp +++ b/src/olympic/studio/modlib/src/imguiutil.cpp @@ -6,6 +6,8 @@ #include +#include + namespace studio::ig { void centerNextWindow(turbine::Context &ctx) noexcept { @@ -16,4 +18,43 @@ void centerNextWindow(turbine::Context &ctx) noexcept { ImGui::SetNextWindowPos(ImVec2(screenW / mod, screenH / mod), ImGuiCond_Always, ImVec2(0.5f, 0.5f)); } +bool PushButton(ox::CStringView lbl, ImVec2 const&btnSz) noexcept { + return ImGui::Button(lbl.c_str(), btnSz); +} + +void PopupBtns(float popupWidth, bool &popupOpen) { + constexpr auto btnSz = ImVec2{50, BtnSz.y}; + ImGui::Separator(); + ImGui::SetCursorPosX(popupWidth - 118); + if (ImGui::Button("OK", btnSz)) { + ImGui::CloseCurrentPopup(); + popupOpen = false; + } + ImGui::SameLine(); + if (ImGui::Button("Cancel", btnSz)) { + ImGui::CloseCurrentPopup(); + popupOpen = false; + } +} + +bool ComboBox( + ox::CStringView lbl, + ox::SpanView list, + size_t &selectedIdx) noexcept { + bool out{}; + auto const first = selectedIdx < list.size() ? list[selectedIdx].c_str() : ""; + if (ImGui::BeginCombo(lbl.c_str(), first, 0)) { + for (auto i = 0u; i < list.size(); ++i) { + const auto selected = (selectedIdx == i); + if (ImGui::Selectable(list[i].c_str(), selected) && selectedIdx != i) { + selectedIdx = i; + //oxLogError(m_model.setPalette(list[n])); + out = true; + } + } + ImGui::EndCombo(); + } + return out; +} + }