Atrinik Client  4.0
book.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 
36 static char *book_content = NULL;
38 static char book_name[HUGE_BUF];
40 static uint32_t book_lines = 0;
42 static uint32_t book_scroll_lines = 0;
44 static uint32_t book_scroll = 0;
46 UT_array *book_help_history = NULL;
48 static uint8_t book_help_history_enabled = 0;
51 
59 void book_name_change(const char *name, size_t len)
60 {
61  len = MIN(sizeof(book_name) - 1, len);
62  strncpy(book_name, name, len);
63  book_name[len] = '\0';
64 }
65 
67 static int popup_draw_func(popup_struct *popup)
68 {
69  if (popup->redraw) {
70  SDL_Rect box;
71 
72  surface_show(popup->surface, 0, 0, NULL, texture_surface(popup->texture));
73 
74  /* Draw the book name. */
75  box.w = BOOK_TITLE_WIDTH;
76  box.h = BOOK_TITLE_HEIGHT;
78 
79  /* Draw the content. */
80  box.w = BOOK_TEXT_WIDTH;
81  box.h = BOOK_TEXT_HEIGHT;
82  box.y = book_scroll;
83  text_color_set(0, 0, 255);
86  text_set_selection(NULL, NULL, NULL);
87 
88  popup->redraw = 0;
89  }
90 
91  return 1;
92 }
93 
96 {
98  surface_show(ScreenSurface, popup->x, popup->y, NULL, TEXTURE_CLIENT("book_border"));
99 
100  return 1;
101 }
102 
105 {
106  size_t len;
107 
108  (void) button;
109 
110  len = utarray_len(book_help_history);
111 
112  if (len >= 2) {
113  size_t pos;
114  char **p;
115 
116  pos = len - 2;
117  p = (char **) utarray_eltptr(book_help_history, pos);
118 
119  if (p) {
120  help_show(*p);
121  utarray_erase(book_help_history, pos, 2);
122  }
123  } else {
124  utarray_clear(book_help_history);
125  help_show("main");
126  }
127 
128  return 1;
129 }
130 
132 static int popup_event_func(popup_struct *popup, SDL_Event *event)
133 {
134  if (scrollbar_event(&scrollbar, event)) {
135  return 1;
136  }
137 
140  tooltip_create(event->motion.x, event->motion.y, FONT_ARIAL11,
141  "Go back");
143  }
144 
145  /* Mouse event and the mouse is inside the book. */
146  if (event->type == SDL_MOUSEBUTTONDOWN && event->motion.x >= popup->x && event->motion.x < popup->x + popup->surface->w && event->motion.y >= popup->y && event->motion.y < popup->y + popup->surface->h) {
147  /* Scroll the book. */
148  if (event->button.button == SDL_BUTTON_WHEELDOWN) {
149  scrollbar_scroll_adjust(&scrollbar, 1);
150  return 1;
151  } else if (event->button.button == SDL_BUTTON_WHEELUP) {
152  scrollbar_scroll_adjust(&scrollbar, -1);
153  return 1;
154  }
155  } else if (event->type == SDL_KEYDOWN) {
156  /* Scrolling. */
157  if (event->key.keysym.sym == SDLK_DOWN) {
158  scrollbar_scroll_adjust(&scrollbar, 1);
159  return 1;
160  } else if (event->key.keysym.sym == SDLK_UP) {
161  scrollbar_scroll_adjust(&scrollbar, -1);
162  return 1;
163  } else if (event->key.keysym.sym == SDLK_PAGEDOWN) {
165  return 1;
166  } else if (event->key.keysym.sym == SDLK_PAGEUP) {
168  return 1;
169  }
170  }
171 
172  return -1;
173 }
174 
177 {
178  (void) popup;
179 
180  if (book_help_history) {
181  utarray_free(book_help_history);
182  book_help_history = NULL;
183  }
184 
186 
187  if (book_content != NULL) {
188  efree(book_content);
189  book_content = NULL;
190  }
191 
192  return 1;
193 }
194 
196 static const char *popup_clipboard_copy_func(popup_struct *popup)
197 {
198  (void) popup;
199  return book_content;
200 }
201 
209 void book_load(const char *data, int len)
210 {
211  SDL_Rect box;
212  int pos;
213 
214  /* Nothing to do. */
215  if (!data || !len) {
216  return;
217  }
218 
219  /* Free old book data and reset the values. */
220  if (book_content != NULL) {
221  efree(book_content);
222  }
223 
224  book_lines = 0;
225  book_scroll_lines = 0;
226  book_scroll = 0;
227 
228  /* Store the data. */
229  book_content = estrdup(data);
230  book_name_change("Book", 4);
231 
232  /* Strip trailing newlines. */
233  for (pos = len - 1; pos >= 0; pos--) {
234  if (book_content[pos] != '\n') {
235  break;
236  }
237 
238  book_content[pos] = '\0';
239  }
240 
241  /* No data... */
242  if (book_content[0] == '\0') {
243  return;
244  }
245 
246  /* Calculate the line numbers. */
247  box.w = BOOK_TEXT_WIDTH;
248  box.h = BOOK_TEXT_HEIGHT;
250  book_lines = box.h;
251  book_scroll_lines = box.y;
252 
253  /* Create the book popup if it doesn't exist yet. */
254  if (!popup_get_head() || popup_get_head()->texture != texture_get(TEXTURE_TYPE_CLIENT, "book")) {
255  popup_struct *popup;
256 
257  popup = popup_create(texture_get(TEXTURE_TYPE_CLIENT, "book"));
258  popup->draw_func = popup_draw_func;
260  popup->event_func = popup_event_func;
263  popup->disable_texture_drawing = 1;
264 
265  popup->button_left.x = 25;
266  popup->button_left.y = 25;
267 
270  popup_button_set_text(&popup->button_left, "<");
271  }
272 
273  popup->button_right.x = 649;
274  popup->button_right.y = 25;
275  }
276 
278  scrollbar.redraw = &popup_get_head()->redraw;
279 
280  popup_get_head()->redraw = 1;
281 }
282 
286 void book_redraw(void)
287 {
288  if (popup_get_head() && popup_get_head()->texture == texture_get(TEXTURE_TYPE_CLIENT, "book")) {
289  popup_get_head()->redraw = 1;
290  }
291 }
292 
296 void book_add_help_history(const char *name)
297 {
300  utarray_new(book_help_history, &ut_str_icd);
301  }
302 
303  utarray_push_back(book_help_history, &name);
304 }
#define BOOK_SCROLLBAR_STARTY
Definition: book.h:68
static char * book_content
Definition: book.c:36
#define COLOR_BLACK
Definition: text.h:323
static int popup_button_event_func(popup_button *button)
Definition: book.c:104
#define TEXT_WORD_WRAP
Definition: text.h:226
static scrollbar_struct scrollbar
Definition: book.c:50
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 book_add_help_history(const char *name)
Definition: book.c:296
void text_set_selection(int64_t *start, int64_t *end, uint8_t *started)
Definition: text.c:425
static int popup_draw_func(popup_struct *popup)
Definition: book.c:67
#define TEXT_LINES_SKIP
Definition: text.h:263
int(* draw_post_func)(struct popup_struct *popup)
Definition: popup.h:137
SDL_Surface * texture_surface(texture_struct *texture)
Definition: texture.c:303
static int popup_destroy_callback(popup_struct *popup)
Definition: book.c:176
static int popup_draw_post_func(popup_struct *popup)
Definition: book.c:95
int(* destroy_callback_func)(struct popup_struct *popup)
Definition: popup.h:159
int y
Definition: popup.h:41
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
button_struct button
Definition: popup.h:47
int x
Definition: popup.h:87
#define COLOR_HGOLD
Definition: text.h:319
void tooltip_create(int mx, int my, font_struct *font, const char *text)
Definition: tooltip.c:60
#define BOOK_TEXT_STARTX
Definition: book.h:38
#define BOOK_TEXT_HEIGHT
Definition: book.h:44
texture_struct * texture_get(texture_type_t type, const char *name)
Definition: texture.c:279
static uint32_t book_scroll_lines
Definition: book.c:42
SDL_Surface * ScreenSurface
Definition: main.c:47
#define TEXT_MARKUP
Definition: text.h:224
static uint32_t book_scroll
Definition: book.c:44
#define TEXT_ALIGN_CENTER
Definition: text.h:230
const char *(* clipboard_copy_func)(struct popup_struct *popup)
Definition: popup.h:168
void scrollbar_show(scrollbar_struct *scrollbar, SDL_Surface *surface, int x, int y)
Definition: scrollbar.c:443
#define BOOK_TITLE_HEIGHT
Definition: book.h:58
#define BOOK_TITLE_STARTX
Definition: book.h:52
void book_load(const char *data, int len)
Definition: book.c:209
void help_show(const char *name)
Definition: help.c:219
#define BOOK_SCROLLBAR_STARTX
Definition: book.h:66
#define BOOK_TEXT_WIDTH
Definition: book.h:42
uint8_t redraw
Definition: popup.h:114
#define BOOK_SCROLLBAR_WIDTH
Definition: book.h:70
static uint8_t book_help_history_enabled
Definition: book.c:48
uint8_t selection_started
Definition: popup.h:111
int(* event_func)(struct popup_button *button)
Definition: popup.h:56
static const char * popup_clipboard_copy_func(popup_struct *popup)
Definition: book.c:196
#define BUTTON_CHECK_TOOLTIP(button)
Definition: button.h:38
void scrollbar_scroll_adjust(scrollbar_struct *scrollbar, int adjust)
Definition: scrollbar.c:427
void scrollbar_create(scrollbar_struct *scrollbar, int w, int h, uint32_t *scroll_offset, uint32_t *num_lines, uint32_t max_lines)
Definition: scrollbar.c:350
void tooltip_enable_delay(uint32_t delay)
Definition: tooltip.c:82
UT_array * book_help_history
Definition: book.c:46
void text_color_set(int r, int g, int b)
Definition: text.c:406
int y
Definition: popup.h:90
uint8_t * redraw
Definition: scrollbar.h:87
popup_button button_left
Definition: popup.h:93
int x
Definition: popup.h:38
static uint32_t book_lines
Definition: book.c:40
void surface_show(SDL_Surface *surface, int x, int y, SDL_Rect *srcrect, SDL_Surface *src)
Definition: sprite.c:761
int scrollbar_event(scrollbar_struct *scrollbar, SDL_Event *event)
Definition: scrollbar.c:529
popup_button button_right
Definition: popup.h:96
#define TEXT_LINES_CALC
Definition: text.h:259
void book_name_change(const char *name, size_t len)
Definition: book.c:59
SDL_Surface * surface
Definition: popup.h:65
int64_t selection_end
Definition: popup.h:108
#define BOOK_SCROLLBAR_HEIGHT
Definition: book.h:72
#define BOOK_TEXT_STARTY
Definition: book.h:40
#define BOOK_TITLE_WIDTH
Definition: book.h:56
static char book_name[HUGE_BUF]
Definition: book.c:38
texture_struct * texture
Definition: popup.h:70
uint8_t disable_texture_drawing
Definition: popup.h:75
static int popup_event_func(popup_struct *popup, SDL_Event *event)
Definition: book.c:132
int64_t selection_start
Definition: popup.h:105
#define BOOK_TITLE_STARTY
Definition: book.h:54
void book_redraw(void)
Definition: book.c:286
#define COLOR_WHITE
Definition: text.h:289