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", DATA_PREFIX, icon_file);
27 if(!faccessible(filename))
28 snprintf(filename, 1024, "%s/images/icons/default-icon.png", DATA_PREFIX);
32 snprintf(filename, 1024, "%s/images/icons/default-icon.png", DATA_PREFIX);
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;
53 pbutton->show_info = NO;
56 button_type* button_create(char* icon_file, char* info, SDLKey shortcut, int x, int y)
58 button_type* pnew_button = (button_type*) malloc(sizeof(button_type));
59 button_load(pnew_button,icon_file, info, shortcut, x, y);
63 void button_draw(button_type* pbutton)
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)
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);
76 if(pbutton->state == BN_PRESSED)
77 fillrect(pbutton->x,pbutton->y,pbutton->w,pbutton->h,75,75,75,200);
80 void button_free(button_type* pbutton)
83 texture_free(&pbutton->icon);
86 void button_event(button_type* pbutton, SDL_Event *event)
88 SDLKey key = event->key.keysym.sym;
90 if(event->motion.x > pbutton->x && event->motion.x < pbutton->x + pbutton->w &&
91 event->motion.y > pbutton->y && event->motion.y < pbutton->y + pbutton->h)
93 if(event->type == SDL_MOUSEBUTTONDOWN)
95 if(event->button.button == SDL_BUTTON_LEFT)
97 pbutton->state = BN_PRESSED;
101 pbutton->show_info = YES;
104 if(event->type == SDL_MOUSEBUTTONUP)
106 if(event->button.button == SDL_BUTTON_LEFT && pbutton->state == BN_PRESSED)
108 pbutton->state = BN_CLICKED;
110 else if(event->button.button != SDL_BUTTON_LEFT && pbutton->state != BN_PRESSED)
112 pbutton->show_info = YES;
116 else if(event->type == SDL_MOUSEBUTTONDOWN || event->type == SDL_MOUSEBUTTONUP)
119 if(pbutton->show_info)
121 pbutton->show_info = NO;
125 if(event->type == SDL_KEYDOWN)
127 if(key == pbutton->shortcut)
128 pbutton->state = BN_PRESSED;
130 else if(event->type == SDL_KEYUP)
132 if(pbutton->state == BN_PRESSED && key == pbutton->shortcut)
133 pbutton->state = BN_CLICKED;
135 else if(event->type == SDL_MOUSEMOTION)
138 if(pbutton->show_info)
140 pbutton->show_info = NO;
145 int button_get_state(button_type* pbutton)
148 if(pbutton->state == BN_CLICKED)
150 state = pbutton->state;
155 return pbutton->state;
158 void button_panel_init(button_panel_type* pbutton_panel, int x, int y, int w, int h)
160 pbutton_panel->num_items = 0;
161 pbutton_panel->item = NULL;
162 pbutton_panel->x = x;
163 pbutton_panel->y = y;
164 pbutton_panel->w = w;
165 pbutton_panel->h = h;
168 void button_panel_free(button_panel_type* pbutton_panel)
171 for(i = 0; i < pbutton_panel->num_items; ++i)
173 button_free(&pbutton_panel->item[i]);
175 if(pbutton_panel->num_items)
176 free(pbutton_panel->item);
179 void button_panel_draw(button_panel_type* pbutton_panel)
182 for(i = 0; i < pbutton_panel->num_items; ++i)
184 button_draw(&pbutton_panel->item[i]);
188 void button_panel_additem(button_panel_type* pbutton_panel, button_type* pbutton)
190 int max_cols, row, col;
192 ++pbutton_panel->num_items;
193 pbutton_panel->item = (button_type*) realloc(pbutton_panel->item, sizeof(button_type) * pbutton_panel->num_items);
194 memcpy(&pbutton_panel->item[pbutton_panel->num_items-1],pbutton,sizeof(button_type));
197 /* A button_panel takes control of the buttons it contains and arranges them */
199 max_cols = pbutton_panel->w / 32;
201 row = pbutton_panel->num_items / max_cols;
202 col = pbutton_panel->num_items % max_cols;
204 pbutton_panel->item[pbutton_panel->num_items-1].x = pbutton_panel->x + col * 32;
205 pbutton_panel->item[pbutton_panel->num_items-1].y = pbutton_panel->y + row * 32;