|
| 1 | +/****************************************************************************** |
| 2 | +* SOFA, Simulation Open-Framework Architecture * |
| 3 | +* (c) 2006 INRIA, USTL, UJF, CNRS, MGH * |
| 4 | +* * |
| 5 | +* This program is free software; you can redistribute it and/or modify it * |
| 6 | +* under the terms of the GNU General Public License as published by the Free * |
| 7 | +* Software Foundation; either version 2 of the License, or (at your option) * |
| 8 | +* any later version. * |
| 9 | +* * |
| 10 | +* This program is distributed in the hope that it will be useful, but WITHOUT * |
| 11 | +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * |
| 12 | +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * |
| 13 | +* more details. * |
| 14 | +* * |
| 15 | +* You should have received a copy of the GNU General Public License along * |
| 16 | +* with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 17 | +******************************************************************************* |
| 18 | +* Authors: The SOFA Team and external contributors (see Authors.txt) * |
| 19 | +* * |
| 20 | +* Contact information: contact@sofa-framework.org * |
| 21 | +******************************************************************************/ |
| 22 | +#pragma once |
| 23 | +#include <sofa/core/objectmodel/Data.h> |
| 24 | +#include <SofaImGui/widgets/ScalarWidget.h> |
| 25 | +#include <imgui.h> |
| 26 | +#include <unordered_map> |
| 27 | +#include <string> |
| 28 | + |
| 29 | +namespace sofaimgui |
| 30 | +{ |
| 31 | + |
| 32 | +using namespace sofa; |
| 33 | + |
| 34 | +/*********************************************************************************************************************** |
| 35 | + * Vectors of Vec |
| 36 | + **********************************************************************************************************************/ |
| 37 | + |
| 38 | +template< Size N, typename ValueType> |
| 39 | +void showVecTableHeader(Data<type::vector<type::Vec<N, ValueType> > >&) |
| 40 | +{ |
| 41 | + ImGui::TableSetupColumn(""); |
| 42 | + for (unsigned int i = 0; i < N; ++i) |
| 43 | + { |
| 44 | + ImGui::TableSetupColumn(std::to_string(i).c_str(), ImGuiTableColumnFlags_WidthStretch); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +template<typename ValueType> |
| 49 | +void showVecTableHeader(Data<type::vector<ValueType> >&) |
| 50 | +{ |
| 51 | + ImGui::TableSetupColumn(""); |
| 52 | + ImGui::TableSetupColumn("Value", ImGuiTableColumnFlags_WidthStretch); |
| 53 | +} |
| 54 | + |
| 55 | +template<typename ValueType> |
| 56 | +void showVecTableHeader(Data<type::vector<type::Vec<1, ValueType> > >&) |
| 57 | +{ |
| 58 | + ImGui::TableSetupColumn(""); |
| 59 | + ImGui::TableSetupColumn("X", ImGuiTableColumnFlags_WidthStretch); |
| 60 | +} |
| 61 | + |
| 62 | +template<typename ValueType> |
| 63 | +void showVecTableHeader(Data<type::vector<type::Vec<2, ValueType> > >&) |
| 64 | +{ |
| 65 | + ImGui::TableSetupColumn(""); |
| 66 | + ImGui::TableSetupColumn("X", ImGuiTableColumnFlags_WidthStretch); |
| 67 | + ImGui::TableSetupColumn("Y", ImGuiTableColumnFlags_WidthStretch); |
| 68 | +} |
| 69 | + |
| 70 | +template<typename ValueType> |
| 71 | +void showVecTableHeader(Data<type::vector<type::Vec<3, ValueType> > >&) |
| 72 | +{ |
| 73 | + ImGui::TableSetupColumn(""); |
| 74 | + ImGui::TableSetupColumn("X", ImGuiTableColumnFlags_WidthStretch); |
| 75 | + ImGui::TableSetupColumn("Y", ImGuiTableColumnFlags_WidthStretch); |
| 76 | + ImGui::TableSetupColumn("Z", ImGuiTableColumnFlags_WidthStretch); |
| 77 | +} |
| 78 | + |
| 79 | +template<Size N, typename ValueType> |
| 80 | +bool showLine(unsigned int lineNumber, const std::string& tableLabel, type::Vec<N, ValueType>& vec) |
| 81 | +{ |
| 82 | + for (const auto& v : vec) |
| 83 | + { |
| 84 | + ImGui::TableNextColumn(); |
| 85 | + ImGui::Text("%f", v); |
| 86 | + } |
| 87 | + return false; |
| 88 | +} |
| 89 | + |
| 90 | +template<typename ValueType> |
| 91 | +bool showLine(unsigned int lineNumber, const std::string& tableLabel, ValueType& value) |
| 92 | +{ |
| 93 | + ImGui::TableNextColumn(); |
| 94 | + return showScalarWidget("", tableLabel + std::to_string(lineNumber), value); |
| 95 | +} |
| 96 | + |
| 97 | +namespace |
| 98 | +{ |
| 99 | + enum class TableExpansionState |
| 100 | + { |
| 101 | + Collapsed, |
| 102 | + Expanded |
| 103 | + }; |
| 104 | + |
| 105 | + // Helper function to toggle expansion state |
| 106 | + void toggleExpansionState(TableExpansionState& expansionState) |
| 107 | + { |
| 108 | + expansionState = (expansionState == TableExpansionState::Expanded) |
| 109 | + ? TableExpansionState::Collapsed |
| 110 | + : TableExpansionState::Expanded; |
| 111 | + } |
| 112 | + |
| 113 | + // Helper function to get or initialize expanded state for a table |
| 114 | + TableExpansionState& getOrInitializeExpandedState(const std::string& tableLabel) |
| 115 | + { |
| 116 | + static std::unordered_map<std::string, TableExpansionState> expandedState; |
| 117 | + if (!expandedState.contains(tableLabel)) |
| 118 | + { |
| 119 | + expandedState[tableLabel] = TableExpansionState::Collapsed; |
| 120 | + } |
| 121 | + return expandedState[tableLabel]; |
| 122 | + } |
| 123 | + |
| 124 | + // Helper function to display element count and toggle button |
| 125 | + void showVectorWidgetHeader(std::size_t elementsCount, const std::string& tableLabel, TableExpansionState& expansionState) |
| 126 | + { |
| 127 | + ImGui::Text("%d elements", elementsCount); |
| 128 | + ImGui::SameLine(); |
| 129 | + const bool isExpanded = (expansionState == TableExpansionState::Expanded); |
| 130 | + |
| 131 | + if (ImGui::Button(std::string(isExpanded ? "Collapse##" + tableLabel : "Expand##" + tableLabel).c_str())) |
| 132 | + { |
| 133 | + toggleExpansionState(expansionState); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + // Helper function to render a single table row |
| 138 | + template<class AccessorType> |
| 139 | + bool renderTableRow(std::size_t index, const std::string& tableLabel, AccessorType& accessor) |
| 140 | + { |
| 141 | + ImGui::TableNextRow(); |
| 142 | + ImGui::TableNextColumn(); |
| 143 | + ImGui::Text("%d", index); |
| 144 | + auto& vec = accessor[index]; |
| 145 | + return showLine(index, tableLabel, vec); |
| 146 | + } |
| 147 | + |
| 148 | + // Helper function to render all rows when expanded |
| 149 | + template<class AccessorType> |
| 150 | + bool renderExpandedTableRows(AccessorType& accessor, const std::string& tableLabel) |
| 151 | + { |
| 152 | + bool anyChange = false; |
| 153 | + for (std::size_t i = 0; i < accessor.size(); ++i) |
| 154 | + { |
| 155 | + anyChange |= renderTableRow(i, tableLabel, accessor); |
| 156 | + } |
| 157 | + return anyChange; |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +template<class T> |
| 162 | +void showVectorWidget(Data<T>& data) |
| 163 | +{ |
| 164 | + static ImGuiTableFlags flags = ImGuiTableFlags_ScrollY | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Resizable | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_ContextMenuInBody | ImGuiTableFlags_NoHostExtendX; |
| 165 | + |
| 166 | + const auto nbColumns = data.getValueTypeInfo()->size() + 1; |
| 167 | + const auto tableLabel = data.getName() + data.getOwner()->getPathName(); |
| 168 | + const auto size = data.getValue().size(); |
| 169 | + |
| 170 | + TableExpansionState& expansionState = getOrInitializeExpandedState(tableLabel); |
| 171 | + showVectorWidgetHeader(size, tableLabel, expansionState); |
| 172 | + |
| 173 | + if (expansionState == TableExpansionState::Collapsed) |
| 174 | + { |
| 175 | + flags |= ImGuiTableFlags_ScrollY; |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + flags &= ~ImGuiTableFlags_ScrollY; |
| 180 | + } |
| 181 | + |
| 182 | + static constexpr float NumberOfLinesToShowWhenCollapsed = 8.5f; |
| 183 | + ImVec2 outerSize =ImVec2(0.0f, ImGui::GetTextLineHeightWithSpacing() * NumberOfLinesToShowWhenCollapsed ); |
| 184 | + if (ImGui::BeginTable(tableLabel.c_str(), nbColumns, flags, outerSize)) |
| 185 | + { |
| 186 | + ImGui::TableSetupScrollFreeze(0, 1); |
| 187 | + showVecTableHeader(data); |
| 188 | + ImGui::TableHeadersRow(); |
| 189 | + |
| 190 | + auto accessor = helper::getWriteAccessor(data); |
| 191 | + |
| 192 | + if (renderExpandedTableRows(accessor, tableLabel)) |
| 193 | + { |
| 194 | + data.updateIfDirty(); |
| 195 | + } |
| 196 | + |
| 197 | + ImGui::EndTable(); |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +} |
0 commit comments