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_controls_menu = 0;
52 Menu* highscore_menu = 0;
53 Menu* load_game_menu = 0;
54 Menu* save_game_menu = 0;
55 Menu* contrib_menu = 0;
56 Menu* contrib_subset_menu = 0;
58 std::vector<Menu*> Menu::last_menus;
59 Menu* Menu::current_ = 0;
62 Menu::push_current(Menu* pmenu)
65 last_menus.push_back(current_);
68 current_->effect.start(500);
74 if (!last_menus.empty())
76 current_ = last_menus.back();
77 current_->effect.start(500);
79 last_menus.pop_back();
88 Menu::set_current(Menu* menu)
93 menu->effect.start(500);
98 /* Return a pointer to a new menu item */
100 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_)
102 MenuItem *pnew_item = new MenuItem;
104 pnew_item->kind = kind_;
105 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
106 strcpy(pnew_item->text, text_);
108 if(kind_ == MN_TOGGLE)
109 pnew_item->toggled = init_toggle_;
111 pnew_item->toggled = false;
113 pnew_item->target_menu = target_menu_;
114 pnew_item->input = (char*) malloc(sizeof(char));
115 pnew_item->input[0] = '\0';
117 if(kind_ == MN_STRINGSELECT)
119 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
120 string_list_init(pnew_item->list);
123 pnew_item->list = NULL;
128 MenuItem::change_text(const char *text_)
133 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
139 MenuItem::change_input(const char *text_)
144 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
145 strcpy(input, text_);
149 /* Free a menu and all its items */
154 for(unsigned int i = 0; i < item.size(); ++i)
158 string_list_free(item[i].list);
167 menuaction = MENU_ACTION_NONE;
168 delete_character = 0;
169 mn_input_char = '\0';
173 has_backitem = false;
179 void Menu::set_pos(int x, int y, float rw, float rh)
181 pos_x = x + (int)((float)get_width() * rw);
182 pos_y = y + (int)((float)get_height() * rh);
186 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_)
191 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_));
194 /* Add an item to a menu */
196 Menu::additem(MenuItem* pmenu_item)
198 if(pmenu_item->kind == MN_BACK)
201 item.push_back(*pmenu_item);
211 /* Process actions done on the menu */
224 active_item = int(item.size())-1;
227 case MENU_ACTION_DOWN:
228 if(active_item < int(item.size())-1)
234 case MENU_ACTION_LEFT:
235 if(item[active_item].kind == MN_STRINGSELECT
236 && item[active_item].list->num_items != 0)
238 if(item[active_item].list->active_item > 0)
239 --item[active_item].list->active_item;
241 item[active_item].list->active_item = item[active_item].list->num_items-1;
245 case MENU_ACTION_RIGHT:
246 if(item[active_item].kind == MN_STRINGSELECT
247 && item[active_item].list->num_items != 0)
249 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
250 ++item[active_item].list->active_item;
252 item[active_item].list->active_item = 0;
256 case MENU_ACTION_HIT:
258 hit_item = active_item;
259 switch (item[active_item].kind)
262 if (item[active_item].target_menu != NULL)
263 Menu::push_current(item[active_item].target_menu);
269 item[active_item].toggled = !item[active_item].toggled;
275 case MN_CONTROLFIELD:
276 Menu::set_current(0);
277 item[active_item].toggled = true;
289 case MENU_ACTION_REMOVE:
290 if(item[active_item].kind == MN_TEXTFIELD
291 || item[active_item].kind == MN_NUMFIELD)
293 if(item[active_item].input != NULL)
295 int i = strlen(item[active_item].input);
297 while(delete_character > 0) /* remove charactes */
299 item[active_item].input[i-1] = '\0';
306 case MENU_ACTION_INPUT:
307 if(item[active_item].kind == MN_TEXTFIELD
308 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
310 if(item[active_item].input != NULL)
312 int i = strlen(item[active_item].input);
313 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
314 item[active_item].input[i] = mn_input_char;
315 item[active_item].input[i+1] = '\0';
319 item[active_item].input = (char*) malloc(2*sizeof(char));
320 item[active_item].input[0] = mn_input_char;
321 item[active_item].input[1] = '\0';
326 case MENU_ACTION_NONE:
331 MenuItem& new_item = item[active_item];
332 if(new_item.kind == MN_DEACTIVE
333 || new_item.kind == MN_LABEL
334 || new_item.kind == MN_HL)
336 // Skip the horzontal line item
337 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
338 menuaction = MENU_ACTION_DOWN;
344 menuaction = MENU_ACTION_NONE;
352 if (item.size() != 0)
354 if((item[active_item].kind == MN_ACTION
355 || item[active_item].kind == MN_TEXTFIELD
356 || item[active_item].kind == MN_NUMFIELD)
357 && item[active_item].toggled)
359 item[active_item].toggled = false;
360 Menu::set_current(0);
363 else if(item[active_item].kind == MN_TOGGLE
364 || item[active_item].kind == MN_GOTO)
377 Menu::draw_item(int index, // Position of the current item in the menu
381 const MenuItem& pitem = item[index];
384 int effect_offset = 0;
389 effect_time = effect.get_left() / 4;
391 effect_offset = (index % 2) ? effect_time : -effect_time;
395 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
397 int text_width = strlen(pitem.text) * font_width;
398 int input_width = strlen(pitem.input) * font_width;
399 int list_width = strlen(string_list_active(pitem.list)) * font_width;
400 Text* text_font = white_text;
403 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
405 if(index == active_item)
408 text_font = blue_text;
415 black_text->draw_align(pitem.text,
417 A_HMIDDLE, A_VMIDDLE, 2);
423 int x = pos_x - menu_width/2;
424 int y = y_pos - 12 - effect_offset;
425 /* Draw a horizontal line with a little 3d effect */
436 white_big_text->draw_align(pitem.text,
438 A_HMIDDLE, A_VMIDDLE, 2);
443 case MN_CONTROLFIELD:
445 int input_pos = input_width/2;
446 int text_pos = (text_width + font_width)/2;
448 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
449 input_width + font_width + 2, 20,
451 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
452 input_width + font_width, 18,
455 gold_text->draw_align(pitem.input,
456 x_pos + text_pos, y_pos,
457 A_HMIDDLE, A_VMIDDLE, 2);
459 text_font->draw_align(pitem.text,
460 x_pos - (input_width + font_width)/2, y_pos,
461 A_HMIDDLE, A_VMIDDLE, shadow_size);
464 case MN_STRINGSELECT:
466 int list_pos_2 = list_width + font_width;
467 int list_pos = list_width/2;
468 int text_pos = (text_width + font_width)/2;
471 arrow_left->draw( x_pos - list_pos + text_pos - 17, y_pos - 8);
472 arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
474 /* Draw input background */
475 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
478 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
482 gold_text->draw_align(string_list_active(pitem.list),
483 x_pos + text_pos, y_pos,
484 A_HMIDDLE, A_VMIDDLE,2);
486 text_font->draw_align(pitem.text,
487 x_pos - list_pos_2/2, y_pos,
488 A_HMIDDLE, A_VMIDDLE, shadow_size);
493 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
494 back->draw( x_pos + text_width/2 + font_width, y_pos - 8);
500 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
503 checkbox_checked->draw(
504 x_pos + (text_width+font_width)/2,
508 x_pos + (text_width+font_width)/2,
513 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
517 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
522 int Menu::get_width() const
524 /* The width of the menu has to be more than the width of the text
525 with the most characters */
527 for(unsigned int i = 0; i < item.size(); ++i)
529 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
533 if( item[i].kind == MN_TOGGLE)
538 return (menu_width * 16 + 24);
541 int Menu::get_height() const
543 return item.size() * 24;
546 /* Draw the current menu. */
550 int menu_height = get_height();
551 int menu_width = get_width();
553 /* Draw a transparent background */
554 fillrect(pos_x - menu_width/2,
555 pos_y - 24*item.size()/2 - 10,
556 menu_width,menu_height + 20,
559 for(unsigned int i = 0; i < item.size(); ++i)
561 draw_item(i, menu_width, menu_height);
565 /* Check for menu event */
567 Menu::event(SDL_Event& event)
573 key = event.key.keysym.sym;
576 keymod = SDL_GetModState();
579 /* If the current unicode character is an ASCII character,
581 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
583 ch[0] = event.key.keysym.unicode & 0x7F;
588 /* An International Character. */
593 case SDLK_UP: /* Menu Up */
594 menuaction = MENU_ACTION_UP;
596 case SDLK_DOWN: /* Menu Down */
597 menuaction = MENU_ACTION_DOWN;
599 case SDLK_LEFT: /* Menu Up */
600 menuaction = MENU_ACTION_LEFT;
602 case SDLK_RIGHT: /* Menu Down */
603 menuaction = MENU_ACTION_RIGHT;
606 if(item[active_item].kind == MN_TEXTFIELD)
608 menuaction = MENU_ACTION_INPUT;
612 case SDLK_RETURN: /* Menu Hit */
613 menuaction = MENU_ACTION_HIT;
617 menuaction = MENU_ACTION_REMOVE;
624 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
626 menuaction = MENU_ACTION_INPUT;
631 mn_input_char = '\0';
636 case SDL_JOYAXISMOTION:
637 if(event.jaxis.axis == joystick_keymap.y_axis)
639 if (event.jaxis.value > 1024)
640 menuaction = MENU_ACTION_DOWN;
641 else if (event.jaxis.value < -1024)
642 menuaction = MENU_ACTION_UP;
645 case SDL_JOYBUTTONDOWN:
646 menuaction = MENU_ACTION_HIT;
648 case SDL_MOUSEBUTTONDOWN:
651 if(x > pos_x - get_width()/2 &&
652 x < pos_x + get_width()/2 &&
653 y > pos_y - get_height()/2 &&
654 y < pos_y + get_height()/2)
656 menuaction = MENU_ACTION_HIT;
659 case SDL_MOUSEMOTION:
662 if(x > pos_x - get_width()/2 &&
663 x < pos_x + get_width()/2 &&
664 y > pos_y - get_height()/2 &&
665 y < pos_y + get_height()/2)
667 active_item = (y - (pos_y - get_height()/2)) / 24;
668 mouse_cursor->set_state(MC_LINK);
672 mouse_cursor->set_state(MC_NORMAL);