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

« back to all changes in this revision

Viewing changes to src/start_screen.cxx

  • 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: start_screen.cxx,v 1.17 2003/04/12 15:52:17 torangan Exp $
 
2
//
 
3
//  Pingus - A free Lemmings clone
 
4
//  Copyright (C) 2002 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 <iostream>
 
21
#include "sprite.hxx"
 
22
#include "gui/gui_manager.hxx"
 
23
#include "gui/surface_button.hxx"
 
24
#include "gui/component.hxx"
 
25
#include "gui/screen_manager.hxx"
 
26
#include "my_gettext.hxx"
 
27
#include "game_session.hxx"
 
28
#include "globals.hxx"
 
29
#include "string_converter.hxx"
 
30
#include "system.hxx"
 
31
#include "fonts.hxx"
 
32
#include "plf.hxx"
 
33
#include "pingus_resource.hxx"
 
34
#include "start_screen.hxx"
 
35
#include "game_time.hxx"
 
36
#include "sound/sound.hxx"
 
37
 
 
38
class StartScreenComponent : public GUI::Component
 
39
{
 
40
private:
 
41
  PLFHandle plf;
 
42
  Sprite background;
 
43
  std::string time_str;
 
44
  std::string description;
 
45
  
 
46
public:
 
47
  StartScreenComponent(PLFHandle plf);
 
48
  void draw(GraphicContext& gc);
 
49
  virtual ~StartScreenComponent() {}
 
50
  
 
51
private:
 
52
  const std::string& format_description(int length);
 
53
};
 
54
 
 
55
class StartScreenOkButton : public GUI::SurfaceButton
 
56
{
 
57
private:
 
58
  StartScreen* parent;
 
59
public:
 
60
  StartScreenOkButton(StartScreen* p)
 
61
    : GUI::SurfaceButton(CL_Display::get_width()/2 + 225,
 
62
                         CL_Display::get_height()/2 + 125, 
 
63
                         ResDescriptor("start/ok", "core", ResDescriptor::RD_RESOURCE),
 
64
                         ResDescriptor("start/ok_clicked", "core", ResDescriptor::RD_RESOURCE),
 
65
                         ResDescriptor("start/ok_hover", "core", ResDescriptor::RD_RESOURCE)),
 
66
      parent(p)
 
67
  {
 
68
  }
 
69
 
 
70
  void draw(GraphicContext& gc) {
 
71
    SurfaceButton::draw(gc);
 
72
    gc.print_center(Fonts::chalk_normal, x_pos + 32, y_pos - 17, _("Ok"));
 
73
  }
 
74
 
 
75
  void on_click() 
 
76
  {
 
77
    PingusSound::play_sound("yipee");
 
78
    parent->start_game();
 
79
  }
 
80
 
 
81
 
 
82
  void on_pointer_enter()
 
83
  {
 
84
    SurfaceButton::on_pointer_enter();
 
85
    PingusSound::play_sound ("tick");
 
86
  }
 
87
};
 
88
 
 
89
 
 
90
class StartScreenAbortButton
 
91
  : public GUI::SurfaceButton
 
92
{
 
93
private:
 
94
  StartScreen* parent;
 
95
public:
 
96
  StartScreenAbortButton(StartScreen* p)
 
97
    : GUI::SurfaceButton(CL_Display::get_width()/2 - 278,
 
98
                         CL_Display::get_height()/2 + 144, 
 
99
                         ResDescriptor("start/back", "core", ResDescriptor::RD_RESOURCE),
 
100
                         ResDescriptor("start/back_clicked", "core", ResDescriptor::RD_RESOURCE),
 
101
                         ResDescriptor("start/back_hover", "core", ResDescriptor::RD_RESOURCE)),
 
102
      parent(p)
 
103
  {
 
104
  }
 
105
 
 
106
  void draw(GraphicContext& gc) {
 
107
    SurfaceButton::draw(gc);
 
108
    gc.print_center(Fonts::chalk_normal, x_pos + 55, y_pos, _("Abort"));
 
109
  }
 
110
 
 
111
  void on_click() {
 
112
    parent->cancel_game();
 
113
  }
 
114
 
 
115
  void on_pointer_enter()
 
116
  {
 
117
    SurfaceButton::on_pointer_enter();
 
118
    PingusSound::play_sound ("tick");
 
119
  }
 
120
};
 
121
 
 
122
StartScreen::~StartScreen()
 
123
{
 
124
  
 
125
}
 
126
 
 
127
StartScreenComponent::StartScreenComponent(PLFHandle p)
 
128
  : plf(p)
 
129
{
 
130
  background = Sprite("menu/startscreenbg", "core");
 
131
  background.set_align_center();
 
132
  time_str = GameTime::ticks_to_realtime_string(plf->get_time());
 
133
}
 
134
 
 
135
void
 
136
StartScreenComponent::draw(GraphicContext& gc)
 
137
{
 
138
  //gc.clear(0,0,0);
 
139
  background.put_screen(CL_Display::get_width()/2,CL_Display::get_height()/2);
 
140
  
 
141
  int left_x  = CL_Display::get_width()/2 - 120;
 
142
  int right_x = CL_Display::get_width()/2 + 120;
 
143
  int y = CL_Display::get_height()/2 + 10;
 
144
 
 
145
  gc.print_center(Fonts::chalk_large, 
 
146
                  gc.get_width()/2, 
 
147
                  CL_Display::get_height()/2 - 200,
 
148
                  System::translate(plf->get_levelname()));
 
149
 
 
150
  gc.print_left(Fonts::chalk_normal,
 
151
                CL_Display::get_width()/2 - 290,
 
152
                CL_Display::get_height()/2 - 140,
 
153
                format_description(800 - 230));
 
154
 
 
155
  gc.print_left (Fonts::chalk_normal, left_x,  y, _("Number of Pingus: "));
 
156
  gc.print_right(Fonts::chalk_normal, right_x, y, to_string(plf->get_pingus()));
 
157
 
 
158
  gc.print_left (Fonts::chalk_normal, left_x,  y += 30, _("Number to Save: "));
 
159
  gc.print_right(Fonts::chalk_normal, right_x, y, to_string(plf->get_number_to_save()));
 
160
 
 
161
  gc.print_left (Fonts::chalk_normal, left_x,  y += 30, _("Time: "));
 
162
  gc.print_right(Fonts::chalk_normal, right_x, y, time_str);
 
163
  
 
164
  gc.print_left (Fonts::chalk_normal, left_x,  y += 30, _("Difficulty:"));
 
165
  gc.print_right(Fonts::chalk_normal, right_x, y, to_string(plf->get_difficulty()) + "/100");
 
166
 
 
167
  /*for (int i = 0; plf->get_difficulty())
 
168
    {
 
169
    }*/
 
170
 
 
171
  gc.print_center(Fonts::chalk_small, CL_Display::get_width()/2,
 
172
                  CL_Display::get_height()/2 + 270, _("Author: ") + plf->get_author());
 
173
 
 
174
  if (maintainer_mode)
 
175
    gc.print_left(Fonts::chalk_small, 110, 430, _("Filename: ") + plf->get_resname());
 
176
    
 
177
  CL_System::sleep(30);
 
178
}
 
179
 
 
180
const std::string&
 
181
StartScreenComponent::format_description(int length)
 
182
{
 
183
  if (description != "")
 
184
    return description;
 
185
    
 
186
  description = System::translate(plf->get_description());
 
187
  
 
188
  if (description == "")
 
189
    return description;
 
190
 
 
191
  std::string::size_type pos = 0;
 
192
  while ((pos = description.find('\t', pos)) != std::string::npos)
 
193
    description.replace(pos, 1, 1, ' ');
 
194
  
 
195
  pos = 0;
 
196
  while ((pos = description.find("  ", pos)) != std::string::npos)
 
197
    description.replace(pos, 2, 1, ' ');
 
198
 
 
199
  pos = 0;  
 
200
  while ((pos = description.find('\n', pos)) != std::string::npos)
 
201
    {
 
202
      if (pos < description.length() && description[pos + 1] == '\n')   // double enter marks paragraph
 
203
        {
 
204
          description.replace(pos, 2, 1, '\n');                         // replace the two \n by one
 
205
        }
 
206
      else if (pos < description.length() - 1 && description[pos + 1] == ' ' && description[pos + 2] == '\n')
 
207
        {
 
208
            description.replace(pos, 3, 1, '\n');                       // whitespace between the two \n doesn't matter
 
209
        }
 
210
      else
 
211
        {
 
212
          description.replace(pos, 1, 1, ' ');
 
213
          continue;                                                     // no \n here anymore, so continue searching
 
214
        }
 
215
 
 
216
      if (pos && description[pos - 1] == ' ')
 
217
        description.replace(pos - 1, 2, 1, '\n');                       // no whitespace in front
 
218
        
 
219
      if (pos < description.length() && description[pos + 1] == ' ')
 
220
         description.replace(pos, 2, 1, '\n');                          // no whitespace behind
 
221
        
 
222
      ++pos;                                                            // we don't want to find it again
 
223
    }
 
224
        
 
225
  pos = 0;
 
226
  while ((pos = description.find("  ", pos)) != std::string::npos)
 
227
    description.replace(pos, 2, 1, ' ');
 
228
 
 
229
 
 
230
  int start_pos      = 0;
 
231
  int previous_space = 0;
 
232
  pos = 0;
 
233
  while ((pos = description.find(' ', pos + 1)) != std::string::npos)
 
234
    {
 
235
      if (Fonts::chalk_normal->get_text_width(description.substr(start_pos, pos - start_pos)) > length)
 
236
        {
 
237
          description[previous_space] = '\n';
 
238
          start_pos = previous_space + 1;
 
239
        }
 
240
      else if (Fonts::chalk_normal->get_text_width(description.substr(start_pos, description.length())) <= length)
 
241
        break;
 
242
 
 
243
      previous_space = pos;
 
244
    }
 
245
    
 
246
  return description;
 
247
}
 
248
 
 
249
 
 
250
StartScreen::StartScreen(PLFHandle arg_plf)
 
251
  : plf(arg_plf)
 
252
{
 
253
  StartScreenComponent* comp = new StartScreenComponent(plf);
 
254
  gui_manager->add(comp);
 
255
  gui_manager->add(new StartScreenOkButton(this));
 
256
  gui_manager->add(new StartScreenAbortButton(this));
 
257
}
 
258
 
 
259
void
 
260
StartScreen::on_fast_forward_press()
 
261
{
 
262
  start_game();
 
263
}
 
264
 
 
265
void
 
266
StartScreen::on_pause_press ()
 
267
{
 
268
  start_game();
 
269
}
 
270
 
 
271
void
 
272
StartScreen::on_escape_press()
 
273
{
 
274
  cancel_game();
 
275
}
 
276
 
 
277
void
 
278
StartScreen::start_game()
 
279
{
 
280
  PingusGameSession* game_session = new PingusGameSession(plf, true);
 
281
  ScreenManager::instance()->replace_screen(game_session, true);
 
282
}
 
283
 
 
284
void
 
285
StartScreen::cancel_game()
 
286
{
 
287
  ScreenManager::instance()->pop_screen();
 
288
}
 
289
 
 
290
/* EOF */