File: mouse.cpp

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (426 lines) | stat: -rw-r--r-- 11,549 bytes parent folder | download | duplicates (4)
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************
 * Mouse management
 *****************************************************************************/

#include <WARMUX_point.h>

#include "character/character.h"
#include "game/config.h"
#include "game/game.h"
#include "game/game_mode.h"
#include "game/game_time.h"
#include "graphic/video.h"
#include "include/app.h"
#include "include/action_handler.h"
#include "interface/cursor.h"
#include "interface/interface.h"
#include "interface/mouse.h"
#include "interface/mouse_cursor.h"
#include "map/camera.h"
#include "map/map.h"
#include "replay/replay.h"
#include "team/macro.h"
#include "team/team.h"
#include "tool/resource_manager.h"
#include "weapon/weapon.h"

#define MOUSE_CLICK_SQUARE_DISTANCE 5*5
#define LONG_CLICK_DURATION 600

std::string __pointers[] = {
  "mouse/pointer_standard",
  "mouse/pointer_select",
  "mouse/pointer_move",
  "mouse/pointer_arrow_up",
  "mouse/pointer_arrow_up_right",
  "mouse/pointer_arrow_up_left",
  "mouse/pointer_arrow_down",
  "mouse/pointer_arrow_down_right",
  "mouse/pointer_arrow_down_left",
  "mouse/pointer_arrow_right",
  "mouse/pointer_arrow_left",
  "mouse/pointer_aim",
  "mouse/pointer_attack_from_left",
  "mouse/pointer_attack_from_right",
};
std::map<Mouse::pointer_t, MouseCursor> Mouse::cursors;

Mouse::Mouse()
  : lastpos(-1,-1)
  , last_hide_time(0)
  , long_click_timer(0)
  , click_pos(-1,-1)
  , is_long_click(false)
  , was_long_click(false)
{
  visible = MOUSE_VISIBLE;

  // Load the different pointers
  Profile *res = GetResourceManager().LoadXMLProfile("cursors.xml", false);

  for (int i=POINTER_SELECT; i < POINTER_ATTACK; i++) {
    cursors.insert(std::make_pair(Mouse::pointer_t(i),
                                  GetResourceManager().LoadMouseCursor(res, __pointers[i],
                                                                       Mouse::pointer_t(i))));
  }

  current_pointer = POINTER_STANDARD;
  GetResourceManager().UnLoadXMLProfile(res);

#ifdef HAVE_TOUCHSCREEN
  SDL_ShowCursor(false);
#endif
}

void Mouse::EndLongClickTimer()
{
  if (long_click_timer) {
    SDL_RemoveTimer(long_click_timer);
    long_click_timer = 0;
  }
  is_long_click = false;
  was_long_click = false;
}

bool Mouse::HasFocus() const
{
  uint8_t state = SDL_GetAppState();

  if ((state & SDL_APPMOUSEFOCUS) &&
      (state & SDL_APPINPUTFOCUS) &&
      (state & SDL_APPACTIVE)) {
    return true;
  }

  return false;
}

void Mouse::ActionLeftClick(bool /*shift*/) const
{
  const Point2i pos_monde = GetWorldPosition();

  //Change character by mouse click only if the choosen weapon allows it
  if (GameMode::GetConstInstance()->AllowCharacterSelection() &&
      ActiveTeam().GetWeapon().mouse_character_selection) {

    // Choose a character of our own team
    bool character_found = false;
    Team::iterator it    = ActiveTeam().begin(),
                   end   = ActiveTeam().end();

    for (; it != end; ++it) {
      if (&(*it) != &ActiveCharacter()
          && !it -> IsDead()
          && it->GetRect().Contains(pos_monde)) {

        character_found = true;
        break;
      }
    }

    if (character_found) {
      ActiveTeam().SelectCharacter(&(*it));
      ActionHandler::GetInstance()->NewActionActiveCharacter();

      return;
    }

    if (ActiveCharacter().GetRect().Contains(pos_monde)) {
      CharacterCursor::GetInstance()->FollowActiveCharacter();
      return;
    }
  }

  // Choosing target for a weapon, many posibilities :
  // - Do nothing
  // - Choose a target but don't fire
  // - Choose a target and fire it !
  Action* a = new Action(Action::ACTION_WEAPON_SET_TARGET);
  a->Push(GetWorldPosition());
  ActionHandler::GetInstance()->NewAction (a);
}


void Mouse::ActionRightClick(bool /*shift*/) const
{
  Interface::GetInstance()->weapons_menu.SwitchDisplay(GetPosition());
}

void Mouse::ActionWheelUp(bool shift) const
{
  ActiveTeam().AccessWeapon().HandleMouseWheelUp(shift);
}

void Mouse::ActionWheelDown(bool shift) const
{
  ActiveTeam().AccessWeapon().HandleMouseWheelDown(shift);
}

bool Mouse::IS_CLICK_BUTTON(uint button)
{
  return (button==SDL_BUTTON_LEFT || button==SDL_BUTTON_RIGHT || button==SDL_BUTTON_MIDDLE);
}

uint8_t Mouse::BUTTON_RIGHT() // static method
{
  return Config::GetConstRef().GetLeftHandedMouse() ? SDL_BUTTON_LEFT : SDL_BUTTON_RIGHT;
}

uint8_t Mouse::BUTTON_LEFT() // static method
{
  return Config::GetConstRef().GetLeftHandedMouse() ? SDL_BUTTON_RIGHT : SDL_BUTTON_LEFT;
}

static Uint32 HandleLongClick(Uint32 /*interval*/, void *param)
{
  ((Mouse*)param)->SetLongClick();
  return 0;
}

bool Mouse::HandleEvent(const SDL_Event& evnt)
{
  if (!HasFocus()) {
    return false;
  }

  Point2i pos = GetPosition();

  if (click_pos.SquareDistance(pos) > MOUSE_CLICK_SQUARE_DISTANCE) {
    // We have moved too much, consider we are not long-clicking
    EndLongClickTimer();
  }

  if (is_long_click) {
    is_long_click = false;
    was_long_click = true;
    if (Interface::GetInstance()->ActionLongClick(pos, click_pos))
      return true;
    return false;
  }

  if (evnt.type != SDL_MOUSEBUTTONDOWN && evnt.type != SDL_MOUSEBUTTONUP) {
    return false;
  }

  if (evnt.type==SDL_MOUSEBUTTONDOWN && evnt.button.button==BUTTON_LEFT()) {
    click_pos = pos;
    if (Interface::GetInstance()->ActionClickDown(pos))
      return true;

    // Either it's out of the menu, or we want to know how long the click was
    long_click_timer = SDL_AddTimer(LONG_CLICK_DURATION, HandleLongClick, this);
    return true;
  }

  if (evnt.type==SDL_MOUSEBUTTONUP && evnt.button.button==BUTTON_LEFT()) {
    // Ignore click-ups from long clicks
    if (was_long_click) {
      was_long_click = false;
      return true;
    } else {
      EndLongClickTimer();
      if (Interface::GetInstance()->ActionClickUp(pos, click_pos))
        return true;
      if (click_pos.SquareDistance(pos) > MOUSE_CLICK_SQUARE_DISTANCE)
        return true;
    }
  }

  if (Game::GetInstance()->ReadState() != Game::PLAYING ||
      Replay::GetConstInstance()->IsPlaying() ||
      !ActiveTeam().IsLocalHuman())
    return true;

  bool shift = !!(SDL_GetModState() & KMOD_SHIFT);
  if (evnt.type == SDL_MOUSEBUTTONUP) {
    if (evnt.button.button == Mouse::BUTTON_RIGHT())
      ActionRightClick(shift);
    else if (evnt.button.button == Mouse::BUTTON_LEFT())
      ActionLeftClick(shift);
    else if (evnt.button.button == SDL_BUTTON_WHEELDOWN)
      ActionWheelDown(shift);
    else if (evnt.button.button == SDL_BUTTON_WHEELUP)
      ActionWheelUp(shift);
  }

  return true;
}

void Mouse::GetDesignatedCharacter() const
{
  if (Game::GetInstance()->ReadState() != Game::PLAYING)
    return;

  const Point2i pos_monde = GetWorldPosition();

  // Which character is pointed by the mouse ? (appart from the active one)
  Interface::GetInstance()->character_under_cursor = NULL;
  FOR_ALL_LIVING_CHARACTERS(team, character) {
    if (&(*character) != &ActiveCharacter()
        && character->GetRect().Contains(pos_monde)) {
      Interface::GetInstance()->character_under_cursor = &(*character);
    }
  }

  // No character is pointed... what about the active one ?
  if (Interface::GetConstInstance()->character_under_cursor
      && ActiveCharacter().GetRect().Contains(pos_monde)) {
    Interface::GetInstance()->character_under_cursor = &ActiveCharacter();
  }

}

void Mouse::Refresh()
{
  /* FIXME the 200 is hardcoded because I cannot find where the main loop
   * refresh rate is set... */
#define NB_LOOP_BEFORE_HIDE 200
  static int counter = 0;
  GetDesignatedCharacter();

  Point2i pos = GetPosition();

  if (lastpos != pos) {
    Show();
    lastpos = pos;
    counter = NB_LOOP_BEFORE_HIDE;
    ShowGameInterface();
  } else if (visible == MOUSE_VISIBLE) {
    /* The mouse is hidden after a while when not moving */
    if (--counter <= 0)
      Hide();
  }
}

Point2i Mouse::GetPosition() const
{
  int x, y;

  SDL_GetMouseState(&x, &y);
  return Point2i(x, y);
}

Point2i Mouse::GetWorldPosition() const
{
  return GetPosition() + Camera::GetConstInstance()->GetPosition();
}

MouseCursor& Mouse::GetCursor(pointer_t pointer) const
{
  ASSERT(pointer != POINTER_STANDARD);

  if (pointer == POINTER_ATTACK) {
    if (ActiveCharacter().GetDirection() == DIRECTION_RIGHT)
      return GetCursor(POINTER_ATTACK_FROM_LEFT);
    else
      return GetCursor(POINTER_ATTACK_FROM_RIGHT);
  }
  return (*cursors.find(pointer)).second;
}

// set the new pointer type and return the previous one
Mouse::pointer_t Mouse::SetPointer(pointer_t pointer)
{
  if (Config::GetConstInstance()->GetDefaultMouseCursor()) {
    Show(); // be sure cursor is visible
    return Mouse::POINTER_STANDARD;
  }

  if (current_pointer == pointer) return pointer;

  if (pointer == POINTER_STANDARD)
#ifndef HAVE_TOUCHSCREEN
    SDL_ShowCursor(true);
#else
    SDL_ShowCursor(false);
#endif
  else
    SDL_ShowCursor(false);

  pointer_t old_pointer = current_pointer;
  current_pointer = pointer;

  return old_pointer;
}

void Mouse::Draw() const
{
  if (visible != MOUSE_VISIBLE)
    return;

  if (current_pointer == POINTER_STANDARD)
    return; // use standard SDL cursor

  const MouseCursor& cursor = GetCursor(current_pointer);
  const Surface& surf = cursor.GetSurface();
  Point2i pos = GetPosition() - cursor.GetClicPos();
  GetMainWindow().Blit(surf, pos);
  GetWorld().ToRedrawOnScreen(Rectanglei(pos, surf.GetSize()));
}

void Mouse::Show()
{
#ifndef HAVE_TOUCHSCREEN
  if (GameTime::GetConstInstance()->Read()-last_hide_time > 10000 && visible == MOUSE_HIDDEN) {
    CenterPointer();
  }
#endif
  visible = MOUSE_VISIBLE;

  if (Config::GetConstInstance()->GetDefaultMouseCursor()) {
    SDL_ShowCursor(true); // be sure cursor is visible
  }

}

void Mouse::Hide()
{
  // If doing a pause or the like, we do want to see it
  if (Replay::GetConstInstance()->IsPlaying())
    return;

  if (visible == MOUSE_VISIBLE)
    last_hide_time = GameTime::GetConstInstance()->Read();

  visible = MOUSE_HIDDEN;
  SDL_ShowCursor(false); // be sure cursor is invisible

}

// Center the pointer on the screen
void Mouse::CenterPointer()
{
  SetPosition(GetMainWindow().GetSize() / 2);
}

void Mouse::SetPosition(Point2i pos)
{
  if (!HasFocus()) // The application has not the focus, don't move the mouse cursor!
    return;

  MSG_DEBUG("mouse", "1) %d, %d\n", GetPosition().GetX(), GetPosition().GetY());

  SDL_WarpMouse(pos.x, pos.y);
  SDL_PumpEvents(); // force new position else GetPosition does not return new position

  lastpos = GetPosition();

  MSG_DEBUG("mouse", "2) %d, %d\n", GetPosition().GetX(), GetPosition().GetY());
}