MeshLib C++ Docs
Loading...
Searching...
No Matches
MRMouseController.h
Go to the documentation of this file.
1#pragma once
2#include "MRViewerFwd.h"
4#include "MRMesh/MRBitSet.h"
5#include "MRMesh/MRphmap.h"
6#include "MRMouse.h"
7#include "MRAsyncTimer.h"
8#include "MRMesh/MRVector2.h"
9#include "MRMesh/MRVector3.h"
10#include <optional>
11
12namespace MR
13{
14// this class stores two maps:
15// 1) mouse control to mouse mode
16// 2) mouse mode to mouse control
17// it is present as field in Viewer, and used to control scene
18// note: default state is usually set from ViewerSetup class
19// note: user config saves its state
20class MRVIEWER_CLASS MouseController
21{
22public:
25 {
26 MouseButton btn{ MouseButton::Left };
27 int mod{ 0 }; // modifier (GLFW_MOD_{SHIFT|CONTROL|ALT})
28 };
29
30 // called in Viewer init, connects to Viewer mouse signals
31 MRVIEWER_API void connect();
32
33 // set control
34 // note: one control can have only one mode, one mode can have only one control
35 // if mode already has other control, other one will be removed
36 MRVIEWER_API void setMouseControl( const MouseControlKey& key, MouseMode mode );
37
38 // returns previous mouse down (if several mouse buttons are down returns position of first one)
39 const Vector2i& getDownMousePos() const { return downMousePos_; }
40 // returns current mouse position
41 const Vector2i& getMousePos() const { return currentMousePos_; }
42 // returns state of mouse button
43 MRVIEWER_API bool isPressed( MouseButton button ) const;
44 // returns true if any mouse btn is down
45 MRVIEWER_API bool isPressedAny() const;
46
47 bool isCursorInside() const { return isCursorInside_; }
48
49 // dropOldEventsOnNew flag - drop active mouse down state (calling mouseUp) if new mouse event happens
50 bool isDropOldEventOnNewActive() const { return dropOldEventsOnNew_; }
51 void dropOldEventsOnNew( bool on ) { dropOldEventsOnNew_ = on; };
52
53 // returns nullopt if no control is present for given mode, otherwise returns associated control
54 MRVIEWER_API std::optional<MouseControlKey> findControlByMode( MouseMode mode ) const;
55 // make string from mouse button and modifier
56 MRVIEWER_API static std::string getControlString( const MouseControlKey& key );
57
58 // cast mouse button and modifier to simple int key
59 MRVIEWER_API static int mouseAndModToKey( const MouseControlKey& key );
60 // cast simple int key to mouse button and modifier
61 MRVIEWER_API static MouseControlKey keyToMouseAndMod( int key );
62
63 // Activate / deactivate mouse scroll in scene
64 MRVIEWER_API void setMouseScroll( bool active );
65
66 // set callback to modify view transform before it is applied to viewport
67 void setTrasformModifierCb( std::function<void( AffineXf3f& )> cb ) { transformModifierCb_ = cb; }
68
69 // set callback to modify new field of view before it is applied to viewport
70 void setFOVModifierCb( std::function<void( float& )> cb ) { fovModifierCb_ = cb; }
71
72 // get number of potential conflicts between opened plugins and camera controls
73 // plugin is conflicting if it listens for mouseDown or dragStart events, and camera control uses LMB
75
76private:
77 bool preMouseDown_( MouseButton button, int modifier );
78 bool mouseDown_( MouseButton button, int modifier );
79 bool preMouseUp_( MouseButton button, int modifier );
80 bool preMouseMove_( int x, int y );
81 bool mouseScroll_( float delta );
82
83 bool isCursorInside_{ false };
84 void cursorEntrance_( bool entered );
85
86 bool dropOldEventsOnNew_{ false };
87 void resetAllIfNeeded_();
88
89 Vector3f downTranslation_;
90 // screen space
91 Vector2i downMousePos_;
92 Vector2i prevMousePos_;
93 Vector2i currentMousePos_;
94
95 BitSet downState_;
96 MouseMode currentMode_{ MouseMode::None };
97
98 // Variables related to mouseClick signal
99 MouseButton clickButton_{ MouseButton::NoButton }; // Current candidate for mouseClick
100 int clickModifiers_{}; // Modifiers state at the moment of button press
101 Time clickTime_{}; // Time point of button press
102 MouseButton clickPendingDown_{ MouseButton::NoButton }; // Button for deterred camera operation
103 MouseButton dragButton_{ MouseButton::NoButton }; // Current candidate for dragging
104 bool dragActive_{}; // Dragging currently active
105
106 using MouseModeMap = HashMap<int, MouseMode>;
107 using MouseModeBackMap = HashMap<MouseMode, int>;
108
109 MouseModeMap map_;
110 MouseModeBackMap backMap_;
111
112 bool scrollActive_{ true };
113
114 std::function<void( AffineXf3f& )> transformModifierCb_;
115 std::function<void( float& )> fovModifierCb_;
116};
117
118}
Definition MRMouseController.h:21
MRVIEWER_API std::optional< MouseControlKey > findControlByMode(MouseMode mode) const
MRVIEWER_API bool isPressed(MouseButton button) const
static MRVIEWER_API int mouseAndModToKey(const MouseControlKey &key)
bool isCursorInside() const
Definition MRMouseController.h:47
MRVIEWER_API void setMouseControl(const MouseControlKey &key, MouseMode mode)
MRVIEWER_API bool isPressedAny() const
MRVIEWER_API void connect()
const Vector2i & getDownMousePos() const
Definition MRMouseController.h:39
static MRVIEWER_API MouseControlKey keyToMouseAndMod(int key)
bool isDropOldEventOnNewActive() const
Definition MRMouseController.h:50
void setTrasformModifierCb(std::function< void(AffineXf3f &)> cb)
Definition MRMouseController.h:67
void dropOldEventsOnNew(bool on)
Definition MRMouseController.h:51
void setFOVModifierCb(std::function< void(float &)> cb)
Definition MRMouseController.h:70
MRVIEWER_API void setMouseScroll(bool active)
const Vector2i & getMousePos() const
Definition MRMouseController.h:41
MR_ADD_CTOR_DELETE_MOVE(MouseController)
static MRVIEWER_API std::string getControlString(const MouseControlKey &key)
Definition MRCameraOrientationPlugin.h:8
MouseMode
Definition MRMouse.h:19
MouseButton
Definition MRMouse.h:9
std::chrono::time_point< std::chrono::system_clock > Time
Definition MRAsyncTimer.h:17
Definition MRMouseController.h:25