Atrinik Client  4.0
server_add.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 #include <toolkit/string.h>
33 
35 static text_input_struct text_input_server_host, text_input_server_port;
38 
40 static int popup_draw(popup_struct *popup)
41 {
42  SDL_Rect box;
43 
44  box.w = popup->surface->w;
45  box.h = 38;
46  text_show(popup->surface, FONT_SERIF16, "Add a server", 0, 0, COLOR_HGOLD, TEXT_ALIGN_CENTER | TEXT_VALIGN_CENTER, &box);
47 
48  box.w = popup->surface->w - 20;
49  box.h = 60;
50 
51  text_show(popup->surface, FONT_ARIAL11, "Here you can add a server to the list of available servers if you know the server's hostname and port.\n\nBe careful about connecting to unlisted servers, since they could be dangerous.", 10, 45, COLOR_WHITE, TEXT_WORD_WRAP, &box);
52 
53  text_show(popup->surface, FONT_ARIAL11, "[b]Hostname:[/b]", 10, 130, COLOR_WHITE, TEXT_MARKUP, NULL);
54  text_show(popup->surface, FONT_ARIAL11, "[b]Port:[/b]", 10, 150, COLOR_WHITE, TEXT_MARKUP, NULL);
55 
56  text_input_set_parent(&text_input_server_host, popup->x, popup->y);
57  text_input_set_parent(&text_input_server_port, popup->x, popup->y);
58 
59  text_input_show(&text_input_server_host, popup->surface, 80, 130);
60  text_input_show(&text_input_server_port, popup->surface, 80, 150);
61 
62  button_set_parent(&button_add, popup->x, popup->y);
63  button_add.x = 230;
64  button_add.y = 170;
65  button_add.surface = popup->surface;
66  button_show(&button_add, "Add");
67 
68  return 1;
69 }
70 
72 static int popup_event(popup_struct *popup, SDL_Event *event)
73 {
74  if (button_event(&button_add, event) || (event->type == SDL_KEYDOWN && IS_ENTER(event->key.keysym.sym))) {
75  char buf[HUGE_BUF];
76 
77  if (*text_input_server_host.str == '\0' || *text_input_server_port.str == '\0' || atoi(text_input_server_port.str) <= 0) {
78  return -1;
79  }
80 
81  snprintf(buf, sizeof(buf), "%s %s", text_input_server_host.str, text_input_server_port.str);
82 
83  clioption_settings.servers = erealloc(clioption_settings.servers, sizeof(*clioption_settings.servers) * (clioption_settings.servers_num + 1));
84  clioption_settings.servers[clioption_settings.servers_num] = estrdup(buf);
85  clioption_settings.servers_num++;
86 
87  if (!ms_connecting(-1)) {
88  cpl.state = ST_META;
89  }
90 
91  popup_destroy(popup);
92  return 1;
93  }
94 
95  if (text_input_event(&text_input_server_host, event) || text_input_event(&text_input_server_port, event)) {
96  return 1;
97  }
98 
99  if (event->type == SDL_MOUSEBUTTONDOWN && event->button.button == SDL_BUTTON_LEFT) {
100  if (text_input_mouse_over(&text_input_server_host, event->button.x, event->button.y)) {
101  text_input_server_port.focus = 0;
102  text_input_server_host.focus = 1;
103  } else if (text_input_mouse_over(&text_input_server_port, event->button.x, event->button.y)) {
104  text_input_server_host.focus = 0;
105  text_input_server_port.focus = 1;
106  }
107  } else if (event->type == SDL_KEYDOWN) {
108  if (event->key.keysym.sym == SDLK_TAB) {
109  text_input_server_host.focus = !text_input_server_host.focus;
110  text_input_server_port.focus = !text_input_server_port.focus;
111  return 1;
112  }
113  }
114 
115  return -1;
116 }
117 
120 {
121  text_input_destroy(&text_input_server_host);
122  text_input_destroy(&text_input_server_port);
123 
124  button_destroy(&button_add);
125 
126  return 1;
127 }
128 
132 void server_add_open(void)
133 {
134  popup_struct *popup;
135 
136  popup = popup_create(texture_get(TEXTURE_TYPE_CLIENT, "popup"));
137  popup->draw_func = popup_draw;
138  popup->event_func = popup_event;
140 
141  text_input_create(&text_input_server_host);
142  text_input_create(&text_input_server_port);
143 
144  text_input_server_port.focus = 0;
145  text_input_set(&text_input_server_port, "13327");
146 
147  button_create(&button_add);
148 }
#define TEXT_WORD_WRAP
Definition: text.h:226
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
static int popup_event(popup_struct *popup, SDL_Event *event)
Definition: server_add.c:72
int button_event(button_struct *button, SDL_Event *event)
Definition: button.c:222
int(* destroy_callback_func)(struct popup_struct *popup)
Definition: popup.h:159
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
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
void server_add_open(void)
Definition: server_add.c:132
Client_Player cpl
Definition: client.c:50
void button_create(button_struct *button)
Definition: button.c:65
static int popup_draw(popup_struct *popup)
Definition: server_add.c:40
static int popup_destroy_callback(popup_struct *popup)
Definition: server_add.c:119
int ms_connecting(int val)
Definition: metaserver.c:678
char str[HUGE_BUF]
Definition: text_input.h:55
static text_input_struct text_input_server_host
Definition: server_add.c:35
int y
Definition: popup.h:90
SDL_Surface * surface
Definition: button.h:46
Definition: main.h:247
clioption_settings_struct clioption_settings
Definition: main.c:87
static button_struct button_add
Definition: server_add.c:37
SDL_Surface * surface
Definition: popup.h:65
void button_show(button_struct *button, const char *text)
Definition: button.c:161
#define TEXT_VALIGN_CENTER
Definition: text.h:246
void text_input_destroy(text_input_struct *text_input)
Definition: text_input.c:96
#define COLOR_WHITE
Definition: text.h:289