fixes and leveleditor interface improvements.
[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       int i = -32;
72       
73       if(0 > pbutton->x - (int)strlen(pbutton->info) * white_small_text.w)
74           i = pbutton->w + strlen(pbutton->info) * white_small_text.w;
75
76       if(pbutton->info)
77         text_draw(&white_small_text, pbutton->info, i + pbutton->x - strlen(pbutton->info) * white_small_text.w, pbutton->y, 1, NO_UPDATE);
78       sprintf(str,"(%s)", SDL_GetKeyName(pbutton->shortcut));
79       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);
80     }
81   if(pbutton->state == BN_PRESSED)
82     fillrect(pbutton->x,pbutton->y,pbutton->w,pbutton->h,75,75,75,200);
83   else if(pbutton->state == BN_HOVER)
84     fillrect(pbutton->x,pbutton->y,pbutton->w,pbutton->h,150,150,150,128);
85 }
86
87 void button_free(button_type* pbutton)
88 {
89   free(pbutton->info);
90   texture_free(&pbutton->icon);
91 }
92
93 void button_event(button_type* pbutton, SDL_Event *event)
94 {
95   SDLKey key = event->key.keysym.sym;
96
97   if(event->motion.x > pbutton->x && event->motion.x < pbutton->x + pbutton->w &&
98       event->motion.y > pbutton->y && event->motion.y < pbutton->y + pbutton->h)
99     {
100       if(event->type == SDL_MOUSEBUTTONDOWN)
101         {
102           if(event->button.button == SDL_BUTTON_LEFT)
103             {
104               pbutton->state = BN_PRESSED;
105             }
106           else
107             {
108               pbutton->show_info = YES;
109             }
110         }
111       else if(event->type == SDL_MOUSEBUTTONUP)
112         {
113           if(event->button.button == SDL_BUTTON_LEFT && pbutton->state == BN_PRESSED)
114             {
115               pbutton->state = BN_CLICKED;
116             }
117           else if(event->button.button != SDL_BUTTON_LEFT && pbutton->state != BN_PRESSED)
118             {
119               pbutton->show_info = YES;
120             }
121         }
122
123       if(pbutton->state != BN_PRESSED && pbutton->state != BN_CLICKED)
124         {
125           pbutton->state = BN_HOVER;
126         }
127     }
128   else if(event->type != SDL_KEYDOWN && event->type != SDL_KEYUP)
129     {
130       pbutton->state = -1;
131       if(pbutton->show_info)
132         {
133           pbutton->show_info = NO;
134         }
135     }
136
137   if(event->type == SDL_KEYDOWN)
138     {
139       if(key == pbutton->shortcut)
140         pbutton->state = BN_PRESSED;
141     }
142   else if(event->type == SDL_KEYUP)
143     {
144       if(pbutton->state == BN_PRESSED && key == pbutton->shortcut)
145         pbutton->state = BN_CLICKED;
146     }
147   else if(event->type == SDL_MOUSEMOTION)
148     {
149
150       if(pbutton->show_info)
151         {
152           pbutton->show_info = NO;
153         }
154     }
155 }
156
157 int button_get_state(button_type* pbutton)
158 {
159   int state;
160   if(pbutton->state == BN_CLICKED)
161     {
162       state = pbutton->state;
163       pbutton->state = -1;
164       return state;
165     }
166   else
167     return pbutton->state;
168 }
169
170 void button_panel_init(button_panel_type* pbutton_panel, int x, int y, int w, int h)
171 {
172   pbutton_panel->num_items = 0;
173   pbutton_panel->item = NULL;
174   pbutton_panel->x = x;
175   pbutton_panel->y = y;
176   pbutton_panel->w = w;
177   pbutton_panel->h = h;
178 }
179
180 void button_panel_free(button_panel_type* pbutton_panel)
181 {
182   int i;
183   for(i = 0; i < pbutton_panel->num_items; ++i)
184     {
185       button_free(&pbutton_panel->item[i]);
186     }
187   if(pbutton_panel->num_items)
188     free(pbutton_panel->item);
189 }
190
191 void button_panel_draw(button_panel_type* pbutton_panel)
192 {
193   int i;
194   fillrect(pbutton_panel->x,pbutton_panel->y,pbutton_panel->w,pbutton_panel->h,100,100,100,200);
195   for(i = 0; i < pbutton_panel->num_items; ++i)
196     {
197       button_draw(&pbutton_panel->item[i]);
198     }
199 }
200
201 void button_panel_additem(button_panel_type* pbutton_panel, button_type* pbutton)
202 {
203   int max_cols, row, col;
204
205   ++pbutton_panel->num_items;
206   pbutton_panel->item = (button_type*) realloc(pbutton_panel->item, sizeof(button_type) * pbutton_panel->num_items);
207   memcpy(&pbutton_panel->item[pbutton_panel->num_items-1],pbutton,sizeof(button_type));
208   free(pbutton);
209
210   /* A button_panel takes control of the buttons it contains and arranges them */
211
212   max_cols = pbutton_panel->w / 32;
213
214   row = pbutton_panel->num_items / max_cols;
215   col = pbutton_panel->num_items % max_cols;
216
217   pbutton_panel->item[pbutton_panel->num_items-1].x = pbutton_panel->x + col * 32;
218   pbutton_panel->item[pbutton_panel->num_items-1].y = pbutton_panel->y + row * 32;
219
220 }
221