1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef GAME_EDITOR_EDITOR_H
4#define GAME_EDITOR_EDITOR_H
5
6#include "editor_history.h"
7#include "editor_server_settings.h"
8#include "editor_trackers.h"
9#include "editor_ui.h"
10#include "font_typer.h"
11#include "layer_selector.h"
12#include "map_view.h"
13#include "quad_art.h"
14
15#include <base/bezier.h>
16#include <base/fs.h>
17
18#include <engine/editor.h>
19#include <engine/graphics.h>
20
21#include <game/client/ui.h>
22#include <game/client/ui_listbox.h>
23#include <game/editor/enums.h>
24#include <game/editor/envelope_editor.h>
25#include <game/editor/file_browser.h>
26#include <game/editor/mapitems/envelope.h>
27#include <game/editor/mapitems/layer.h>
28#include <game/editor/mapitems/layer_front.h>
29#include <game/editor/mapitems/layer_game.h>
30#include <game/editor/mapitems/layer_group.h>
31#include <game/editor/mapitems/layer_quads.h>
32#include <game/editor/mapitems/layer_sounds.h>
33#include <game/editor/mapitems/layer_speedup.h>
34#include <game/editor/mapitems/layer_switch.h>
35#include <game/editor/mapitems/layer_tele.h>
36#include <game/editor/mapitems/layer_tiles.h>
37#include <game/editor/mapitems/layer_tune.h>
38#include <game/editor/mapitems/map.h>
39#include <game/editor/prompt.h>
40#include <game/editor/quick_action.h>
41#include <game/mapitems.h>
42
43#include <deque>
44#include <functional>
45#include <map>
46#include <memory>
47#include <string>
48#include <vector>
49
50template<typename T>
51using FDropdownRenderCallback = std::function<void(const T &, char (&aOutput)[128], std::vector<STextColorSplit> &)>;
52
53// CEditor SPECIFIC
54enum
55{
56 MODE_LAYERS = 0,
57 MODE_IMAGES,
58 MODE_SOUNDS,
59
60 NUM_MODES,
61};
62
63enum
64{
65 DIALOG_NONE = 0,
66 DIALOG_FILE,
67 DIALOG_MAPSETTINGS_ERROR,
68 DIALOG_QUICK_PROMPT,
69
70 // The font typer component sets m_Dialog
71 // while it is active to make sure no other component
72 // interprets the key presses
73 DIALOG_PSEUDO_FONT_TYPER,
74};
75
76class CProperty
77{
78public:
79 CProperty(const char *pName, int Value, int Type, int Min, int Max) :
80 m_pName(pName), m_Value(Value), m_Type(Type), m_Min(Min), m_Max(Max) {}
81
82 CProperty(std::nullptr_t) :
83 m_pName(nullptr), m_Value(0), m_Type(0), m_Min(0), m_Max(0) {}
84
85 const char *m_pName;
86 int m_Value;
87 int m_Type;
88 int m_Min;
89 int m_Max;
90};
91
92enum
93{
94 PROPTYPE_NULL = 0,
95 PROPTYPE_BOOL,
96 PROPTYPE_INT,
97 PROPTYPE_ANGLE_SCROLL,
98 PROPTYPE_COLOR,
99 PROPTYPE_IMAGE,
100 PROPTYPE_ENVELOPE,
101 PROPTYPE_SHIFT,
102 PROPTYPE_SOUND,
103 PROPTYPE_AUTOMAPPER,
104 PROPTYPE_AUTOMAPPER_REFERENCE,
105};
106
107class CEditor : public IEditor
108{
109 class IInput *m_pInput = nullptr;
110 class IClient *m_pClient = nullptr;
111 class IConfigManager *m_pConfigManager = nullptr;
112 class CConfig *m_pConfig = nullptr;
113 class IEngine *m_pEngine = nullptr;
114 class IGraphics *m_pGraphics = nullptr;
115 class ITextRender *m_pTextRender = nullptr;
116 class ISound *m_pSound = nullptr;
117 class IStorage *m_pStorage = nullptr;
118 CRenderMap m_RenderMap;
119 CUi m_UI;
120
121 std::vector<std::reference_wrapper<CEditorComponent>> m_vComponents;
122 CMapView m_MapView;
123 CEnvelopeEditor m_EnvelopeEditor;
124 CLayerSelector m_LayerSelector;
125 CFileBrowser m_FileBrowser;
126 CPrompt m_Prompt;
127 CFontTyper m_FontTyper;
128 CQuadKnife m_QuadKnife;
129
130 IGraphics::CTextureHandle m_EntitiesTexture;
131
132 IGraphics::CTextureHandle m_FrontTexture;
133 IGraphics::CTextureHandle m_TeleTexture;
134 IGraphics::CTextureHandle m_SpeedupTexture;
135 IGraphics::CTextureHandle m_SwitchTexture;
136 IGraphics::CTextureHandle m_TuneTexture;
137
138 enum EPreviewState
139 {
140 PREVIEW_UNLOADED,
141 PREVIEW_LOADED,
142 PREVIEW_ERROR,
143 };
144
145 std::shared_ptr<CLayerGroup> m_apSavedBrushes[10];
146 static constexpr ColorRGBA ms_DefaultPropColor = ColorRGBA(1, 1, 1, 0.5f);
147
148public:
149 class IInput *Input() const { return m_pInput; }
150 class IClient *Client() const { return m_pClient; }
151 class IConfigManager *ConfigManager() const { return m_pConfigManager; }
152 class CConfig *Config() const { return m_pConfig; }
153 class IEngine *Engine() const { return m_pEngine; }
154 class IGraphics *Graphics() const { return m_pGraphics; }
155 class ISound *Sound() const { return m_pSound; }
156 class ITextRender *TextRender() const { return m_pTextRender; }
157 class IStorage *Storage() const { return m_pStorage; }
158 CUi *Ui() { return &m_UI; }
159 CRenderMap *RenderMap() { return &m_RenderMap; }
160
161 CEditorMap *Map();
162 const CEditorMap *Map() const;
163 CMapView *MapView() { return &m_MapView; }
164 const CMapView *MapView() const { return &m_MapView; }
165 CQuadKnife *QuadKnife() { return &m_QuadKnife; }
166 const CQuadKnife *QuadKnife() const { return &m_QuadKnife; }
167 CLayerSelector *LayerSelector() { return &m_LayerSelector; }
168
169 void FillGameTiles(EGameTileOp FillTile) const;
170 bool CanFillGameTiles() const;
171 void AddQuadOrSound();
172 void AddGroup();
173 void AddSoundLayer();
174 void AddTileLayer();
175 void AddQuadsLayer();
176 void AddSwitchLayer();
177 void AddFrontLayer();
178 void AddTuneLayer();
179 void AddSpeedupLayer();
180 void AddTeleLayer();
181 void DeleteSelectedLayer();
182 void LayerSelectImage();
183 bool IsNonGameTileLayerSelected() const;
184 void MapDetails();
185 void TestMapLocally();
186 void GotoPosition();
187#define REGISTER_QUICK_ACTION(name, text, callback, disabled, active, button_color, description) CQuickAction m_QuickAction##name;
188#include <game/editor/quick_actions.h>
189#undef REGISTER_QUICK_ACTION
190
191 CEditor() :
192#define REGISTER_QUICK_ACTION(name, text, callback, disabled, active, button_color, description) m_QuickAction##name(text, description, callback, disabled, active, button_color),
193#include <game/editor/quick_actions.h>
194#undef REGISTER_QUICK_ACTION
195 m_Dialog(DIALOG_NONE)
196 {
197 m_EntitiesTexture.Invalidate();
198 m_FrontTexture.Invalidate();
199 m_TeleTexture.Invalidate();
200 m_SpeedupTexture.Invalidate();
201 m_SwitchTexture.Invalidate();
202 m_TuneTexture.Invalidate();
203
204 m_Mode = MODE_LAYERS;
205
206 m_BrushColorEnabled = true;
207
208 m_ToolbarPreviewSound = -1;
209
210 m_SelectEntitiesImage = "DDNet";
211
212 m_ShowMousePointer = true;
213
214 m_GuiActive = true;
215
216 m_ShowTileInfo = SHOW_TILE_OFF;
217
218 for(size_t i = 0; i < std::size(m_aSavedColors); ++i)
219 {
220 m_aSavedColors[i] = color_cast<ColorRGBA>(hsl: ColorHSLA(i / (float)std::size(m_aSavedColors), 1.0f, 0.5f));
221 }
222
223 m_CheckerTexture.Invalidate();
224 for(auto &CursorTexture : m_aCursorTextures)
225 CursorTexture.Invalidate();
226
227 m_CursorType = CURSOR_NORMAL;
228
229 // DDRace
230
231 m_TeleNumber = 1;
232 m_TeleCheckpointNumber = 1;
233 m_ViewTeleNumber = 0;
234
235 m_TuningNumber = 1;
236 m_ViewTuning = 0;
237
238 m_SwitchNumber = 1;
239 m_SwitchDelay = 0;
240 m_SpeedupForce = 50;
241 m_SpeedupMaxSpeed = 0;
242 m_SpeedupAngle = 0;
243 m_LargeLayerWasWarned = false;
244 m_PreventUnusedTilesWasWarned = false;
245 m_AllowPlaceUnusedTiles = EUnusedEntities::NOT_ALLOWED;
246 m_BrushDrawDestructive = true;
247 }
248
249 void Init() override;
250 void OnUpdate() override;
251 void OnRender() override;
252 void OnActivate() override;
253 void OnWindowResize() override;
254 void OnClose() override;
255 void OnDialogClose();
256 bool HasUnsavedData() const override;
257 void UpdateMentions() override { m_Mentions++; }
258 void ResetMentions() override { m_Mentions = 0; }
259 void OnIngameMoved() override { m_IngameMoved = true; }
260 void ResetIngameMoved() override { m_IngameMoved = false; }
261
262 void HandleCursorMovement();
263 void OnInput(const IInput::CEvent &Event);
264 void MouseAxisLock(vec2 &CursorRel);
265 vec2 m_MouseAxisInitialPos = vec2(0.0f, 0.0f);
266 enum class EAxisLock
267 {
268 START,
269 NONE,
270 HORIZONTAL,
271 VERTICAL,
272 } m_MouseAxisLockState = EAxisLock::START;
273
274 /**
275 * Global time when the autosave was last updated in the @link HandleAutosave @endlink function.
276 * This is used so that the autosave does not immediately activate when reopening the editor after
277 * a longer time of inactivity, as autosaves are only updated while the editor is open.
278 */
279 float m_LastAutosaveUpdateTime = -1.0f;
280 void HandleAutosave();
281 std::deque<std::shared_ptr<CDataFileWriterFinishJob>> m_WriterFinishJobs;
282 void HandleWriterFinishJobs();
283 bool IsSaving(const char *pFilename) const;
284 void UpdateMapDisplayNames();
285
286 // TODO: The name of the ShowFileDialogError function is not accurate anymore, this is used for generic error messages.
287 // Popups in UI should be shared_ptrs to make this even more generic.
288 class CStringKeyComparator
289 {
290 public:
291 bool operator()(const char *pLhs, const char *pRhs) const;
292 };
293 std::map<const char *, CUi::SMessagePopupContext *, CStringKeyComparator> m_PopupMessageContexts;
294 [[gnu::format(printf, 2, 3)]] void ShowFileDialogError(const char *pFormat, ...);
295
296 void Reset();
297 void AddDefaultMap();
298 void CloseMap(size_t Index, bool Confirm);
299 bool Save(const char *pFilename) override;
300 bool Load(const char *pFilename, int StorageType) override;
301 bool HandleMapDrop(const char *pFilename, int StorageType) override;
302 void LoadIngameMap();
303 void Render();
304
305 void UpdateBrushPicker();
306 void RenderPressedKeys(CUIRect View);
307 void RenderSavingIndicator(CUIRect View);
308 void FreeDynamicPopupMenus();
309 void UpdateColorPipette();
310 void RenderMousePointer();
311 void RenderIngameEntities(const CLayerGroup &Group, const CLayerTiles &TilesLayer);
312
313 template<typename E>
314 SEditResult<E> DoPropertiesWithState(CUIRect *pToolbox, CProperty *pProps, int *pIds, int *pNewVal, const std::vector<ColorRGBA> &vColors = {});
315 int DoProperties(CUIRect *pToolbox, CProperty *pProps, int *pIds, int *pNewVal, const std::vector<ColorRGBA> &vColors = {});
316
317 CUi::SColorPickerPopupContext m_ColorPickerPopupContext;
318 const void *m_pColorPickerPopupActiveId = nullptr;
319 void DoColorPickerButton(const void *pId, const CUIRect *pRect, ColorRGBA Color, const std::function<void(ColorRGBA Color)> &SetColor);
320
321 int m_Mode;
322 int m_Dialog;
323 bool m_CloseMapAfterSave = false;
324 char m_aTooltip[256] = "";
325
326 bool m_BrushColorEnabled;
327
328 enum
329 {
330 POPEVENT_EXIT = 0,
331 POPEVENT_CLOSE_MAP,
332 POPEVENT_LARGELAYER,
333 POPEVENT_PREVENTUNUSEDTILES,
334 POPEVENT_IMAGEDIV16,
335 POPEVENT_IMAGE_MAX,
336 POPEVENT_SOUND_MAX,
337 POPEVENT_PLACE_BORDER_TILES,
338 POPEVENT_TILE_ART_BIG_IMAGE,
339 POPEVENT_TILE_ART_MANY_COLORS,
340 POPEVENT_TILE_ART_TOO_MANY_COLORS,
341 POPEVENT_QUAD_ART_BIG_IMAGE,
342 POPEVENT_REMOVE_USED_IMAGE,
343 POPEVENT_REMOVE_USED_SOUND,
344 POPEVENT_RESTART_SERVER,
345 POPEVENT_RESTARTING_SERVER,
346 };
347
348 int m_PopupEventType;
349 int m_PopupEventActivated = false;
350 bool m_LargeLayerWasWarned;
351 bool m_PreventUnusedTilesWasWarned;
352
353 enum class EUnusedEntities
354 {
355 ALLOWED_IMPLICIT = -1,
356 NOT_ALLOWED = 0,
357 ALLOWED_EXPLICIT = 1,
358 };
359 EUnusedEntities m_AllowPlaceUnusedTiles;
360 bool IsAllowPlaceUnusedTiles() const;
361
362 bool m_BrushDrawDestructive;
363
364 int m_Mentions = 0;
365 bool m_IngameMoved = false;
366
367 int m_ToolbarPreviewSound;
368
369 std::vector<std::string> m_vSelectEntitiesFiles;
370 std::string m_SelectEntitiesImage;
371
372 bool m_ShowMousePointer;
373 bool m_GuiActive;
374
375 const void *m_pContainerPanned;
376 const void *m_pContainerPannedLast;
377
378 enum EShowTile
379 {
380 SHOW_TILE_OFF,
381 SHOW_TILE_DECIMAL,
382 SHOW_TILE_HEXADECIMAL
383 };
384 EShowTile m_ShowTileInfo;
385
386 enum EExtraEditor
387 {
388 EXTRAEDITOR_NONE = -1,
389 EXTRAEDITOR_ENVELOPES,
390 EXTRAEDITOR_SERVER_SETTINGS,
391 EXTRAEDITOR_HISTORY,
392 NUM_EXTRAEDITORS,
393 };
394 EExtraEditor m_ActiveExtraEditor = EXTRAEDITOR_NONE;
395 float m_aExtraEditorSplits[NUM_EXTRAEDITORS] = {250.0f, 250.0f, 250.0f};
396 float m_ToolBoxWidth = 100.0f;
397
398 bool m_ShowEnvelopePreview = false;
399 enum class EEnvelopePreview
400 {
401 NONE,
402 SELECTED,
403 ALL,
404 };
405 EEnvelopePreview m_ActiveEnvelopePreview = EEnvelopePreview::NONE;
406 enum class EQuadEnvelopePointOperation
407 {
408 NONE = 0,
409 MOVE,
410 ROTATE,
411 };
412 EQuadEnvelopePointOperation m_QuadEnvelopePointOperation = EQuadEnvelopePointOperation::NONE;
413
414 bool m_ShowPicker = false;
415 bool m_ShowPickerToggle = false;
416
417 // Color palette and pipette
418 ColorRGBA m_aSavedColors[8];
419 ColorRGBA m_PipetteColor = ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f);
420 bool m_ColorPipetteActive = false;
421
422 IGraphics::CTextureHandle m_CheckerTexture;
423
424 enum ECursorType
425 {
426 CURSOR_NORMAL,
427 CURSOR_RESIZE_V,
428 CURSOR_RESIZE_H,
429 NUM_CURSORS
430 };
431 IGraphics::CTextureHandle m_aCursorTextures[ECursorType::NUM_CURSORS];
432 ECursorType m_CursorType;
433
434 IGraphics::CTextureHandle GetEntitiesTexture();
435
436 std::unique_ptr<CEditorMap> m_pToolsMap;
437 std::shared_ptr<CLayerGroup> m_pBrush;
438 std::shared_ptr<CLayerTiles> m_pTilesetPicker;
439 std::shared_ptr<CLayerQuads> m_pQuadsetPicker;
440
441 const void *m_pUiGotContext = nullptr;
442
443 CMapSettingsBackend m_MapSettingsBackend;
444
445 // editor_ui.cpp
446 void UpdateTooltip(const void *pId, const CUIRect *pRect, const char *pToolTip);
447 ColorRGBA GetButtonColor(const void *pId, int Checked);
448 int DoButtonLogic(const void *pId, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
449 int DoButton_Editor(const void *pId, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
450 int DoButton_Env(const void *pId, const char *pText, int Checked, const CUIRect *pRect, const char *pToolTip, ColorRGBA Color, int Corners);
451 int DoButton_Ex(const void *pId, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip, int Corners, float FontSize = EditorFontSizes::MENU, int Align = TEXTALIGN_MC);
452 int DoButton_FontIcon(const void *pId, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip, int Corners, float FontSize = 10.0f);
453 int DoButton_MenuItem(const void *pId, const char *pText, int Checked, const CUIRect *pRect, int Flags = BUTTONFLAG_LEFT, const char *pToolTip = nullptr);
454 int DoButton_DraggableEx(const void *pId, const char *pText, int Checked, const CUIRect *pRect, bool *pClicked, bool *pAbrupted, int Flags, const char *pToolTip = nullptr, int Corners = IGraphics::CORNER_ALL, float FontSize = 10.0f);
455 bool DoEditBox(CLineInput *pLineInput, const CUIRect *pRect, float FontSize, int Corners = IGraphics::CORNER_ALL, const char *pToolTip = nullptr, const std::vector<STextColorSplit> &vColorSplits = {});
456 bool DoClearableEditBox(CLineInput *pLineInput, const CUIRect *pRect, float FontSize, int Corners = IGraphics::CORNER_ALL, const char *pToolTip = nullptr, const std::vector<STextColorSplit> &vColorSplits = {});
457 SEditResult<int> UiDoValueSelector(const void *pId, CUIRect *pRect, const char *pLabel, int Current, int Min, int Max, int Step, float Scale, const char *pToolTip, bool IsDegree = false, bool IsHex = false, int Corners = IGraphics::CORNER_ALL, const ColorRGBA *pColor = nullptr, bool ShowValue = true);
458 void RenderBackground(CUIRect View, IGraphics::CTextureHandle Texture, float Size, float Brightness) const;
459
460 // editor_server_settings.cpp
461 void DoMapSettingsEditBox(CMapSettingsBackend::CContextWithInput *pContext, const CUIRect *pRect, float FontSize, float DropdownMaxHeight, int Corners = IGraphics::CORNER_ALL, const char *pToolTip = nullptr);
462 template<typename T>
463 int DoEditBoxDropdown(SEditBoxDropdownContext *pDropdown, CLineInput *pLineInput, const CUIRect *pEditBoxRect, int x, float MaxHeight, bool AutoWidth, const std::vector<T> &vData, const FDropdownRenderCallback<T> &pfnMatchCallback);
464 template<typename T>
465 int RenderEditBoxDropdown(SEditBoxDropdownContext *pDropdown, CUIRect View, CLineInput *pLineInput, int x, float MaxHeight, bool AutoWidth, const std::vector<T> &vData, const FDropdownRenderCallback<T> &pfnMatchCallback);
466
467 // For tile art popups
468 CImageInfo m_TileArtImageInfo;
469 char m_aTileArtFilename[IO_MAX_PATH_LENGTH];
470 void TileArtCheckColors();
471
472 // For quad art popups
473 CImageInfo m_QuadArtImageInfo;
474 CQuadArtParameters m_QuadArtParameters;
475
476 static CUi::EPopupMenuFunctionResult PopupMenuFile(void *pContext, CUIRect View, bool Active);
477 static CUi::EPopupMenuFunctionResult PopupMenuTools(void *pContext, CUIRect View, bool Active);
478 static CUi::EPopupMenuFunctionResult PopupMenuSettings(void *pContext, CUIRect View, bool Active);
479 class CPopupMapTab : public SPopupMenuId
480 {
481 public:
482 CEditor *m_pEditor;
483 size_t m_SelectedMap;
484 static CUi::EPopupMenuFunctionResult Render(void *pContext, CUIRect View, bool Active);
485
486 private:
487 const char m_CloseButtonId = 0;
488 const char m_CopyNameButtonId = 0;
489 const char m_CopyPathButtonId = 0;
490 const char m_ShowFileButtonId = 0;
491 };
492 CPopupMapTab m_PopupMapTab;
493 static CUi::EPopupMenuFunctionResult PopupGroup(void *pContext, CUIRect View, bool Active);
494 struct SLayerPopupContext : public SPopupMenuId
495 {
496 CEditor *m_pEditor;
497 std::vector<std::shared_ptr<CLayerTiles>> m_vpLayers;
498 std::vector<int> m_vLayerIndices;
499 CLayerTiles::SCommonPropState m_CommonPropState;
500 };
501 static CUi::EPopupMenuFunctionResult PopupLayer(void *pContext, CUIRect View, bool Active);
502 class CQuadPopupContext : public SPopupMenuId
503 {
504 public:
505 CEditor *m_pEditor;
506 int m_SelectedQuadIndex;
507 int m_Color;
508 };
509 CQuadPopupContext m_QuadPopupContext;
510 static CUi::EPopupMenuFunctionResult PopupQuad(void *pContext, CUIRect View, bool Active);
511 static CUi::EPopupMenuFunctionResult PopupSource(void *pContext, CUIRect View, bool Active);
512 class CPointPopupContext : public SPopupMenuId
513 {
514 public:
515 CEditor *m_pEditor;
516 int m_SelectedQuadPoint;
517 int m_SelectedQuadIndex;
518 };
519 CPointPopupContext m_PointPopupContext;
520 static CUi::EPopupMenuFunctionResult PopupPoint(void *pContext, CUIRect View, bool Active);
521 static CUi::EPopupMenuFunctionResult PopupImage(void *pContext, CUIRect View, bool Active);
522 static CUi::EPopupMenuFunctionResult PopupSound(void *pContext, CUIRect View, bool Active);
523 static CUi::EPopupMenuFunctionResult PopupMapInfo(void *pContext, CUIRect View, bool Active);
524 static CUi::EPopupMenuFunctionResult PopupEvent(void *pContext, CUIRect View, bool Active);
525 static CUi::EPopupMenuFunctionResult PopupSelectImage(void *pContext, CUIRect View, bool Active);
526 static CUi::EPopupMenuFunctionResult PopupSelectSound(void *pContext, CUIRect View, bool Active);
527 static CUi::EPopupMenuFunctionResult PopupSelectGametileOp(void *pContext, CUIRect View, bool Active);
528 static CUi::EPopupMenuFunctionResult PopupSelectAutomapperConfig(void *pContext, CUIRect View, bool Active);
529 static CUi::EPopupMenuFunctionResult PopupSelectAutomapperReference(void *pContext, CUIRect View, bool Active);
530 static CUi::EPopupMenuFunctionResult PopupTele(void *pContext, CUIRect View, bool Active);
531 static CUi::EPopupMenuFunctionResult PopupSpeedup(void *pContext, CUIRect View, bool Active);
532 static CUi::EPopupMenuFunctionResult PopupSwitch(void *pContext, CUIRect View, bool Active);
533 static CUi::EPopupMenuFunctionResult PopupTune(void *pContext, CUIRect View, bool Active);
534 static CUi::EPopupMenuFunctionResult PopupGoto(void *pContext, CUIRect View, bool Active);
535 static CUi::EPopupMenuFunctionResult PopupEntities(void *pContext, CUIRect View, bool Active);
536 static CUi::EPopupMenuFunctionResult PopupProofMode(void *pContext, CUIRect View, bool Active);
537 static CUi::EPopupMenuFunctionResult PopupAnimateSettings(void *pContext, CUIRect View, bool Active);
538 static CUi::EPopupMenuFunctionResult PopupQuadArt(void *pContext, CUIRect View, bool Active);
539
540 static bool CallbackOpenMap(const char *pFilename, int StorageType, void *pUser);
541 static bool CallbackAppendMap(const char *pFilename, int StorageType, void *pUser);
542 static bool CallbackSaveMap(const char *pFilename, int StorageType, void *pUser);
543 static bool CallbackSaveCopyMap(const char *pFilename, int StorageType, void *pUser);
544 static bool CallbackAddTileArt(const char *pFilepath, int StorageType, void *pUser);
545 static bool CallbackAddQuadArt(const char *pFilepath, int StorageType, void *pUser);
546 static bool CallbackSaveImage(const char *pFilename, int StorageType, void *pUser);
547 static bool CallbackSaveSound(const char *pFilename, int StorageType, void *pUser);
548 static bool CallbackCustomEntities(const char *pFilename, int StorageType, void *pUser);
549
550 void PopupSelectImageInvoke(int Current, float x, float y);
551 int PopupSelectImageResult();
552
553 void PopupSelectGametileOpInvoke(float x, float y);
554 int PopupSelectGameTileOpResult();
555
556 void PopupSelectAutomapperConfigInvoke(int Current, float x, float y);
557 int PopupSelectAutomapperConfigResult();
558
559 void PopupSelectSoundInvoke(int Current, float x, float y);
560 int PopupSelectSoundResult();
561
562 void PopupSelectAutomapperReferenceInvoke(int Current, float x, float y);
563 int PopupSelectAutomapperReferenceResult();
564
565 void DoQuadEnvelopes(const CLayerQuads *pLayerQuads);
566 void DoQuadEnvPoint(const CQuad *pQuad, CEnvelope *pEnvelope, int QuadIndex, int PointIndex);
567 void DrawQuadPointHandle(const CQuad *pQuad, int PointIndex, bool Selected);
568 void DoQuadPointInteraction(int LayerIndex, const std::shared_ptr<CLayerQuads> &pLayer, CQuad *pQuad, int QuadIndex, int PointIndex);
569 void UpdateHotQuadPoint(const CLayerQuads *pLayer, const std::vector<bool> &vQuadSelected);
570
571 void DoSoundSource(int LayerIndex, CSoundSource *pSource, int Index);
572 void UpdateHotSoundSource(const CLayerSounds *pLayer);
573
574 enum class EAxis
575 {
576 NONE = 0,
577 X,
578 Y,
579 };
580 struct SAxisAlignedBoundingBox
581 {
582 enum
583 {
584 POINT_TL = 0,
585 POINT_TR,
586 POINT_BL,
587 POINT_BR,
588 POINT_CENTER,
589 NUM_POINTS
590 };
591 CPoint m_aPoints[NUM_POINTS];
592 };
593
594 CScrollRegion m_MapTabsScrollRegion;
595 bool m_MapTabsRevealSelected = false;
596 void DoMapTabs(CUIRect MapTabs);
597
598 void DoToolbarLayers(CUIRect Toolbar);
599 void DoToolbarImages(CUIRect Toolbar);
600 void DoToolbarSounds(CUIRect Toolbar);
601 void DrawQuadPivotHandle(const CQuad *pQuad, bool Selected);
602 void DoQuadPivotInteraction(int LayerIndex, const std::shared_ptr<CLayerQuads> &pLayer, CQuad *pQuad, int Index);
603 void PreparePointDrag(const CQuad *pQuad, int QuadIndex, int PointIndex);
604 void DoPointDrag(CQuad *pQuad, int QuadIndex, int PointIndex, ivec2 Offset);
605 EAxis GetDragAxis(ivec2 Offset) const;
606 void DrawAxis(EAxis Axis, CPoint &OriginalPoint, CPoint &Point) const;
607 void DrawAABB(const SAxisAlignedBoundingBox &AABB, ivec2 Offset) const;
608
609 // Alignment methods
610 // These methods take `OffsetX` and `OffsetY` because the calculations are made with the original positions
611 // of the quad(s), before we started dragging. This allows us to edit `OffsetX` and `OffsetY` based on the previously
612 // calculated alignments.
613 struct SAlignmentInfo
614 {
615 CPoint m_AlignedPoint; // The "aligned" point, which we want to align/snap to
616 union
617 {
618 // The current changing value when aligned to this point. When aligning to a point on the X axis, then the X value is changing because
619 // we aligned the Y values (X axis aligned => Y values are the same, Y axis aligned => X values are the same).
620 int m_X;
621 int m_Y;
622 };
623 EAxis m_Axis; // The axis we are aligning on
624 int m_PointIndex; // The point index we are aligning
625 int m_Diff; // Store the difference
626 };
627 void ComputePointAlignments(const std::shared_ptr<CLayerQuads> &pLayer, CQuad *pQuad, int QuadIndex, int PointIndex, ivec2 Offset, std::vector<SAlignmentInfo> &vAlignments, bool Append = false) const;
628 void ComputePointsAlignments(const std::shared_ptr<CLayerQuads> &pLayer, bool Pivot, ivec2 Offset, std::vector<SAlignmentInfo> &vAlignments) const;
629 void ComputeAABBAlignments(const std::shared_ptr<CLayerQuads> &pLayer, const SAxisAlignedBoundingBox &AABB, ivec2 Offset, std::vector<SAlignmentInfo> &vAlignments) const;
630 void DrawPointAlignments(const std::vector<SAlignmentInfo> &vAlignments, ivec2 Offset) const;
631 void QuadSelectionAABB(const std::shared_ptr<CLayerQuads> &pLayer, SAxisAlignedBoundingBox &OutAABB);
632 void ApplyAlignments(const std::vector<SAlignmentInfo> &vAlignments, ivec2 &Offset);
633 void ApplyAxisAlignment(ivec2 &Offset) const;
634
635 bool ReplaceImage(const char *pFilename, int StorageType, bool CheckDuplicate);
636 static bool ReplaceImageCallback(const char *pFilename, int StorageType, void *pUser);
637 bool ReplaceSound(const char *pFilename, int StorageType, bool CheckDuplicate);
638 static bool ReplaceSoundCallback(const char *pFilename, int StorageType, void *pUser);
639 static bool AddImage(const char *pFilename, int StorageType, void *pUser);
640 static bool AddSound(const char *pFilename, int StorageType, void *pUser);
641
642 static bool IsVanillaImage(const char *pImage);
643
644 enum class ELayerOperation
645 {
646 NONE,
647 CLICK,
648 LAYER_DRAG,
649 GROUP_DRAG,
650 };
651 class CRenderLayersState
652 {
653 public:
654 ELayerOperation m_Operation;
655 ELayerOperation m_PreviousOperation;
656 const void *m_pDraggedButton;
657 float m_InitialMouseY;
658 float m_InitialCutHeight;
659 bool m_ScrollToSelectionNext;
660 int m_InitialGroupIndex;
661 std::vector<int> m_vInitialLayerIndices;
662 const char m_AddGroupButtonId = 0;
663 const char m_CollapseAllButtonId = 0;
664 const SPopupMenuId m_PopupGroupId = {};
665 SLayerPopupContext m_LayerPopupContext;
666
667 void Reset();
668 };
669 CRenderLayersState m_RenderLayersState;
670 void RenderLayers(CUIRect LayersBox);
671
672 void RenderImagesList(CUIRect Toolbox);
673 void RenderSelectedImage(CUIRect View) const;
674 void RenderSounds(CUIRect Toolbox);
675 void RenderModebar(CUIRect View);
676 void RenderStatusbar(CUIRect View, CUIRect *pTooltipRect);
677 void RenderTooltip(CUIRect TooltipRect);
678
679 void RenderMapSettingsErrorDialog();
680 void RenderServerSettingsEditor(CUIRect View, bool ShowServerSettingsEditorLast);
681 static void MapSettingsDropdownRenderCallback(const SPossibleValueMatch &Match, char (&aOutput)[128], std::vector<STextColorSplit> &vColorSplits);
682
683 void RenderEditorHistory(CUIRect View);
684
685 enum class EDragSide // Which side is the drag bar on
686 {
687 BOTTOM,
688 LEFT,
689 TOP,
690 RIGHT,
691 };
692 void DoEditorDragBar(CUIRect View, CUIRect *pDragBar, EDragSide Side, float *pValue, float MinValue = 100.0f, float MaxValue = 400.0f);
693
694 void RenderMenubar(CUIRect Menubar);
695 void ShowHelp();
696 void Exit();
697
698 void DoAudioPreview(CUIRect View, const void *pPlayPauseButtonId, const void *pStopButtonId, const void *pSeekBarId, int SampleId);
699
700 // DDRace
701
702 IGraphics::CTextureHandle GetFrontTexture();
703 IGraphics::CTextureHandle GetTeleTexture();
704 IGraphics::CTextureHandle GetSpeedupTexture();
705 IGraphics::CTextureHandle GetSwitchTexture();
706 IGraphics::CTextureHandle GetTuneTexture();
707
708 unsigned char m_TeleNumber;
709 unsigned char m_TeleCheckpointNumber;
710 unsigned char m_ViewTeleNumber;
711
712 unsigned char m_TuningNumber;
713 unsigned char m_ViewTuning;
714
715 unsigned char m_SpeedupForce;
716 unsigned char m_SpeedupMaxSpeed;
717 short m_SpeedupAngle;
718
719 unsigned char m_SwitchNumber;
720 unsigned char m_SwitchDelay;
721 unsigned char m_ViewSwitch;
722
723 // AdjustValue must be -1, 0 or 1
724 void AdjustBrushSpecialTiles(bool UseNextFree, int AdjustModifiers, int AdjustValue);
725
726private:
727 std::vector<std::unique_ptr<CEditorMap>> m_vpMaps;
728 size_t m_SelectedMap;
729
730 CEditorHistory &ActiveHistory();
731
732 std::map<int, CPoint[5]> m_QuadDragOriginalPoints;
733};
734
735#endif
736