Debian Codegraph @

engine/editor/patch_editor.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name patch_editor.cpp - The patch editor. */
11 //
12 // (c) Copyright 2008 by Jimmy Salmon
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27 //
28
29 //@{
30
31 #include "stratagus.h"
32
33 #include <sstream>
34
35 #include "sound.h"
36 #include "iolib.h"
37 #include "network.h"
38 #include "interface.h"
39 #include "cursor.h"
40 #include "map.h"
41 #include "guichan.h"
42 #include "patch_type.h"
43 #include "patch.h"
44 #include "patch_manager.h"
45 #include "ui.h"
46 #include "font.h"
47
48
49 extern void DrawGuichanWidgets();
50
51 bool PatchEditorRunning;
52
53 extern gcn::Gui *Gui;
54 static gcn::Container *PatchEditorContainer;
55
56 static CPatch *Patch;
57
58 static const int PatchMenuWidth = 150;
59
60 static float ScrollX;
61 static float ScrollY;
62
63 static CGraphic *TransparentG;
64 static CGraphic *TransparentSmallG;
65 static CGraphic *ImpassableG;
66 static CGraphic *ImpassableSmallG;
67 static CGraphic *CoastG;
68 static CGraphic *CoastSmallG;
69 static CGraphic *ShallowWaterG;
70 static CGraphic *ShallowWaterSmallG;
71 static CGraphic *DeepWaterG;
72 static CGraphic *DeepWaterSmallG;
73 #define NumSpeeds 7
74 static CGraphic *SpeedG[NumSpeeds + 1];
75 static CGraphic *SpeedSmallG[NumSpeeds + 1];
76
77 enum PatchButton {
78 ButtonNone,
79 ButtonImpassable,
80 ButtonCoast,
81 ButtonShallowWater,
82 ButtonDeepWater,
83 ButtonSpeed0,
84 ButtonSpeed1,
85 ButtonSpeed2,
86 ButtonSpeed3,
87 ButtonSpeed4,
88 ButtonSpeed5,
89 ButtonSpeed6,
90 ButtonSpeed7,
91 ButtonTransparent,
92 };
93
94 struct PatchIcon
95 {
96 int X;
97 int Y;
98 CGraphic *G;
99 PatchButton Button;
100 };
101
102 static std::vector<PatchIcon> PatchIcons;
103
104 static std::map<int, unsigned short> FlagMap;
105 static PatchButton CurrentButton;
106 static PatchButton MouseOverButton;
107 static int MouseOverTileX;
108 static int MouseOverTileY;
109
110 static unsigned short DraggingClearFlags;
111 static unsigned short DraggingSetFlags; // overrides DraggingClearFlags
112
113
114 static void DoScroll()
115 {
116 int state = MouseScrollState | KeyScrollState;
117 static Uint32 lastTicks = GetTicks() - 1;
118
119 Uint32 ticks = GetTicks();
120 float speed = (ticks - lastTicks) / 1000.f * 400;
121 lastTicks = ticks;
122
123 if (state == ScrollNone) {
124 return;
125 }
126
127 if ((state & (ScrollLeft | ScrollRight)) &&
128 (state & (ScrollLeft | ScrollRight)) != (ScrollLeft | ScrollRight)) {
129 if (state & ScrollRight) {
130 ScrollX += speed;
131 } else {
132 ScrollX -= speed;
133 }
134 }
135 if ((state & (ScrollUp | ScrollDown)) &&
136 (state & (ScrollUp | ScrollDown)) != (ScrollUp | ScrollDown)) {
137 if (state & ScrollDown) {
138 ScrollY += speed;
139 } else {
140 ScrollY -= speed;
141 }
142 }
143
144 if (ScrollX < 0.f ||
145 Patch->getType()->getGraphic()->Width < (Video.Width - PatchMenuWidth)) {
146 ScrollX = 0.f;
147 } else if (ScrollX > Patch->getType()->getGraphic()->Width - (Video.Width - PatchMenuWidth)) {
148 ScrollX = (float)(Patch->getType()->getGraphic()->Width - (Video.Width - PatchMenuWidth));
149 }
150 if (ScrollY < 0.f ||
151 Patch->getType()->getGraphic()->Height < Video.Height) {
152 ScrollY = 0.f;
153 } else if (ScrollY > Patch->getType()->getGraphic()->Height - Video.Height) {
154 ScrollY = (float)(Patch->getType()->getGraphic()->Height - Video.Height);
155 }
156 }
157
158 /**
159 ** Change the patch-tile flags at (::MouseOverTileX, ::MouseOverTileY),
160 ** as specified with ::DraggingClearFlags and ::DraggingSetFlags.
161 **
162 ** The patch editor has no real map, so this function does not
163 ** attempt to update any CMapField.
164 */
165 static void ChangeFlagsAtMouse()
166 {
167 CPatchType *patchType = Patch->getType();
168 unsigned short flag = patchType->getFlag(MouseOverTileX, MouseOverTileY);
169 flag &= ~DraggingClearFlags;
170 flag |= DraggingSetFlags;
171 patchType->setFlag(MouseOverTileX, MouseOverTileY, flag);
172 }
173
174 static void PatchEditorCallbackButtonDown(unsigned button)
175 {
176 if ((1 << button) != LeftButton) {
177 return;
178 }
179
180 // Icons
181 if (MouseOverButton != ButtonNone) {
182 CurrentButton = MouseOverButton;
183 }
184
185 // Patch area
186 if (MouseOverTileX != -1) {
187 unsigned short flag = Patch->getType()->getFlag(MouseOverTileX, MouseOverTileY);
188 if (ButtonSpeed0 <= CurrentButton && CurrentButton <= ButtonSpeed7) {
189 DraggingClearFlags = MapFieldSpeedMask;
190 DraggingSetFlags = FlagMap[CurrentButton];
191 } else if (CurrentButton == ButtonCoast
192 || CurrentButton == ButtonShallowWater
193 || CurrentButton == ButtonDeepWater) {
194 // clear existing land/water flags then set the new flag
195 DraggingClearFlags = (MapFieldLandAllowed
196 | MapFieldCoastAllowed
197 | MapFieldShallowWater
198 | MapFieldDeepWater
199 | MapFieldNoBuilding);
200 if (flag & FlagMap[CurrentButton]) {
201 // set default to land
202 DraggingSetFlags = MapFieldLandAllowed;
203 } else {
204 DraggingSetFlags = FlagMap[CurrentButton];
205 }
206 } else {
207 // toggle flag
208 DraggingClearFlags = flag & FlagMap[CurrentButton];
209 DraggingSetFlags = DraggingClearFlags ^ FlagMap[CurrentButton];
210 }
211
212 ChangeFlagsAtMouse();
213 }
214 }
215
216 static void PatchEditorCallbackButtonUp(unsigned button)
217 {
218 DraggingClearFlags = 0;
219 DraggingSetFlags = 0;
220 }
221
222 static void PatchEditorCallbackKeyDown(unsigned key, unsigned keychar)
223 {
224 if (HandleKeyModifiersDown(key, keychar)) {
225 return;
226 }
227
228 switch (key) {
229 case 'f': // ALT+F, CTRL+F toggle fullscreen
230 if (!(KeyModifiers & (ModifierAlt | ModifierControl))) {
231 break;
232 }
233 ToggleFullScreen();
234 break;
235
236 case 'x': // ALT+X, CTRL+X: Exit editor
237 if (!(KeyModifiers & (ModifierAlt | ModifierControl))) {
238 break;
239 }
240 Exit(0);
241
242 case SDLK_UP: // Keyboard scrolling
243 case SDLK_KP8:
244 KeyScrollState |= ScrollUp;
245 break;
246 case SDLK_DOWN:
247 case SDLK_KP2:
248 KeyScrollState |= ScrollDown;
249 break;
250 case SDLK_LEFT:
251 case SDLK_KP4:
252 KeyScrollState |= ScrollLeft;
253 break;
254 case SDLK_RIGHT:
255 case SDLK_KP6:
256 KeyScrollState |= ScrollRight;
257 break;
258
259 default:
260 HandleCommandKey(key);
261 return;
262 }
263 }
264
265 static void PatchEditorCallbackKeyUp(unsigned key, unsigned keychar)
266 {
267 if (HandleKeyModifiersUp(key, keychar)) {
268 return;
269 }
270
271 switch (key) {
272 case SDLK_UP: // Keyboard scrolling
273 case SDLK_KP8:
274 KeyScrollState &= ~ScrollUp;
275 break;
276 case SDLK_DOWN:
277 case SDLK_KP2:
278 KeyScrollState &= ~ScrollDown;
279 break;
280 case SDLK_LEFT:
281 case SDLK_KP4:
282 KeyScrollState &= ~ScrollLeft;
283 break;
284 case SDLK_RIGHT:
285 case SDLK_KP6:
286 KeyScrollState &= ~ScrollRight;
287 break;
288 default:
289 break;
290 }
291 }
292
293 static void PatchEditorCallbackKeyRepeated(unsigned dummy1, unsigned dummy2)
294 {
295 }
296
297 static void PatchEditorCallbackMouse(int x, int y)
298 {
299 HandleCursorMove(&x, &y);
300
301 MouseScrollState = ScrollNone;
302 GameCursor = UI.Point.Cursor;
303 MouseOverButton = ButtonNone;
304 MouseOverTileX = -1;
305
306 // Icons
307 std::vector<PatchIcon>::iterator i;
308 for (i = PatchIcons.begin(); i != PatchIcons.end(); ++i) {
309 if (i->X <= x && x <= i->X + i->G->Width &&
310 i->Y <= y && y <= i->Y + i->G->Height)
311 {
312 MouseOverButton = i->Button;
313 }
314 }
315
316 // Patch area
317 if (0 <= x && x < Video.Width - PatchMenuWidth &&
318 0 <= y && y < Video.Height)
319 {
320 int tileX = (x + (int)ScrollX) / TileSizeX;
321 int tileY = (y + (int)ScrollY) / TileSizeY;
322 if (0 <= tileX && tileX < Patch->getType()->getTileWidth() &&
323 0 <= tileY && tileY < Patch->getType()->getTileHeight())
324 {
325 MouseOverTileX = tileX;
326 MouseOverTileY = tileY;
327
328 // If dragging did not begin in the patch area,
329 // then DraggingClearFlags and DraggingSetFlags
330 // are both zero, and this call doesn't change
331 // anything.
332 ChangeFlagsAtMouse();
333 }
334 }
335
336 // Scroll area at edge of the screen
337 if (HandleMouseScrollArea(x, y)) {
338 return;
339 }
340 }
341
342 static void PatchEditorCallbackExit(void)
343 {
344 // Disabled
345 if (!LeaveStops) {
346 return;
347 }
348
349 // Prevent scrolling while out of focus (on other applications)
350 KeyScrollState = MouseScrollState = ScrollNone;
351 }
352
353 static void DrawPatch()
354 {
355 const CGraphic *g = Patch->getType()->getGraphic();
356 g->DrawClip(-(int)ScrollX, -(int)ScrollY);
357 }
358
359 static void DrawPatchTileIcons()
360 {
361 CGraphic *g;
362 int x, y;
363
364 for (int j = 0; j < Patch->getType()->getTileHeight(); ++j) {
365 y = j * TileSizeY - (int)ScrollY;
366
367 for (int i = 0; i < Patch->getType()->getTileWidth(); ++i) {
368 x = i * TileSizeX - (int)ScrollX;
369
370 unsigned short flags = Patch->getType()->getFlag(i, j);
371 if (flags & MapFieldTransparent) {
372 g = TransparentSmallG;
373 g->DrawClip(x, y);
374 continue;
375 }
376 if (flags & MapFieldUnpassable) {
377 g = ImpassableSmallG;
378 g->DrawClip(x, y);
379 }
380 if (flags & MapFieldCoastAllowed) {
381 g = CoastSmallG;
382 g->DrawClip(x + 16, y);
383 }
384 if (flags & MapFieldShallowWater) {
385 g = ShallowWaterSmallG;
386 g->DrawClip(x + 16, y);
387 }
388 if (flags & MapFieldDeepWater) {
389 g = DeepWaterSmallG;
390 g->DrawClip(x + 16, y);
391 }
392 if ((flags & MapFieldSpeedMask) != 3) {
393 g = SpeedSmallG[(flags & MapFieldSpeedMask)];
394 g->DrawClip(x + 16, y + 16);
395 }
396 }
397 }
398 }
399
400 static void DrawGrids()
401 {
402 int i;
403 int width = Patch->getType()->getTileWidth();
404 int height = Patch->getType()->getTileHeight();
405
406 for (i = 1; i < width; ++i) {
407 Video.DrawVLineClip(ColorBlack, i * TileSizeX - (int)ScrollX, 0, height * TileSizeY);
408 }
409 for (i = 1; i < height; ++i) {
410 Video.DrawHLineClip(ColorBlack, 0, i * TileSizeY - (int)ScrollY, width * TileSizeX);
411 }
412 }
413
414 static void DrawIcons()
415 {
416 Uint32 color;
417 std::vector<PatchIcon>::iterator i;
418
419 for (i = PatchIcons.begin(); i != PatchIcons.end(); ++i) {
420 i->G->DrawClip(i->X, i->Y);
421
422 if (i->Button == MouseOverButton) {
423 color = ColorBlue;
424 } else if (i->Button == CurrentButton) {
425 color = ColorGreen;
426 } else {
427 color = ColorBlack;
428 }
429
430 Video.DrawRectangleClip(color, i->X - 1, i->Y - 1, 50, 50);
431 Video.DrawRectangleClip(color, i->X - 2, i->Y - 2, 52, 52);
432 }
433 }
434
435 static void DrawCoordinates()
436 {
437 if (MouseOverTileX == -1) {
438 return;
439 }
440
441 std::ostringstream o;
442 o << MouseOverTileX << "," << MouseOverTileY;
443 VideoDrawTextCentered(Video.Width - PatchMenuWidth / 2, Video.Height - 15, GameFont, o.str());
444 }
445
446 static void PatchEditorUpdateDisplay()
447 {
448 // Patch area
449 DrawPatch();
450 DrawPatchTileIcons();
451 DrawGrids();
452
453 // Menu area
454 Video.FillRectangle(ColorGray, Video.Width - PatchMenuWidth, 0, PatchMenuWidth, Video.Height);
455 DrawIcons();
456 DrawCoordinates();
457
458 DrawGuichanWidgets();
459
460 DrawCursor();
461
462 Invalidate();
463 RealizeVideoMemory();
464 }
465
466 static gcn::Button *PatchNewButton(const std::string &caption)
467 {
468 gcn::Color darkColor(38, 38, 78, 128);
469 gcn::Button *button;
470
471 button = new gcn::Button(caption);
472 button->setSize(106, 28);
473 button->setBackgroundColor(darkColor);
474 button->setBaseColor(darkColor);
475 return button;
476 }
477
478 class PatchSaveButtonListener : public gcn::ActionListener
479 {
480 public:
481 virtual void action(const std::string &eventId) {
482 CFile file;
483 std::string name = UserDirectory + "patches/" + Patch->getType()->getName() + ".lua";
484 if (file.open(name.c_str(), CL_OPEN_WRITE) == -1) {
485 // TODO: show error
486 return;
487 }
488 file.printf("%s", Map.PatchManager.savePatchType(Patch->getType()).c_str());
489 file.close();
490 // TODO: show success
491
492 Patch->getType()->setCustomPatch(false);
493 }
494 };
495
496 class PatchExitButtonListener : public gcn::ActionListener
497 {
498 public:
499 virtual void action(const std::string &eventId) {
500 // TODO: prompt to save changes
501 PatchEditorRunning = false;
502 }
503 };
504
505 static void InitCallbacks(EventCallback *callbacks)
506 {
507 callbacks->ButtonPressed = PatchEditorCallbackButtonDown;
508 callbacks->ButtonReleased = PatchEditorCallbackButtonUp;
509 callbacks->MouseMoved = PatchEditorCallbackMouse;
510 callbacks->MouseExit = PatchEditorCallbackExit;
511 callbacks->KeyPressed = PatchEditorCallbackKeyDown;
512 callbacks->KeyReleased = PatchEditorCallbackKeyUp;
513 callbacks->KeyRepeated = PatchEditorCallbackKeyRepeated;
514 callbacks->NetworkEvent = NetworkEvent;
515 }
516
517 static void PatchEditorMainLoop()
518 {
519 const EventCallback *old_callbacks = GetCallbacks();
520 EventCallback callbacks;
521 gcn::Button *saveButton;
522 gcn::Button *exitButton;
523 PatchSaveButtonListener *saveButtonListener;
524 PatchExitButtonListener *exitButtonListener;
525
526 InitCallbacks(&callbacks);
527 SetCallbacks(&callbacks);
528
529 ScrollX = 0.f;
530 ScrollY = 0.f;
531
532 CurrentButton = ButtonImpassable;
533 MouseOverButton = ButtonNone;
534
535 gcn::Widget *oldTop = Gui->getTop();
536
537 PatchEditorContainer = new gcn::Container();
538 PatchEditorContainer->setDimension(gcn::Rectangle(0, 0, Video.Width, Video.Height));
539 PatchEditorContainer->setOpaque(false);
540 Gui->setTop(PatchEditorContainer);
541
542 saveButton = PatchNewButton(_("Save"));
543 saveButtonListener = new PatchSaveButtonListener();
544 saveButton->addActionListener(saveButtonListener);
545 PatchEditorContainer->add(saveButton,
546 Video.Width - PatchMenuWidth / 2 - saveButton->getWidth() / 2, 20);
547
548 exitButton = PatchNewButton(_("Exit"));
549 exitButtonListener = new PatchExitButtonListener();
550 exitButton->addActionListener(exitButtonListener);
551 PatchEditorContainer->add(exitButton,
552 Video.Width - PatchMenuWidth / 2 - saveButton->getWidth() / 2, Video.Height - exitButton->getHeight() - 20);
553
554 Map.PatchManager.load();
555
556 PatchEditorRunning = true;
557 while (PatchEditorRunning) {
558 CheckMusicFinished();
559 DoScroll();
560
561 PatchEditorUpdateDisplay();
562
563 WaitEventsOneFrame();
564 }
565
566 SetCallbacks(old_callbacks);
567
568 Gui->setTop(oldTop);
569 delete PatchEditorContainer;
570 delete saveButton;
571 delete saveButtonListener;
572 delete exitButton;
573 delete exitButtonListener;
574 }
575
576 static void PatchAddIcon(int x, int y, CGraphic *g, PatchButton b)
577 {
578 PatchIcon icon;
579 icon.X = x;
580 icon.Y = y;
581 icon.G = g;
582 icon.Button = b;
583 PatchIcons.push_back(icon);
584 }
585
586 static void PatchLoadIcons()
587 {
588 std::string patchEditorPath = "ui/patcheditor/";
589 int spacing = 5;
590 int leftX = Video.Width - PatchMenuWidth + spacing;
591 int rightX = Video.Width - 48 - spacing;
592 int topY = 60;
593
594 ImpassableG = CGraphic::New(patchEditorPath + "impassable.png");
595 ImpassableG->Load();
596 ImpassableSmallG = CGraphic::New(patchEditorPath + "impassable-small.png");
597 ImpassableSmallG->Load();
598 PatchAddIcon(leftX, topY, ImpassableG, ButtonImpassable);
599 FlagMap[ButtonImpassable] = MapFieldUnpassable;
600
601 CoastSmallG = CGraphic::New(patchEditorPath + "coast-small.png");
602 CoastSmallG->Load();
603 CoastG = CGraphic::New(patchEditorPath + "coast.png");
604 CoastG->Load();
605 PatchAddIcon(rightX, topY, CoastG, ButtonCoast);
606 FlagMap[ButtonCoast] = MapFieldCoastAllowed;
607
608 ShallowWaterG = CGraphic::New(patchEditorPath + "water.png");
609 ShallowWaterG->Load();
610 ShallowWaterSmallG = CGraphic::New(patchEditorPath + "water-small.png");
611 ShallowWaterSmallG->Load();
612 PatchAddIcon(leftX, topY + (48 + spacing), ShallowWaterG, ButtonShallowWater);
613 FlagMap[ButtonShallowWater] = MapFieldShallowWater;
614
615 DeepWaterG = CGraphic::New(patchEditorPath + "deep-sea.png");
616 DeepWaterG->Load();
617 DeepWaterSmallG = CGraphic::New(patchEditorPath + "deep-sea-small.png");
618 DeepWaterSmallG->Load();
619 PatchAddIcon(rightX, topY + (48 + spacing), DeepWaterG, ButtonDeepWater);
620 FlagMap[ButtonDeepWater] = MapFieldDeepWater;
621
622 for (int i = 0; i <= NumSpeeds; ++i) {
623 std::ostringstream o;
624 o << patchEditorPath << "speed" << i << ".png";
625 SpeedG[i] = CGraphic::New(o.str());
626 SpeedG[i]->Load();
627 o.str(std::string());
628 o.clear();
629 o << patchEditorPath << "speed" << i << "-small.png";
630 SpeedSmallG[i] = CGraphic::New(o.str());
631 SpeedSmallG[i]->Load();
632 PatchAddIcon((!(i & 1) ? leftX : rightX), topY + (48 + spacing) * ((i + 4) / 2), SpeedG[i], (PatchButton)(ButtonSpeed0 + i));
633 FlagMap[ButtonSpeed0 + i] = i;
634 }
635
636 TransparentG = CGraphic::New(patchEditorPath + "transparent.png");
637 TransparentG->Load();
638 TransparentSmallG = CGraphic::New(patchEditorPath + "transparent-small.png");
639 TransparentSmallG->Load();
640 PatchAddIcon(leftX, topY + (48 + spacing) * ((NumSpeeds + 5) / 2), TransparentG, ButtonTransparent);
641 FlagMap[ButtonTransparent] = MapFieldTransparent;
642 }
643
644 static void PatchFreeIcons()
645 {
646 CGraphic::Free(ImpassableG);
647 ImpassableG = NULL;
648 CGraphic::Free(ImpassableSmallG);
649 ImpassableSmallG = NULL;
650
651 CGraphic::Free(CoastG);
652 CoastG = NULL;
653 CGraphic::Free(CoastSmallG);
654 CoastSmallG = NULL;
655
656 CGraphic::Free(ShallowWaterG);
657 ShallowWaterG = NULL;
658 CGraphic::Free(ShallowWaterSmallG);
659 ShallowWaterSmallG = NULL;
660
661 CGraphic::Free(DeepWaterG);
662 DeepWaterG = NULL;
663 CGraphic::Free(DeepWaterSmallG);
664 DeepWaterSmallG = NULL;
665
666 PatchIcons.clear();
667 FlagMap.clear();
668 }
669
670 void StartPatchEditor(const std::string &patchName)
671 {
672 std::string name;
673 if (patchName.substr(0, 5) != "user-") {
674 name = "user-" + patchName;
675 if (Map.PatchManager.getPatchType(name) == NULL) {
676 CPatchType *patchType = Map.PatchManager.getPatchType(patchName);
677 Map.PatchManager.newPatchType(name,
678 patchType->getFile(),
679 patchType->getTileWidth(), patchType->getTileHeight(),
680 patchType->getFlags());
681 }
682 } else {
683 name = patchName;
684 }
685
686 Patch = Map.PatchManager.add(name, 0, 0);
687 if (!Patch) {
688 fprintf(stderr, "Invalid patch name: %s\n", patchName.c_str());
689 return;
690 }
691
692 PatchLoadIcons();
693
694 PatchEditorMainLoop();
695
696 PatchFreeIcons();
697
698 Map.PatchManager.clear();
699 }
700
701 //@}