MouseEvent.h
1 /*
2  Copyright (C) 2015 Marc Boris Duerner
3  Copyright (C) 2015 Laurentiu-Gheorghe Crisan
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  As a special exception, you may use this file as part of a free
11  software library without restriction. Specifically, if other files
12  instantiate templates or use macros or inline functions from this
13  file, or you compile this file and link it with other files to
14  produce an executable, this file does not by itself cause the
15  resulting executable to be covered by the GNU General Public
16  License. This exception does not however invalidate any other
17  reasons why the executable file might be covered by the GNU Library
18  General Public License.
19 
20  This library is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  Lesser General Public License for more details.
24 
25  You should have received a copy of the GNU Lesser General Public
26  License along with this library; if not, write to the Free Software
27  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
28  MA 02110-1301 USA
29 */
30 
31 #ifndef Pt_Forms_MouseEvent_h
32 #define Pt_Forms_MouseEvent_h
33 
34 #include <Pt/Forms/Api.h>
35 #include <Pt/Forms/Widget.h>
36 #include <Pt/Gfx/Point.h>
37 #include <Pt/Event.h>
38 
39 namespace Pt {
40 
41 namespace Forms {
42 
48 {
49  public:
52  enum Type
53  {
54  Left = 0,
55  Right = 1,
56  Middle = 2,
57  };
58 
61  MouseButton(Type type = Left)
62  : _type(type)
63  { }
64 
67  operator Pt::uint32_t() const
68  {
69  return _type;
70  }
71 
72  private:
73  Pt::uint32_t _type;
74 };
75 
76 
82 {
83  public:
87  : _buttonState(0)
88  { }
89 
92  bool isPressed(MouseButton button) const
93  {
94  Pt::uint32_t mask = 0x1 << button;
95  return (_buttonState & mask) == mask;
96  }
97 
100  void setPressed(MouseButton button)
101  {
102  Pt::uint32_t mask = 0x1 << button;
103  _buttonState |= mask;
104  }
105 
108  bool isReleased(MouseButton button) const
109  {
110  Pt::uint32_t mask = 0x1 << button;
111  return (_buttonState & mask) != mask;
112  }
113 
117  {
118  Pt::uint32_t mask = 0x1 << button;
119  _buttonState &= (~mask);
120  }
121  private:
122  Pt::uint32_t _buttonState;
123 };
124 
125 
133 class MouseEvent : public Pt::BasicEvent<MouseEvent>
134 {
135  public:
138  enum Action
139  {
140  Move = 0,
141  Press = 1,
142  Release = 2
143  };
144 
147  enum Button
148  {
149  Left = 0,
150  Right = 1,
151  Middle = 2,
152  };
153 
156  explicit MouseEvent()
157  : _widgetId_(0)
158  , _widget(0)
159  , _pos(0, 0)
160  , _action(Move)
161  , _buttonState(0)
162  , _button(0)
163  { }
164 
168  : _widgetId_( widget.id() )
169  , _widget(&widget)
170  , _pos(0, 0)
171  , _action(Move)
172  , _buttonState(0)
173  , _button(0)
174  { }
175 
179  {
180  return _widgetId_;
181  }
182 
185  Widget* widget() const
186  {
187  return _widget;
188  }
189 
193  {
194  _widget = widget;
195  _widgetId_ = widget ? widget->id() : 0;
196  }
197 
200  const Gfx::PointF& position() const
201  {
202  return _pos;
203  }
204 
207  void setPosition(const Gfx::PointF& pos)
208  {
209  _pos = pos;
210  }
211 
214  double x() const
215  {
216  return _pos.x();
217  }
218 
221  void setX(double x)
222  {
223  _pos.setX(x);
224  }
225 
228  double y() const
229  {
230  return _pos.y();
231  }
232 
235  void setY(double y)
236  {
237  _pos.setY(y);
238  }
239 
242  bool isMove() const
243  {
244  return _action == Move;
245  }
246 
249  void setMove()
250  {
251  _action = Move;
252  _button = 0;
253  }
254 
257  bool isPressed(Pt::uint32_t button = Left) const
258  {
259  Pt::uint32_t mask = 0x1 << button;
260  return (_buttonState & mask) == mask;
261  }
262 
265  bool isPress(Pt::uint32_t button = Left) const
266  {
267  Pt::uint32_t mask = 0x1 << button;
268  return (_button & mask) == mask && _action == Press;
269  }
270 
273  void setPress(Pt::uint32_t button = Left)
274  {
275  Pt::uint32_t mask = 0x1 << button;
276 
277  _action = Press;
278  _button = mask;
279  _buttonState |= mask;
280  }
281 
284  bool isReleased(Pt::uint32_t button = Left) const
285  {
286  Pt::uint32_t mask = 0x1 << button;
287  return (_buttonState & mask) != mask;
288  }
289 
292  bool isRelease(Pt::uint32_t button = Left) const
293  {
294  Pt::uint32_t mask = 0x1 << button;
295  return (_button & mask) == mask && _action == Release;
296  }
297 
300  void setRelease(Pt::uint32_t button = Left)
301  {
302  Pt::uint32_t mask = 0x1 << button;
303 
304  _action = Release;
305  _button = mask;
306  _buttonState &= (~mask);
307  }
308 
309  private:
310  Pt::uint64_t _widgetId_;
311  Widget* _widget;
312  Gfx::PointF _pos;
313  Action _action;
314  Pt::uint32_t _buttonState;
315  Pt::uint32_t _button;
316 };
317 
318 } // namespace
319 
320 } // namespace
321 
322 #endif
Widget * widget() const
Returns the target widget, or 0 when no target is set.
Definition: MouseEvent.h:185
Core module.
Definition: Allocator.h:33
MouseEvent(Widget &widget)
Creates an event targeted at widget.
Definition: MouseEvent.h:167
Mouse button identity.
Definition: MouseEvent.h:48
void setY(double y)
Sets the local vertical pointer position to y.
Definition: MouseEvent.h:235
double y() const
Returns the local vertical pointer position.
Definition: MouseEvent.h:228
bool isReleased(MouseButton button) const
Returns true if the button is in released state.
Definition: MouseEvent.h:108
void setReleased(MouseButton button)
Records button as released.
Definition: MouseEvent.h:116
const Gfx::PointF & position() const
Returns the pointer position in local coordinates.
Definition: MouseEvent.h:200
void setWidget(Widget *widget)
Sets the target widget and its ID.
Definition: MouseEvent.h:192
MouseState()
Creates a state in which no button is pressed.
Definition: MouseEvent.h:86
Pt::uint64_t widgetId() const
Returns the target widget ID, or 0 when no target is set.
Definition: MouseEvent.h:178
bool isPressed(MouseButton button) const
Returns true if the button is in pressed state.
Definition: MouseEvent.h:92
Common type of every visual Forms object.
Definition: Widget.h:105
void setPressed(MouseButton button)
Records button as pressed.
Definition: MouseEvent.h:100
Point with floating-point X and Y coordinates.
Definition: Point.h:47
Type
Defines a mouse button.
Definition: MouseEvent.h:53
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Api-Types.h:48
MouseButton(Type type=Left)
Creates a mouse button with type.
Definition: MouseEvent.h:61
double x() const
Returns the local horizontal pointer position.
Definition: MouseEvent.h:214
void setMove()
Changes the event to pointer movement.
Definition: MouseEvent.h:249
bool isMove() const
Returns true when the event reports pointer movement.
Definition: MouseEvent.h:242
void setRelease(Pt::uint32_t button=Left)
Changes the event to a release of button.
Definition: MouseEvent.h:300
MouseEvent()
Creates an event without a target.
Definition: MouseEvent.h:156
Pt::uint64_t id() const
Returns the application-unique ID of this live widget.
void setPosition(const Gfx::PointF &pos)
Sets the pointer position to pos.
Definition: MouseEvent.h:207
Button
Defines a mouse button.
Definition: MouseEvent.h:148
bool isReleased(Pt::uint32_t button=Left) const
Returns true if the button is in released state.
Definition: MouseEvent.h:284
bool isPressed(Pt::uint32_t button=Left) const
Returns true if the button is in pressed state.
Definition: MouseEvent.h:257
Action
Defines the mouse action.
Definition: MouseEvent.h:139
Pressed or released state of the mouse buttons.
Definition: MouseEvent.h:82
void setPress(Pt::uint32_t button=Left)
Changes the event to a press of button.
Definition: MouseEvent.h:273
void setX(double x)
Sets the local horizontal pointer position to x.
Definition: MouseEvent.h:221
uint_type uint64_t
Unsigned 64-bit integer type.
Definition: Api-Types.h:62
Pointer movement or button change.
Definition: MouseEvent.h:134
bool isPress(Pt::uint32_t button=Left) const
Returns true if the button was just pressed.
Definition: MouseEvent.h:265
bool isRelease(Pt::uint32_t button=Left) const
Returns true if the button was just released.
Definition: MouseEvent.h:292