diff --git a/SofaImGui/src/SofaImGui/ImGuiDataWidget.cpp b/SofaImGui/src/SofaImGui/ImGuiDataWidget.cpp index a05d648884..2af9d894ce 100644 --- a/SofaImGui/src/SofaImGui/ImGuiDataWidget.cpp +++ b/SofaImGui/src/SofaImGui/ImGuiDataWidget.cpp @@ -26,6 +26,7 @@ #include #include #include +#include namespace sofaimgui @@ -586,6 +587,47 @@ void DataWidget::showWidget(MyData& data) } } +/*********************************************************************************************************************** + * SelectableItems + **********************************************************************************************************************/ +template<> +void DataWidget::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 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 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(selectableItems->getNumberOfItems()))) + { + const_cast(selectableItems)->setSelectedId(selectedId); + } +} + /*********************************************************************************************************************** * Factory **********************************************************************************************************************/ @@ -653,4 +695,7 @@ const bool dw_map_vectord = DataWidgetFactory::Add(); + +const bool dw_selectable_items = DataWidgetFactory::Add(); + } diff --git a/SofaImGui/src/SofaImGui/ImGuiDataWidget.h b/SofaImGui/src/SofaImGui/ImGuiDataWidget.h index 75cce3b3d3..bdc6670832 100644 --- a/SofaImGui/src/SofaImGui/ImGuiDataWidget.h +++ b/SofaImGui/src/SofaImGui/ImGuiDataWidget.h @@ -56,14 +56,42 @@ struct DataWidget : BaseDataWidget { showWidget(*d); } + else + { + const void* rawPtr = data.getValueVoidPtr(); + if (const T* castedPtr = static_cast(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