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>
37 #include "leveleditor.h"
39 #include "high_scores.h"
42 Surface* checkbox_checked;
49 Menu* worldmap_menu = 0;
50 Menu* options_menu = 0;
51 Menu* options_keys_menu = 0;
52 Menu* options_joystick_menu = 0;
53 Menu* highscore_menu = 0;
54 Menu* load_game_menu = 0;
55 Menu* save_game_menu = 0;
56 Menu* contrib_menu = 0;
57 Menu* contrib_subset_menu = 0;
59 std::vector<Menu*> Menu::last_menus;
60 Menu* Menu::current_ = 0;
63 Menu::push_current(Menu* pmenu)
66 last_menus.push_back(current_);
69 current_->effect.start(500);
75 if (!last_menus.empty())
77 current_ = last_menus.back();
78 current_->effect.start(500);
80 last_menus.pop_back();
89 Menu::set_current(Menu* menu)
94 menu->effect.start(500);
99 /* Return a pointer to a new menu item */
101 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_)
103 MenuItem *pnew_item = new MenuItem;
105 pnew_item->kind = kind_;
106 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
107 strcpy(pnew_item->text, text_);
109 if(kind_ == MN_TOGGLE)
110 pnew_item->toggled = init_toggle_;
112 pnew_item->toggled = false;
114 pnew_item->target_menu = target_menu_;
115 pnew_item->input = (char*) malloc(sizeof(char));
116 pnew_item->input[0] = '\0';
118 if(kind_ == MN_STRINGSELECT)
120 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
121 string_list_init(pnew_item->list);
124 pnew_item->list = NULL;
127 pnew_item->int_p = int_p_;
133 MenuItem::change_text(const char *text_)
138 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
144 MenuItem::change_input(const char *text_)
149 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
150 strcpy(input, text_);
154 /* Set ControlField a key */
155 void Menu::get_controlfield_key_into_input(MenuItem *item)
160 strcpy(item->input, "Up cursor");
163 strcpy(item->input, "Down cursor");
166 strcpy(item->input, "Left cursor");
169 strcpy(item->input, "Right cursor");
172 strcpy(item->input, "Return");
175 strcpy(item->input, "Space");
178 strcpy(item->input, "Right Shift");
181 strcpy(item->input, "Left Shift");
184 strcpy(item->input, "Right Control");
187 strcpy(item->input, "Left Control");
190 strcpy(item->input, "Right Alt");
193 strcpy(item->input, "Left Alt");
196 strcpy(item->input, (char*)item->int_p);
201 /* Free a menu and all its items */
206 for(unsigned int i = 0; i < item.size(); ++i)
210 string_list_free(item[i].list);
219 menuaction = MENU_ACTION_NONE;
220 delete_character = 0;
221 mn_input_char = '\0';
225 has_backitem = false;
232 void Menu::set_pos(int x, int y, float rw, float rh)
234 pos_x = x + (int)((float)get_width() * rw);
235 pos_y = y + (int)((float)get_height() * rh);
239 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
244 if(id == -1 && item.size() == (unsigned)last_id)
250 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
253 /* Add an item to a menu */
255 Menu::additem(MenuItem* pmenu_item)
257 if(pmenu_item->kind == MN_BACK)
260 item.push_back(*pmenu_item);
270 /* Process actions done on the menu */
283 active_item = int(item.size())-1;
286 case MENU_ACTION_DOWN:
287 if(active_item < int(item.size())-1)
293 case MENU_ACTION_LEFT:
294 if(item[active_item].kind == MN_STRINGSELECT
295 && item[active_item].list->num_items != 0)
297 if(item[active_item].list->active_item > 0)
298 --item[active_item].list->active_item;
300 item[active_item].list->active_item = item[active_item].list->num_items-1;
304 case MENU_ACTION_RIGHT:
305 if(item[active_item].kind == MN_STRINGSELECT
306 && item[active_item].list->num_items != 0)
308 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
309 ++item[active_item].list->active_item;
311 item[active_item].list->active_item = 0;
315 case MENU_ACTION_HIT:
317 hit_item = active_item;
318 switch (item[active_item].kind)
321 if (item[active_item].target_menu != NULL)
322 Menu::push_current(item[active_item].target_menu);
328 item[active_item].toggled = !item[active_item].toggled;
334 Menu::set_current(0);
335 item[active_item].toggled = true;
347 case MENU_ACTION_REMOVE:
348 if(item[active_item].kind == MN_TEXTFIELD
349 || item[active_item].kind == MN_NUMFIELD)
351 if(item[active_item].input != NULL)
353 int i = strlen(item[active_item].input);
355 while(delete_character > 0) /* remove charactes */
357 item[active_item].input[i-1] = '\0';
364 case MENU_ACTION_INPUT:
365 if(item[active_item].kind == MN_TEXTFIELD
366 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
368 if(item[active_item].input != NULL)
370 int i = strlen(item[active_item].input);
371 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
372 item[active_item].input[i] = mn_input_char;
373 item[active_item].input[i+1] = '\0';
377 item[active_item].input = (char*) malloc(2*sizeof(char));
378 item[active_item].input[0] = mn_input_char;
379 item[active_item].input[1] = '\0';
383 case MENU_ACTION_NONE:
388 MenuItem& new_item = item[active_item];
389 if(new_item.kind == MN_DEACTIVE
390 || new_item.kind == MN_LABEL
391 || new_item.kind == MN_HL)
393 // Skip the horzontal line item
394 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
395 menuaction = MENU_ACTION_DOWN;
401 menuaction = MENU_ACTION_NONE;
408 return item[hit_item].id;
414 Menu::draw_item(int index, // Position of the current item in the menu
418 MenuItem& pitem = item[index];
421 int effect_offset = 0;
426 effect_time = effect.get_left() / 4;
428 effect_offset = (index % 2) ? effect_time : -effect_time;
432 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
434 int text_width = strlen(pitem.text) * font_width;
435 int input_width = strlen(pitem.input) * font_width;
436 int list_width = strlen(string_list_active(pitem.list)) * font_width;
437 Text* text_font = white_text;
440 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
442 if(index == active_item)
445 text_font = blue_text;
452 black_text->draw_align(pitem.text,
454 A_HMIDDLE, A_VMIDDLE, 2);
460 int x = pos_x - menu_width/2;
461 int y = y_pos - 12 - effect_offset;
462 /* Draw a horizontal line with a little 3d effect */
473 white_big_text->draw_align(pitem.text,
475 A_HMIDDLE, A_VMIDDLE, 2);
480 case MN_CONTROLFIELD:
482 int input_pos = input_width/2;
483 int text_pos = (text_width + font_width)/2;
485 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
486 input_width + font_width + 2, 20,
488 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
489 input_width + font_width, 18,
492 if(pitem.kind == MN_CONTROLFIELD)
493 get_controlfield_key_into_input(&pitem);
495 gold_text->draw_align(pitem.input,
496 x_pos + text_pos, y_pos,
497 A_HMIDDLE, A_VMIDDLE, 2);
499 text_font->draw_align(pitem.text,
500 x_pos - (input_width + font_width)/2, y_pos,
501 A_HMIDDLE, A_VMIDDLE, shadow_size);
504 case MN_STRINGSELECT:
506 int list_pos_2 = list_width + font_width;
507 int list_pos = list_width/2;
508 int text_pos = (text_width + font_width)/2;
511 arrow_left->draw( x_pos - list_pos + text_pos - 17, y_pos - 8);
512 arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
514 /* Draw input background */
515 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
518 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
522 gold_text->draw_align(string_list_active(pitem.list),
523 x_pos + text_pos, y_pos,
524 A_HMIDDLE, A_VMIDDLE,2);
526 text_font->draw_align(pitem.text,
527 x_pos - list_pos_2/2, y_pos,
528 A_HMIDDLE, A_VMIDDLE, shadow_size);
533 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
534 back->draw( x_pos + text_width/2 + font_width, y_pos - 8);
540 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
543 checkbox_checked->draw(
544 x_pos + (text_width+font_width)/2,
548 x_pos + (text_width+font_width)/2,
553 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
557 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
562 int Menu::get_width() const
564 /* The width of the menu has to be more than the width of the text
565 with the most characters */
567 for(unsigned int i = 0; i < item.size(); ++i)
569 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
573 if( item[i].kind == MN_TOGGLE)
578 return (menu_width * 16 + 24);
581 int Menu::get_height() const
583 return item.size() * 24;
586 /* Draw the current menu. */
590 int menu_height = get_height();
591 int menu_width = get_width();
593 /* Draw a transparent background */
594 fillrect(pos_x - menu_width/2,
595 pos_y - 24*item.size()/2 - 10,
596 menu_width,menu_height + 20,
599 for(unsigned int i = 0; i < item.size(); ++i)
601 draw_item(i, menu_width, menu_height);
606 Menu::get_item_by_id(int id)
608 for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i) {
614 static MenuItem dummyitem;
619 Menu::isToggled(int id)
621 return get_item_by_id(id).toggled;
624 /* Check for menu event */
626 Menu::event(SDL_Event& event)
632 key = event.key.keysym.sym;
635 keymod = SDL_GetModState();
638 /* If the current unicode character is an ASCII character,
640 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
642 ch[0] = event.key.keysym.unicode & 0x7F;
647 /* An International Character. */
650 if(item[active_item].kind == MN_CONTROLFIELD)
652 if(key == SDLK_ESCAPE)
657 *item[active_item].int_p = key;
658 menuaction = MENU_ACTION_DOWN;
665 case SDLK_UP: /* Menu Up */
666 menuaction = MENU_ACTION_UP;
668 case SDLK_DOWN: /* Menu Down */
669 menuaction = MENU_ACTION_DOWN;
671 case SDLK_LEFT: /* Menu Up */
672 menuaction = MENU_ACTION_LEFT;
674 case SDLK_RIGHT: /* Menu Down */
675 menuaction = MENU_ACTION_RIGHT;
678 if(item[active_item].kind == MN_TEXTFIELD)
680 menuaction = MENU_ACTION_INPUT;
684 case SDLK_RETURN: /* Menu Hit */
685 menuaction = MENU_ACTION_HIT;
689 menuaction = MENU_ACTION_REMOVE;
696 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
698 menuaction = MENU_ACTION_INPUT;
703 mn_input_char = '\0';
708 case SDL_JOYAXISMOTION:
709 if(event.jaxis.axis == joystick_keymap.y_axis)
711 if (event.jaxis.value > 1024)
712 menuaction = MENU_ACTION_DOWN;
713 else if (event.jaxis.value < -1024)
714 menuaction = MENU_ACTION_UP;
717 case SDL_JOYBUTTONDOWN:
718 menuaction = MENU_ACTION_HIT;
720 case SDL_MOUSEBUTTONDOWN:
723 if(x > pos_x - get_width()/2 &&
724 x < pos_x + get_width()/2 &&
725 y > pos_y - get_height()/2 &&
726 y < pos_y + get_height()/2)
728 menuaction = MENU_ACTION_HIT;
731 case SDL_MOUSEMOTION:
734 if(x > pos_x - get_width()/2 &&
735 x < pos_x + get_width()/2 &&
736 y > pos_y - get_height()/2 &&
737 y < pos_y + get_height()/2)
739 active_item = (y - (pos_y - get_height()/2)) / 24;
740 mouse_cursor->set_state(MC_LINK);
744 mouse_cursor->set_state(MC_NORMAL);