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>
38 #include "leveleditor.h"
40 #include "high_scores.h"
42 #define FLICK_CURSOR_TIME 500
45 Surface* checkbox_checked;
52 Menu* worldmap_menu = 0;
53 Menu* options_menu = 0;
54 Menu* options_keys_menu = 0;
55 Menu* options_joystick_menu = 0;
56 Menu* highscore_menu = 0;
57 Menu* load_game_menu = 0;
58 Menu* save_game_menu = 0;
59 Menu* contrib_menu = 0;
60 Menu* contrib_subset_menu = 0;
62 std::vector<Menu*> Menu::last_menus;
63 Menu* Menu::current_ = 0;
65 /* just displays a Yes/No text that can be used to confirm stuff */
66 bool confirm_dialog(char *text)
68 white_text->drawf(text,0,0,A_HMIDDLE,A_VMIDDLE,2);
69 red_text->drawf("(Y)es/(N)o",0,20,A_HMIDDLE,A_VMIDDLE,2);
76 while(SDL_PollEvent(&event))
79 case SDL_KEYDOWN: // key pressed
80 switch(event.key.keysym.sym)
93 case SDL_QUIT: // quit signal
104 Menu::push_current(Menu* pmenu)
107 last_menus.push_back(current_);
110 current_->effect.start(500);
116 if (!last_menus.empty())
118 current_ = last_menus.back();
119 current_->effect.start(500);
121 last_menus.pop_back();
130 Menu::set_current(Menu* menu)
135 menu->effect.start(500);
140 /* Return a pointer to a new menu item */
142 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_)
144 MenuItem *pnew_item = new MenuItem;
146 pnew_item->kind = kind_;
147 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
148 strcpy(pnew_item->text, text_);
150 if(kind_ == MN_TOGGLE)
151 pnew_item->toggled = init_toggle_;
153 pnew_item->toggled = false;
155 pnew_item->target_menu = target_menu_;
156 pnew_item->input = (char*) malloc(sizeof(char));
157 pnew_item->input[0] = '\0';
159 if(kind_ == MN_STRINGSELECT)
161 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
162 string_list_init(pnew_item->list);
165 pnew_item->list = NULL;
168 pnew_item->int_p = int_p_;
170 pnew_item->input_flickering = false;
171 pnew_item->input_flickering_timer.init(true);
172 pnew_item->input_flickering_timer.start(FLICK_CURSOR_TIME);
178 MenuItem::change_text(const char *text_)
183 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
189 MenuItem::change_input(const char *text_)
194 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
195 strcpy(input, text_);
199 std::string MenuItem::get_input_with_symbol(bool active_item)
202 input_flickering = true;
205 if(input_flickering_timer.get_left() < 0)
208 input_flickering = false;
210 input_flickering = true;
211 input_flickering_timer.start(FLICK_CURSOR_TIME);
217 sprintf(str,"%s-",input);
219 sprintf(str,"%s ",input);
221 std::string string = str;
226 /* Set ControlField a key */
227 void Menu::get_controlfield_key_into_input(MenuItem *item)
232 item->change_input("Up cursor");
235 item->change_input("Down cursor");
238 item->change_input("Left cursor");
241 item->change_input("Right cursor");
244 item->change_input("Return");
247 item->change_input("Space");
250 item->change_input("Right Shift");
253 item->change_input("Left Shift");
256 item->change_input("Right Control");
259 item->change_input("Left Control");
262 item->change_input("Right Alt");
265 item->change_input("Left Alt");
270 snprintf(tmp, 64, "%d", *item->int_p);
271 item->change_input(tmp);
277 /* Free a menu and all its items */
282 for(unsigned int i = 0; i < item.size(); ++i)
286 string_list_free(item[i].list);
295 menuaction = MENU_ACTION_NONE;
296 delete_character = 0;
297 mn_input_char = '\0';
306 void Menu::set_pos(int x, int y, float rw, float rh)
308 pos_x = x + (int)((float)get_width() * rw);
309 pos_y = y + (int)((float)get_height() * rh);
313 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
315 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
318 /* Add an item to a menu */
320 Menu::additem(MenuItem* pmenu_item)
322 item.push_back(*pmenu_item);
332 /* Process actions done on the menu */
345 active_item = int(item.size())-1;
348 case MENU_ACTION_DOWN:
349 if(active_item < int(item.size())-1)
355 case MENU_ACTION_LEFT:
356 if(item[active_item].kind == MN_STRINGSELECT
357 && item[active_item].list->num_items != 0)
359 if(item[active_item].list->active_item > 0)
360 --item[active_item].list->active_item;
362 item[active_item].list->active_item = item[active_item].list->num_items-1;
366 case MENU_ACTION_RIGHT:
367 if(item[active_item].kind == MN_STRINGSELECT
368 && item[active_item].list->num_items != 0)
370 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
371 ++item[active_item].list->active_item;
373 item[active_item].list->active_item = 0;
377 case MENU_ACTION_HIT:
379 hit_item = active_item;
380 switch (item[active_item].kind)
383 if (item[active_item].target_menu != NULL)
384 Menu::push_current(item[active_item].target_menu);
390 item[active_item].toggled = !item[active_item].toggled;
394 Menu::set_current(0);
395 item[active_item].toggled = true;
399 menuaction = MENU_ACTION_DOWN;
412 case MENU_ACTION_REMOVE:
413 if(item[active_item].kind == MN_TEXTFIELD
414 || item[active_item].kind == MN_NUMFIELD)
416 if(item[active_item].input != NULL)
418 int i = strlen(item[active_item].input);
420 while(delete_character > 0) /* remove charactes */
422 item[active_item].input[i-1] = '\0';
429 case MENU_ACTION_INPUT:
430 if(item[active_item].kind == MN_TEXTFIELD
431 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
433 if(item[active_item].input != NULL)
435 int i = strlen(item[active_item].input);
436 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
437 item[active_item].input[i] = mn_input_char;
438 item[active_item].input[i+1] = '\0';
442 item[active_item].input = (char*) malloc(2*sizeof(char));
443 item[active_item].input[0] = mn_input_char;
444 item[active_item].input[1] = '\0';
448 case MENU_ACTION_NONE:
453 MenuItem& new_item = item[active_item];
454 if(new_item.kind == MN_DEACTIVE
455 || new_item.kind == MN_LABEL
456 || new_item.kind == MN_HL)
458 // Skip the horzontal line item
459 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
460 menuaction = MENU_ACTION_DOWN;
466 menuaction = MENU_ACTION_NONE;
473 return item[hit_item].id;
479 Menu::draw_item(int index, // Position of the current item in the menu
483 MenuItem& pitem = item[index];
486 int effect_offset = 0;
491 effect_time = effect.get_left() / 4;
493 effect_offset = (index % 2) ? effect_time : -effect_time;
497 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
499 int text_width = strlen(pitem.text) * font_width;
500 int input_width = (strlen(pitem.input)+ 1) * font_width;
501 int list_width = strlen(string_list_active(pitem.list)) * font_width;
502 Text* text_font = white_text;
505 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
507 if(index == active_item)
510 text_font = blue_text;
517 black_text->draw_align(pitem.text,
519 A_HMIDDLE, A_VMIDDLE, 2);
525 int x = pos_x - menu_width/2;
526 int y = y_pos - 12 - effect_offset;
527 /* Draw a horizontal line with a little 3d effect */
538 white_big_text->draw_align(pitem.text,
540 A_HMIDDLE, A_VMIDDLE, 2);
545 case MN_CONTROLFIELD:
547 int input_pos = input_width/2;
548 int text_pos = (text_width + font_width)/2;
550 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
551 input_width + font_width + 2, 20,
553 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
554 input_width + font_width, 18,
557 if(pitem.kind == MN_CONTROLFIELD)
558 get_controlfield_key_into_input(&pitem);
560 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
562 if(active_item == index)
563 gold_text->draw_align((pitem.get_input_with_symbol(true)).c_str(), x_pos + text_pos, y_pos, A_HMIDDLE, A_VMIDDLE, 2);
565 gold_text->draw_align((pitem.get_input_with_symbol(false)).c_str(), x_pos + text_pos, y_pos, A_HMIDDLE, A_VMIDDLE, 2);
568 gold_text->draw_align(pitem.input,
569 x_pos + text_pos, y_pos,
570 A_HMIDDLE, A_VMIDDLE, 2);
572 text_font->draw_align(pitem.text,
573 x_pos - (input_width + font_width)/2, y_pos,
574 A_HMIDDLE, A_VMIDDLE, shadow_size);
577 case MN_STRINGSELECT:
579 int list_pos_2 = list_width + font_width;
580 int list_pos = list_width/2;
581 int text_pos = (text_width + font_width)/2;
584 arrow_left->draw( x_pos - list_pos + text_pos - 17, y_pos - 8);
585 arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
587 /* Draw input background */
588 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
591 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
595 gold_text->draw_align(string_list_active(pitem.list),
596 x_pos + text_pos, y_pos,
597 A_HMIDDLE, A_VMIDDLE,2);
599 text_font->draw_align(pitem.text,
600 x_pos - list_pos_2/2, y_pos,
601 A_HMIDDLE, A_VMIDDLE, shadow_size);
606 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
607 back->draw( x_pos + text_width/2 + font_width, y_pos - 8);
613 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
616 checkbox_checked->draw(
617 x_pos + (text_width+font_width)/2,
621 x_pos + (text_width+font_width)/2,
626 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
630 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
635 int Menu::get_width() const
637 /* The width of the menu has to be more than the width of the text
638 with the most characters */
640 for(unsigned int i = 0; i < item.size(); ++i)
642 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
646 if( item[i].kind == MN_TOGGLE)
651 return (menu_width * 16 + 24);
654 int Menu::get_height() const
656 return item.size() * 24;
659 /* Draw the current menu. */
663 int menu_height = get_height();
664 int menu_width = get_width();
666 /* Draw a transparent background */
667 fillrect(pos_x - menu_width/2,
668 pos_y - 24*item.size()/2 - 10,
669 menu_width,menu_height + 20,
672 for(unsigned int i = 0; i < item.size(); ++i)
674 draw_item(i, menu_width, menu_height);
679 Menu::get_item_by_id(int id)
681 for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i) {
687 static MenuItem dummyitem;
691 int Menu::get_active_item_id()
693 return item[active_item].id;
697 Menu::isToggled(int id)
699 return get_item_by_id(id).toggled;
702 /* Check for menu event */
704 Menu::event(SDL_Event& event)
710 key = event.key.keysym.sym;
713 keymod = SDL_GetModState();
716 /* If the current unicode character is an ASCII character,
718 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
720 ch[0] = event.key.keysym.unicode & 0x7F;
725 /* An International Character. */
728 if(item[active_item].kind == MN_CONTROLFIELD)
730 if(key == SDLK_ESCAPE)
735 *item[active_item].int_p = key;
736 menuaction = MENU_ACTION_DOWN;
743 case SDLK_UP: /* Menu Up */
744 menuaction = MENU_ACTION_UP;
746 case SDLK_DOWN: /* Menu Down */
747 menuaction = MENU_ACTION_DOWN;
749 case SDLK_LEFT: /* Menu Up */
750 menuaction = MENU_ACTION_LEFT;
752 case SDLK_RIGHT: /* Menu Down */
753 menuaction = MENU_ACTION_RIGHT;
756 if(item[active_item].kind == MN_TEXTFIELD)
758 menuaction = MENU_ACTION_INPUT;
762 case SDLK_RETURN: /* Menu Hit */
763 menuaction = MENU_ACTION_HIT;
767 menuaction = MENU_ACTION_REMOVE;
774 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
776 menuaction = MENU_ACTION_INPUT;
781 mn_input_char = '\0';
786 case SDL_JOYAXISMOTION:
787 if(event.jaxis.axis == joystick_keymap.y_axis)
789 if (event.jaxis.value > 1024)
790 menuaction = MENU_ACTION_DOWN;
791 else if (event.jaxis.value < -1024)
792 menuaction = MENU_ACTION_UP;
795 case SDL_JOYBUTTONDOWN:
796 menuaction = MENU_ACTION_HIT;
798 case SDL_MOUSEBUTTONDOWN:
801 if(x > pos_x - get_width()/2 &&
802 x < pos_x + get_width()/2 &&
803 y > pos_y - get_height()/2 &&
804 y < pos_y + get_height()/2)
806 menuaction = MENU_ACTION_HIT;
809 case SDL_MOUSEMOTION:
812 if(x > pos_x - get_width()/2 &&
813 x < pos_x + get_width()/2 &&
814 y > pos_y - get_height()/2 &&
815 y < pos_y + get_height()/2)
817 active_item = (y - (pos_y - get_height()/2)) / 24;
818 mouse_cursor->set_state(MC_LINK);
822 mouse_cursor->set_state(MC_NORMAL);