Atrinik Client  4.0
settings.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 #include <toolkit/string.h>
34 
38 enum {
40  BUTTON_KEY_SETTINGS,
41  BUTTON_LOGOUT,
42  BUTTON_DISCONNECT,
43 
44  BUTTON_NUM
45 } ;
46 
50 static const char *const button_names[BUTTON_NUM] = {
51  "Client Settings", "Key Settings", "Logout", "Disconnect"
52 };
53 
57 static size_t button_selected;
58 
66 static void settings_button_handle(popup_struct *popup, size_t button)
67 {
68  if (button == BUTTON_SETTINGS) {
69  settings_client_open();
70  } else if (button == BUTTON_KEY_SETTINGS) {
71  settings_keybinding_open();
72  } else if (button == BUTTON_LOGOUT) {
73  clioption_settings.connect[1] = estrdup(cpl.account);
74  clioption_settings.connect[2] = estrdup(cpl.password);
76  login_start();
77  } else if (button == BUTTON_DISCONNECT) {
79  cpl.state = ST_INIT;
80  }
81 
82  popup_destroy(popup);
83 }
84 
86 static int popup_draw(popup_struct *popup)
87 {
88  SDL_Rect box;
89  size_t i;
90 
91  box.w = popup->surface->w;
92  box.h = 38;
93  text_show(popup->surface, FONT_SERIF20, "Settings", 0, 0, COLOR_HGOLD, TEXT_ALIGN_CENTER | TEXT_VALIGN_CENTER, &box);
94 
95  box.h = 0;
96 
97  for (i = 0; i < BUTTON_NUM; i++) {
98  if (cpl.state != ST_PLAY && (i == BUTTON_DISCONNECT || i == BUTTON_LOGOUT)) {
99  continue;
100  }
101 
102  if (button_selected == i) {
103  text_show_shadow_format(popup->surface, FONT_SERIF40, 0, 60 + i * FONT_HEIGHT(FONT_SERIF40), COLOR_HGOLD, COLOR_BLACK, TEXT_ALIGN_CENTER | TEXT_MARKUP, &box, "[c=#9f0408]>[/c] %s [c=#9f0408]<[/c]", button_names[i]);
104  } else {
105  text_show_shadow(popup->surface, FONT_SERIF40, button_names[i], 0, 60 + i * FONT_HEIGHT(FONT_SERIF40), COLOR_WHITE, COLOR_BLACK, TEXT_ALIGN_CENTER, &box);
106  }
107  }
108 
109  return 1;
110 }
111 
113 static int popup_event(popup_struct *popup, SDL_Event *event)
114 {
115  if (event->type == SDL_KEYDOWN) {
116  /* Move the selected button up and down. */
117  if (event->key.keysym.sym == SDLK_UP || event->key.keysym.sym == SDLK_DOWN) {
118  int selected, num_buttons;
119 
120  selected = button_selected + (event->key.keysym.sym == SDLK_DOWN ? 1 : -1);
121  num_buttons = (cpl.state == ST_PLAY ? BUTTON_NUM : BUTTON_LOGOUT) - 1;
122 
123  if (selected < 0) {
124  selected = num_buttons;
125  } else if (selected > num_buttons) {
126  selected = 0;
127  }
128 
129  button_selected = selected;
130  return 1;
131  } else if (IS_ENTER(event->key.keysym.sym)) {
133  return 1;
134  }
135  } else if (event->type == SDL_MOUSEBUTTONDOWN || event->type == SDL_MOUSEMOTION) {
136  size_t i;
137  int x, y, width;
138 
139  for (i = 0; i < BUTTON_NUM; i++) {
140  if (cpl.state != ST_PLAY && (i == BUTTON_DISCONNECT || i == BUTTON_LOGOUT)) {
141  continue;
142  }
143 
144  y = popup->y + 60 + i * FONT_HEIGHT(FONT_SERIF40);
145 
146  if (event->motion.y >= y && event->motion.y < y + FONT_HEIGHT(FONT_SERIF40)) {
147  width = text_get_width(FONT_SERIF40, button_names[i], 0);
148  x = popup->x + popup->surface->w / 2 - width / 2;
149 
150  if (event->motion.x >= x && event->motion.x < x + width) {
151  if (event->type == SDL_MOUSEMOTION) {
152  button_selected = i;
153  } else if (event->type == SDL_MOUSEBUTTONDOWN && event->button.button == SDL_BUTTON_LEFT) {
154  settings_button_handle(popup, i);
155  return 1;
156  }
157 
158  break;
159  }
160  }
161  }
162  }
163 
164  return -1;
165 }
166 
168 static int popup_button_event(popup_button *button)
169 {
170  help_show("esc menu");
171  return 1;
172 }
173 
177 void settings_open(void)
178 {
179  popup_struct *popup;
180 
181  popup = popup_create(texture_get(TEXTURE_TYPE_CLIENT, "popup"));
182  popup->draw_func = popup_draw;
183  popup->event_func = popup_event;
184 
186  popup_button_set_text(&popup->button_left, "?");
187 
188  button_selected = cpl.state == ST_PLAY ? BUTTON_DISCONNECT : BUTTON_SETTINGS;
189 }
void client_socket_close(client_socket_t *csock)
Definition: socket.c:429
#define COLOR_BLACK
Definition: text.h:323
Definition: main.h:332
int(* event_func)(struct popup_struct *popup, SDL_Event *event)
Definition: popup.h:148
int(* draw_func)(struct popup_struct *popup)
Definition: popup.h:127
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
int x
Definition: popup.h:87
#define COLOR_HGOLD
Definition: text.h:319
texture_struct * texture_get(texture_type_t type, const char *name)
Definition: texture.c:279
#define TEXT_MARKUP
Definition: text.h:224
#define TEXT_ALIGN_CENTER
Definition: text.h:230
static int popup_event(popup_struct *popup, SDL_Event *event)
Definition: settings.c:113
char account[MAX_BUF]
Definition: player.h:227
client_socket_t csocket
Definition: main.c:51
void help_show(const char *name)
Definition: help.c:219
static const char *const button_names[BUTTON_NUM]
Definition: settings.c:50
void text_show_shadow_format(SDL_Surface *surface, font_struct *font, int x, int y, const char *color_notation, const char *color_shadow_notation, uint64_t flags, SDL_Rect *box, const char *format,...)
Definition: text.c:2305
Client_Player cpl
Definition: client.c:50
char password[MAX_BUF]
Definition: player.h:232
int(* event_func)(struct popup_button *button)
Definition: popup.h:56
static size_t button_selected
Definition: settings.c:57
static void settings_button_handle(popup_struct *popup, size_t button)
Definition: settings.c:66
#define FONT_HEIGHT(font)
Definition: text.h:327
void login_start(void)
Definition: login.c:281
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
void settings_open(void)
Definition: settings.c:177
Definition: main.h:242
int y
Definition: popup.h:90
popup_button button_left
Definition: popup.h:93
static int popup_button_event(popup_button *button)
Definition: settings.c:168
int text_get_width(font_struct *font, const char *text, uint64_t flags)
Definition: text.c:2328
clioption_settings_struct clioption_settings
Definition: main.c:87
static int popup_draw(popup_struct *popup)
Definition: settings.c:86
SDL_Surface * surface
Definition: popup.h:65
#define TEXT_VALIGN_CENTER
Definition: text.h:246
#define COLOR_WHITE
Definition: text.h:289