Atrinik Client 2.5
toolkit/scroll_buttons.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 
00044 #include <global.h>
00045 
00056 static void scroll_buttons_show_one(SDL_Surface *surface, int x, int y, int *pos, int max_pos, int advance, SDL_Rect *box)
00057 {
00058     int state, mx, my;
00059 
00060     /* Draw the button's borders. */
00061     draw_frame(surface, box->x - 1, box->y - 1, box->w + 1, box->h + 1);
00062 
00063     state = SDL_GetMouseState(&mx, &my);
00064 
00065     /* Visual feedback so the user knows whether there is anything more
00066      * further up/down so they don't have to bother clicking the buttons
00067      * to find out. */
00068     if (*pos + MAX(-1, MIN(1, advance)) < 0 || *pos + MAX(-1, MIN(1, advance)) > max_pos)
00069     {
00070         SDL_FillRect(surface, box, SDL_MapRGB(surface->format, 123, 115, 115));
00071     }
00072     /* Mouse over the button? */
00073     else if (mx > x && mx < x + box->w && my > y && my < y + box->h)
00074     {
00075         /* Clicked? */
00076         if (state == SDL_BUTTON(SDL_BUTTON_LEFT))
00077         {
00078             /* Advance the position pointer. */
00079             *pos += advance;
00080             SDL_FillRect(surface, box, SDL_MapRGB(surface->format, 119, 106, 77));
00081         }
00082         else
00083         {
00084             SDL_FillRect(surface, box, SDL_MapRGB(surface->format, 147, 133, 103));
00085         }
00086     }
00087     else
00088     {
00089         SDL_FillRect(surface, box, SDL_MapRGB(surface->format, 181, 165, 132));
00090     }
00091 }
00092 
00103 void scroll_buttons_show(SDL_Surface *surface, int x, int y, int *pos, int max_pos, int advance, SDL_Rect *box)
00104 {
00105     _BLTFX bltfx;
00106 
00107     bltfx.surface = surface;
00108     bltfx.flags = 0;
00109     bltfx.alpha = 0;
00110 
00111     box->h = 20;
00112     box->w = 20;
00113 
00114     /* Show each of the buttons, color them depending on the mouse's
00115      * status, and show the arrow's bitmap. */
00116     scroll_buttons_show_one(surface, x, y, pos, max_pos, -advance, box);
00117     sprite_blt(Bitmaps[BITMAP_ARROW_UP2], box->x + box->w / 2 - Bitmaps[BITMAP_ARROW_UP2]->bitmap->w / 2, box->y + box->h / 2 - Bitmaps[BITMAP_ARROW_UP2]->bitmap->h / 2, NULL, &bltfx);
00118     y += 30;
00119     box->y += 30;
00120     scroll_buttons_show_one(surface, x, y, pos, max_pos, -1, box);
00121     sprite_blt(Bitmaps[BITMAP_ARROW_UP], box->x + box->w / 2 - Bitmaps[BITMAP_ARROW_UP]->bitmap->w / 2, box->y + box->h / 2 - Bitmaps[BITMAP_ARROW_UP]->bitmap->h / 2, NULL, &bltfx);
00122     y += 30;
00123     box->y += 30;
00124     scroll_buttons_show_one(surface, x, y, pos, max_pos, 1, box);
00125     sprite_blt(Bitmaps[BITMAP_ARROW_DOWN], box->x + box->w / 2 - Bitmaps[BITMAP_ARROW_DOWN]->bitmap->w / 2, box->y + box->h / 2 - Bitmaps[BITMAP_ARROW_DOWN]->bitmap->h / 2, NULL, &bltfx);
00126     y += 30;
00127     box->y += 30;
00128     scroll_buttons_show_one(surface, x, y, pos, max_pos, advance, box);
00129     sprite_blt(Bitmaps[BITMAP_ARROW_DOWN2], box->x + box->w / 2 - Bitmaps[BITMAP_ARROW_DOWN2]->bitmap->w / 2, box->y + box->h / 2 - Bitmaps[BITMAP_ARROW_DOWN2]->bitmap->h / 2, NULL, &bltfx);
00130 
00131     /* Check out of bounds values. */
00132     if (*pos > max_pos)
00133     {
00134         *pos = max_pos;
00135     }
00136 
00137     if (*pos < 0)
00138     {
00139         *pos = 0;
00140     }
00141 }