4 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <sys/types.h>
37 #include "leveleditor.h"
39 #include "high_scores.h"
42 Surface* checkbox_checked;
49 Menu* worldmap_menu = 0;
50 Menu* options_menu = 0;
51 Menu* options_controls_menu = 0;
52 Menu* highscore_menu = 0;
53 Menu* load_game_menu = 0;
54 Menu* save_game_menu = 0;
55 Menu* contrib_menu = 0;
56 Menu* contrib_subset_menu = 0;
58 std::stack<Menu*> Menu::last_menus;
59 Menu* Menu::current_ = 0;
62 Menu::push_current(Menu* pmenu)
65 last_menus.push(current_);
73 if (!last_menus.empty())
75 set_current(last_menus.top());
85 Menu::set_current(Menu* menu)
88 menu->effect.start(500);
93 /* Return a pointer to a new menu item */
95 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_)
97 MenuItem *pnew_item = new MenuItem;
99 pnew_item->kind = kind_;
100 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
101 strcpy(pnew_item->text, text_);
103 if(kind_ == MN_TOGGLE)
104 pnew_item->toggled = init_toggle_;
106 pnew_item->toggled = false;
108 pnew_item->target_menu = target_menu_;
109 pnew_item->input = (char*) malloc(sizeof(char));
110 pnew_item->input[0] = '\0';
112 if(kind_ == MN_STRINGSELECT)
114 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
115 string_list_init(pnew_item->list);
118 pnew_item->list = NULL;
123 MenuItem::change_text(const char *text_)
128 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
134 MenuItem::change_input(const char *text_)
139 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
140 strcpy(input, text_);
144 /* Free a menu and all its items */
149 for(unsigned int i = 0; i < item.size(); ++i)
153 string_list_free(item[i].list);
161 menuaction = MENU_ACTION_NONE;
162 delete_character = 0;
163 mn_input_char = '\0';
167 has_backitem = false;
173 void Menu::set_pos(int x, int y, float rw, float rh)
175 pos_x = x + (int)((float)width() * rw);
176 pos_y = y + (int)((float)height() * rh);
180 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_)
185 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_));
188 /* Add an item to a menu */
190 Menu::additem(MenuItem* pmenu_item)
192 if(pmenu_item->kind == MN_BACK)
195 item.push_back(*pmenu_item);
205 /* Process actions done on the menu */
217 active_item = int(item.size())-1;
220 case MENU_ACTION_DOWN:
221 if(active_item < int(item.size())-1)
227 case MENU_ACTION_LEFT:
228 if(item[active_item].kind == MN_STRINGSELECT
229 && item[active_item].list->num_items != 0)
231 if(item[active_item].list->active_item > 0)
232 --item[active_item].list->active_item;
234 item[active_item].list->active_item = item[active_item].list->num_items-1;
238 case MENU_ACTION_RIGHT:
239 if(item[active_item].kind == MN_STRINGSELECT
240 && item[active_item].list->num_items != 0)
242 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
243 ++item[active_item].list->active_item;
245 item[active_item].list->active_item = 0;
249 case MENU_ACTION_HIT:
251 switch (item[active_item].kind)
254 if (item[active_item].target_menu != NULL)
255 Menu::push_current(item[active_item].target_menu);
261 item[active_item].toggled = !item[active_item].toggled;
267 case MN_CONTROLFIELD:
268 item[active_item].toggled = true;
280 case MENU_ACTION_REMOVE:
281 if(item[active_item].kind == MN_TEXTFIELD
282 || item[active_item].kind == MN_NUMFIELD)
284 if(item[active_item].input != NULL)
286 int i = strlen(item[active_item].input);
288 while(delete_character > 0) /* remove charactes */
290 item[active_item].input[i-1] = '\0';
297 case MENU_ACTION_INPUT:
298 if(item[active_item].kind == MN_TEXTFIELD
299 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
301 if(item[active_item].input != NULL)
303 int i = strlen(item[active_item].input);
304 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
305 item[active_item].input[i] = mn_input_char;
306 item[active_item].input[i+1] = '\0';
310 item[active_item].input = (char*) malloc(2*sizeof(char));
311 item[active_item].input[0] = mn_input_char;
312 item[active_item].input[1] = '\0';
317 case MENU_ACTION_NONE:
322 MenuItem& new_item = item[active_item];
323 if(new_item.kind == MN_DEACTIVE
324 || new_item.kind == MN_LABEL
325 || new_item.kind == MN_HL)
327 // Skip the horzontal line item
328 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
329 menuaction = MENU_ACTION_DOWN;
335 menuaction = MENU_ACTION_NONE;
341 if (item.size() != 0)
343 if((item[active_item].kind == MN_ACTION
344 || item[active_item].kind == MN_TEXTFIELD
345 || item[active_item].kind == MN_NUMFIELD)
346 && item[active_item].toggled)
348 item[active_item].toggled = false;
349 Menu::set_current(0);
352 else if(item[active_item].kind == MN_TOGGLE || item[active_item].kind == MN_GOTO)
364 Menu::draw_item(int index, // Position of the current item in the menu
368 const MenuItem& pitem = item[index];
371 int effect_offset = 0;
376 effect_time = effect.get_left() / 4;
378 effect_offset = (index % 2) ? effect_time : -effect_time;
382 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
384 int text_width = strlen(pitem.text) * font_width;
385 int input_width = strlen(pitem.input) * font_width;
386 int list_width = strlen(string_list_active(pitem.list)) * font_width;
387 Text* text_font = white_text;
390 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
392 if(index == active_item)
395 text_font = blue_text;
402 black_text->draw_align(pitem.text,
404 A_HMIDDLE, A_VMIDDLE, 2);
410 int x = pos_x - menu_width/2;
411 int y = y_pos - 12 - effect_offset;
412 /* Draw a horizontal line with a little 3d effect */
423 white_big_text->draw_align(pitem.text,
425 A_HMIDDLE, A_VMIDDLE, 2);
430 case MN_CONTROLFIELD:
432 int input_pos = input_width/2;
433 int text_pos = (text_width + font_width)/2;
435 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
436 input_width + font_width + 2, 20,
438 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
439 input_width + font_width, 18,
442 gold_text->draw_align(pitem.input,
443 x_pos + text_pos, y_pos,
444 A_HMIDDLE, A_VMIDDLE, 2);
446 text_font->draw_align(pitem.text,
447 x_pos - (input_width + font_width)/2, y_pos,
448 A_HMIDDLE, A_VMIDDLE, shadow_size);
451 case MN_STRINGSELECT:
453 int list_pos_2 = list_width + font_width;
454 int list_pos = list_width/2;
455 int text_pos = (text_width + font_width)/2;
458 arrow_left->draw( x_pos - list_pos + text_pos - 17, y_pos - 8);
459 arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
461 /* Draw input background */
462 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
465 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
469 gold_text->draw_align(string_list_active(pitem.list),
470 x_pos + text_pos, y_pos,
471 A_HMIDDLE, A_VMIDDLE,2);
473 text_font->draw_align(pitem.text,
474 x_pos - list_pos_2/2, y_pos,
475 A_HMIDDLE, A_VMIDDLE, shadow_size);
480 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
481 back->draw( x_pos + text_width/2 + font_width, y_pos - 8);
487 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
490 checkbox_checked->draw(
491 x_pos + (text_width+font_width)/2,
495 x_pos + (text_width+font_width)/2,
500 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
504 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
511 /* The width of the menu has to be more than the width of the text
512 with the most characters */
514 for(unsigned int i = 0; i < item.size(); ++i)
516 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
520 if( item[i].kind == MN_TOGGLE)
525 return (menu_width * 16 + 24);
530 return item.size() * 24;
533 /* Draw the current menu. */
537 int menu_height = height();
538 int menu_width = width();
540 /* Draw a transparent background */
541 fillrect(pos_x - menu_width/2,
542 pos_y - 24*item.size()/2 - 10,
543 menu_width,menu_height + 20,
546 for(unsigned int i = 0; i < item.size(); ++i)
548 draw_item(i, menu_width, menu_height);
552 /* Check for menu event */
554 Menu::event(SDL_Event& event)
560 key = event.key.keysym.sym;
563 keymod = SDL_GetModState();
566 /* If the current unicode character is an ASCII character,
568 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
570 ch[0] = event.key.keysym.unicode & 0x7F;
575 /* An International Character. */
580 case SDLK_UP: /* Menu Up */
581 menuaction = MENU_ACTION_UP;
583 case SDLK_DOWN: /* Menu Down */
584 menuaction = MENU_ACTION_DOWN;
586 case SDLK_LEFT: /* Menu Up */
587 menuaction = MENU_ACTION_LEFT;
589 case SDLK_RIGHT: /* Menu Down */
590 menuaction = MENU_ACTION_RIGHT;
593 if(item[active_item].kind == MN_TEXTFIELD)
595 menuaction = MENU_ACTION_INPUT;
599 case SDLK_RETURN: /* Menu Hit */
600 menuaction = MENU_ACTION_HIT;
604 menuaction = MENU_ACTION_REMOVE;
611 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
613 menuaction = MENU_ACTION_INPUT;
618 mn_input_char = '\0';
623 case SDL_JOYAXISMOTION:
624 if(event.jaxis.axis == JOY_Y)
626 if (event.jaxis.value > 1024)
627 menuaction = MENU_ACTION_DOWN;
628 else if (event.jaxis.value < -1024)
629 menuaction = MENU_ACTION_UP;
632 case SDL_JOYBUTTONDOWN:
633 menuaction = MENU_ACTION_HIT;
635 case SDL_MOUSEBUTTONDOWN:
638 if(x > pos_x - width()/2 &&
639 x < pos_x + width()/2 &&
640 y > pos_y - height()/2 &&
641 y < pos_y + height()/2)
643 menuaction = MENU_ACTION_HIT;
646 case SDL_MOUSEMOTION:
649 if(x > pos_x - width()/2 &&
650 x < pos_x + width()/2 &&
651 y > pos_y - height()/2 &&
652 y < pos_y + height()/2)
654 active_item = (y - (pos_y - height()/2)) / 24;
655 mouse_cursor->set_state(MC_LINK);
659 mouse_cursor->set_state(MC_NORMAL);