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 
43 class MouseButton
44 {
45  public:
46  enum Type
47  {
48  Left = 0,
49  Right = 1,
50  Middle = 2,
51  };
52 
53  MouseButton(Type type = Left)
54  : _type(type)
55  { }
56 
57  operator Pt::uint32_t() const
58  {
59  return _type;
60  }
61 
62  private:
63  Pt::uint32_t _type;
64 };
65 
66 
67 class MouseState
68 {
69  public:
70  MouseState()
71  : _buttonState(0)
72  { }
73 
76  bool isPressed(MouseButton button) const
77  {
78  Pt::uint32_t mask = 0x1 << button;
79  return (_buttonState & mask) == mask;
80  }
81 
82  void setPressed(MouseButton button)
83  {
84  Pt::uint32_t mask = 0x1 << button;
85  _buttonState |= mask;
86  }
87 
90  bool isReleased(MouseButton button) const
91  {
92  Pt::uint32_t mask = 0x1 << button;
93  return (_buttonState & mask) != mask;
94  }
95 
96  void setReleased(MouseButton button)
97  {
98  Pt::uint32_t mask = 0x1 << button;
99  _buttonState &= (~mask);
100  }
101  private:
102  Pt::uint32_t _buttonState;
103 };
104 
105 
106 class MouseEvent : public Pt::BasicEvent<MouseEvent>
107 {
108  public:
109  enum Action
110  {
111  Move = 0,
112  Press = 1,
113  Release = 2
114  };
115 
116  enum Button
117  {
118  Left = 0,
119  Right = 1,
120  Middle = 2,
121  };
122 
123  explicit MouseEvent()
124  : _widgetId_(0)
125  , _widget(0)
126  , _pos(0, 0)
127  , _action(Move)
128  , _buttonState(0)
129  , _button(0)
130  { }
131 
132  explicit MouseEvent(Widget& widget)
133  : _widgetId_( widget.id() )
134  , _widget(&widget)
135  , _pos(0, 0)
136  , _action(Move)
137  , _buttonState(0)
138  , _button(0)
139  { }
140 
141  Pt::uint64_t widgetId() const
142  {
143  return _widgetId_;
144  }
145 
146  Widget* widget() const
147  {
148  return _widget;
149  }
150 
151  void setWidget(Widget* widget)
152  {
153  _widget = widget;
154  _widgetId_ = widget ? widget->id() : 0;
155  }
156 
157  const Gfx::PointF& position() const
158  {
159  return _pos;
160  }
161 
162  void setPosition(const Gfx::PointF& pos)
163  {
164  _pos = pos;
165  }
166 
167  double x() const
168  {
169  return _pos.x();
170  }
171 
172  void setX(double x)
173  {
174  _pos.setX(x);
175  }
176 
177  double y() const
178  {
179  return _pos.y();
180  }
181 
182  void setY(double y)
183  {
184  _pos.setY(y);
185  }
186 
187  bool isMove() const
188  {
189  return _action == Move;
190  }
191 
192  void setMove()
193  {
194  _action = Move;
195  _button = 0;
196  }
197 
200  bool isPressed(Pt::uint32_t button = Left) const
201  {
202  Pt::uint32_t mask = 0x1 << button;
203  return (_buttonState & mask) == mask;
204  }
205 
208  bool isPress(Pt::uint32_t button = Left) const
209  {
210  Pt::uint32_t mask = 0x1 << button;
211  return (_button & mask) == mask && _action == Press;
212  }
213 
214  void setPress(Pt::uint32_t button = Left)
215  {
216  Pt::uint32_t mask = 0x1 << button;
217 
218  _action = Press;
219  _button = mask;
220  _buttonState |= mask;
221  }
222 
225  bool isReleased(Pt::uint32_t button = Left) const
226  {
227  Pt::uint32_t mask = 0x1 << button;
228  return (_buttonState & mask) != mask;
229  }
230 
233  bool isRelease(Pt::uint32_t button = Left) const
234  {
235  Pt::uint32_t mask = 0x1 << button;
236  return (_button & mask) == mask && _action == Release;
237  }
238 
239  void setRelease(Pt::uint32_t button = Left)
240  {
241  Pt::uint32_t mask = 0x1 << button;
242 
243  _action = Release;
244  _button = mask;
245  _buttonState &= (~mask);
246  }
247 
248  private:
249  Pt::uint64_t _widgetId_;
250  Widget* _widget;
251  Gfx::PointF _pos;
252  Action _action;
253  Pt::uint32_t _buttonState;
254  Pt::uint32_t _button;
255 };
256 
257 } // namespace
258 
259 } // namespace
260 
261 #endif
uint_type uint64_t
Unsigned 64-bit integer type.
Definition: Types.h:54
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Types.h:42