Skip to content

Commit

Permalink
Merge branch 'v24.12' into v24.12_update_workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
bakpaul committed Dec 19, 2024
2 parents bd81d9b + fb2a803 commit 39f2109
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
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 @@ -586,6 +587,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 @@ -653,4 +695,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

0 comments on commit 39f2109

Please sign in to comment.