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/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
26struct ImGuiWindow;
27
28// Extend ImGui by populating its namespace directly
29//
30// Code snippets taken from there:
31// https://eliasdaler.github.io/using-imgui-with-sfml-pt2/
32namespace ImGui
33{
34
35static auto vector_getter = [] ( void* vec, int idx ) -> const char*
36{
37 auto& vector = *static_cast< std::vector<std::string>* >( vec );
38 if ( idx < 0 || idx >= static_cast< int >( vector.size() ) )
39 {
40 assert( false && "Combo: vector_getter invalid index" );
41 return "";
42 }
43 return vector.at( idx ).c_str();
44};
45
46inline bool Combo(const char* label, int* idx, const std::vector<std::string>& values)
47{
48 if (values.empty()) { return false; }
49 return Combo(label, idx, vector_getter,
50 const_cast<void *>( static_cast<const void*>(&values) ), (int)values.size());
51}
52
53inline bool Combo(const char* label, int* idx, std::function<const char *(int)> getter, int items_count)
54{
55 auto func = [](void* data, int i) -> const char*
56 {
57 auto &getter = *reinterpret_cast<std::function<const char *(int)> *>(data);
58 const char *s = getter(i);
59 if ( s )
60 return s;
61 else
62 {
63 assert( false && "Combo: getter return nullptr" );
64 return "";
65 }
66 };
67 return Combo(label, idx, func, reinterpret_cast<void *>(&getter), items_count);
68}
69
70inline bool ListBox(const char* label, int* idx, const std::vector<std::string>& values)
71{
72 if (values.empty()) { return false; }
73 return ListBox(label, idx, vector_getter,
74 const_cast<void *>( static_cast<const void*>(&values) ), (int)values.size());
75}
76
77inline bool InputText(const char* label, std::string &str, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL)
78{
79 return ImGui::InputText( label, &str, flags, callback, user_data );
80}
81
82
86MRVIEWER_API bool DragFloatValid( const char *label, float* value, float speed=1.0f,
87 float min = std::numeric_limits<float>::lowest(),
88 float max = std::numeric_limits<float>::max(),
89 const char* format = "%.3f", ImGuiSliderFlags flags = 0 );
90
92MRVIEWER_API bool DragFloatValidLineWidth( const char* label, float* value );
93
95{
96 bool valueChanged = false; // any of N
97 bool itemDeactivatedAfterEdit = false; //any of N
98 explicit operator bool() const { return valueChanged; }
99};
100
105MRVIEWER_API MultiDragRes DragFloatValid2( const char* label, float v[2], float v_speed = 1.0f,
106 float min = std::numeric_limits<float>::lowest(),
107 float max = std::numeric_limits<float>::max(),
108 const char* format = "%.3f", ImGuiSliderFlags flags = 0,
109 const char* ( *tooltips )[2] = nullptr );
110
115MRVIEWER_API MultiDragRes DragFloatValid3( const char * label, float v[3], float v_speed = 1.0f,
116 float min = std::numeric_limits<float>::lowest(),
117 float max = std::numeric_limits<float>::max(),
118 const char* format = "%.3f", ImGuiSliderFlags flags = 0,
119 const char* (*tooltips)[3] = nullptr );
120
124MRVIEWER_API bool DragIntValid( const char *label, int* value, float speed = 1,
125 int min = std::numeric_limits<int>::lowest(),
126 int max = std::numeric_limits<int>::max(),
127 const char* format = "%d" );
128
133MRVIEWER_API MultiDragRes DragIntValid3( const char* label, int v[3], float speed = 1,
134 int min = std::numeric_limits<int>::lowest(),
135 int max = std::numeric_limits<int>::max(),
136 const char* format = "%d",
137 const char* ( *tooltips )[3] = nullptr );
138
139// similar to ImGui::InputInt but
140// 1) value on output is forced to be in [min,max] range;
141// 2) tooltip about valid range is shown when the item is active
142MRVIEWER_API bool InputIntValid( const char* label, int* value, int min, int max,
143 int step = 1, int step_fast = 100, ImGuiInputTextFlags flags = 0 );
144
145// draw check-box that takes initial value from Getter and then saves the final value in Setter
146template<typename Getter, typename Setter>
147inline bool Checkbox(const char* label, Getter get, Setter set)
148{
149 bool value = get();
150 bool ret = ImGui::Checkbox(label, &value);
151 set(value);
152 return ret;
153}
154
157{
159 float value{};
161 std::string label;
163 std::string tooltip;
164};
165
169MRVIEWER_API void PlotCustomHistogram( const char* str_id,
170 std::function<float( int idx )> values_getter,
171 std::function<void( int idx )> tooltip,
172 std::function<void( int idx )> on_click,
173 int values_count, int values_offset = 0,
174 float scale_min = FLT_MAX, float scale_max = FLT_MAX,
175 ImVec2 frame_size = ImVec2( 0, 0 ), int selectedBarId = -1, int hoveredBarId = -1,
176 const std::vector<HistogramGridLine>& gridIndexes = {},
177 const std::vector<HistogramGridLine>& gridValues = {} );
178
180MRVIEWER_API bool BeginStatePlugin( const char* label, bool* open, float width );
181
184{
185 // All fields those have explicit initializers, even if they have sane default constructors.
186 // This makes it so that Clangd doens't warn when they aren't initialized in partial aggregate initialization.
187
190 bool* collapsed{ nullptr };
192 float width{ 0.0f };
194 float height{ 0.0f };
196 bool allowScrollbar = true;
198 ImVec2* position{ nullptr };
200 ImVec2 pivot{ 0.0f, 0.0f };
202 ImGuiWindowFlags flags = ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize;
204 ImVec2* changedSize{ nullptr };
206 std::function<void()> customHeaderFn = nullptr;
208 std::function<void()> helpBtnFn = nullptr;
210 bool closeWithEscape{ true };
211};
212
215MRVIEWER_API ImVec2 GetDownPosition( const float width );
216
218MRVIEWER_API float GetTitleBarHeght();
219
227MRVIEWER_API std::pair<ImVec2, bool> LoadSavedWindowPos( const char* label, ImGuiWindow* window, float width, const ImVec2* position = nullptr );
228MRVIEWER_API std::pair<ImVec2, bool> LoadSavedWindowPos( const char* label, float width, const ImVec2* position = nullptr );
229
233MRVIEWER_API void SaveWindowPosition( const char* label, ImGuiWindow* window );
234MRVIEWER_API void SaveWindowPosition( const char* label );
235
238{
240 ImVec2 size = { -1, -1 };
242 const ImVec2* pos = 0;
243 ImGuiWindowFlags flags = 0;
244};
247MRVIEWER_API bool BeginSavedWindowPos( const std::string& name, bool* open, const SavedWindowPosParams& params );
248
251MRVIEWER_API bool BeginCustomStatePlugin( const char* label, bool* open, const CustomStatePluginWindowParameters& params = {} );
253MRVIEWER_API void EndCustomStatePlugin();
254
256MRVIEWER_API bool BeginModalNoAnimation( const char* label, bool* open = nullptr, ImGuiWindowFlags flags = 0 );
257
263MRVIEWER_API bool InputIntBitSet( const char* label, int* v, const MR::BitSet& bs, int step = 1, int step_fast = 100, ImGuiInputTextFlags flags = 0 );
264
270MRVIEWER_API bool DragInputInt( const char* label, int* value, float speed = 1, int min = std::numeric_limits<int>::lowest(),
271 int max = std::numeric_limits<int>::max(), const char* format = "%d", ImGuiSliderFlags flags = ImGuiSliderFlags_None );
272
277MRVIEWER_API bool Link( const char* label, uint32_t color = MR::Color( 60, 120, 255 ).getUInt32() );
278
282{
283 None = 0,
284 Reset = 1, // reset palette
285 Texture = 2, // texture and legend must be updated
286 Ranges = 4, // uv-coordinates must be recomputed for the same values
287 All = Texture | Ranges | Reset, // 0b111
288};
290
291
298 const char* label,
299 MR::Palette& palette,
300 std::string& presetName,
301 float width,
302 bool* fixZero = nullptr,
303 float speed = 1.0f,
304 float min = std::numeric_limits<float>::lowest(),
305 float max = std::numeric_limits<float>::max()
306);
307
308// Parameters for the `Plane( MR::PlaneWidget& ... )` function
310{
311 None = 0, // Default setup
312 DisableVisibility = 1 // Don't show "Show Plane" checkbox (and the preceding separator)
313};
315
316
319MRVIEWER_API void Plane( MR::PlaneWidget& planeWidget, PlaneWidgetFlags flags = {} );
320
325MRVIEWER_API bool Direction( MR::DirectionWidget& dirWidget, bool& editDragging, const std::string& historyName );
326
328MRVIEWER_API void Image( const MR::ImGuiImage& image, const ImVec2& size, const MR::Color& multColor );
329MRVIEWER_API void Image( const MR::ImGuiImage& image, const ImVec2& size, const ImVec4& multColor = { 1, 1, 1, 1 } );
330
332MRVIEWER_API MR::Vector2i GetImagePointerCoord( const MR::ImGuiImage& image, const ImVec2& size, const ImVec2& imagePos );
333
334
336MRVIEWER_API void Spinner( float radius );
337
339MRVIEWER_API bool ModalBigTitle( const char* title );
340
342MRVIEWER_API bool ModalExitButton();
343
345inline float getExpSpeed( float val, float frac = 0.01f, float min = 1e-5f )
346 { return std::max( val * frac, min ); }
347
349inline float getLuminance( const ImVec4& col )
350{
351 return 0.2126f * col.x + 0.7152f * col.y + 0.0722f * col.z;
352}
353
354#ifdef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
357MRVIEWER_API ImVec2 GetWindowContentRegionMax();
358
361inline ImVec2 GetContentRegionMax()
362{
363 return GetContentRegionAvail() + GetCursorScreenPos() - GetWindowPos();
364}
365#endif
366
367} // namespace ImGui
368
#define MR_MAKE_FLAG_OPERATORS(T)
Definition MRFlagOperators.h:6
Definition MRMesh/MRBitSet.h:23
Widget for visualizing the direction.
Definition MRDirectionWidget.h:15
Definition MRImGuiImage.h:15
Definition ImGuiHelpers.h:33
bool Checkbox(const char *label, Getter get, Setter set)
Definition ImGuiHelpers.h:147
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 void SaveWindowPosition(const char *label, ImGuiWindow *window)
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
MRVIEWER_API void Plane(MR::PlaneWidget &planeWidget, PlaneWidgetFlags flags={})
float getExpSpeed(float val, float frac=0.01f, float min=1e-5f)
get exponential speed for this value
Definition ImGuiHelpers.h:345
MRVIEWER_API bool BeginStatePlugin(const char *label, bool *open, float width)
begin typical state plugin window
PlaneWidgetFlags
Definition ImGuiHelpers.h:310
PaletteChanges
Definition ImGuiHelpers.h:282
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 bool ModalExitButton()
draw exit button with close cross (i.e. for settings modal popup )
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:70
bool InputText(const char *label, std::string &str, ImGuiInputTextFlags flags=0, ImGuiInputTextCallback callback=NULL, void *user_data=NULL)
Definition ImGuiHelpers.h:77
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 bool DragFloatValidLineWidth(const char *label, float *value)
similar to ImGui::DragFloatValid but use available line width range
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 float GetTitleBarHeght()
Calculate and return the height of the window title.
MRVIEWER_API bool BeginModalNoAnimation(const char *label, bool *open=nullptr, ImGuiWindowFlags flags=0)
starts modal window with no animation for background
MRVIEWER_API PaletteChanges Palette(const char *label, MR::Palette &palette, std::string &presetName, float width, bool *fixZero=nullptr, float speed=1.0f, float min=std::numeric_limits< float >::lowest(), float max=std::numeric_limits< float >::max())
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 BeginSavedWindowPos(const std::string &name, bool *open, const SavedWindowPosParams &params)
MRVIEWER_API std::pair< ImVec2, bool > LoadSavedWindowPos(const char *label, ImGuiWindow *window, float width, const ImVec2 *position=nullptr)
MRVIEWER_API void Spinner(float radius)
draw spinner in given place, radius with respect to scaling
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
MRVIEWER_API bool ModalBigTitle(const char *title)
draw big title with close cross (i.e. for settings modal popup )
bool Combo(const char *label, int *idx, const std::vector< std::string > &values)
Definition ImGuiHelpers.h:46
float getLuminance(const ImVec4 &col)
A crude conversion to grayscale. Good enough for our purposes.
Definition ImGuiHelpers.h:349
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 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:184
std::function< void()> helpBtnFn
reaction on press "Help" button
Definition ImGuiHelpers.h:208
float width
window width (should be already scaled with UI::scale())
Definition ImGuiHelpers.h:192
float height
window height, usually calculated internally (if value is zero)
Definition ImGuiHelpers.h:194
std::function< void()> customHeaderFn
draw custom header items immediately after the caption
Definition ImGuiHelpers.h:206
ImGuiWindowFlags flags
window flags, ImGuiWindowFlags_NoScrollbar and ImGuiWindow_NoScrollingWithMouse are forced inside Beg...
Definition ImGuiHelpers.h:202
bool allowScrollbar
If false, will never show the scrollbar.
Definition ImGuiHelpers.h:196
ImVec2 * position
start Position
Definition ImGuiHelpers.h:198
bool closeWithEscape
if true esc button closes the plugin
Definition ImGuiHelpers.h:210
ImVec2 * changedSize
outside owned parameter for windows with resize option
Definition ImGuiHelpers.h:204
ImVec2 pivot
the position of the starting point of the window
Definition ImGuiHelpers.h:200
bool * collapsed
Definition ImGuiHelpers.h:190
helper structure for PlotCustomHistogram describing background grid line and label
Definition ImGuiHelpers.h:157
std::string tooltip
label tooltip
Definition ImGuiHelpers.h:163
std::string label
label text
Definition ImGuiHelpers.h:161
float value
value on the corresponding axis where the line and label are located
Definition ImGuiHelpers.h:159
Definition ImGuiHelpers.h:95
bool itemDeactivatedAfterEdit
Definition ImGuiHelpers.h:97
bool valueChanged
Definition ImGuiHelpers.h:96
Parameters drawing classic ImGui::Begin with loading / saving window position.
Definition ImGuiHelpers.h:238
const ImVec2 * pos
(optional) preliminary window position
Definition ImGuiHelpers.h:242
ImVec2 size
window size
Definition ImGuiHelpers.h:240
ImGuiWindowFlags flags
Definition ImGuiHelpers.h:243
Definition MRMesh/MRColor.h:9