Atrinik Client  4.0
menu_buttons.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 
37 enum {
50 
52 } ;
53 
61 static const char *button_images[NUM_BUTTONS] = {
62  "magic", "skill", "protections", "party", "music", "buddy", "ignore",
63  "minimap", "map", "quest", NULL, "cogs"
64 };
68 static const char *const button_tooltips[NUM_BUTTONS] = {
69  "Spells", "Skills", "Protections", "Party", "Music player", "Buddy List",
70  "Ignore List", "Minimap", "Region map", "Quest list", "Help", "Settings"
71 };
75 static int button_widgets[NUM_BUTTONS] = {
76  SPELLS_ID, SKILLS_ID, PROTECTIONS_ID, PARTY_ID, MPLAYER_ID, BUDDY_ID,
77  BUDDY_ID, MINIMAP_ID, -1, -1, -1, -1
78 };
79 
81 static void widget_draw(widgetdata *widget)
82 {
83  int i, x, y;
84  const char *text;
85 
86  if (!widget->redraw) {
87  return;
88  }
89 
90  x = 4;
91  y = 3;
92 
93  /* Render the buttons. */
94  for (i = 0; i < NUM_BUTTONS; i++) {
95  if (x > widget->w - 4) {
96  x = 4;
97  y += texture_surface(buttons[i].texture)->h + 1;
98  }
99 
100  text = NULL;
101 
102  if (button_widgets[i] != -1) {
103  widgetdata *tmp;
104 
105  if (button_widgets[i] == BUDDY_ID) {
106  tmp = widget_find(NULL, button_widgets[i], button_images[i],
107  NULL);
108  } else {
109  tmp = cur_widget[button_widgets[i]];
110  }
111 
112  SOFT_ASSERT(tmp != NULL, "Could not find widget type: %d",
113  button_widgets[i]);
114  buttons[i].pressed_forced = tmp->show;
115  } else if (i == BUTTON_HELP) {
116  text = "[y=2]?";
117  }
118 
119  buttons[i].x = x;
120  buttons[i].y = y;
121  buttons[i].surface = widget->surface;
122  button_set_parent(&buttons[i], widget->x, widget->y);
123  button_show(&buttons[i], text);
124 
125  if (button_images[i]) {
126  char buf[MAX_BUF];
127 
128  snprintf(buf, sizeof(buf), "icon_%s", button_images[i]);
129  surface_show(widget->surface, x, y, NULL, TEXTURE_CLIENT(buf));
130  }
131 
132  x += texture_surface(buttons[i].texture)->w + 3;
133  }
134 }
135 
137 static void widget_background(widgetdata *widget, int draw)
138 {
139  int i;
140 
141  /* Figure out whether we need to redraw the widget due to change in
142  * the buttons.*/
143  for (i = 0; i < NUM_BUTTONS; i++) {
144  if (button_need_redraw(&buttons[i])) {
145  widget->redraw = 1;
146  break;
147  }
148  }
149 }
150 
152 static int widget_event(widgetdata *widget, SDL_Event *event)
153 {
154  int i;
155 
156  for (i = 0; i < NUM_BUTTONS; i++) {
157  if (!button_event(&buttons[i], event)) {
158  if (buttons[i].redraw) {
159  widget->redraw = 1;
160  }
161 
162  if (BUTTON_CHECK_TOOLTIP(&buttons[i])) {
163  tooltip_create(event->motion.x, event->motion.y, FONT_ARIAL11,
164  button_tooltips[i]);
166  }
167 
168  continue;
169  }
170 
171  widget->redraw = 1;
172 
173  /* Toggle visibility of a widget. */
174  if (button_widgets[i] != -1) {
175  widgetdata *tmp;
176 
177  if (button_widgets[i] == BUDDY_ID) {
178  tmp = widget_find(NULL, button_widgets[i], button_images[i],
179  NULL);
180  } else {
181  tmp = cur_widget[button_widgets[i]];
182  }
183 
184  SOFT_ASSERT_RC(tmp != NULL, 1, "Could not find widget type: %d",
185  button_widgets[i]);
186  WIDGET_SHOW_TOGGLE(tmp);
187  SetPriorityWidget(tmp);
188 
189  if (button_widgets[i] == PARTY_ID && tmp->show) {
190  send_command("/party list");
191  }
192 
193  return 1;
194  }
195 
196  /* Decide how to handle the button. */
197  switch (i) {
198  case BUTTON_SETTINGS:
199  settings_open();
200  break;
201 
202  case BUTTON_MAP:
203  send_command_check("/region_map");
204  break;
205 
206  case BUTTON_QUEST:
207  keybind_process_command("?QLIST");
208  break;
209 
210  case BUTTON_HELP:
211  help_show("main");
212  break;
213 
214  default:
215  LOG(BUG, "Cannot handle button ID: %d", i);
216  break;
217  }
218 
219  return 1;
220  }
221 
222  return 0;
223 }
224 
226 static void widget_deinit(widgetdata *widget)
227 {
228  for (int i = 0; i < NUM_BUTTONS; i++) {
229  button_destroy(&buttons[i]);
230  }
231 }
232 
237 {
238  int i;
239 
240  for (i = 0; i < NUM_BUTTONS; i++) {
241  button_create(&buttons[i]);
242 
243  buttons[i].texture = texture_get(TEXTURE_TYPE_CLIENT,
244  "button_rect");
245  buttons[i].texture_over = texture_get(TEXTURE_TYPE_CLIENT,
246  "button_rect_over");
247  buttons[i].texture_pressed = texture_get(TEXTURE_TYPE_CLIENT,
248  "button_rect_down");
249  }
250 
251  buttons[BUTTON_HELP].flags |= TEXT_MARKUP;
252  button_set_font(&buttons[BUTTON_HELP], FONT_SANS16);
253 
254  widget->draw_func = widget_draw;
255  widget->background_func = widget_background;
256  widget->event_func = widget_event;
257  widget->deinit_func = widget_deinit;
258 }
SDL_Surface * texture_surface(texture_struct *texture)
Definition: texture.c:303
SDL_Surface * surface
Definition: widget.h:110
int button_event(button_struct *button, SDL_Event *event)
Definition: button.c:222
void button_destroy(button_struct *button)
Definition: button.c:94
void tooltip_create(int mx, int my, font_struct *font, const char *text)
Definition: tooltip.c:60
texture_struct * texture_get(texture_type_t type, const char *name)
Definition: texture.c:279
#define TEXT_MARKUP
Definition: text.h:224
void send_command(const char *command)
Definition: player.c:175
widgetdata * widget_find(widgetdata *where, int type, const char *id, SDL_Surface *surface)
Definition: widget.c:2091
void help_show(const char *name)
Definition: help.c:219
uint8_t show
Definition: widget.h:69
void button_set_font(button_struct *button, font_struct *font)
Definition: button.c:114
texture_struct * texture_over
Definition: button.h:67
widgetdata * cur_widget[TOTAL_SUBWIDGETS]
Definition: widget.c:75
uint8_t redraw
Definition: widget.h:72
void button_create(button_struct *button)
Definition: button.c:65
int x
Definition: widget.h:48
#define BUTTON_CHECK_TOOLTIP(button)
Definition: button.h:38
void tooltip_enable_delay(uint32_t delay)
Definition: tooltip.c:82
void settings_open(void)
Definition: settings.c:177
texture_struct * texture
Definition: button.h:61
SDL_Surface * surface
Definition: button.h:46
int w
Definition: widget.h:54
texture_struct * texture_pressed
Definition: button.h:73
int send_command_check(const char *cmd)
Definition: menu.c:376
void surface_show(SDL_Surface *surface, int x, int y, SDL_Rect *srcrect, SDL_Surface *src)
Definition: sprite.c:761
void SetPriorityWidget(widgetdata *node)
Definition: widget.c:1788
void button_show(button_struct *button, const char *text)
Definition: button.c:161
uint64_t flags
Definition: button.h:79
int keybind_process_command(const char *cmd)
Definition: keybind.c:553
int pressed_forced
Definition: button.h:108
int y
Definition: widget.h:51