JNR
stageEdit.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 "laStagesJR.h"
32 #include <Gen\Gen.h>
33 
34 // Level loading dialog for the level editor
35 //
36 class laLoadDlg_Editor : public uiFileDialog
37 {
38 public:
39  class stageEdit* pEditor;
40 
41  laLoadDlg_Editor(): uiFileDialog(M_LOAD)
42  {
43  pEditor = NULL;
44  hide();
45 
46  laSettings *s = laSystemIntegrator::getSettings();
47  strcpy(strFileName, s->load_level);
48  }
49 
50  virtual ~laLoadDlg_Editor(void){
51  }
52 
53  virtual void show()
54  {
55  uiFileDialog::show();
56 
57  if(pEditor)
58  {
59  pEditor->pCanvas->disable();
60  pEditor->_pMenu->disable();
61  }
62  }
63 
64  virtual void hide()
65  {
66  uiFileDialog::hide();
67 
68  if(pEditor)
69  {
70  pEditor->pCanvas->enable();
71  pEditor->_pMenu->enable();
72  }
73  }
74 
75  virtual void onOK()
76  {
77  //MSG("Load");
78  }
79 };
80 
81 // Saving dialog for the level editor
82 //
83 class laSaveDlg_Editor : public uiFileDialog
84 {
85 public:
86  class stageEdit* pEditor;
87 
88  laSaveDlg_Editor(): uiFileDialog(M_SAVE)
89  {
90  pEditor = NULL;
91  hide();
92 
93  laSettings *s = laSystemIntegrator::getSettings();
94  strcpy(strFileName, s->load_level);
95  }
96 
97  virtual ~laSaveDlg_Editor(void){
98  }
99 
100  virtual void show()
101  {
102  uiFileDialog::show();
103 
104  if(pEditor)
105  {
106  pEditor->pCanvas->disable();
107  pEditor->_pMenu->disable();
108  }
109  }
110 
111  virtual void hide()
112  {
113  uiFileDialog::hide();
114 
115  if(pEditor)
116  {
117  pEditor->pCanvas->enable();
118  pEditor->_pMenu->enable();
119  }
120  }
121 
122  virtual void onOK()
123  {
124  //MSG("Save");
125 
126  if(pEditor)
127  {
128  pEditor->pBlueprint->ppLoad("gen/post-processing/pp-editor.pp");
129  pEditor->pBlueprint->pp();
130  pEditor->pBlueprint->save(strFileName);
131  }
132  }
133 };
134 
135 
136 void uiNewLevelDlg::show(){
137  pEditor->pCanvas->disable();
138  pEditor->_pMenu->disable();
139  uiContainer::show();
140 }
141 
142 void uiNewLevelDlg::hide(){
143  pEditor->pCanvas->enable();
144  pEditor->_pMenu->enable();
145  uiContainer::hide();
146 }
147 
148 // Handle palette commands
149 void uiPalette::onCommand(unsigned long uID, unsigned long nCmd)
150 {
151  //Get delta t per frame
152  double dt = ::laSystemIntegrator::getEnvironment()->getTimer()->delta();
153 
154  //Scroll buttons
155  if(uID==101)
156  { //Scroll left
157  pEditor->dScroll += dt*500;
158  return;
159  }
160  else if(uID==102)
161  {
162  //Scroll right
163  pEditor->dScroll -= dt*500;
164  return;
165  }
166 
167  //Identify the button
168  uiButton* pb = (uiButton*)get(uID);
169  char title[64];
170  pb->getText(title);
171 
172  //Hide/show
173  //
174  if(strstr(title, "hide"))
175  {
176  move(laPoint3(20, -90), M_AC, M_AT);
177  ((uiEdit*)get(2))->setText("show palette");
178  return;
179  }
180  else if(strstr(title, "show"))
181  {
182  move(laPoint3(20, -40), M_AC, M_AT);
183  ((uiEdit*)get(2))->setText("hide palette");
184  return;
185  }
186 
187 
188  // Radio buttons
189  //
190 
191  for(unsigned n=0; n<=6; n++) arBtn[n]->unpress();
192 
193  if(strstr(title, "Empty"))
194  {
195  arBtn[0]->press();
196  nBrushType = 0;
197  }
198  else if(strstr(title, "Solid"))
199  {
200  arBtn[1]->press();
201  nBrushType = 1;
202  }
203  else if(strstr(title, "Treasure"))
204  {
205  arBtn[2]->press();
206  nBrushType = GEN_BP_REWARD;
207  }
208  else if(strstr(title, "Lava"))
209  {
210  arBtn[3]->press();
211  nBrushType = GEN_BP_CHALLENGE;
212  }
213  else if(strstr(title, "A"))
214  {
215  arBtn[4]->press();
216  nBrushType = GEN_BP_CHALLENGE+1;
217  }
218  else if(strstr(title, "B"))
219  {
220  arBtn[5]->press();
221  nBrushType = GEN_BP_CHALLENGE+2;
222  }
223  else if(strstr(title, "C"))
224  {
225  arBtn[6]->press();
226  nBrushType = GEN_BP_CHALLENGE+3;
227  }
228 
229 }
230 
231 // Handle new level commands
232 void uiNewLevelDlg::onCommand(unsigned long uID, unsigned long nCmd)
233 {
234  //Identify the button
235  uiButton* pb = (uiButton*)get(uID);
236  char title[64];
237  pb->getText(title);
238 
239  if(strstr(title, "Ok"))
240  {
241  char strName[128];
242  char strTileset[128];
243  unsigned nLenght;
244 
245  //Extract parameters
246  _pEdName->getText(strName);
247  _pEdTileset->getText(strTileset);
248  nLenght = (unsigned)(_pSliderLenght->getValue());
249 
250  //MSG(strName);
251  //MSG(strTileset);
252  //::laSystemIntegrator::getEnvironment()->message(M_FALSE, "%u", nLenght);
253 
254  //Delete old objects
255  if(pEditor->pBlueprint)
256  {
257  delete pEditor->pBlueprint;
258  delete _pParams;
259  }
260 
261  //Create new objects
262  genBlueprint_CB *pbp = new genBlueprint_CB();
263  pbp->setName(strName);
264  pbp->setTileset(strTileset);
265 
266  _pParams = new genParameters(NULL, pbp);
267  (*_pParams)["Level Lenght"].dValue = nLenght;
268  pbp->create(_pParams, 0);
269  pbp->clear();
270  pbp->set(nLenght-1, 0, 0);
271 
272  //Supply a pointer to the level editor
273  //::laSystemIntegrator::getEnvironment()->message(M_FALSE, "%u", pbp->lenght());
274  pEditor->pBlueprint = pbp;
275  pEditor->dScroll = 0;
276  hide();
277  }
278  else if(strstr(title, "Cancel"))
279  {
280  //Return to the main menu
281  if(pEditor->pBlueprint == NULL)
282  {
283  laSystemIntegrator::getGame()->setStage(M_STAGE_MENU);
284  }
285  else hide();
286  }
287 }
288 
289 //Create the canvas window
290 //
291 uiEditorCanvas::uiEditorCanvas(): uiWindow()
292 {
293  laSettings *s = laSystemIntegrator::getSettings();
294 
295  create(
296  laPoint3(50, (s->graphics_resolution_h - (25)*GEN_BP_HEIGHT)/2.0 + 50 ),
297  laPoint3(s->graphics_resolution_w-100, (25)*GEN_BP_HEIGHT)
298  );
299 
300  pEditor = NULL;
301  nBlueprintX = nBlueprintY = 0;
302 }
303 
304 //Repply to mouse commands within the canvas
305 //
306 void uiEditorCanvas::reply()
307 {
308  if(!pEditor) throw laError("uiEditorCanvas::reply(): Nil editor object.");
309  //::laSystemIntegrator::getEnvironment()->message(M_TRUE, "%f %f", getAbsolutePos().x(), getAbsolutePos().y());
310 
311  // Map cursor position to blueprint position
312  //
313  if( (pEditor->pBlueprint) && isMouseInside() && (!(pEditor->_pMenu->isMouseInside())) )
314  {
315  laSettings *s = laSystemIntegrator::getSettings();
316  int nOffsetX = pEditor->dScroll + getAbsolutePos().x() ;
317  int nOffsetY = getAbsolutePos().y();
318 
319  nBlueprintX = ( getMousePointer()->pos().x() - nOffsetX) / 25;
320  nBlueprintY = ( getMousePointer()->pos().y() - nOffsetY) / 25;
321 
322  if(nBlueprintY>=GEN_BP_HEIGHT-1) nBlueprintY = GEN_BP_HEIGHT-2;
323  else if(nBlueprintY<0) nBlueprintY = 0;
324 
325  if(nBlueprintX>=pEditor->pBlueprint->lenght()) nBlueprintX = pEditor->pBlueprint->lenght()-1;
326  else if(nBlueprintX<=0) nBlueprintX = 0;
327 
328  // Let the user draw
329  //
330  laInputManager* pi = laSystemIntegrator::getInput();
331  if( pi->mouseButton(1) )
332  {
333  pEditor->pBlueprint->set(nBlueprintX, nBlueprintY, pEditor->_pPalette->nBrushType);
334  }
335  }
336 }
337 
338 void uiEditorCanvas::draw()
339 {
340  if(!pEditor) throw laError("uiEditorCanvas::reply(): Nil editor object.");
341  laRenderer* pr = ::laSystemIntegrator::getRenderer();
342 
343  // Draw level blueprint
344  //
345  if( pEditor->pBlueprint )
346  {
347  pr->transPush();
348  pr->transTranslate( getPos() + laPoint3(pEditor->dScroll, 0) );
349 
350  pEditor->pBlueprint->draw(25, 0);
351  pEditor->pBlueprint->drawGrid(25, 0);
352 
353  if( isEnabled() && (!(pEditor->_pMenu->isMouseInside())) )
354  {
355  pr->modeTexture(M_FALSE);
356  pr->styleSet( laColor(0,0,0,100) );
357  pr->drawRect( laPoint3(nBlueprintX*25, nBlueprintY*25 ),
358  laPoint3(25, 25), laPoint2(), laPoint2(1, 1));
359  }
360 
361  pr->transPop();
362  }
363 }
364 
365 stageEdit::stageEdit(void)
366 {
367  pBlueprint = NULL;
368  dScroll = 0;
369 }
370 
371 stageEdit::~stageEdit(void)
372 {
373 }
374 
375 void stageEdit::onInit()
376 {
377  laSettings *s = laSystemIntegrator::getSettings();
378  uiButton* pButton;
379 
380  //Initialize GUI
381  //
382  _uiDesktop.create("ui\\desktop-edit.dsk");
383 
384  uiLabel* pLabel = new uiLabel();
385  pLabel->setText(M_COPYRIGHT_STRING);
386  pLabel->move(laPoint3(20,20), M_AL, M_AB);
387  pLabel->align(M_AL, M_AB);
388  pLabel->size(11);
389  _uiDesktop.insert(pLabel);
390 
391  pCanvas = new uiEditorCanvas();
392  pCanvas->pEditor = this;
393  _uiDesktop.insert( pCanvas );
394 
395  _pPalette = new uiPalette();
396  _pPalette->pEditor = this;
397  _uiDesktop.insert(_pPalette);
398 
399  pButton = new uiButton();
400  pButton->create( laPoint3(5, s->graphics_resolution_h/2), laPoint3(45, 45) );
401  pButton->setText("<<");
402  pButton->setReceiver(_pPalette);
403  pButton->toggleRepetitive(M_TRUE);
404  pButton->setID(101);
405  _uiDesktop.insert(pButton);
406 
407  pButton = new uiButton();
408  pButton->create( laPoint3(s->graphics_resolution_w-55, s->graphics_resolution_h/2), laPoint3(45, 45) );
409  pButton->setText(">>");
410  pButton->setReceiver(_pPalette);
411  pButton->toggleRepetitive(M_TRUE);
412  pButton->setID(102);
413  _uiDesktop.insert(pButton);
414 
415  _pMenu = new uiMenu_JR(M_JRMENU_EDIT);
416  _uiDesktop.insert(_pMenu);
417 
418  pNewLevelDlg = new uiNewLevelDlg();
419  pNewLevelDlg->pEditor = this;
420  _uiDesktop.insert( pNewLevelDlg );
421 
422  pSaveDlg = new laSaveDlg_Editor();
423  ((laSaveDlg_Editor*)pSaveDlg)->pEditor = this;
424  _uiDesktop.insert( pSaveDlg );
425 
426  pOpenDlg = new laLoadDlg_Editor();
427  ((laLoadDlg_Editor*)pOpenDlg)->pEditor = this;
428  _uiDesktop.insert( pOpenDlg );
429 }
430 
431 void stageEdit::onActivate()
432 {
433  laSystemIntegrator::getEnvironment()->setTitle(M_EDITORTITLE_STRING);
434 
435  dScroll = 0;
436 
437  if(pBlueprint==NULL)
438  pNewLevelDlg->show();
439  else
440  pNewLevelDlg->hide();
441 
442  _pMenu->show();
443  _pMenu->collapse();
444 }
445 
446 void stageEdit::onDeactivate()
447 {
448  if(pBlueprint)
449  {
450  delete pBlueprint;
451  pBlueprint = NULL;
452  }
453 }
454 
455 
456 void stageEdit::onFini()
457 {
458  delete _pMenu;
459  _pMenu = NULL;
460 
461  delete pCanvas;
462  pCanvas = NULL;
463 }
464 
465 void stageEdit::onFrame(laRenderer *pr, laInputManager *pi, laTimer *pt)
466 {
467  laSettings *s = laSystemIntegrator::getSettings();
468 
469  pr->modeInterface();
470  _uiDesktop.drawBackground();
471 
472  // Draw GUI
473  //
474  _uiDesktop.draw();
475  _uiDesktop.reply();
476 
477  // Handle key input
478  //
479  if(pi->key(KEY_ESCAPE)) _pMenu->expand();
480 }
virtual void create(laPoint3 pos, laPoint3 size)
Create new empty window.
Definition: uiButton.cpp:55
virtual void modeTexture(M_BOOL bOn)=0
Enable/disable texturing.
M_BOOL isMouseInside()
Helpful function that checks if the mouse pointer is inside the window.
Definition: uiWindow.h:140
JR Main Menu, and in-game Menu.
Definition: uiMenu_JR.h:43
#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
2D Point
Definition: laPoint_novec.h:41
#define M_AL
Text align left.
#define M_AT
Text align top.
GUI Push-button.
Definition: uiButton.h:41
GUI Editbox.
Definition: uiEdit.h:44
void setReceiver(uiWindow *pWnd)
Set receiver window (fires onCommand when the button is clicked)
Definition: uiButton.h:95
Level Editor Stage.
Definition: stageEdit.h:120
Game Settings.
Definition: laSettings.h:38
Base class for GUI windows.
Definition: uiWindow.h:45
#define M_AC
Text align center.
File Selection Dialog.
Definition: uiFileDialog.h:39
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
virtual void modeInterface()=0
Switch to interface rendering (2D projection mode)
Text Label.
Definition: uiLabel.h:41
virtual void reply()
Handle input message.
Definition: uiDesktop.cpp:124
virtual void draw()
Display the window.
Definition: uiDesktop.cpp:112
Error handling class.
Definition: laError.h:46