Atrinik Client 2.5
gui/book.c
Go to the documentation of this file.
00001 /************************************************************************
00002 *            Atrinik, a Multiplayer Online Role Playing Game            *
00003 *                                                                       *
00004 *    Copyright (C) 2009-2011 Alex Tokar and Atrinik Development Team    *
00005 *                                                                       *
00006 * Fork from Daimonin (Massive Multiplayer Online Role Playing Game)     *
00007 * and Crossfire (Multiplayer game for X-windows).                       *
00008 *                                                                       *
00009 * This program is free software; you can redistribute it and/or modify  *
00010 * it under the terms of the GNU General Public License as published by  *
00011 * the Free Software Foundation; either version 2 of the License, or     *
00012 * (at your option) any later version.                                   *
00013 *                                                                       *
00014 * This program is distributed in the hope that it will be useful,       *
00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00017 * GNU General Public License for more details.                          *
00018 *                                                                       *
00019 * You should have received a copy of the GNU General Public License     *
00020 * along with this program; if not, write to the Free Software           *
00021 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             *
00022 *                                                                       *
00023 * The author can be reached at admin@atrinik.org                        *
00024 ************************************************************************/
00025 
00030 #include <global.h>
00031 
00033 static char *book_content = NULL;
00035 static char book_name[HUGE_BUF];
00037 static int book_lines = 0;
00039 static int book_scroll_lines = 0;
00041 static int book_scroll = 0;
00043 static SDL_Surface *surface = NULL;
00045 static uint8 redraw = 1;
00049 static int redraw_scroll = 0;
00050 
00055 void book_name_change(const char *name, size_t len)
00056 {
00057     len = MIN(sizeof(book_name) - 1, len);
00058     strncpy(book_name, name, len);
00059     book_name[len] = '\0';
00060 }
00061 
00066 void book_load(const char *data, int len)
00067 {
00068     SDL_Rect box;
00069     int pos;
00070 
00071     /* Nothing to do. */
00072     if (!data || !len)
00073     {
00074         return;
00075     }
00076 
00077     /* Free old book data and reset the values. */
00078     if (book_content)
00079     {
00080         free(book_content);
00081         book_lines = 0;
00082         book_scroll_lines = 0;
00083         book_scroll = 0;
00084     }
00085 
00086     /* Store the data. */
00087     book_content = strdup(data);
00088     book_name_change("Book", 4);
00089 
00090     /* Strip trailing newlines. */
00091     for (pos = len - 1; pos >= 0; pos--)
00092     {
00093         if (book_content[pos] != '\n')
00094         {
00095             break;
00096         }
00097 
00098         book_content[pos] = '\0';
00099     }
00100 
00101     /* No data... */
00102     if (book_content[0] == '\0')
00103     {
00104         return;
00105     }
00106 
00107     /* Calculate the line numbers. */
00108     box.w = BOOK_CONTENT_WIDTH;
00109     box.h = BOOK_CONTENT_HEIGHT;
00110     string_blt(NULL, FONT_ARIAL11, book_content, 30, 50, COLOR_WHITE, TEXT_WORD_WRAP | TEXT_MARKUP | TEXT_LINES_CALC, &box);
00111     book_lines = box.h;
00112     book_scroll_lines = box.y;
00113     redraw = 1;
00114 
00115     /* The book menu is now ready to be shown. */
00116     cpl.menustatus = MENU_BOOK;
00117 }
00118 
00121 void book_show()
00122 {
00123     SDL_Rect box, box2;
00124     int x, y;
00125 
00126     /* Draw the background. */
00127     x = BOOK_BACKGROUND_X;
00128     y = BOOK_BACKGROUND_Y;
00129 
00130     if (!surface)
00131     {
00132         surface = SDL_ConvertSurface(Bitmaps[BITMAP_BOOK]->bitmap, Bitmaps[BITMAP_BOOK]->bitmap->format, Bitmaps[BITMAP_BOOK]->bitmap->flags);
00133     }
00134 
00135     if (book_scroll != redraw_scroll)
00136     {
00137         redraw = 1;
00138     }
00139 
00140     if (redraw)
00141     {
00142         _BLTFX bltfx;
00143 
00144         bltfx.surface = surface;
00145         bltfx.flags = 0;
00146         bltfx.alpha = 0;
00147         sprite_blt(Bitmaps[BITMAP_BOOK], 0, 0, NULL, &bltfx);
00148 
00149         redraw = 0;
00150         redraw_scroll = book_scroll;
00151 
00152         /* Draw the book name. */
00153         box.w = Bitmaps[BITMAP_BOOK]->bitmap->w - 60;
00154         box.h = 0;
00155         text_offset_set(x, y);
00156         string_blt(surface, FONT_SERIF16, book_name, 30, 30, COLOR_BLACK, TEXT_WORD_WRAP | TEXT_MARKUP | TEXT_ALIGN_CENTER, &box);
00157 
00158         /* Draw the content. */
00159         box.w = BOOK_CONTENT_WIDTH;
00160         box.h = BOOK_CONTENT_HEIGHT;
00161         box.y = book_scroll;
00162         text_color_set(0, 0, 255);
00163         string_blt(surface, FONT_ARIAL11, book_content, 30, 50, COLOR_BLACK, TEXT_WORD_WRAP | TEXT_MARKUP | TEXT_LINES_SKIP, &box);
00164         text_offset_reset();
00165     }
00166 
00167     box2.x = x;
00168     box2.y = y;
00169     SDL_BlitSurface(surface, NULL, ScreenSurface, &box2);
00170 
00171     /* Show scroll buttons. */
00172     box.x = x + Bitmaps[BITMAP_BOOK]->bitmap->w - 50;
00173     box.y = y + Bitmaps[BITMAP_BOOK]->bitmap->h / 2 - 55;
00174     scroll_buttons_show(ScreenSurface, box.x, box.y, &book_scroll, book_lines - book_scroll_lines, book_scroll_lines, &box);
00175 
00176     /* Show close button. */
00177     if (button_show(BITMAP_BUTTON_ROUND, -1, BITMAP_BUTTON_ROUND_DOWN, box.x, y + 30, "X", FONT_ARIAL10, COLOR_WHITE, COLOR_BLACK, COLOR_HGOLD, COLOR_BLACK, 0))
00178     {
00179         cpl.menustatus = MENU_NO;
00180         map_udate_flag = 2;
00181         reset_keys();
00182     }
00183 }
00184 
00188 void book_handle_key(SDLKey key)
00189 {
00190     /* Scrolling. */
00191     if (key == SDLK_DOWN)
00192     {
00193         book_scroll++;
00194     }
00195     else if (key == SDLK_UP)
00196     {
00197         book_scroll--;
00198     }
00199     else if (key == SDLK_PAGEDOWN)
00200     {
00201         book_scroll += book_scroll_lines;
00202     }
00203     else if (key == SDLK_PAGEUP)
00204     {
00205         book_scroll -= book_scroll_lines;
00206     }
00207 
00208     /* Make sure the new scroll value is within range. */
00209     if (book_scroll < 0)
00210     {
00211         book_scroll = 0;
00212     }
00213     else if (book_scroll > book_lines - book_scroll_lines)
00214     {
00215         book_scroll = book_lines - book_scroll_lines;
00216     }
00217 }
00218 
00222 void book_handle_event(SDL_Event *event)
00223 {
00224     /* Mouse event and the mouse is inside the book. */
00225     if (event->type == SDL_MOUSEBUTTONDOWN && event->motion.x > BOOK_BACKGROUND_X && event->motion.x < BOOK_BACKGROUND_X + Bitmaps[BITMAP_BOOK]->bitmap->w && event->motion.y > BOOK_BACKGROUND_Y && event->motion.y < BOOK_BACKGROUND_Y + Bitmaps[BITMAP_BOOK]->bitmap->h)
00226     {
00227         /* Scroll the book. */
00228         if (event->button.button == SDL_BUTTON_WHEELDOWN)
00229         {
00230             book_handle_key(SDLK_DOWN);
00231         }
00232         else if (event->button.button == SDL_BUTTON_WHEELUP)
00233         {
00234             book_handle_key(SDLK_UP);
00235         }
00236 
00237         redraw = 1;
00238     }
00239 }
00240 
00243 void book_redraw()
00244 {
00245     redraw = 1;
00246 }