Atrinik Client  4.0
input.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 
35 static void widget_input_handle_enter(widgetdata *widget)
36 {
38 
39  widget->show = 0;
40  text_input = &WIDGET_INPUT(widget)->text_input;
41 
42  if (*text_input->str != '\0') {
43  StringBuffer *sb;
44  char *cp, *str;
45 
46  sb = stringbuffer_new();
47 
48  if (*text_input->str != '/') {
49  stringbuffer_append_string(sb, WIDGET_INPUT(widget)->prepend_text);
50  }
51 
52  str = text_escape_markup(text_input->str);
53  stringbuffer_append_string(sb, str);
54  efree(str);
55 
56  cp = stringbuffer_finish(sb);
58  efree(cp);
59  }
60 }
61 
63 static void widget_draw(widgetdata *widget)
64 {
66 
67  widget->redraw++;
68 
69  text_input = &WIDGET_INPUT(widget)->text_input;
70  text_input->coords.w = widget->w - 16;
71  text_input_show(text_input, widget->surface, widget->w / 2 - text_input->coords.w / 2, widget->h / 2 - text_input->coords.h / 2 + 8);
72 
73  text_truncate_overflow(FONT_ARIAL10, WIDGET_INPUT(widget)->title_text, 220);
74  text_show(widget->surface, FONT_ARIAL10, WIDGET_INPUT(widget)->title_text, 8, 6, COLOR_HGOLD, 0, NULL);
75 }
76 
78 static int widget_event(widgetdata *widget, SDL_Event *event)
79 {
80  widget_input_struct *input;
82 
83  if (widget->show && event->type == SDL_KEYDOWN) {
84  input = WIDGET_INPUT(widget);
85 
86  if (SDL_GetTicks() - widget->showed_ticks > 125 && ((string_startswith(input->prepend_text, "/gettag ") && keybind_command_matches_event("?GET", &event->key)) || (string_startswith(input->prepend_text, "/droptag ") && keybind_command_matches_event("?DROP", &event->key)))) {
87  widget_input_handle_enter(widget);
88  keys[event->key.keysym.sym].time = SDL_GetTicks() + 125;
89  return 1;
90  }
91 
92  text_input = &input->text_input;
93 
94  if (IS_ENTER(event->key.keysym.sym)) {
95  widget_input_handle_enter(widget);
96  }
97 
98  if (event->key.keysym.sym == SDLK_ESCAPE) {
99  widget->show = 0;
100  return 1;
101  } else if (event->key.keysym.sym == SDLK_TAB) {
102  help_handle_tabulator(text_input);
103  return 1;
104  } else if (text_input_event(text_input, event)) {
105  return 1;
106  }
107  }
108 
109  return 0;
110 }
111 
113 static void widget_deinit(widgetdata *widget)
114 {
115  widget_input_struct *input;
116 
117  input = WIDGET_INPUT(widget);
118 
119  text_input_destroy(&input->text_input);
120  text_input_history_free(input->text_input_history);
121 }
122 
123 void widget_input_init(widgetdata *widget)
124 {
125  widget_input_struct *input;
126 
127  input = ecalloc(1, sizeof(*input));
128  text_input_create(&input->text_input);
129  input->text_input.max = 250;
130  input->text_input_history = text_input_history_create();
131 
132  widget->draw_func = widget_draw;
133  widget->event_func = widget_event;
134  widget->deinit_func = widget_deinit;
135  widget->subwidget = input;
136 }
void * subwidget
Definition: widget.h:107
static void widget_draw(widgetdata *widget)
Definition: input.c:63
SDL_Surface * surface
Definition: widget.h:110
SDL_Rect coords
Definition: text_input.h:96
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
#define COLOR_HGOLD
Definition: text.h:319
static text_input_struct text_input
Definition: interface.c:59
void help_handle_tabulator(text_input_struct *text_input)
Definition: help.c:249
static void widget_deinit(widgetdata *widget)
Definition: input.c:113
uint8_t show
Definition: widget.h:69
void text_truncate_overflow(font_struct *font, char *text, int max_width)
Definition: text.c:2446
uint8_t redraw
Definition: widget.h:72
uint32_t time
Definition: event.h:53
char str[HUGE_BUF]
Definition: text_input.h:55
int keybind_command_matches_event(const char *cmd, SDL_KeyboardEvent *event)
Definition: keybind.c:391
int w
Definition: widget.h:54
int h
Definition: widget.h:57
int send_command_check(const char *cmd)
Definition: menu.c:376
static int widget_event(widgetdata *widget, SDL_Event *event)
Definition: input.c:78
void text_input_destroy(text_input_struct *text_input)
Definition: text_input.c:96
char * text_escape_markup(const char *buf)
Definition: text.c:532