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#include "smooth_value.h"
15
16#include <base/bezier.h>
17#include <base/fs.h>
18
19#include <engine/editor.h>
20#include <engine/graphics.h>
21
22#include <game/client/ui.h>
23#include <game/client/ui_listbox.h>
24#include <game/editor/enums.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/map/render_interfaces.h>
42#include <game/mapitems.h>
43
44#include <deque>
45#include <functional>
46#include <map>
47#include <memory>
48#include <string>
49#include <vector>
50
51template<typename T>
52using FDropdownRenderCallback = std::function<void(const T &, char (&aOutput)[128], std::vector<STextColorSplit> &)>;
53
54// CEditor SPECIFIC
55enum
56{
57 MODE_LAYERS = 0,
58 MODE_IMAGES,
59 MODE_SOUNDS,
60
61 NUM_MODES,
62};
63
64enum
65{
66 DIALOG_NONE = 0,
67 DIALOG_FILE,
68 DIALOG_MAPSETTINGS_ERROR,
69 DIALOG_QUICK_PROMPT,
70
71 // The font typer component sets m_Dialog
72 // while it is active to make sure no other component
73 // interprets the key presses
74 DIALOG_PSEUDO_FONT_TYPER,
75};
76
77class CProperty
78{
79public:
80 CProperty(const char *pName, int Value, int Type, int Min, int Max) :
81 m_pName(pName), m_Value(Value), m_Type(Type), m_Min(Min), m_Max(Max) {}
82
83 CProperty(std::nullptr_t) :
84 m_pName(nullptr), m_Value(0), m_Type(0), m_Min(0), m_Max(0) {}
85
86 const char *m_pName;
87 int m_Value;
88 int m_Type;
89 int m_Min;
90 int m_Max;
91};
92
93enum
94{
95 PROPTYPE_NULL = 0,
96 PROPTYPE_BOOL,
97 PROPTYPE_INT,
98 PROPTYPE_ANGLE_SCROLL,
99 PROPTYPE_COLOR,
100 PROPTYPE_IMAGE,
101 PROPTYPE_ENVELOPE,
102 PROPTYPE_SHIFT,
103 PROPTYPE_SOUND,
104 PROPTYPE_AUTOMAPPER,
105 PROPTYPE_AUTOMAPPER_REFERENCE,
106};
107
108class CEditor : public IEditor, public IEnvelopeEval
109{
110 class IInput *m_pInput = nullptr;
111 class IClient *m_pClient = nullptr;
112 class IConfigManager *m_pConfigManager = nullptr;
113 class CConfig *m_pConfig = nullptr;
114 class IEngine *m_pEngine = nullptr;
115 class IGraphics *m_pGraphics = nullptr;
116 class ITextRender *m_pTextRender = nullptr;
117 class ISound *m_pSound = nullptr;
118 class IStorage *m_pStorage = nullptr;
119 CRenderMap m_RenderMap;
120 CUi m_UI;
121
122 std::vector<std::reference_wrapper<CEditorComponent>> m_vComponents;
123 CMapView m_MapView;
124 CLayerSelector m_LayerSelector;
125 CFileBrowser m_FileBrowser;
126 CPrompt m_Prompt;
127 CFontTyper m_FontTyper;
128
129 bool m_EditorWasUsedBefore = false;
130
131 IGraphics::CTextureHandle m_EntitiesTexture;
132
133 IGraphics::CTextureHandle m_FrontTexture;
134 IGraphics::CTextureHandle m_TeleTexture;
135 IGraphics::CTextureHandle m_SpeedupTexture;
136 IGraphics::CTextureHandle m_SwitchTexture;
137 IGraphics::CTextureHandle m_TuneTexture;
138
139 int GetTextureUsageFlag() const;
140
141 enum EPreviewState
142 {
143 PREVIEW_UNLOADED,
144 PREVIEW_LOADED,
145 PREVIEW_ERROR,
146 };
147
148 std::shared_ptr<CLayerGroup> m_apSavedBrushes[10];
149 static constexpr ColorRGBA ms_DefaultPropColor = ColorRGBA(1, 1, 1, 0.5f);
150
151public:
152 class IInput *Input() const { return m_pInput; }
153 class IClient *Client() const { return m_pClient; }
154 class IConfigManager *ConfigManager() const { return m_pConfigManager; }
155 class CConfig *Config() const { return m_pConfig; }
156 class IEngine *Engine() const { return m_pEngine; }
157 class IGraphics *Graphics() const { return m_pGraphics; }
158 class ISound *Sound() const { return m_pSound; }
159 class ITextRender *TextRender() const { return m_pTextRender; }
160 class IStorage *Storage() const { return m_pStorage; }
161 CUi *Ui() { return &m_UI; }
162 CRenderMap *RenderMap() { return &m_RenderMap; }
163
164 CEditorMap *Map() { return &m_Map; }
165 const CEditorMap *Map() const { return &m_Map; }
166 CMapView *MapView() { return &m_MapView; }
167 const CMapView *MapView() const { return &m_MapView; }
168 CLayerSelector *LayerSelector() { return &m_LayerSelector; }
169
170 void FillGameTiles(EGameTileOp FillTile) const;
171 bool CanFillGameTiles() const;
172 void AddQuadOrSound();
173 void AddGroup();
174 void AddSoundLayer();
175 void AddTileLayer();
176 void AddQuadsLayer();
177 void AddSwitchLayer();
178 void AddFrontLayer();
179 void AddTuneLayer();
180 void AddSpeedupLayer();
181 void AddTeleLayer();
182 void DeleteSelectedLayer();
183 void LayerSelectImage();
184 bool IsNonGameTileLayerSelected() const;
185 void MapDetails();
186 void TestMapLocally();
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_ZoomEnvelopeX(1.0f, 0.1f, 600.0f),
196 m_ZoomEnvelopeY(640.0f, 0.1f, 32000.0f),
197 m_MapSettingsCommandContext(m_MapSettingsBackend.NewContext(pLineInput: &m_SettingsCommandInput)),
198 m_Map(this)
199 {
200 m_EntitiesTexture.Invalidate();
201 m_FrontTexture.Invalidate();
202 m_TeleTexture.Invalidate();
203 m_SpeedupTexture.Invalidate();
204 m_SwitchTexture.Invalidate();
205 m_TuneTexture.Invalidate();
206
207 m_Mode = MODE_LAYERS;
208 m_Dialog = 0;
209
210 m_BrushColorEnabled = true;
211
212 m_aFilenamePendingLoad[0] = '\0';
213
214 m_PopupEventActivated = false;
215 m_PopupEventWasActivated = false;
216
217 m_ToolbarPreviewSound = -1;
218
219 m_SelectEntitiesImage = "DDNet";
220
221 m_ResetZoomEnvelope = true;
222 m_OffsetEnvelopeX = 0.1f;
223 m_OffsetEnvelopeY = 0.5f;
224
225 m_ShowMousePointer = true;
226
227 m_GuiActive = true;
228 m_PreviewZoom = false;
229
230 m_ShowTileInfo = SHOW_TILE_OFF;
231 m_ShowDetail = true;
232 m_Animate = false;
233 m_AnimateStart = 0;
234 m_AnimateTime = 0;
235 m_AnimateSpeed = 1;
236 m_AnimateUpdatePopup = false;
237
238 for(size_t i = 0; i < std::size(m_aSavedColors); ++i)
239 {
240 m_aSavedColors[i] = color_cast<ColorRGBA>(hsl: ColorHSLA(i / (float)std::size(m_aSavedColors), 1.0f, 0.5f));
241 }
242
243 m_CheckerTexture.Invalidate();
244 for(auto &CursorTexture : m_aCursorTextures)
245 CursorTexture.Invalidate();
246
247 m_CursorType = CURSOR_NORMAL;
248
249 // DDRace
250
251 m_TeleNumber = 1;
252 m_TeleCheckpointNumber = 1;
253 m_ViewTeleNumber = 0;
254
255 m_TuningNumber = 1;
256 m_ViewTuning = 0;
257
258 m_SwitchNumber = 1;
259 m_SwitchDelay = 0;
260 m_SpeedupForce = 50;
261 m_SpeedupMaxSpeed = 0;
262 m_SpeedupAngle = 0;
263 m_LargeLayerWasWarned = false;
264 m_PreventUnusedTilesWasWarned = false;
265 m_AllowPlaceUnusedTiles = EUnusedEntities::NOT_ALLOWED;
266 m_BrushDrawDestructive = true;
267 }
268
269 class CHoverTile
270 {
271 public:
272 CHoverTile(int Group, int Layer, int x, int y, const CTile Tile) :
273 m_Group(Group),
274 m_Layer(Layer),
275 m_X(x),
276 m_Y(y),
277 m_Tile(Tile)
278 {
279 }
280
281 int m_Group;
282 int m_Layer;
283 int m_X;
284 int m_Y;
285 const CTile m_Tile;
286 };
287 std::vector<CHoverTile> m_vHoverTiles;
288 const std::vector<CHoverTile> &HoverTiles() const { return m_vHoverTiles; }
289
290 void Init() override;
291 void OnUpdate() override;
292 void OnRender() override;
293 void OnActivate() override;
294 void OnWindowResize() override;
295 void OnClose() override;
296 void OnDialogClose();
297 bool HasUnsavedData() const override { return Map()->m_Modified; }
298 void UpdateMentions() override { m_Mentions++; }
299 void ResetMentions() override { m_Mentions = 0; }
300 void OnIngameMoved() override { m_IngameMoved = true; }
301 void ResetIngameMoved() override { m_IngameMoved = false; }
302
303 void HandleCursorMovement();
304 void OnMouseMove(vec2 MousePos);
305 void MouseAxisLock(vec2 &CursorRel);
306 vec2 m_MouseAxisInitialPos = vec2(0.0f, 0.0f);
307 enum class EAxisLock
308 {
309 START,
310 NONE,
311 HORIZONTAL,
312 VERTICAL,
313 } m_MouseAxisLockState = EAxisLock::START;
314
315 /**
316 * Global time when the autosave was last updated in the @link HandleAutosave @endlink function.
317 * This is used so that the autosave does not immediately activate when reopening the editor after
318 * a longer time of inactivity, as autosaves are only updated while the editor is open.
319 */
320 float m_LastAutosaveUpdateTime = -1.0f;
321 void HandleAutosave();
322 void HandleWriterFinishJobs();
323
324 // TODO: The name of the ShowFileDialogError function is not accurate anymore, this is used for generic error messages.
325 // Popups in UI should be shared_ptrs to make this even more generic.
326 class CStringKeyComparator
327 {
328 public:
329 bool operator()(const char *pLhs, const char *pRhs) const;
330 };
331 std::map<const char *, CUi::SMessagePopupContext *, CStringKeyComparator> m_PopupMessageContexts;
332 [[gnu::format(printf, 2, 3)]] void ShowFileDialogError(const char *pFormat, ...);
333
334 void Reset(bool CreateDefault = true);
335 bool Save(const char *pFilename) override;
336 bool Load(const char *pFilename, int StorageType) override;
337 bool HandleMapDrop(const char *pFilename, int StorageType) override;
338 void LoadCurrentMap();
339 void Render();
340
341 void RenderPressedKeys(CUIRect View);
342 void RenderSavingIndicator(CUIRect View);
343 void FreeDynamicPopupMenus();
344 void UpdateColorPipette();
345 void RenderMousePointer();
346 void RenderGameEntities(const std::shared_ptr<CLayerTiles> &pTiles);
347 void RenderSwitchEntities(const std::shared_ptr<CLayerTiles> &pTiles);
348
349 template<typename E>
350 SEditResult<E> DoPropertiesWithState(CUIRect *pToolbox, CProperty *pProps, int *pIds, int *pNewVal, const std::vector<ColorRGBA> &vColors = {});
351 int DoProperties(CUIRect *pToolbox, CProperty *pProps, int *pIds, int *pNewVal, const std::vector<ColorRGBA> &vColors = {});
352
353 CUi::SColorPickerPopupContext m_ColorPickerPopupContext;
354 const void *m_pColorPickerPopupActiveId = nullptr;
355 void DoColorPickerButton(const void *pId, const CUIRect *pRect, ColorRGBA Color, const std::function<void(ColorRGBA Color)> &SetColor);
356
357 int m_Mode;
358 int m_Dialog;
359 char m_aTooltip[256] = "";
360
361 bool m_BrushColorEnabled;
362
363 /**
364 * File which is pending to be loaded by @link POPEVENT_LOADDROP @endlink.
365 */
366 char m_aFilenamePendingLoad[IO_MAX_PATH_LENGTH] = "";
367
368 enum
369 {
370 POPEVENT_EXIT = 0,
371 POPEVENT_LOAD,
372 POPEVENT_LOADCURRENT,
373 POPEVENT_LOADDROP,
374 POPEVENT_NEW,
375 POPEVENT_LARGELAYER,
376 POPEVENT_PREVENTUNUSEDTILES,
377 POPEVENT_IMAGEDIV16,
378 POPEVENT_IMAGE_MAX,
379 POPEVENT_SOUND_MAX,
380 POPEVENT_PLACE_BORDER_TILES,
381 POPEVENT_TILE_ART_BIG_IMAGE,
382 POPEVENT_TILE_ART_MANY_COLORS,
383 POPEVENT_TILE_ART_TOO_MANY_COLORS,
384 POPEVENT_QUAD_ART_BIG_IMAGE,
385 POPEVENT_REMOVE_USED_IMAGE,
386 POPEVENT_REMOVE_USED_SOUND,
387 POPEVENT_RESTART_SERVER,
388 POPEVENT_RESTARTING_SERVER,
389 };
390
391 int m_PopupEventType;
392 int m_PopupEventActivated;
393 int m_PopupEventWasActivated;
394 bool m_LargeLayerWasWarned;
395 bool m_PreventUnusedTilesWasWarned;
396
397 enum class EUnusedEntities
398 {
399 ALLOWED_IMPLICIT = -1,
400 NOT_ALLOWED = 0,
401 ALLOWED_EXPLICIT = 1,
402 };
403 EUnusedEntities m_AllowPlaceUnusedTiles;
404 bool IsAllowPlaceUnusedTiles() const;
405
406 bool m_BrushDrawDestructive;
407
408 int m_Mentions = 0;
409 bool m_IngameMoved = false;
410
411 int m_ToolbarPreviewSound;
412
413 std::vector<std::string> m_vSelectEntitiesFiles;
414 std::string m_SelectEntitiesImage;
415
416 // Zooming
417 CSmoothValue m_ZoomEnvelopeX;
418 CSmoothValue m_ZoomEnvelopeY;
419
420 bool m_ResetZoomEnvelope;
421
422 float m_OffsetEnvelopeX;
423 float m_OffsetEnvelopeY;
424
425 bool m_ShowMousePointer;
426 bool m_GuiActive;
427
428 bool m_PreviewZoom;
429 float m_MouseWorldScale = 1.0f; // Mouse (i.e. UI) scale relative to the World (selected Group)
430 vec2 m_MouseWorldPos = vec2(0.0f, 0.0f);
431 vec2 m_MouseWorldNoParaPos = vec2(0.0f, 0.0f);
432 vec2 m_MouseDeltaWorld = vec2(0.0f, 0.0f);
433 const void *m_pContainerPanned;
434 const void *m_pContainerPannedLast;
435 char m_MapEditorId; // UI element ID for the main map editor
436
437 enum EShowTile
438 {
439 SHOW_TILE_OFF,
440 SHOW_TILE_DECIMAL,
441 SHOW_TILE_HEXADECIMAL
442 };
443 EShowTile m_ShowTileInfo;
444 bool m_ShowDetail;
445
446 bool m_Animate;
447 int64_t m_AnimateStart;
448 float m_AnimateTime;
449 float m_AnimateSpeed;
450 bool m_AnimateUpdatePopup;
451
452 enum EExtraEditor
453 {
454 EXTRAEDITOR_NONE = -1,
455 EXTRAEDITOR_ENVELOPES,
456 EXTRAEDITOR_SERVER_SETTINGS,
457 EXTRAEDITOR_HISTORY,
458 NUM_EXTRAEDITORS,
459 };
460 EExtraEditor m_ActiveExtraEditor = EXTRAEDITOR_NONE;
461 float m_aExtraEditorSplits[NUM_EXTRAEDITORS] = {250.0f, 250.0f, 250.0f};
462 float m_ToolBoxWidth = 100.0f;
463
464 bool m_ShowEnvelopePreview = false;
465 enum class EEnvelopePreview
466 {
467 NONE,
468 SELECTED,
469 ALL,
470 };
471 EEnvelopePreview m_ActiveEnvelopePreview = EEnvelopePreview::NONE;
472 enum class EQuadEnvelopePointOperation
473 {
474 NONE = 0,
475 MOVE,
476 ROTATE,
477 };
478 EQuadEnvelopePointOperation m_QuadEnvelopePointOperation = EQuadEnvelopePointOperation::NONE;
479
480 bool m_ShowPicker;
481
482 // Color palette and pipette
483 ColorRGBA m_aSavedColors[8];
484 ColorRGBA m_PipetteColor = ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f);
485 bool m_ColorPipetteActive = false;
486
487 IGraphics::CTextureHandle m_CheckerTexture;
488
489 enum ECursorType
490 {
491 CURSOR_NORMAL,
492 CURSOR_RESIZE_V,
493 CURSOR_RESIZE_H,
494 NUM_CURSORS
495 };
496 IGraphics::CTextureHandle m_aCursorTextures[ECursorType::NUM_CURSORS];
497 ECursorType m_CursorType;
498
499 IGraphics::CTextureHandle GetEntitiesTexture();
500
501 std::shared_ptr<CLayerGroup> m_pBrush;
502 std::shared_ptr<CLayerTiles> m_pTilesetPicker;
503 std::shared_ptr<CLayerQuads> m_pQuadsetPicker;
504
505 const void *m_pUiGotContext = nullptr;
506
507 std::deque<std::shared_ptr<CDataFileWriterFinishJob>> m_WriterFinishJobs;
508
509 void EnvelopeEval(int TimeOffsetMillis, int EnvelopeIndex, ColorRGBA &Result, size_t Channels) override;
510
511 CLineInputBuffered<256> m_SettingsCommandInput;
512 CMapSettingsBackend m_MapSettingsBackend;
513 CMapSettingsBackend::CContext m_MapSettingsCommandContext;
514
515 // editor_ui.cpp
516 void UpdateTooltip(const void *pId, const CUIRect *pRect, const char *pToolTip);
517 ColorRGBA GetButtonColor(const void *pId, int Checked);
518 int DoButtonLogic(const void *pId, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
519 int DoButton_Editor(const void *pId, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
520 int DoButton_Env(const void *pId, const char *pText, int Checked, const CUIRect *pRect, const char *pToolTip, ColorRGBA Color, int Corners);
521 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);
522 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);
523 int DoButton_MenuItem(const void *pId, const char *pText, int Checked, const CUIRect *pRect, int Flags = BUTTONFLAG_LEFT, const char *pToolTip = nullptr);
524 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);
525 bool DoEditBox(CLineInput *pLineInput, const CUIRect *pRect, float FontSize, int Corners = IGraphics::CORNER_ALL, const char *pToolTip = nullptr, const std::vector<STextColorSplit> &vColorSplits = {});
526 bool DoClearableEditBox(CLineInput *pLineInput, const CUIRect *pRect, float FontSize, int Corners = IGraphics::CORNER_ALL, const char *pToolTip = nullptr, const std::vector<STextColorSplit> &vColorSplits = {});
527 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);
528 void RenderBackground(CUIRect View, IGraphics::CTextureHandle Texture, float Size, float Brightness) const;
529
530 // editor_server_settings.cpp
531 void DoMapSettingsEditBox(CMapSettingsBackend::CContext *pContext, const CUIRect *pRect, float FontSize, float DropdownMaxHeight, int Corners = IGraphics::CORNER_ALL, const char *pToolTip = nullptr);
532 template<typename T>
533 int DoEditBoxDropdown(SEditBoxDropdownContext *pDropdown, CLineInput *pLineInput, const CUIRect *pEditBoxRect, int x, float MaxHeight, bool AutoWidth, const std::vector<T> &vData, const FDropdownRenderCallback<T> &pfnMatchCallback);
534 template<typename T>
535 int RenderEditBoxDropdown(SEditBoxDropdownContext *pDropdown, CUIRect View, CLineInput *pLineInput, int x, float MaxHeight, bool AutoWidth, const std::vector<T> &vData, const FDropdownRenderCallback<T> &pfnMatchCallback);
536
537 // For tile art popups
538 CImageInfo m_TileArtImageInfo;
539 char m_aTileArtFilename[IO_MAX_PATH_LENGTH];
540 void TileArtCheckColors();
541
542 // For quad art popups
543 CImageInfo m_QuadArtImageInfo;
544 CQuadArtParameters m_QuadArtParameters;
545
546 static CUi::EPopupMenuFunctionResult PopupMenuFile(void *pContext, CUIRect View, bool Active);
547 static CUi::EPopupMenuFunctionResult PopupMenuTools(void *pContext, CUIRect View, bool Active);
548 static CUi::EPopupMenuFunctionResult PopupMenuSettings(void *pContext, CUIRect View, bool Active);
549 static CUi::EPopupMenuFunctionResult PopupGroup(void *pContext, CUIRect View, bool Active);
550 struct SLayerPopupContext : public SPopupMenuId
551 {
552 CEditor *m_pEditor;
553 std::vector<std::shared_ptr<CLayerTiles>> m_vpLayers;
554 std::vector<int> m_vLayerIndices;
555 CLayerTiles::SCommonPropState m_CommonPropState;
556 };
557 static CUi::EPopupMenuFunctionResult PopupLayer(void *pContext, CUIRect View, bool Active);
558 class CQuadPopupContext : public SPopupMenuId
559 {
560 public:
561 CEditor *m_pEditor;
562 int m_SelectedQuadIndex;
563 int m_Color;
564 };
565 CQuadPopupContext m_QuadPopupContext;
566 static CUi::EPopupMenuFunctionResult PopupQuad(void *pContext, CUIRect View, bool Active);
567 static CUi::EPopupMenuFunctionResult PopupSource(void *pContext, CUIRect View, bool Active);
568 class CPointPopupContext : public SPopupMenuId
569 {
570 public:
571 CEditor *m_pEditor;
572 int m_SelectedQuadPoint;
573 int m_SelectedQuadIndex;
574 };
575 CPointPopupContext m_PointPopupContext;
576 static CUi::EPopupMenuFunctionResult PopupPoint(void *pContext, CUIRect View, bool Active);
577 static CUi::EPopupMenuFunctionResult PopupEnvPoint(void *pContext, CUIRect View, bool Active);
578 static CUi::EPopupMenuFunctionResult PopupEnvPointMulti(void *pContext, CUIRect View, bool Active);
579 static CUi::EPopupMenuFunctionResult PopupEnvPointCurveType(void *pContext, CUIRect View, bool Active);
580 static CUi::EPopupMenuFunctionResult PopupImage(void *pContext, CUIRect View, bool Active);
581 static CUi::EPopupMenuFunctionResult PopupSound(void *pContext, CUIRect View, bool Active);
582 static CUi::EPopupMenuFunctionResult PopupMapInfo(void *pContext, CUIRect View, bool Active);
583 static CUi::EPopupMenuFunctionResult PopupEvent(void *pContext, CUIRect View, bool Active);
584 static CUi::EPopupMenuFunctionResult PopupSelectImage(void *pContext, CUIRect View, bool Active);
585 static CUi::EPopupMenuFunctionResult PopupSelectSound(void *pContext, CUIRect View, bool Active);
586 static CUi::EPopupMenuFunctionResult PopupSelectGametileOp(void *pContext, CUIRect View, bool Active);
587 static CUi::EPopupMenuFunctionResult PopupSelectConfigAutoMap(void *pContext, CUIRect View, bool Active);
588 static CUi::EPopupMenuFunctionResult PopupSelectAutoMapReference(void *pContext, CUIRect View, bool Active);
589 static CUi::EPopupMenuFunctionResult PopupTele(void *pContext, CUIRect View, bool Active);
590 static CUi::EPopupMenuFunctionResult PopupSpeedup(void *pContext, CUIRect View, bool Active);
591 static CUi::EPopupMenuFunctionResult PopupSwitch(void *pContext, CUIRect View, bool Active);
592 static CUi::EPopupMenuFunctionResult PopupTune(void *pContext, CUIRect View, bool Active);
593 static CUi::EPopupMenuFunctionResult PopupGoto(void *pContext, CUIRect View, bool Active);
594 static CUi::EPopupMenuFunctionResult PopupEntities(void *pContext, CUIRect View, bool Active);
595 static CUi::EPopupMenuFunctionResult PopupProofMode(void *pContext, CUIRect View, bool Active);
596 static CUi::EPopupMenuFunctionResult PopupAnimateSettings(void *pContext, CUIRect View, bool Active);
597 int m_PopupEnvelopeSelectedPoint = -1;
598 static CUi::EPopupMenuFunctionResult PopupEnvelopeCurvetype(void *pContext, CUIRect View, bool Active);
599 static CUi::EPopupMenuFunctionResult PopupQuadArt(void *pContext, CUIRect View, bool Active);
600
601 static bool CallbackOpenMap(const char *pFilename, int StorageType, void *pUser);
602 static bool CallbackAppendMap(const char *pFilename, int StorageType, void *pUser);
603 static bool CallbackSaveMap(const char *pFilename, int StorageType, void *pUser);
604 static bool CallbackSaveCopyMap(const char *pFilename, int StorageType, void *pUser);
605 static bool CallbackAddTileArt(const char *pFilepath, int StorageType, void *pUser);
606 static bool CallbackAddQuadArt(const char *pFilepath, int StorageType, void *pUser);
607 static bool CallbackSaveImage(const char *pFilename, int StorageType, void *pUser);
608 static bool CallbackSaveSound(const char *pFilename, int StorageType, void *pUser);
609 static bool CallbackCustomEntities(const char *pFilename, int StorageType, void *pUser);
610
611 void PopupSelectImageInvoke(int Current, float x, float y);
612 int PopupSelectImageResult();
613
614 void PopupSelectGametileOpInvoke(float x, float y);
615 int PopupSelectGameTileOpResult();
616
617 void PopupSelectConfigAutoMapInvoke(int Current, float x, float y);
618 int PopupSelectConfigAutoMapResult();
619
620 void PopupSelectSoundInvoke(int Current, float x, float y);
621 int PopupSelectSoundResult();
622
623 void PopupSelectAutoMapReferenceInvoke(int Current, float x, float y);
624 int PopupSelectAutoMapReferenceResult();
625
626 void DoQuadEnvelopes(const CLayerQuads *pLayerQuads);
627 void DoQuadEnvPoint(const CQuad *pQuad, CEnvelope *pEnvelope, int QuadIndex, int PointIndex);
628 void DoQuadPoint(int LayerIndex, const std::shared_ptr<CLayerQuads> &pLayer, CQuad *pQuad, int QuadIndex, int v);
629 void UpdateHotQuadPoint(const CLayerQuads *pLayer);
630
631 float TriangleArea(vec2 A, vec2 B, vec2 C);
632 bool IsInTriangle(vec2 Point, vec2 A, vec2 B, vec2 C);
633 void DoQuadKnife(int QuadIndex);
634
635 void DoSoundSource(int LayerIndex, CSoundSource *pSource, int Index);
636 void UpdateHotSoundSource(const CLayerSounds *pLayer);
637
638 enum class EAxis
639 {
640 NONE = 0,
641 X,
642 Y,
643 };
644 struct SAxisAlignedBoundingBox
645 {
646 enum
647 {
648 POINT_TL = 0,
649 POINT_TR,
650 POINT_BL,
651 POINT_BR,
652 POINT_CENTER,
653 NUM_POINTS
654 };
655 CPoint m_aPoints[NUM_POINTS];
656 };
657 void DoMapEditor(CUIRect View);
658 void DoToolbarLayers(CUIRect Toolbar);
659 void DoToolbarImages(CUIRect Toolbar);
660 void DoToolbarSounds(CUIRect Toolbar);
661 void DoQuad(int LayerIndex, const std::shared_ptr<CLayerQuads> &pLayer, CQuad *pQuad, int Index);
662 void PreparePointDrag(const CQuad *pQuad, int QuadIndex, int PointIndex);
663 void DoPointDrag(CQuad *pQuad, int QuadIndex, int PointIndex, ivec2 Offset);
664 EAxis GetDragAxis(ivec2 Offset) const;
665 void DrawAxis(EAxis Axis, CPoint &OriginalPoint, CPoint &Point) const;
666 void DrawAABB(const SAxisAlignedBoundingBox &AABB, ivec2 Offset) const;
667
668 // Alignment methods
669 // These methods take `OffsetX` and `OffsetY` because the calculations are made with the original positions
670 // of the quad(s), before we started dragging. This allows us to edit `OffsetX` and `OffsetY` based on the previously
671 // calculated alignments.
672 struct SAlignmentInfo
673 {
674 CPoint m_AlignedPoint; // The "aligned" point, which we want to align/snap to
675 union
676 {
677 // 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
678 // we aligned the Y values (X axis aligned => Y values are the same, Y axis aligned => X values are the same).
679 int m_X;
680 int m_Y;
681 };
682 EAxis m_Axis; // The axis we are aligning on
683 int m_PointIndex; // The point index we are aligning
684 int m_Diff; // Store the difference
685 };
686 void ComputePointAlignments(const std::shared_ptr<CLayerQuads> &pLayer, CQuad *pQuad, int QuadIndex, int PointIndex, ivec2 Offset, std::vector<SAlignmentInfo> &vAlignments, bool Append = false) const;
687 void ComputePointsAlignments(const std::shared_ptr<CLayerQuads> &pLayer, bool Pivot, ivec2 Offset, std::vector<SAlignmentInfo> &vAlignments) const;
688 void ComputeAABBAlignments(const std::shared_ptr<CLayerQuads> &pLayer, const SAxisAlignedBoundingBox &AABB, ivec2 Offset, std::vector<SAlignmentInfo> &vAlignments) const;
689 void DrawPointAlignments(const std::vector<SAlignmentInfo> &vAlignments, ivec2 Offset) const;
690 void QuadSelectionAABB(const std::shared_ptr<CLayerQuads> &pLayer, SAxisAlignedBoundingBox &OutAABB);
691 void ApplyAlignments(const std::vector<SAlignmentInfo> &vAlignments, ivec2 &Offset);
692 void ApplyAxisAlignment(ivec2 &Offset) const;
693
694 bool ReplaceImage(const char *pFilename, int StorageType, bool CheckDuplicate);
695 static bool ReplaceImageCallback(const char *pFilename, int StorageType, void *pUser);
696 bool ReplaceSound(const char *pFilename, int StorageType, bool CheckDuplicate);
697 static bool ReplaceSoundCallback(const char *pFilename, int StorageType, void *pUser);
698 static bool AddImage(const char *pFilename, int StorageType, void *pUser);
699 static bool AddSound(const char *pFilename, int StorageType, void *pUser);
700
701 static bool IsVanillaImage(const char *pImage);
702
703 void RenderLayers(CUIRect LayersBox);
704 void RenderImagesList(CUIRect Toolbox);
705 void RenderSelectedImage(CUIRect View) const;
706 void RenderSounds(CUIRect Toolbox);
707 void RenderModebar(CUIRect View);
708 void RenderStatusbar(CUIRect View, CUIRect *pTooltipRect);
709 void RenderTooltip(CUIRect TooltipRect);
710
711 void RenderEnvelopeEditor(CUIRect View);
712 void RenderEnvelopeEditorColorBar(CUIRect ColorBar, const std::shared_ptr<CEnvelope> &pEnvelope);
713
714 void RenderMapSettingsErrorDialog();
715 void RenderServerSettingsEditor(CUIRect View, bool ShowServerSettingsEditorLast);
716 static void MapSettingsDropdownRenderCallback(const SPossibleValueMatch &Match, char (&aOutput)[128], std::vector<STextColorSplit> &vColorSplits);
717
718 void RenderEditorHistory(CUIRect View);
719
720 enum class EDragSide // Which side is the drag bar on
721 {
722 BOTTOM,
723 LEFT,
724 TOP,
725 RIGHT,
726 };
727 void DoEditorDragBar(CUIRect View, CUIRect *pDragBar, EDragSide Side, float *pValue, float MinValue = 100.0f, float MaxValue = 400.0f);
728
729 void UpdateHotEnvelopePoint(const CUIRect &View, const CEnvelope *pEnvelope, int ActiveChannels);
730
731 void RenderMenubar(CUIRect Menubar);
732 void ShowHelp();
733
734 void DoAudioPreview(CUIRect View, const void *pPlayPauseButtonId, const void *pStopButtonId, const void *pSeekBarId, int SampleId);
735
736 // Zooming
737 void ZoomAdaptOffsetX(float ZoomFactor, const CUIRect &View);
738 void UpdateZoomEnvelopeX(const CUIRect &View);
739
740 void ZoomAdaptOffsetY(float ZoomFactor, const CUIRect &View);
741 void UpdateZoomEnvelopeY(const CUIRect &View);
742
743 void ResetZoomEnvelope(const std::shared_ptr<CEnvelope> &pEnvelope, int ActiveChannels);
744 void RemoveTimeOffsetEnvelope(const std::shared_ptr<CEnvelope> &pEnvelope);
745 float ScreenToEnvelopeX(const CUIRect &View, float x) const;
746 float EnvelopeToScreenX(const CUIRect &View, float x) const;
747 float ScreenToEnvelopeY(const CUIRect &View, float y) const;
748 float EnvelopeToScreenY(const CUIRect &View, float y) const;
749 float ScreenToEnvelopeDX(const CUIRect &View, float DeltaX);
750 float ScreenToEnvelopeDY(const CUIRect &View, float DeltaY);
751
752 // DDRace
753
754 IGraphics::CTextureHandle GetFrontTexture();
755 IGraphics::CTextureHandle GetTeleTexture();
756 IGraphics::CTextureHandle GetSpeedupTexture();
757 IGraphics::CTextureHandle GetSwitchTexture();
758 IGraphics::CTextureHandle GetTuneTexture();
759
760 unsigned char m_TeleNumber;
761 unsigned char m_TeleCheckpointNumber;
762 unsigned char m_ViewTeleNumber;
763
764 unsigned char m_TuningNumber;
765 unsigned char m_ViewTuning;
766
767 unsigned char m_SpeedupForce;
768 unsigned char m_SpeedupMaxSpeed;
769 short m_SpeedupAngle;
770
771 unsigned char m_SwitchNumber;
772 unsigned char m_SwitchDelay;
773 unsigned char m_ViewSwitch;
774
775 void AdjustBrushSpecialTiles(bool UseNextFree, int Adjust = 0);
776
777private:
778 CEditorMap m_Map;
779
780 CEditorHistory &ActiveHistory();
781
782 std::map<int, CPoint[5]> m_QuadDragOriginalPoints;
783};
784
785#endif
786