MeshLib C++ Docs
Loading...
Searching...
No Matches
ImGuiMenu.h
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
10#include "MRViewerPlugin.h"
11#include "MRViewerEventsListener.h"
12#include "MRNotificationType.h"
13#include "MRSignalCombiners.h"
14#include "MRShowModal.h"
15#include "imgui.h"
16#include "MRMesh/MRIRenderObject.h" //only for BasicUiRenderTask::BackwardPassParams
17#include "MRMesh/MRFlagOperators.h"
18#include "MRMesh/MRBox.h"
19#include "MRMesh/MRColor.h"
20#include <optional>
21#include <unordered_map>
22
23// Forward declarations
24struct ImGuiContext;
25struct ImGuiWindow;
26
27namespace MR
28{
29
30class ShortcutManager;
31class MeshModifier;
32struct UiRenderManager;
33class SceneObjectsListDrawer;
34class ObjectComparableWithReference;
35
36enum class SelectedTypesMask
37{
38 ObjectBit = 1 << 0,
39 ObjectPointsHolderBit = 1 << 1,
40 ObjectLinesHolderBit = 1 << 2,
41 ObjectMeshHolderBit = 1 << 3,
42 ObjectLabelBit = 1 << 4,
43 ObjectMeshBit = 1 << 5,
44 ObjectFeatureBit = 1 << 6,
45 ObjectMeasurementBit = 1 << 7,
46};
47MR_MAKE_FLAG_OPERATORS( SelectedTypesMask )
48
49class MRVIEWER_CLASS ImGuiMenu : public MR::ViewerPlugin,
50 public MultiListener<
58{
67protected:
68 // Hidpi scaling to be used for text rendering.
69 float hidpi_scaling_;
70
71 // Ratio between the framebuffer size and the window size.
72 // May be different from the hipdi scaling!
73 float pixel_ratio_;
74
75 // user defined additional scaling modifier
76 float userScaling_ = 1.0f;
77
78 // ImGui Context
79 ImGuiContext * context_ = nullptr;
80 // last focused plugin window
81 ImGuiWindow* prevFrameFocusPlugin_ = nullptr;
82
83 // if true, then pre_draw will start from polling glfw events
84 bool pollEventsInPreDraw = false; // be careful here with true, this can cause infinite recurse
85
86 bool showShortcuts_{ false };
87 bool showStatistics_{ false };
88 long long frameTimeMillisecThreshold_{ 25 };
89 bool showRenameModal_{ false };
90 std::string renameBuffer_;
91 std::string popUpRenameBuffer_;
92 bool needModalBgChange_{ false };
93 bool showInfoModal_{ false };
94 std::string storedModalMessage_;
95 NotificationType modalMessageType_{ NotificationType::Error };
96 std::shared_ptr<ShortcutManager> shortcutManager_;
97
98 ImVec2 sceneWindowPos_;
99 ImVec2 sceneWindowSize_;
100 ImVec2 mainWindowPos_;
101 ImVec2 mainWindowSize_;
102
103 MRVIEWER_API virtual void setupShortcuts_();
104
105 bool savedDialogPositionEnabled_{ false };
106
107 std::weak_ptr<Object> lastRenameObj_;
108 Box3f selectionBbox_; // updated in drawSelectionInformation_
109 Box3f selectionWorldBox_;
110
112 {
113 std::string lastLabel;
114 std::string labelBuffer;
115 std::shared_ptr<ObjectLabel> obj{ nullptr };
116 } oldLabelParams_;
117
118 bool allowRemoval_{ true };
119 bool uniformScale_{ true };
120 bool xfHistUpdated_{ false };
121 bool invertedRotation_{ false };
122
123 std::optional<std::pair<std::string, Vector4f>> storedColor_;
124 Vector4f getStoredColor_( const std::string& str, const Color& defaultColor ) const;
125
126 std::string searchPluginsString_;
127
128 std::vector<std::shared_ptr<MR::MeshModifier>> modifiers_;
129
130 enum ViewportConfigurations
131 {
132 Single,
133 Horizontal, // left viewport, right viewport
134 Vertical, // lower viewport, upper viewport
135 Quad // left lower vp, left upper vp, right lower vp, right upper vp
136 } viewportConfig_{ Single };
137
138 // flag to correctly update scroll on transform window appearing
139 bool selectionChangedToSingleObj_{ false };
140 // menu will change objects' colors in this viewport
141 ViewportId selectedViewport_ = {};
142
143 // When editing feature properties, this is the target object.
144 std::weak_ptr<Object> editedFeatureObject_;
145 // When editing feature properties, this is the original xf of the target object, for history purposes.
146 AffineXf3f editedFeatureObjectOldXf_;
147
148 // state for the Edit Tag modal dialog
150 {
151 std::string initName;
152 std::string name;
153 bool initHasFrontColor = false;
154 bool hasFrontColor = false;
155 ImVec4 selectedColor;
156 ImVec4 unselectedColor;
157 };
158 TagEditorState tagEditorState_;
159 // whether to open the Edit Tag modal dialog
160 bool showEditTag_ = false;
161 // buffer string for the tag name input widget
162 std::string tagNewName_;
163
164public:
165 MRVIEWER_API static const std::shared_ptr<ImGuiMenu>& instance();
166
167 MRVIEWER_API virtual void init(MR::Viewer *_viewer) override;
168
169 // inits glfw and glsl backend
170 MRVIEWER_API virtual void initBackend();
171
172 // call this to validate imgui context in the begining of the frame
173 MRVIEWER_API virtual void startFrame();
174 // call this to draw valid imgui context at the end of the frame
175 MRVIEWER_API virtual void finishFrame();
176
177 MRVIEWER_API virtual void load_font(int font_size = 13);
178 MRVIEWER_API virtual void reload_font(int font_size = 13);
179
180 MRVIEWER_API virtual void shutdown() override;
181
182 // Draw menu
183 MRVIEWER_API virtual void draw_menu();
184
185 MRVIEWER_API void draw_helpers();
186
187 // Can be overwritten by `callback_draw_viewer_window`
188 MRVIEWER_API virtual void draw_viewer_window();
189
190 // Can be overwritten by `callback_draw_custom_window`
191 virtual void draw_custom_window() {}
192
193 // Easy-to-customize callbacks
194 std::function<void(void)> callback_draw_viewer_window;
195 std::function<void(void)> callback_draw_viewer_menu;
196 std::function<void(void)> callback_draw_custom_window;
197
198 void draw_labels_window();
199
200 MRVIEWER_API void draw_text(
201 const Viewport& viewport,
202 const Vector3f& pos,
203 const Vector3f& normal,
204 const std::string& text,
205 const Color& color,
206 bool clipByViewport );
207
208 MRVIEWER_API float pixel_ratio();
209
210 MRVIEWER_API float hidpi_scaling();
211
212 MRVIEWER_API float menu_scaling() const;
213
214 // returns UI scaling modifier specified by user
215 float getUserScaling() const { return userScaling_; }
216 // sets UI scaling modifier specified by user
217 MRVIEWER_API void setUserScaling( float scaling );
218
219 MRVIEWER_API ImGuiContext* getCurrentContext() const;
220
221 ImGuiWindow* getLastFocusedPlugin() const { return prevFrameFocusPlugin_; };
222
223 // opens Error / Warning / Info modal window with message text
224 MRVIEWER_API virtual void showModalMessage( const std::string& msg, NotificationType msgType );
225
226 MRVIEWER_API virtual std::filesystem::path getMenuFontPath() const;
227
228 // setup maximum good time for frame rendering (if rendering is slower it will become red in statistics window)
229 MRVIEWER_API void setDrawTimeMillisecThreshold( long long maxGoodTimeMillisec );
230
231 // Draw scene list window with content
232 MRVIEWER_API void draw_scene_list();
233 // Draw scene list content only
234 MRVIEWER_API void draw_scene_list_content( const std::vector<std::shared_ptr<Object>>& selected, const std::vector<std::shared_ptr<Object>>& all );
235
236 // override this to have custom "Selection Properties" window
237 // draw window with content
238 MRVIEWER_API virtual void draw_selection_properties( const std::vector<std::shared_ptr<Object>>& selected );
239 // override this to have custom "Selection Properties" content
240 // draw content only
241 MRVIEWER_API virtual void draw_selection_properties_content( const std::vector<std::shared_ptr<Object>>& selected );
242 // override this to have custom UI in "Selection Properties" window (under "Draw Options")
243
244 // override this to customize appearance of collapsing headers
245 MRVIEWER_API virtual bool drawCollapsingHeader_( const char* label, ImGuiTreeNodeFlags flags = 0);
246 // override this to customize appearance of collapsing headers for transform block
247 MRVIEWER_API virtual bool drawCollapsingHeaderTransform_();
248
249 bool make_visualize_checkbox( std::vector<std::shared_ptr<VisualObject>> selectedVisualObjs, const char* label, AnyVisualizeMaskEnum type, MR::ViewportMask viewportid, bool invert = false );
250 template<typename ObjectT>
251 void make_color_selector( std::vector<std::shared_ptr<ObjectT>> selectedVisualObjs, const char* label,
252 std::function<Vector4f( const ObjectT* )> getter,
253 std::function<void( ObjectT*, const Vector4f& )> setter );
254 template<typename ObjType,typename ValueT>
255 void make_width( std::vector<std::shared_ptr<VisualObject>> selectedVisualObjs, const char* label,
256 std::function<ValueT( const ObjType* )> getter,
257 std::function<void( ObjType*, const ValueT& )> setter );
258
259 void make_light_strength( std::vector<std::shared_ptr<VisualObject>> selectedVisualObjs, const char* label,
260 std::function<float( const VisualObject* )> getter,
261 std::function<void( VisualObject*, const float& )> setter);
262
263 template <typename T, typename ObjectType>
264 void make_slider( std::vector<std::shared_ptr<ObjectType>> selectedVisualObjs, const char* label,
265 std::function<T( const ObjectType* )> getter,
266 std::function<void( ObjectType*, T )> setter, T min, T max );
267
268 void make_points_discretization( std::vector<std::shared_ptr<VisualObject>> selectedVisualObjs, const char* label,
269 std::function<int( const ObjectPointsHolder* )> getter,
270 std::function<void( ObjectPointsHolder*, const int& )> setter );
271
272 std::shared_ptr<ShortcutManager> getShortcutManager() { return shortcutManager_; };
273
274 MRVIEWER_API void add_modifier( std::shared_ptr<MR::MeshModifier> modifier );
275
276 MRVIEWER_API void allowSceneReorder( bool allow );
277 bool checkPossibilityObjectRemoval() { return allowRemoval_; };
278
279 MRVIEWER_API void allowObjectsRemoval( bool allow );
280
281 MRVIEWER_API void tryRenameSelectedObject();
282
283 MRVIEWER_API void setObjectTreeState( const Object* obj, bool open );
284
286 MRVIEWER_API void expandObjectTreeAndScroll( const Object* obj );
287
288 //set show shortcuts state (enable / disable)
289 MRVIEWER_API void setShowShortcuts( bool val );
290 //return show shortcuts state (enable / disable)
291 MRVIEWER_API bool getShowShortcuts() const;
292
293 // enables using of saved positions of plugin windows in the config file
294 void enableSavedDialogPositions( bool on ) { savedDialogPositionEnabled_ = on; }
295 // returns true if enabled using of saved positions of plugin windows in the config file, false otherwise
296 bool isSavedDialogPositionsEnabled() const { return savedDialogPositionEnabled_; }
297
298 // This class helps the viewer to `renderUi()` from `IRenderObject`s.
299 MRVIEWER_API virtual UiRenderManager& getUiRenderManager();
300
301 MRVIEWER_API const std::shared_ptr<SceneObjectsListDrawer>& getSceneObjectsList() { return sceneObjectsList_; };
302
303 enum class NameTagSelectionMode
304 {
305 // Click without modifiers, selects one object and unselects all others.
306 selectOne,
307 // Ctrl+Click, toggles the selection of one object.
308 toggle,
309 };
310 using NameTagClickSignal = boost::signals2::signal<bool( Object& object, NameTagSelectionMode mode ), StopOnTrueCombiner>;
311 // This is triggered whenever a name tag of an object is clicked.
312 NameTagClickSignal nameTagClickSignal;
313 // Behaves as if the user clicked the object name tag, by invoking `nameTagClickSignal`.
314 MRVIEWER_API bool simulateNameTagClick( Object& object, NameTagSelectionMode mode );
315 // This version uses the currently held keyboard modifiers instead of a custom `mode`.
316 MRVIEWER_API bool simulateNameTagClickWithKeyboardModifiers( Object& object );
317
318 using DrawSceneUiSignal = boost::signals2::signal<void( ViewportId viewportId, UiRenderParams::UiTaskList& tasks )>;
319 // This is called every frame for every viewport. Use this to draw UI bits on top of the scene.
320 DrawSceneUiSignal drawSceneUiSignal;
321
322 // Scene pick should be disabled because an ImGui window is in the way.
323 MRVIEWER_API bool anyImGuiWindowIsHovered() const;
324 // Scene pick should be disabled because a `renderUi()` UI of some object is in the way.
325 MRVIEWER_API bool anyUiObjectIsHovered() const;
326
327 // ======== selected objects options drawing
328 // getting the mask of the list of selected objects
329 MRVIEWER_API SelectedTypesMask calcSelectedTypesMask( const std::vector<std::shared_ptr<Object>>& selectedObjs );
330 MRVIEWER_API bool drawGeneralOptions( const std::vector<std::shared_ptr<Object>>& selectedObjs );
331 MRVIEWER_API bool drawAdvancedOptions( const std::vector<std::shared_ptr<VisualObject>>& selectedObjs, SelectedTypesMask selectedMask );
332 MRVIEWER_API bool drawRemoveButton( const std::vector<std::shared_ptr<Object>>& selectedObjs );
333 MRVIEWER_API bool drawDrawOptionsCheckboxes( const std::vector<std::shared_ptr<VisualObject>>& selectedObjs, SelectedTypesMask selectedMask );
334 MRVIEWER_API bool drawDrawOptionsColors( const std::vector<std::shared_ptr<VisualObject>>& selectedObjs );
335
338 {
346 float itemWidth {};
348 float item2Width {};
350 float item3Width {};
351 };
352
353protected:
354 MRVIEWER_API virtual void drawModalMessage_();
355
356 bool capturedMouse_{ false };
357 // Mouse IO
358 MRVIEWER_API virtual bool onMouseDown_( MouseButton button, int modifier ) override;
359 MRVIEWER_API virtual bool onMouseUp_( MouseButton button, int modifier ) override;
360 MRVIEWER_API virtual bool onMouseMove_( int mouse_x, int mouse_y ) override;
361 MRVIEWER_API virtual bool onMouseScroll_( float delta_y ) override;
362 MRVIEWER_API virtual void cursorEntrance_( bool entered ) override;
363 // Keyboard IO
364 MRVIEWER_API virtual bool onCharPressed_( unsigned key, int modifiers ) override;
365 MRVIEWER_API virtual bool onKeyDown_( int key, int modifiers ) override;
366 MRVIEWER_API virtual bool onKeyUp_( int key, int modifiers ) override;
367 MRVIEWER_API virtual bool onKeyRepeat_( int key, int modifiers ) override;
368 // Scene events
369 MRVIEWER_API virtual void postResize_( int width, int height ) override;
370 MRVIEWER_API virtual void postRescale_( float x, float y) override;
371 // Spacemouse events
372 MRVIEWER_API virtual bool spaceMouseMove_( const Vector3f& translate, const Vector3f& rotate ) override;
373 MRVIEWER_API virtual bool spaceMouseDown_( int key ) override;
374 // Touchpad gesture events
375 MRVIEWER_API virtual bool touchpadRotateGestureBegin_() override;
376 MRVIEWER_API virtual bool touchpadRotateGestureUpdate_( float angle ) override;
377 MRVIEWER_API virtual bool touchpadRotateGestureEnd_() override;
378 MRVIEWER_API virtual bool touchpadSwipeGestureBegin_() override;
379 MRVIEWER_API virtual bool touchpadSwipeGestureUpdate_( float deltaX, float deltaY, bool kinetic ) override;
380 MRVIEWER_API virtual bool touchpadSwipeGestureEnd_() override;
381 MRVIEWER_API virtual bool touchpadZoomGestureBegin_() override;
382 MRVIEWER_API virtual bool touchpadZoomGestureUpdate_( float scale, bool kinetic ) override;
383 MRVIEWER_API virtual bool touchpadZoomGestureEnd_() override;
384 // Other events
385 MRVIEWER_API virtual void postFocus_( bool focused ) override;
386
387 // This function reset ImGui style to current theme and scale it by menu_scaling
388 // called in ImGuiMenu::postRescale_()
389 MRVIEWER_API virtual void rescaleStyle_();
390
391 MRVIEWER_API float drawSelectionInformation_();
392 MRVIEWER_API void drawFeaturePropertiesEditor_( const std::shared_ptr<Object>& object );
393
394 MRVIEWER_API void drawComparablePropertiesEditor_( ObjectComparableWithReference& object );
395
397 MRVIEWER_API virtual void drawCustomSelectionInformation_( const std::vector<std::shared_ptr<Object>>& selected, const SelectionInformationStyle& style );
398
399 MRVIEWER_API virtual void draw_custom_selection_properties( const std::vector<std::shared_ptr<Object>>& selected );
400
401 MRVIEWER_API void drawTagInformation_( const std::vector<std::shared_ptr<Object>>& selected );
402
403 MRVIEWER_API float drawTransform_();
404
405 MRVIEWER_API virtual bool drawTransformContextMenu_( const std::shared_ptr<Object>& /*selected*/ ) { return false; }
406
407 // A virtual function for drawing of the dialog with shortcuts. It can be overriden in the inherited classes
408 MRVIEWER_API virtual void drawShortcutsWindow_();
409 // returns width of items in Scene Info window
410 MRVIEWER_API float getSceneInfoItemWidth_( int itemCount = 1 );
411
413 {
414 public:
415 MRVIEWER_API void preRenderViewport( ViewportId viewport ) override;
416 MRVIEWER_API void postRenderViewport( ViewportId viewport ) override;
417 MRVIEWER_API BasicUiRenderTask::BackwardPassParams beginBackwardPass( ViewportId viewport, UiRenderParams::UiTaskList& tasks ) override;
418 MRVIEWER_API void finishBackwardPass( ViewportId viewport, const BasicUiRenderTask::BackwardPassParams& params ) override;
419
420 // Which things are blocked by our `renderUi()` calls.
421 BasicUiRenderTask::InteractionMask consumedInteractions{};
422
423 // If this returns false, the event should be allowed to pass through to other plugins, even if ImGui wants to consume it.
424 // Pass at most one bit at a time.
425 MRVIEWER_API bool canConsumeEvent( BasicUiRenderTask::InteractionMask event ) const;
426 };
427 // This class helps the viewer to `renderUi()` from `IRenderObject`s.
428 std::unique_ptr<UiRenderManagerImpl> uiRenderManager_;
429 std::shared_ptr<SceneObjectsListDrawer> sceneObjectsList_;
430};
431
432
433// call if you want ImGui to take event if this key is pressed (to prevent scene reaction on key press)
434MRVIEWER_API void reserveKeyEvent( ImGuiKey key );
435
436
437} // end namespace
Definition ImGuiMenu.h:413
Definition ImGuiMenu.h:58
virtual MRVIEWER_API void drawCustomSelectionInformation_(const std::vector< std::shared_ptr< Object > > &selected, const SelectionInformationStyle &style)
draw additional selection information (e.g. for custom objects)
MRVIEWER_API void expandObjectTreeAndScroll(const Object *obj)
expands all objs parents in tree and scroll scene tree window so selection becomes visible
Definition MRObject.h:62
Definition MRIRenderObject.h:115
Definition MRViewerPlugin.h:27
Definition MRViewer.h:66
Definition MRViewportId.h:42
Definition MRViewport.h:46
auto width(const Box< V > &box)
returns size along x axis
Definition MRMesh/MRBox.h:354
auto height(const Box< V > &box)
returns size along y axis
Definition MRMesh/MRBox.h:361
Definition MRIRenderObject.h:81
Definition MRViewerEventsListener.h:123
Definition MRMesh/MRColor.h:9
class to subscribe on CursorEntranceSingal
Definition MRViewerEventsListener.h:407
Definition ImGuiMenu.h:112
style constants used for the information panel
Definition ImGuiMenu.h:338
Color labelColor
property label color
Definition ImGuiMenu.h:342
Color textColor
value text color
Definition ImGuiMenu.h:340
Color selectedTextColor
selected value text color
Definition ImGuiMenu.h:344
Definition ImGuiMenu.h:150
Definition MRViewerEventsListener.h:141
Definition MRViewerEventsListener.h:150
Definition MRViewerEventsListener.h:132
Definition MRViewerEventsListener.h:51
Definition MRViewerEventsListener.h:69
Definition MRViewerEventsListener.h:78
Definition MRViewerEventsListener.h:60
Definition MRViewerEventsListener.h:29
class to subscribe on PostFocusSingal
Definition MRViewerEventsListener.h:397
Definition MRViewerEventsListener.h:240
Definition MRViewerEventsListener.h:222
class to subscribe on SpaceMouseDownSgnal
Definition MRViewerEventsListener.h:287
class to subscribe on SpaceMouseMoveSignal
Definition MRViewerEventsListener.h:277
class to subscribe on TouchpadRotateGestureBeginEvent
Definition MRViewerEventsListener.h:307
class to subscribe on TouchpadRotateGestureEndEvent
Definition MRViewerEventsListener.h:327
class to subscribe on TouchpadRotateGestureUpdateEvent
Definition MRViewerEventsListener.h:317
class to subscribe on TouchpadSwipeGestureBeginEvent
Definition MRViewerEventsListener.h:337
class to subscribe on TouchpadSwipeGestureEndEvent
Definition MRViewerEventsListener.h:357
class to subscribe on TouchpadSwipeGestureUpdateEvent
Definition MRViewerEventsListener.h:347
class to subscribe on TouchpadZoomGestureBeginEvent
Definition MRViewerEventsListener.h:367
class to subscribe on TouchpadZoomGestureEndEvent
Definition MRViewerEventsListener.h:387
class to subscribe on TouchpadZoomGestureUpdateEvent
Definition MRViewerEventsListener.h:377
Definition MRViewportId.h:16