From 07326edb6b16dff01f2e3e01ee15130e8496d58e Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Thu, 19 Mar 2020 23:21:29 -0700 Subject: [PATCH] Sim GUI: Add user rename support to SimDevice list (#2426) --- .../src/main/native/cpp/IniSaverInfo.cpp | 27 +++++++++++++++++ .../src/main/native/cpp/SimDeviceGui.cpp | 30 ++++++++++++------- .../src/main/native/include/IniSaverInfo.h | 3 ++ 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/simulation/halsim_gui/src/main/native/cpp/IniSaverInfo.cpp b/simulation/halsim_gui/src/main/native/cpp/IniSaverInfo.cpp index 15f3a4c09d..7baea3d875 100644 --- a/simulation/halsim_gui/src/main/native/cpp/IniSaverInfo.cpp +++ b/simulation/halsim_gui/src/main/native/cpp/IniSaverInfo.cpp @@ -14,6 +14,14 @@ using namespace halsimgui; +void NameInfo::GetName(char* buf, size_t size, const char* defaultName) { + if (m_name[0] != '\0') { + std::snprintf(buf, size, "%s###Name%s", m_name, defaultName); + } else { + std::snprintf(buf, size, "%s###Name%s", defaultName, defaultName); + } +} + void NameInfo::GetName(char* buf, size_t size, const char* defaultName, int index) { if (m_name[0] != '\0') { @@ -52,6 +60,12 @@ void NameInfo::PushEditNameId(int index) { ImGui::PushID(id); } +void NameInfo::PushEditNameId(const char* name) { + char id[128]; + std::snprintf(id, sizeof(id), "Name%s", name); + ImGui::PushID(id); +} + void NameInfo::PopupEditName(int index) { char id[64]; std::snprintf(id, sizeof(id), "Name%d", index); @@ -65,6 +79,19 @@ void NameInfo::PopupEditName(int index) { } } +void NameInfo::PopupEditName(const char* name) { + char id[128]; + std::snprintf(id, sizeof(id), "Name%s", name); + if (ImGui::BeginPopupContextItem(id)) { + ImGui::Text("Edit name:"); + if (ImGui::InputText("##edit", m_name, sizeof(m_name), + ImGuiInputTextFlags_EnterReturnsTrue)) + ImGui::CloseCurrentPopup(); + if (ImGui::Button("Close")) ImGui::CloseCurrentPopup(); + ImGui::EndPopup(); + } +} + bool OpenInfo::ReadIni(wpi::StringRef name, wpi::StringRef value) { if (name != "open") return false; int num; diff --git a/simulation/halsim_gui/src/main/native/cpp/SimDeviceGui.cpp b/simulation/halsim_gui/src/main/native/cpp/SimDeviceGui.cpp index 90d66f3e8b..12b162f66e 100644 --- a/simulation/halsim_gui/src/main/native/cpp/SimDeviceGui.cpp +++ b/simulation/halsim_gui/src/main/native/cpp/SimDeviceGui.cpp @@ -22,7 +22,16 @@ using namespace halsimgui; namespace { -struct ElementInfo : public OpenInfo { +struct ElementInfo : public NameInfo, public OpenInfo { + bool ReadIni(wpi::StringRef name, wpi::StringRef value) { + if (NameInfo::ReadIni(name, value)) return true; + if (OpenInfo::ReadIni(name, value)) return true; + return false; + } + void WriteIni(ImGuiTextBuffer* out) { + NameInfo::WriteIni(out); + OpenInfo::WriteIni(out); + } bool visible = true; // not saved }; } // namespace @@ -40,15 +49,16 @@ bool SimDeviceGui::StartDevice(const char* label, ImGuiTreeNodeFlags flags) { auto& element = gElements[label]; if (!element.visible) return false; - if (ImGui::CollapsingHeader( - label, - flags | (element.IsOpen() ? ImGuiTreeNodeFlags_DefaultOpen : 0))) { - ImGui::PushID(label); - element.SetOpen(true); - return true; - } - element.SetOpen(false); - return false; + char name[128]; + element.GetName(name, sizeof(name), label); + + bool open = ImGui::CollapsingHeader( + name, flags | (element.IsOpen() ? ImGuiTreeNodeFlags_DefaultOpen : 0)); + element.SetOpen(open); + element.PopupEditName(label); + + if (open) ImGui::PushID(label); + return open; } void SimDeviceGui::FinishDevice() { ImGui::PopID(); } diff --git a/simulation/halsim_gui/src/main/native/include/IniSaverInfo.h b/simulation/halsim_gui/src/main/native/include/IniSaverInfo.h index 67f668d4f4..96e5621eb3 100644 --- a/simulation/halsim_gui/src/main/native/include/IniSaverInfo.h +++ b/simulation/halsim_gui/src/main/native/include/IniSaverInfo.h @@ -18,13 +18,16 @@ class NameInfo { bool HasName() const { return m_name[0] != '\0'; } const char* GetName() const { return m_name; } + void GetName(char* buf, size_t size, const char* defaultName); void GetName(char* buf, size_t size, const char* defaultName, int index); void GetName(char* buf, size_t size, const char* defaultName, int index, int index2); bool ReadIni(wpi::StringRef name, wpi::StringRef value); void WriteIni(ImGuiTextBuffer* out); void PushEditNameId(int index); + void PushEditNameId(const char* name); void PopupEditName(int index); + void PopupEditName(const char* name); private: char m_name[64];