commited Matzes Win32 patch
[supertux.git] / src / button.cpp
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", datadir.c_str(), icon_file);
27       if(!faccessible(filename))
28         snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
29     }
30   else
31     {
32       snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
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->tag = -1;
53   pbutton->state = -1;
54   pbutton->show_info = NO;
55   pbutton->bkgd = NULL;
56 }
57
58 void button_change_icon(button_type* pbutton,char* icon_file)
59 {
60   char filename[1024];
61
62   if(icon_file != NULL)
63     {
64       snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file);
65       if(!faccessible(filename))
66         snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
67     }
68   else
69     {
70       snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
71     }
72   
73   texture_free(&pbutton->icon);
74   texture_load(&pbutton->icon,filename,USE_ALPHA);
75 }
76
77 button_type* button_create(char* icon_file, char* info, SDLKey shortcut, int x, int y)
78 {
79   button_type* pnew_button = (button_type*) malloc(sizeof(button_type));
80   button_load(pnew_button,icon_file, info, shortcut, x, y);
81   return pnew_button;
82 }
83
84 void button_draw(button_type* pbutton)
85 {
86   fillrect(pbutton->x,pbutton->y,pbutton->w,pbutton->h,75,75,75,200);
87   fillrect(pbutton->x+1,pbutton->y+1,pbutton->w-2,pbutton->h-2,175,175,175,200);
88   if(pbutton->bkgd != NULL)
89   {
90   texture_draw(pbutton->bkgd,pbutton->x,pbutton->y,NO_UPDATE);
91   }
92   texture_draw(&pbutton->icon,pbutton->x,pbutton->y,NO_UPDATE);
93   if(pbutton->show_info == YES)
94     {
95       char str[80];
96       int i = -32;
97
98       if(0 > pbutton->x - (int)strlen(pbutton->info) * white_small_text.w)
99         i = pbutton->w + strlen(pbutton->info) * white_small_text.w;
100
101       if(pbutton->info)
102         text_draw(&white_small_text, pbutton->info, i + pbutton->x - strlen(pbutton->info) * white_small_text.w, pbutton->y, 1, NO_UPDATE);
103       sprintf(str,"(%s)", SDL_GetKeyName(pbutton->shortcut));
104       text_draw(&white_small_text, str, i + pbutton->x - strlen(str) * white_small_text.w, pbutton->y + white_small_text.h+2, 1, NO_UPDATE);
105     }
106   if(pbutton->state == BUTTON_PRESSED)
107     fillrect(pbutton->x,pbutton->y,pbutton->w,pbutton->h,75,75,75,200);
108   else if(pbutton->state == BUTTON_HOVER)
109     fillrect(pbutton->x,pbutton->y,pbutton->w,pbutton->h,150,150,150,128);
110 }
111
112 void button_free(button_type* pbutton)
113 {
114   free(pbutton->info);
115   texture_free(&pbutton->icon);
116 }
117
118 void button_event(button_type* pbutton, SDL_Event *event)
119 {
120   SDLKey key = event->key.keysym.sym;
121
122   if(event->motion.x > pbutton->x && event->motion.x < pbutton->x + pbutton->w &&
123       event->motion.y > pbutton->y && event->motion.y < pbutton->y + pbutton->h)
124     {
125       if(event->type == SDL_MOUSEBUTTONDOWN)
126         {
127           if(event->button.button == SDL_BUTTON_LEFT)
128             {
129               pbutton->state = BUTTON_PRESSED;
130             }
131           else
132             {
133               pbutton->show_info = YES;
134             }
135         }
136       else if(event->type == SDL_MOUSEBUTTONUP)
137         {
138           if(event->button.button == SDL_BUTTON_LEFT && pbutton->state == BUTTON_PRESSED)
139             {
140               pbutton->state = BUTTON_CLICKED;
141             }
142           else if(event->button.button != SDL_BUTTON_LEFT && pbutton->state != BUTTON_PRESSED)
143             {
144               pbutton->show_info = YES;
145             }
146         }
147
148       if(pbutton->state != BUTTON_PRESSED && pbutton->state != BUTTON_CLICKED)
149         {
150           pbutton->state = BUTTON_HOVER;
151         }
152     }
153   else if(event->type != SDL_KEYDOWN && event->type != SDL_KEYUP)
154     {
155       pbutton->state = -1;
156       if(pbutton->show_info)
157         {
158           pbutton->show_info = NO;
159         }
160     }
161
162   if(event->type == SDL_KEYDOWN)
163     {
164       if(key == pbutton->shortcut)
165         pbutton->state = BUTTON_PRESSED;
166     }
167   else if(event->type == SDL_KEYUP)
168     {
169       if(pbutton->state == BUTTON_PRESSED && key == pbutton->shortcut)
170         pbutton->state = BUTTON_CLICKED;
171     }
172   else if(event->type == SDL_MOUSEMOTION)
173     {
174
175       if(pbutton->show_info)
176         {
177           pbutton->show_info = NO;
178         }
179     }
180 }
181
182 int button_get_state(button_type* pbutton)
183 {
184   int state;
185   if(pbutton->state == BUTTON_CLICKED)
186     {
187       state = pbutton->state;
188       pbutton->state = -1;
189       return state;
190     }
191   else
192     {
193       return pbutton->state;
194     }
195 }
196
197 void button_panel_init(button_panel_type* pbutton_panel, int x, int y, int w, int h)
198 {
199   pbutton_panel->num_items = 0;
200   pbutton_panel->item = NULL;
201   pbutton_panel->x = x;
202   pbutton_panel->y = y;
203   pbutton_panel->w = w;
204   pbutton_panel->h = h;
205   pbutton_panel->hidden = NO;
206 }
207
208 button_type* button_panel_event(button_panel_type* pbutton_panel, SDL_Event* event)
209 {
210   if(pbutton_panel->hidden == NO)
211     {
212       int i;
213       for(i = 0; i < pbutton_panel->num_items; ++i)
214         {
215           button_event(&pbutton_panel->item[i],event);
216           if(pbutton_panel->item[i].state != -1)
217             return &pbutton_panel->item[i];
218         }
219       return NULL;
220     }
221   else
222     {
223       return NULL;
224     }
225 }
226
227 void button_panel_free(button_panel_type* pbutton_panel)
228 {
229   int i;
230   for(i = 0; i < pbutton_panel->num_items; ++i)
231     {
232       button_free(&pbutton_panel->item[i]);
233     }
234   if(pbutton_panel->num_items)
235     free(pbutton_panel->item);
236 }
237
238 void button_panel_draw(button_panel_type* pbutton_panel)
239 {
240   if(pbutton_panel->hidden == NO)
241     {
242       int i;
243       fillrect(pbutton_panel->x,pbutton_panel->y,pbutton_panel->w,pbutton_panel->h,100,100,100,200);
244       for(i = 0; i < pbutton_panel->num_items; ++i)
245         {
246           button_draw(&pbutton_panel->item[i]);
247         }
248     }
249 }
250
251 void button_panel_additem(button_panel_type* pbutton_panel, button_type* pbutton, int tag)
252 {
253   int max_cols, row, col;
254
255   ++pbutton_panel->num_items;
256   pbutton_panel->item = (button_type*) realloc(pbutton_panel->item, sizeof(button_type) * pbutton_panel->num_items);
257   memcpy(&pbutton_panel->item[pbutton_panel->num_items-1],pbutton,sizeof(button_type));
258   free(pbutton);
259
260   /* A button_panel takes control of the buttons it contains and arranges them */
261
262   max_cols = pbutton_panel->w / 32;
263
264   row = (pbutton_panel->num_items-1) / max_cols;
265   col = (pbutton_panel->num_items-1) % max_cols;
266
267   pbutton_panel->item[pbutton_panel->num_items-1].x = pbutton_panel->x + col * 32;
268   pbutton_panel->item[pbutton_panel->num_items-1].y = pbutton_panel->y + row * 32;
269   pbutton_panel->item[pbutton_panel->num_items-1].tag = tag;
270
271 }
272