MeshLib Documentation
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"
14#include "MRViewer/MRUnits.h"
15#include <imgui.h>
16#include <misc/cpp/imgui_stdlib.h>
17#include <algorithm>
18#include <functional>
19#include <cstddef>
20#include <limits>
21#include <string>
22#include <vector>
23#include <optional>
24
25namespace MR
26{
27class Palette;
28class ImGuiImage;
29class PlaneWidget;
30}
31
32// Extend ImGui by populating its namespace directly
33//
34// Code snippets taken from there:
35// https://eliasdaler.github.io/using-imgui-with-sfml-pt2/
36namespace ImGui
37{
38
39static auto vector_getter = [](void* vec, int idx, const char** out_text)
40{
41 auto& vector = *static_cast<std::vector<std::string>*>(vec);
42 if (idx < 0 || idx >= static_cast<int>(vector.size())) { return false; }
43 *out_text = vector.at(idx).c_str();
44 return true;
45};
46
47inline bool Combo(const char* label, int* idx, const std::vector<std::string>& values)
48{
49 if (values.empty()) { return false; }
50 return Combo(label, idx, vector_getter,
51 const_cast<void *>( static_cast<const void*>(&values) ), (int)values.size());
52}
53
54inline bool Combo(const char* label, int* idx, std::function<const char *(int)> getter, int items_count)
55{
56 auto func = [](void* data, int i, const char** out_text) {
57 auto &getter = *reinterpret_cast<std::function<const char *(int)> *>(data);
58 const char *s = getter(i);
59 if (s) { *out_text = s; return true; }
60 else { return false; }
61 };
62 return Combo(label, idx, func, reinterpret_cast<void *>(&getter), items_count);
63}
64
65inline bool ListBox(const char* label, int* idx, const std::vector<std::string>& values)
66{
67 if (values.empty()) { return false; }
68 return ListBox(label, idx, vector_getter,
69 const_cast<void *>( static_cast<const void*>(&values) ), (int)values.size());
70}
71
72inline bool InputText(const char* label, std::string &str, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL)
73{
74 return ImGui::InputText( label, &str, flags, callback, user_data );
75}
76
77
81MRVIEWER_API bool DragFloatValid( const char *label, float* value, float speed=1.0f,
82 float min = std::numeric_limits<float>::lowest(),
83 float max = std::numeric_limits<float>::max(),
84 const char* format = "%.3f", ImGuiSliderFlags flags = 0 );
85
87MRVIEWER_API bool DragFloatValidLineWidth( const char* label, float* value );
88
90{
91 bool valueChanged = false; // any of N
92 bool itemDeactivatedAfterEdit = false; //any of N
93 explicit operator bool() const { return valueChanged; }
94};
95
100MRVIEWER_API MultiDragRes DragFloatValid2( const char* label, float v[2], float v_speed = 1.0f,
101 float min = std::numeric_limits<float>::lowest(),
102 float max = std::numeric_limits<float>::max(),
103 const char* format = "%.3f", ImGuiSliderFlags flags = 0,
104 const char* ( *tooltips )[2] = nullptr );
105
110MRVIEWER_API MultiDragRes DragFloatValid3( const char * label, float v[3], float v_speed = 1.0f,
111 float min = std::numeric_limits<float>::lowest(),
112 float max = std::numeric_limits<float>::max(),
113 const char* format = "%.3f", ImGuiSliderFlags flags = 0,
114 const char* (*tooltips)[3] = nullptr );
115
119MRVIEWER_API bool DragIntValid( const char *label, int* value, float speed = 1,
120 int min = std::numeric_limits<int>::lowest(),
121 int max = std::numeric_limits<int>::max(),
122 const char* format = "%d" );
123
128MRVIEWER_API MultiDragRes DragIntValid3( const char* label, int v[3], float speed = 1,
129 int min = std::numeric_limits<int>::lowest(),
130 int max = std::numeric_limits<int>::max(),
131 const char* format = "%d",
132 const char* ( *tooltips )[3] = nullptr );
133
134// similar to ImGui::InputInt but
135// 1) value on output is forced to be in [min,max] range;
136// 2) tooltip about valid range is shown when the item is active
137MRVIEWER_API bool InputIntValid( const char* label, int* value, int min, int max,
138 int step = 1, int step_fast = 100, ImGuiInputTextFlags flags = 0 );
139
140// draw check-box that takes initial value from Getter and then saves the final value in Setter
141template<typename Getter, typename Setter>
142inline bool Checkbox(const char* label, Getter get, Setter set)
143{
144 bool value = get();
145 bool ret = ImGui::Checkbox(label, &value);
146 set(value);
147 return ret;
148}
149
152{
154 float value{};
156 std::string label;
158 std::string tooltip;
159};
160
164MRVIEWER_API void PlotCustomHistogram( const char* str_id,
165 std::function<float( int idx )> values_getter,
166 std::function<void( int idx )> tooltip,
167 std::function<void( int idx )> on_click,
168 int values_count, int values_offset = 0,
169 float scale_min = FLT_MAX, float scale_max = FLT_MAX,
170 ImVec2 frame_size = ImVec2( 0, 0 ), int selectedBarId = -1, int hoveredBarId = -1,
171 const std::vector<HistogramGridLine>& gridIndexes = {},
172 const std::vector<HistogramGridLine>& gridValues = {} );
173
175MRVIEWER_API bool BeginStatePlugin( const char* label, bool* open, float width );
176
179{
182 bool* collapsed{ nullptr };
184 float width{ 0.0f };
186 float height{ 0.0f };
188 bool allowScrollbar = true;
190 ImVec2* position{ nullptr };
192 ImVec2 pivot{ 0.0f, 0.0f };
194 float menuScaling{ 1.0f };
196 ImGuiWindowFlags flags = ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize;
198 ImVec2* changedSize{ nullptr };
200 std::function<void()> helpBtnFn;
202 bool closeWithEscape{ true };
203};
204
207MRVIEWER_API ImVec2 GetDownPosition( const float width );
208
209// Calculate and return the height of the window title
210MRVIEWER_API float GetTitleBarHeght( float menuScaling );
213MRVIEWER_API bool BeginCustomStatePlugin( const char* label, bool* open, const CustomStatePluginWindowParameters& params = {} );
215MRVIEWER_API void EndCustomStatePlugin();
216
218MRVIEWER_API bool BeginModalNoAnimation( const char* label, bool* open = nullptr, ImGuiWindowFlags flags = 0 );
219
225MRVIEWER_API bool InputIntBitSet( const char* label, int* v, const MR::BitSet& bs, int step = 1, int step_fast = 100, ImGuiInputTextFlags flags = 0 );
226
232MRVIEWER_API bool DragInputInt( const char* label, int* value, float speed = 1, int min = std::numeric_limits<int>::lowest(),
233 int max = std::numeric_limits<int>::max(), const char* format = "%d", ImGuiSliderFlags flags = ImGuiSliderFlags_None );
234
239MRVIEWER_API bool Link( const char* label, uint32_t color = MR::Color( 60, 120, 255 ).getUInt32() );
240
244{
245 None = 0,
246 Texture = 1, // texture and legend must be updated
247 Ranges = 2, // uv-coordinates must be recomputed for the same values
248 All = Texture | Ranges // 0b11
249};
251
252
259 const char* label,
260 MR::Palette& palette,
261 std::string& presetName,
262 float width,
263 float menuScaling,
264 bool* fixZero = nullptr,
265 float speed = 1.0f,
266 float min = std::numeric_limits<float>::lowest(),
267 float max = std::numeric_limits<float>::max()
268);
269
270// Parameters for the `Plane( MR::PlaneWidget& ... )` function
272{
273 None = 0, // Default setup
274 DisableVisibility = 1 // Don't show "Show Plane" checkbox (and the preceding separator)
275};
277
278
281MRVIEWER_API void Plane( MR::PlaneWidget& planeWidget, float menuScaling, PlaneWidgetFlags flags = {} );
282
283
285MRVIEWER_API void Image( const MR::ImGuiImage& image, const ImVec2& size, const MR::Color& multColor );
286MRVIEWER_API void Image( const MR::ImGuiImage& image, const ImVec2& size, const ImVec4& multColor = { 1, 1, 1, 1 } );
287
289MRVIEWER_API MR::Vector2i GetImagePointerCoord( const MR::ImGuiImage& image, const ImVec2& size, const ImVec2& imagePos );
290
291
293MRVIEWER_API void Spinner( float radius, float scaling );
294
296MRVIEWER_API bool ModalBigTitle( const char* title, float scaling );
297
299MRVIEWER_API bool ModalExitButton( float scaling );
300
302inline float getExpSpeed( float val, float frac = 0.01f, float min = 1e-5f )
303 { return std::max( val * frac, min ); }
304
305} // namespace ImGui
#define MR_MAKE_FLAG_OPERATORS(T)
Definition MRFlagOperators.h:6
container of bits
Definition MRMesh/MRBitSet.h:27
Definition MRImGuiImage.h:14
Definition ImGuiHelpers.h:37
bool Checkbox(const char *label, Getter get, Setter set)
Definition ImGuiHelpers.h:142
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:302
MRVIEWER_API bool BeginStatePlugin(const char *label, bool *open, float width)
begin typical state plugin window
PlaneWidgetFlags
Definition ImGuiHelpers.h:272
PaletteChanges
Definition ImGuiHelpers.h:244
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:65
bool InputText(const char *label, std::string &str, ImGuiInputTextFlags flags=0, ImGuiInputTextCallback callback=NULL, void *user_data=NULL)
Definition ImGuiHelpers.h:72
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 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:47
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)
Definition MRCameraOrientationPlugin.h:8
Structure that contains parameters for State plugin window with custom style.
Definition ImGuiHelpers.h:179
std::function< void()> helpBtnFn
reaction on press "Help" button
Definition ImGuiHelpers.h:200
float width
window width (should be already scaled with menuScaling)
Definition ImGuiHelpers.h:184
float height
window height, usually calculated internally (if value is zero)
Definition ImGuiHelpers.h:186
float menuScaling
menu scaling, needed to proper scaling of internal window parts
Definition ImGuiHelpers.h:194
ImGuiWindowFlags flags
window flags, ImGuiWindowFlags_NoScrollbar and ImGuiWindow_NoScrollingWithMouse are forced inside Beg...
Definition ImGuiHelpers.h:196
bool allowScrollbar
If false, will never show the scrollbar.
Definition ImGuiHelpers.h:188
ImVec2 * position
start Position
Definition ImGuiHelpers.h:190
bool closeWithEscape
if true esc button closes the plugin
Definition ImGuiHelpers.h:202
ImVec2 * changedSize
outside owned parameter for windows with resize option
Definition ImGuiHelpers.h:198
ImVec2 pivot
the position of the starting point of the window
Definition ImGuiHelpers.h:192
bool * collapsed
Definition ImGuiHelpers.h:182
helper structure for PlotCustomHistogram describing background grid line and label
Definition ImGuiHelpers.h:152
std::string tooltip
label tooltip
Definition ImGuiHelpers.h:158
std::string label
label text
Definition ImGuiHelpers.h:156
float value
value on the corresponding axis where the line and label are located
Definition ImGuiHelpers.h:154
Definition ImGuiHelpers.h:90
bool itemDeactivatedAfterEdit
Definition ImGuiHelpers.h:92
bool valueChanged
Definition ImGuiHelpers.h:91
Definition MRColor.h:9