Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
modaldialog.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2003 Mike Byron
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 #include "vegastrike.h"
23 
24 #include "modaldialog.h"
25 
26 //Need these until we can read XML window descriptions.
27 #include "staticdisplay.h"
28 #include "newbutton.h"
29 #include "simplepicker.h"
30 #include "scroller.h"
31 #include "eventmanager.h"
32 
33 //The class that gets called when the window closes.
34 void ModalDialog::setCallback( ModalDialogCallback *cb, const std::string &id )
35 {
36  m_callback = cb;
37  m_callbackId = id;
38 }
39 
40 //Make everything happen.
41 void ModalDialog::run( void )
42 {
43  if ( window() )
44  window()->setModal( true );
46 }
47 
49 {
51  //One of our buttons was clicked. We're done.
52  m_deleteOnWindowClose = false; //Prevent deleting until we are done.
53  window()->close();
54  if (m_callback)
56  delete this;
57 }
58 
59 //Process a command from the window.
61 {
62  return WindowController::processWindowCommand( command, control );
63 }
64 
65 //CONSTRUCTOR
66 ModalDialog::ModalDialog() : m_callbackId()
67  , m_callback( NULL )
68  , m_result( 0 )
69 {}
70 
72 
73 //Hack for creating the alert window.
74 static void CreateControlsForAlertWindow( Window *window )
75 {
76  window->setSizeAndCenter( Size( 1, .8 ) );
77  window->setTexture( "basecomputer.png" );
78  window->setColor( SaturatedColor( 1, 0, 0, .1 ) );
79  window->setOutlineColor( SaturatedColor( .7, .7, .7 ) );
80  window->setOutlineWidth( 2.0 );
81 
82  //Title text display.
83  StaticDisplay *title = new StaticDisplay;
84  title->setRect( Rect( -.46, -.25, .92, .55 ) );
85  title->setMultiLine( true );
87  title->setTextColor( GUI_OPAQUE_WHITE() );
88  title->setColor( GUI_CLEAR );
89  title->setFont( Font( .07, BOLD_STROKE ) );
90  title->setId( "Title" );
91  //Put it on the window.
92  window->addControl( title );
93 
94  //OK button.
95  NewButton *ok = new NewButton;
96  ok->setRect( Rect( -.11, -.35, .22, .1 ) );
97  ok->setLabel( "OK" );
98  ok->setCommand( "Window::Close" );
99  ok->setColor( GFXColor( 1, 0, 0, .25 ) );
101  ok->setDownColor( GFXColor( 1, 0, 0, .6 ) );
103  ok->setHighlightColor( GFXColor( 1, 0, 0, .4 ) );
104  ok->setFont( Font( .08, BOLD_STROKE ) );
105  //Put the button on the window.
106  window->addControl( ok );
107 }
108 
109 //Hack for creating the Yes/No window.
110 static void CreateControlsForYesNoWindow( Window *window )
111 {
112  window->setSizeAndCenter( Size( 1, .8 ) );
113  window->setTexture( "basecomputer.png" );
114  window->setColor( SaturatedColor( 1, 0, 0, .1 ) );
115  window->setOutlineColor( SaturatedColor( .7, .7, .7 ) );
116  window->setOutlineWidth( 2.0 );
117 
118  //Title text display.
119  StaticDisplay *title = new StaticDisplay;
120  title->setRect( Rect( -.46, -.25, .92, .55 ) );
121  title->setMultiLine( true );
123  title->setTextColor( GUI_OPAQUE_WHITE() );
124  title->setColor( GUI_CLEAR );
125  title->setFont( Font( .07, BOLD_STROKE ) );
126  title->setId( "Title" );
127  //Put it on the window.
128  window->addControl( title );
129 
130  //Yes button.
131  NewButton *yes = new NewButton;
132  yes->setRect( Rect( -.27, -.35, .22, .1 ) );
133  yes->setLabel( "Yes" );
134  yes->setCommand( "Yes" );
135  yes->setColor( GFXColor( 1, 0, 0, .25 ) );
136  yes->setTextColor( GUI_OPAQUE_WHITE() );
137  yes->setDownColor( GFXColor( 1, 0, 0, .6 ) );
139  yes->setHighlightColor( GFXColor( 1, 0, 0, .4 ) );
140  yes->setFont( Font( .08, BOLD_STROKE ) );
141  //Put the button on the window.
142  window->addControl( yes );
143 
144  //No button.
145  NewButton *no = new NewButton;
146  no->setRect( Rect( .05, -.35, .22, .1 ) );
147  no->setLabel( "No" );
148  no->setCommand( "No" );
149  no->setColor( GFXColor( 1, 0, 0, .25 ) );
151  no->setDownColor( GFXColor( 1, 0, 0, .6 ) );
153  no->setHighlightColor( GFXColor( 1, 0, 0, .4 ) );
154  no->setFont( Font( .08, BOLD_STROKE ) );
155  //Put the button on the window.
156  window->addControl( no );
157 }
158 
159 //Hack for creating the List Question window.
161 {
162  window->setSizeAndCenter( Size( 1.2, 1.2 ) );
163  window->setTexture( "basecomputer.png" );
164  window->setColor( SaturatedColor( 0, 0, 1, .1 ) );
165  window->setOutlineColor( SaturatedColor( .7, .7, .7 ) );
166  window->setOutlineWidth( 2.0 );
167 
168  //Title text display.
169  StaticDisplay *title = new StaticDisplay;
170  title->setRect( Rect( -.57, .42, 1.14, .15 ) );
171  title->setMultiLine( true );
173  title->setTextColor( GUI_OPAQUE_WHITE() );
174  title->setColor( GUI_CLEAR );
175  title->setFont( Font( .08, BOLD_STROKE ) );
176  title->setId( "Title" );
177  //Put it on the window.
178  window->addControl( title );
179 
180  //OK button.
181  NewButton *ok = new NewButton;
182  ok->setRect( Rect( -.27, -.57, .22, .1 ) );
183  ok->setLabel( "OK" );
184  ok->setCommand( "OK" );
185  ok->setColor( GFXColor( 0, 0, 1, .25 ) );
187  ok->setDownColor( GFXColor( 0, 0, 1, .6 ) );
189  ok->setHighlightColor( GFXColor( 0, 0, 1, .4 ) );
190  ok->setFont( Font( .08, BOLD_STROKE ) );
191  ok->setId( "OK" );
192  //Put the button on the window.
193  window->addControl( ok );
194 
195  //Cancel button.
196  NewButton *cancel = new NewButton;
197  cancel->setRect( Rect( .05, -.57, .22, .1 ) );
198  cancel->setLabel( "Cancel" );
199  cancel->setCommand( "Cancel" );
200  cancel->setColor( GFXColor( 0, 0, 1, .25 ) );
201  cancel->setTextColor( GUI_OPAQUE_WHITE() );
202  cancel->setDownColor( GFXColor( 0, 0, 1, .6 ) );
203  cancel->setDownTextColor( GUI_OPAQUE_BLACK() );
204  cancel->setHighlightColor( GFXColor( 0, 0, 1, .4 ) );
205  cancel->setFont( Font( .08, BOLD_STROKE ) );
206  cancel->setId( "Cancel" );
207  //Put the button on the window.
208  window->addControl( cancel );
209 
210  //Scroller for description.
211  Scroller *pickScroller = new Scroller;
212  pickScroller->setRect( Rect( .52, -.4, .05, .85 ) );
213  pickScroller->setColor( SaturatedColor( 0, 0, 1, .1 ) );
214  pickScroller->setThumbColor( SaturatedColor( 0, 0, .4 ), GUI_OPAQUE_WHITE() );
215  pickScroller->setButtonColor( SaturatedColor( 0, 0, .4 ) );
216  pickScroller->setTextColor( GUI_OPAQUE_WHITE() );
217  pickScroller->setOutlineColor( GUI_OPAQUE_MEDIUM_GRAY() );
218 
219  //List picker.
220  SimplePicker *pick = new SimplePicker;
221  pick->setRect( Rect( -.57, -.4, 1.09, .85 ) );
222  pick->setColor( SaturatedColor( 0, 0, 1, .1 ) );
224  pick->setTextColor( GUI_OPAQUE_WHITE() );
225  pick->setSelectionColor( SaturatedColor( .2, .2, .7, .8 ) );
226  pick->setHighlightColor( SaturatedColor( 0, 0, .6, .35 ) );
228  pick->setFont( Font( .065 ) );
229  pick->setTextMargins( Size( 0.02, 0.01 ) );
230  pick->setId( "Picker" );
231  pick->setScroller( pickScroller );
232  //Put the picker on the window.
233  window->addControl( pick );
234 
235  window->addControl( pickScroller ); //Want scroller "over" picker.
236 }
237 
238 //Initialization of our Question Dialog class.
239 //Create a window, load controls, and set the title.
240 void QuestionDialog::init( const std::string &title )
241 {
242  setWindow( new Window );
243  initControls();
244 
245  StaticDisplay *titleControl = static_cast< StaticDisplay* > ( window()->findControlById( "Title" ) );
246  assert( titleControl != NULL );
247  std::string fixedTitle;
248  std::string::size_type lastpos = 0, pos;
249  for ( pos = title.find( '\n' ); pos != std::string::npos; pos = title.find( '\n', lastpos ) ) {
250  fixedTitle.append( title, lastpos, pos-lastpos );
251  fixedTitle.append( "#n#" );
252  lastpos = pos+1;
253  }
254  fixedTitle.append( title, lastpos, std::string::npos );
255  titleControl->setText( fixedTitle );
256 
258 }
259 
260 //Make everything happen.
262 {
263  //We need to delete the Window we created until the Window is opened.
264  m_deleteWindow = false;
265 
267 }
268 
269 //Class that will handle Alert correctly.
271 {
272 //Load the controls for this dialog into the window.
273  virtual void initControls( void )
274  {
276  }
277 };
278 
279 //Display a modal message to the user. The message will be shown until the user
280 //hits the OK button.
281 void showAlert( const std::string &title )
282 {
283  AlertDialog *dialog = new AlertDialog;
284  dialog->init( title );
285  dialog->run();
286 }
287 
288 //Class that will handle Yes/No dialog correctly.
290 {
291 //Load the controls for this dialog into the window.
292  virtual void initControls( void )
293  {
295  }
296 
297 //Process a command event from the window.
298  virtual bool processWindowCommand( const EventCommandId &command, Control *control );
299 };
300 
301 //Process a command from the window.
302 bool YesNoDialog::processWindowCommand( const EventCommandId &command, Control *control )
303 {
304  if (command == "Yes") {
306  modalFinished();
307  } else if (command == "No") {
309  modalFinished();
310  } else {
311  //We only care about buttons clicked.
312  return QuestionDialog::processWindowCommand( command, control );
313  }
314  return true;
315 }
316 
317 //Display a modal yes/no question.
318 //The result is supplied in the callback.
319 void showYesNoQuestion( const std::string &title, ModalDialogCallback *cb, const std::string &id )
320 {
321  YesNoDialog *dialog = new YesNoDialog;
322  dialog->init( title );
323  dialog->setCallback( cb, id );
324  dialog->run();
325 }
326 
327 //Process a command from the window.
329 {
330  if (command == "Cancel") {
331  //Just close down, with the result unknown.
332  m_result = (-1);
333  modalFinished();
334  return true;
335  } else if (command == "OK") {
336  //The OK button was clicked.
337  Picker *picker = static_cast< Picker* > ( window()->findControlById( "Picker" ) );
338  assert( picker != NULL );
339  m_result = picker->selectedItem();
340  if (m_result >= 0) {
341  //We have a selection. We are done.
342  modalFinished();
343  } else {
344  //Ignore OK button until we have a selection.
345  //Do nothing here.
346  }
347  return true;
348  } else {
349  //We only care about buttons clicked.
350  return QuestionDialog::processWindowCommand( command, control );
351  }
352  assert( false ); //Should never get here.
353 }
354 
356 {
357  return static_cast< SimplePicker* > ( window()->findControlById( "Picker" ) );
358 }
359 
360 //Display a modal list of options.
361 //The result is supplied in the callback.
362 void showListQuestion( const std::string &title,
363  const std::vector< std::string > &options,
365  const std::string &id )
366 {
368  dialog->init( title );
369  dialog->setCallback( cb, id );
370 
371  //Fill the picker with the choices supplied by the caller.
372  SimplePicker *picker = dialog->getPicker();
373  assert( picker != NULL );
374  for (size_t i = 0; i < options.size(); i++)
375  picker->addCell( new SimplePickerCell( options[i] ) );
376  dialog->run();
377 }
378