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::stack<Menu*> Menu::last_menus;
59 Menu* Menu::current_ = 0;
62 Menu::set_current(Menu* menu)
66 if (last_menus.empty() || menu != last_menus.top())
67 last_menus.push(current_);
69 menu->effect.start(500);
75 /* Return a pointer to a new menu item */
77 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_)
79 MenuItem *pnew_item = new MenuItem;
81 pnew_item->kind = kind_;
82 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
83 strcpy(pnew_item->text, text_);
85 if(kind_ == MN_TOGGLE)
86 pnew_item->toggled = init_toggle_;
88 pnew_item->toggled = false;
90 pnew_item->target_menu = target_menu_;
91 pnew_item->input = (char*) malloc(sizeof(char));
92 pnew_item->input[0] = '\0';
94 if(kind_ == MN_STRINGSELECT)
96 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
97 string_list_init(pnew_item->list);
100 pnew_item->list = NULL;
105 MenuItem::change_text(const char *text_)
110 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
116 MenuItem::change_input(const char *text_)
121 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
122 strcpy(input, text_);
126 /* Free a menu and all its items */
131 for(unsigned int i = 0; i < item.size(); ++i)
135 string_list_free(item[i].list);
143 menuaction = MENU_ACTION_NONE;
144 delete_character = 0;
145 mn_input_char = '\0';
149 has_backitem = false;
155 void Menu::set_pos(int x, int y, float rw, float rh)
157 pos_x = x + (int)((float)width() * rw);
158 pos_y = y + (int)((float)height() * rh);
162 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_)
167 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_));
170 /* Add an item to a menu */
172 Menu::additem(MenuItem* pmenu_item)
174 if(pmenu_item->kind == MN_BACK)
177 item.push_back(*pmenu_item);
187 /* Process actions done on the menu */
199 active_item = int(item.size())-1;
202 case MENU_ACTION_DOWN:
203 if(active_item < int(item.size())-1)
209 case MENU_ACTION_LEFT:
210 if(item[active_item].kind == MN_STRINGSELECT
211 && item[active_item].list->num_items != 0)
213 if(item[active_item].list->active_item > 0)
214 --item[active_item].list->active_item;
216 item[active_item].list->active_item = item[active_item].list->num_items-1;
220 case MENU_ACTION_RIGHT:
221 if(item[active_item].kind == MN_STRINGSELECT
222 && item[active_item].list->num_items != 0)
224 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
225 ++item[active_item].list->active_item;
227 item[active_item].list->active_item = 0;
231 case MENU_ACTION_HIT:
233 switch (item[active_item].kind)
236 if (item[active_item].target_menu != NULL)
237 Menu::set_current(item[active_item].target_menu);
243 item[active_item].toggled = !item[active_item].toggled;
249 case MN_CONTROLFIELD:
250 item[active_item].toggled = true;
254 if (!last_menus.empty())
256 Menu::set_current(last_menus.top());
266 case MENU_ACTION_REMOVE:
267 if(item[active_item].kind == MN_TEXTFIELD
268 || item[active_item].kind == MN_NUMFIELD)
270 if(item[active_item].input != NULL)
272 int i = strlen(item[active_item].input);
274 while(delete_character > 0) /* remove charactes */
276 item[active_item].input[i-1] = '\0';
283 case MENU_ACTION_INPUT:
284 if(item[active_item].kind == MN_TEXTFIELD
285 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
287 if(item[active_item].input != NULL)
289 int i = strlen(item[active_item].input);
290 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
291 item[active_item].input[i] = mn_input_char;
292 item[active_item].input[i+1] = '\0';
296 item[active_item].input = (char*) malloc(2*sizeof(char));
297 item[active_item].input[0] = mn_input_char;
298 item[active_item].input[1] = '\0';
303 case MENU_ACTION_NONE:
308 MenuItem& new_item = item[active_item];
309 if(new_item.kind == MN_DEACTIVE
310 || new_item.kind == MN_LABEL
311 || new_item.kind == MN_HL)
313 // Skip the horzontal line item
314 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
315 menuaction = MENU_ACTION_DOWN;
321 menuaction = MENU_ACTION_NONE;
327 if (item.size() != 0)
329 if((item[active_item].kind == MN_ACTION
330 || item[active_item].kind == MN_TEXTFIELD
331 || item[active_item].kind == MN_NUMFIELD)
332 && item[active_item].toggled)
334 item[active_item].toggled = false;
335 Menu::set_current(0);
338 else if(item[active_item].kind == MN_TOGGLE || item[active_item].kind == MN_GOTO)
350 Menu::draw_item(int index, // Position of the current item in the menu
354 const MenuItem& pitem = item[index];
357 int effect_offset = 0;
362 effect_time = effect.get_left() / 4;
364 effect_offset = (index % 2) ? effect_time : -effect_time;
368 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
370 int text_width = strlen(pitem.text) * font_width;
371 int input_width = strlen(pitem.input) * font_width;
372 int list_width = strlen(string_list_active(pitem.list)) * font_width;
373 Text* text_font = white_text;
376 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
378 if(index == active_item)
381 text_font = blue_text;
388 black_text->draw_align(pitem.text,
390 A_HMIDDLE, A_VMIDDLE, 2);
396 int x = pos_x - menu_width/2;
397 int y = y_pos - 12 - effect_offset;
398 /* Draw a horizontal line with a little 3d effect */
409 white_big_text->draw_align(pitem.text,
411 A_HMIDDLE, A_VMIDDLE, 2);
416 case MN_CONTROLFIELD:
418 int input_pos = input_width/2;
419 int text_pos = (text_width + font_width)/2;
421 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
422 input_width + font_width + 2, 20,
424 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
425 input_width + font_width, 18,
428 gold_text->draw_align(pitem.input,
429 x_pos + text_pos, y_pos,
430 A_HMIDDLE, A_VMIDDLE, 2);
432 text_font->draw_align(pitem.text,
433 x_pos - (input_width + font_width)/2, y_pos,
434 A_HMIDDLE, A_VMIDDLE, shadow_size);
437 case MN_STRINGSELECT:
439 int list_pos_2 = list_width + font_width;
440 int list_pos = list_width/2;
441 int text_pos = (text_width + font_width)/2;
444 arrow_left->draw( x_pos - list_pos + text_pos - 17, y_pos - 8);
445 arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
447 /* Draw input background */
448 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
451 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
455 gold_text->draw_align(string_list_active(pitem.list),
456 x_pos + text_pos, y_pos,
457 A_HMIDDLE, A_VMIDDLE,2);
459 text_font->draw_align(pitem.text,
460 x_pos - list_pos_2/2, y_pos,
461 A_HMIDDLE, A_VMIDDLE, shadow_size);
466 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
467 back->draw( x_pos + text_width/2 + font_width, y_pos - 8);
473 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
476 checkbox_checked->draw(
477 x_pos + (text_width+font_width)/2,
481 x_pos + (text_width+font_width)/2,
486 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
490 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
497 /* The width of the menu has to be more than the width of the text
498 with the most characters */
500 for(unsigned int i = 0; i < item.size(); ++i)
502 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
506 if( item[i].kind == MN_TOGGLE)
511 return (menu_width * 16 + 24);
516 return item.size() * 24;
519 /* Draw the current menu. */
523 int menu_height = height();
524 int menu_width = width();
526 /* Draw a transparent background */
527 fillrect(pos_x - menu_width/2,
528 pos_y - 24*item.size()/2 - 10,
529 menu_width,menu_height + 20,
532 for(unsigned int i = 0; i < item.size(); ++i)
534 draw_item(i, menu_width, menu_height);
538 /* Check for menu event */
540 Menu::event(SDL_Event& event)
546 key = event.key.keysym.sym;
549 keymod = SDL_GetModState();
552 /* If the current unicode character is an ASCII character,
554 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
556 ch[0] = event.key.keysym.unicode & 0x7F;
561 /* An International Character. */
566 case SDLK_UP: /* Menu Up */
567 menuaction = MENU_ACTION_UP;
569 case SDLK_DOWN: /* Menu Down */
570 menuaction = MENU_ACTION_DOWN;
572 case SDLK_LEFT: /* Menu Up */
573 menuaction = MENU_ACTION_LEFT;
575 case SDLK_RIGHT: /* Menu Down */
576 menuaction = MENU_ACTION_RIGHT;
579 if(item[active_item].kind == MN_TEXTFIELD)
581 menuaction = MENU_ACTION_INPUT;
585 case SDLK_RETURN: /* Menu Hit */
586 menuaction = MENU_ACTION_HIT;
590 menuaction = MENU_ACTION_REMOVE;
596 if (has_backitem == true && !last_menus.empty())
598 Menu::set_current(last_menus.top());
602 Menu::set_current(0);
605 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
607 menuaction = MENU_ACTION_INPUT;
612 mn_input_char = '\0';
617 case SDL_JOYAXISMOTION:
618 if(event.jaxis.axis == JOY_Y)
620 if (event.jaxis.value > 1024)
621 menuaction = MENU_ACTION_DOWN;
622 else if (event.jaxis.value < -1024)
623 menuaction = MENU_ACTION_UP;
626 case SDL_JOYBUTTONDOWN:
627 menuaction = MENU_ACTION_HIT;
629 case SDL_MOUSEBUTTONDOWN:
632 if(x > pos_x - width()/2 &&
633 x < pos_x + width()/2 &&
634 y > pos_y - height()/2 &&
635 y < pos_y + height()/2)
637 menuaction = MENU_ACTION_HIT;
640 case SDL_MOUSEMOTION:
643 if(x > pos_x - width()/2 &&
644 x < pos_x + width()/2 &&
645 y > pos_y - height()/2 &&
646 y < pos_y + height()/2)
648 active_item = (y - (pos_y - height()/2)) / 24;
649 mouse_cursor->set_state(MC_LINK);
653 mouse_cursor->set_state(MC_NORMAL);