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 if(background == NULL)
93 context.draw_gradient(Color(200, 100, 200), Color(100, 200, 100), LAYER_BACKGROUND0);
95 context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
97 dialog->draw(context);
100 switch (dialog->check())
104 Menu::set_current(0);
110 Menu::set_current(0);
118 mouse_cursor->draw(context);
119 context.do_drawing();
127 Menu::push_current(Menu* pmenu)
130 last_menus.push_back(current_);
133 current_->effect.start(500);
139 if (!last_menus.empty())
141 current_ = last_menus.back();
142 current_->effect.start(500);
144 last_menus.pop_back();
153 Menu::set_current(Menu* menu)
158 menu->effect.start(500);
163 /* Return a pointer to a new menu item */
165 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_)
167 MenuItem *pnew_item = new MenuItem;
169 pnew_item->kind = kind_;
170 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
171 strcpy(pnew_item->text, text_);
173 if(kind_ == MN_TOGGLE)
174 pnew_item->toggled = init_toggle_;
176 pnew_item->toggled = false;
178 pnew_item->target_menu = target_menu_;
179 pnew_item->input = (char*) malloc(sizeof(char));
180 pnew_item->input[0] = '\0';
182 if(kind_ == MN_STRINGSELECT)
184 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
185 string_list_init(pnew_item->list);
188 pnew_item->list = NULL;
191 pnew_item->int_p = int_p_;
193 pnew_item->input_flickering = false;
194 pnew_item->input_flickering_timer.init(true);
195 pnew_item->input_flickering_timer.start(FLICK_CURSOR_TIME);
201 MenuItem::change_text(const char *text_)
206 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
212 MenuItem::change_input(const char *text_)
217 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
218 strcpy(input, text_);
222 std::string MenuItem::get_input_with_symbol(bool active_item)
225 input_flickering = true;
228 if(input_flickering_timer.get_left() < 0)
231 input_flickering = false;
233 input_flickering = true;
234 input_flickering_timer.start(FLICK_CURSOR_TIME);
240 sprintf(str,"%s_",input);
242 sprintf(str,"%s ",input);
244 std::string string = str;
249 /* Set ControlField for keyboard key */
250 void Menu::get_controlfield_key_into_input(MenuItem *item)
255 item->change_input("Up cursor");
258 item->change_input("Down cursor");
261 item->change_input("Left cursor");
264 item->change_input("Right cursor");
267 item->change_input("Return");
270 item->change_input("Space");
273 item->change_input("Right Shift");
276 item->change_input("Left Shift");
279 item->change_input("Right Control");
282 item->change_input("Left Control");
285 item->change_input("Right Alt");
288 item->change_input("Left Alt");
293 snprintf(tmp, 64, "%d", *item->int_p);
294 item->change_input(tmp);
300 /* Set ControlField for joystick button */
301 void Menu::get_controlfield_js_into_input(MenuItem *item)
303 std::ostringstream oss;
304 oss << "Button " << *item->int_p;
305 item->change_input(oss.str().c_str());
308 /* Free a menu and all its items */
313 for(unsigned int i = 0; i < item.size(); ++i)
317 string_list_free(item[i].list);
326 menuaction = MENU_ACTION_NONE;
327 delete_character = 0;
328 mn_input_char = '\0';
336 joystick_timer.init(true);
339 void Menu::set_pos(int x, int y, float rw, float rh)
341 pos_x = x + (int)((float)get_width() * rw);
342 pos_y = y + (int)((float)get_height() * rh);
346 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
348 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
351 /* Add an item to a menu */
353 Menu::additem(MenuItem* pmenu_item)
355 item.push_back(*pmenu_item);
365 /* Process actions done on the menu */
378 active_item = int(item.size())-1;
381 case MENU_ACTION_DOWN:
382 if(active_item < int(item.size())-1)
388 case MENU_ACTION_LEFT:
389 if(item[active_item].kind == MN_STRINGSELECT
390 && item[active_item].list->num_items != 0)
392 if(item[active_item].list->active_item > 0)
393 --item[active_item].list->active_item;
395 item[active_item].list->active_item = item[active_item].list->num_items-1;
399 case MENU_ACTION_RIGHT:
400 if(item[active_item].kind == MN_STRINGSELECT
401 && item[active_item].list->num_items != 0)
403 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
404 ++item[active_item].list->active_item;
406 item[active_item].list->active_item = 0;
410 case MENU_ACTION_HIT:
412 hit_item = active_item;
413 switch (item[active_item].kind)
416 if (item[active_item].target_menu != NULL)
417 Menu::push_current(item[active_item].target_menu);
423 item[active_item].toggled = !item[active_item].toggled;
427 Menu::set_current(0);
428 item[active_item].toggled = true;
432 menuaction = MENU_ACTION_DOWN;
445 case MENU_ACTION_REMOVE:
446 if(item[active_item].kind == MN_TEXTFIELD
447 || item[active_item].kind == MN_NUMFIELD)
449 if(item[active_item].input != NULL)
451 int i = strlen(item[active_item].input);
453 while(delete_character > 0) /* remove charactes */
455 item[active_item].input[i-1] = '\0';
462 case MENU_ACTION_INPUT:
463 if(item[active_item].kind == MN_TEXTFIELD
464 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
466 if(item[active_item].input != NULL)
468 int i = strlen(item[active_item].input);
469 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
470 item[active_item].input[i] = mn_input_char;
471 item[active_item].input[i+1] = '\0';
475 item[active_item].input = (char*) malloc(2*sizeof(char));
476 item[active_item].input[0] = mn_input_char;
477 item[active_item].input[1] = '\0';
481 case MENU_ACTION_NONE:
486 MenuItem& new_item = item[active_item];
487 if(new_item.kind == MN_DEACTIVE
488 || new_item.kind == MN_LABEL
489 || new_item.kind == MN_HL)
491 // Skip the horzontal line item
492 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
493 menuaction = MENU_ACTION_DOWN;
499 menuaction = MENU_ACTION_NONE;
501 if (active_item >= int(item.size()))
502 active_item = int(item.size()) - 1;
509 return item[hit_item].id;
515 Menu::draw_item(DrawingContext& context,
516 int index, // Position of the current item in the menu
517 int menu_width, int menu_height)
519 MenuItem& pitem = item[index];
521 int effect_offset = 0;
526 effect_time = effect.get_left() / 4;
528 effect_offset = (index % 2) ? effect_time : -effect_time;
531 Font* text_font = white_text;
533 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
535 int text_width = int(text_font->get_text_width(pitem.text));
536 int input_width = int(text_font->get_text_width(pitem.input) + 10);
538 int(text_font->get_text_width(string_list_active(pitem.list)));
541 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
543 if(index == active_item)
546 text_font = blue_text;
553 context.draw_text_center(blue_text, pitem.text,
554 Vector(0, y_pos - int(blue_text->get_height()/2)),
562 int x = pos_x - menu_width/2;
563 int y = y_pos - 12 - effect_offset;
564 /* Draw a horizontal line with a little 3d effect */
565 context.draw_filled_rect(Vector(x, y + 6),
566 Vector(menu_width, 4), Color(150,200,255,225), LAYER_FOREGROUND1);
567 context.draw_filled_rect(Vector(x, y + 6),
568 Vector(menu_width, 2), Color(255,255,255,255), LAYER_FOREGROUND1);
573 context.draw_text_center(white_big_text,
574 pitem.text, Vector(0, y_pos - int(white_big_text->get_height()/2)),
580 case MN_CONTROLFIELD_KB:
581 case MN_CONTROLFIELD_JS:
583 int width = text_width + input_width + 5;
584 int text_pos = screen->w/2 - width/2;
585 int input_pos = text_pos + text_width + 10;
587 context.draw_filled_rect(
588 Vector(input_pos - 5, y_pos - 10),
589 Vector(input_width + 10, 20),
590 Color(255,255,255,255), LAYER_FOREGROUND1-5);
591 context.draw_filled_rect(
592 Vector(input_pos - 4, y_pos - 9),
593 Vector(input_width + 8, 18),
594 Color(0,0,0,128), LAYER_FOREGROUND1-4);
596 if(pitem.kind == MN_CONTROLFIELD_KB)
597 get_controlfield_key_into_input(&pitem);
598 else if (pitem.kind == MN_CONTROLFIELD_JS)
599 get_controlfield_js_into_input(&pitem);
601 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
603 if(active_item == index)
604 context.draw_text(gold_text,
605 pitem.get_input_with_symbol(true),
606 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
609 context.draw_text(gold_text,
610 pitem.get_input_with_symbol(false),
611 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
615 context.draw_text(gold_text, pitem.input,
616 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
619 context.draw_text(text_font, pitem.text,
620 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
624 case MN_STRINGSELECT:
626 int list_pos_2 = list_width + 16;
627 int list_pos = list_width/2;
628 int text_pos = (text_width + 16)/2;
631 context.draw_surface(arrow_left,
632 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
634 context.draw_surface(arrow_right,
635 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
638 /* Draw input background */
639 context.draw_filled_rect(
640 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
641 Vector(list_pos_2 + 2, 20),
642 Color(255,255,255,255), LAYER_FOREGROUND1 - 4);
643 context.draw_filled_rect(
644 Vector(x_pos - list_pos + text_pos, y_pos - 9),
645 Vector(list_pos_2, 18),
646 Color(0,0,0,128), LAYER_FOREGROUND1 - 5);
648 context.draw_text_center(text_font, string_list_active(pitem.list),
649 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
651 context.draw_text_center(text_font, pitem.text,
652 Vector(list_pos_2/2, y_pos - int(text_font->get_height()/2)),
658 context.draw_text_center(text_font, pitem.text,
659 Vector(0, y_pos - int(text_font->get_height()/2)),
661 context.draw_surface(back,
662 Vector(x_pos + text_width/2 + 16, y_pos - 8),
669 context.draw_text_center(text_font, pitem.text,
670 Vector(0, y_pos - (text_font->get_height()/2)),
674 context.draw_surface(checkbox_checked,
675 Vector(x_pos + (text_width+16)/2, y_pos - 8),
676 LAYER_FOREGROUND1 + 1);
678 context.draw_surface(checkbox,
679 Vector(x_pos + (text_width+16)/2, y_pos - 8),
680 LAYER_FOREGROUND1 + 1);
684 context.draw_text_center(text_font, pitem.text,
685 Vector(0, y_pos - int(text_font->get_height()/2)),
690 context.draw_text_center(text_font, pitem.text,
691 Vector(0, y_pos - int(text_font->get_height()/2)),
697 int Menu::get_width() const
699 /* The width of the menu has to be more than the width of the text
700 with the most characters */
702 for(unsigned int i = 0; i < item.size(); ++i)
704 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
708 if( item[i].kind == MN_TOGGLE)
713 return (menu_width * 16 + 24);
716 int Menu::get_height() const
718 return item.size() * 24;
721 /* Draw the current menu. */
723 Menu::draw(DrawingContext& context)
725 int menu_height = get_height();
726 int menu_width = get_width();
728 /* Draw a transparent background */
729 context.draw_filled_rect(
730 Vector(pos_x - menu_width/2, pos_y - 24*item.size()/2 - 10),
731 Vector(menu_width,menu_height + 20),
732 Color(150,180,200,125), LAYER_FOREGROUND1-10);
734 for(unsigned int i = 0; i < item.size(); ++i)
736 draw_item(context, i, menu_width, menu_height);
741 Menu::get_item_by_id(int id)
743 for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i)
750 static MenuItem dummyitem;
754 int Menu::get_active_item_id()
756 return item[active_item].id;
760 Menu::isToggled(int id)
762 return get_item_by_id(id).toggled;
765 /* Check for menu event */
767 Menu::event(SDL_Event& event)
773 key = event.key.keysym.sym;
776 keymod = SDL_GetModState();
779 /* If the current unicode character is an ASCII character,
781 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
783 ch[0] = event.key.keysym.unicode & 0x7F;
788 /* An International Character. */
791 if(item[active_item].kind == MN_CONTROLFIELD_KB)
793 if(key == SDLK_ESCAPE)
798 *item[active_item].int_p = key;
799 menuaction = MENU_ACTION_DOWN;
806 case SDLK_UP: /* Menu Up */
807 menuaction = MENU_ACTION_UP;
809 case SDLK_DOWN: /* Menu Down */
810 menuaction = MENU_ACTION_DOWN;
812 case SDLK_LEFT: /* Menu Up */
813 menuaction = MENU_ACTION_LEFT;
815 case SDLK_RIGHT: /* Menu Down */
816 menuaction = MENU_ACTION_RIGHT;
819 if(item[active_item].kind == MN_TEXTFIELD)
821 menuaction = MENU_ACTION_INPUT;
825 case SDLK_RETURN: /* Menu Hit */
826 menuaction = MENU_ACTION_HIT;
830 menuaction = MENU_ACTION_REMOVE;
837 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
839 menuaction = MENU_ACTION_INPUT;
844 mn_input_char = '\0';
849 case SDL_JOYAXISMOTION:
850 if(event.jaxis.axis == joystick_keymap.y_axis)
852 if (event.jaxis.value > joystick_keymap.dead_zone && !joystick_timer.started())
854 menuaction = MENU_ACTION_DOWN;
855 joystick_timer.start(JOYSTICK_MENU_DELAY);
857 else if (event.jaxis.value < -joystick_keymap.dead_zone && !joystick_timer.started())
859 menuaction = MENU_ACTION_UP;
860 joystick_timer.start(JOYSTICK_MENU_DELAY);
863 joystick_timer.stop();
866 case SDL_JOYBUTTONDOWN:
867 if (item[active_item].kind == MN_CONTROLFIELD_JS)
869 *item[active_item].int_p = key;
870 menuaction = MENU_ACTION_DOWN;
872 menuaction = MENU_ACTION_HIT;
874 case SDL_MOUSEBUTTONDOWN:
877 if(x > pos_x - get_width()/2 &&
878 x < pos_x + get_width()/2 &&
879 y > pos_y - get_height()/2 &&
880 y < pos_y + get_height()/2)
882 menuaction = MENU_ACTION_HIT;
885 case SDL_MOUSEMOTION:
888 if(x > pos_x - get_width()/2 &&
889 x < pos_x + get_width()/2 &&
890 y > pos_y - get_height()/2 &&
891 y < pos_y + get_height()/2)
893 active_item = (y - (pos_y - get_height()/2)) / 24;
894 mouse_cursor->set_state(MC_LINK);
898 mouse_cursor->set_state(MC_NORMAL);