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"
43 Surface* checkbox_checked;
50 Menu* worldmap_menu = 0;
51 Menu* options_menu = 0;
52 Menu* options_keys_menu = 0;
53 Menu* options_joystick_menu = 0;
54 Menu* highscore_menu = 0;
55 Menu* load_game_menu = 0;
56 Menu* save_game_menu = 0;
57 Menu* contrib_menu = 0;
58 Menu* contrib_subset_menu = 0;
60 std::vector<Menu*> Menu::last_menus;
61 Menu* Menu::current_ = 0;
64 Menu::push_current(Menu* pmenu)
67 last_menus.push_back(current_);
70 current_->effect.start(500);
76 if (!last_menus.empty())
78 current_ = last_menus.back();
79 current_->effect.start(500);
81 last_menus.pop_back();
90 Menu::set_current(Menu* menu)
95 menu->effect.start(500);
100 /* Return a pointer to a new menu item */
102 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_)
104 MenuItem *pnew_item = new MenuItem;
106 pnew_item->kind = kind_;
107 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
108 strcpy(pnew_item->text, text_);
110 if(kind_ == MN_TOGGLE)
111 pnew_item->toggled = init_toggle_;
113 pnew_item->toggled = false;
115 pnew_item->target_menu = target_menu_;
116 pnew_item->input = (char*) malloc(sizeof(char));
117 pnew_item->input[0] = '\0';
119 if(kind_ == MN_STRINGSELECT)
121 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
122 string_list_init(pnew_item->list);
125 pnew_item->list = NULL;
128 pnew_item->int_p = int_p_;
134 MenuItem::change_text(const char *text_)
139 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
145 MenuItem::change_input(const char *text_)
150 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
151 strcpy(input, text_);
155 /* Set ControlField a key */
156 void Menu::get_controlfield_key_into_input(MenuItem *item)
161 strcpy(item->input, "Up cursor");
164 strcpy(item->input, "Down cursor");
167 strcpy(item->input, "Left cursor");
170 strcpy(item->input, "Right cursor");
173 strcpy(item->input, "Return");
176 strcpy(item->input, "Space");
179 strcpy(item->input, "Right Shift");
182 strcpy(item->input, "Left Shift");
185 strcpy(item->input, "Right Control");
188 strcpy(item->input, "Left Control");
191 strcpy(item->input, "Right Alt");
194 strcpy(item->input, "Left Alt");
197 strcpy(item->input, (char*)item->int_p);
202 /* Free a menu and all its items */
207 for(unsigned int i = 0; i < item.size(); ++i)
211 string_list_free(item[i].list);
220 menuaction = MENU_ACTION_NONE;
221 delete_character = 0;
222 mn_input_char = '\0';
226 has_backitem = false;
233 void Menu::set_pos(int x, int y, float rw, float rh)
235 pos_x = x + (int)((float)get_width() * rw);
236 pos_y = y + (int)((float)get_height() * rh);
240 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
245 if(id == -1 && item.size() == (unsigned)last_id)
251 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
254 /* Add an item to a menu */
256 Menu::additem(MenuItem* pmenu_item)
258 if(pmenu_item->kind == MN_BACK)
261 item.push_back(*pmenu_item);
271 /* Process actions done on the menu */
284 active_item = int(item.size())-1;
287 case MENU_ACTION_DOWN:
288 if(active_item < int(item.size())-1)
294 case MENU_ACTION_LEFT:
295 if(item[active_item].kind == MN_STRINGSELECT
296 && item[active_item].list->num_items != 0)
298 if(item[active_item].list->active_item > 0)
299 --item[active_item].list->active_item;
301 item[active_item].list->active_item = item[active_item].list->num_items-1;
305 case MENU_ACTION_RIGHT:
306 if(item[active_item].kind == MN_STRINGSELECT
307 && item[active_item].list->num_items != 0)
309 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
310 ++item[active_item].list->active_item;
312 item[active_item].list->active_item = 0;
316 case MENU_ACTION_HIT:
318 hit_item = active_item;
319 switch (item[active_item].kind)
322 if (item[active_item].target_menu != NULL)
323 Menu::push_current(item[active_item].target_menu);
329 item[active_item].toggled = !item[active_item].toggled;
335 Menu::set_current(0);
336 item[active_item].toggled = true;
348 case MENU_ACTION_REMOVE:
349 if(item[active_item].kind == MN_TEXTFIELD
350 || item[active_item].kind == MN_NUMFIELD)
352 if(item[active_item].input != NULL)
354 int i = strlen(item[active_item].input);
356 while(delete_character > 0) /* remove charactes */
358 item[active_item].input[i-1] = '\0';
365 case MENU_ACTION_INPUT:
366 if(item[active_item].kind == MN_TEXTFIELD
367 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
369 if(item[active_item].input != NULL)
371 int i = strlen(item[active_item].input);
372 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
373 item[active_item].input[i] = mn_input_char;
374 item[active_item].input[i+1] = '\0';
378 item[active_item].input = (char*) malloc(2*sizeof(char));
379 item[active_item].input[0] = mn_input_char;
380 item[active_item].input[1] = '\0';
384 case MENU_ACTION_NONE:
389 MenuItem& new_item = item[active_item];
390 if(new_item.kind == MN_DEACTIVE
391 || new_item.kind == MN_LABEL
392 || new_item.kind == MN_HL)
394 // Skip the horzontal line item
395 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
396 menuaction = MENU_ACTION_DOWN;
402 menuaction = MENU_ACTION_NONE;
409 return item[hit_item].id;
415 Menu::draw_item(int index, // Position of the current item in the menu
419 MenuItem& pitem = item[index];
422 int effect_offset = 0;
427 effect_time = effect.get_left() / 4;
429 effect_offset = (index % 2) ? effect_time : -effect_time;
433 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
435 int text_width = strlen(pitem.text) * font_width;
436 int input_width = strlen(pitem.input) * font_width;
437 int list_width = strlen(string_list_active(pitem.list)) * font_width;
438 Text* text_font = white_text;
441 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
443 if(index == active_item)
446 text_font = blue_text;
453 black_text->draw_align(pitem.text,
455 A_HMIDDLE, A_VMIDDLE, 2);
461 int x = pos_x - menu_width/2;
462 int y = y_pos - 12 - effect_offset;
463 /* Draw a horizontal line with a little 3d effect */
474 white_big_text->draw_align(pitem.text,
476 A_HMIDDLE, A_VMIDDLE, 2);
481 case MN_CONTROLFIELD:
483 int input_pos = input_width/2;
484 int text_pos = (text_width + font_width)/2;
486 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
487 input_width + font_width + 2, 20,
489 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
490 input_width + font_width, 18,
493 if(pitem.kind == MN_CONTROLFIELD)
494 get_controlfield_key_into_input(&pitem);
496 gold_text->draw_align(pitem.input,
497 x_pos + text_pos, y_pos,
498 A_HMIDDLE, A_VMIDDLE, 2);
500 text_font->draw_align(pitem.text,
501 x_pos - (input_width + font_width)/2, y_pos,
502 A_HMIDDLE, A_VMIDDLE, shadow_size);
505 case MN_STRINGSELECT:
507 int list_pos_2 = list_width + font_width;
508 int list_pos = list_width/2;
509 int text_pos = (text_width + font_width)/2;
512 arrow_left->draw( x_pos - list_pos + text_pos - 17, y_pos - 8);
513 arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
515 /* Draw input background */
516 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
519 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
523 gold_text->draw_align(string_list_active(pitem.list),
524 x_pos + text_pos, y_pos,
525 A_HMIDDLE, A_VMIDDLE,2);
527 text_font->draw_align(pitem.text,
528 x_pos - list_pos_2/2, y_pos,
529 A_HMIDDLE, A_VMIDDLE, shadow_size);
534 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
535 back->draw( x_pos + text_width/2 + font_width, y_pos - 8);
541 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
544 checkbox_checked->draw(
545 x_pos + (text_width+font_width)/2,
549 x_pos + (text_width+font_width)/2,
554 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
558 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
563 int Menu::get_width() const
565 /* The width of the menu has to be more than the width of the text
566 with the most characters */
568 for(unsigned int i = 0; i < item.size(); ++i)
570 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
574 if( item[i].kind == MN_TOGGLE)
579 return (menu_width * 16 + 24);
582 int Menu::get_height() const
584 return item.size() * 24;
587 /* Draw the current menu. */
591 int menu_height = get_height();
592 int menu_width = get_width();
594 /* Draw a transparent background */
595 fillrect(pos_x - menu_width/2,
596 pos_y - 24*item.size()/2 - 10,
597 menu_width,menu_height + 20,
600 for(unsigned int i = 0; i < item.size(); ++i)
602 draw_item(i, menu_width, menu_height);
607 Menu::get_item_by_id(int id)
609 for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i) {
615 static MenuItem dummyitem;
620 Menu::isToggled(int id)
622 return get_item_by_id(id).toggled;
625 /* Check for menu event */
627 Menu::event(SDL_Event& event)
633 key = event.key.keysym.sym;
636 keymod = SDL_GetModState();
639 /* If the current unicode character is an ASCII character,
641 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
643 ch[0] = event.key.keysym.unicode & 0x7F;
648 /* An International Character. */
651 if(item[active_item].kind == MN_CONTROLFIELD)
653 if(key == SDLK_ESCAPE)
658 *item[active_item].int_p = key;
659 menuaction = MENU_ACTION_DOWN;
666 case SDLK_UP: /* Menu Up */
667 menuaction = MENU_ACTION_UP;
669 case SDLK_DOWN: /* Menu Down */
670 menuaction = MENU_ACTION_DOWN;
672 case SDLK_LEFT: /* Menu Up */
673 menuaction = MENU_ACTION_LEFT;
675 case SDLK_RIGHT: /* Menu Down */
676 menuaction = MENU_ACTION_RIGHT;
679 if(item[active_item].kind == MN_TEXTFIELD)
681 menuaction = MENU_ACTION_INPUT;
685 case SDLK_RETURN: /* Menu Hit */
686 menuaction = MENU_ACTION_HIT;
690 menuaction = MENU_ACTION_REMOVE;
697 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
699 menuaction = MENU_ACTION_INPUT;
704 mn_input_char = '\0';
709 case SDL_JOYAXISMOTION:
710 if(event.jaxis.axis == joystick_keymap.y_axis)
712 if (event.jaxis.value > 1024)
713 menuaction = MENU_ACTION_DOWN;
714 else if (event.jaxis.value < -1024)
715 menuaction = MENU_ACTION_UP;
718 case SDL_JOYBUTTONDOWN:
719 menuaction = MENU_ACTION_HIT;
721 case SDL_MOUSEBUTTONDOWN:
724 if(x > pos_x - get_width()/2 &&
725 x < pos_x + get_width()/2 &&
726 y > pos_y - get_height()/2 &&
727 y < pos_y + get_height()/2)
729 menuaction = MENU_ACTION_HIT;
732 case SDL_MOUSEMOTION:
735 if(x > pos_x - get_width()/2 &&
736 x < pos_x + get_width()/2 &&
737 y > pos_y - get_height()/2 &&
738 y < pos_y + get_height()/2)
740 active_item = (y - (pos_y - get_height()/2)) / 24;
741 mouse_cursor->set_state(MC_LINK);
745 mouse_cursor->set_state(MC_NORMAL);