iMSTK
Interactive Medical Simulation Toolkit
imstkMouseDeviceClient.h
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #pragma once
8 
9 #include "imstkDeviceClient.h"
10 
11 #include <unordered_map>
12 
13 namespace imstk
14 {
15 using MouseActionType = int;
16 #define MOUSE_RELEASE 0
17 #define MOUSE_PRESS 1
18 
19 using MouseButtonType = int;
20 #define LEFT_BUTTON 0
21 #define RIGHT_BUTTON 1
22 #define MIDDLE_BUTTON 2
23 
30 class MouseEvent : public Event
31 {
32 public:
33  MouseEvent(const std::string type, const MouseButtonType buttonId) :
34  Event(type), m_scrollDx(0.0), m_buttonId(buttonId)
35  {
36  }
37 
38  MouseEvent(std::string type, const double scrollDx) :
39  Event(type), m_scrollDx(scrollDx), m_buttonId(0)
40  {
41  }
42 
43  MouseEvent(std::string type) :
44  Event(type), m_scrollDx(0.0), m_buttonId(0)
45  {
46  }
47 
48  ~MouseEvent() override = default;
49 
50 public:
51  double m_scrollDx;
52  MouseButtonType m_buttonId;
53 };
54 
61 {
62 protected:
66  MouseDeviceClient() : DeviceClient("MouseDevice", ""),
67  m_prevPos(Vec2d(0.0, 0.0)), m_pos(Vec2d(0.0, 0.0)) { }
68 
72  static std::shared_ptr<MouseDeviceClient> New();
73 
74 public:
75  ~MouseDeviceClient() override = default;
76 
77  // Only the viewer is allowed to provide these objects
78  friend class VTKInteractorStyle;
79 
80 public:
81  // *INDENT-OFF*
85  SIGNAL(MouseDeviceClient, mouseButtonPress);
86 
90  SIGNAL(MouseDeviceClient, mouseButtonRelease);
91 
95  SIGNAL(MouseDeviceClient, mouseScroll);
96 
100  SIGNAL(MouseDeviceClient, mouseMove);
101  // *INDENT-ON*
102 
103 public:
107  const Vec2d& getPos() const { return m_pos; }
108 
112  const Vec2d& getPrevPos() const { return m_prevPos; }
113 
117  bool isButtonDown(const MouseButtonType buttonId) const
118  {
119  if (m_buttons.find(buttonId) != m_buttons.end())
120  {
121  return m_buttons.at(buttonId) == MOUSE_PRESS;
122  }
123  else
124  {
125  return MOUSE_RELEASE;
126  }
127  }
128 
129 protected:
133  void updateMousePos(const Vec2d& pos)
134  {
135  m_prevPos = m_pos;
136  m_pos = pos;
137  this->postEvent(MouseEvent(MouseDeviceClient::mouseMove()));
138  }
139 
143  void emitButtonPress(const MouseButtonType buttonId)
144  {
145  m_buttons[buttonId] = MOUSE_PRESS;
146  this->postEvent(MouseEvent(MouseDeviceClient::mouseButtonPress(), buttonId));
147  }
148 
152  void emitButtonRelease(const MouseButtonType buttonId)
153  {
154  m_buttons[buttonId] = MOUSE_RELEASE;
155  this->postEvent(MouseEvent(MouseDeviceClient::mouseButtonRelease(), buttonId));
156  }
157 
161  void emitScroll(const double dx) { this->postEvent(MouseEvent(MouseDeviceClient::mouseScroll(), dx)); }
162 
163 protected:
164  Vec2d m_prevPos;
165  Vec2d m_pos;
166 };
167 } // namespace imstk
The device client&#39;s represents the device and provides an interface to acquire data from a device...
Base class for events which contain a type, priority, and data priority defaults to 0 and uses a grea...
void emitButtonPress(const MouseButtonType buttonId)
Post a click down event.
MouseButtonType m_buttonId
Button id.
Compound Geometry.
const Vec2d & getPrevPos() const
Get the previous position of the mouse.
void emitButtonRelease(const MouseButtonType buttonId)
Post a click release event.
void emitScroll(const double dx)
Post a mouse scroll event.
double m_scrollDx
Mouse scroll.
Provides the information of a mouse event, this includes button presses/releases and scrolling...
const Vec2d & getPos() const
Get the current position of the mouse.
bool isButtonDown(const MouseButtonType buttonId) const
Query if the button is down.
void updateMousePos(const Vec2d &pos)
Update the mouse position.
Interactor styles forward their controls to imstk objects.