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"
44 #define FLICK_CURSOR_TIME 500
47 Surface* checkbox_checked;
54 Menu* worldmap_menu = 0;
55 Menu* options_menu = 0;
56 Menu* options_keys_menu = 0;
57 Menu* options_joystick_menu = 0;
58 Menu* highscore_menu = 0;
59 Menu* load_game_menu = 0;
60 Menu* save_game_menu = 0;
61 Menu* contrib_menu = 0;
62 Menu* contrib_subset_menu = 0;
64 std::vector<Menu*> Menu::last_menus;
65 Menu* Menu::current_ = 0;
67 /* just displays a Yes/No text that can be used to confirm stuff */
68 bool confirm_dialog(Surface *background, std::string text)
70 //Surface* cap_screen = Surface::CaptureScreen();
72 Menu* dialog = new Menu;
73 dialog->additem(MN_DEACTIVE, text,0,0);
74 dialog->additem(MN_HL,"",0,0);
75 dialog->additem(MN_ACTION,"Yes",0,0,true);
76 dialog->additem(MN_ACTION,"No",0,0,false);
77 dialog->additem(MN_HL,"",0,0);
79 Menu::set_current(dialog);
81 DrawingContext context;
87 while (SDL_PollEvent(&event))
92 context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
94 dialog->draw(context);
97 switch (dialog->check())
101 Menu::set_current(0);
107 Menu::set_current(0);
115 mouse_cursor->draw(context);
116 context.do_drawing();
124 Menu::push_current(Menu* pmenu)
127 last_menus.push_back(current_);
130 current_->effect.start(500);
136 if (!last_menus.empty())
138 current_ = last_menus.back();
139 current_->effect.start(500);
141 last_menus.pop_back();
150 Menu::set_current(Menu* menu)
155 menu->effect.start(500);
160 /* Return a pointer to a new menu item */
162 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_)
164 MenuItem *pnew_item = new MenuItem;
166 pnew_item->kind = kind_;
167 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
168 strcpy(pnew_item->text, text_);
170 if(kind_ == MN_TOGGLE)
171 pnew_item->toggled = init_toggle_;
173 pnew_item->toggled = false;
175 pnew_item->target_menu = target_menu_;
176 pnew_item->input = (char*) malloc(sizeof(char));
177 pnew_item->input[0] = '\0';
179 if(kind_ == MN_STRINGSELECT)
181 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
182 string_list_init(pnew_item->list);
185 pnew_item->list = NULL;
188 pnew_item->int_p = int_p_;
190 pnew_item->input_flickering = false;
191 pnew_item->input_flickering_timer.init(true);
192 pnew_item->input_flickering_timer.start(FLICK_CURSOR_TIME);
198 MenuItem::change_text(const char *text_)
203 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
209 MenuItem::change_input(const char *text_)
214 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
215 strcpy(input, text_);
219 std::string MenuItem::get_input_with_symbol(bool active_item)
222 input_flickering = true;
225 if(input_flickering_timer.get_left() < 0)
228 input_flickering = false;
230 input_flickering = true;
231 input_flickering_timer.start(FLICK_CURSOR_TIME);
237 sprintf(str,"%s_",input);
239 sprintf(str,"%s ",input);
241 std::string string = str;
246 /* Set ControlField for keyboard key */
247 void Menu::get_controlfield_key_into_input(MenuItem *item)
252 item->change_input("Up cursor");
255 item->change_input("Down cursor");
258 item->change_input("Left cursor");
261 item->change_input("Right cursor");
264 item->change_input("Return");
267 item->change_input("Space");
270 item->change_input("Right Shift");
273 item->change_input("Left Shift");
276 item->change_input("Right Control");
279 item->change_input("Left Control");
282 item->change_input("Right Alt");
285 item->change_input("Left Alt");
290 snprintf(tmp, 64, "%d", *item->int_p);
291 item->change_input(tmp);
297 /* Set ControlField for joystick button */
298 void Menu::get_controlfield_js_into_input(MenuItem *item)
300 std::ostringstream oss;
301 oss << "Button " << *item->int_p;
302 item->change_input(oss.str().c_str());
305 /* Free a menu and all its items */
310 for(unsigned int i = 0; i < item.size(); ++i)
314 string_list_free(item[i].list);
323 menuaction = MENU_ACTION_NONE;
324 delete_character = 0;
325 mn_input_char = '\0';
333 joystick_timer.init(true);
336 void Menu::set_pos(int x, int y, float rw, float rh)
338 pos_x = x + (int)((float)get_width() * rw);
339 pos_y = y + (int)((float)get_height() * rh);
343 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
345 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
348 /* Add an item to a menu */
350 Menu::additem(MenuItem* pmenu_item)
352 item.push_back(*pmenu_item);
362 /* Process actions done on the menu */
375 active_item = int(item.size())-1;
378 case MENU_ACTION_DOWN:
379 if(active_item < int(item.size())-1)
385 case MENU_ACTION_LEFT:
386 if(item[active_item].kind == MN_STRINGSELECT
387 && item[active_item].list->num_items != 0)
389 if(item[active_item].list->active_item > 0)
390 --item[active_item].list->active_item;
392 item[active_item].list->active_item = item[active_item].list->num_items-1;
396 case MENU_ACTION_RIGHT:
397 if(item[active_item].kind == MN_STRINGSELECT
398 && item[active_item].list->num_items != 0)
400 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
401 ++item[active_item].list->active_item;
403 item[active_item].list->active_item = 0;
407 case MENU_ACTION_HIT:
409 hit_item = active_item;
410 switch (item[active_item].kind)
413 if (item[active_item].target_menu != NULL)
414 Menu::push_current(item[active_item].target_menu);
420 item[active_item].toggled = !item[active_item].toggled;
424 Menu::set_current(0);
425 item[active_item].toggled = true;
429 menuaction = MENU_ACTION_DOWN;
442 case MENU_ACTION_REMOVE:
443 if(item[active_item].kind == MN_TEXTFIELD
444 || item[active_item].kind == MN_NUMFIELD)
446 if(item[active_item].input != NULL)
448 int i = strlen(item[active_item].input);
450 while(delete_character > 0) /* remove charactes */
452 item[active_item].input[i-1] = '\0';
459 case MENU_ACTION_INPUT:
460 if(item[active_item].kind == MN_TEXTFIELD
461 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
463 if(item[active_item].input != NULL)
465 int i = strlen(item[active_item].input);
466 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
467 item[active_item].input[i] = mn_input_char;
468 item[active_item].input[i+1] = '\0';
472 item[active_item].input = (char*) malloc(2*sizeof(char));
473 item[active_item].input[0] = mn_input_char;
474 item[active_item].input[1] = '\0';
478 case MENU_ACTION_NONE:
483 MenuItem& new_item = item[active_item];
484 if(new_item.kind == MN_DEACTIVE
485 || new_item.kind == MN_LABEL
486 || new_item.kind == MN_HL)
488 // Skip the horzontal line item
489 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
490 menuaction = MENU_ACTION_DOWN;
496 menuaction = MENU_ACTION_NONE;
498 if (active_item >= int(item.size()))
499 active_item = int(item.size()) - 1;
506 return item[hit_item].id;
512 Menu::draw_item(DrawingContext& context,
513 int index, // Position of the current item in the menu
514 int menu_width, int menu_height)
516 MenuItem& pitem = item[index];
518 int effect_offset = 0;
523 effect_time = effect.get_left() / 4;
525 effect_offset = (index % 2) ? effect_time : -effect_time;
528 Font* text_font = white_text;
530 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
532 int text_width = int(text_font->get_text_width(pitem.text));
533 int input_width = int(text_font->get_text_width(pitem.input) + 10);
535 int(text_font->get_text_width(string_list_active(pitem.list)));
538 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
540 if(index == active_item)
543 text_font = blue_text;
550 context.draw_text_center(blue_text, pitem.text,
551 Vector(0, y_pos - int(blue_text->get_height()/2)),
559 int x = pos_x - menu_width/2;
560 int y = y_pos - 12 - effect_offset;
561 /* Draw a horizontal line with a little 3d effect */
562 context.draw_filled_rect(Vector(x, y + 6),
563 Vector(menu_width, 4), Color(150,200,255,225), LAYER_FOREGROUND1);
564 context.draw_filled_rect(Vector(x, y + 6),
565 Vector(menu_width, 2), Color(255,255,255,255), LAYER_FOREGROUND1);
570 context.draw_text_center(white_big_text,
571 pitem.text, Vector(0, y_pos - int(white_big_text->get_height()/2)),
577 case MN_CONTROLFIELD_KB:
578 case MN_CONTROLFIELD_JS:
580 int width = text_width + input_width + 5;
581 int text_pos = screen->w/2 - width/2;
582 int input_pos = text_pos + text_width + 10;
584 context.draw_filled_rect(
585 Vector(input_pos - 5, y_pos - 10),
586 Vector(input_width + 10, 20),
587 Color(255,255,255,255), LAYER_FOREGROUND1-5);
588 context.draw_filled_rect(
589 Vector(input_pos - 4, y_pos - 9),
590 Vector(input_width + 8, 18),
591 Color(0,0,0,128), LAYER_FOREGROUND1-4);
593 if(pitem.kind == MN_CONTROLFIELD_KB)
594 get_controlfield_key_into_input(&pitem);
595 else if (pitem.kind == MN_CONTROLFIELD_JS)
596 get_controlfield_js_into_input(&pitem);
598 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
600 if(active_item == index)
601 context.draw_text(gold_text,
602 pitem.get_input_with_symbol(true),
603 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
606 context.draw_text(gold_text,
607 pitem.get_input_with_symbol(false),
608 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
612 context.draw_text(gold_text, pitem.input,
613 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
616 context.draw_text(text_font, pitem.text,
617 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
621 case MN_STRINGSELECT:
623 int list_pos_2 = list_width + 16;
624 int list_pos = list_width/2;
625 int text_pos = (text_width + 16)/2;
628 context.draw_surface(arrow_left,
629 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
631 context.draw_surface(arrow_right,
632 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
635 /* Draw input background */
636 context.draw_filled_rect(
637 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
638 Vector(list_pos_2 + 2, 20),
639 Color(255,255,255,255), LAYER_FOREGROUND1 - 4);
640 context.draw_filled_rect(
641 Vector(x_pos - list_pos + text_pos, y_pos - 9),
642 Vector(list_pos_2, 18),
643 Color(0,0,0,128), LAYER_FOREGROUND1 - 5);
645 context.draw_text_center(text_font, string_list_active(pitem.list),
646 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
648 context.draw_text_center(text_font, pitem.text,
649 Vector(list_pos_2/2, y_pos - int(text_font->get_height()/2)),
655 context.draw_text_center(text_font, pitem.text,
656 Vector(0, y_pos - int(text_font->get_height()/2)),
658 context.draw_surface(back,
659 Vector(x_pos + text_width/2 + 16, y_pos - 8),
666 context.draw_text_center(text_font, pitem.text,
667 Vector(0, y_pos - (text_font->get_height()/2)),
671 context.draw_surface(checkbox_checked,
672 Vector(x_pos + (text_width+16)/2, y_pos - 8),
673 LAYER_FOREGROUND1 + 1);
675 context.draw_surface(checkbox,
676 Vector(x_pos + (text_width+16)/2, y_pos - 8),
677 LAYER_FOREGROUND1 + 1);
681 context.draw_text_center(text_font, pitem.text,
682 Vector(0, y_pos - int(text_font->get_height()/2)),
687 context.draw_text_center(text_font, pitem.text,
688 Vector(0, y_pos - int(text_font->get_height()/2)),
694 int Menu::get_width() const
696 /* The width of the menu has to be more than the width of the text
697 with the most characters */
699 for(unsigned int i = 0; i < item.size(); ++i)
701 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
705 if( item[i].kind == MN_TOGGLE)
710 return (menu_width * 16 + 24);
713 int Menu::get_height() const
715 return item.size() * 24;
718 /* Draw the current menu. */
720 Menu::draw(DrawingContext& context)
722 int menu_height = get_height();
723 int menu_width = get_width();
725 /* Draw a transparent background */
726 context.draw_filled_rect(
727 Vector(pos_x - menu_width/2, pos_y - 24*item.size()/2 - 10),
728 Vector(menu_width,menu_height + 20),
729 Color(150,180,200,125), LAYER_FOREGROUND1-10);
731 for(unsigned int i = 0; i < item.size(); ++i)
733 draw_item(context, i, menu_width, menu_height);
738 Menu::get_item_by_id(int id)
740 for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i)
747 static MenuItem dummyitem;
751 int Menu::get_active_item_id()
753 return item[active_item].id;
757 Menu::isToggled(int id)
759 return get_item_by_id(id).toggled;
762 /* Check for menu event */
764 Menu::event(SDL_Event& event)
770 key = event.key.keysym.sym;
773 keymod = SDL_GetModState();
776 /* If the current unicode character is an ASCII character,
778 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
780 ch[0] = event.key.keysym.unicode & 0x7F;
785 /* An International Character. */
788 if(item[active_item].kind == MN_CONTROLFIELD_KB)
790 if(key == SDLK_ESCAPE)
795 *item[active_item].int_p = key;
796 menuaction = MENU_ACTION_DOWN;
803 case SDLK_UP: /* Menu Up */
804 menuaction = MENU_ACTION_UP;
806 case SDLK_DOWN: /* Menu Down */
807 menuaction = MENU_ACTION_DOWN;
809 case SDLK_LEFT: /* Menu Up */
810 menuaction = MENU_ACTION_LEFT;
812 case SDLK_RIGHT: /* Menu Down */
813 menuaction = MENU_ACTION_RIGHT;
816 if(item[active_item].kind == MN_TEXTFIELD)
818 menuaction = MENU_ACTION_INPUT;
822 case SDLK_RETURN: /* Menu Hit */
823 menuaction = MENU_ACTION_HIT;
827 menuaction = MENU_ACTION_REMOVE;
834 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
836 menuaction = MENU_ACTION_INPUT;
841 mn_input_char = '\0';
846 case SDL_JOYAXISMOTION:
847 if(event.jaxis.axis == joystick_keymap.y_axis)
849 if (event.jaxis.value > joystick_keymap.dead_zone && !joystick_timer.started())
851 menuaction = MENU_ACTION_DOWN;
852 joystick_timer.start(JOYSTICK_MENU_DELAY);
854 else if (event.jaxis.value < -joystick_keymap.dead_zone && !joystick_timer.started())
856 menuaction = MENU_ACTION_UP;
857 joystick_timer.start(JOYSTICK_MENU_DELAY);
860 joystick_timer.stop();
863 case SDL_JOYBUTTONDOWN:
864 if (item[active_item].kind == MN_CONTROLFIELD_JS)
866 *item[active_item].int_p = key;
867 menuaction = MENU_ACTION_DOWN;
869 menuaction = MENU_ACTION_HIT;
871 case SDL_MOUSEBUTTONDOWN:
874 if(x > pos_x - get_width()/2 &&
875 x < pos_x + get_width()/2 &&
876 y > pos_y - get_height()/2 &&
877 y < pos_y + get_height()/2)
879 menuaction = MENU_ACTION_HIT;
882 case SDL_MOUSEMOTION:
885 if(x > pos_x - get_width()/2 &&
886 x < pos_x + get_width()/2 &&
887 y > pos_y - get_height()/2 &&
888 y < pos_y + get_height()/2)
890 active_item = (y - (pos_y - get_height()/2)) / 24;
891 mouse_cursor->set_state(MC_LINK);
895 mouse_cursor->set_state(MC_NORMAL);