Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selectableitem #137

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions SofaImGui/src/SofaImGui/ImGuiDataWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <implot.h>
#include <sofa/helper/map.h>
#include <sofa/helper/OptionsGroup.h>
#include <sofa/helper/SelectableItem.h>


namespace sofaimgui
Expand Down Expand Up @@ -507,6 +508,47 @@ void DataWidget<helper::OptionsGroup>::showWidget(MyData& data)
}
}

/***********************************************************************************************************************
* SelectableItems
**********************************************************************************************************************/
template<>
void DataWidget<helper::BaseSelectableItem>::showWidget(
sofa::core::objectmodel::BaseData& data, const helper::BaseSelectableItem* selectableItems)
{
const auto& label = data.getName();
const auto id = data.getName() + data.getOwner()->getPathName();

int selectedId = selectableItems->getSelectedId();

sofa::type::vector<std::string> descriptiveItems;
descriptiveItems.reserve(selectableItems->getNumberOfItems());
for (unsigned int i = 0; i < selectableItems->getNumberOfItems(); ++i)
{
const auto& [key, description] = selectableItems->getItemsData()[i];

std::stringstream ss;
ss << key;
if (!description.empty())
{
ss << " (" << description << ")";
}

descriptiveItems.push_back(ss.str());
}

std::unique_ptr<const char*[]> charArray(new const char*[selectableItems->getNumberOfItems()]);
for (unsigned int i = 0; i < selectableItems->getNumberOfItems(); ++i)
{
charArray[i] = descriptiveItems[i].data();
}

if (ImGui::Combo((label + "##" + id).c_str(), &selectedId, charArray.get(),
static_cast<int>(selectableItems->getNumberOfItems())))
{
const_cast<helper::BaseSelectableItem*>(selectableItems)->setSelectedId(selectedId);
}
}

/***********************************************************************************************************************
* Factory
**********************************************************************************************************************/
Expand Down Expand Up @@ -568,4 +610,7 @@ const bool dw_map_vectord = DataWidgetFactory::Add<std::map<std::string, type::v

const bool dw_optionsGroup = DataWidgetFactory::Add<helper::OptionsGroup>();


const bool dw_selectable_items = DataWidgetFactory::Add<helper::BaseSelectableItem>();

}
30 changes: 29 additions & 1 deletion SofaImGui/src/SofaImGui/ImGuiDataWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,42 @@ struct DataWidget : BaseDataWidget
{
showWidget(*d);
}
else
{
const void* rawPtr = data.getValueVoidPtr();
if (const T* castedPtr = static_cast<const T*>(rawPtr))
{
showWidget(data, castedPtr);
}
else
{
showWidgetAsText(data);
}
}
}

void showWidget(MyData& data)
{
ImGui::TextWrapped(data.getValueString().c_str());
showWidgetAsText(data);
}

~DataWidget() override = default;

protected:
/**
* This method is called when the Data cannot be dynamic_cast from a BaseData.
* Instead, the BaseData is provided, as well as the object.
*/
void showWidget(sofa::core::objectmodel::BaseData& data, const T* object)
{
SOFA_UNUSED(object);
showWidgetAsText(data);
}

void showWidgetAsText(sofa::core::objectmodel::BaseData& data)
{
ImGui::TextWrapped(data.getValueString().c_str());
}
};

struct DataWidgetFactory
Expand Down
Loading