Debian Codegraph @

engine/editor/editloop.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 editloop.cpp - The editor main loop. */
11 //
12 // (c) Copyright 2002-2010 by Lutz Sammer and 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 /*----------------------------------------------------------------------------
32 -- Includes
33 ----------------------------------------------------------------------------*/
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <sstream>
38 #include <deque>
39
40 #include "stratagus.h"
41 #include "unittype.h"
42 #include "unit_cache.h"
43 #include "video.h"
44 #include "map.h"
45 #include "patch.h"
46 #include "patch_type.h"
47 #include "minimap.h"
48 #include "settings.h"
49 #include "network.h"
50 #include "sound_server.h"
51 #include "ui.h"
52 #include "interface.h"
53 #include "font.h"
54 #include "widgets.h"
55 #include "editor.h"
56 #include "results.h"
57 #include "menus.h"
58 #include "sound.h"
59 #include "iolib.h"
60 #include "iocompat.h"
61 #include "replay.h"
62 #include "guichan.h"
63
64 #include "script.h"
65
66 extern void DoScrollArea(int state, bool fast);
67 extern void DrawGuichanWidgets();
68 extern void CleanGame();
69
70 /*----------------------------------------------------------------------------
71 -- Defines
72 ----------------------------------------------------------------------------*/
73
74 #define IconSpacing (IconWidth + 2)
75 #define UNIT_ICON_X (IconSpacing) /// Unit mode icon
76 #define UNIT_ICON_Y (0) /// Unit mode icon
77 #define PATCH_ICON_X (IconSpacing * 2) /// Tile mode icon
78 #define PATCH_ICON_Y (0) /// Tile mode icon
79 #define START_ICON_X (IconSpacing * 3) /// Start mode icon
80 #define START_ICON_Y (0) /// Start mode icon
81
82 /*----------------------------------------------------------------------------
83 -- Variables
84 ----------------------------------------------------------------------------*/
85
86 CEditor Editor;
87
88 const char *EditorStartFile; /// Editor CCL start file
89
90 static int IconWidth; /// Icon width in panels
91 static int IconHeight; /// Icon height in panels
92
93 static int ButtonPanelWidth;
94 static int ButtonPanelHeight;
95
96 static CPatch *PatchUnderCursor; /// Patch under cursor
97 static bool UnitPlacedThisPress = false; /// Only allow one unit per press
98 static bool PatchPlacedThisPress = false; /// Only allow one patch per press
99 static bool UpdateMinimap = false; /// Update units on the minimap
100 static bool UpdateMinimapTerrain = false; /// Terrain has changed, minimap needs updating
101 static int VisibleIcons; /// Number of icons that are visible at a time
102
103 enum _mode_buttons_ {
104 SelectButton = 201, /// Select mode button
105 UnitButton, /// Unit mode button
106 PatchButton, /// Patch mode button
107 StartButton
108 };
109
110 enum EditorActionType
111 {
112 EditorActionTypePlaceUnit,
113 EditorActionTypeRemoveUnit,
114 EditorActionTypePlacePatch,
115 EditorActionTypeRemovePatch,
116 };
117
118 struct EditorAction
119 {
120 EditorActionType Type;
121 bool IsFirstOfGroup;
122 int X;
123 int Y;
124 CUnitType *UnitType;
125 CPlayer *Player;
126 const CPatchType *PatchType;
127 };
128
129 static std::deque<EditorAction> EditorUndoActions;
130 static std::deque<EditorAction> EditorRedoActions;
131
132 static void EditorUndoAction(const EditorAction &);
133 static void EditorRedoAction(const EditorAction &);
134 static void EditorAddUndoAction(EditorAction action);
135
136 extern gcn::Gui *Gui;
137 static gcn::Container *editorContainer;
138 static gcn::Slider *editorUnitSlider;
139 static gcn::DropDown *editorUnitDropDown;
140 static StringListModel *editorUnitListModel;
141 static gcn::Slider *editorPatchSlider;
142 static gcn::DropDown *editorPatchDropDown;
143 static StringListModel *editorPatchListModel;
144
145 class EditorUnitSliderListener : public gcn::ActionListener
146 {
147 public:
148 virtual void action(const std::string &eventId)
149 {
150 int iconsPerStep = VisibleIcons;
151 int steps = (Editor.ShownUnitTypes.size() + iconsPerStep - 1) / iconsPerStep;
152 double value = editorUnitSlider->getValue();
153 for (int i = 1; i <= steps; ++i)
154 {
155 if (value <= (double)i / steps)
156 {
157 Editor.UnitIndex = iconsPerStep * (i - 1);
158 break;
159 }
160 }
161 }
162 };
163
164 static EditorUnitSliderListener *editorUnitSliderListener;
165
166 typedef bool (*UnitFilterFunc)(const CUnitType *);
167 static void RecalculateShownUnits(UnitFilterFunc filter);
168
169 static bool unitFilterAll(const CUnitType *type)
170 {
171 return true;
172 }
173 static bool unitFilterUnits(const CUnitType *type)
174 {
175 return !type->Building;
176 }
177 static bool unitFilterBuildings(const CUnitType *type)
178 {
179 return type->Building && !type->Neutral;
180 }
181 static bool unitFilterNeutral(const CUnitType *type)
182 {
183 return type->Neutral;
184 }
185
186 struct UnitFilter
187 {
188 UnitFilterFunc filter;
189 std::string name;
190 };
191
192 static UnitFilter unitFilters[] = {
193 { &unitFilterAll, "All" },
194 { &unitFilterUnits, "Units" },
195 { &unitFilterBuildings, "Buildings" },
196 { &unitFilterNeutral, "Neutral" },
197 };
198
199 class EditorUnitDropDownListener : public gcn::ActionListener
200 {
201 public:
202 virtual void action(const std::string &eventId)
203 {
204 int selected = editorUnitDropDown->getSelected();
205 RecalculateShownUnits(unitFilters[selected].filter);
206 }
207 };
208
209 static EditorUnitDropDownListener *editorUnitDropDownListener;
210
211 class EditorPatchSliderListener : public gcn::ActionListener
212 {
213 public:
214 virtual void action(const std::string &eventId)
215 {
216 int iconsPerStep = VisibleIcons;
217 int steps = (Editor.ShownPatchTypes.size() + iconsPerStep - 1) / iconsPerStep;
218 double value = editorPatchSlider->getValue();
219 for (int i = 1; i <= steps; ++i)
220 {
221 if (value <= (double)i / steps)
222 {
223 Editor.PatchIndex = iconsPerStep * (i - 1);
224 break;
225 }
226 }
227 }
228 };
229
230 static EditorPatchSliderListener *editorPatchSliderListener;
231
232 static void RecalculateShownPatches(const std::string &theme);
233
234 class EditorPatchDropDownListener : public gcn::ActionListener
235 {
236 public:
237 virtual void action(const std::string &eventId)
238 {
239 int selected = editorPatchDropDown->getSelected();
240 const std::string &theme = selected != 0 ? editorPatchListModel->getElementAt(selected) : "";
241 RecalculateShownPatches(theme);
242 }
243 };
244
245 static EditorPatchDropDownListener *editorPatchDropDownListener;
246
247 /*----------------------------------------------------------------------------
248 -- Functions
249 ----------------------------------------------------------------------------*/
250
251 extern void InitDefinedVariables();
252
253 /**
254 ** Set the editor's select icon
255 **
256 ** @param icon The icon to use.
257 */
258 void SetEditorSelectIcon(const std::string &icon)
259 {
260 Editor.Select.Name = icon;
261 }
262
263 /**
264 ** Set the editor's units icon
265 **
266 ** @param icon The icon to use.
267 */
268 void SetEditorUnitsIcon(const std::string &icon)
269 {
270 Editor.Units.Name = icon;
271 }
272
273 /**
274 ** Set the editor's patch icon
275 **
276 ** @param icon The icon to use.
277 */
278 void SetEditorPatchIcon(const std::string &icon)
279 {
280 Editor.Patch.Name = icon;
281 }
282
283 /**
284 ** Set the editor's start location unit
285 **
286 ** @param name The name of the unit to use.
287 */
288 void SetEditorStartUnit(const std::string &name)
289 {
290 Editor.StartUnitName = name;
291 }
292
293 /**
294 ** Called after a patch has been placed, removed, or moved
295 */
296 static void PatchChanged()
297 {
298 int cursorMapX = UI.MouseViewport->Viewport2MapX(CursorX);
299 int cursorMapY = UI.MouseViewport->Viewport2MapY(CursorY);
300
301 PatchUnderCursor = Map.PatchManager.getPatch(cursorMapX, cursorMapY);
302 UpdateMinimapTerrain = true;
303 }
304
305 /*----------------------------------------------------------------------------
306 -- Actions
307 ----------------------------------------------------------------------------*/
308
309 /**
310 ** Place a unit.
311 **
312 ** @param x X map tile coordinate.
313 ** @param y Y map tile coordinate.
314 ** @param type Unit type to edit.
315 ** @param player Player owning the unit.
316 */
317 static void EditorActionPlaceUnit(int x, int y, CUnitType *type, CPlayer *player)
318 {
319 CUnit *unit;
320 CBuildRestrictionOnTop *b;
321
322 if (type->Neutral)
323 {
324 player = &Players[PlayerNumNeutral];
325 }
326
327 unit = MakeUnitAndPlace(x, y, type, player);
328 if (unit == NoUnitP)
329 {
330 DebugPrint("Unable to allocate Unit");
331 return;
332 }
333
334 b = OnTopDetails(unit, NULL);
335 if (b && b->ReplaceOnBuild)
336 {
337 int n;
338 CUnit *table[UnitMax];
339
340 n = UnitCache.Select(x, y, table, UnitMax);
341 while (n--)
342 {
343 if (table[n]->Type == b->Parent)
344 {
345 // We capture the value of what is beneath.
346 memcpy(unit->ResourcesHeld, table[n]->ResourcesHeld, sizeof(unit->ResourcesHeld));
347 table[n]->Remove(NULL); // Destroy building beneath
348 UnitLost(table[n]);
349 UnitClearOrders(table[n]);
350 table[n]->Release();
351 break;
352 }
353 }
354 }
355
356 UpdateMinimap = true;
357 }
358
359 /**
360 ** Place a unit.
361 **
362 ** @param x X map tile coordinate.
363 ** @param y Y map tile coordinate.
364 ** @param type Unit type to edit.
365 ** @param player Player owning the unit.
366 */
367 static void EditorPlaceUnit(int x, int y, CUnitType *type, CPlayer *player)
368 {
369 EditorAction editorAction;
370 editorAction.Type = EditorActionTypePlaceUnit;
371 editorAction.IsFirstOfGroup = true;
372 editorAction.X = x;
373 editorAction.Y = y;
374 editorAction.UnitType = type;
375 editorAction.Player = player;
376
377 EditorActionPlaceUnit(x, y, type, player);
378 EditorAddUndoAction(editorAction);
379 }
380
381 /**
382 ** Remove a unit
383 */
384 static void EditorActionRemoveUnit(CUnit *unit)
385 {
386 unit->Remove(NULL);
387 UnitLost(unit);
388 UnitClearOrders(unit);
389 unit->Release();
390 UI.StatusLine.Set(_("Unit deleted"));
391 UpdateMinimap = true;
392 }
393
394 /**
395 ** Remove a unit
396 */
397 static void EditorRemoveUnit(CUnit *unit)
398 {
399 EditorAction editorAction;
400 editorAction.Type = EditorActionTypeRemoveUnit;
401 editorAction.IsFirstOfGroup = true;
402 editorAction.X = unit->X;
403 editorAction.Y = unit->Y;
404 editorAction.UnitType = unit->Type;
405 editorAction.Player = unit->Player;
406
407 EditorActionRemoveUnit(unit);
408 EditorAddUndoAction(editorAction);
409 }
410
411 /**
412 ** Place a patch
413 */
414 static void EditorActionPlacePatch(int x, int y, const CPatchType *patchType)
415 {
416 Map.PatchManager.add(patchType->getName(), x, y);
417 PatchChanged();
418 }
419
420 /**
421 ** Place a patch
422 */
423 static void EditorPlacePatch(int x, int y, const CPatchType *patchType,
424 bool *isFirstOfGroup)
425 {
426 EditorAction editorAction;
427 editorAction.Type = EditorActionTypePlacePatch;
428 editorAction.IsFirstOfGroup = *isFirstOfGroup;
429 editorAction.X = x;
430 editorAction.Y = y;
431 editorAction.PatchType = patchType;
432
433 *isFirstOfGroup = false;
434
435 EditorActionPlacePatch(x, y, patchType);
436 EditorAddUndoAction(editorAction);
437 }
438
439 /**
440 ** Remove a patch
441 */
442 static void EditorActionRemovePatch(CPatch *patch)
443 {
444 Map.PatchManager.remove(patch);
445 PatchChanged();
446 }
447
448 /**
449 ** Remove a patch
450 */
451 static void EditorRemovePatch(CPatch *patch, bool *isFirstOfGroup)
452 {
453 EditorAction editorAction;
454 editorAction.Type = EditorActionTypeRemovePatch;
455 editorAction.IsFirstOfGroup = *isFirstOfGroup;
456 editorAction.X = patch->getX();
457 editorAction.Y = patch->getY();
458 editorAction.PatchType = patch->getType();
459
460 *isFirstOfGroup = false;
461
462 EditorActionRemovePatch(patch);
463 EditorAddUndoAction(editorAction);
464 }
465
466 /**
467 ** Remove all patches at a location
468 */
469 static void EditorRemovePatches(int x, int y, int w, int h,
470 bool *isFirstOfGroup)
471 {
472 for (int i = 0; i < w; ++i)
473 {
474 for (int j = 0; j < h; ++j)
475 {
476 CPatch *patch = Map.PatchManager.getPatch(x + i, y + j);
477 if (patch)
478 {
479 EditorRemovePatch(patch, isFirstOfGroup);
480 }
481 }
482 }
483 }
484
485 void CEditor::TileSelectedPatch()
486 {
487 bool isFirstOfGroup = true;
488
489 if (Editor.State == EditorEditPatch && Editor.SelectedPatchIndex != -1)
490 {
491 // Remove all patches
492 const CPatchType *patchType = Editor.ShownPatchTypes[Editor.SelectedPatchIndex]->PatchType;
493 EditorRemovePatches(0, 0, Map.Info.MapWidth, Map.Info.MapHeight,
494 &isFirstOfGroup);
495
496 // Create the new patches
497 for (int j = 0; j <= Map.Info.MapHeight - patchType->getTileHeight(); j += patchType->getTileHeight())
498 {
499 for (int i = 0; i <= Map.Info.MapWidth - patchType->getTileWidth(); i += patchType->getTileWidth())
500 {
501 EditorPlacePatch(i, j, patchType,
502 &isFirstOfGroup);
503 }
504 }
505
506 PatchPlacedThisPress = true;
507 }
508 }
509
510 /*----------------------------------------------------------------------------
511 -- Undo/Redo
512 ----------------------------------------------------------------------------*/
513
514 static void EditorUndoAction(const EditorAction &action)
515 {
516 switch (action.Type)
517 {
518 case EditorActionTypePlaceUnit:
519 {
520 CUnit *unit = UnitOnMapTile(action.X, action.Y, action.UnitType->UnitType);
521 EditorActionRemoveUnit(unit);
522 break;
523 }
524
525 case EditorActionTypeRemoveUnit:
526 EditorActionPlaceUnit(action.X, action.Y, action.UnitType, action.Player);
527 break;
528
529 case EditorActionTypePlacePatch:
530 {
531 CPatch *patch = Map.PatchManager.getPatch(action.X, action.Y);
532 Assert(patch->getType() == action.PatchType);
533 EditorActionRemovePatch(patch);
534 break;
535 }
536
537 case EditorActionTypeRemovePatch:
538 EditorActionPlacePatch(action.X, action.Y, action.PatchType);
539 break;
540 }
541 }
542
543 static void EditorUndoGroup()
544 {
545 if (EditorUndoActions.empty())
546 {
547 return;
548 }
549
550 // If the oldest undo group is not properly tagged, detect
551 // that bug before popping the actions off the queue, so that
552 // it is easier to debug. In release builds, this assertion
553 // does not run, and the EditorUndoActions.empty() check in
554 // the loop instead prevents crashes.
555 Assert(EditorUndoActions.front().IsFirstOfGroup);
556
557 EditorAction action;
558 do
559 {
560 action = EditorUndoActions.back();
561 EditorUndoActions.pop_back();
562 EditorUndoAction(action);
563 EditorRedoActions.push_back(action);
564 }
565 while (!action.IsFirstOfGroup && !EditorUndoActions.empty());
566 }
567
568 static void EditorRedoAction(const EditorAction &action)
569 {
570 switch (action.Type)
571 {
572 case EditorActionTypePlaceUnit:
573 EditorActionPlaceUnit(action.X, action.Y, action.UnitType, action.Player);
574 break;
575
576 case EditorActionTypeRemoveUnit:
577 {
578 CUnit *unit = UnitOnMapTile(action.X, action.Y, action.UnitType->UnitType);
579 EditorActionRemoveUnit(unit);
580 break;
581 }
582
583 case EditorActionTypePlacePatch:
584 EditorActionPlacePatch(action.X, action.Y, action.PatchType);
585 break;
586
587 case EditorActionTypeRemovePatch:
588 {
589 CPatch *patch = Map.PatchManager.getPatch(action.X, action.Y);
590 Assert(patch->getType() == action.PatchType);
591 EditorActionRemovePatch(patch);
592 break;
593 }
594 }
595 }
596
597 static void EditorRedoGroup()
598 {
599 if (EditorRedoActions.empty())
600 {
601 return;
602 }
603
604 // Because moving the actions from EditorUndoActions to
605 // EditorRedoActions reverses their sequence but does not
606 // alter the IsFirstOfGroup flags, the flagged action is the
607 // last one pushed to the back of EditorRedoActions and the
608 // first one to be popped from there. The following assertion
609 // and loop thus differ from the ones in EditorUndoGroup().
610
611 Assert(EditorRedoActions.back().IsFirstOfGroup);
612
613 EditorAction action;
614 do
615 {
616 action = EditorRedoActions.back();
617 EditorRedoActions.pop_back();
618 EditorRedoAction(action);
619 EditorUndoActions.push_back(action);
620 }
621 while (!EditorRedoActions.empty()
622 && !EditorRedoActions.back().IsFirstOfGroup);
623 }
624
625 static void EditorAddUndoAction(EditorAction action)
626 {
627 EditorRedoActions.clear();
628 EditorUndoActions.push_back(action);
629 }
630
631 /*----------------------------------------------------------------------------
632 -- Other
633 ----------------------------------------------------------------------------*/
634
635 /**
636 ** Calculate the number of icons that can be displayed
637 **
638 ** @return Number of icons that can be displayed.
639 */
640 static int CalculateVisibleIcons()
641 {
642 int i;
643 int x;
644 int count;
645
646 i = 0;
647 count = 0;
648 x = UI.ButtonPanel.Y + 24;
649 while (x < UI.ButtonPanel.Y + ButtonPanelHeight - IconHeight)
650 {
651 ++i;
652 x += IconHeight + 2;
653 }
654 x = UI.ButtonPanel.X + 10;
655 while (x < UI.ButtonPanel.X + ButtonPanelWidth - IconWidth)
656 {
657 count += i;
658 x += IconWidth + 8;
659 }
660 return count;
661 }
662
663 /**
664 ** Calculate the max height and the max width of icons,
665 ** and assign them to IconHeight and IconWidth
666 */
667 static void CalculateMaxIconSize()
668 {
669 const CUnitType *type;
670 const CIcon *icon;
671
672 IconWidth = 0;
673 IconHeight = 0;
674 for (int i = 0; i < (int)Editor.UnitTypes.size(); ++i)
675 {
676 type = UnitTypeByIdent(Editor.UnitTypes[i]);
677 Assert(type && type->Icon.Icon);
678 icon = type->Icon.Icon;
679 if (IconWidth < icon->G->Width)
680 {
681 IconWidth = icon->G->Width;
682 }
683 if (IconHeight < icon->G->Height)
684 {
685 IconHeight = icon->G->Height;
686 }
687 }
688 }
689
690
691 /**
692 ** Recalculate the shown units.
693 */
694 static void RecalculateShownUnits(UnitFilterFunc filter)
695 {
696 const CUnitType *type;
697
698 Editor.ShownUnitTypes.clear();
699
700 for (int i = 0; i < (int)Editor.UnitTypes.size(); ++i)
701 {
702 type = UnitTypeByIdent(Editor.UnitTypes[i]);
703 if (!filter || filter(type))
704 {
705 Editor.ShownUnitTypes.push_back(type);
706 }
707 }
708
709 if (Editor.UnitIndex >= (int)Editor.ShownUnitTypes.size())
710 {
711 Editor.UnitIndex = Editor.ShownUnitTypes.size() / VisibleIcons * VisibleIcons;
712 }
713 // Quick & dirty make them invalid
714 Editor.CursorUnitIndex = -1;
715 Editor.SelectedUnitIndex = -1;
716 }
717
718 /**
719 ** Recalculate the shown patches.
720 */
721 static void RecalculateShownPatches(const std::string &theme)
722 {
723 Editor.ShownPatchTypes.clear();
724
725 for (std::map<std::string, CPatchIcon *>::iterator i = Editor.AllPatchTypes.begin(); i != Editor.AllPatchTypes.end(); ++i)
726 {
727 CPatchIcon *patchIcon = i->second;
728 if (theme.empty() || patchIcon->PatchType->getTheme() == theme)
729 {
730 Editor.ShownPatchTypes.push_back(patchIcon);
731 }
732 }
733
734 if (Editor.PatchIndex >= (int)Editor.ShownPatchTypes.size())
735 {
736 Editor.PatchIndex = Editor.ShownPatchTypes.size() / VisibleIcons * VisibleIcons;
737 }
738 // Quick & dirty make them invalid
739 Editor.CursorPatchIndex = -1;
740 Editor.SelectedPatchIndex = -1;
741 }
742
743 /*----------------------------------------------------------------------------
744 -- Display
745 ----------------------------------------------------------------------------*/
746
747 /**
748 ** Draw a table with the players
749 */
750 static void DrawPlayers()
751 {
752 int x = UI.InfoPanel.X + 8;
753 int y = UI.InfoPanel.Y + 4 + IconHeight + 10;
754
755 for (int i = 0; i < PlayerMax; ++i)
756 {
757 if (i == Editor.CursorPlayer && Map.Info.PlayerType[i] != PlayerNobody)
758 {
759 Video.DrawRectangle(ColorWhite, x + i * 20, y, 20, 20);
760 }
761 Video.DrawRectangle(
762 i == Editor.CursorPlayer && Map.Info.PlayerType[i] != PlayerNobody ?
763 ColorWhite : ColorGray,
764 x + i * 20, y, 19, 19);
765 if (Map.Info.PlayerType[i] != PlayerNobody)
766 {
767 Video.FillRectangle(Players[i].Color, x + 1 + i * 20, y + 1,
768 17, 17);
769 }
770 if (i == Editor.SelectedPlayer)
771 {
772 Video.DrawRectangle(ColorGreen, x + 1 + i * 20, y + 1, 17, 17);
773 }
774 std::ostringstream o;
775 o << i;
776 VideoDrawTextCentered(x + i * 20 + 9, y + 3, SmallFont, o.str());
777 }
778
779 x = UI.InfoPanel.X + 4;
780 y += 18 * 1 + 4;
781 if (Editor.SelectedPlayer != -1)
782 {
783 std::ostringstream o;
784 o << _("Player") << " " << Editor.SelectedPlayer << " ";
785
786 switch (Map.Info.PlayerType[Editor.SelectedPlayer])
787 {
788 case PlayerNeutral:
789 o << _("Neutral");
790 break;
791 case PlayerNobody:
792 default:
793 o << _("Nobody");
794 break;
795 case PlayerPerson:
796 o << _("Person");
797 break;
798 case PlayerComputer:
799 case PlayerRescuePassive:
800 case PlayerRescueActive:
801 o << _("Computer");
802 break;
803 }
804
805 VideoDrawText(x, y, GameFont, o.str());
806 }
807 }
808
809
810 /**
811 ** Draw unit icons.
812 */
813 static void DrawUnitIcons()
814 {
815 int x;
816 int y;
817 int i;
818 CIcon *icon;
819
820 x = UI.InfoPanel.X + 10;
821 y = UI.InfoPanel.Y + 140;
822
823 //
824 // Draw the unit icons.
825 //
826 y = UI.ButtonPanel.Y + 24;
827 i = Editor.UnitIndex;
828 while (y < UI.ButtonPanel.Y + ButtonPanelHeight - IconHeight)
829 {
830 if (i >= (int)Editor.ShownUnitTypes.size())
831 {
832 break;
833 }
834 x = UI.ButtonPanel.X + 10;
835 while (x < UI.ButtonPanel.X + ButtonPanelWidth - IconWidth)
836 {
837 if (i >= (int) Editor.ShownUnitTypes.size())
838 {
839 break;
840 }
841 icon = Editor.ShownUnitTypes[i]->Icon.Icon;
842 icon->DrawIcon(Players + Editor.SelectedPlayer, x, y);
843
844 Video.DrawRectangleClip(ColorGray, x, y, icon->G->Width, icon->G->Height);
845 if (i == Editor.SelectedUnitIndex)
846 {
847 Video.DrawRectangleClip(ColorGreen, x + 1, y + 1,
848 icon->G->Width - 2, icon->G->Height - 2);
849 }
850 if (i == Editor.CursorUnitIndex)
851 {
852 Video.DrawRectangleClip(ColorWhite, x - 1, y - 1,
853 icon->G->Width + 2, icon->G->Height + 2);
854 }
855
856 x += IconWidth + 8;
857 ++i;
858 }
859 y += IconHeight + 2;
860 }
861 }
862
863 /**
864 ** Draw patch icons.
865 */
866 static void DrawPatchIcons()
867 {
868 int x;
869 int y;
870 int i;
871 CGraphic *g;
872
873 x = UI.InfoPanel.X + 10;
874 y = UI.InfoPanel.Y + 140;
875
876 //
877 // Draw the patch icons.
878 //
879 y = UI.ButtonPanel.Y + 24;
880 i = Editor.PatchIndex;
881 while (y < UI.ButtonPanel.Y + ButtonPanelHeight - IconHeight)
882 {
883 if (i >= (int)Editor.ShownPatchTypes.size())
884 {
885 break;
886 }
887 x = UI.ButtonPanel.X + 10;
888 while (x < UI.ButtonPanel.X + ButtonPanelWidth - IconWidth)
889 {
890 if (i >= (int) Editor.ShownPatchTypes.size())
891 {
892 break;
893 }
894 g = Editor.ShownPatchTypes[i]->G;
895 g->DrawClip(x, y);
896
897 Video.DrawRectangleClip(ColorGray, x, y, IconWidth, IconHeight);
898 if (i == Editor.SelectedPatchIndex)
899 {
900 Video.DrawRectangleClip(ColorGreen, x + 1, y + 1,
901 IconWidth - 2, IconHeight - 2);
902 }
903 if (i == Editor.CursorPatchIndex)
904 {
905 Video.DrawRectangleClip(ColorWhite, x - 1, y - 1,
906 IconWidth + 2, IconHeight + 2);
907 }
908
909 x += IconWidth + 8;
910 ++i;
911 }
912 y += IconHeight + 2;
913 }
914 }
915
916 /**
917 ** Draw the editor panels.
918 */
919 static void DrawEditorPanel()
920 {
921 int x;
922 int y;
923 CIcon *icon;
924
925 x = UI.InfoPanel.X + 4;
926 y = UI.InfoPanel.Y + 4;
927
928 //
929 // Select / Units / Patch / Start
930 // FIXME: wrong button style
931 //
932 icon = Editor.Select.Icon;
933 icon->DrawUnitIcon(Players, UI.SingleSelectedButton->Style,
934 (ButtonUnderCursor == SelectButton ? IconActive : 0) |
935 (Editor.State == EditorSelecting ? IconSelected : 0),
936 x, y, "");
937
938 icon = Editor.Units.Icon;
939 icon->DrawUnitIcon(Players, UI.SingleSelectedButton->Style,
940 (ButtonUnderCursor == UnitButton ? IconActive : 0) |
941 (Editor.State == EditorEditUnit ? IconSelected : 0),
942 x + UNIT_ICON_X, y + UNIT_ICON_Y, "");
943
944 icon = Editor.Patch.Icon;
945 icon->DrawUnitIcon(Players, UI.SingleSelectedButton->Style,
946 (ButtonUnderCursor == PatchButton ? IconActive : 0) |
947 (Editor.State == EditorEditPatch ? IconSelected : 0),
948 x + PATCH_ICON_X, y + PATCH_ICON_Y, "");
949
950 icon = Editor.StartUnit->Icon.Icon;
951 icon->DrawUnitIcon(Players, UI.SingleSelectedButton->Style,
952 (ButtonUnderCursor == StartButton ? IconActive : 0) |
953 (Editor.State == EditorSetStartLocation ? IconSelected : 0),
954 x + START_ICON_X, y + START_ICON_Y, "");
955
956 switch (Editor.State)
957 {
958 case EditorSelecting:
959 break;
960 case EditorEditPatch:
961 DrawPatchIcons();
962 break;
963 case EditorSetStartLocation:
964 DrawPlayers();
965 break;
966 case EditorEditUnit:
967 DrawPlayers();
968 DrawUnitIcons();
969 break;
970 }
971 }
972
973 /**
974 ** Draw special cursor on map.
975 */
976 static void DrawMapCursor()
977 {
978 if (!CursorBuilding)
979 {
980 switch (Editor.State)
981 {
982 case EditorSelecting:
983 case EditorEditPatch:
984 break;
985 case EditorEditUnit:
986 if (Editor.SelectedUnitIndex != -1)
987 {
988 CursorBuilding = const_cast<CUnitType *> (Editor.ShownUnitTypes[Editor.SelectedUnitIndex]);
989 }
990 break;
991 case EditorSetStartLocation:
992 CursorBuilding = const_cast<CUnitType *>(Editor.StartUnit);
993 break;
994 }
995 }
996
997 //
998 // Draw map cursor
999 //
1000 if (UI.MouseViewport && !CursorBuilding)
1001 {
1002 int x, y;
1003
1004 x = UI.MouseViewport->Viewport2MapX(CursorX);
1005 y = UI.MouseViewport->Viewport2MapY(CursorY);
1006 x = UI.MouseViewport->Map2ViewportX(x);
1007 y = UI.MouseViewport->Map2ViewportY(y);
1008
1009 if (Editor.State == EditorEditPatch)
1010 {
1011 if (Editor.SelectedPatchIndex != -1)
1012 {
1013 // Draw an outline where the new patch would be placed
1014 const CPatchType *patchType = Editor.ShownPatchTypes[Editor.SelectedPatchIndex]->PatchType;
1015 PushClipping();
1016 SetClipping(UI.MouseViewport->X, UI.MouseViewport->Y,
1017 UI.MouseViewport->EndX, UI.MouseViewport->EndY);
1018 Video.DrawRectangleClip(ColorWhite, x, y,
1019 patchType->getTileWidth() * TileSizeX,
1020 patchType->getTileHeight() * TileSizeY);
1021 PopClipping();
1022 }
1023 }
1024 else
1025 {
1026 //
1027 // If there is a unit under the cursor, its selection thing
1028 // is drawn somewhere else (Check DrawUnitSelection.)
1029 //
1030 if (!UnitUnderCursor)
1031 {
1032 PushClipping();
1033 SetClipping(UI.MouseViewport->X, UI.MouseViewport->Y,
1034 UI.MouseViewport->EndX, UI.MouseViewport->EndY);
1035 Video.DrawRectangleClip(ColorWhite, x, y, TileSizeX, TileSizeY);
1036 PopClipping();
1037 }
1038 }
1039 }
1040 }
1041
1042 /**
1043 ** Draw some flags of map fields as a colorful overlay.
1044 ** This is intended to let the map designer easily see
1045 ** which paths are blocked and which ones are not.
1046 ** (However, if the map designer has difficulty with that,
1047 ** then the player probably will too; it would be best
1048 ** to make the boundaries clearly visible in the graphics
1049 ** of the patches.)
1050 **
1051 ** This function reads MapFieldLandUnit and related flags
1052 ** from CUnitType::FieldFlags rather than CMapField::Flags;
1053 ** thus, it cannot be used for testing whether the latter
1054 ** is correctly maintained.
1055 **
1056 ** @param vp Viewport in which to draw.
1057 */
1058 static void EditorDrawTerrainFlags(const CViewport *vp)
1059 {
1060 PushClipping();
1061 SetClipping(vp->X, vp->Y, vp->EndX, vp->EndY);
1062
1063 for (int mapY = std::max(vp->MapY, 0);
1064 mapY < vp->MapY + vp->MapHeight && mapY < Map.Info.MapHeight;
1065 ++mapY)
1066 {
1067 const int screenY = vp->Map2ViewportY(mapY);
1068 for (int mapX = std::max(vp->MapX, 0);
1069 mapX < vp->MapX + vp->MapWidth && mapX < Map.Info.MapWidth;
1070 ++mapX)
1071 {
1072 const int screenX = vp->Map2ViewportX(mapX);
1073 const CMapField *const field = Map.Field(mapX, mapY);
1074 Uint32 color;
1075
1076 if (field->Flags & MapFieldUnpassable)
1077 {
1078 color = ColorRed;
1079 }
1080 else if (field->Flags & MapFieldNoBuilding)
1081 {
1082 color = ColorOrange;
1083 }
1084 else if (field->Flags & MapFieldDeepWater)
1085 {
1086 color = ColorDarkBlue;
1087 }
1088 else if (field->Flags & MapFieldShallowWater)
1089 {
1090 color = ColorBlue;
1091 }
1092 else if (field->Flags & MapFieldCoastAllowed)
1093 {
1094 color = ColorCyan;
1095 }
1096 else if (field->Flags & MapFieldLandAllowed)
1097 {
1098 const int speed = field->Flags & MapFieldSpeedMask;
1099 if (speed == MapFieldNormalSpeed)
1100 {
1101 color = ColorGreen;
1102 }
1103 else if (speed < MapFieldNormalSpeed) // road
1104 {
1105 color = ColorGray;
1106 }
1107 else // difficult terrain
1108 {
1109 color = ColorYellow;
1110 }
1111 }
1112 else // probably no patch here at all
1113 {
1114 color = ColorBlack;
1115 }
1116
1117 Video.FillTransRectangleClip(
1118 color, screenX, screenY,
1119 TileSizeX, TileSizeY, 80);
1120 }
1121 }
1122
1123 // Mark the units that could block other units; but not if the
1124 // editor is in patch-placement mode, because then the user
1125 // presumably wants to ignore the units. Draw each unit as a
1126 // contiguous rectangle even if it spans multiple map tiles.
1127 if (Editor.State != EditorEditPatch)
1128 {
1129 const int marginX = TileSizeX / 8;
1130 const int marginY = TileSizeY / 8;
1131 CUnit *units[UnitMax];
1132 int nunits = FindAndSortUnits(vp, units, UnitMax);
1133
1134 for (int i = 0; i < nunits; ++i)
1135 {
1136 CUnit *const unit = units[i];
1137
1138 // Ignore air units because they typically block
1139 // only each other and are unaffected by the
1140 // terrain. Also ignore decorative units that
1141 // can be walked through; this is why we check
1142 // FieldFlags rather than enum UnitTypeType.
1143 if (unit->Type->FieldFlags & (
1144 MapFieldLandUnit |
1145 MapFieldSeaUnit |
1146 MapFieldBuilding))
1147 {
1148 // The alpha must be pretty high here
1149 // because this feature is useful for
1150 // showing paths in forests and those
1151 // already have much luma variation.
1152 Video.FillTransRectangleClip(
1153 ColorBlack,
1154 vp->Map2ViewportX(unit->X) + marginX,
1155 vp->Map2ViewportY(unit->Y) + marginY,
1156 unit->Type->TileWidth * TileSizeX
1157 - 2 * marginX,
1158 unit->Type->TileHeight * TileSizeY
1159 - 2 * marginY,
1160 96);
1161 }
1162 }
1163 }
1164
1165 PopClipping();
1166 }
1167
1168 /**
1169 ** Draw some flags of map fields as a colorful overlay, if this
1170 ** feature has been enabled. See ::EditorDrawTerrainFlags for
1171 ** more information.
1172 */
1173 static void EditorDrawTerrainFlagsIfEnabled()
1174 {
1175 if (Editor.ShowTerrainFlags)
1176 {
1177 for (const CViewport *vp = UI.Viewports; vp < UI.Viewports + UI.NumViewports; ++vp)
1178 {
1179 EditorDrawTerrainFlags(vp);
1180 }
1181 }
1182 }
1183
1184 /**
1185 ** Draw the start locations of all active players on the map
1186 */
1187 static void DrawStartLocations()
1188 {
1189 for (const CViewport *vp = UI.Viewports; vp < UI.Viewports + UI.NumViewports; ++vp)
1190 {
1191 PushClipping();
1192 SetClipping(vp->X, vp->Y, vp->EndX, vp->EndY);
1193
1194 for (int i = 0; i < PlayerMax; ++i)
1195 {
1196 if (Map.Info.PlayerType[i] != PlayerNobody && Map.Info.PlayerType[i] != PlayerNeutral)
1197 {
1198 int x = vp->Map2ViewportX(Players[i].StartX);
1199 int y = vp->Map2ViewportY(Players[i].StartY);
1200
1201 DrawUnitType(Editor.StartUnit, Editor.StartUnit->Sprite, i, 0, x, y);
1202 }
1203 }
1204
1205 PopClipping();
1206 }
1207 }
1208
1209 /**
1210 ** Show info about unit.
1211 **
1212 ** @param unit Unit pointer.
1213 */
1214 static void ShowUnitInfo(const CUnit *unit)
1215 {
1216 std::ostringstream o;
1217
1218 o << "#" << UnitNumber(unit) << " '" << unit->Type->Name
1219 << "' - " << _("Player") << ": " << unit->Player->Index;
1220
1221 if (unit->Type->CanHarvestFrom)
1222 {
1223 int res = unit->Type->ProductionCosts[0] ? 0 : 1;
1224 o << " - " << _("Amount of") << " " << _(DefaultDisplayResourceNames[res].c_str()) << ": "
1225 << unit->ResourcesHeld[res] / CYCLES_PER_SECOND;
1226 }
1227
1228 UI.StatusLine.Set(o.str());
1229 }
1230
1231 /**
1232 ** Show info about a patch and about one of its fields.
1233 **
1234 ** @param patch Patch pointer. Can be NULL.
1235 ** @param mapX X coordinate of the cursor on the map.
1236 ** @param mapY Y coordinate of the cursor on the map.
1237 */
1238 static void ShowPatchInfo(const CPatch *patch, int mapX, int mapY)
1239 {
1240 std::ostringstream o;
1241
1242 if (patch == NULL)
1243 {
1244 o << _("Patch missing");
1245 }
1246 else
1247 {
1248 o << _("Patch") << " (" << patch->getX() << ", "
1249 << patch->getY() << "): " << patch->getType()->getName();
1250 }
1251
1252 o << " - ";
1253
1254 const CMapField *field = Map.Field(mapX, mapY);
1255 o << _("Field") << " (" << mapX << ", " << mapY << ")";
1256
1257 int comma = 0;
1258 Assert(!(field->Flags & MapFieldTransparent));
1259 if (field->Flags & MapFieldUnpassable)
1260 {
1261 o << (comma++ ? ", " : ": ")
1262 << _("impassable");
1263 }
1264 if (field->Flags & MapFieldLandAllowed)
1265 {
1266 o << (comma++ ? ", " : ": ")
1267 << _("land");
1268 }
1269 if (field->Flags & MapFieldCoastAllowed)
1270 {
1271 o << (comma++ ? ", " : ": ")
1272 << _("coast");
1273 }
1274 if (field->Flags & MapFieldShallowWater)
1275 {
1276 o << (comma++ ? ", " : ": ")
1277 << _("shallow water");
1278 }
1279 if (field->Flags & MapFieldDeepWater)
1280 {
1281 o << (comma++ ? ", " : ": ")
1282 << _("deep water");
1283 }
1284 if (field->Flags & MapFieldNoBuilding)
1285 {
1286 // If you edit here, please check that xgettext still
1287 // copies the TRANSLATORS comment to engine.pot.
1288
1289 o << (comma++ ? ", " : ": ");
1290 // TRANSLATORS: This flag means players cannot build
1291 // here, e.g. because the ground is too soft. There
1292 // may be a building here anyway, if the map designer
1293 // has found a way to circumvent the restriction.
1294 o << _("no building");
1295 }
1296 // Ignore MapFieldLandUnit etc. because this function
1297 // is supposed to describe the patch and not the units.
1298
1299 // The patch editor displays the speed from the flags,
1300 // rather than CMapField::Cost (which is a larger number),
1301 // so do the same here.
1302 int speed = field->Flags & MapFieldSpeedMask;
1303 if (speed != MapFieldNormalSpeed)
1304 {
1305 o << (comma++ ? ", " : ": ")
1306 << _("speed=") << speed;
1307 if (speed < MapFieldNormalSpeed)
1308 {
1309 o << _(" (fast)");
1310 }
1311 else
1312 {
1313 o << _(" (slow)");
1314 }
1315 }
1316
1317 UI.StatusLine.Set(o.str());
1318 }
1319
1320 /**
1321 ** Update editor display.
1322 */
1323 static void EditorUpdateDisplay()
1324 {
1325 DrawMapArea();
1326 EditorDrawTerrainFlagsIfEnabled();
1327
1328 DrawStartLocations();
1329
1330 //
1331 // Fillers
1332 //
1333 for (int i = 0; i < (int)UI.Fillers.size(); ++i)
1334 {
1335 UI.Fillers[i].G->DrawClip(UI.Fillers[i].X, UI.Fillers[i].Y);
1336 }
1337
1338 if (CursorOn == CursorOnMap && Gui->getTop() == editorContainer)
1339 {
1340 DrawMapCursor();
1341 }
1342
1343 //
1344 // Menu button
1345 //
1346 DrawMenuButton(UI.MenuButton.Style,
1347 (ButtonAreaUnderCursor == ButtonAreaMenu
1348 && ButtonUnderCursor == ButtonUnderMenu ? MI_FLAGS_ACTIVE : 0) |
1349 (GameMenuButtonClicked ? MI_FLAGS_CLICKED : 0),
1350 UI.MenuButton.X, UI.MenuButton.Y,
1351 UI.MenuButton.Text);
1352
1353 //
1354 // Minimap
1355 //
1356 if (UI.SelectedViewport)
1357 {
1358 UI.Minimap.Draw(UI.SelectedViewport->MapX, UI.SelectedViewport->MapY);
1359 UI.Minimap.DrawCursor(UI.SelectedViewport->MapX,
1360 UI.SelectedViewport->MapY);
1361 }
1362 //
1363 // Button panel
1364 //
1365 if (UI.ButtonPanel.G)
1366 {
1367 UI.ButtonPanel.G->DrawClip(UI.ButtonPanel.X, UI.ButtonPanel.Y);
1368 }
1369 DrawEditorPanel();
1370
1371 //
1372 // Status line
1373 //
1374 UI.StatusLine.Draw();
1375
1376 DrawGuichanWidgets();
1377
1378 DrawCursor();
1379
1380 // refresh entire screen, so no further invalidate needed
1381 Invalidate();
1382 RealizeVideoMemory();
1383 }
1384
1385 /*----------------------------------------------------------------------------
1386 -- Input / Keyboard / Mouse
1387 ----------------------------------------------------------------------------*/
1388
1389 /**
1390 ** Handle key down.
1391 **
1392 ** @param key Key scancode.
1393 ** @param keychar Character code.
1394 */
1395 static void EditorCallbackKeyDown(unsigned key, unsigned keychar)
1396 {
1397 if (HandleKeyModifiersDown(key, keychar))
1398 {
1399 return;
1400 }
1401
1402 switch (key)
1403 {
1404 case 'f': // ALT+F, CTRL+F toggle fullscreen
1405 if (!(KeyModifiers & (ModifierAlt | ModifierControl)))
1406 {
1407 break;
1408 }
1409 ToggleFullScreen();
1410 break;
1411
1412 case 'v': // 'v' Viewport
1413 if (KeyModifiers & ModifierControl)
1414 {
1415 CycleViewportMode(-1);
1416 }
1417 else
1418 {
1419 CycleViewportMode(1);
1420 }
1421 break;
1422
1423 case 'x': // ALT+X, CTRL+X: Exit editor
1424 if (!(KeyModifiers & (ModifierAlt | ModifierControl)))
1425 {
1426 break;
1427 }
1428 Exit(0);
1429
1430 case 'z':
1431 if (KeyModifiers & ModifierControl)
1432 {
1433 EditorUndoGroup();
1434 }
1435 break;
1436 case 'y':
1437 if (KeyModifiers & ModifierControl)
1438 {
1439 EditorRedoGroup();
1440 }
1441 break;
1442
1443 case SDLK_BACKSPACE:
1444 case SDLK_DELETE:
1445 if (Editor.State != EditorEditUnit && PatchUnderCursor)
1446 {
1447 bool isFirstOfGroup = true;
1448 EditorRemovePatch(PatchUnderCursor, &isFirstOfGroup);
1449 UI.StatusLine.Set(_("Patch deleted"));
1450 }
1451 else if (Editor.State != EditorEditPatch && UnitUnderCursor)
1452 {
1453 EditorRemoveUnit(UnitUnderCursor);
1454 }
1455 break;
1456
1457 case SDLK_UP: // Keyboard scrolling
1458 case SDLK_KP8:
1459 KeyScrollState |= ScrollUp;
1460 break;
1461 case SDLK_DOWN:
1462 case SDLK_KP2:
1463 KeyScrollState |= ScrollDown;
1464 break;
1465 case SDLK_LEFT:
1466 case SDLK_KP4:
1467 KeyScrollState |= ScrollLeft;
1468 break;
1469 case SDLK_RIGHT:
1470 case SDLK_KP6:
1471 KeyScrollState |= ScrollRight;
1472 break;
1473
1474 case '0': case '1': case '2': // 0-8 change player owner
1475 case '3': case '4': case '5':
1476 case '6': case '7': case '8':
1477 if (UnitUnderCursor && Map.Info.PlayerType[(int)key - '0'] != PlayerNobody)
1478 {
1479 UnitUnderCursor->ChangeOwner(&Players[(int)key - '0']);
1480 UI.StatusLine.Set(_("Unit owner modified"));
1481 UpdateMinimap = true;
1482 }
1483 break;
1484
1485 default:
1486 HandleCommandKey(key);
1487 return;
1488 }
1489 return;
1490 }
1491
1492 /**
1493 ** Handle key up.
1494 **
1495 ** @param key Key scancode.
1496 ** @param keychar Character code.
1497 */
1498 static void EditorCallbackKeyUp(unsigned key, unsigned keychar)
1499 {
1500 if (HandleKeyModifiersUp(key, keychar))
1501 {
1502 return;
1503 }
1504
1505 switch (key)
1506 {
1507 case SDLK_UP: // Keyboard scrolling
1508 case SDLK_KP8:
1509 KeyScrollState &= ~ScrollUp;
1510 break;
1511 case SDLK_DOWN:
1512 case SDLK_KP2:
1513 KeyScrollState &= ~ScrollDown;
1514 break;
1515 case SDLK_LEFT:
1516 case SDLK_KP4:
1517 KeyScrollState &= ~ScrollLeft;
1518 break;
1519 case SDLK_RIGHT:
1520 case SDLK_KP6:
1521 KeyScrollState &= ~ScrollRight;
1522 break;
1523 default:
1524 break;
1525 }
1526 }
1527
1528 /**
1529 ** Callback for input.
1530 */
1531 static void EditorCallbackKeyRepeated(unsigned key, unsigned keychar)
1532 {
1533 switch (key)
1534 {
1535 case 'z':
1536 if (KeyModifiers & ModifierControl)
1537 {
1538 EditorUndoGroup();
1539 }
1540 break;
1541 case 'y':
1542 if (KeyModifiers & ModifierControl)
1543 {
1544 EditorRedoGroup();
1545 }
1546 break;
1547 }
1548 }
1549
1550 /**
1551 ** Callback for input movement of the cursor.
1552 **
1553 ** @param x Screen X position.
1554 ** @param y Screen Y position.
1555 */
1556 static void EditorCallbackMouse(int x, int y)
1557 {
1558 int i;
1559 int bx, by;
1560 static int LastMapX = 0;
1561 static int LastMapY = 0;
1562 int cursorMapX, cursorMapY;
1563
1564 HandleCursorMove(&x, &y); // Reduce to screen
1565
1566 //
1567 // Move map.
1568 //
1569 if (GameCursor == UI.Scroll.Cursor)
1570 {
1571 MouseScrollMap(x, y);
1572 return;
1573 }
1574
1575 cursorMapX = UI.SelectedViewport->Viewport2MapX(CursorX);
1576 cursorMapY = UI.SelectedViewport->Viewport2MapY(CursorY);
1577
1578 // Automatically unpress when map tile has changed
1579 if (LastMapX != cursorMapX || LastMapY != cursorMapY)
1580 {
1581 LastMapX = cursorMapX;
1582 LastMapY = cursorMapY;
1583 UnitPlacedThisPress = false;
1584 }
1585
1586 // Dragging mouse
1587 if ((MouseButtons & LeftButton) == LeftButton)
1588 {
1589 //
1590 // Dragging new units on map.
1591 //
1592 if (CursorOn == CursorOnMap &&
1593 Editor.State == EditorEditUnit &&
1594 CursorBuilding)
1595 {
1596 int moveX = 0;
1597 int moveY = 0;
1598
1599 //
1600 // Scroll the map
1601 //
1602 if (CursorX <= UI.SelectedViewport->X)
1603 moveX = -1;
1604 else if (CursorX >= UI.SelectedViewport->EndX)
1605 moveX = +1;
1606 if (CursorY <= UI.SelectedViewport->Y)
1607 moveY = -1;
1608 else if (CursorY >= UI.SelectedViewport->EndY)
1609 moveY = +1;
1610
1611 if (moveX != 0 || moveY != 0)
1612 {
1613 UI.SelectedViewport->Set(
1614 UI.SelectedViewport->MapX + moveX,
1615 UI.SelectedViewport->MapY + moveY,
1616 UI.SelectedViewport->OffsetX,
1617 UI.SelectedViewport->OffsetY);
1618 }
1619
1620 //
1621 // Scroll the map, if cursor moves outside the viewport.
1622 //
1623 RestrictCursorToViewport();
1624
1625 if (!UnitPlacedThisPress &&
1626 CanBuildUnitType(NULL, CursorBuilding, cursorMapX, cursorMapY, 1))
1627 {
1628 EditorPlaceUnit(cursorMapX, cursorMapY, CursorBuilding, Players + Editor.SelectedPlayer);
1629 UnitPlacedThisPress = true;
1630 UI.StatusLine.Clear();
1631 }
1632 return;
1633 }
1634
1635 //
1636 // Minimap move viewpoint
1637 //
1638 if (CursorOn == CursorOnMinimap)
1639 {
1640 RestrictCursorToMinimap();
1641 UI.SelectedViewport->Set(
1642 UI.Minimap.Screen2MapX(CursorX) - UI.SelectedViewport->MapWidth / 2,
1643 UI.Minimap.Screen2MapY(CursorY) - UI.SelectedViewport->MapHeight / 2,
1644 0, 0);
1645 return;
1646 }
1647
1648 return;
1649 }
1650
1651
1652 MouseScrollState = ScrollNone;
1653 GameCursor = UI.Point.Cursor;
1654 CursorOn = CursorOnUnknown;
1655 Editor.CursorPlayer = -1;
1656 Editor.CursorUnitIndex = -1;
1657 Editor.CursorPatchIndex = -1;
1658 ButtonUnderCursor = -1;
1659
1660 //
1661 // Minimap
1662 //
1663 if (x >= UI.Minimap.X && x < UI.Minimap.X + UI.Minimap.W &&
1664 y >= UI.Minimap.Y && y < UI.Minimap.Y + UI.Minimap.H)
1665 {
1666 CursorOn = CursorOnMinimap;
1667 }
1668
1669 //
1670 // Handle start location area
1671 //
1672 if (Editor.State == EditorSetStartLocation)
1673 {
1674 // Select player
1675 bx = UI.InfoPanel.X + 8;
1676 by = UI.InfoPanel.Y + 4 + IconHeight + 10;
1677 for (i = 0; i < PlayerMax; ++i)
1678 {
1679 if (bx < x && x < bx + 20 && by < y && y < by + 20)
1680 {
1681 if (Map.Info.PlayerType[i] != PlayerNobody)
1682 {
1683 std::ostringstream o;
1684 o << _("Select player #") << i;
1685 UI.StatusLine.Set(o.str());
1686 }
1687 else
1688 {
1689 UI.StatusLine.Clear();
1690 }
1691 Editor.CursorPlayer = i;
1692 return;
1693 }
1694 bx += 20;
1695 }
1696 }
1697
1698 //
1699 // Handle edit unit area
1700 //
1701 if (Editor.State == EditorEditUnit)
1702 {
1703 // Scrollbar
1704 if (UI.ButtonPanel.X + 4 < CursorX && CursorX < UI.ButtonPanel.X + 176 - 4 &&
1705 UI.ButtonPanel.Y + 4 < CursorY && CursorY < UI.ButtonPanel.Y + 24)
1706 {
1707 return;
1708 }
1709
1710 // Select player
1711 bx = UI.InfoPanel.X + 8;
1712 by = UI.InfoPanel.Y + 4 + IconHeight + 10;
1713 for (i = 0; i < PlayerMax; ++i)
1714 {
1715 if (bx < x && x < bx + 20 && by < y && y < by + 20)
1716 {
1717 if (Map.Info.PlayerType[i] != PlayerNobody)
1718 {
1719 std::ostringstream o;
1720 o << _("Select player #") << i;
1721 UI.StatusLine.Set(o.str());
1722 }
1723 else
1724 {
1725 UI.StatusLine.Clear();
1726 }
1727 Editor.CursorPlayer = i;
1728 return;
1729 }
1730 bx += 20;
1731 }
1732
1733 // Unit buttons
1734 i = Editor.UnitIndex;
1735 by = UI.ButtonPanel.Y + 24;
1736 while (by < UI.ButtonPanel.Y + ButtonPanelHeight - IconHeight)
1737 {
1738 if (i >= (int)Editor.ShownUnitTypes.size())
1739 {
1740 break;
1741 }
1742 bx = UI.ButtonPanel.X + 10;
1743 while (bx < UI.ButtonPanel.X + 146)
1744 {
1745 if (i >= (int)Editor.ShownUnitTypes.size())
1746 {
1747 break;
1748 }
1749 if (bx < x && x < bx + IconWidth &&
1750 by < y && y < by + IconHeight)
1751 {
1752 std::ostringstream o;
1753 o << Editor.ShownUnitTypes[i]->Ident << " \""
1754 << Editor.ShownUnitTypes[i]->Name << "\"";
1755 UI.StatusLine.Set(o.str());
1756 Editor.CursorUnitIndex = i;
1757 return;
1758 }
1759 bx += IconWidth + 8;
1760 i++;
1761 }
1762 by += IconHeight + 2;
1763 }
1764 }
1765
1766 //
1767 // Handle patch area
1768 //
1769 if (Editor.State == EditorEditPatch)
1770 {
1771 i = Editor.PatchIndex;
1772 by = UI.ButtonPanel.Y + 24;
1773 while (by < UI.ButtonPanel.Y + ButtonPanelHeight - IconHeight)
1774 {
1775 if (i >= (int)Editor.ShownPatchTypes.size())
1776 {
1777 break;
1778 }
1779 bx = UI.ButtonPanel.X + 10;
1780 while (bx < UI.ButtonPanel.X + 146)
1781 {
1782 if (i >= (int)Editor.ShownPatchTypes.size())
1783 {
1784 break;
1785 }
1786 if (bx < x && x < bx + IconWidth &&
1787 by < y && y < by + IconHeight)
1788 {
1789 CPatchType *patchType = Editor.ShownPatchTypes[i]->PatchType;
1790 std::ostringstream ostr;
1791 ostr << patchType->getName() << " ("
1792 << patchType->getTileWidth() << "x"
1793 << patchType->getTileHeight() << ")";
1794 UI.StatusLine.Set(ostr.str());
1795 Editor.CursorPatchIndex = i;
1796 PatchUnderCursor = NULL;
1797 return;
1798 }
1799 bx += IconWidth + 8;
1800 i++;
1801 }
1802 by += IconHeight + 2;
1803 }
1804 }
1805
1806 //
1807 // Handle buttons
1808 //
1809 if (UI.InfoPanel.X + 4 < CursorX &&
1810 CursorX < UI.InfoPanel.X + 4 + Editor.Select.Icon->G->Width &&
1811 UI.InfoPanel.Y + 4 < CursorY &&
1812 CursorY < UI.InfoPanel.Y + 4 + Editor.Select.Icon->G->Width)
1813 {
1814 // FIXME: what is this button?
1815 ButtonAreaUnderCursor = -1;
1816 ButtonUnderCursor = SelectButton;
1817 CursorOn = CursorOnButton;
1818 UI.StatusLine.Set(_("Select mode"));
1819 return;
1820 }
1821 if (UI.InfoPanel.X + 4 + UNIT_ICON_X < CursorX &&
1822 CursorX < UI.InfoPanel.X + 4 + UNIT_ICON_X + Editor.Units.Icon->G->Width &&
1823 UI.InfoPanel.Y + 4 + UNIT_ICON_Y < CursorY &&
1824 CursorY < UI.InfoPanel.Y + 4 + UNIT_ICON_Y + Editor.Units.Icon->G->Height)
1825 {
1826 ButtonAreaUnderCursor = -1;
1827 ButtonUnderCursor = UnitButton;
1828 CursorOn = CursorOnButton;
1829 UI.StatusLine.Set(_("Unit mode"));
1830 return;
1831 }
1832 if (UI.InfoPanel.X + 4 + PATCH_ICON_X < CursorX &&
1833 CursorX < UI.InfoPanel.X + 4 + PATCH_ICON_X + Editor.Patch.Icon->G->Width &&
1834 UI.InfoPanel.Y + 4 + PATCH_ICON_Y < CursorY &&
1835 CursorY < UI.InfoPanel.Y + 4 + PATCH_ICON_Y + Editor.Patch.Icon->G->Height)
1836 {
1837 ButtonAreaUnderCursor = -1;
1838 ButtonUnderCursor = PatchButton;
1839 CursorOn = CursorOnButton;
1840 UI.StatusLine.Set(_("Patch mode"));
1841 return;
1842 }
1843 if (UI.InfoPanel.X + 4 + START_ICON_X < CursorX &&
1844 CursorX < UI.InfoPanel.X + 4 + START_ICON_X + Editor.StartUnit->Icon.Icon->G->Width &&
1845 UI.InfoPanel.Y + 4 + START_ICON_Y < CursorY &&
1846 CursorY < UI.InfoPanel.Y + 4 + START_ICON_Y + Editor.StartUnit->Icon.Icon->G->Height)
1847 {
1848 ButtonAreaUnderCursor = -1;
1849 ButtonUnderCursor = StartButton;
1850 CursorOn = CursorOnButton;
1851 UI.StatusLine.Set(_("Set start location mode"));
1852 return;
1853 }
1854 if (UI.MenuButton.X != -1)
1855 {
1856 if (x >= UI.MenuButton.X &&
1857 x <= UI.MenuButton.X + UI.MenuButton.Style->Width &&
1858 y > UI.MenuButton.Y &&
1859 y <= UI.MenuButton.Y + UI.MenuButton.Style->Height)
1860 {
1861 ButtonAreaUnderCursor = ButtonAreaMenu;
1862 ButtonUnderCursor = ButtonUnderMenu;
1863 CursorOn = CursorOnButton;
1864 return;
1865 }
1866 }
1867
1868 //
1869 // Minimap
1870 //
1871 if (x >= UI.Minimap.X && x < UI.Minimap.X + UI.Minimap.W &&
1872 y >= UI.Minimap.Y && y < UI.Minimap.Y + UI.Minimap.H)
1873 {
1874 CursorOn = CursorOnMinimap;
1875 return;
1876 }
1877
1878 //
1879 // Map
1880 //
1881 PatchUnderCursor = NULL;
1882 UnitUnderCursor = NULL;
1883 if (x >= UI.MapArea.X && x <= UI.MapArea.EndX &&
1884 y >= UI.MapArea.Y && y <= UI.MapArea.EndY)
1885 {
1886 CViewport *vp = GetViewport(x, y);
1887 if (UI.MouseViewport != vp)
1888 {
1889 // viewport changed
1890 UI.MouseViewport = vp;
1891 }
1892 CursorOn = CursorOnMap;
1893
1894 if (Editor.State != EditorEditPatch)
1895 {
1896 // See if there is a unit under the cursor.
1897 UnitUnderCursor = UnitOnScreen(
1898 CursorX - UI.MouseViewport->X + UI.MouseViewport->MapX * TileSizeX + UI.MouseViewport->OffsetX,
1899 CursorY - UI.MouseViewport->Y + UI.MouseViewport->MapY * TileSizeY + UI.MouseViewport->OffsetY);
1900
1901 if (UnitUnderCursor)
1902 {
1903 ShowUnitInfo(UnitUnderCursor);
1904 return;
1905 }
1906 }
1907
1908 // Show what patch is under the cursor
1909 PatchUnderCursor = Map.PatchManager.getPatch(cursorMapX, cursorMapY);
1910 ShowPatchInfo(PatchUnderCursor, cursorMapX, cursorMapY);
1911 HandleMouseScrollArea(x, y);
1912 return;
1913 }
1914
1915 //
1916 // Scrolling Region Handling
1917 //
1918 if (HandleMouseScrollArea(x, y))
1919 {
1920 return;
1921 }
1922
1923 // Not reached if cursor is inside the scroll area
1924
1925 UI.StatusLine.Clear();
1926 }
1927
1928 /**
1929 ** Callback for input.
1930 */
1931 static void EditorCallbackButtonUp(unsigned button)
1932 {
1933 if (GameCursor == UI.Scroll.Cursor)
1934 {
1935 // Move map.
1936 GameCursor = UI.Point.Cursor; // Reset
1937 return;
1938 }
1939
1940 if ((1 << button) == LeftButton && GameMenuButtonClicked)
1941 {
1942 GameMenuButtonClicked = false;
1943 if (ButtonUnderCursor == ButtonUnderMenu)
1944 {
1945 if (UI.MenuButton.Callback)
1946 {
1947 UI.MenuButton.Callback->action("");
1948 }
1949 }
1950 }
1951 if ((1 << button) == LeftButton)
1952 {
1953 PatchPlacedThisPress = false;
1954 UnitPlacedThisPress = false;
1955 EditorCallbackMouse(CursorX, CursorY);
1956 }
1957 }
1958
1959 /**
1960 ** Called if mouse button pressed down.
1961 **
1962 ** @param button Mouse button number (0 left, 1 middle, 2 right)
1963 */
1964 static void EditorCallbackButtonDown(unsigned button)
1965 {
1966 if ((button >> MouseHoldShift) != 0)
1967 {
1968 // Ignore repeated events when holding down a button
1969 return;
1970 }
1971
1972 //
1973 // Click on menu button
1974 //
1975 if (CursorOn == CursorOnButton && ButtonAreaUnderCursor == ButtonAreaMenu &&
1976 (MouseButtons & LeftButton) && !GameMenuButtonClicked)
1977 {
1978 PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume);
1979 GameMenuButtonClicked = true;
1980 return;
1981 }
1982
1983 //
1984 // Click on minimap
1985 //
1986 if (CursorOn == CursorOnMinimap)
1987 {
1988 // enter move mini-mode
1989 if (MouseButtons & LeftButton)
1990 {
1991 UI.SelectedViewport->Set(
1992 UI.Minimap.Screen2MapX(CursorX) - UI.SelectedViewport->MapWidth / 2,
1993 UI.Minimap.Screen2MapY(CursorY) - UI.SelectedViewport->MapHeight / 2,
1994 TileSizeX / 2, TileSizeY / 2);
1995 }
1996 return;
1997 }
1998
1999 //
2000 // Click on mode area
2001 //
2002 if (CursorOn == CursorOnButton)
2003 {
2004 CursorBuilding = NULL;
2005 switch (ButtonUnderCursor)
2006 {
2007 case SelectButton :
2008 Editor.State = EditorSelecting;
2009 Editor.ShowPatchOutlines = false;
2010 editorUnitSlider->setVisible(false);
2011 editorUnitDropDown->setVisible(false);
2012 editorPatchSlider->setVisible(false);
2013 editorPatchDropDown->setVisible(false);
2014 return;
2015 case UnitButton:
2016 Editor.State = EditorEditUnit;
2017 Editor.ShowPatchOutlines = false;
2018 editorUnitSlider->setVisible(true);
2019 editorUnitDropDown->setVisible(true);
2020 editorPatchSlider->setVisible(false);
2021 editorPatchDropDown->setVisible(false);
2022
2023 // DrawBuildingCursor doesn't draw blocked
2024 // map fields correctly if ThisPlayer is
2025 // a player whose type is PlayerNobody,
2026 // because UnitCountSeen does not compute
2027 // the visibility of units to such players.
2028 ThisPlayer = Players + Editor.SelectedPlayer;
2029 return;
2030 case PatchButton :
2031 if (EditorEditPatch)
2032 {
2033 Editor.State = EditorEditPatch;
2034 Editor.ShowPatchOutlines = true;
2035 }
2036 editorUnitSlider->setVisible(false);
2037 editorUnitDropDown->setVisible(false);
2038 editorPatchSlider->setVisible(true);
2039 editorPatchDropDown->setVisible(true);
2040 return;
2041 case StartButton:
2042 Editor.State = EditorSetStartLocation;
2043 Editor.ShowPatchOutlines = false;
2044 editorUnitSlider->setVisible(false);
2045 editorUnitDropDown->setVisible(false);
2046 editorPatchSlider->setVisible(false);
2047 editorPatchDropDown->setVisible(false);
2048 return;
2049 default:
2050 break;
2051 }
2052 }
2053
2054 //
2055 // Click on player area
2056 //
2057 if (Editor.State == EditorEditUnit || Editor.State == EditorSetStartLocation)
2058 {
2059 // Cursor on player icons
2060 if (Editor.CursorPlayer != -1)
2061 {
2062 if (Map.Info.PlayerType[Editor.CursorPlayer] != PlayerNobody)
2063 {
2064 Editor.SelectedPlayer = Editor.CursorPlayer;
2065 ThisPlayer = Players + Editor.SelectedPlayer;
2066 }
2067 return;
2068 }
2069 }
2070
2071 //
2072 // Click on unit area
2073 //
2074 if (Editor.State == EditorEditUnit)
2075 {
2076 // Cursor on unit icons
2077 if (Editor.CursorUnitIndex != -1)
2078 {
2079 Editor.SelectedUnitIndex = Editor.CursorUnitIndex;
2080 CursorBuilding = const_cast<CUnitType *>(Editor.ShownUnitTypes[Editor.CursorUnitIndex]);
2081 return;
2082 }
2083 }
2084
2085 //
2086 // Click on patch area
2087 //
2088 if (Editor.State == EditorEditPatch)
2089 {
2090 // Cursor on patch icons
2091 if (Editor.CursorPatchIndex != -1)
2092 {
2093 Editor.SelectedPatchIndex = Editor.CursorPatchIndex;
2094 return;
2095 }
2096 }
2097
2098 //
2099 // Click on map area
2100 //
2101 if (CursorOn == CursorOnMap)
2102 {
2103 // Check if the selected viewport changed
2104 CViewport *vp = GetViewport(CursorX, CursorY);
2105 if ((MouseButtons & LeftButton) && UI.SelectedViewport != vp)
2106 {
2107 UI.SelectedViewport = vp;
2108 }
2109
2110 if (MouseButtons & LeftButton)
2111 {
2112 int cursorMapX = UI.MouseViewport->Viewport2MapX(CursorX);
2113 int cursorMapY = UI.MouseViewport->Viewport2MapY(CursorY);
2114
2115 if (Editor.State == EditorSelecting)
2116 {
2117 // Nothing to do
2118 }
2119 else if (Editor.State == EditorEditPatch)
2120 {
2121 if (!PatchPlacedThisPress && Editor.SelectedPatchIndex != -1)
2122 {
2123 // Remove any patches under the new patch
2124 const CPatchType *patchType = Editor.ShownPatchTypes[Editor.SelectedPatchIndex]->PatchType;
2125 bool isFirstOfGroup = true;
2126 EditorRemovePatches(cursorMapX, cursorMapY, patchType->getTileWidth(), patchType->getTileHeight(),
2127 &isFirstOfGroup);
2128
2129 // Create the new patch
2130 EditorPlacePatch(cursorMapX, cursorMapY, patchType,
2131 &isFirstOfGroup);
2132 PatchPlacedThisPress = true;
2133 }
2134 }
2135 else if (Editor.State == EditorEditUnit)
2136 {
2137 if (!UnitPlacedThisPress && CursorBuilding)
2138 {
2139 // Try to place a new unit
2140 if (CanBuildUnitType(NULL, CursorBuilding, cursorMapX, cursorMapY, 1))
2141 {
2142 PlayGameSound(GameSounds.PlacementSuccess.Sound,
2143 MaxSampleVolume);
2144 EditorPlaceUnit(cursorMapX, cursorMapY,
2145 CursorBuilding, Players + Editor.SelectedPlayer);
2146 UnitPlacedThisPress = true;
2147 UI.StatusLine.Clear();
2148 }
2149 else
2150 {
2151 UI.StatusLine.Set(_("Unit can't be placed here."));
2152 PlayGameSound(GameSounds.PlacementError.Sound,
2153 MaxSampleVolume);
2154 }
2155 }
2156 }
2157 else if (Editor.State == EditorSetStartLocation)
2158 {
2159 Players[Editor.SelectedPlayer].StartX = cursorMapX;
2160 Players[Editor.SelectedPlayer].StartY = cursorMapY;
2161 }
2162 }
2163 else if (MouseButtons & MiddleButton)
2164 {
2165 // enter move map mode
2166 CursorStartX = CursorX;
2167 CursorStartY = CursorY;
2168 GameCursor = UI.Scroll.Cursor;
2169 }
2170 }
2171 }
2172
2173 /**
2174 ** Callback for exit.
2175 */
2176 static void EditorCallbackExit()
2177 {
2178 // Disabled
2179 if (!LeaveStops) {
2180 return;
2181 }
2182
2183 // Denote cursor not on anything in window (used?)
2184 CursorOn = CursorOnUnknown;
2185
2186 // Prevent scrolling while out of focus (on other applications)
2187 KeyScrollState = MouseScrollState = ScrollNone;
2188 }
2189
2190 /**
2191 ** Create the patch icons
2192 */
2193 static void CreatePatchIcons()
2194 {
2195 std::vector<std::string> patchTypeNames;
2196 std::vector<std::string>::iterator i;
2197
2198 // Load all of the patch types
2199 Map.PatchManager.loadAll();
2200
2201 // Create icons out of the patch graphics
2202 patchTypeNames = Map.PatchManager.getPatchTypeNames();
2203 for (i = patchTypeNames.begin(); i != patchTypeNames.end(); ++i)
2204 {
2205 CPatchType *patchType = Map.PatchManager.getPatchType(*i);
2206 CGraphic *g = patchType->getGraphic()->Clone();
2207
2208 g->Resize(IconWidth, IconHeight);
2209
2210 Editor.AllPatchTypes[*i] = new CPatchIcon(patchType, g);
2211 }
2212 }
2213
2214 /**
2215 ** Clean up the patch icons
2216 */
2217 static void CleanPatchIcons()
2218 {
2219 Editor.ShownPatchTypes.clear();
2220
2221 for (std::map<std::string, CPatchIcon *>::iterator i = Editor.AllPatchTypes.begin(); i != Editor.AllPatchTypes.end(); ++i)
2222 {
2223 CGraphic::Free(i->second->G);
2224 delete i->second;
2225 }
2226 Editor.AllPatchTypes.clear();
2227 }
2228
2229 /**
2230 ** Create editor.
2231 */
2232 void CEditor::Init()
2233 {
2234 int i;
2235 char *file;
2236 char buf[PATH_MAX];
2237 CFile clf;
2238
2239 //
2240 // Load and evaluate the editor configuration file
2241 // FIXME: the CLopen is very slow and repeats the work of LibraryFileName.
2242 //
2243 file = LibraryFileName(EditorStartFile, buf, sizeof(buf));
2244 if (clf.open(file, CL_OPEN_READ) != -1)
2245 {
2246 clf.close();
2247 ShowLoadProgress("Script %s", file);
2248 LuaLoadFile(file);
2249 CclGarbageCollect(0); // Cleanup memory after load
2250 }
2251
2252 ThisPlayer = &Players[0];
2253
2254 FlagRevealMap = 1; // editor without fog and all visible
2255 Map.NoFogOfWar = true;
2256
2257 // new map!
2258 if (!*CurrentMapPath)
2259 {
2260 InitUnitTypes(1);
2261 InitPlayers();
2262
2263 for (i = 0; i < PlayerMax; ++i)
2264 {
2265 PlayerTypes playerType = PlayerNobody;
2266
2267 if (i == 0 || i == 1)
2268 {
2269 // New maps should have 2 players by default
2270 playerType = PlayerPerson;
2271 }
2272 else if (i == PlayerNumNeutral)
2273 {
2274 playerType = PlayerNeutral;
2275 }
2276
2277 CreatePlayer(playerType);
2278 Map.Info.PlayerType[i] = playerType;
2279 Map.Info.PlayerSide[i] = 0;
2280 }
2281
2282 Map.Fields = new CMapField[Map.Info.MapWidth * Map.Info.MapHeight];
2283 Map.Visible[0] = new unsigned[Map.Info.MapWidth * Map.Info.MapHeight / 2];
2284 memset(Map.Visible[0], 0, Map.Info.MapWidth * Map.Info.MapHeight / 2 * sizeof(unsigned));
2285 UnitCache.Init(Map.Info.MapWidth, Map.Info.MapHeight);
2286
2287 GameSettings.Resources = SettingsPresetMapDefault;
2288 CreateGame("", &Map);
2289 }
2290 else
2291 {
2292 CreateGame(CurrentMapPath, &Map);
2293 }
2294
2295 ReplayRevealMap = 1;
2296 FlagRevealMap = 0;
2297 Editor.SelectedPlayer = PlayerNumNeutral;
2298 Editor.PatchOutlineColor = ColorBlack;
2299 Editor.SelectedPatchIndex = -1;
2300
2301 //
2302 // Place the start points, which the loader discarded.
2303 //
2304 for (i = 0; i < PlayerMax; ++i)
2305 {
2306 if (Map.Info.PlayerType[i] != PlayerNobody)
2307 {
2308 // Set SelectedPlayer to a valid player
2309 if (Editor.SelectedPlayer == PlayerNumNeutral)
2310 {
2311 Editor.SelectedPlayer = i;
2312 break;
2313 }
2314 }
2315 }
2316
2317 ButtonPanelWidth = 200;
2318 ButtonPanelHeight = 160 + (Video.Height - 480);
2319
2320 CalculateMaxIconSize();
2321 VisibleIcons = CalculateVisibleIcons();
2322
2323 if (!StartUnitName.empty())
2324 {
2325 StartUnit = UnitTypeByIdent(StartUnitName);
2326 }
2327 Select.Icon = NULL;
2328 Select.Load();
2329 Units.Icon = NULL;
2330 Units.Load();
2331 Patch.Icon = NULL;
2332 Patch.Load();
2333
2334 CreatePatchIcons();
2335
2336 RecalculateShownUnits(NULL);
2337 RecalculateShownPatches("");
2338
2339 EditorUndoActions.clear();
2340 EditorRedoActions.clear();
2341
2342 EditorCallbacks.ButtonPressed = EditorCallbackButtonDown;
2343 EditorCallbacks.ButtonReleased = EditorCallbackButtonUp;
2344 EditorCallbacks.MouseMoved = EditorCallbackMouse;
2345 EditorCallbacks.MouseExit = EditorCallbackExit;
2346 EditorCallbacks.KeyPressed = EditorCallbackKeyDown;
2347 EditorCallbacks.KeyReleased = EditorCallbackKeyUp;
2348 EditorCallbacks.KeyRepeated = EditorCallbackKeyRepeated;
2349 EditorCallbacks.NetworkEvent = NetworkEvent;
2350 }
2351
2352 /**
2353 ** Save a map from editor.
2354 **
2355 ** @param file Save the level to this file.
2356 **
2357 ** @return 0 for success, -1 for error
2358 **
2359 ** @todo FIXME: Check if the map is valid, contains no failures.
2360 ** At least two players, one human slot, every player a startpoint
2361 */
2362 int EditorSaveMap(const std::string &file)
2363 {
2364 std::string fullName;
2365
2366 fullName = StratagusLibPath + "/" + file;
2367 if (SaveStratagusMap(fullName, &Map) == -1)
2368 {
2369 fprintf(stderr, "Cannot save map\n");
2370 return -1;
2371 }
2372
2373 return 0;
2374 }
2375
2376 /*----------------------------------------------------------------------------
2377 -- Editor main loop
2378 ----------------------------------------------------------------------------*/
2379
2380 /**
2381 ** Editor main event loop.
2382 */
2383 static void EditorMainLoop()
2384 {
2385 bool OldCommandLogDisabled = CommandLogDisabled;
2386 const EventCallback *old_callbacks = GetCallbacks();
2387
2388 CommandLogDisabled = true;
2389 SetCallbacks(&EditorCallbacks);
2390
2391 gcn::Widget *oldTop = Gui->getTop();
2392
2393 editorContainer = new gcn::Container();
2394 editorContainer->setDimension(gcn::Rectangle(0, 0, Video.Width, Video.Height));
2395 editorContainer->setOpaque(false);
2396 Gui->setTop(editorContainer);
2397
2398 editorUnitSliderListener = new EditorUnitSliderListener();
2399 editorUnitDropDownListener = new EditorUnitDropDownListener();
2400 editorPatchSliderListener = new EditorPatchSliderListener();
2401 editorPatchDropDownListener = new EditorPatchDropDownListener();
2402
2403 gcn::Color darkNoAlphaColor(38, 38, 78);
2404 gcn::Color clearColor(200, 200, 120);
2405
2406 editorUnitSlider = new gcn::Slider();
2407 editorUnitSlider->setBaseColor(darkNoAlphaColor);
2408 editorUnitSlider->setForegroundColor(clearColor);
2409 editorUnitSlider->setBackgroundColor(clearColor);
2410 editorUnitSlider->setSize(176, 16);
2411 editorUnitSlider->setVisible(false);
2412 editorUnitSlider->addActionListener(editorUnitSliderListener);
2413
2414 editorUnitListModel = new StringListModel();
2415 for (size_t i = 0; i < sizeof(unitFilters) / sizeof(*unitFilters); ++i)
2416 {
2417 editorUnitListModel->add(unitFilters[i].name);
2418 }
2419
2420 editorUnitDropDown = new gcn::DropDown();
2421 editorUnitDropDown->setFont(GameFont);
2422 editorUnitDropDown->setBaseColor(darkNoAlphaColor);
2423 editorUnitDropDown->setForegroundColor(clearColor);
2424 editorUnitDropDown->setBackgroundColor(darkNoAlphaColor);
2425 editorUnitDropDown->setVisible(false);
2426 editorUnitDropDown->addActionListener(editorUnitDropDownListener);
2427 editorUnitDropDown->setListModel(editorUnitListModel);
2428
2429 editorPatchSlider = new gcn::Slider();
2430 editorPatchSlider->setBaseColor(darkNoAlphaColor);
2431 editorPatchSlider->setForegroundColor(clearColor);
2432 editorPatchSlider->setBackgroundColor(clearColor);
2433 editorPatchSlider->setSize(176, 16);
2434 editorPatchSlider->setVisible(false);
2435 editorPatchSlider->addActionListener(editorPatchSliderListener);
2436
2437 std::vector<std::string> themes = Map.PatchManager.getPatchTypeThemes();
2438
2439 editorPatchListModel = new StringListModel();
2440 editorPatchListModel->add(_("All"));
2441 for (size_t i = 0; i < themes.size(); ++i)
2442 {
2443 editorPatchListModel->add(themes[i]);
2444 }
2445
2446 editorPatchDropDown = new gcn::DropDown();
2447 editorPatchDropDown->setFont(GameFont);
2448 editorPatchDropDown->setBaseColor(darkNoAlphaColor);
2449 editorPatchDropDown->setForegroundColor(clearColor);
2450 editorPatchDropDown->setBackgroundColor(darkNoAlphaColor);
2451 editorPatchDropDown->setVisible(false);
2452 editorPatchDropDown->addActionListener(editorPatchDropDownListener);
2453 editorPatchDropDown->setListModel(editorPatchListModel);
2454
2455 editorContainer->add(editorUnitSlider, UI.ButtonPanel.X + 2, UI.ButtonPanel.Y + 4);
2456 editorContainer->add(editorUnitDropDown, UI.ButtonPanel.X + 2, UI.ButtonPanel.Y - 20);
2457 editorContainer->add(editorPatchSlider, UI.ButtonPanel.X + 2, UI.ButtonPanel.Y + 4);
2458 editorContainer->add(editorPatchDropDown, UI.ButtonPanel.X + 2, UI.ButtonPanel.Y - 20);
2459
2460 UpdateMinimap = true;
2461
2462 while (1)
2463 {
2464 Editor.MapLoaded = false;
2465 Editor.Running = EditorEditing;
2466
2467 Editor.Init();
2468
2469 //ProcessMenu("menu-editor-tips", 1);
2470 InterfaceState = IfaceStateNormal;
2471
2472 SetVideoSync();
2473
2474 GameCursor = UI.Point.Cursor;
2475 InterfaceState = IfaceStateNormal;
2476 Editor.State = EditorSelecting;
2477 Editor.ShowPatchOutlines = false;
2478 Editor.ShowTerrainFlags = false;
2479 UI.SelectedViewport = UI.Viewports;
2480
2481 while (Editor.Running)
2482 {
2483 CheckMusicFinished();
2484
2485 if ((FrameCounter % (5 * FRAMES_PER_SECOND)) == 0)
2486 {
2487 if (UpdateMinimapTerrain)
2488 {
2489 UI.Minimap.UpdateTerrain();
2490 UpdateMinimapTerrain = false;
2491 UpdateMinimap = true;
2492 }
2493 if (UpdateMinimap)
2494 {
2495 UI.Minimap.Update();
2496 UpdateMinimap = false;
2497 }
2498 }
2499
2500 EditorUpdateDisplay();
2501
2502 //
2503 // Map scrolling
2504 //
2505 if (UI.MouseScroll && MouseScrollState != ScrollNone)
2506 {
2507 DoScrollArea(MouseScrollState, 0);
2508 }
2509 if (UI.KeyScroll && KeyScrollState != ScrollNone)
2510 {
2511 DoScrollArea(KeyScrollState, (KeyModifiers & ModifierControl) != 0);
2512 if (CursorOn == CursorOnMap && (MouseButtons & LeftButton) &&
2513 Editor.State == EditorEditUnit)
2514 {
2515 EditorCallbackButtonDown(0);
2516 }
2517 }
2518
2519 WaitEventsOneFrame();
2520 }
2521
2522 if (!Editor.MapLoaded)
2523 {
2524 break;
2525 }
2526
2527 CleanModules();
2528 InitDefinedVariables();
2529
2530 LoadCcl(); // Reload the main config file
2531
2532 PreMenuSetup();
2533
2534 InterfaceState = IfaceStateMenu;
2535 GameCursor = UI.Point.Cursor;
2536
2537 Video.ClearScreen();
2538 Invalidate();
2539 }
2540
2541 CommandLogDisabled = OldCommandLogDisabled;
2542 SetCallbacks(old_callbacks);
2543 Gui->setTop(oldTop);
2544 delete editorContainer;
2545 delete editorUnitSliderListener;
2546 delete editorUnitDropDownListener;
2547 delete editorUnitListModel;
2548 delete editorPatchSliderListener;
2549 delete editorPatchDropDownListener;
2550 delete editorPatchListModel;
2551 delete editorUnitSlider;
2552 delete editorUnitDropDown;
2553 delete editorPatchSlider;
2554 delete editorPatchDropDown;
2555 }
2556
2557 /**
2558 ** Validate the width and height of the map
2559 **
2560 ** @param width Width of the map
2561 ** @param height Height of the map
2562 */
2563 static void ValidateMapSize(int &width, int &height)
2564 {
2565 // Make sure we have good values
2566 if (width < 32)
2567 {
2568 fprintf(stderr, "Invalid map width, using default value\n");
2569 width = 32;
2570 }
2571 else if (width > MaxMapWidth)
2572 {
2573 fprintf(stderr, "Invalid map width, using default value\n");
2574 width = MaxMapWidth;
2575 }
2576 if (height < 32)
2577 {
2578 fprintf(stderr, "Invalid map height, using default value\n");
2579 height = 32;
2580 }
2581 else if (height > MaxMapHeight)
2582 {
2583 fprintf(stderr, "Invalid map height, using default value\n");
2584 height = MaxMapHeight;
2585 }
2586 }
2587
2588 /**
2589 ** Start the editor
2590 **
2591 ** @param filename Map to load, empty string to create a new map
2592 */
2593 void StartEditor(const std::string &filename)
2594 {
2595 std::string nc, rc;
2596 bool newMap;
2597
2598 GetDefaultTextColors(nc, rc);
2599
2600 DebugPrint("StartEditor - %s\n" _C_ !filename.empty() ? filename.c_str() : "new map");
2601
2602 CleanPlayers();
2603 newMap = filename.empty();
2604 if (!newMap)
2605 {
2606 if (strcpy_s(CurrentMapPath, sizeof(CurrentMapPath), filename.c_str()) != 0)
2607 {
2608 newMap = true;
2609 }
2610 }
2611
2612 if (newMap)
2613 {
2614 // new map, choose some default values
2615 strcpy_s(CurrentMapPath, sizeof(CurrentMapPath), "");
2616 Map.Info.Description.clear();
2617 ValidateMapSize(Map.Info.MapWidth, Map.Info.MapHeight);
2618 }
2619
2620 // Run the editor.
2621 EditorMainLoop();
2622
2623 // Clear screen
2624 Video.ClearScreen();
2625 Invalidate();
2626
2627 CleanPatchIcons();
2628 CleanGame();
2629 CleanPlayers();
2630
2631 SetDefaultTextColors(nc, rc);
2632 }
2633
2634 //@}