MeshLib C++ Docs
Loading...
Searching...
No Matches
ImGuiHelpers.h
Go to the documentation of this file.
1#pragma once
2// This file is part of libigl, a simple c++ geometry processing library.
3//
4// Copyright (C) 2018 Jérémie Dumas <jeremie.dumas@ens-lyon.org>
5//
6// This Source Code Form is subject to the terms of the Mozilla Public License
7// v. 2.0. If a copy of the MPL was not distributed with this file, You can
8// obtain one at http://mozilla.org/MPL/2.0/.
9
11#include "exports.h"
12#include "MRMesh/MRVector2.h"
13#include "MRMesh/MRColor.h"
15#include "MRViewer/MRUnits.h"
16#include "MRViewer/MRImGui.h"
17#include <misc/cpp/imgui_stdlib.h>
18#include <algorithm>
19#include <functional>
20#include <cstddef>
21#include <limits>
22#include <string>
23#include <vector>
24#include <optional>
25
26// Extend ImGui by populating its namespace directly
27//
28// Code snippets taken from there:
29// https://eliasdaler.github.io/using-imgui-with-sfml-pt2/
30namespace ImGui
31{
32
33static auto vector_getter = [](void* vec, int idx, const char** out_text)
34{
35 auto& vector = *static_cast<std::vector<std::string>*>(vec);
36 if (idx < 0 || idx >= static_cast<int>(vector.size())) { return false; }
37 *out_text = vector.at(idx).c_str();
38 return true;
39};
40
41inline bool Combo(const char* label, int* idx, const std::vector<std::string>& values)
42{
43 if (values.empty()) { return false; }
44 return Combo(label, idx, vector_getter,
45 const_cast<void *>( static_cast<const void*>(&values) ), (int)values.size());
46}
47
48inline bool Combo(const char* label, int* idx, std::function<const char *(int)> getter, int items_count)
49{
50 auto func = [](void* data, int i, const char** out_text) {
51 auto &getter = *reinterpret_cast<std::function<const char *(int)> *>(data);
52 const char *s = getter(i);
53 if (s) { *out_text = s; return true; }
54 else { return false; }
55 };
56 return Combo(label, idx, func, reinterpret_cast<void *>(&getter), items_count);
57}
58
59inline bool ListBox(const char* label, int* idx, const std::vector<std::string>& values)
60{
61 if (values.empty()) { return false; }
62 return ListBox(label, idx, vector_getter,
63 const_cast<void *>( static_cast<const void*>(&values) ), (int)values.size());
64}
65
66inline bool InputText(const char* label, std::string &str, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL)
67{
68 return ImGui::InputText( label, &str, flags, callback, user_data );
69}
70
71
75MRVIEWER_API bool DragFloatValid( const char *label, float* value, float speed=1.0f,
76 float min = std::numeric_limits<float>::lowest(),
77 float max = std::numeric_limits<float>::max(),
78 const char* format = "%.3f", ImGuiSliderFlags flags = 0 );
79
81MRVIEWER_API bool DragFloatValidLineWidth( const char* label, float* value );
82
84{
85 bool valueChanged = false; // any of N
86 bool itemDeactivatedAfterEdit = false; //any of N
87 explicit operator bool() const { return valueChanged; }
88};
89
94MRVIEWER_API MultiDragRes DragFloatValid2( const char* label, float v[2], float v_speed = 1.0f,
95 float min = std::numeric_limits<float>::lowest(),
96 float max = std::numeric_limits<float>::max(),
97 const char* format = "%.3f", ImGuiSliderFlags flags = 0,
98 const char* ( *tooltips )[2] = nullptr );
99
104MRVIEWER_API MultiDragRes DragFloatValid3( const char * label, float v[3], float v_speed = 1.0f,
105 float min = std::numeric_limits<float>::lowest(),
106 float max = std::numeric_limits<float>::max(),
107 const char* format = "%.3f", ImGuiSliderFlags flags = 0,
108 const char* (*tooltips)[3] = nullptr );
109
113MRVIEWER_API bool DragIntValid( const char *label, int* value, float speed = 1,
114 int min = std::numeric_limits<int>::lowest(),
115 int max = std::numeric_limits<int>::max(),
116 const char* format = "%d" );
117
122MRVIEWER_API MultiDragRes DragIntValid3( const char* label, int v[3], float speed = 1,
123 int min = std::numeric_limits<int>::lowest(),
124 int max = std::numeric_limits<int>::max(),
125 const char* format = "%d",
126 const char* ( *tooltips )[3] = nullptr );
127
128// similar to ImGui::InputInt but
129// 1) value on output is forced to be in [min,max] range;
130// 2) tooltip about valid range is shown when the item is active
131MRVIEWER_API bool InputIntValid( const char* label, int* value, int min, int max,
132 int step = 1, int step_fast = 100, ImGuiInputTextFlags flags = 0 );
133
134// draw check-box that takes initial value from Getter and then saves the final value in Setter
135template<typename Getter, typename Setter>
136inline bool Checkbox(const char* label, Getter get, Setter set)
137{
138 bool value = get();
139 bool ret = ImGui::Checkbox(label, &value);
140 set(value);
141 return ret;
142}
143
146{
148 float value{};
150 std::string label;
152 std::string tooltip;
153};
154
158MRVIEWER_API void PlotCustomHistogram( const char* str_id,
159 std::function<float( int idx )> values_getter,
160 std::function<void( int idx )> tooltip,
161 std::function<void( int idx )> on_click,
162 int values_count, int values_offset = 0,
163 float scale_min = FLT_MAX, float scale_max = FLT_MAX,
164 ImVec2 frame_size = ImVec2( 0, 0 ), int selectedBarId = -1, int hoveredBarId = -1,
165 const std::vector<HistogramGridLine>& gridIndexes = {},
166 const std::vector<HistogramGridLine>& gridValues = {} );
167
169MRVIEWER_API bool BeginStatePlugin( const char* label, bool* open, float width );
170
173{
174 // All fields those have explicit initializers, even if they have sane default constructors.
175 // This makes it so that Clangd doens't warn when they aren't initialized in partial aggregate initialization.
176
179 bool* collapsed{ nullptr };
181 float width{ 0.0f };
183 float height{ 0.0f };
185 bool allowScrollbar = true;
187 ImVec2* position{ nullptr };
189 ImVec2 pivot{ 0.0f, 0.0f };
191 float menuScaling{ 1.0f };
193 ImGuiWindowFlags flags = ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize;
195 ImVec2* changedSize{ nullptr };
197 std::function<void()> helpBtnFn = nullptr;
199 bool closeWithEscape{ true };
200};
201
204MRVIEWER_API ImVec2 GetDownPosition( const float width );
205
206// Calculate and return the height of the window title
207MRVIEWER_API float GetTitleBarHeght( float menuScaling );
210MRVIEWER_API bool BeginCustomStatePlugin( const char* label, bool* open, const CustomStatePluginWindowParameters& params = {} );
212MRVIEWER_API void EndCustomStatePlugin();
213
215MRVIEWER_API bool BeginModalNoAnimation( const char* label, bool* open = nullptr, ImGuiWindowFlags flags = 0 );
216
222MRVIEWER_API bool InputIntBitSet( const char* label, int* v, const MR::BitSet& bs, int step = 1, int step_fast = 100, ImGuiInputTextFlags flags = 0 );
223
229MRVIEWER_API bool DragInputInt( const char* label, int* value, float speed = 1, int min = std::numeric_limits<int>::lowest(),
230 int max = std::numeric_limits<int>::max(), const char* format = "%d", ImGuiSliderFlags flags = ImGuiSliderFlags_None );
231
236MRVIEWER_API bool Link( const char* label, uint32_t color = MR::Color( 60, 120, 255 ).getUInt32() );
237
241{
242 None = 0,
243 Reset = 1, // reset palette
244 Texture = 2, // texture and legend must be updated
245 Ranges = 4, // uv-coordinates must be recomputed for the same values
246 All = Texture | Ranges | Reset, // 0b111
247};
249
250
257 const char* label,
258 MR::Palette& palette,
259 std::string& presetName,
260 float width,
261 float menuScaling,
262 bool* fixZero = nullptr,
263 float speed = 1.0f,
264 float min = std::numeric_limits<float>::lowest(),
265 float max = std::numeric_limits<float>::max()
266);
267
268// Parameters for the `Plane( MR::PlaneWidget& ... )` function
270{
271 None = 0, // Default setup
272 DisableVisibility = 1 // Don't show "Show Plane" checkbox (and the preceding separator)
273};
275
276
279MRVIEWER_API void Plane( MR::PlaneWidget& planeWidget, float menuScaling, PlaneWidgetFlags flags = {} );
280
285MRVIEWER_API bool Direction( MR::DirectionWidget& dirWidget, bool& editDragging, const std::string& historyName );
286
288MRVIEWER_API void Image( const MR::ImGuiImage& image, const ImVec2& size, const MR::Color& multColor );
289MRVIEWER_API void Image( const MR::ImGuiImage& image, const ImVec2& size, const ImVec4& multColor = { 1, 1, 1, 1 } );
290
292MRVIEWER_API MR::Vector2i GetImagePointerCoord( const MR::ImGuiImage& image, const ImVec2& size, const ImVec2& imagePos );
293
294
296MRVIEWER_API void Spinner( float radius, float scaling );
297
299MRVIEWER_API bool ModalBigTitle( const char* title, float scaling );
300
302MRVIEWER_API bool ModalExitButton( float scaling );
303
305inline float getExpSpeed( float val, float frac = 0.01f, float min = 1e-5f )
306 { return std::max( val * frac, min ); }
307
308} // namespace ImGui
#define MR_MAKE_FLAG_OPERATORS(T)
Definition MRFlagOperators.h:6
container of bits
Definition MRMesh/MRBitSet.h:27
Widget for visualizing the direction.
Definition MRDirectionWidget.h:15
Definition MRImGuiImage.h:14
Definition ImGuiHelpers.h:31
bool Checkbox(const char *label, Getter get, Setter set)
Definition ImGuiHelpers.h:136
MRVIEWER_API MultiDragRes DragIntValid3(const char *label, int v[3], float speed=1, int min=std::numeric_limits< int >::lowest(), int max=std::numeric_limits< int >::max(), const char *format="%d", const char *(*tooltips)[3]=nullptr)
MRVIEWER_API MultiDragRes DragFloatValid3(const char *label, float v[3], float v_speed=1.0f, float min=std::numeric_limits< float >::lowest(), float max=std::numeric_limits< float >::max(), const char *format="%.3f", ImGuiSliderFlags flags=0, const char *(*tooltips)[3]=nullptr)
MRVIEWER_API MR::Vector2i GetImagePointerCoord(const MR::ImGuiImage &image, const ImVec2 &size, const ImVec2 &imagePos)
get image coordinates under cursor considering Y-direction flipping
float getExpSpeed(float val, float frac=0.01f, float min=1e-5f)
get exponential speed for this value
Definition ImGuiHelpers.h:305
MRVIEWER_API bool BeginStatePlugin(const char *label, bool *open, float width)
begin typical state plugin window
PlaneWidgetFlags
Definition ImGuiHelpers.h:270
PaletteChanges
Definition ImGuiHelpers.h:241
MRVIEWER_API bool DragFloatValid(const char *label, float *value, float speed=1.0f, float min=std::numeric_limits< float >::lowest(), float max=std::numeric_limits< float >::max(), const char *format="%.3f", ImGuiSliderFlags flags=0)
MRVIEWER_API void Spinner(float radius, float scaling)
draw spinner in given place, radius with respect to scaling
MRVIEWER_API bool DragInputInt(const char *label, int *value, float speed=1, int min=std::numeric_limits< int >::lowest(), int max=std::numeric_limits< int >::max(), const char *format="%d", ImGuiSliderFlags flags=ImGuiSliderFlags_None)
Combine of ImGui::DragInt and ImGui::InputInt.
MRVIEWER_API void Image(const MR::ImGuiImage &image, const ImVec2 &size, const MR::Color &multColor)
draw image with Y-direction inversed up-down
bool ListBox(const char *label, int *idx, const std::vector< std::string > &values)
Definition ImGuiHelpers.h:59
bool InputText(const char *label, std::string &str, ImGuiInputTextFlags flags=0, ImGuiInputTextCallback callback=NULL, void *user_data=NULL)
Definition ImGuiHelpers.h:66
MRVIEWER_API bool ModalExitButton(float scaling)
draw exit button with close cross (i.e. for settings modal popup )
MRVIEWER_API bool InputIntValid(const char *label, int *value, int min, int max, int step=1, int step_fast=100, ImGuiInputTextFlags flags=0)
MRVIEWER_API PaletteChanges Palette(const char *label, MR::Palette &palette, std::string &presetName, float width, float menuScaling, bool *fixZero=nullptr, float speed=1.0f, float min=std::numeric_limits< float >::lowest(), float max=std::numeric_limits< float >::max())
MRVIEWER_API bool ModalBigTitle(const char *title, float scaling)
draw big title with close cross (i.e. for settings modal popup )
MRVIEWER_API bool DragFloatValidLineWidth(const char *label, float *value)
similar to ImGui::DragFloatValid but use available line width range
MRVIEWER_API float GetTitleBarHeght(float menuScaling)
MRVIEWER_API ImVec2 GetDownPosition(const float width)
MRVIEWER_API void EndCustomStatePlugin()
end state plugin window with custom style
MRVIEWER_API bool Direction(MR::DirectionWidget &dirWidget, bool &editDragging, const std::string &historyName)
MRVIEWER_API bool BeginCustomStatePlugin(const char *label, bool *open, const CustomStatePluginWindowParameters &params={})
MRVIEWER_API bool BeginModalNoAnimation(const char *label, bool *open=nullptr, ImGuiWindowFlags flags=0)
starts modal window with no animation for background
MRVIEWER_API void PlotCustomHistogram(const char *str_id, std::function< float(int idx)> values_getter, std::function< void(int idx)> tooltip, std::function< void(int idx)> on_click, int values_count, int values_offset=0, float scale_min=FLT_MAX, float scale_max=FLT_MAX, ImVec2 frame_size=ImVec2(0, 0), int selectedBarId=-1, int hoveredBarId=-1, const std::vector< HistogramGridLine > &gridIndexes={}, const std::vector< HistogramGridLine > &gridValues={})
MRVIEWER_API bool Link(const char *label, uint32_t color=MR::Color(60, 120, 255).getUInt32())
Draw text as link, calls callback on click.
MRVIEWER_API bool InputIntBitSet(const char *label, int *v, const MR::BitSet &bs, int step=1, int step_fast=100, ImGuiInputTextFlags flags=0)
same as ImGui::InputInt
bool Combo(const char *label, int *idx, const std::vector< std::string > &values)
Definition ImGuiHelpers.h:41
MRVIEWER_API bool DragIntValid(const char *label, int *value, float speed=1, int min=std::numeric_limits< int >::lowest(), int max=std::numeric_limits< int >::max(), const char *format="%d")
MRVIEWER_API void Plane(MR::PlaneWidget &planeWidget, float menuScaling, PlaneWidgetFlags flags={})
MRVIEWER_API MultiDragRes DragFloatValid2(const char *label, float v[2], float v_speed=1.0f, float min=std::numeric_limits< float >::lowest(), float max=std::numeric_limits< float >::max(), const char *format="%.3f", ImGuiSliderFlags flags=0, const char *(*tooltips)[2]=nullptr)
Structure that contains parameters for State plugin window with custom style.
Definition ImGuiHelpers.h:173
std::function< void()> helpBtnFn
reaction on press "Help" button
Definition ImGuiHelpers.h:197
float width
window width (should be already scaled with menuScaling)
Definition ImGuiHelpers.h:181
float height
window height, usually calculated internally (if value is zero)
Definition ImGuiHelpers.h:183
float menuScaling
menu scaling, needed to proper scaling of internal window parts
Definition ImGuiHelpers.h:191
ImGuiWindowFlags flags
window flags, ImGuiWindowFlags_NoScrollbar and ImGuiWindow_NoScrollingWithMouse are forced inside Beg...
Definition ImGuiHelpers.h:193
bool allowScrollbar
If false, will never show the scrollbar.
Definition ImGuiHelpers.h:185
ImVec2 * position
start Position
Definition ImGuiHelpers.h:187
bool closeWithEscape
if true esc button closes the plugin
Definition ImGuiHelpers.h:199
ImVec2 * changedSize
outside owned parameter for windows with resize option
Definition ImGuiHelpers.h:195
ImVec2 pivot
the position of the starting point of the window
Definition ImGuiHelpers.h:189
bool * collapsed
Definition ImGuiHelpers.h:179
helper structure for PlotCustomHistogram describing background grid line and label
Definition ImGuiHelpers.h:146
std::string tooltip
label tooltip
Definition ImGuiHelpers.h:152
std::string label
label text
Definition ImGuiHelpers.h:150
float value
value on the corresponding axis where the line and label are located
Definition ImGuiHelpers.h:148
Definition ImGuiHelpers.h:84
bool itemDeactivatedAfterEdit
Definition ImGuiHelpers.h:86
bool valueChanged
Definition ImGuiHelpers.h:85
Definition MRMesh/MRColor.h:9