JNR
uiMenu_JR.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 #include "stdafx.h"
31 #include "jnr.h"
32 
33 #include <windows.h>
34 
35 class uiLoadProfile_JR: public uiContainer
36 {
37  unsigned _nProfiles;
38  char _title_buf[64];
39 
40 public:
41  unsigned profiles() { return _nProfiles; }
42 
43  uiLoadProfile_JR();
44  void onCommand(unsigned long uID, unsigned long nCmd);
45 
46  void loadProfile(char*strProfile);
47  char* profileName(unsigned i)
48  {
49  uiButton* pb = (uiButton*)get(i + 2);
50  pb->getText(_title_buf);
51  return _title_buf;
52  }
53 };
54 
55 uiLoadProfile_JR::uiLoadProfile_JR(): uiContainer("ui\\dialogs\\mm-profile-list.cui")
56 {
57  WIN32_FIND_DATA FindFileData;
58  HANDLE hFind;
59 
60  BOOL bOK = FALSE;
61  hFind = FindFirstFile("save\\*.profile", &FindFileData);
62  if(hFind != INVALID_HANDLE_VALUE) bOK = TRUE;
63  //ASSERT(hFind != INVALID_HANDLE_VALUE, "Can't list profiles or empty profile list");
64 
65  _nProfiles=0;
66  #define M_BUT_HEIGHT 45
67 
68  while(bOK)
69  {
70  uiButton* but = new uiButton();
71  but->create( laPoint3(0, (_nProfiles++)*M_BUT_HEIGHT*1.1 + 10), laPoint3(150, M_BUT_HEIGHT) );
72  *strstr(FindFileData.cFileName, ".profile") = '\0';
73  but->setText(FindFileData.cFileName);
74  but->setID( insert(but) );
75  //MSG( "%d", but->getID() );
76 
77  bOK = FindNextFile(hFind, &FindFileData);
78  }
79 
80  uiButton* but = new uiButton();
81  but->create( laPoint3(0, (_nProfiles)*M_BUT_HEIGHT*1.1 + 10 + M_BUT_HEIGHT*0.3), laPoint3(150, M_BUT_HEIGHT) );
82  but->setText( "Cancel" );
83  but->setID( insert(but) );
84 
85  _ptSize[1] = (_nProfiles+2)*M_BUT_HEIGHT*1.1 + 30;
86 
87  //MSG(FindFileData.cFileName);
88 }
89 
90 void uiLoadProfile_JR::onCommand(unsigned long uID, unsigned long nCmd)
91 {
92  if(!isEnabled()) return;
93 
94  //Identify the button
95  uiButton* pb = (uiButton*)get(uID);
96  char title[64];
97  pb->getText(title);
98 
99  if( !strstr(title, "Cancel") ) {
100  loadProfile(title);
101  }
102 
103  hide();
104 }
105 
106 void uiLoadProfile_JR::loadProfile(char*strProfile)
107 {
108  char strfile[64];
109  sprintf(strfile, "save\\%s.profile", strProfile);
110  laFileParser fp;
111 
112  // Try to read next level
113  if( fp.fileOpen(strfile) )
114  {
115  try{
116  laPropertyList pl;
117  fp.readObj(&pl, M_FALSE);
118  fp.fileClose();
119 
120  strcpy( laSystemIntegrator::getSettings()->load_level, pl.getText("load-level"));
121  strcpy( laSystemIntegrator::getSettings()->load_player_name, strProfile);
122 
123  //MSG(laSystemIntegrator::getSettings()->load_level);
124  laSystemIntegrator::getGame()->setStage(M_STAGE_LOAD);
125 
126  return;
127  }
128  catch(laError&) { };
129  }
130 
131  ASSERT(M_FALSE, "Corrupted player profile '%s'", strProfile);
132 }
133 
134 class uiNewProfile_JR: public uiChoiceDialog
135 {
136 public:
137  uiLoadProfile_JR **_ppListProf;
138 
139  uiNewProfile_JR();
140  virtual void onOK();
141 };
142 
143 uiNewProfile_JR::uiNewProfile_JR(): uiChoiceDialog("ui\\dialogs\\mm-profile-new.cui"){
144 }
145 
146 void uiNewProfile_JR::onOK()
147 {
148  char str[32];
149  ((uiEdit*)get(3))->getText(str);
150 
151  // Check if profile exists
152  //
153  char strfile[64];
154  sprintf(strfile, "save\\%s.profile", str);
155  FILE* f = fopen(strfile, "r");
156  if(f)
157  {
158  // Aleeady exists. Close prof file and show warning
159  //
160  fclose(f);
161  hide();
162 
163  static uiWarningDialog* pWarn = NULL;
164  if(pWarn) pWarn->show();
165  else{
166  pWarn = new uiWarningDialog("ui\\dialogs\\mm-profile-w-duplicate.cui", this);
167  ((uiContainer*)_pParent)->insert( pWarn );
168  }
169  }
170  else
171  {
172  // Let the player create the sheet with the
173  // default params
174  //
175  f = fopen(strfile, "w+");
176  fprintf(f, "[profile]\n [text] load-level: '%s' \n[/profile]", M_NEWGAME_FIRST_LEVEL);
177  fclose(f);
178 
179  strcpy( laSystemIntegrator::getSettings()->load_player_name, str);
180  strcpy( laSystemIntegrator::getSettings()->load_level, M_NEWGAME_FIRST_LEVEL);
181  laSystemIntegrator::getGame()->setStage(M_STAGE_LOAD);
182 
183  // Invalidate profile list;
184  delete *_ppListProf;
185  *_ppListProf = NULL;
186  }
187 }
188 
189 uiMenu_JR::uiMenu_JR(unsigned nMenuType)
190 {
191  _nMenuType = nMenuType;
192 
193  switch(_nMenuType)
194  {
195  case M_JRMENU_MAIN:
196  load("ui\\dialogs\\mmenu.cui");
197  break;
198  case M_JRMENU_INGAME:
199  load("ui\\dialogs\\mmenu-ingame.cui");
200  break;
201  case M_JRMENU_EDIT:
202  load("ui\\dialogs\\mmenu-edit.cui");
203  break;
204  default:
205  throw laError("uiMenu_JR::uiMenu_JR(): Unimplemented menu type");
206  }
207 
208  _pListProf = NULL;
209  _pNewProf = NULL;
210 }
211 
212 uiMenu_JR::~uiMenu_JR(void)
213 {
214  if(_pListProf) delete _pListProf;
215  if(_pNewProf) delete _pNewProf;
216 }
217 
218 void uiMenu_JR::_create_profile_dialogs()
219 {
220  ASSERT(_pParent, "Paremnt must be set to create dialogs");
221 
222  if(!_pListProf)
223  {
224  _pListProf = new uiLoadProfile_JR();
225  ((uiContainer*)_pParent)->insert( _pListProf );
226  _pListProf->hide();
227  }
228 
229  if(!_pNewProf)
230  {
231  _pNewProf = new uiNewProfile_JR();
232  ((uiContainer*)_pParent)->insert( _pNewProf );
233  _pNewProf->hide();
234  }
235 
236  // Allows the new prof window to invalidate the prof list
237  _pNewProf->_ppListProf = &_pListProf;
238 }
239 
240 
242 {
243  _create_profile_dialogs();
244  if( _pListProf->profiles() == 0 ) get(2)->disable();
245  else get(2)->enable();
246 
248 }
249 
250 void uiMenu_JR::onCommand(unsigned long uID, unsigned long nCmd)
251 {
252  if(!isEnabled()) return;
253 
254  //Identify the button
255  uiButton* pb = (uiButton*)get(uID);
256  char title[64];
257  pb->getText(title);
258 
259  if(strstr(title, "New Game"))
260  {
261  // New profile prompt
262  _pNewProf->show();
263  }
264  else if(strstr(title, "Continue"))
265  {
266  // Only 1 profile, so activate it without prompting
267  if(_pListProf->profiles() == 1)
268  {
269  _pListProf->loadProfile( _pListProf->profileName(0) );
270  }
271  else
272  {
273  // New profile prompt
274  _pListProf->show();
275  }
276  }
277  else if(strstr(title, "New Level"))
278  {
279  ((stageEdit*)(laSystemIntegrator::getGame()->getStage(M_STAGE_EDIT)))
280  ->pNewLevelDlg->show();
281  }
282  else if(strstr(title, "Save As"))
283  {
284  ((stageEdit*)(laSystemIntegrator::getGame()->getStage(M_STAGE_EDIT)))
285  ->pSaveDlg->show();
286  }
287  else if(strstr(title, "Open"))
288  {
289  ((stageEdit*)(laSystemIntegrator::getGame()->getStage(M_STAGE_EDIT)))
290  ->pOpenDlg->show();
291  }
292  else if(strstr(title, "Editor"))
293  {
294  disable();
295  ((uiContainer*)_pParent)->insert(new uiStartEditorDlg());
296  }
297  else if(strstr(title, "About"))
298  {
299  hide();
300  ((uiContainer*)_pParent)->insert(new uiAboutDlg());
301  }
302  else if(strstr(title, "Exit"))
303  {
304  ::laSystemIntegrator::getEnvironment()->terminate();
305  }
306  else if(strstr(title, "Resume"))
307  {
308  hide();
309  }
310  else if(strstr(title, "Return"))
311  {
312  laSystemIntegrator::getGame()->setStage(M_STAGE_MENU);
313  hide();
314  }
315  else if(strstr(title, "hide"))
316  {
317  collapse();
318  }
319  else if(strstr(title, "show"))
320  {
321  expand();
322  }
323 }
324 
325 void uiMenu_JR::collapse()
326 {
327  move(laPoint3(20, -290), M_AR, M_AB);
328  ((uiEdit*)get(2))->setText("show menu");
329 }
330 
331 void uiMenu_JR::expand()
332 {
333  move(laPoint3(20, -20), M_AR, M_AB);
334  ((uiEdit*)get(2))->setText("hide menu");
335 }
336 
337 void uiMenu_JR::toggle()
338 {
339  char caption[128];
340  ((uiEdit*)get(2))->getText(caption);
341 
342  if(strstr( caption, "show" )) expand();
343  else collapse();
344 }
virtual void create(laPoint3 pos, laPoint3 size)
Create new empty window.
Definition: uiButton.cpp:55
GUI Container Window.
Definition: uiContainer.h:42
Ok/Cancel Dialog.
#define M_AB
Text align bottom.
virtual void move(laPoint3 ptNewPos, unsigned nHReference=M_AL, unsigned nVReference=M_AT)
Change window position.
Definition: uiWindow.cpp:51
#define M_AR
Text align right.
uiLabel caption
Caption.
Definition: uiContainer.h:83
GUI Push-button.
Definition: uiButton.h:41
laPoint3 _ptSize
Window size.
Definition: uiWindow.h:76
GUI Editbox.
Definition: uiEdit.h:44
Level Editor Stage.
Definition: stageEdit.h:120
uiWindow * _pParent
Pointer to parent window, if any.
Definition: uiWindow.h:71
virtual void onCommand(unsigned long uID, unsigned long nCmd)
Command event handler.
Definition: uiMenu_JR.cpp:250
virtual void reply()
Handle input message.
Error handling class.
Definition: laError.h:46
virtual void onCommand(unsigned long uID, unsigned long nCmd)
Command event handler.
Definition: uiWindow.cpp:263
File Parser.
Definition: laFileParser.h:41
virtual void reply()
Handle input message.
Definition: uiMenu_JR.cpp:241