Atrinik Client  4.0
event.c
Go to the documentation of this file.
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
31 #include <global.h>
32 
35 
36 static int dragging_old_mx = -1, dragging_old_my = -1;
37 
38 int event_dragging_check(void)
39 {
40  int mx, my;
41 
42  if (!cpl.dragging_tag) {
43  return 0;
44  }
45 
46  SDL_GetMouseState(&mx, &my);
47 
48  if (abs(cpl.dragging_startx - mx) < 3 && abs(cpl.dragging_starty - my) < 3) {
49  return 0;
50  }
51 
52  return 1;
53 }
54 
55 int event_dragging_need_redraw(void)
56 {
57  int mx, my;
58 
59  if (!event_dragging_check()) {
60  return 0;
61  }
62 
63  SDL_GetMouseState(&mx, &my);
64 
65  if (mx != dragging_old_mx || my != dragging_old_my) {
66  dragging_old_mx = mx;
67  dragging_old_my = my;
68 
69  return 1;
70  }
71 
72  return 0;
73 }
74 
75 void event_dragging_start(tag_t tag, int mx, int my)
76 {
77  dragging_old_mx = -1;
78  dragging_old_my = -1;
79 
80  cpl.dragging_tag = tag;
81  cpl.dragging_startx = mx;
82  cpl.dragging_starty = my;
83 
84  event_dragging_set_callback(NULL);
85 }
86 
87 void event_dragging_set_callback(event_drag_cb_fnc fnc)
88 {
89  event_drag_cb = fnc;
90 }
91 
92 void event_dragging_stop(void)
93 {
94  cpl.dragging_tag = 0;
95 }
96 
97 static void event_dragging_stop_internal(void)
98 {
99  if (event_dragging_check() && event_drag_cb != NULL) {
100  event_drag_cb();
101  }
102 
103  event_dragging_stop();
104 }
105 
115 void resize_window(int width, int height)
116 {
119 
120  if (!setting_get_int(OPT_CAT_CLIENT, OPT_OFFSCREEN_WIDGETS) && width > 100 && height > 100) {
122  }
123 }
124 
131 {
132  SDL_Event event;
133  int x, y, done = 0;
134  static Uint32 Ticks = 0;
135  SDLKey key;
136 
137  /* Execute mouse actions, even if mouse button is being held. */
138  if ((SDL_GetTicks() - Ticks > 125) || !Ticks) {
139  if (cpl.state >= ST_PLAY) {
140  /* Mouse gesture: hold right+left buttons or middle button
141  * to fire. */
142  if (widget_mouse_event.owner == cur_widget[MAP_ID]) {
143  if (map_mouse_fire()) {
144  Ticks = SDL_GetTicks();
145  }
146  }
147  }
148  }
149 
150  while (SDL_PollEvent(&event)) {
151  x = event.motion.x;
152  y = event.motion.y;
153 
154  if (event.type == SDL_KEYDOWN) {
155  if (!keys[event.key.keysym.sym].pressed) {
156  keys[event.key.keysym.sym].repeated = 0;
157  keys[event.key.keysym.sym].pressed = 1;
158  keys[event.key.keysym.sym].time = LastTick + KEY_REPEAT_TIME_INIT;
159  }
160  } else if (event.type == SDL_KEYUP) {
161  keys[event.key.keysym.sym].pressed = 0;
162  } else if (event.type == SDL_MOUSEMOTION) {
163  tooltip_dismiss();
164  }
165 
166  if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_PRINT) {
168  continue;
169  }
170 
171  switch (event.type) {
172  /* Screen has been resized, update screen size. */
173  case SDL_VIDEORESIZE:
174  ScreenSurface = SDL_SetVideoMode(event.resize.w, event.resize.h, video_get_bpp(), get_video_flags());
175 
176  if (!ScreenSurface) {
177  LOG(ERROR, "Unable to grab surface after resize event: %s", SDL_GetError());
178  exit(1);
179  }
180 
181  /* Set resolution to custom. */
183  resize_window(event.resize.w, event.resize.h);
184  break;
185 
186  case SDL_MOUSEBUTTONDOWN:
187  case SDL_MOUSEBUTTONUP:
188  case SDL_MOUSEMOTION:
189  case SDL_KEYUP:
190  case SDL_KEYDOWN:
191 
192  if (event.type == SDL_MOUSEMOTION) {
193  cursor_x = x;
194  cursor_y = y;
195  cursor_texture = texture_get(TEXTURE_TYPE_CLIENT, "cursor_default");
196  }
197 
198  if (popup_handle_event(&event)) {
199  break;
200  }
201 
202  if (event_dragging_check() && event.type != SDL_MOUSEBUTTONUP) {
203  break;
204  }
205 
206  if (cpl.state <= ST_WAITFORPLAY && intro_event(&event)) {
207  break;
208  } else if (cpl.state == ST_PLAY && widgets_event(&event)) {
209  break;
210  }
211 
212  if (cpl.state == ST_PLAY && (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP)) {
213  key_handle_event(&event.key);
214  break;
215  }
216 
217  break;
218 
219  case SDL_QUIT:
220  done = 1;
221  break;
222 
223  default:
224  break;
225  }
226 
227  if (event.type == SDL_MOUSEBUTTONUP) {
228  event_dragging_stop_internal();
229  }
230  }
231 
232  for (key = 0; key < SDLK_LAST; key++) {
233  /* Ignore modifier keys. */
234  if (KEY_IS_MODIFIER(key)) {
235  continue;
236  }
237 
238  if (keys[key].pressed && keys[key].time + KEY_REPEAT_TIME - 5 < LastTick) {
239  keys[key].time = LastTick + KEY_REPEAT_TIME - 5;
240  keys[key].repeated = 1;
241  event_push_key(SDL_KEYDOWN, key, SDL_GetModState());
242  }
243  }
244 
245  return done;
246 }
247 
248 void event_push_key(SDL_EventType type, SDLKey key, SDLMod mod)
249 {
250  SDL_Event event;
251 
252  event.type = type;
253  event.key.which = 0;
254  event.key.state = type == SDL_KEYDOWN ? SDL_PRESSED : SDL_RELEASED;
255  event.key.keysym.unicode = key;
256  event.key.keysym.sym = key;
257  event.key.keysym.mod = mod;
258  SDL_PushEvent(&event);
259 }
260 
261 void event_push_key_once(SDLKey key, SDLMod mod)
262 {
263  event_push_key(SDL_KEYDOWN, key, mod);
264  event_push_key(SDL_KEYUP, key, mod);
265 }
#define KEY_REPEAT_TIME_INIT
Definition: keybind.h:56
Definition: main.h:332
uint32_t get_video_flags(void)
Definition: video.c:365
bool map_mouse_fire(void)
Definition: map.c:2173
void key_handle_event(SDL_KeyboardEvent *event)
Definition: keys.c:47
static event_drag_cb_fnc event_drag_cb
Definition: event.c:34
void setting_set_int(int cat, int setting, int64_t val)
Definition: settings.c:592
int intro_event(SDL_Event *event)
Definition: intro.c:408
texture_struct * texture_get(texture_type_t type, const char *name)
Definition: texture.c:279
SDL_Surface * ScreenSurface
Definition: main.c:47
void(* event_drag_cb_fnc)(void)
Definition: event.h:43
uint8_t pressed
Definition: event.h:50
int video_get_bpp(void)
Definition: video.c:334
tag_t dragging_tag
Definition: player.h:187
void widgets_ensure_onscreen(void)
Definition: widget.c:804
#define KEY_IS_MODIFIER(_key)
Definition: keybind.h:58
Client_Player cpl
Definition: client.c:50
#define KEY_REPEAT_TIME
Definition: keybind.h:54
int64_t setting_get_int(int cat, int setting)
Definition: settings.c:414
widgetdata * cur_widget[TOTAL_SUBWIDGETS]
Definition: widget.c:75
int dragging_startx
Definition: player.h:192
int widgets_event(SDL_Event *event)
Definition: widget.c:1205
uint32_t time
Definition: event.h:53
void tooltip_dismiss(void)
Definition: tooltip.c:175
void resize_window(int width, int height)
Definition: event.c:115
uint32_t LastTick
Definition: main.c:57
uint8_t repeated
Definition: event.h:56
widgetdata * owner
Definition: widget.h:360
widgetevent widget_mouse_event
Definition: widget.c:82
int dragging_starty
Definition: player.h:197
int Event_PollInputDevice(void)
Definition: event.c:130
void screenshot_create(SDL_Surface *surface)
Definition: misc.c:132