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>
35 #include "screen/screen.h"
36 #include "screen/drawing_context.h"
40 #include "leveleditor.h"
42 #include "high_scores.h"
45 #define FLICK_CURSOR_TIME 500
48 Surface* checkbox_checked;
55 Menu* worldmap_menu = 0;
56 Menu* options_menu = 0;
57 Menu* options_keys_menu = 0;
58 Menu* options_joystick_menu = 0;
59 Menu* highscore_menu = 0;
60 Menu* load_game_menu = 0;
61 Menu* save_game_menu = 0;
62 Menu* contrib_menu = 0;
63 Menu* contrib_subset_menu = 0;
65 std::vector<Menu*> Menu::last_menus;
66 Menu* Menu::current_ = 0;
68 /* just displays a Yes/No text that can be used to confirm stuff */
69 bool confirm_dialog(Surface *background, std::string text)
71 //Surface* cap_screen = Surface::CaptureScreen();
73 Menu* dialog = new Menu;
74 dialog->additem(MN_DEACTIVE, text,0,0);
75 dialog->additem(MN_HL,"",0,0);
76 dialog->additem(MN_ACTION,_("Yes"),0,0,true);
77 dialog->additem(MN_ACTION,_("No"),0,0,false);
78 dialog->additem(MN_HL,"",0,0);
80 Menu::set_current(dialog);
82 DrawingContext context;
88 while (SDL_PollEvent(&event))
93 if(background == NULL)
94 context.draw_gradient(Color(200, 100, 200), Color(100, 200, 100), LAYER_BACKGROUND0);
96 context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
98 dialog->draw(context);
101 switch (dialog->check())
105 Menu::set_current(0);
111 Menu::set_current(0);
119 mouse_cursor->draw(context);
120 context.do_drawing();
128 Menu::push_current(Menu* pmenu)
131 last_menus.push_back(current_);
134 current_->effect.start(500);
140 if (!last_menus.empty())
142 current_ = last_menus.back();
143 current_->effect.start(500);
145 last_menus.pop_back();
154 Menu::set_current(Menu* menu)
159 menu->effect.start(500);
164 /* Return a pointer to a new menu item */
166 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_)
168 MenuItem *pnew_item = new MenuItem;
170 pnew_item->kind = kind_;
171 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
172 strcpy(pnew_item->text, text_);
174 if(kind_ == MN_TOGGLE)
175 pnew_item->toggled = init_toggle_;
177 pnew_item->toggled = false;
179 pnew_item->target_menu = target_menu_;
180 pnew_item->input = (char*) malloc(sizeof(char));
181 pnew_item->input[0] = '\0';
183 if(kind_ == MN_STRINGSELECT)
185 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
186 string_list_init(pnew_item->list);
189 pnew_item->list = NULL;
192 pnew_item->int_p = int_p_;
194 pnew_item->input_flickering = false;
195 pnew_item->input_flickering_timer.init(true);
196 pnew_item->input_flickering_timer.start(FLICK_CURSOR_TIME);
202 MenuItem::change_text(const char *text_)
207 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
213 MenuItem::change_input(const char *text_)
218 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
219 strcpy(input, text_);
223 std::string MenuItem::get_input_with_symbol(bool active_item)
226 input_flickering = true;
229 if(input_flickering_timer.get_left() < 0)
232 input_flickering = false;
234 input_flickering = true;
235 input_flickering_timer.start(FLICK_CURSOR_TIME);
241 sprintf(str,"%s_",input);
243 sprintf(str,"%s ",input);
245 std::string string = str;
250 /* Set ControlField for keyboard key */
251 void Menu::get_controlfield_key_into_input(MenuItem *item)
256 item->change_input(_("Up cursor"));
259 item->change_input(_("Down cursor"));
262 item->change_input(_("Left cursor"));
265 item->change_input(_("Right cursor"));
268 item->change_input(_("Return"));
271 item->change_input(_("Space"));
274 item->change_input(_("Right Shift"));
277 item->change_input(_("Left Shift"));
280 item->change_input(_("Right Control"));
283 item->change_input(_("Left Control"));
286 item->change_input(_("Right Alt"));
289 item->change_input(_("Left Alt"));
294 snprintf(tmp, 64, "%d", *item->int_p);
295 item->change_input(tmp);
301 /* Set ControlField for joystick button */
302 void Menu::get_controlfield_js_into_input(MenuItem *item)
304 std::ostringstream oss;
305 oss << "Button " << *item->int_p;
306 item->change_input(oss.str().c_str());
309 /* Free a menu and all its items */
314 for(unsigned int i = 0; i < item.size(); ++i)
318 string_list_free(item[i].list);
327 menuaction = MENU_ACTION_NONE;
328 delete_character = 0;
329 mn_input_char = '\0';
337 joystick_timer.init(true);
340 void Menu::set_pos(int x, int y, float rw, float rh)
342 pos_x = x + (int)((float)get_width() * rw);
343 pos_y = y + (int)((float)get_height() * rh);
347 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
349 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
352 /* Add an item to a menu */
354 Menu::additem(MenuItem* pmenu_item)
356 item.push_back(*pmenu_item);
366 /* Process actions done on the menu */
379 active_item = int(item.size())-1;
382 case MENU_ACTION_DOWN:
383 if(active_item < int(item.size())-1)
389 case MENU_ACTION_LEFT:
390 if(item[active_item].kind == MN_STRINGSELECT
391 && item[active_item].list->num_items != 0)
393 if(item[active_item].list->active_item > 0)
394 --item[active_item].list->active_item;
396 item[active_item].list->active_item = item[active_item].list->num_items-1;
400 case MENU_ACTION_RIGHT:
401 if(item[active_item].kind == MN_STRINGSELECT
402 && item[active_item].list->num_items != 0)
404 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
405 ++item[active_item].list->active_item;
407 item[active_item].list->active_item = 0;
411 case MENU_ACTION_HIT:
413 hit_item = active_item;
414 switch (item[active_item].kind)
417 if (item[active_item].target_menu != NULL)
418 Menu::push_current(item[active_item].target_menu);
424 item[active_item].toggled = !item[active_item].toggled;
428 Menu::set_current(0);
429 item[active_item].toggled = true;
433 menuaction = MENU_ACTION_DOWN;
446 case MENU_ACTION_REMOVE:
447 if(item[active_item].kind == MN_TEXTFIELD
448 || item[active_item].kind == MN_NUMFIELD)
450 if(item[active_item].input != NULL)
452 int i = strlen(item[active_item].input);
454 while(delete_character > 0) /* remove charactes */
456 item[active_item].input[i-1] = '\0';
463 case MENU_ACTION_INPUT:
464 if(item[active_item].kind == MN_TEXTFIELD
465 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
467 if(item[active_item].input != NULL)
469 int i = strlen(item[active_item].input);
470 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
471 item[active_item].input[i] = mn_input_char;
472 item[active_item].input[i+1] = '\0';
476 item[active_item].input = (char*) malloc(2*sizeof(char));
477 item[active_item].input[0] = mn_input_char;
478 item[active_item].input[1] = '\0';
482 case MENU_ACTION_NONE:
487 MenuItem& new_item = item[active_item];
488 if(new_item.kind == MN_DEACTIVE
489 || new_item.kind == MN_LABEL
490 || new_item.kind == MN_HL)
492 // Skip the horzontal line item
493 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
494 menuaction = MENU_ACTION_DOWN;
500 menuaction = MENU_ACTION_NONE;
502 if (active_item >= int(item.size()))
503 active_item = int(item.size()) - 1;
510 return item[hit_item].id;
516 Menu::draw_item(DrawingContext& context,
517 int index, // Position of the current item in the menu
518 int menu_width, int menu_height)
520 MenuItem& pitem = item[index];
522 int effect_offset = 0;
527 effect_time = effect.get_left() / 4;
529 effect_offset = (index % 2) ? effect_time : -effect_time;
532 Font* text_font = white_text;
534 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
536 int text_width = int(text_font->get_text_width(pitem.text));
537 int input_width = int(text_font->get_text_width(pitem.input) + 10);
539 int(text_font->get_text_width(string_list_active(pitem.list)));
542 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
544 if(index == active_item)
547 text_font = blue_text;
554 context.draw_text_center(blue_text, pitem.text,
555 Vector(0, y_pos - int(blue_text->get_height()/2)),
563 int x = pos_x - menu_width/2;
564 int y = y_pos - 12 - effect_offset;
565 /* Draw a horizontal line with a little 3d effect */
566 context.draw_filled_rect(Vector(x, y + 6),
567 Vector(menu_width, 4), Color(150,200,255,225), LAYER_FOREGROUND1);
568 context.draw_filled_rect(Vector(x, y + 6),
569 Vector(menu_width, 2), Color(255,255,255,255), LAYER_FOREGROUND1);
574 context.draw_text_center(white_big_text,
575 pitem.text, Vector(0, y_pos - int(white_big_text->get_height()/2)),
581 case MN_CONTROLFIELD_KB:
582 case MN_CONTROLFIELD_JS:
584 int width = text_width + input_width + 5;
585 int text_pos = screen->w/2 - width/2;
586 int input_pos = text_pos + text_width + 10;
588 context.draw_filled_rect(
589 Vector(input_pos - 5, y_pos - 10),
590 Vector(input_width + 10, 20),
591 Color(255,255,255,255), LAYER_FOREGROUND1-5);
592 context.draw_filled_rect(
593 Vector(input_pos - 4, y_pos - 9),
594 Vector(input_width + 8, 18),
595 Color(0,0,0,128), LAYER_FOREGROUND1-4);
597 if(pitem.kind == MN_CONTROLFIELD_KB)
598 get_controlfield_key_into_input(&pitem);
599 else if (pitem.kind == MN_CONTROLFIELD_JS)
600 get_controlfield_js_into_input(&pitem);
602 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
604 if(active_item == index)
605 context.draw_text(gold_text,
606 pitem.get_input_with_symbol(true),
607 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
610 context.draw_text(gold_text,
611 pitem.get_input_with_symbol(false),
612 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
616 context.draw_text(gold_text, pitem.input,
617 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
620 context.draw_text(text_font, pitem.text,
621 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
625 case MN_STRINGSELECT:
627 int list_pos_2 = list_width + 16;
628 int list_pos = list_width/2;
629 int text_pos = (text_width + 16)/2;
632 context.draw_surface(arrow_left,
633 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
635 context.draw_surface(arrow_right,
636 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
639 /* Draw input background */
640 context.draw_filled_rect(
641 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
642 Vector(list_pos_2 + 2, 20),
643 Color(255,255,255,255), LAYER_FOREGROUND1 - 4);
644 context.draw_filled_rect(
645 Vector(x_pos - list_pos + text_pos, y_pos - 9),
646 Vector(list_pos_2, 18),
647 Color(0,0,0,128), LAYER_FOREGROUND1 - 5);
649 context.draw_text_center(text_font, string_list_active(pitem.list),
650 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
652 context.draw_text_center(text_font, pitem.text,
653 Vector(list_pos_2/2, y_pos - int(text_font->get_height()/2)),
659 context.draw_text_center(text_font, pitem.text,
660 Vector(0, y_pos - int(text_font->get_height()/2)),
662 context.draw_surface(back,
663 Vector(x_pos + text_width/2 + 16, y_pos - 8),
670 context.draw_text_center(text_font, pitem.text,
671 Vector(0, y_pos - (text_font->get_height()/2)),
675 context.draw_surface(checkbox_checked,
676 Vector(x_pos + (text_width+16)/2, y_pos - 8),
677 LAYER_FOREGROUND1 + 1);
679 context.draw_surface(checkbox,
680 Vector(x_pos + (text_width+16)/2, y_pos - 8),
681 LAYER_FOREGROUND1 + 1);
685 context.draw_text_center(text_font, pitem.text,
686 Vector(0, y_pos - int(text_font->get_height()/2)),
691 context.draw_text_center(text_font, pitem.text,
692 Vector(0, y_pos - int(text_font->get_height()/2)),
698 int Menu::get_width() const
700 /* The width of the menu has to be more than the width of the text
701 with the most characters */
703 for(unsigned int i = 0; i < item.size(); ++i)
705 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
709 if( item[i].kind == MN_TOGGLE)
714 return (menu_width * 16 + 24);
717 int Menu::get_height() const
719 return item.size() * 24;
722 /* Draw the current menu. */
724 Menu::draw(DrawingContext& context)
726 int menu_height = get_height();
727 int menu_width = get_width();
729 /* Draw a transparent background */
730 context.draw_filled_rect(
731 Vector(pos_x - menu_width/2, pos_y - 24*item.size()/2 - 10),
732 Vector(menu_width,menu_height + 20),
733 Color(150,180,200,125), LAYER_FOREGROUND1-10);
735 for(unsigned int i = 0; i < item.size(); ++i)
737 draw_item(context, i, menu_width, menu_height);
742 Menu::get_item_by_id(int id)
744 for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i)
751 static MenuItem dummyitem;
755 int Menu::get_active_item_id()
757 return item[active_item].id;
761 Menu::isToggled(int id)
763 return get_item_by_id(id).toggled;
766 /* Check for menu event */
768 Menu::event(SDL_Event& event)
774 key = event.key.keysym.sym;
777 keymod = SDL_GetModState();
780 /* If the current unicode character is an ASCII character,
782 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
784 ch[0] = event.key.keysym.unicode & 0x7F;
789 /* An International Character. */
792 if(item[active_item].kind == MN_CONTROLFIELD_KB)
794 if(key == SDLK_ESCAPE)
799 *item[active_item].int_p = key;
800 menuaction = MENU_ACTION_DOWN;
807 case SDLK_UP: /* Menu Up */
808 menuaction = MENU_ACTION_UP;
810 case SDLK_DOWN: /* Menu Down */
811 menuaction = MENU_ACTION_DOWN;
813 case SDLK_LEFT: /* Menu Up */
814 menuaction = MENU_ACTION_LEFT;
816 case SDLK_RIGHT: /* Menu Down */
817 menuaction = MENU_ACTION_RIGHT;
820 if(item[active_item].kind == MN_TEXTFIELD)
822 menuaction = MENU_ACTION_INPUT;
826 case SDLK_RETURN: /* Menu Hit */
827 menuaction = MENU_ACTION_HIT;
831 menuaction = MENU_ACTION_REMOVE;
838 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
840 menuaction = MENU_ACTION_INPUT;
845 mn_input_char = '\0';
850 case SDL_JOYAXISMOTION:
851 if(event.jaxis.axis == joystick_keymap.y_axis)
853 if (event.jaxis.value > joystick_keymap.dead_zone && !joystick_timer.started())
855 menuaction = MENU_ACTION_DOWN;
856 joystick_timer.start(JOYSTICK_MENU_DELAY);
858 else if (event.jaxis.value < -joystick_keymap.dead_zone && !joystick_timer.started())
860 menuaction = MENU_ACTION_UP;
861 joystick_timer.start(JOYSTICK_MENU_DELAY);
864 joystick_timer.stop();
867 case SDL_JOYBUTTONDOWN:
868 if (item[active_item].kind == MN_CONTROLFIELD_JS)
870 *item[active_item].int_p = key;
871 menuaction = MENU_ACTION_DOWN;
873 menuaction = MENU_ACTION_HIT;
875 case SDL_MOUSEBUTTONDOWN:
878 if(x > pos_x - get_width()/2 &&
879 x < pos_x + get_width()/2 &&
880 y > pos_y - get_height()/2 &&
881 y < pos_y + get_height()/2)
883 menuaction = MENU_ACTION_HIT;
886 case SDL_MOUSEMOTION:
889 if(x > pos_x - get_width()/2 &&
890 x < pos_x + get_width()/2 &&
891 y > pos_y - get_height()/2 &&
892 y < pos_y + get_height()/2)
894 active_item = (y - (pos_y - get_height()/2)) / 24;
895 mouse_cursor->set_state(MC_LINK);
899 mouse_cursor->set_state(MC_NORMAL);