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