2 // C Implementation: button
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
9 // Copyright: See COPYING file that comes with this distribution
20 void button_load(button_type* pbutton,char* icon_file, char* info, SDLKey shortcut, int x, int y)
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());
32 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
34 texture_load(&pbutton->icon,filename,USE_ALPHA);
42 pbutton->info = (char*) malloc(sizeof(char)*(strlen(info) + 1));
43 strcpy(pbutton->info,info);
46 pbutton->shortcut = shortcut;
50 pbutton->w = pbutton->icon.w;
51 pbutton->h = pbutton->icon.h;
54 pbutton->show_info = NO;
58 void button_change_icon(button_type* pbutton,char* icon_file)
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());
70 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
73 texture_free(&pbutton->icon);
74 texture_load(&pbutton->icon,filename,USE_ALPHA);
77 button_type* button_create(char* icon_file, char* info, SDLKey shortcut, int x, int y)
79 button_type* pnew_button = (button_type*) malloc(sizeof(button_type));
80 button_load(pnew_button,icon_file, info, shortcut, x, y);
84 void button_draw(button_type* pbutton)
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)
90 texture_draw(pbutton->bkgd,pbutton->x,pbutton->y,NO_UPDATE);
92 texture_draw(&pbutton->icon,pbutton->x,pbutton->y,NO_UPDATE);
93 if(pbutton->show_info == YES)
98 if(0 > pbutton->x - (int)strlen(pbutton->info) * white_small_text.w)
99 i = pbutton->w + strlen(pbutton->info) * white_small_text.w;
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);
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);
112 void button_free(button_type* pbutton)
115 texture_free(&pbutton->icon);
118 void button_event(button_type* pbutton, SDL_Event *event)
120 SDLKey key = event->key.keysym.sym;
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)
125 if(event->type == SDL_MOUSEBUTTONDOWN)
127 if(event->button.button == SDL_BUTTON_LEFT)
129 pbutton->state = BUTTON_PRESSED;
133 pbutton->show_info = YES;
136 else if(event->type == SDL_MOUSEBUTTONUP)
138 if(event->button.button == SDL_BUTTON_LEFT && pbutton->state == BUTTON_PRESSED)
140 pbutton->state = BUTTON_CLICKED;
142 else if(event->button.button != SDL_BUTTON_LEFT && pbutton->state != BUTTON_PRESSED)
144 pbutton->show_info = YES;
148 if(pbutton->state != BUTTON_PRESSED && pbutton->state != BUTTON_CLICKED)
150 pbutton->state = BUTTON_HOVER;
153 else if(event->type != SDL_KEYDOWN && event->type != SDL_KEYUP)
156 if(pbutton->show_info)
158 pbutton->show_info = NO;
162 if(event->type == SDL_KEYDOWN)
164 if(key == pbutton->shortcut)
165 pbutton->state = BUTTON_PRESSED;
167 else if(event->type == SDL_KEYUP)
169 if(pbutton->state == BUTTON_PRESSED && key == pbutton->shortcut)
170 pbutton->state = BUTTON_CLICKED;
172 else if(event->type == SDL_MOUSEMOTION)
175 if(pbutton->show_info)
177 pbutton->show_info = NO;
182 int button_get_state(button_type* pbutton)
185 if(pbutton->state == BUTTON_CLICKED)
187 state = pbutton->state;
193 return pbutton->state;
197 void button_panel_init(button_panel_type* pbutton_panel, int x, int y, int w, int h)
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;
208 button_type* button_panel_event(button_panel_type* pbutton_panel, SDL_Event* event)
210 if(pbutton_panel->hidden == NO)
213 for(i = 0; i < pbutton_panel->num_items; ++i)
215 button_event(&pbutton_panel->item[i],event);
216 if(pbutton_panel->item[i].state != -1)
217 return &pbutton_panel->item[i];
227 void button_panel_free(button_panel_type* pbutton_panel)
230 for(i = 0; i < pbutton_panel->num_items; ++i)
232 button_free(&pbutton_panel->item[i]);
234 if(pbutton_panel->num_items)
235 free(pbutton_panel->item);
238 void button_panel_draw(button_panel_type* pbutton_panel)
240 if(pbutton_panel->hidden == NO)
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)
246 button_draw(&pbutton_panel->item[i]);
251 void button_panel_additem(button_panel_type* pbutton_panel, button_type* pbutton, int tag)
253 int max_cols, row, col;
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));
260 /* A button_panel takes control of the buttons it contains and arranges them */
262 max_cols = pbutton_panel->w / 32;
264 row = (pbutton_panel->num_items-1) / max_cols;
265 col = (pbutton_panel->num_items-1) % max_cols;
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;