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>
39 #include "leveleditor.h"
41 #include "high_scores.h"
43 #define FLICK_CURSOR_TIME 500
46 Surface* checkbox_checked;
53 Menu* worldmap_menu = 0;
54 Menu* options_menu = 0;
55 Menu* options_keys_menu = 0;
56 Menu* options_joystick_menu = 0;
57 Menu* highscore_menu = 0;
58 Menu* load_game_menu = 0;
59 Menu* save_game_menu = 0;
60 Menu* contrib_menu = 0;
61 Menu* contrib_subset_menu = 0;
63 std::vector<Menu*> Menu::last_menus;
64 Menu* Menu::current_ = 0;
66 /* just displays a Yes/No text that can be used to confirm stuff */
67 bool confirm_dialog(std::string text)
69 Surface* cap_screen = Surface::CaptureScreen();
71 Menu* dialog = new Menu;
72 dialog->additem(MN_DEACTIVE, text,0,0);
73 dialog->additem(MN_HL,"",0,0);
74 dialog->additem(MN_ACTION,"Yes",0,0,true);
75 dialog->additem(MN_ACTION,"No",0,0,false);
76 dialog->additem(MN_HL,"",0,0);
78 Menu::set_current(dialog);
84 while (SDL_PollEvent(&event))
89 cap_screen->draw(0,0);
94 switch (dialog->check())
104 Menu::set_current(0);
112 mouse_cursor->draw();
121 Menu::push_current(Menu* pmenu)
124 last_menus.push_back(current_);
127 current_->effect.start(500);
133 if (!last_menus.empty())
135 current_ = last_menus.back();
136 current_->effect.start(500);
138 last_menus.pop_back();
147 Menu::set_current(Menu* menu)
152 menu->effect.start(500);
157 /* Return a pointer to a new menu item */
159 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_)
161 MenuItem *pnew_item = new MenuItem;
163 pnew_item->kind = kind_;
164 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
165 strcpy(pnew_item->text, text_);
167 if(kind_ == MN_TOGGLE)
168 pnew_item->toggled = init_toggle_;
170 pnew_item->toggled = false;
172 pnew_item->target_menu = target_menu_;
173 pnew_item->input = (char*) malloc(sizeof(char));
174 pnew_item->input[0] = '\0';
176 if(kind_ == MN_STRINGSELECT)
178 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
179 string_list_init(pnew_item->list);
182 pnew_item->list = NULL;
185 pnew_item->int_p = int_p_;
187 pnew_item->input_flickering = false;
188 pnew_item->input_flickering_timer.init(true);
189 pnew_item->input_flickering_timer.start(FLICK_CURSOR_TIME);
195 MenuItem::change_text(const char *text_)
200 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
206 MenuItem::change_input(const char *text_)
211 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
212 strcpy(input, text_);
216 std::string MenuItem::get_input_with_symbol(bool active_item)
219 input_flickering = true;
222 if(input_flickering_timer.get_left() < 0)
225 input_flickering = false;
227 input_flickering = true;
228 input_flickering_timer.start(FLICK_CURSOR_TIME);
234 sprintf(str,"%s_",input);
236 sprintf(str,"%s ",input);
238 std::string string = str;
243 /* Set ControlField for keyboard key */
244 void Menu::get_controlfield_key_into_input(MenuItem *item)
249 item->change_input("Up cursor");
252 item->change_input("Down cursor");
255 item->change_input("Left cursor");
258 item->change_input("Right cursor");
261 item->change_input("Return");
264 item->change_input("Space");
267 item->change_input("Right Shift");
270 item->change_input("Left Shift");
273 item->change_input("Right Control");
276 item->change_input("Left Control");
279 item->change_input("Right Alt");
282 item->change_input("Left Alt");
287 snprintf(tmp, 64, "%d", *item->int_p);
288 item->change_input(tmp);
294 /* Set ControlField for joystick button */
295 void Menu::get_controlfield_js_into_input(MenuItem *item)
297 std::ostringstream oss;
298 oss << "Button " << *item->int_p;
299 item->change_input(oss.str().c_str());
302 /* Free a menu and all its items */
307 for(unsigned int i = 0; i < item.size(); ++i)
311 string_list_free(item[i].list);
320 menuaction = MENU_ACTION_NONE;
321 delete_character = 0;
322 mn_input_char = '\0';
331 void Menu::set_pos(int x, int y, float rw, float rh)
333 pos_x = x + (int)((float)get_width() * rw);
334 pos_y = y + (int)((float)get_height() * rh);
338 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
340 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
343 /* Add an item to a menu */
345 Menu::additem(MenuItem* pmenu_item)
347 item.push_back(*pmenu_item);
357 /* Process actions done on the menu */
370 active_item = int(item.size())-1;
373 case MENU_ACTION_DOWN:
374 if(active_item < int(item.size())-1)
380 case MENU_ACTION_LEFT:
381 if(item[active_item].kind == MN_STRINGSELECT
382 && item[active_item].list->num_items != 0)
384 if(item[active_item].list->active_item > 0)
385 --item[active_item].list->active_item;
387 item[active_item].list->active_item = item[active_item].list->num_items-1;
391 case MENU_ACTION_RIGHT:
392 if(item[active_item].kind == MN_STRINGSELECT
393 && item[active_item].list->num_items != 0)
395 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
396 ++item[active_item].list->active_item;
398 item[active_item].list->active_item = 0;
402 case MENU_ACTION_HIT:
404 hit_item = active_item;
405 switch (item[active_item].kind)
408 if (item[active_item].target_menu != NULL)
409 Menu::push_current(item[active_item].target_menu);
415 item[active_item].toggled = !item[active_item].toggled;
419 Menu::set_current(0);
420 item[active_item].toggled = true;
424 menuaction = MENU_ACTION_DOWN;
437 case MENU_ACTION_REMOVE:
438 if(item[active_item].kind == MN_TEXTFIELD
439 || item[active_item].kind == MN_NUMFIELD)
441 if(item[active_item].input != NULL)
443 int i = strlen(item[active_item].input);
445 while(delete_character > 0) /* remove charactes */
447 item[active_item].input[i-1] = '\0';
454 case MENU_ACTION_INPUT:
455 if(item[active_item].kind == MN_TEXTFIELD
456 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
458 if(item[active_item].input != NULL)
460 int i = strlen(item[active_item].input);
461 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
462 item[active_item].input[i] = mn_input_char;
463 item[active_item].input[i+1] = '\0';
467 item[active_item].input = (char*) malloc(2*sizeof(char));
468 item[active_item].input[0] = mn_input_char;
469 item[active_item].input[1] = '\0';
473 case MENU_ACTION_NONE:
478 MenuItem& new_item = item[active_item];
479 if(new_item.kind == MN_DEACTIVE
480 || new_item.kind == MN_LABEL
481 || new_item.kind == MN_HL)
483 // Skip the horzontal line item
484 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
485 menuaction = MENU_ACTION_DOWN;
491 menuaction = MENU_ACTION_NONE;
493 if (active_item >= int(item.size()))
494 active_item = int(item.size()) - 1;
501 return item[hit_item].id;
507 Menu::draw_item(int index, // Position of the current item in the menu
511 MenuItem& pitem = item[index];
514 int effect_offset = 0;
519 effect_time = effect.get_left() / 4;
521 effect_offset = (index % 2) ? effect_time : -effect_time;
525 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
527 int text_width = strlen(pitem.text) * font_width;
528 int input_width = (strlen(pitem.input)+ 1) * font_width;
529 int list_width = strlen(string_list_active(pitem.list)) * font_width;
530 Text* text_font = white_text;
533 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
535 if(index == active_item)
538 text_font = blue_text;
545 black_text->draw_align(pitem.text,
547 A_HMIDDLE, A_VMIDDLE, 2);
553 int x = pos_x - menu_width/2;
554 int y = y_pos - 12 - effect_offset;
555 /* Draw a horizontal line with a little 3d effect */
566 white_big_text->draw_align(pitem.text,
568 A_HMIDDLE, A_VMIDDLE, 2);
573 case MN_CONTROLFIELD_KB:
574 case MN_CONTROLFIELD_JS:
576 int input_pos = input_width/2;
577 int text_pos = (text_width + font_width)/2;
579 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
580 input_width + font_width + 2, 20,
582 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
583 input_width + font_width, 18,
586 if(pitem.kind == MN_CONTROLFIELD_KB)
587 get_controlfield_key_into_input(&pitem);
588 else if (pitem.kind == MN_CONTROLFIELD_JS)
589 get_controlfield_js_into_input(&pitem);
591 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
593 if(active_item == index)
594 gold_text->draw_align((pitem.get_input_with_symbol(true)).c_str(), x_pos + text_pos, y_pos, A_HMIDDLE, A_VMIDDLE, 2);
596 gold_text->draw_align((pitem.get_input_with_symbol(false)).c_str(), x_pos + text_pos, y_pos, A_HMIDDLE, A_VMIDDLE, 2);
599 gold_text->draw_align(pitem.input,
600 x_pos + text_pos, y_pos,
601 A_HMIDDLE, A_VMIDDLE, 2);
603 text_font->draw_align(pitem.text,
604 x_pos - (input_width + font_width)/2, y_pos,
605 A_HMIDDLE, A_VMIDDLE, shadow_size);
608 case MN_STRINGSELECT:
610 int list_pos_2 = list_width + font_width;
611 int list_pos = list_width/2;
612 int text_pos = (text_width + font_width)/2;
615 arrow_left->draw( x_pos - list_pos + text_pos - 17, y_pos - 8);
616 arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
618 /* Draw input background */
619 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
622 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
626 gold_text->draw_align(string_list_active(pitem.list),
627 x_pos + text_pos, y_pos,
628 A_HMIDDLE, A_VMIDDLE,2);
630 text_font->draw_align(pitem.text,
631 x_pos - list_pos_2/2, y_pos,
632 A_HMIDDLE, A_VMIDDLE, shadow_size);
637 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
638 back->draw( x_pos + text_width/2 + font_width, y_pos - 8);
644 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
647 checkbox_checked->draw(
648 x_pos + (text_width+font_width)/2,
652 x_pos + (text_width+font_width)/2,
657 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
661 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
666 int Menu::get_width() const
668 /* The width of the menu has to be more than the width of the text
669 with the most characters */
671 for(unsigned int i = 0; i < item.size(); ++i)
673 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
677 if( item[i].kind == MN_TOGGLE)
682 return (menu_width * 16 + 24);
685 int Menu::get_height() const
687 return item.size() * 24;
690 /* Draw the current menu. */
694 int menu_height = get_height();
695 int menu_width = get_width();
697 /* Draw a transparent background */
698 fillrect(pos_x - menu_width/2,
699 pos_y - 24*item.size()/2 - 10,
700 menu_width,menu_height + 20,
703 for(unsigned int i = 0; i < item.size(); ++i)
705 draw_item(i, menu_width, menu_height);
710 Menu::get_item_by_id(int id)
712 for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i)
719 static MenuItem dummyitem;
723 int Menu::get_active_item_id()
725 return item[active_item].id;
729 Menu::isToggled(int id)
731 return get_item_by_id(id).toggled;
734 /* Check for menu event */
736 Menu::event(SDL_Event& event)
742 key = event.key.keysym.sym;
745 keymod = SDL_GetModState();
748 /* If the current unicode character is an ASCII character,
750 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
752 ch[0] = event.key.keysym.unicode & 0x7F;
757 /* An International Character. */
760 if(item[active_item].kind == MN_CONTROLFIELD_KB)
762 if(key == SDLK_ESCAPE)
767 *item[active_item].int_p = key;
768 menuaction = MENU_ACTION_DOWN;
775 case SDLK_UP: /* Menu Up */
776 menuaction = MENU_ACTION_UP;
778 case SDLK_DOWN: /* Menu Down */
779 menuaction = MENU_ACTION_DOWN;
781 case SDLK_LEFT: /* Menu Up */
782 menuaction = MENU_ACTION_LEFT;
784 case SDLK_RIGHT: /* Menu Down */
785 menuaction = MENU_ACTION_RIGHT;
788 if(item[active_item].kind == MN_TEXTFIELD)
790 menuaction = MENU_ACTION_INPUT;
794 case SDLK_RETURN: /* Menu Hit */
795 menuaction = MENU_ACTION_HIT;
799 menuaction = MENU_ACTION_REMOVE;
806 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
808 menuaction = MENU_ACTION_INPUT;
813 mn_input_char = '\0';
818 case SDL_JOYAXISMOTION:
819 if(event.jaxis.axis == joystick_keymap.y_axis)
821 if (event.jaxis.value > 1024)
822 menuaction = MENU_ACTION_DOWN;
823 else if (event.jaxis.value < -1024)
824 menuaction = MENU_ACTION_UP;
827 case SDL_JOYBUTTONDOWN:
828 if (item[active_item].kind == MN_CONTROLFIELD_JS)
830 *item[active_item].int_p = key;
831 menuaction = MENU_ACTION_DOWN;
833 menuaction = MENU_ACTION_HIT;
835 case SDL_MOUSEBUTTONDOWN:
838 if(x > pos_x - get_width()/2 &&
839 x < pos_x + get_width()/2 &&
840 y > pos_y - get_height()/2 &&
841 y < pos_y + get_height()/2)
843 menuaction = MENU_ACTION_HIT;
846 case SDL_MOUSEMOTION:
849 if(x > pos_x - get_width()/2 &&
850 x < pos_x + get_width()/2 &&
851 y > pos_y - get_height()/2 &&
852 y < pos_y + get_height()/2)
854 active_item = (y - (pos_y - get_height()/2)) / 24;
855 mouse_cursor->set_state(MC_LINK);
859 mouse_cursor->set_state(MC_NORMAL);