From 0c3f100d7f96c9a99b340ffe09b4b40c012d3b3e Mon Sep 17 00:00:00 2001 From: Turanszki Janos Date: Sun, 8 Nov 2020 17:38:43 +0100 Subject: [PATCH] gui improvement: combobox userdata --- Editor/TransformWindow.cpp | 11 ++++++++--- WickedEngine/wiVersion.cpp | 2 +- WickedEngine/wiWidget.cpp | 23 +++++++++++++++++------ WickedEngine/wiWidget.h | 11 +++++++++-- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/Editor/TransformWindow.cpp b/Editor/TransformWindow.cpp index 0385021fb..601adc7ec 100644 --- a/Editor/TransformWindow.cpp +++ b/Editor/TransformWindow.cpp @@ -42,7 +42,7 @@ void TransformWindow::Create(EditorComponent* editor) if(args.iValue != 0) { - scene.Component_Attach(entity, scene.transforms.GetEntity(args.iValue - 1)); + scene.Component_Attach(entity, args.userdata); } }); @@ -234,12 +234,17 @@ void TransformWindow::SetEntity(Entity entity) for (size_t i = 0; i < scene.transforms.GetCount(); ++i) { Entity entity = scene.transforms.GetEntity(i); + if (entity == this->entity) + { + continue; // Don't list selected (don't allow attach to self) + } + const NameComponent* name = scene.names.GetComponent(entity); - parentCombo.AddItem(name == nullptr ? std::to_string(entity) : name->name); + parentCombo.AddItem(name == nullptr ? std::to_string(entity) : name->name, entity); if (hier != nullptr && hier->parentID == entity) { - parentCombo.SetSelected((int)i + 1); + parentCombo.SetSelected(parentCombo.GetItemCount() - 1); } } diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index a82de4b03..0f00cd9f0 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wiVersion // minor features, major updates, breaking API changes const int minor = 49; // minor bug fixes, alterations, refactors, updates - const int revision = 17; + const int revision = 18; const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision); diff --git a/WickedEngine/wiWidget.cpp b/WickedEngine/wiWidget.cpp index 7969b3a63..7deb40667 100644 --- a/WickedEngine/wiWidget.cpp +++ b/WickedEngine/wiWidget.cpp @@ -1292,7 +1292,7 @@ void wiComboBox::Render(const wiGUI* gui, CommandList cmd) const if (selected >= 0) { - wiFont::Draw(items[selected], wiFontParams(translation.x + scale.x*0.5f, translation.y + scale.y*0.5f, WIFONTSIZE_DEFAULT, WIFALIGN_CENTER, WIFALIGN_CENTER, + wiFont::Draw(items[selected].name, wiFontParams(translation.x + scale.x*0.5f, translation.y + scale.y*0.5f, WIFONTSIZE_DEFAULT, WIFALIGN_CENTER, WIFALIGN_CENTER, font.params.color, font.params.shadowColor), cmd); } @@ -1359,7 +1359,7 @@ void wiComboBox::Render(const wiGUI* gui, CommandList cmd) const } } wiImage::Draw(wiTextureHelper::getWhite(), fx, cmd); - wiFont::Draw(items[i], wiFontParams(translation.x + scale.x*0.5f, translation.y + scale.y*0.5f + GetItemOffset(i), WIFONTSIZE_DEFAULT, WIFALIGN_CENTER, WIFALIGN_CENTER, + wiFont::Draw(items[i].name, wiFontParams(translation.x + scale.x*0.5f, translation.y + scale.y*0.5f + GetItemOffset(i), WIFONTSIZE_DEFAULT, WIFALIGN_CENTER, WIFALIGN_CENTER, font.params.color, font.params.shadowColor), cmd); } } @@ -1368,9 +1368,11 @@ void wiComboBox::OnSelect(function func) { onSelect = move(func); } -void wiComboBox::AddItem(const std::string& item) +void wiComboBox::AddItem(const std::string& name, uint64_t userdata) { - items.push_back(item); + items.emplace_back(); + items.back().name = name; + items.back().userdata = userdata; if (selected < 0) { @@ -1379,7 +1381,7 @@ void wiComboBox::AddItem(const std::string& item) } void wiComboBox::RemoveItem(int index) { - std::vector newItems(0); + std::vector newItems(0); newItems.reserve(items.size()); for (size_t i = 0; i < items.size(); ++i) { @@ -1416,16 +1418,25 @@ void wiComboBox::SetSelected(int index) wiEventArgs args; args.iValue = selected; args.sValue = GetItemText(selected); + args.userdata = GetItemUserData(selected); onSelect(args); } string wiComboBox::GetItemText(int index) const { if (index >= 0) { - return items[index]; + return items[index].name; } return ""; } +uint64_t wiComboBox::GetItemUserData(int index) const +{ + if (index >= 0) + { + return items[index].userdata; + } + return 0; +} int wiComboBox::GetSelected() const { return selected; diff --git a/WickedEngine/wiWidget.h b/WickedEngine/wiWidget.h index 09ff05b07..6948dd16a 100644 --- a/WickedEngine/wiWidget.h +++ b/WickedEngine/wiWidget.h @@ -222,13 +222,18 @@ protected: float scrollbar_delta = 0; - std::vector items; + struct Item + { + std::string name; + uint64_t userdata = 0; + }; + std::vector items; float GetItemOffset(int index) const; public: void Create(const std::string& name); - void AddItem(const std::string& item); + void AddItem(const std::string& name, uint64_t userdata = 0); void RemoveItem(int index); void ClearItems(); void SetMaxVisibleItemCount(int value); @@ -237,6 +242,8 @@ public: void SetSelected(int index); int GetSelected() const; std::string GetItemText(int index) const; + uint64_t GetItemUserData(int index) const; + size_t GetItemCount() const { return items.size(); } void Update(wiGUI* gui, float dt ) override; void Render(const wiGUI* gui, wiGraphics::CommandList cmd) const override;