Debian Codegraph @

src/option/MenuOptions.cpp

raw
1 /*
2 * Copyright (C) 2004 Ivo Danihelka ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9 #include "MenuOptions.h"
10
11 #include "Path.h"
12 #include "HBox.h"
13 #include "VBox.h"
14 #include "WiPicture.h"
15 #include "WiSpace.h"
16 #include "WiButton.h"
17 #include "WiStatusBar.h"
18 #include "Slider.h"
19 #include "SelectLang.h"
20 #include "RadioBox.h"
21
22 #include "Font.h"
23 #include "Labels.h"
24 #include "SimpleMsg.h"
25 #include "OptionsInput.h"
26 #include "OptionAgent.h"
27 #include "SurfaceTool.h"
28 #include "StringMsg.h"
29 #include "UnknownMsgException.h"
30
31 //-----------------------------------------------------------------
32 MenuOptions::MenuOptions()
33 {
34 m_container = NULL;
35 m_statusBar = NULL;
36 m_needRefresh = false;
37 registerDrawable(this);
38 prepareMenu();
39
40 takeHandler(new OptionsInput(this));
41 }
42 //-----------------------------------------------------------------
43 MenuOptions::~MenuOptions()
44 {
45 delete m_container;
46 }
47 //-----------------------------------------------------------------
48 /**
49 * Display menu.
50 */
51 void
52 MenuOptions::own_initState()
53 {
54 registerWatcher("lang");
55 own_resumeState();
56 }
57 //-----------------------------------------------------------------
58 /**
59 * Display options menu.
60 * Menu will be centred on screen.
61 */
62 void
63 MenuOptions::own_resumeState()
64 {
65 int contentW = m_container->getW();
66 int contentH = m_container->getH();
67 OptionAgent *options = OptionAgent::agent();
68 int screenW = options->getAsInt("screen_width");
69 int screenH = options->getAsInt("screen_height");
70
71 m_container->setShift(
72 V2((screenW - contentW) / 2, (screenH - contentH) / 2));
73 }
74 //-----------------------------------------------------------------
75 /**
76 * Update statusbar.
77 */
78 void
79 MenuOptions::own_updateState()
80 {
81 if (m_needRefresh) {
82 m_needRefresh = false;
83 prepareMenu();
84 own_resumeState();
85 }
86 std::string tooltip = m_container->getTip(getInput()->getMouseLoc());
87 m_statusBar->setLabel(tooltip);
88 }
89 //-----------------------------------------------------------------
90 void
91 MenuOptions::prepareMenu()
92 {
93 if (m_container) {
94 deregisterDrawable(m_container);
95 delete m_container;
96 m_container = NULL;
97 }
98
99 Labels labels(Path::dataReadPath("script/labels.lua"));
100 IWidget *soundBox = createSoundPanel(labels);
101 IWidget *musicBox = createMusicPanel(labels);
102
103 VBox *vbox = new VBox();
104 vbox->addWidget(soundBox);
105 vbox->addWidget(new WiSpace(0, 10));
106 vbox->addWidget(musicBox);
107 vbox->addWidget(new WiSpace(0, 10));
108 vbox->addWidget(createLangPanel(labels));
109 vbox->addWidget(new WiSpace(0, 5));
110 vbox->addWidget(createSpeechPanel(labels));
111 vbox->addWidget(new WiSpace(0, 5));
112 vbox->addWidget(createSubtitlesPanel(labels));
113
114 IWidget *backButton = createBackButton(labels);
115 m_statusBar = createStatusBar(musicBox->getW() - backButton->getW());
116 HBox *backBox = new HBox();
117 backBox->addWidget(m_statusBar);
118 backBox->addWidget(backButton);
119
120 vbox->addWidget(backBox);
121 m_container = vbox;
122 registerDrawable(m_container);
123 }
124 //-----------------------------------------------------------------
125 IWidget *
126 MenuOptions::createSoundPanel(const Labels &labels)
127 {
128 HBox *soundBox = new HBox();
129 soundBox->addWidget(new WiPicture(
130 Path::dataReadPath("images/menu/volume_sound.png")));
131 soundBox->addWidget(new WiSpace(10, 0));
132 soundBox->addWidget(new Slider("volume_sound", 0, 100));
133 soundBox->setTip(labels.getLabel("menu_sound"));
134 return soundBox;
135 }
136 //-----------------------------------------------------------------
137 IWidget *
138 MenuOptions::createMusicPanel(const Labels &labels)
139 {
140 HBox *musicBox = new HBox();
141 musicBox->addWidget(new WiPicture(
142 Path::dataReadPath("images/menu/volume_music.png")));
143 musicBox->addWidget(new WiSpace(10, 0));
144 musicBox->addWidget(new Slider("volume_music", 0, 100));
145 musicBox->setTip(labels.getLabel("menu_music"));
146 return musicBox;
147 }
148 //-----------------------------------------------------------------
149 IWidget *
150 MenuOptions::createLangPanel(const Labels &labels)
151 {
152 HBox *langBox = new HBox();
153 langBox->addWidget(new WiPicture(
154 Path::dataReadPath("images/menu/lang.png")));
155 langBox->addWidget(new WiSpace(10, 0));
156 langBox->addWidget(new SelectLang("lang",
157 Path::dataReadPath("script/select_lang.lua")));
158 langBox->setTip(labels.getLabel("menu_lang"));
159 return langBox;
160 }
161 //-----------------------------------------------------------------
162 /**
163 * Creates menu to select 'speech'.
164 * NOTE: default speech is customized in "script/init.lua"
165 */
166 IWidget *
167 MenuOptions::createSpeechPanel(const Labels &labels)
168 {
169 HBox *speechBox = new HBox();
170 speechBox->addWidget(new WiPicture(
171 Path::dataReadPath("images/menu/speech.png")));
172 speechBox->addWidget(new WiSpace(10, 0));
173 speechBox->addWidget(new SelectLang("speech",
174 Path::dataReadPath("script/select_speech.lua")));
175 speechBox->setTip(labels.getLabel("menu_speech"));
176 return speechBox;
177 }
178 //-----------------------------------------------------------------
179 IWidget *
180 MenuOptions::createSubtitlesPanel(const Labels &labels)
181 {
182 HBox *chooseBox = new HBox();
183 chooseBox->addWidget(new WiPicture(
184 Path::dataReadPath("images/menu/subtitle.png")));
185 chooseBox->addWidget(new WiSpace(10, 0));
186 chooseBox->addWidget(new RadioBox("subtitles", "1",
187 Path::dataReadPath("images/menu/subtitles/yes.png")));
188 chooseBox->addWidget(new RadioBox("subtitles", "0",
189 Path::dataReadPath("images/menu/subtitles/no.png")));
190 chooseBox->setTip(labels.getLabel("menu_subtitles"));
191 return chooseBox;
192 }
193 //-----------------------------------------------------------------
194 IWidget *
195 MenuOptions::createBackButton(const Labels &labels)
196 {
197 IWidget *button = new WiButton(
198 new WiPicture(Path::dataReadPath("images/menu/back.png")),
199 new SimpleMsg(this, "quit"));
200 button->setTip(labels.getLabel("menu_back"));
201 return button;
202 }
203 //-----------------------------------------------------------------
204 WiStatusBar *
205 MenuOptions::createStatusBar(int width)
206 {
207 SDL_Color color = {0, 255, 0, 255};
208 Font *new_font = new Font(Path::dataReadPath("font/font_menu.ttf"), 16);
209 return new WiStatusBar(new_font, color, width);
210 }
211 //-----------------------------------------------------------------
212 /**
213 * Start level under pressed button.
214 * Start pedometer when level is solved already.
215 */
216 void
217 MenuOptions::mouseButton(const MouseStroke &stroke)
218 {
219 m_container->mouseButton(stroke);
220 }
221 //-----------------------------------------------------------------
222 void
223 MenuOptions::drawOn(SDL_Surface *screen)
224 {
225 SDL_Color gray = {0xf0, 0xf0, 0xf0, 129};
226 SurfaceTool::alphaFill(screen, NULL, gray);
227 }
228 //-----------------------------------------------------------------
229 /**
230 * Handle incoming message.
231 * Messages:
232 * - param_changed(lang) ... refresh menu
233 *
234 * @throws UnknownMsgException
235 */
236 void
237 MenuOptions::receiveString(const StringMsg *msg)
238 {
239 if (msg->equalsName("param_changed")) {
240 std::string param = msg->getValue();
241 if ("lang" == param) {
242 m_needRefresh = true;
243 }
244 else {
245 throw UnknownMsgException(msg);
246 }
247 }
248 else {
249 throw UnknownMsgException(msg);
250 }
251 }
252