diff --git a/SofaImGui/src/SofaImGui/ImGuiDataWidget.cpp b/SofaImGui/src/SofaImGui/ImGuiDataWidget.cpp index 127aea4306..a05d648884 100644 --- a/SofaImGui/src/SofaImGui/ImGuiDataWidget.cpp +++ b/SofaImGui/src/SofaImGui/ImGuiDataWidget.cpp @@ -48,6 +48,40 @@ void DataWidget::showWidget(MyData& data) } } +bool showScalarWidget(const std::string& label, const std::string& id, float& value) +{ + return ImGui::InputFloat((label + "##" + id).c_str(), &value, 0.0f, 0.0f, "%.3f", ImGuiInputTextFlags_EnterReturnsTrue); +} + +bool showScalarWidget(const std::string& label, const std::string& id, double& value) +{ + return ImGui::InputDouble((label + "##" + id).c_str(), &value, 0.0f, 0.0f, "%.3f", ImGuiInputTextFlags_EnterReturnsTrue); +} + +template +void showScalarWidget(Data& data) +{ + Scalar initialValue = data.getValue(); + const auto& label = data.getName(); + const auto id = data.getName() + data.getOwner()->getPathName(); + if (showScalarWidget(label, id, initialValue)) + { + data.setValue(initialValue); + } +} + +template<> +void DataWidget::showWidget(MyData& data) +{ + showScalarWidget(data); +} + +template<> +void DataWidget::showWidget(MyData& data) +{ + showScalarWidget(data); +} + /*********************************************************************************************************************** * Vec **********************************************************************************************************************/ @@ -167,6 +201,13 @@ void showVecTableHeader(Data > >&) } } +template +void showVecTableHeader(Data >&) +{ + ImGui::TableSetupColumn(""); + ImGui::TableSetupColumn("Value"); +} + template void showVecTableHeader(Data > >&) { @@ -191,80 +232,118 @@ void showVecTableHeader(Data > >&) ImGui::TableSetupColumn("Z"); } -template< Size N, typename ValueType> -void showWidgetT(Data > >& data) +template +bool showLine(unsigned int lineNumber, const std::string& tableLabel, type::Vec& vec) +{ + for (const auto& v : vec) + { + ImGui::TableNextColumn(); + ImGui::Text("%f", v); + } + return false; +} + +template +bool showLine(unsigned int lineNumber, const std::string& tableLabel, ValueType& value) +{ + ImGui::TableNextColumn(); + return showScalarWidget("", tableLabel + std::to_string(lineNumber), value); +} + +template +void showVectorWidget(Data& data) { static ImGuiTableFlags flags = ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Resizable | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_ContextMenuInBody | ImGuiTableFlags_NoHostExtendX; ImGui::Text("%d elements", data.getValue().size()); - if (ImGui::BeginTable((data.getName() + data.getOwner()->getPathName()).c_str(), N + 1, flags)) + const auto nbColumns = data.getValueTypeInfo()->size() + 1; + const auto tableLabel = data.getName() + data.getOwner()->getPathName(); + if (ImGui::BeginTable(tableLabel.c_str(), nbColumns, flags)) { showVecTableHeader(data); ImGui::TableHeadersRow(); - unsigned int counter {}; - for (const auto& vec : *helper::getReadAccessor(data)) + auto accessor = helper::getWriteAccessor(data); + bool anyChange = false; + for (std::size_t i = 0; i < accessor.size(); ++i) { ImGui::TableNextRow(); ImGui::TableNextColumn(); - ImGui::Text("%d", counter++); - for (const auto& v : vec) + ImGui::Text("%d", i); + auto& vec = accessor[i]; + if (showLine(i, tableLabel, vec)) { - ImGui::TableNextColumn(); - ImGui::Text("%f", v); + anyChange = true; + data.setDirtyValue(); } } + if (anyChange) + { + data.updateIfDirty(); + } ImGui::EndTable(); } } +template<> +void DataWidget >::showWidget(MyData& data) +{ + showVectorWidget(data); +} + +template<> +void DataWidget >::showWidget(MyData& data) +{ + showVectorWidget(data); +} + template<> void DataWidget > >::showWidget(MyData& data) { - showWidgetT(data); + showVectorWidget(data); } template<> void DataWidget > >::showWidget(MyData& data) { - showWidgetT(data); + showVectorWidget(data); } template<> void DataWidget > >::showWidget(MyData& data) { - showWidgetT(data); + showVectorWidget(data); } template<> void DataWidget > >::showWidget(MyData& data) { - showWidgetT(data); + showVectorWidget(data); } template<> void DataWidget > >::showWidget(MyData& data) { - showWidgetT(data); + showVectorWidget(data); } template<> void DataWidget > >::showWidget(MyData& data) { - showWidgetT(data); + showVectorWidget(data); } template<> void DataWidget > >::showWidget(MyData& data) { - showWidgetT(data); + showVectorWidget(data); } template<> void DataWidget > >::showWidget(MyData& data) { - showWidgetT(data); + showVectorWidget(data); } /*********************************************************************************************************************** @@ -513,6 +592,9 @@ void DataWidget::showWidget(MyData& data) const bool dw_bool = DataWidgetFactory::Add(); +const bool dw_float = DataWidgetFactory::Add(); +const bool dw_double = DataWidgetFactory::Add(); + const bool dw_vec1d = DataWidgetFactory::Add >(); const bool dw_vec1f = DataWidgetFactory::Add >(); @@ -531,6 +613,9 @@ const bool dw_vec6f = DataWidgetFactory::Add >(); const bool dw_vec8d = DataWidgetFactory::Add >(); const bool dw_vec8f = DataWidgetFactory::Add >(); +const bool dw_vector_double = DataWidgetFactory::Add >(); +const bool dw_vector_float = DataWidgetFactory::Add >(); + const bool dw_vector_vec1d = DataWidgetFactory::Add > >(); const bool dw_vector_vec1f = DataWidgetFactory::Add > >();