Atrinik Client  4.0
button.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 
32 #include <global.h>
33 
38 {
39 }
40 
50 {
51  if (button->pressed && button->texture_pressed) {
52  return button->texture_pressed;
53  } else if (button->mouse_over && button->texture_over) {
54  return button->texture_over;
55  } else {
56  return button->texture;
57  }
58 }
59 
66 {
67  memset(button, 0, sizeof(*button));
68 
69  /* Initialize default values. */
70  button->surface = ScreenSurface;
71  button->x = button->y = 0;
72  button->texture = texture_get(TEXTURE_TYPE_CLIENT, "button");
73  button->texture_pressed = texture_get(TEXTURE_TYPE_CLIENT, "button_down");
74  button->texture_over = texture_get(TEXTURE_TYPE_CLIENT, "button_over");
75  button->flags = 0;
76  button->center = 1;
77  button->color = COLOR_WHITE;
78  button->color_shadow = COLOR_BLACK;
79  button->color_over = COLOR_HGOLD;
81  button->mouse_over = button->pressed = button->pressed_forced = button->disabled = 0;
82  button->pressed_ticks = button->hover_ticks = button->pressed_repeat_ticks = 0;
83  button->repeat_func = NULL;
84 
85  button_set_font(button, FONT_ARIAL10);
86 }
87 
95 {
96  if (button->font) {
97  font_free(button->font);
98  }
99 }
100 
101 void button_set_parent(button_struct *button, int px, int py)
102 {
103  button->px = px;
104  button->py = py;
105 }
106 
115 {
116  if (button->font) {
117  font_free(button->font);
118  }
119 
120  if (font) {
121  FONT_INCREF(font);
122  }
123 
124  button->font = font;
125 }
126 
127 int button_need_redraw(button_struct *button)
128 {
129  int ret;
130 
131  ret = 0;
132 
133  if (button->mouse_over || button->pressed) {
134  int state, mx, my, mover;
135 
136  state = SDL_GetMouseState(&mx, &my);
137  mover = BUTTON_MOUSE_OVER(button, mx, my, texture_surface(button->texture));
138 
139  if (button->mouse_over && !mover) {
140  button->mouse_over = 0;
141  ret = 1;
142  }
143 
144  if (button->pressed && !button->pressed_forced &&
145  (!mover || state != SDL_BUTTON_LEFT)) {
146  button->pressed = 0;
147  ret = 1;
148  }
149  }
150 
151  return ret;
152 }
153 
161 void button_show(button_struct *button, const char *text)
162 {
163  SDL_Surface *texture;
164 
165  (void) button_need_redraw(button);
166 
167  if (button->pressed_forced) {
168  button->pressed = 1;
169  }
170 
171  texture = texture_surface(button_determine_texture(button));
172  surface_show(button->surface, button->x, button->y, NULL, texture);
173 
174  if (text) {
175  const char *color, *color_shadow;
176  int x, y;
177 
178  if (button->disabled) {
179  color = COLOR_GRAY;
180  color_shadow = COLOR_BLACK;
181  } else if (button->mouse_over) {
182  color = button->color_over;
183  color_shadow = button->color_over_shadow;
184  } else {
185  color = button->color;
186  color_shadow = button->color_shadow;
187  }
188 
189  x = button->x;
190  y = button->y;
191 
192  if (button->center) {
193  x += texture->w / 2 - text_get_width(button->font, text, button->flags) / 2;
194  y += texture->h / 2 - FONT_HEIGHT(button->font) / 2;
195  }
196 
197  if (!color_shadow) {
198  text_show(button->surface, button->font, text, x, y, color, button->flags, NULL);
199  } else {
200  text_show_shadow(button->surface, button->font, text, x, y - 2, color, color_shadow, button->flags, NULL);
201  }
202  }
203 
204  if (button->repeat_func && button->pressed && SDL_GetTicks() - button->pressed_ticks > button->pressed_repeat_ticks) {
205  button->repeat_func(button);
206  button->pressed_ticks = SDL_GetTicks();
207  button->pressed_repeat_ticks = 150;
208  }
209 
210  button->redraw = 0;
211 }
212 
222 int button_event(button_struct *button, SDL_Event *event)
223 {
224  SDL_Surface *texture;
225  int old_mouse_over;
226 
227  if (event->type != SDL_MOUSEBUTTONUP && event->type != SDL_MOUSEBUTTONDOWN && event->type != SDL_MOUSEMOTION) {
228  return 0;
229  }
230 
231  /* Mouse button is released, the button is no longer being pressed. */
232  if (event->type == SDL_MOUSEBUTTONUP) {
233  button->pressed = 0;
234  button->redraw = 1;
235  return 0;
236  }
237 
238  old_mouse_over = button->mouse_over;
239  /* Always reset this. */
240  button->mouse_over = 0;
241 
242  /* The button is disabled, we don't care about the mouse. */
243  if (button->disabled) {
244  return 0;
245  }
246 
247  texture = texture_surface(button_determine_texture(button));
248 
249  if (BUTTON_MOUSE_OVER(button, event->motion.x, event->motion.y, texture)) {
250  if (event->type == SDL_MOUSEMOTION) {
251  cursor_texture = texture_get(TEXTURE_TYPE_CLIENT, "cursor_pointer");
252  }
253 
254  /* Left mouse click, the button has been pressed. */
255  if (event->type == SDL_MOUSEBUTTONDOWN && event->button.button == SDL_BUTTON_LEFT) {
256  button->pressed = 1;
257  button->pressed_ticks = SDL_GetTicks();
258  button->pressed_repeat_ticks = 750;
259  button->redraw = 1;
260  return 1;
261  } else {
262  button->mouse_over = 1;
263 
264  /* Do not reset hover ticks if the previous state was already
265  * in highlight mode. */
266  if (!old_mouse_over) {
267  button->hover_ticks = SDL_GetTicks();
268  }
269  }
270  }
271 
272  if (old_mouse_over != button->mouse_over) {
273  button->redraw = 1;
274  }
275 
276  return 0;
277 }
#define COLOR_BLACK
Definition: text.h:323
font_struct * font
Definition: button.h:76
uint32_t hover_ticks
Definition: button.h:117
uint8_t redraw
Definition: button.h:132
SDL_Surface * texture_surface(texture_struct *texture)
Definition: texture.c:303
int button_event(button_struct *button, SDL_Event *event)
Definition: button.c:222
void button_destroy(button_struct *button)
Definition: button.c:94
void text_show(SDL_Surface *surface, font_struct *font, const char *text, int x, int y, const char *color_notation, uint64_t flags, SDL_Rect *box)
Definition: text.c:1983
void button_init()
Definition: button.c:37
#define COLOR_HGOLD
Definition: text.h:319
texture_struct * texture_get(texture_type_t type, const char *name)
Definition: texture.c:279
void font_free(font_struct *font)
Definition: text.c:262
const char * color_shadow
Definition: button.h:90
SDL_Surface * ScreenSurface
Definition: main.c:47
void(* repeat_func)(struct button_struct *button)
Definition: button.h:127
static texture_struct * button_determine_texture(button_struct *button)
Definition: button.c:49
#define COLOR_GRAY
Definition: text.h:301
void button_set_font(button_struct *button, font_struct *font)
Definition: button.c:114
texture_struct * texture_over
Definition: button.h:67
const char * color_over_shadow
Definition: button.h:96
#define FONT_INCREF(font)
Definition: text.h:67
void button_create(button_struct *button)
Definition: button.c:65
#define FONT_HEIGHT(font)
Definition: text.h:327
void text_show_shadow(SDL_Surface *surface, font_struct *font, const char *text, int x, int y, const char *color_notation, const char *color_shadow_notation, uint64_t flags, SDL_Rect *box)
Definition: text.c:2278
uint32_t pressed_repeat_ticks
Definition: button.h:120
texture_struct * texture
Definition: button.h:61
uint32_t pressed_ticks
Definition: button.h:114
SDL_Surface * surface
Definition: button.h:46
uint8_t center
Definition: button.h:84
int disabled
Definition: button.h:111
texture_struct * texture_pressed
Definition: button.h:73
int text_get_width(font_struct *font, const char *text, uint64_t flags)
Definition: text.c:2328
#define BUTTON_MOUSE_OVER(button, mx, my, texture)
Definition: button.h:34
void surface_show(SDL_Surface *surface, int x, int y, SDL_Rect *srcrect, SDL_Surface *src)
Definition: sprite.c:761
const char * color_over
Definition: button.h:93
void button_show(button_struct *button, const char *text)
Definition: button.c:161
uint64_t flags
Definition: button.h:79
#define COLOR_WHITE
Definition: text.h:289
int mouse_over
Definition: button.h:99
int pressed_forced
Definition: button.h:108
const char * color
Definition: button.h:87