b52ed4ca844544026e55ae5c92419dc0f1c7a4f9
[supertux.git] / src / button.c
1 //
2 // C Implementation: button
3 //
4 // Description:
5 //
6 //
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12
13 #include <string.h>
14 #include <stdlib.h>
15 #include "setup.h"
16 #include "screen.h"
17 #include "globals.h"
18 #include "button.h"
19
20 void button_load(button_type* pbutton,char* icon_file, char* info, SDLKey shortcut, int x, int y)
21 {
22   char filename[1024];
23
24   if(icon_file != NULL)
25     {
26       snprintf(filename, 1024, "%s/%s", DATA_PREFIX, icon_file);
27       if(!faccessible(filename))
28         snprintf(filename, 1024, "%s/images/icons/default-icon.png", DATA_PREFIX);
29     }
30   else
31     {
32       snprintf(filename, 1024, "%s/images/icons/default-icon.png", DATA_PREFIX);
33     }
34   texture_load(&pbutton->icon,filename,USE_ALPHA);
35
36   if(info == NULL)
37     {
38       pbutton->info = NULL;
39     }
40   else
41     {
42       pbutton->info = (char*) malloc(sizeof(char)*(strlen(info) + 1));
43       strcpy(pbutton->info,info);
44     }
45
46   pbutton->shortcut = shortcut;
47
48   pbutton->x = x;
49   pbutton->y = y;
50   pbutton->w = pbutton->icon.w;
51   pbutton->h = pbutton->icon.h;
52   pbutton->state = -1;
53   pbutton->show_info = NO;
54 }
55
56 button_type* button_create(char* icon_file, char* info, SDLKey shortcut, int x, int y)
57 {
58   button_type* pnew_button = (button_type*) malloc(sizeof(button_type));
59   button_load(pnew_button,icon_file, info, shortcut, x, y);
60   return pnew_button;
61 }
62
63 void button_draw(button_type* pbutton)
64 {
65   fillrect(pbutton->x,pbutton->y,pbutton->w,pbutton->h,75,75,75,200);
66   fillrect(pbutton->x+1,pbutton->y+1,pbutton->w-2,pbutton->h-2,175,175,175,200);
67   texture_draw(&pbutton->icon,pbutton->x,pbutton->y,NO_UPDATE);
68   if(pbutton->show_info == YES)
69     {
70       char str[80];
71       if(pbutton->info)
72         text_draw(&white_small_text, pbutton->info, pbutton->x - strlen(pbutton->info) * white_small_text.w, pbutton->y, 1, NO_UPDATE);
73       sprintf(str,"(%s)", SDL_GetKeyName(pbutton->shortcut));
74       text_draw(&white_small_text, str, pbutton->x - strlen(str) * white_small_text.w, pbutton->y + white_small_text.h+2, 1, NO_UPDATE);
75     }
76 }
77
78 void button_free(button_type* pbutton)
79 {
80   free(pbutton->info);
81   texture_free(&pbutton->icon);
82 }
83
84 void button_event(button_type* pbutton, SDL_Event *event)
85 {
86   if(event->type == SDL_KEYDOWN)
87     {
88       SDLKey key = event->key.keysym.sym;
89       if(key == pbutton->shortcut)
90         pbutton->state = BN_CLICKED;
91     }
92   else if(event->motion.x > pbutton->x && event->motion.x < pbutton->x + pbutton->w &&
93           event->motion.y > pbutton->y && event->motion.y < pbutton->y + pbutton->h)
94     {
95       if(event->type == SDL_MOUSEBUTTONDOWN)
96         {
97           if(event->button.button == SDL_BUTTON_LEFT)
98             {
99               pbutton->state = BN_PRESSED;
100             }
101           else
102             {
103               pbutton->show_info = YES;
104             }
105         }
106       if(event->type == SDL_MOUSEBUTTONUP)
107         {
108           if(event->button.button == SDL_BUTTON_LEFT && pbutton->state == BN_PRESSED)
109             {
110               pbutton->state = BN_CLICKED;
111             }
112           else if(event->button.button != SDL_BUTTON_LEFT && pbutton->state != BN_PRESSED)
113             {
114               pbutton->show_info = YES;
115             }
116         }
117     }
118   else if(event->type == SDL_MOUSEMOTION)
119     {
120
121       if(pbutton->show_info)
122         {
123           pbutton->show_info = NO;
124         }
125     }
126 }
127
128 int button_get_state(button_type* pbutton)
129 {
130   int state;
131   if(pbutton->state == BN_CLICKED)
132     {
133       state = pbutton->state;
134       pbutton->state = -1;
135       return state;
136     }
137   else
138     return pbutton->state;
139 }
140
141 void button_panel_init(button_panel_type* pbutton_panel, int x, int y, int w, int h)
142 {
143   pbutton_panel->num_items = 0;
144   pbutton_panel->item = NULL;
145   pbutton_panel->x = x;
146   pbutton_panel->y = y;
147   pbutton_panel->w = w;
148   pbutton_panel->h = h;
149 }
150
151 void button_panel_free(button_panel_type* pbutton_panel)
152 {
153   int i;
154   for(i = 0; i < pbutton_panel->num_items; ++i)
155     {
156       button_free(&pbutton_panel->item[i]);
157     }
158   if(pbutton_panel->num_items)
159     free(pbutton_panel->item);
160 }
161
162 void button_panel_draw(button_panel_type* pbutton_panel)
163 {
164   int i;
165   for(i = 0; i < pbutton_panel->num_items; ++i)
166     {
167       button_draw(&pbutton_panel->item[i]);
168     }
169 }
170
171 void button_panel_additem(button_panel_type* pbutton_panel, button_type* pbutton)
172 {
173 int max_cols, row, col;
174
175   ++pbutton_panel->num_items;
176   pbutton_panel->item = (button_type*) realloc(pbutton_panel->item, sizeof(button_type) * pbutton_panel->num_items);
177   memcpy(&pbutton_panel->item[pbutton_panel->num_items-1],pbutton,sizeof(button_type));
178   free(pbutton);
179   
180   /* A button_panel takes control of the buttons it contains and arranges them */
181   
182   max_cols = pbutton_panel->w / 32;
183   
184   row = pbutton_panel->num_items / max_cols;
185   col = pbutton_panel->num_items % max_cols;
186   
187   pbutton_panel->item[pbutton_panel->num_items-1].x = pbutton_panel->x + col * 32;
188   pbutton_panel->item[pbutton_panel->num_items-1].y = pbutton_panel->y + row * 32;
189   
190 }
191