MeshLib
 
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
45 bool isCursorInside() const { return isCursorInside_; }
46
47 // dropOldEventsOnNew flag - drop active mouse down state (calling mouseUp) if new mouse event happens
48 bool isDropOldEventOnNewActive() const { return dropOldEventsOnNew_; }
49 void dropOldEventsOnNew( bool on ) { dropOldEventsOnNew_ = on; };
50
51 // returns nullopt if no control is present for given mode, otherwise returns associated control
52 MRVIEWER_API std::optional<MouseControlKey> findControlByMode( MouseMode mode ) const;
53 // make string from mouse button and modifier
54 MRVIEWER_API static std::string getControlString( const MouseControlKey& key );
55
56 // cast mouse button and modifier to simple int key
57 MRVIEWER_API static int mouseAndModToKey( const MouseControlKey& key );
58 // cast simple int key to mouse button and modifier
59 MRVIEWER_API static MouseControlKey keyToMouseAndMod( int key );
60
61 // Activate / diactivate mouse scroll in scene
62 MRVIEWER_API void setMouseScroll( bool active );
63
64 // set callback to modify view transform before it is applied to viewport
65 void setTrasformModifierCb( std::function<void( AffineXf3f& )> cb ) { transformModifierCb_ = cb; }
66
67 // set callback to modify new field of view before it is applied to viewport
68 void setFOVModifierCb( std::function<void( float& )> cb ) { fovModifierCb_ = cb; }
69
70 // get number of potential conflicts between opened plugins and camera controls
71 // plugin is conflicting if it listens for mouseDown or dragStart events, and camera control uses LMB
73
74private:
75 bool preMouseDown_( MouseButton button, int modifier );
76 bool mouseDown_( MouseButton button, int modifier );
77 bool preMouseUp_( MouseButton button, int modifier );
78 bool preMouseMove_( int x, int y );
79 bool mouseScroll_( float delta );
80
81 bool isCursorInside_{ false };
82 void cursorEntrance_( bool entered );
83
84 bool dropOldEventsOnNew_{ false };
85 void resetAllIfNeeded_();
86
87 Vector3f downTranslation_;
88 // screen space
89 Vector2i downMousePos_;
90 Vector2i prevMousePos_;
91 Vector2i currentMousePos_;
92
93 BitSet downState_;
94 MouseMode currentMode_{ MouseMode::None };
95
96 // Variables related to mouseClick signal
97 MouseButton clickButton_{ MouseButton::NoButton }; // Current candidate for mouseClick
98 int clickModifiers_{}; // Modifiers state at the moment of button press
99 Time clickTime_{}; // Time point of button press
100 MouseButton clickPendingDown_{ MouseButton::NoButton }; // Button for deterred camera operation
101 MouseButton dragButton_{ MouseButton::NoButton }; // Current candidate for dragging
102 bool dragActive_{}; // Dragging currently active
103
104 using MouseModeMap = HashMap<int, MouseMode>;
105 using MouseModeBackMap = HashMap<MouseMode, int>;
106
107 MouseModeMap map_;
108 MouseModeBackMap backMap_;
109
110 bool scrollActive_{ true };
111
112 std::function<void( AffineXf3f& )> transformModifierCb_;
113 std::function<void( float& )> fovModifierCb_;
114};
115
116}
affine transformation: y = A*x + b, where A in VxV, and b in V
Definition MRDotNet/MRAffineXf.h:8
Definition MRDotNet/MRBitSet.h:39
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:45
MRVIEWER_API void setMouseControl(const MouseControlKey &key, MouseMode mode)
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:48
void setTrasformModifierCb(std::function< void(AffineXf3f &)> cb)
Definition MRMouseController.h:65
void dropOldEventsOnNew(bool on)
Definition MRMouseController.h:49
void setFOVModifierCb(std::function< void(float &)> cb)
Definition MRMouseController.h:68
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)
represents a 3-dimentional float-typed vector
Definition MRDotNet/MRVector3.h:8
Definition MRCameraOrientationPlugin.h:7
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