ScummVM API documentation
mouse.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef ULTIMA8_KERNEL_MOUSE_H
23 #define ULTIMA8_KERNEL_MOUSE_H
24 
25 #include "common/system.h"
26 #include "common/rect.h"
27 #include "ultima/ultima8/misc/common_types.h"
28 #include "ultima/ultima8/misc/direction.h"
29 
30 namespace Ultima {
31 namespace Ultima8 {
32 
33 enum MouseButtonState {
34  MBS_DOWN = 0x1,
35  MBS_HANDLED = 0x2 // Mousedown event handled
36 };
37 
38 struct MButton {
39  uint16 _downGump;
40  uint32 _lastDown;
41  uint32 _curDown;
42  Common::Point _downPoint;
43  int _state;
44 
45  MButton() : _downGump(0), _curDown(0), _lastDown(0), _state(MBS_HANDLED)
46  {
47  }
48 
49  bool isState(MouseButtonState state) const {
50  return _state & state;
51  }
52  void setState(MouseButtonState state) {
53  _state |= state;
54  }
55  void clearState(MouseButtonState state) {
56  _state &= ~state;
57  }
58 
60  bool isUnhandledPastTimeout(uint32 now, uint32 timeout) {
61  return !isState(MBS_HANDLED) && _curDown > 0 && (now - _curDown) > timeout;
62  }
63 
65  bool isUnhandledDoubleClick(uint32 timeout) {
66  return !isState(MBS_HANDLED) && _lastDown > 0 && (_curDown - _lastDown) <= timeout;
67  }
68 
69 };
70 
71 class Gump;
72 
73 class Mouse {
74 public:
75  enum MouseButton {
76  BUTTON_NONE = 0,
77  BUTTON_LEFT = 1,
78  BUTTON_RIGHT = 2,
79  BUTTON_MIDDLE = 3,
80  MOUSE_LAST
81  };
82 
83  enum MouseCursor {
84  MOUSE_NORMAL = 0,
85  MOUSE_NONE = 1,
86  MOUSE_TARGET = 2,
87  MOUSE_WAIT = 3,
88  MOUSE_HAND = 4,
89  MOUSE_QUILL = 5,
90  MOUSE_MAGGLASS = 6,
91  MOUSE_CROSS = 7
92  };
93 
94  enum DraggingState {
95  DRAG_NOT = 0,
96  DRAG_OK = 1,
97  DRAG_INVALID = 2,
98  DRAG_TEMPFAIL = 3
99  };
100 private:
101  static Mouse *_instance;
103  int _lastMouseFrame;
104 
108  uint32 _flashingCursorTime;
109 
110  // mouse input state
111  MButton _mouseButton[MOUSE_LAST];
112 
113  uint16 _mouseOverGump;
114  Common::Point _mousePos;
115  Common::Point _draggingOffset;
116  DraggingState _dragging;
117 
118  ObjId _dragging_objId;
119  uint16 _draggingItem_startGump;
120  uint16 _draggingItem_lastGump;
121 
122  int _walkThreshold;
123  int _runThreshold;
124 private:
125  void startDragging(int mx, int my);
126  void moveDragging(int mx, int my);
127  void stopDragging(int mx, int my);
128  int mouseFrameForDir(Direction mousedir) const;
129 
130 public:
131  static Mouse *get_instance() { return _instance; }
132 public:
133  Mouse();
134  ~Mouse();
135 
139  bool buttonDown(MouseButton button);
140 
144  bool buttonUp(MouseButton button);
145 
147  int getMouseLength(int mx, int my) const;
148 
150  int getMouseLength() const {
151  return getMouseLength(_mousePos.x, _mousePos.y);
152  }
153 
155  Direction getMouseDirectionScreen(int mx, int my) const;
156 
158  Direction getMouseDirectionScreen() const {
159  return getMouseDirectionScreen(_mousePos.x, _mousePos.y);
160  }
161 
163  Direction getMouseDirectionWorld(int mx, int my) const;
164 
166  Direction getMouseDirectionWorld() const {
167  return getMouseDirectionWorld(_mousePos.x, _mousePos.y);
168  }
169 
171  void getMouseCoords(int32 &mx, int32 &my) const {
172  mx = _mousePos.x;
173  my = _mousePos.y;
174  }
175 
177  void setMouseCoords(int mx, int my);
178 
179  bool isMouseDownEvent(MouseButton button) const;
180 
182  void popAllCursors();
183 
185  void setMouseCursor(MouseCursor cursor);
186 
188  void flashCrossCursor();
189 
191  void pushMouseCursor(MouseCursor cursor);
192 
194  void popMouseCursor();
195 
197  int getMouseFrame();
198 
199  DraggingState dragging() const { return _dragging; }
200 
201  void setDraggingOffset(int32 x, int32 y) {
202  _draggingOffset.x = x;
203  _draggingOffset.y = y;
204  }
205  void getDraggingOffset(int32 &x, int32 &y) {
206  x = _draggingOffset.x;
207  y = _draggingOffset.y;
208  }
209 
210  uint32 getDoubleClickTime() const;
211 
212  void handleDelayedEvents();
213 
214  Gump *getMouseOverGump() const;
215  void resetMouseOverGump() { _mouseOverGump = 0; }
216 
217  void update();
218 };
219 
220 } // End of namespace Ultima8
221 } // End of namespace Ultima
222 
223 #endif
bool isUnhandledPastTimeout(uint32 now, uint32 timeout)
True if the current state is unhandled and past the double click timeout.
Definition: mouse.h:60
Direction getMouseDirectionScreen() const
get mouse cursor direction on the screen using the current coordinates.
Definition: mouse.h:158
Definition: gump.h:47
int getMouseLength() const
get mouse cursor length for the current coordinates
Definition: mouse.h:150
Definition: detection.h:27
void getMouseCoords(int32 &mx, int32 &my) const
get current mouse cursor location
Definition: mouse.h:171
Definition: rect.h:45
bool isUnhandledDoubleClick(uint32 timeout)
True if the current state is unhandled and within the double click timeout.
Definition: mouse.h:65
int16 x
Definition: rect.h:46
int16 y
Definition: rect.h:47
Direction getMouseDirectionWorld() const
get mouse cursor direction in the world using the current coordinates.
Definition: mouse.h:166
Definition: mouse.h:73
Definition: mouse.h:38