1 /*
2 Xye License (it is a PNG/ZLIB license)
3 
4 Copyright (c) 2006 Victor Hugo Soliz Kuncar
5 
6 This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
7 
8 Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
9 
10     1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
11 
12     2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 
14     3. This notice may not be removed or altered from any source distribution.
15 
16 */
17 #include "vxsdl.h"
18 #ifndef FONTINCLUDED
19    #include "font.h"
20 #endif
21 #include<string>
22 #ifndef WINDOWINCLUDED
23     #include "window.h"
24 #endif
25 #include "dialogs.h"
26 const int XYEDIT_OBJECT_COLORS = 5;
27 
28 using namespace std;
29 
30 
31 string stripXML(const string h);
32 
33 class button;
34 
35 struct editorbuttons;
36 struct editorboard;
37 
38 
39 #define MAXCONTROLS 100
40 
41 enum contenttype
42 {
43     CONTENT_NOCONTENT,
44     CONTENT_VARIATION,
45     CONTENT_MAKEROUND,
46 
47     CONTENT_RECOLOR,
48     CONTENT_DIRECTION,
49     CONTENT_CHANGEOBJECT
50 };
51 
52 enum editorobjecttype
53 {
54     EDOT_XYE,
55     EDOT_WALL,
56     EDOT_GEM,
57 
58     EDOT_BLOCK,
59 
60     EDOT_EARTH,
61     EDOT_GEMBLOCK,
62     EDOT_MAGNET,
63     EDOT_PUSHER,
64     EDOT_ARROWMAKER,
65     EDOT_HAZARD,
66     EDOT_ONEDIRECTION,
67     EDOT_BEAST,
68     EDOT_TELEPORT,
69     EDOT_TURNER,
70     EDOT_COLORSYSTEM,
71     EDOT_KEYSYSTEM,
72     EDOT_BOT,
73     EDOT_NUMBER,
74     EDOT_SPECIALBLOCKS,
75     EDOT_FIREPAD,
76     EDOT_RATTLERHEAD,
77     EDOT_FOOD,
78 
79     EDOT_LARGEBLOCK,
80     EDOT_PORTAL,
81 
82     EDOT_COLORFACTORY,
83     EDOT_DANGERFACTORY,
84 
85     EDOT_HINT,
86 
87     EDOT_ERROR,
88 
89     EDOT_NONE
90 };
91 
92 enum editorcolor
93 {
94     EDCO_RED=1,
95     EDCO_GREEN=3,
96     EDCO_BLUE=2,
97     EDCO_YELLOW=0,
98     EDCO_PURPLE=4,
99     EDCO_WHITE=5,
100     EDCO_METAL = 6,
101     EDCO_WILD = 7
102 
103 };
104 
105 struct TiXmlElement;
106 struct boardelement;
107 
108 class editor
109 {
110   private:
111 
112      static button * savebutton;
113      static button * solutionbutton;
114 
115      static bool ExitPrompt;
116      static string loadError;
117 
118      static void loop();
119 
120 
121 
122 
123      static void onExitAttempt();
124      static void onQuitClick(const buttondata* data);
125      static void beforeDraw();
126      static void onKeyDown(SDLKey keysim,Uint16 unicode);
127      static void onKeyUp(SDLKey keysim,Uint16 unicode);
128 
129      static void cancel();
cancel(const buttondata * data)130      static void cancel(const buttondata* data) { cancel(); }
131 
132      static window* editorwindow;
133 
134      static void onExitWithoutSavingClick(bool yes);
135      static void onBrowseWithoutSavingClick(bool yes);
136 
137      static enum setText_state_enum
138      {
139           ASSIGN_TITLE=0,
140           ASSIGN_DESCRIPTION=1,
141           ASSIGN_AUTHOR=2,
142           ASSIGN_HINT=3,
143           ASSIGN_BYE=4,
144           ASSIGN_LEVEL_TITLE=5
145 
146      } setText_state;
147      static void continueSetText(bool okclicked, const string text, inputDialogData * dat);
148      static void beginSetText(const buttondata* data);
149 
150      static void continueAppendFile(bool okclicked, const string text, inputDialogData * dat);
151      static void beginAppendFile(const buttondata* data);
152 
153      static void continueChangeLevelNumber(bool okclicked, const string text, inputDialogData * dat);
154      static void beginChangeLevelNumber(const buttondata*data);
155 
156      static void continueChangeLevelColor(bool okclicked, const string text, inputDialogData * dat);
157      static void beginChangeLevelColor(const buttondata*data);
158 
159      static void onClearConfirmation(bool yes);
160      static void onClearClick(const buttondata* data);
161      static void onPreviousLevelClick(const buttondata* data);
162      static void onNextLevelClick(const buttondata* data);
163      static void onInsertLevelClick(const buttondata* data);
164      static void onEraseLevelClick(const buttondata* data);
165      static void onEraseLevelConfirmation(bool yes);
166 
167      static void saveAs(bool okclicked, const string text, inputDialogData * dat);
168 
169      static void updateCountRelated();
170 
171      static bool appendLevels(const string filename); //implemented in editorload.cpp
172      static bool load_kye(); //implemented in editorload.cpp
173      static bool load_KyeFormat(TiXmlElement * el); //implemented in editorload.cpp
174 
175      static void continueAskHint(bool okclicked, const string text, inputDialogData * dat);
176  public:
177 
178      static string filename;
179      static string filename_name;
180      static string filename_path;
181 
182     static editorobjecttype SelectedType();
183     static bool SavedFile;
184     static void SetFile(const string &path, const string &file);
185     static bool save(); //implemented in editorsave.cpp
186     static bool save(const string &target, bool onlyOneLevel = false);
buttonSave(const buttondata * data)187     static void buttonSave(const buttondata* data) { save(); }
188     static void onSaveAsClick(const buttondata* data);
189     static void onBrowseClick(const buttondata* data);
190 
191     static bool load(); //implemented in editorload.cpp
192 
193     static void SendSolution(const char* sol);
194 
195     static void test(bool solution);
196 
197     static void test();
198     static void playSolution(const buttondata* data=NULL);
199 
test(const buttondata * data)200     static void test(const buttondata* data) { test(); }
201 
202 
203 
204     static Font* FontRes;
205 
206     static LuminositySprites sprites;
207 
208     static int Width;
209     static int Height;
210     static int GRIDSIZE;
211     static void Error(const char* msg);
212 
213     static editorbuttons* buttons;
214     static editorboard *  board;
215     static unsigned int tic4;
216     static unsigned int subtic4;
217 
218     static void StartSection(window* wind)  ;
219     static void ResumeSection(window* wind)  ;
220     static void ResumeSectionAndQuit(window* wind)  ;
221 
222     static void askHint(boardelement* o);
223 
224 
225 };
226 
227 
228 struct singleobject
229 {
230     contenttype      content;
231     int              variation;
232     int              direction;
233     editorobjecttype type;
234     bool selected;
235     bool flash;
236     bool round;
237     editorcolor color;
238 };
239 
240 struct boardobject
241 {
242     bool  empty;
243     int              variation;
244     editorobjecttype type;
245     editorcolor color;
246     string hint;
247 };
248 #define EDITORDIRECTION_RIGHT 0
249 #define EDITORDIRECTION_UP    1
250 #define EDITORDIRECTION_LEFT  2
251 #define EDITORDIRECTION_DOWN  3
252 
253 
254 void drawObjectBySpecs( SDL_Surface * target, int x, int y, editorobjecttype ot, editorcolor color, bool round, int variation, int direction);
255 
256 #define EDITORBUTTONS_COUNTX 26
257 #define EDITORBUTTONS_COUNTY 3
258 class editorbuttons: public control
259 {
260  private:
261     int lastclickedx,lastclickedy;
262 
263     singleobject buttons[EDITORBUTTONS_COUNTX][EDITORBUTTONS_COUNTY];
264     singleobject * getbuttonbyxy(int x, int y);
265     int detectRotation(int x0, int y0, int x1, int y1);
266 
267 
268     void extendButtons(editorobjecttype ot, editorcolor color, bool round, int variation);
269     void switchToObject( editorobjecttype ot, editorcolor color, bool round, int variation, int direction);
270     void updateText( editorobjecttype ot, editorcolor color, bool round, int variation, int direction);
271     void handleClick(singleobject* target);
272 
273     string text;
274     bool saved;
275 
276  public:
277 
278 
279 
280     bool clicked;
281 
282 
283     bool mouse;
284     int mousex,mousey;
285 
286     singleobject * hover;
287     singleobject * clickedempty;
288     singleobject * selection;
289     singleobject * clickedobject;
290 
291 
292     bool Eraser;
293     editorobjecttype SelectedObjectType;
294     editorcolor      SelectedColor;
295     int              SelectedVariation;
296     int              SelectedDirection;
297     bool             SelectedRound;
298 
299     editorbuttons(int sx, int sy, int sw, int sh);
300 
301     void draw(SDL_Surface* target);
loop()302     void loop() {}
303     void onMouseMove(int px,int py);
304     void onMouseOut();
305     void onMouseDown(int px,int py);
306     void onMouseUp(int px,int py);
onMouseRightUp(int px,int py)307     inline void onMouseRightUp(int px,int py) {}
308     void drawbutton(SDL_Surface* target,singleobject &o, int x, int y);
309 
310     void setInfo(const string& msg);
311 
312     static editorbuttons copy;
313     void SaveCopy();
314     void LoadCopy();
315 };
316 
317 struct boardelement
318 {
319     editorobjecttype type;
320     editorcolor      color;
321     int              variation;
322     bool             round;
323     int              direction;
324     Uint8 r1mem,r3mem,r7mem,r9mem;
325     int              parentx;
326     int              parenty;
327     string           hint;
328 
boardelementboardelement329     boardelement() { parentx=parenty = -1; type=EDOT_NONE; };
330 
331 };
332 
333 #ifndef XYE_HORZ
334     #define XYE_HORZ 30
335     #define XYE_VERT 20
336 #endif
337 
338 struct DefaultColorData {
339     bool useDefault;
340     SDL_Color color;
341 };
342 const int TOTAL_EDITOR_COLOR_OPTIONS = 5;
343 const int EDITOR_COLOR_WALLS = 0;
344 const int EDITOR_COLOR_FLOOR = 1;
345 const int EDITOR_COLOR_EARTH = 2;
346 const int EDITOR_COLOR_DOORS = 3;
347 const int EDITOR_COLOR_FORCE = 4;
348 
349 
350 
351 class editorboard: public control
352 {
353  private:
354     void applyFromButtons(int x, int y);
355 
356     void drawWallInBoard(SDL_Surface*target,int ox,int oy, int x,int y, int variation, bool round);
357     void drawTeleportInBoard(SDL_Surface*target,int ox,int oy, int x, int y, int direction);
358     void drawLargeBlockInBoard(SDL_Surface * target, int ox,int oy, int x, int y, editorcolor color, int variation, int direction);
359     void enforceUniquePortals(int x, int y, int variation, editorcolor color);
360 
361     void assign(editorboard* other);
362     static vector<editorboard> levelList;
363     static int                 currentLevel;
364     bool findWall(int x, int y, int variation);
365     bool wallContainsRoundCorners(int x, int y);
366  public:
367     DefaultColorData colors[TOTAL_EDITOR_COLOR_OPTIONS];
368     bool clicked;
369     bool mouse;
370     int mousex,mousey;
371 
372     boardelement objects[XYE_HORZ ][XYE_VERT];
373     int xye_x;
374     int xye_y;
375     int portal_x[XYEDIT_OBJECT_COLORS+1][2];
376     int portal_y[XYEDIT_OBJECT_COLORS+1][2];
377 
378 
379 
380 
381     string title;
382     string hint;
383 
384     static string filetitle;
385     static string description;
386     static string author;
387     string bye;
388     string solution;
389 
390 
391     editorboard(int sx, int sy);
392     editorboard();
393 
394     void updateWallMem(int ox, int oy);
395 
396     void draw(SDL_Surface* target);
loop()397     void loop() {}
398     void onMouseMove(int px,int py);
399     void onMouseOut();
400     void onMouseDown(int px,int py);
401     void onMouseUp(int px,int py);
402     void onMouseRightUp(int px,int py);
403 
404     void makeDefaultLevel();
405 
406 
407     static void SaveCopy(editorboard* ed);
408     static void SetCopySolution(const char* sol);
409     static void LoadCopy(editorboard* ed);
410 
411     static int  CountLevels();
412     static int  CurrentLevelNumber();
413     static void SaveAtLevelNumber(editorboard* ed, int num);
414     static void ResetLevels(bool empty = false);
415     static void LoadLevelNumber(editorboard* ed, int num);
416     static void DeleteLevel(editorboard * ed);
417     static void CreateLevel(editorboard * ed);
418     static void MoveToLevelNumber(editorboard* ed, int x);
419 
420 
421 
422 };
423 
424 Uint8 getLargeBlockFlagsByVarDir( int variation, int direction);
425 Uint32 getHiddenWayFlagsByVariationAndDir(int variation, int direction);
426