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::vector<Menu*> Menu::last_menus;
59 Menu* Menu::current_ = 0;
62 Menu::push_current(Menu* pmenu)
65 last_menus.push_back(current_);
68 current_->effect.start(500);
74 if (!last_menus.empty())
76 current_ = last_menus.back();
77 current_->effect.start(500);
79 last_menus.pop_back();
88 Menu::set_current(Menu* menu)
93 menu->effect.start(500);
98 /* Return a pointer to a new menu item */
100 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int* int_p_)
102 MenuItem *pnew_item = new MenuItem;
104 pnew_item->kind = kind_;
105 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
106 strcpy(pnew_item->text, text_);
108 if(kind_ == MN_TOGGLE)
109 pnew_item->toggled = init_toggle_;
111 pnew_item->toggled = false;
113 pnew_item->target_menu = target_menu_;
114 pnew_item->input = (char*) malloc(sizeof(char));
115 pnew_item->input[0] = '\0';
117 if(kind_ == MN_STRINGSELECT)
119 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
120 string_list_init(pnew_item->list);
123 pnew_item->list = NULL;
125 pnew_item->int_p = int_p_;
131 MenuItem::change_text(const char *text_)
136 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
142 MenuItem::change_input(const char *text_)
147 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
148 strcpy(input, text_);
152 /* Free a menu and all its items */
157 for(unsigned int i = 0; i < item.size(); ++i)
161 string_list_free(item[i].list);
170 menuaction = MENU_ACTION_NONE;
171 delete_character = 0;
172 mn_input_char = '\0';
176 has_backitem = false;
182 void Menu::set_pos(int x, int y, float rw, float rh)
184 pos_x = x + (int)((float)get_width() * rw);
185 pos_y = y + (int)((float)get_height() * rh);
189 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int* int_p)
194 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, int_p));
197 /* Add an item to a menu */
199 Menu::additem(MenuItem* pmenu_item)
201 if(pmenu_item->kind == MN_BACK)
204 item.push_back(*pmenu_item);
214 /* Process actions done on the menu */
227 active_item = int(item.size())-1;
230 case MENU_ACTION_DOWN:
231 if(active_item < int(item.size())-1)
237 case MENU_ACTION_LEFT:
238 if(item[active_item].kind == MN_STRINGSELECT
239 && item[active_item].list->num_items != 0)
241 if(item[active_item].list->active_item > 0)
242 --item[active_item].list->active_item;
244 item[active_item].list->active_item = item[active_item].list->num_items-1;
248 case MENU_ACTION_RIGHT:
249 if(item[active_item].kind == MN_STRINGSELECT
250 && item[active_item].list->num_items != 0)
252 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
253 ++item[active_item].list->active_item;
255 item[active_item].list->active_item = 0;
259 case MENU_ACTION_HIT:
261 hit_item = active_item;
262 switch (item[active_item].kind)
265 if (item[active_item].target_menu != NULL)
266 Menu::push_current(item[active_item].target_menu);
272 item[active_item].toggled = !item[active_item].toggled;
278 Menu::set_current(0);
279 item[active_item].toggled = true;
281 case MN_CONTROLFIELD:
293 case MENU_ACTION_REMOVE:
294 if(item[active_item].kind == MN_TEXTFIELD
295 || item[active_item].kind == MN_NUMFIELD)
297 if(item[active_item].input != NULL)
299 int i = strlen(item[active_item].input);
301 while(delete_character > 0) /* remove charactes */
303 item[active_item].input[i-1] = '\0';
310 case MENU_ACTION_INPUT:
311 if(item[active_item].kind == MN_TEXTFIELD
312 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
314 if(item[active_item].input != NULL)
316 int i = strlen(item[active_item].input);
317 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
318 item[active_item].input[i] = mn_input_char;
319 item[active_item].input[i+1] = '\0';
323 item[active_item].input = (char*) malloc(2*sizeof(char));
324 item[active_item].input[0] = mn_input_char;
325 item[active_item].input[1] = '\0';
329 case MENU_ACTION_NONE:
334 MenuItem& new_item = item[active_item];
335 if(new_item.kind == MN_DEACTIVE
336 || new_item.kind == MN_LABEL
337 || new_item.kind == MN_HL)
339 // Skip the horzontal line item
340 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
341 menuaction = MENU_ACTION_DOWN;
347 menuaction = MENU_ACTION_NONE;
355 if (item.size() != 0)
357 if((item[active_item].kind == MN_ACTION
358 || item[active_item].kind == MN_TEXTFIELD
359 || item[active_item].kind == MN_NUMFIELD)
360 && item[active_item].toggled)
362 item[active_item].toggled = false;
363 Menu::set_current(0);
366 else if(item[active_item].kind == MN_TOGGLE
367 || item[active_item].kind == MN_GOTO)
380 Menu::draw_item(int index, // Position of the current item in the menu
384 const MenuItem& pitem = item[index];
387 int effect_offset = 0;
392 effect_time = effect.get_left() / 4;
394 effect_offset = (index % 2) ? effect_time : -effect_time;
398 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
400 int text_width = strlen(pitem.text) * font_width;
401 int input_width = strlen(pitem.input) * font_width;
402 int list_width = strlen(string_list_active(pitem.list)) * font_width;
403 Text* text_font = white_text;
406 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
408 if(index == active_item)
411 text_font = blue_text;
418 black_text->draw_align(pitem.text,
420 A_HMIDDLE, A_VMIDDLE, 2);
426 int x = pos_x - menu_width/2;
427 int y = y_pos - 12 - effect_offset;
428 /* Draw a horizontal line with a little 3d effect */
439 white_big_text->draw_align(pitem.text,
441 A_HMIDDLE, A_VMIDDLE, 2);
446 case MN_CONTROLFIELD:
448 int input_pos = input_width/2;
449 int text_pos = (text_width + font_width)/2;
451 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
452 input_width + font_width + 2, 20,
454 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
455 input_width + font_width, 18,
458 gold_text->draw_align(pitem.input,
459 x_pos + text_pos, y_pos,
460 A_HMIDDLE, A_VMIDDLE, 2);
462 text_font->draw_align(pitem.text,
463 x_pos - (input_width + font_width)/2, y_pos,
464 A_HMIDDLE, A_VMIDDLE, shadow_size);
467 // case MN_CONTROLFIELD:
469 /* display key */ // FIXME: the key number is not that obvious to the user :P
471 sprintf(str, "%i", *pitem.int_p);
472 input_width = strlen(str) * font_width;
474 int input_pos = input_width/2;
475 int text_pos = (text_width + font_width)/2;
477 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
478 input_width + font_width + 2, 20,
480 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
481 input_width + font_width, 18,
484 gold_text->draw_align(str,
485 x_pos + text_pos, y_pos,
486 A_HMIDDLE, A_VMIDDLE, 2);
488 text_font->draw_align(pitem.text,
489 x_pos - (input_width + font_width)/2, y_pos,
490 A_HMIDDLE, A_VMIDDLE, shadow_size);
493 case MN_STRINGSELECT:
495 int list_pos_2 = list_width + font_width;
496 int list_pos = list_width/2;
497 int text_pos = (text_width + font_width)/2;
500 arrow_left->draw( x_pos - list_pos + text_pos - 17, y_pos - 8);
501 arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
503 /* Draw input background */
504 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
507 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
511 gold_text->draw_align(string_list_active(pitem.list),
512 x_pos + text_pos, y_pos,
513 A_HMIDDLE, A_VMIDDLE,2);
515 text_font->draw_align(pitem.text,
516 x_pos - list_pos_2/2, y_pos,
517 A_HMIDDLE, A_VMIDDLE, shadow_size);
522 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
523 back->draw( x_pos + text_width/2 + font_width, y_pos - 8);
529 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
532 checkbox_checked->draw(
533 x_pos + (text_width+font_width)/2,
537 x_pos + (text_width+font_width)/2,
542 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
546 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
551 int Menu::get_width() const
553 /* The width of the menu has to be more than the width of the text
554 with the most characters */
556 for(unsigned int i = 0; i < item.size(); ++i)
558 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
562 if( item[i].kind == MN_TOGGLE)
567 return (menu_width * 16 + 24);
570 int Menu::get_height() const
572 return item.size() * 24;
575 /* Draw the current menu. */
579 int menu_height = get_height();
580 int menu_width = get_width();
582 /* Draw a transparent background */
583 fillrect(pos_x - menu_width/2,
584 pos_y - 24*item.size()/2 - 10,
585 menu_width,menu_height + 20,
588 for(unsigned int i = 0; i < item.size(); ++i)
590 draw_item(i, menu_width, menu_height);
594 /* Check for menu event */
596 Menu::event(SDL_Event& event)
602 key = event.key.keysym.sym;
605 keymod = SDL_GetModState();
608 /* If the current unicode character is an ASCII character,
610 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
612 ch[0] = event.key.keysym.unicode & 0x7F;
617 /* An International Character. */
620 if(item[active_item].kind == MN_CONTROLFIELD)
622 *item[active_item].int_p = event.key.keysym.sym;
624 strcpy(item[active_item].input, ch);
629 strcpy(item[active_item].input, "Up cursor");
632 strcpy(item[active_item].input, "Down cursor");
635 strcpy(item[active_item].input, "Left cursor");
638 strcpy(item[active_item].input, "Right cursor");
641 strcpy(item[active_item].input, "Return");
644 strcpy(item[active_item].input, "Space");
647 strcpy(item[active_item].input, "Right Shift");
650 strcpy(item[active_item].input, "Left Shift");
653 strcpy(item[active_item].input, "Right Control");
656 strcpy(item[active_item].input, "Left Control");
659 strcpy(item[active_item].input, "Right Alt");
662 strcpy(item[active_item].input, "Left Alt");
665 strcpy(item[active_item].input, "?");
669 menuaction = MENU_ACTION_DOWN;
676 case SDLK_UP: /* Menu Up */
677 menuaction = MENU_ACTION_UP;
679 case SDLK_DOWN: /* Menu Down */
680 menuaction = MENU_ACTION_DOWN;
682 case SDLK_LEFT: /* Menu Up */
683 menuaction = MENU_ACTION_LEFT;
685 case SDLK_RIGHT: /* Menu Down */
686 menuaction = MENU_ACTION_RIGHT;
689 if(item[active_item].kind == MN_TEXTFIELD)
691 menuaction = MENU_ACTION_INPUT;
695 case SDLK_RETURN: /* Menu Hit */
696 menuaction = MENU_ACTION_HIT;
700 menuaction = MENU_ACTION_REMOVE;
707 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
709 menuaction = MENU_ACTION_INPUT;
714 mn_input_char = '\0';
719 case SDL_JOYAXISMOTION:
720 if(event.jaxis.axis == joystick_keymap.y_axis)
722 if (event.jaxis.value > 1024)
723 menuaction = MENU_ACTION_DOWN;
724 else if (event.jaxis.value < -1024)
725 menuaction = MENU_ACTION_UP;
728 case SDL_JOYBUTTONDOWN:
729 menuaction = MENU_ACTION_HIT;
731 case SDL_MOUSEBUTTONDOWN:
734 if(x > pos_x - get_width()/2 &&
735 x < pos_x + get_width()/2 &&
736 y > pos_y - get_height()/2 &&
737 y < pos_y + get_height()/2)
739 menuaction = MENU_ACTION_HIT;
742 case SDL_MOUSEMOTION:
745 if(x > pos_x - get_width()/2 &&
746 x < pos_x + get_width()/2 &&
747 y > pos_y - get_height()/2 &&
748 y < pos_y + get_height()/2)
750 active_item = (y - (pos_y - get_height()/2)) / 24;
751 mouse_cursor->set_state(MC_LINK);
755 mouse_cursor->set_state(MC_NORMAL);