-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMouseListener.cpp
More file actions
330 lines (298 loc) · 8.83 KB
/
Copy pathMouseListener.cpp
File metadata and controls
330 lines (298 loc) · 8.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//*****************************************************************************
// FILE NAME: MouseListener.cpp
//
//*****************************************************************************
// standard includes
#include <iostream>
// fife includes
#include "eventchannel/mouse/mouseevent.h"
#include "model/metamodel/modelcoords.h"
#include "model/structures/location.h"
#include "util/structures/rect.h"
#include "model/structures/instance.h"
#include "view/camera.h"
#include "Game.h"
#include "ViewController.h"
#include "MouseListener.h"
//!***************************************************************
//! @details:
//! constructor
//!
//! @param[in]: parent
//! the game object that created this listener
//!
//! @param[in]: cam
//! the camera for manipulation
//!
//! @param[in]: eventManager
//! the engine event manager for registering for events
//!
//!***************************************************************
MouseListener::MouseListener(Game* parent, FIFE::Camera *cam, FIFE::EventManager* eventManager, FIFE::TimeManager* timeManager)
: m_parent(parent), m_dragX(0), m_dragY(0), m_camera(cam), m_autoscreenscroller(cam, eventManager, timeManager),
m_controller(0), m_prevEventType(FIFE::MouseEvent::UNKNOWN_EVENT)
{
}
//!***************************************************************
//! @details:
//! destructor
//!
//!***************************************************************
MouseListener::~MouseListener()
{
}
//!***************************************************************
//! @details:
//! overridden from base class
//!
//! @param[in]: evt
//! signaled event
//!
//! @return:
//! void
//!
//!***************************************************************
void MouseListener::mouseEntered(FIFE::MouseEvent& evt)
{
SetPreviousMouseEvent(evt.getType());
}
//!***************************************************************
//! @details:
//! overridden from base class
//!
//! @param[in]: evt
//! signaled event
//!
//! @return:
//! void
//!
//!***************************************************************
void MouseListener::mouseExited(FIFE::MouseEvent& evt)
{
SetPreviousMouseEvent(evt.getType());
}
//!***************************************************************
//! @details:
//! overridden from base class
//!
//! @param[in]: evt
//! signaled event
//!
//! @return:
//! void
//!
//!***************************************************************
void MouseListener::mousePressed(FIFE::MouseEvent& evt)
{
if (evt.getButton() == FIFE::MouseEvent::LEFT)
{
// save mouse position
m_dragX = evt.getX();
m_dragY = evt.getY();
}
SetPreviousMouseEvent(evt.getType());
}
//!***************************************************************
//! @details:
//! overridden from base class
//!
//! @param[in]: evt
//! signaled event
//!
//! @return:
//! void
//!
//!***************************************************************
void MouseListener::mouseReleased(FIFE::MouseEvent& evt)
{
// only activate the move action if the mouse was pressed and released without dragging
if (m_controller && evt.getButton() == FIFE::MouseEvent::LEFT && m_prevEventType != FIFE::MouseEvent::DRAGGED)
{
// move controller to clicked spot
FIFE::Location destination(m_controller->getLocation());
FIFE::ScreenPoint screenPoint(evt.getX(), evt.getY());
FIFE::ExactModelCoordinate mapCoords = m_camera->toMapCoordinates(screenPoint, false);
mapCoords.z = 0.0;
destination.setMapCoordinates(mapCoords);
m_controller->move("walk", destination, m_controller->getTotalTimeMultiplier());
}
SetPreviousMouseEvent(evt.getType());
}
//!***************************************************************
//! @details:
//! overridden from base class
//!
//! @param[in]: evt
//! signaled event
//!
//! @return:
//! void
//!
//!***************************************************************
void MouseListener::mouseClicked(FIFE::MouseEvent& evt)
{
// this event never seems to fire, not sure if it works properly
// I suspect this would remove the need for my keeping track of the previous
// event for dragging vs. releasing if this event signifies what I think
SetPreviousMouseEvent(evt.getType());
}
//!***************************************************************
//! @details:
//! overridden from base class
//!
//! @param[in]: evt
//! signaled event
//!
//! @return:
//! void
//!
//!***************************************************************
void MouseListener::mouseWheelMovedUp(FIFE::MouseEvent& evt)
{
// zoom in
m_parent->GetViewController()->ZoomIn();
// save mouse position
m_dragX = evt.getX();
m_dragY = evt.getY();
SetPreviousMouseEvent(evt.getType());
}
//!***************************************************************
//! @details:
//! overridden from base class
//!
//! @param[in]: evt
//! signaled event
//!
//! @return:
//! void
//!
//!***************************************************************
void MouseListener::mouseWheelMovedDown(FIFE::MouseEvent& evt)
{
// zoom out
m_parent->GetViewController()->ZoomOut();
SetPreviousMouseEvent(evt.getType());
}
//!***************************************************************
//! @details:
//! overridden from base class
//!
//! @param[in]: evt
//! signaled event
//!
//! @return:
//! void
//!
//!***************************************************************
void MouseListener::mouseWheelMovedRight(FIFE::MouseEvent& evt)
{
SetPreviousMouseEvent(evt.getType());
}
//!***************************************************************
//! @details:
//! overridden from base class
//!
//! @param[in]: evt
//! signaled event
//!
//! @return:
//! void
//!
//!***************************************************************
void MouseListener::mouseWheelMovedLeft(FIFE::MouseEvent& evt)
{
SetPreviousMouseEvent(evt.getType());
}
//!***************************************************************
//! @details:
//! overridden from base class
//!
//! @param[in]: evt
//! signaled event
//!
//! @return:
//! void
//!
//!***************************************************************
void MouseListener::mouseMoved(FIFE::MouseEvent& evt)
{
m_autoscreenscroller.updateLocation(evt.getX(), evt.getY());
SetPreviousMouseEvent(evt.getType());
}
//!***************************************************************
//! @details:
//! overridden from base class
//!
//! @param[in]: evt
//! signaled event
//!
//! @return:
//! void
//!
//!***************************************************************
void MouseListener::mouseDragged(FIFE::MouseEvent& evt)
{
if (evt.getButton() == FIFE::MouseEvent::LEFT)
{
// unregister the auto-scrolling event
// we are now scrolling manually and do not
// want both trying to do it at the same time
m_autoscreenscroller.unregisterEvent();
// get the screen coordinates of the mouse
int currX = evt.getX();
int currY = evt.getY();
// get the mouse delta for camera movement
FIFE::ScreenPoint delta(m_dragX - currX, m_dragY - currY);
// get the current camera location
FIFE::ScreenPoint cameraScreenCoords = m_camera->toScreenCoordinates(m_camera->getLocation().getMapCoordinates());
cameraScreenCoords += delta;
// set the new coordinates
FIFE::Location camLocation(m_camera->getLocation());
FIFE::ExactModelCoordinate mapCoords = m_camera->toMapCoordinates(cameraScreenCoords, false);
mapCoords.z = 0.0;
camLocation.setMapCoordinates(mapCoords);
m_camera->setLocation(camLocation);
// update last saved x,y values for dragging
m_dragX = currX;
m_dragY = currY;
}
// make sure to save that the last event was drag
// this is important to get around the drag & click problem
SetPreviousMouseEvent(evt.getType());
}
//!***************************************************************
//! @details:
//! store the character we want to move with our mouse
//!
//! @param[in]: controller
//! the character to manipulate
//!
//! @return:
//! void
//!
//!***************************************************************
void MouseListener::SetController(FIFE::Instance* controller)
{
m_controller = controller;
}
//!***************************************************************
//! @details:
//! saves the last action that was received
//! this is done to make sure we can distinguish between dragging
//! and clicking since they do 2 different things
//!
//! if we do not keep track we will drag and then when we release
//! it will perform the action for the mouse release as well as
//! the mouse dragged
//!
//! @param[in]: type
//! event type
//!
//! @return:
//! void
//!
//!***************************************************************
void MouseListener::SetPreviousMouseEvent(FIFE::MouseEvent::MouseEventType type)
{
m_prevEventType = type;
}