1 /***************************************************************************
2 menumanager.cpp - description
3 -------------------
4 begin : Tue Feb 29 2000
5 copyright : (C) 2000 by Michael Speck
6 email :
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18 #include <stdlib.h>
19 #include "menumanager.h"
20
MenuManager(SDL_Surface * bkgnd,int cx,int cy,SFnt * fnt,SFnt * enlgt_fnt,int n,char * vck)21 MenuManager::MenuManager(SDL_Surface *bkgnd, int cx, int cy, SFnt *fnt, SFnt *enlgt_fnt, int n, char *vck)
22 : backgnd(bkgnd), font(fnt), enlgt_font(enlgt_fnt), menu_num(n), x(cx), y(cy), val_ctrl_keys(vck)
23 {
24 int i;
25 //menu pointer
26 menus = 0;
27 menus = new Menu*[menu_num];
28 for (i = 0; i < menu_num; i++)
29 menus[i] = 0;
30 cur_menu = 0;
31 }
32
InsertMenu(int i,Menu * m)33 int MenuManager::InsertMenu(int i, Menu *m)
34 {
35 if (i >= 0 && i < menu_num && m != 0 && menus[i] == 0) {
36 menus[i] = m;
37 return 0;
38 }
39 return 1;
40 }
41
~MenuManager()42 MenuManager::~MenuManager()
43 {
44 for (int i = 0; i < menu_num; i++)
45 if (menus[i]) delete menus[i];
46 delete menus;
47 }
48
CurMenu()49 Menu* MenuManager::CurMenu()
50 {
51 return cur_menu;
52 }
53
GetMenu(int i)54 Menu* MenuManager::GetMenu(int i)
55 {
56 return menus[i];
57 }
58
SetCurMenu(int i)59 void MenuManager::SetCurMenu(int i)
60 {
61 if (i >= 0 && i < menu_num)
62 cur_menu = menus[i];
63 }
64
KeyEvent(SDL_KeyboardEvent * event)65 int MenuManager::KeyEvent(SDL_KeyboardEvent *event)
66 {
67 int code = event->keysym.sym;
68 //string?
69 if (cur_menu->CurItem()->Type() == ET_STRING) {
70 cur_menu->CurItem()->EditStr(code, event->keysym.unicode);
71 }
72 //waiting for a ctrl key?
73 if (cur_menu->CurItem()->Type() == ET_KEY &&
74 cur_menu->CurItem()->Pos() == 0 &&
75 val_ctrl_keys[code]) {
76 //check all menus for keys
77 for (int j = 0; j < menu_num; j++)
78 for (int i = 0; i < menus[j]->ItemNum(); i++)
79 if (menus[j]->Item(i)->Type() == ET_KEY && menus[j]->Item(i)->Pos() == code && menus[j]->Item(i) != cur_menu->CurItem())
80 return MR_CONTINUE;
81 //directly to KeyEvent
82 cur_menu->CurItem()->SetKey(code);
83 return MR_CONTINUE;
84 }
85 //enter pressed?
86 if (code == SDLK_RETURN) {
87 switch (cur_menu->CurItem()->Type()) {
88 case ET_SUBMENU:
89 cur_menu->CurItem()->SetUsed(1);
90 cur_menu->Hide(1);
91 cur_menu = cur_menu->CurItem()->Submenu();
92 cur_menu->Prepare(0);
93 cur_menu->Show(1);
94 return MR_CONTINUE;
95 case ET_ACTION:
96 return cur_menu->CurItem()->ActionId();
97 }
98 }
99 //items changed?
100 return cur_menu->KeyEvent(code);
101 }
102
Activate()103 void MenuManager::Activate()
104 {
105 MenuItem *item;
106 for (int i = 0; i < menu_num; i++)
107 for (int j = 0; j < menus[i]->ItemNum(); j++) {
108 if (menus[i]->Item(j) == 0) continue;
109 item = menus[i]->Item(j);
110 item->SetXY(x, y + j * font->lh + (font->lh / 2) - (menus[i]->ItemNum() * font->lh) / 2);
111 item->SetFont(font, enlgt_font);
112 item->SetBackgnd(backgnd);
113 item->ComputeStr();
114 }
115 }
116
Prepare()117 void MenuManager::Prepare()
118 {
119 SetCurMenu(0);
120 CurMenu()->Prepare(0);
121 }
122
ButtonEvent(SDL_MouseButtonEvent button)123 int MenuManager::ButtonEvent(SDL_MouseButtonEvent button)
124 {
125 switch (cur_menu->CurItem()->Type()) {
126 case ET_SEPARATOR:
127 case ET_STRING:
128 return MR_CONTINUE;
129 case ET_SUBMENU:
130 cur_menu->CurItem()->SetUsed(1);
131 cur_menu->Hide(1);
132 cur_menu = cur_menu->CurItem()->Submenu();
133 cur_menu->Prepare(0);
134 cur_menu->Show(1);
135 return MR_CONTINUE;
136 case ET_ACTION:
137 return cur_menu->CurItem()->ActionId();
138 default:
139 return cur_menu->CurItem()->ButtonEvent(button);
140 }
141 }
142