Lagrange
UIWidget.h
1/*
2 * Copyright 2019 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <imgui.h>
15#include <lagrange/ui/utils/math.h>
16#include <lagrange/utils/strings.h>
17
18#include <memory>
19#include <string>
20#include <unordered_set>
21
22namespace lagrange {
23namespace ui {
24
25
26class Texture;
27class Color;
28class Keybinds;
29
31{
32public:
35 UIWidget(const std::string& name = "##value");
36
37 template <typename T>
38 bool operator()(T& value)
39 {
40 (void)(value);
41 return false;
42 }
43
44 bool operator()(float& value);
45 bool operator()(int& value);
46 bool operator()(bool& value);
47 bool operator()(double& value);
48 bool operator()(std::string& value);
49
50
51 bool operator()(Texture& value, int width = 0, int height = 0);
52 bool operator()(Color& value);
53
54 bool operator()(Eigen::Vector2f& value);
55 bool operator()(Eigen::Vector3f& value);
56 bool operator()(Eigen::Vector4f& value);
57 bool operator()(Eigen::Vector2i& value);
58 bool operator()(Eigen::Vector3i& value);
59 bool operator()(Eigen::Vector4i& value);
60 bool operator()(Eigen::Matrix2f& value);
61 bool operator()(Eigen::Matrix3f& value);
62 bool operator()(Eigen::Matrix4f& value);
63 bool operator()(Eigen::Affine3f& value);
64
65private:
66 bool render_matrix(float* M, int dimension);
67
68 const std::string& m_name;
69};
70
71
72namespace utils {
73template <typename T>
75{
76 bool draw(T& val);
77};
78
79template <>
80struct value_field<float>
81{
82 bool draw(float& val) { return ImGui::DragFloat("##", &val, val / 100.0f); }
83};
84
85template <>
86struct value_field<int>
87{
88 bool draw(int& val) { return ImGui::DragInt("##", &val); }
89};
90
91template <>
92struct value_field<double>
93{
94 bool draw(double& val)
95 {
96 float f = static_cast<float>(val);
97 if (ImGui::DragFloat("##", &f, f / 100.0f)) {
98 val = static_cast<double>(f);
99 return true;
100 }
101 return false;
102 }
103};
104} // namespace utils
105
107{
108public:
109 template <typename MatrixType>
110 bool operator()(
111 const MatrixType& matrix,
112 int& row_out,
113 int& col_out,
114 typename MatrixType::Scalar& value_out)
115 {
116 const int pages = (int(matrix.rows()) + m_per_page - 1) / m_per_page;
117 ImGui::DragInt(
118 string_format("/ {} pages", pages).c_str(),
119 &m_current_page,
120 1,
121 0,
122 pages - 1);
123
124 m_current_page = std::max(int(0), std::min(m_current_page, pages - 1));
125
126 ImGui::Columns(int(matrix.cols()) + 1);
127
128 int begin = m_current_page * m_per_page;
129 int end = std::min(int((m_current_page + 1) * m_per_page), static_cast<int>(matrix.rows()));
130
131 bool changed = false;
132
133 for (auto i = begin; i < end; i++) {
134 ImGui::Text("[%d]", i);
135 ImGui::NextColumn();
136
137 for (auto k = 0; k < matrix.cols(); k++) {
138 typename MatrixType::Scalar new_value;
139 if (matrix_field(matrix, i, k, new_value)) {
140 row_out = i;
141 col_out = k;
142 value_out = new_value;
143 changed = true;
144 }
145 ImGui::NextColumn();
146 }
147 }
148 ImGui::Columns(1);
149 return changed;
150 }
151
152
153 template <typename MatrixType, typename IndexType>
154 bool operator()(
155 const MatrixType& matrix,
156 const std::unordered_set<IndexType>& selection,
157 int& row_out,
158 int& col_out,
159 typename MatrixType::Scalar& value_out)
160 {
161 const int pages = (int(selection.size()) + m_per_page - 1) / m_per_page;
162 ImGui::DragInt(
163 string_format("/ {} pages (Selection)", pages).c_str(),
164 &m_current_page,
165 1,
166 0,
167 pages - 1);
168
169 m_current_page = std::max(int(0), std::min(m_current_page, pages - 1));
170
171 ImGui::Columns(int(matrix.cols()) + 1);
172
173 int begin = m_current_page * m_per_page;
174 int end = std::min(int((m_current_page + 1) * m_per_page), static_cast<int>(matrix.rows()));
175
176 bool changed = false;
177
178 int cnt = -1;
179 for (auto elem : selection) {
180 cnt++;
181 if (cnt < begin) continue;
182 if (cnt == end) break;
183
184 ImGui::Text("[%d]", int(elem));
185 ImGui::NextColumn();
186
187 for (auto k = 0; k < matrix.cols(); k++) {
188 typename MatrixType::Scalar new_value;
189 if (matrix_field(matrix, elem, k, new_value)) {
190 row_out = elem;
191 col_out = k;
192 value_out = new_value;
193 changed = true;
194 }
195 ImGui::NextColumn();
196 }
197 }
198 ImGui::Columns(1);
199 return changed;
200 }
201
202
203 int get_per_page() const { return m_per_page; }
204 void set_per_page(int value) { m_per_page = value; }
205
206private:
207 template <typename MatrixType>
208 bool matrix_field(const MatrixType& matrix, int row, int col, typename MatrixType::Scalar& out)
209 {
210 ImGui::PushID(row * int(matrix.cols()) + col);
211 auto val = matrix(row, col);
212 bool changed = utils::value_field<typename MatrixType::Scalar>().draw(val);
213 if (changed) {
214 out = val;
215 }
216 ImGui::PopID();
217 return changed;
218 }
219
220 int m_current_page = 0;
221 int m_per_page = 25;
222};
223
224
225} // namespace ui
226} // namespace lagrange
Definition: Color.h:24
Definition: UIWidget.h:107
Definition: Texture.h:30
Definition: UIWidget.h:31
UIWidget(const std::string &name="##value")
Constructs a widget with name, if no name given an invisible label is used.
Definition: UIWidget.cpp:211
@ Color
Mesh attribute can have 1, 2, 3 or 4 channels.
std::string string_format(fmt::format_string< Args... > format, Args &&... args)
Format args according to the format string fmt, and return the result as a string.
Definition: strings.h:103
Lagrange UI Viewer and mini 3D engine.
Definition: AcceleratedPicking.h:22
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Definition: UIWidget.h:75