~ubuntu-branches/ubuntu/precise/pingus/precise

« back to all changes in this revision

Viewing changes to src/OptionMenu.cc

  • Committer: Bazaar Package Importer
  • Author(s): Raphael Goulais
  • Date: 2004-08-09 10:26:00 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040809102600-lg2q9lfars0q1p42
Tags: 0.6.0-8
Applied patch from Andreas Jochens (Closes: #263992)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//  $Id: OptionMenu.cc,v 1.40 2002/01/14 23:37:59 grumbel Exp $
2
 
//
3
 
//  Pingus - A free Lemmings clone
4
 
//  Copyright (C) 1999 Ingo Ruhnke <grumbel@gmx.de>
5
 
//
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.
10
 
//
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.
15
 
//
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.
19
 
 
20
 
#include <cstdio>
21
 
 
22
 
#include "globals.hh"
23
 
#include "PingusResource.hh"
24
 
#include "Display.hh"
25
 
#include "OptionMenu.hh"
26
 
#include "StringConverter.hh"
27
 
#include "PingusMenuManager.hh"
28
 
 
29
 
/* Headers needed for i18n / gettext */
30
 
#include <clocale>
31
 
#include <config.h>
32
 
#include "my_gettext.hh"
33
 
 
34
 
 
35
 
// Define the global option menu
36
 
OptionMenu   option_menu (0);
37
 
 
38
 
// ----- OptionEntry -----
39
 
 
40
 
OptionEntry::OptionEntry(std::string s, bool* v, int x, int y)
41
 
{
42
 
  font = PingusResource::load_font("Fonts/smallfont_h", "fonts");
43
 
  str = s;
44
 
  value_bool = v;
45
 
  value_int = 0;
46
 
  value_str = 0;
47
 
  x_pos = x;
48
 
  y_pos = y;
49
 
}
50
 
 
51
 
OptionEntry::OptionEntry(std::string s, std::string* v, int x, int y)
52
 
{
53
 
  font = PingusResource::load_font("Fonts/smallfont_h", "fonts");
54
 
  str = s;
55
 
  value_str = v;
56
 
  value_int = 0;
57
 
  value_bool = 0;
58
 
  x_pos = x;
59
 
  y_pos = y;
60
 
}
61
 
 
62
 
OptionEntry::OptionEntry(std::string s, int* v, int x, int y)
63
 
{
64
 
  font = PingusResource::load_font("Fonts/smallfont_h", "fonts");
65
 
  str = s;
66
 
  value_str = 0;
67
 
  value_int = v;
68
 
  value_bool = 0;
69
 
  x_pos = x;
70
 
  y_pos = y;
71
 
}
72
 
 
73
 
void
74
 
OptionEntry::draw()
75
 
{
76
 
  if (!mouse_over()) {
77
 
    CL_Display::fill_rect(x_pos - 7, y_pos - 1,
78
 
                          x_pos + CL_Display::get_width() - 35, y_pos + 11,
79
 
                          0.0, 0.0, 0.0, 1.0);
80
 
  } else {
81
 
    CL_Display::fill_rect(x_pos - 7, y_pos - 1,
82
 
                          x_pos + CL_Display::get_width() - 35, y_pos + 11,
83
 
                          1.0f, 0.0f, 0.0f, 0.4f);
84
 
  }
85
 
  font->print_left(x_pos, y_pos, str.c_str());
86
 
 
87
 
  if (value_bool) {
88
 
    if (*value_bool) {
89
 
      font->print_right(x_pos + CL_Display::get_width() - 35, y_pos, _("on"));
90
 
    } else {
91
 
      font->print_right(x_pos + CL_Display::get_width() - 35, y_pos, _("off"));
92
 
    }
93
 
  } else if (value_str) {
94
 
    font->print_right(x_pos + CL_Display::get_width() - 35, y_pos, value_str->c_str());    
95
 
  } else if (value_int) {
96
 
 
97
 
    font->print_right(x_pos + CL_Display::get_width() - 35, y_pos, to_string (*value_int).c_str ());
98
 
  }
99
 
}
100
 
 
101
 
void
102
 
OptionEntry::toggle()
103
 
{
104
 
  if (value_bool) {
105
 
    *(value_bool) = !*(value_bool);
106
 
  } else if (value_str) {
107
 
    std::cout << _("Not implemented") << std::endl;
108
 
  } else if (value_int) {
109
 
    ++(*value_int);
110
 
  }
111
 
}
112
 
 
113
 
void
114
 
OptionEntry::rtoggle()
115
 
{
116
 
  if (value_bool) {
117
 
    *(value_bool) = !*(value_bool);
118
 
  } else if (value_str) {
119
 
    std::cout << _("Not implemented") << std::endl;
120
 
  } else if (value_int) {
121
 
    --(*value_int);    
122
 
  }
123
 
}
124
 
 
125
 
bool
126
 
OptionEntry::mouse_over()
127
 
{
128
 
  if (CL_Mouse::get_x() > x_pos && CL_Mouse::get_x() < x_pos + CL_Display::get_width() - 30
129
 
      && CL_Mouse::get_y() > y_pos && CL_Mouse::get_y() < y_pos + 15) {
130
 
    return true;
131
 
  } else {
132
 
    return false;
133
 
  }
134
 
}
135
 
 
136
 
void
137
 
OptionMenu::Event::on_button_press(CL_InputDevice *device, const CL_Key &key)
138
 
{
139
 
}
140
 
 
141
 
void
142
 
OptionMenu::Event::on_button_release(CL_InputDevice *device, const CL_Key &key)
143
 
{
144
 
  if (device == CL_Input::keyboards[0])
145
 
    {
146
 
      switch(key.id)
147
 
        {
148
 
        case CL_KEY_ESCAPE:
149
 
          option_menu->quit = true;
150
 
          break;
151
 
        default:
152
 
          if (verbose) std::cout << _("OptionMenu::Event: Unknown key released: id=") << key.id << std::endl;
153
 
          break;
154
 
        } 
155
 
    }
156
 
  else if (device == CL_Input::pointers[0])
157
 
    {
158
 
      switch(key.id)
159
 
        {
160
 
        case CL_MOUSE_LEFTBUTTON:
161
 
          if (verbose) std::cout << _("Button 0 pressed") << std::endl;
162
 
          break;
163
 
        case CL_MOUSE_MIDDLEBUTTON:
164
 
          option_menu->quit = true;
165
 
          if (verbose) std::cout << _("Button 1 pressed") << std::endl;
166
 
          break;
167
 
        case CL_MOUSE_RIGHTBUTTON:
168
 
          if (verbose) std::cout << _("Button 2 pressed") << std::endl;
169
 
          break;
170
 
        default:
171
 
          if (verbose) std::cout << _("OptionMenu::Event: Unknown mouse button released: id=") << key.id << std::endl;
172
 
          break;  
173
 
        }
174
 
    }
175
 
}
176
 
 
177
 
// ----- OptionMenu -----
178
 
 
179
 
OptionMenu::OptionMenu(PingusMenuManager* m)
180
 
  : PingusSubMenu (m)
181
 
{
182
 
  quit = false;
183
 
  is_init = false;
184
 
 
185
 
  event = new Event;
186
 
  event->option_menu = this;
187
 
}
188
 
 
189
 
OptionMenu::~OptionMenu()
190
 
{
191
 
  delete event;
192
 
}
193
 
 
194
 
void
195
 
OptionMenu::init() 
196
 
{
197
 
  if (is_init)
198
 
    return;
199
 
  //if (!preload_data)
200
 
    //loading_screen.draw();
201
 
 
202
 
  font       = PingusResource::load_font("Fonts/smallfont_h",  "fonts");
203
 
  title_font = PingusResource::load_font("Fonts/pingus", "fonts");
204
 
  background =  PingusResource::load_surface("Textures/rocktile", "textures");
205
 
  back       = PingusResource::load_surface("buttons/back", "core");
206
 
 
207
 
  entry_x = 20;
208
 
  entry_y = 60;
209
 
  is_init = true;
210
 
 
211
 
  // Bool options
212
 
  add_entry(_("Gimmicks"),  &gimmicks_enabled);
213
 
  add_entry(_("Unlimited Actions"),  &unlimited_actions);
214
 
  add_entry(_("Print fps"), &print_fps);
215
 
  //  add_entry("Debug actions", &debug_actions);  
216
 
  add_entry(_("Cursor enabled"), &cursor_enabled);  
217
 
  add_entry(_("Fast Mode"), &fast_mode);
218
 
  //add_entry(_("Pingus Datadir"), &pingus_datadir);
219
 
  add_entry(_("Game Speed"), &game_speed);
220
 
  add_entry(_("Verbose"), &verbose);
221
 
  add_entry(_("Music"), &music_enabled);
222
 
  add_entry(_("Sound"), &sound_enabled);
223
 
  add_entry(_("Level Previews"), &previews_enabled);
224
 
  add_entry(_("Draw ColMap"), &draw_collision_map);
225
 
  //add_entry(_("Debug GameTime"), &debug_game_time);
226
 
  //add_entry(_("Debug Tiles"), &debug_tiles);
227
 
  add_entry(_("Auto Scrolling"), &auto_scrolling);
228
 
  add_entry(_("Action Help Strings"), &action_help);
229
 
}
230
 
 
231
 
void
232
 
OptionMenu::add_entry(std::string e, bool* v)
233
 
{
234
 
  entry.push_back(OptionEntry(e, v, entry_x, entry_y));
235
 
  entry_y += 20;
236
 
}
237
 
 
238
 
void 
239
 
OptionMenu::add_entry(std::string e, int* v)
240
 
{
241
 
  entry.push_back(OptionEntry(e, v, entry_x, entry_y));
242
 
  entry_y += 20;
243
 
}
244
 
 
245
 
void 
246
 
OptionMenu::add_entry(std::string e, std::string* v)
247
 
{
248
 
  entry.push_back(OptionEntry(e, v, entry_x, entry_y));
249
 
  entry_y += 20;
250
 
}
251
 
 
252
 
void
253
 
OptionMenu::draw_background()
254
 
{
255
 
  for(int y=0; y < CL_Display::get_height(); y += background.get_height()) {
256
 
    for(int x=0; x < CL_Display::get_width(); x += background.get_width()) {
257
 
      background.put_screen(x, y);
258
 
    }
259
 
  }
260
 
}
261
 
 
262
 
void
263
 
OptionMenu::draw()
264
 
{  
265
 
  //draw_background();
266
 
  
267
 
  title_font->print_center(CL_Display::get_width() / 2, 10, _("Pingus Option Menu"));
268
 
 
269
 
  for(EntryIter item = entry.begin(); item != entry.end(); ++item) {
270
 
    item->draw();
271
 
  }
272
 
 
273
 
  back.put_screen(0, 0);
274
 
  if (CL_Mouse::get_x() < (int)back.get_width()
275
 
      && CL_Mouse::get_y() < (int)back.get_height())
276
 
    CL_Display::fill_rect(0, 0, back.get_width(), back.get_height(),
277
 
                          1.0f, 1.0f, 1.0f, 0.3f);
278
 
}
279
 
 
280
 
void
281
 
OptionMenu::check_click()
282
 
{
283
 
  for(EntryIter item = entry.begin(); item != entry.end(); ++item) {
284
 
    if (item->mouse_over()) {
285
 
      item->toggle();
286
 
      break;
287
 
    }
288
 
  }
289
 
}
290
 
 
291
 
void
292
 
OptionMenu::update (float delta)
293
 
{
294
 
  EntryIter item = EntryIter();
295
 
  item = current_item();
296
 
      
297
 
  if (CL_Mouse::left_pressed()) 
298
 
    {
299
 
      if (CL_Mouse::get_x() < (int)back.get_width() &&
300
 
          CL_Mouse::get_y() < (int)back.get_height())
301
 
        {
302
 
          quit = true;
303
 
          if (manager) manager->set_menu (&manager->mainmenu);
304
 
        }
305
 
 
306
 
      if (item != EntryIter()) 
307
 
        item->toggle();
308
 
      draw();
309
 
      while(CL_Mouse::left_pressed())
310
 
        CL_System::keep_alive();
311
 
    }
312
 
 
313
 
  if (CL_Mouse::right_pressed()) 
314
 
    {
315
 
      if (item != EntryIter()) 
316
 
        item->rtoggle();
317
 
      draw();
318
 
      while(CL_Mouse::right_pressed())
319
 
        CL_System::keep_alive();
320
 
    }
321
 
}
322
 
 
323
 
void
324
 
OptionMenu::display()
325
 
{
326
 
  if (!is_init)
327
 
    init();
328
 
 
329
 
  //int temp_index = -2;
330
 
  //int index = -2;
331
 
  EntryIter item = EntryIter();
332
 
  EntryIter temp_item = EntryIter();
333
 
  
334
 
  while(CL_Mouse::left_pressed())
335
 
    CL_System::keep_alive();
336
 
 
337
 
  quit = false;
338
 
 
339
 
  //CL_Input::chain_button_press.push_back(event);
340
 
  //CL_Input::chain_button_release.push_back(event);
341
 
  
342
 
  CL_Input::sig_button_press.connect (event, &OptionMenu::Event::on_button_press);
343
 
  CL_Input::sig_button_release.connect (event, &OptionMenu::Event::on_button_release);
344
 
 
345
 
  Display::set_cursor(CL_MouseCursorProvider::load("Cursors/cursor",
346
 
                                                   PingusResource::get("game")));
347
 
  Display::show_cursor();
348
 
 
349
 
  draw();
350
 
 
351
 
  while(!quit)
352
 
    {
353
 
 
354
 
 
355
 
      {
356
 
        draw();
357
 
        temp_item = item;
358
 
      }
359
 
      update (0.1f);
360
 
    }
361
 
 
362
 
  Display::hide_cursor();
363
 
 
364
 
  //CL_Input::chain_button_release.remove(event);
365
 
  //CL_Input::chain_button_press.remove(event);
366
 
}
367
 
 
368
 
std::vector<OptionEntry>::iterator
369
 
OptionMenu::current_item()
370
 
{
371
 
  for(EntryIter item = entry.begin(); item != entry.end(); ++item) {
372
 
    if (item->mouse_over()) {
373
 
      return item;
374
 
    }
375
 
  }
376
 
  return std::vector<OptionEntry>::iterator();
377
 
}
378
 
 
379
 
/* EOF */