mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Sim GUI: Add user rename support to SimDevice list (#2426)
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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(); }
|
||||
|
||||
Reference in New Issue
Block a user