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 "quadart.h"
14#include "smooth_value.h"
15
16#include <base/bezier.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/file_browser.h>
25#include <game/editor/mapitems/envelope.h>
26#include <game/editor/mapitems/layer.h>
27#include <game/editor/mapitems/layer_front.h>
28#include <game/editor/mapitems/layer_game.h>
29#include <game/editor/mapitems/layer_group.h>
30#include <game/editor/mapitems/layer_quads.h>
31#include <game/editor/mapitems/layer_sounds.h>
32#include <game/editor/mapitems/layer_speedup.h>
33#include <game/editor/mapitems/layer_switch.h>
34#include <game/editor/mapitems/layer_tele.h>
35#include <game/editor/mapitems/layer_tiles.h>
36#include <game/editor/mapitems/layer_tune.h>
37#include <game/editor/mapitems/map.h>
38#include <game/editor/prompt.h>
39#include <game/editor/quick_action.h>
40#include <game/map/render_interfaces.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, public IEnvelopeEval
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 CLayerSelector m_LayerSelector;
124 CFileBrowser m_FileBrowser;
125 CPrompt m_Prompt;
126 CFontTyper m_FontTyper;
127
128 bool m_EditorWasUsedBefore = false;
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 int GetTextureUsageFlag() const;
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 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#define REGISTER_QUICK_ACTION(name, text, callback, disabled, active, button_color, description) CQuickAction m_QuickAction##name;
187#include <game/editor/quick_actions.h>
188#undef REGISTER_QUICK_ACTION
189
190 CEditor() :
191#define REGISTER_QUICK_ACTION(name, text, callback, disabled, active, button_color, description) m_QuickAction##name(text, description, callback, disabled, active, button_color),
192#include <game/editor/quick_actions.h>
193#undef REGISTER_QUICK_ACTION
194 m_ZoomEnvelopeX(1.0f, 0.1f, 600.0f),
195 m_ZoomEnvelopeY(640.0f, 0.1f, 32000.0f),
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_ResetZoomEnvelope = true;
221 m_OffsetEnvelopeX = 0.1f;
222 m_OffsetEnvelopeY = 0.5f;
223
224 m_ShowMousePointer = true;
225
226 m_GuiActive = true;
227 m_PreviewZoom = false;
228
229 m_ShowTileInfo = SHOW_TILE_OFF;
230 m_ShowDetail = true;
231 m_Animate = false;
232 m_AnimateStart = 0;
233 m_AnimateTime = 0;
234 m_AnimateSpeed = 1;
235 m_AnimateUpdatePopup = false;
236
237 for(size_t i = 0; i < std::size(m_aSavedColors); ++i)
238 {
239 m_aSavedColors[i] = color_cast<ColorRGBA>(hsl: ColorHSLA(i / (float)std::size(m_aSavedColors), 1.0f, 0.5f));
240 }
241
242 m_CheckerTexture.Invalidate();
243 for(auto &CursorTexture : m_aCursorTextures)
244 CursorTexture.Invalidate();
245
246 m_CursorType = CURSOR_NORMAL;
247
248 // DDRace
249
250 m_TeleNumber = 1;
251 m_TeleCheckpointNumber = 1;
252 m_ViewTeleNumber = 0;
253
254 m_TuningNumber = 1;
255 m_ViewTuning = 0;
256
257 m_SwitchNumber = 1;
258 m_SwitchDelay = 0;
259 m_SpeedupForce = 50;
260 m_SpeedupMaxSpeed = 0;
261 m_SpeedupAngle = 0;
262 m_LargeLayerWasWarned = false;
263 m_PreventUnusedTilesWasWarned = false;
264 m_AllowPlaceUnusedTiles = EUnusedEntities::NOT_ALLOWED;
265 m_BrushDrawDestructive = true;
266 }
267
268 class CHoverTile
269 {
270 public:
271 CHoverTile(int Group, int Layer, int x, int y, const CTile Tile) :
272 m_Group(Group),
273 m_Layer(Layer),
274 m_X(x),
275 m_Y(y),
276 m_Tile(Tile)
277 {
278 }
279
280 int m_Group;
281 int m_Layer;
282 int m_X;
283 int m_Y;
284 const CTile m_Tile;
285 };
286 std::vector<CHoverTile> m_vHoverTiles;
287 const std::vector<CHoverTile> &HoverTiles() const { return m_vHoverTiles; }
288
289 void Init() override;
290 void OnUpdate() override;
291 void OnRender() override;
292 void OnActivate() override;
293 void OnWindowResize() override;
294 void OnClose() override;
295 void OnDialogClose();
296 bool HasUnsavedData() const override { return Map()->m_Modified; }
297 void UpdateMentions() override { m_Mentions++; }
298 void ResetMentions() override { m_Mentions = 0; }
299 void OnIngameMoved() override { m_IngameMoved = true; }
300 void ResetIngameMoved() override { m_IngameMoved = false; }
301
302 void HandleCursorMovement();
303 void OnMouseMove(vec2 MousePos);
304 void MouseAxisLock(vec2 &CursorRel);
305 vec2 m_MouseAxisInitialPos = vec2(0.0f, 0.0f);
306 enum class EAxisLock
307 {
308 START,
309 NONE,
310 HORIZONTAL,
311 VERTICAL,
312 } m_MouseAxisLockState = EAxisLock::START;
313
314 /**
315 * Global time when the autosave was last updated in the @link HandleAutosave @endlink function.
316 * This is used so that the autosave does not immediately activate when reopening the editor after
317 * a longer time of inactivity, as autosaves are only updated while the editor is open.
318 */
319 float m_LastAutosaveUpdateTime = -1.0f;
320 void HandleAutosave();
321 void HandleWriterFinishJobs();
322
323 // TODO: The name of the ShowFileDialogError function is not accurate anymore, this is used for generic error messages.
324 // Popups in UI should be shared_ptrs to make this even more generic.
325 class CStringKeyComparator
326 {
327 public:
328 bool operator()(const char *pLhs, const char *pRhs) const;
329 };
330 std::map<const char *, CUi::SMessagePopupContext *, CStringKeyComparator> m_PopupMessageContexts;
331 [[gnu::format(printf, 2, 3)]] void ShowFileDialogError(const char *pFormat, ...);
332
333 void Reset(bool CreateDefault = true);
334 bool Save(const char *pFilename) override;
335 bool Load(const char *pFilename, int StorageType) override;
336 bool HandleMapDrop(const char *pFilename, int StorageType) override;
337 void LoadCurrentMap();
338 void Render();
339
340 void RenderPressedKeys(CUIRect View);
341 void RenderSavingIndicator(CUIRect View);
342 void FreeDynamicPopupMenus();
343 void UpdateColorPipette();
344 void RenderMousePointer();
345 void RenderGameEntities(const std::shared_ptr<CLayerTiles> &pTiles);
346 void RenderSwitchEntities(const std::shared_ptr<CLayerTiles> &pTiles);
347
348 template<typename E>
349 SEditResult<E> DoPropertiesWithState(CUIRect *pToolbox, CProperty *pProps, int *pIds, int *pNewVal, const std::vector<ColorRGBA> &vColors = {});
350 int DoProperties(CUIRect *pToolbox, CProperty *pProps, int *pIds, int *pNewVal, const std::vector<ColorRGBA> &vColors = {});
351
352 CUi::SColorPickerPopupContext m_ColorPickerPopupContext;
353 const void *m_pColorPickerPopupActiveId = nullptr;
354 void DoColorPickerButton(const void *pId, const CUIRect *pRect, ColorRGBA Color, const std::function<void(ColorRGBA Color)> &SetColor);
355
356 int m_Mode;
357 int m_Dialog;
358 char m_aTooltip[256] = "";
359
360 bool m_BrushColorEnabled;
361
362 /**
363 * File which is pending to be loaded by @link POPEVENT_LOADDROP @endlink.
364 */
365 char m_aFilenamePendingLoad[IO_MAX_PATH_LENGTH] = "";
366
367 enum
368 {
369 POPEVENT_EXIT = 0,
370 POPEVENT_LOAD,
371 POPEVENT_LOADCURRENT,
372 POPEVENT_LOADDROP,
373 POPEVENT_NEW,
374 POPEVENT_LARGELAYER,
375 POPEVENT_PREVENTUNUSEDTILES,
376 POPEVENT_IMAGEDIV16,
377 POPEVENT_IMAGE_MAX,
378 POPEVENT_SOUND_MAX,
379 POPEVENT_PLACE_BORDER_TILES,
380 POPEVENT_TILEART_BIG_IMAGE,
381 POPEVENT_TILEART_MANY_COLORS,
382 POPEVENT_TILEART_TOO_MANY_COLORS,
383 POPEVENT_QUADART_BIG_IMAGE,
384 POPEVENT_REMOVE_USED_IMAGE,
385 POPEVENT_REMOVE_USED_SOUND,
386 POPEVENT_RESTART_SERVER,
387 POPEVENT_RESTARTING_SERVER,
388 };
389
390 int m_PopupEventType;
391 int m_PopupEventActivated;
392 int m_PopupEventWasActivated;
393 bool m_LargeLayerWasWarned;
394 bool m_PreventUnusedTilesWasWarned;
395
396 enum class EUnusedEntities
397 {
398 ALLOWED_IMPLICIT = -1,
399 NOT_ALLOWED = 0,
400 ALLOWED_EXPLICIT = 1,
401 };
402 EUnusedEntities m_AllowPlaceUnusedTiles;
403 bool IsAllowPlaceUnusedTiles() const;
404
405 bool m_BrushDrawDestructive;
406
407 int m_Mentions = 0;
408 bool m_IngameMoved = false;
409
410 int m_ToolbarPreviewSound;
411
412 std::vector<std::string> m_vSelectEntitiesFiles;
413 std::string m_SelectEntitiesImage;
414
415 // Zooming
416 CSmoothValue m_ZoomEnvelopeX;
417 CSmoothValue m_ZoomEnvelopeY;
418
419 bool m_ResetZoomEnvelope;
420
421 float m_OffsetEnvelopeX;
422 float m_OffsetEnvelopeY;
423
424 bool m_ShowMousePointer;
425 bool m_GuiActive;
426
427 bool m_PreviewZoom;
428 float m_MouseWorldScale = 1.0f; // Mouse (i.e. UI) scale relative to the World (selected Group)
429 vec2 m_MouseWorldPos = vec2(0.0f, 0.0f);
430 vec2 m_MouseWorldNoParaPos = vec2(0.0f, 0.0f);
431 vec2 m_MouseDeltaWorld = vec2(0.0f, 0.0f);
432 const void *m_pContainerPanned;
433 const void *m_pContainerPannedLast;
434 char m_MapEditorId; // UI element ID for the main map editor
435
436 enum EShowTile
437 {
438 SHOW_TILE_OFF,
439 SHOW_TILE_DECIMAL,
440 SHOW_TILE_HEXADECIMAL
441 };
442 EShowTile m_ShowTileInfo;
443 bool m_ShowDetail;
444
445 bool m_Animate;
446 int64_t m_AnimateStart;
447 float m_AnimateTime;
448 float m_AnimateSpeed;
449 bool m_AnimateUpdatePopup;
450
451 enum EExtraEditor
452 {
453 EXTRAEDITOR_NONE = -1,
454 EXTRAEDITOR_ENVELOPES,
455 EXTRAEDITOR_SERVER_SETTINGS,
456 EXTRAEDITOR_HISTORY,
457 NUM_EXTRAEDITORS,
458 };
459 EExtraEditor m_ActiveExtraEditor = EXTRAEDITOR_NONE;
460 float m_aExtraEditorSplits[NUM_EXTRAEDITORS] = {250.0f, 250.0f, 250.0f};
461 float m_ToolBoxWidth = 100.0f;
462
463 bool m_ShowEnvelopePreview = false;
464 enum class EEnvelopePreview
465 {
466 NONE,
467 SELECTED,
468 ALL,
469 };
470 EEnvelopePreview m_ActiveEnvelopePreview = EEnvelopePreview::NONE;
471 enum class EQuadEnvelopePointOperation
472 {
473 NONE = 0,
474 MOVE,
475 ROTATE,
476 };
477 EQuadEnvelopePointOperation m_QuadEnvelopePointOperation = EQuadEnvelopePointOperation::NONE;
478
479 bool m_ShowPicker;
480
481 // Color palette and pipette
482 ColorRGBA m_aSavedColors[8];
483 ColorRGBA m_PipetteColor = ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f);
484 bool m_ColorPipetteActive = false;
485
486 IGraphics::CTextureHandle m_CheckerTexture;
487
488 enum ECursorType
489 {
490 CURSOR_NORMAL,
491 CURSOR_RESIZE_V,
492 CURSOR_RESIZE_H,
493 NUM_CURSORS
494 };
495 IGraphics::CTextureHandle m_aCursorTextures[ECursorType::NUM_CURSORS];
496 ECursorType m_CursorType;
497
498 IGraphics::CTextureHandle GetEntitiesTexture();
499
500 std::shared_ptr<CLayerGroup> m_pBrush;
501 std::shared_ptr<CLayerTiles> m_pTilesetPicker;
502 std::shared_ptr<CLayerQuads> m_pQuadsetPicker;
503
504 const void *m_pUiGotContext = nullptr;
505
506 std::deque<std::shared_ptr<CDataFileWriterFinishJob>> m_WriterFinishJobs;
507
508 void EnvelopeEval(int TimeOffsetMillis, int EnvelopeIndex, ColorRGBA &Result, size_t Channels) override;
509
510 CLineInputBuffered<256> m_SettingsCommandInput;
511 CMapSettingsBackend m_MapSettingsBackend;
512 CMapSettingsBackend::CContext m_MapSettingsCommandContext;
513
514 CImageInfo m_TileartImageInfo;
515 void AddTileart(bool IgnoreHistory = false);
516 char m_aTileartFilename[IO_MAX_PATH_LENGTH];
517 void TileartCheckColors();
518
519 CImageInfo m_QuadArtImageInfo;
520 CQuadArtParameters m_QuadArtParameters;
521 void AddQuadArt(bool IgnoreHistory = false);
522
523 // editor_ui.cpp
524 void UpdateTooltip(const void *pId, const CUIRect *pRect, const char *pToolTip);
525 ColorRGBA GetButtonColor(const void *pId, int Checked);
526 int DoButtonLogic(const void *pId, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
527 int DoButton_Editor(const void *pId, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
528 int DoButton_Env(const void *pId, const char *pText, int Checked, const CUIRect *pRect, const char *pToolTip, ColorRGBA Color, int Corners);
529 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);
530 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);
531 int DoButton_MenuItem(const void *pId, const char *pText, int Checked, const CUIRect *pRect, int Flags = BUTTONFLAG_LEFT, const char *pToolTip = nullptr);
532 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);
533 bool DoEditBox(CLineInput *pLineInput, const CUIRect *pRect, float FontSize, int Corners = IGraphics::CORNER_ALL, const char *pToolTip = nullptr, const std::vector<STextColorSplit> &vColorSplits = {});
534 bool DoClearableEditBox(CLineInput *pLineInput, const CUIRect *pRect, float FontSize, int Corners = IGraphics::CORNER_ALL, const char *pToolTip = nullptr, const std::vector<STextColorSplit> &vColorSplits = {});
535 SEditResult<int> UiDoValueSelector(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);
536 void RenderBackground(CUIRect View, IGraphics::CTextureHandle Texture, float Size, float Brightness) const;
537
538 // editor_server_settings.cpp
539 void DoMapSettingsEditBox(CMapSettingsBackend::CContext *pContext, const CUIRect *pRect, float FontSize, float DropdownMaxHeight, int Corners = IGraphics::CORNER_ALL, const char *pToolTip = nullptr);
540 template<typename T>
541 int DoEditBoxDropdown(SEditBoxDropdownContext *pDropdown, CLineInput *pLineInput, const CUIRect *pEditBoxRect, int x, float MaxHeight, bool AutoWidth, const std::vector<T> &vData, const FDropdownRenderCallback<T> &pfnMatchCallback);
542 template<typename T>
543 int RenderEditBoxDropdown(SEditBoxDropdownContext *pDropdown, CUIRect View, CLineInput *pLineInput, int x, float MaxHeight, bool AutoWidth, const std::vector<T> &vData, const FDropdownRenderCallback<T> &pfnMatchCallback);
544
545 static CUi::EPopupMenuFunctionResult PopupMenuFile(void *pContext, CUIRect View, bool Active);
546 static CUi::EPopupMenuFunctionResult PopupMenuTools(void *pContext, CUIRect View, bool Active);
547 static CUi::EPopupMenuFunctionResult PopupMenuSettings(void *pContext, CUIRect View, bool Active);
548 static CUi::EPopupMenuFunctionResult PopupGroup(void *pContext, CUIRect View, bool Active);
549 struct SLayerPopupContext : public SPopupMenuId
550 {
551 CEditor *m_pEditor;
552 std::vector<std::shared_ptr<CLayerTiles>> m_vpLayers;
553 std::vector<int> m_vLayerIndices;
554 CLayerTiles::SCommonPropState m_CommonPropState;
555 };
556 static CUi::EPopupMenuFunctionResult PopupLayer(void *pContext, CUIRect View, bool Active);
557 class CQuadPopupContext : public SPopupMenuId
558 {
559 public:
560 CEditor *m_pEditor;
561 int m_SelectedQuadIndex;
562 int m_Color;
563 };
564 CQuadPopupContext m_QuadPopupContext;
565 static CUi::EPopupMenuFunctionResult PopupQuad(void *pContext, CUIRect View, bool Active);
566 static CUi::EPopupMenuFunctionResult PopupSource(void *pContext, CUIRect View, bool Active);
567 class CPointPopupContext : public SPopupMenuId
568 {
569 public:
570 CEditor *m_pEditor;
571 int m_SelectedQuadPoint;
572 int m_SelectedQuadIndex;
573 };
574 CPointPopupContext m_PointPopupContext;
575 static CUi::EPopupMenuFunctionResult PopupPoint(void *pContext, CUIRect View, bool Active);
576 static CUi::EPopupMenuFunctionResult PopupEnvPoint(void *pContext, CUIRect View, bool Active);
577 static CUi::EPopupMenuFunctionResult PopupEnvPointMulti(void *pContext, CUIRect View, bool Active);
578 static CUi::EPopupMenuFunctionResult PopupEnvPointCurveType(void *pContext, CUIRect View, bool Active);
579 static CUi::EPopupMenuFunctionResult PopupImage(void *pContext, CUIRect View, bool Active);
580 static CUi::EPopupMenuFunctionResult PopupSound(void *pContext, CUIRect View, bool Active);
581 static CUi::EPopupMenuFunctionResult PopupMapInfo(void *pContext, CUIRect View, bool Active);
582 static CUi::EPopupMenuFunctionResult PopupEvent(void *pContext, CUIRect View, bool Active);
583 static CUi::EPopupMenuFunctionResult PopupSelectImage(void *pContext, CUIRect View, bool Active);
584 static CUi::EPopupMenuFunctionResult PopupSelectSound(void *pContext, CUIRect View, bool Active);
585 static CUi::EPopupMenuFunctionResult PopupSelectGametileOp(void *pContext, CUIRect View, bool Active);
586 static CUi::EPopupMenuFunctionResult PopupSelectConfigAutoMap(void *pContext, CUIRect View, bool Active);
587 static CUi::EPopupMenuFunctionResult PopupSelectAutoMapReference(void *pContext, CUIRect View, bool Active);
588 static CUi::EPopupMenuFunctionResult PopupTele(void *pContext, CUIRect View, bool Active);
589 static CUi::EPopupMenuFunctionResult PopupSpeedup(void *pContext, CUIRect View, bool Active);
590 static CUi::EPopupMenuFunctionResult PopupSwitch(void *pContext, CUIRect View, bool Active);
591 static CUi::EPopupMenuFunctionResult PopupTune(void *pContext, CUIRect View, bool Active);
592 static CUi::EPopupMenuFunctionResult PopupGoto(void *pContext, CUIRect View, bool Active);
593 static CUi::EPopupMenuFunctionResult PopupEntities(void *pContext, CUIRect View, bool Active);
594 static CUi::EPopupMenuFunctionResult PopupProofMode(void *pContext, CUIRect View, bool Active);
595 static CUi::EPopupMenuFunctionResult PopupAnimateSettings(void *pContext, CUIRect View, bool Active);
596 int m_PopupEnvelopeSelectedPoint = -1;
597 static CUi::EPopupMenuFunctionResult PopupEnvelopeCurvetype(void *pContext, CUIRect View, bool Active);
598 static CUi::EPopupMenuFunctionResult PopupQuadArt(void *pContext, CUIRect View, bool Active);
599
600 static bool CallbackOpenMap(const char *pFilename, int StorageType, void *pUser);
601 static bool CallbackAppendMap(const char *pFilename, int StorageType, void *pUser);
602 static bool CallbackSaveMap(const char *pFilename, int StorageType, void *pUser);
603 static bool CallbackSaveCopyMap(const char *pFilename, int StorageType, void *pUser);
604 static bool CallbackAddTileart(const char *pFilepath, int StorageType, void *pUser);
605 static bool CallbackAddQuadArt(const char *pFilepath, int StorageType, void *pUser);
606 static bool CallbackSaveImage(const char *pFilename, int StorageType, void *pUser);
607 static bool CallbackSaveSound(const char *pFilename, int StorageType, void *pUser);
608 static bool CallbackCustomEntities(const char *pFilename, int StorageType, void *pUser);
609
610 void PopupSelectImageInvoke(int Current, float x, float y);
611 int PopupSelectImageResult();
612
613 void PopupSelectGametileOpInvoke(float x, float y);
614 int PopupSelectGameTileOpResult();
615
616 void PopupSelectConfigAutoMapInvoke(int Current, float x, float y);
617 int PopupSelectConfigAutoMapResult();
618
619 void PopupSelectSoundInvoke(int Current, float x, float y);
620 int PopupSelectSoundResult();
621
622 void PopupSelectAutoMapReferenceInvoke(int Current, float x, float y);
623 int PopupSelectAutoMapReferenceResult();
624
625 void DoQuadEnvelopes(const CLayerQuads *pLayerQuads);
626 void DoQuadEnvPoint(const CQuad *pQuad, CEnvelope *pEnvelope, int QuadIndex, int PointIndex);
627 void DoQuadPoint(int LayerIndex, const std::shared_ptr<CLayerQuads> &pLayer, CQuad *pQuad, int QuadIndex, int v);
628 void UpdateHotQuadPoint(const CLayerQuads *pLayer);
629
630 float TriangleArea(vec2 A, vec2 B, vec2 C);
631 bool IsInTriangle(vec2 Point, vec2 A, vec2 B, vec2 C);
632 void DoQuadKnife(int QuadIndex);
633
634 void DoSoundSource(int LayerIndex, CSoundSource *pSource, int Index);
635 void UpdateHotSoundSource(const CLayerSounds *pLayer);
636
637 enum class EAxis
638 {
639 NONE = 0,
640 X,
641 Y,
642 };
643 struct SAxisAlignedBoundingBox
644 {
645 enum
646 {
647 POINT_TL = 0,
648 POINT_TR,
649 POINT_BL,
650 POINT_BR,
651 POINT_CENTER,
652 NUM_POINTS
653 };
654 CPoint m_aPoints[NUM_POINTS];
655 };
656 void DoMapEditor(CUIRect View);
657 void DoToolbarLayers(CUIRect Toolbar);
658 void DoToolbarImages(CUIRect Toolbar);
659 void DoToolbarSounds(CUIRect Toolbar);
660 void DoQuad(int LayerIndex, const std::shared_ptr<CLayerQuads> &pLayer, CQuad *pQuad, int Index);
661 void PreparePointDrag(const CQuad *pQuad, int QuadIndex, int PointIndex);
662 void DoPointDrag(CQuad *pQuad, int QuadIndex, int PointIndex, ivec2 Offset);
663 EAxis GetDragAxis(ivec2 Offset) const;
664 void DrawAxis(EAxis Axis, CPoint &OriginalPoint, CPoint &Point) const;
665 void DrawAABB(const SAxisAlignedBoundingBox &AABB, ivec2 Offset) const;
666
667 // Alignment methods
668 // These methods take `OffsetX` and `OffsetY` because the calculations are made with the original positions
669 // of the quad(s), before we started dragging. This allows us to edit `OffsetX` and `OffsetY` based on the previously
670 // calculated alignments.
671 struct SAlignmentInfo
672 {
673 CPoint m_AlignedPoint; // The "aligned" point, which we want to align/snap to
674 union
675 {
676 // 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
677 // we aligned the Y values (X axis aligned => Y values are the same, Y axis aligned => X values are the same).
678 int m_X;
679 int m_Y;
680 };
681 EAxis m_Axis; // The axis we are aligning on
682 int m_PointIndex; // The point index we are aligning
683 int m_Diff; // Store the difference
684 };
685 void ComputePointAlignments(const std::shared_ptr<CLayerQuads> &pLayer, CQuad *pQuad, int QuadIndex, int PointIndex, ivec2 Offset, std::vector<SAlignmentInfo> &vAlignments, bool Append = false) const;
686 void ComputePointsAlignments(const std::shared_ptr<CLayerQuads> &pLayer, bool Pivot, ivec2 Offset, std::vector<SAlignmentInfo> &vAlignments) const;
687 void ComputeAABBAlignments(const std::shared_ptr<CLayerQuads> &pLayer, const SAxisAlignedBoundingBox &AABB, ivec2 Offset, std::vector<SAlignmentInfo> &vAlignments) const;
688 void DrawPointAlignments(const std::vector<SAlignmentInfo> &vAlignments, ivec2 Offset) const;
689 void QuadSelectionAABB(const std::shared_ptr<CLayerQuads> &pLayer, SAxisAlignedBoundingBox &OutAABB);
690 void ApplyAlignments(const std::vector<SAlignmentInfo> &vAlignments, ivec2 &Offset);
691 void ApplyAxisAlignment(ivec2 &Offset) const;
692
693 bool ReplaceImage(const char *pFilename, int StorageType, bool CheckDuplicate);
694 static bool ReplaceImageCallback(const char *pFilename, int StorageType, void *pUser);
695 bool ReplaceSound(const char *pFilename, int StorageType, bool CheckDuplicate);
696 static bool ReplaceSoundCallback(const char *pFilename, int StorageType, void *pUser);
697 static bool AddImage(const char *pFilename, int StorageType, void *pUser);
698 static bool AddSound(const char *pFilename, int StorageType, void *pUser);
699
700 static bool IsVanillaImage(const char *pImage);
701
702 void RenderLayers(CUIRect LayersBox);
703 void RenderImagesList(CUIRect Toolbox);
704 void RenderSelectedImage(CUIRect View) const;
705 void RenderSounds(CUIRect Toolbox);
706 void RenderModebar(CUIRect View);
707 void RenderStatusbar(CUIRect View, CUIRect *pTooltipRect);
708 void RenderTooltip(CUIRect TooltipRect);
709
710 void RenderEnvelopeEditor(CUIRect View);
711 void RenderEnvelopeEditorColorBar(CUIRect ColorBar, const std::shared_ptr<CEnvelope> &pEnvelope);
712
713 void RenderMapSettingsErrorDialog();
714 void RenderServerSettingsEditor(CUIRect View, bool ShowServerSettingsEditorLast);
715 static void MapSettingsDropdownRenderCallback(const SPossibleValueMatch &Match, char (&aOutput)[128], std::vector<STextColorSplit> &vColorSplits);
716
717 void RenderEditorHistory(CUIRect View);
718
719 enum class EDragSide // Which side is the drag bar on
720 {
721 BOTTOM,
722 LEFT,
723 TOP,
724 RIGHT,
725 };
726 void DoEditorDragBar(CUIRect View, CUIRect *pDragBar, EDragSide Side, float *pValue, float MinValue = 100.0f, float MaxValue = 400.0f);
727
728 void UpdateHotEnvelopePoint(const CUIRect &View, const CEnvelope *pEnvelope, int ActiveChannels);
729
730 void RenderMenubar(CUIRect Menubar);
731
732 void DoAudioPreview(CUIRect View, const void *pPlayPauseButtonId, const void *pStopButtonId, const void *pSeekBarId, int SampleId);
733
734 // Zooming
735 void ZoomAdaptOffsetX(float ZoomFactor, const CUIRect &View);
736 void UpdateZoomEnvelopeX(const CUIRect &View);
737
738 void ZoomAdaptOffsetY(float ZoomFactor, const CUIRect &View);
739 void UpdateZoomEnvelopeY(const CUIRect &View);
740
741 void ResetZoomEnvelope(const std::shared_ptr<CEnvelope> &pEnvelope, int ActiveChannels);
742 void RemoveTimeOffsetEnvelope(const std::shared_ptr<CEnvelope> &pEnvelope);
743 float ScreenToEnvelopeX(const CUIRect &View, float x) const;
744 float EnvelopeToScreenX(const CUIRect &View, float x) const;
745 float ScreenToEnvelopeY(const CUIRect &View, float y) const;
746 float EnvelopeToScreenY(const CUIRect &View, float y) const;
747 float ScreenToEnvelopeDX(const CUIRect &View, float DeltaX);
748 float ScreenToEnvelopeDY(const CUIRect &View, float DeltaY);
749
750 // DDRace
751
752 IGraphics::CTextureHandle GetFrontTexture();
753 IGraphics::CTextureHandle GetTeleTexture();
754 IGraphics::CTextureHandle GetSpeedupTexture();
755 IGraphics::CTextureHandle GetSwitchTexture();
756 IGraphics::CTextureHandle GetTuneTexture();
757
758 unsigned char m_TeleNumber;
759 unsigned char m_TeleCheckpointNumber;
760 unsigned char m_ViewTeleNumber;
761
762 unsigned char m_TuningNumber;
763 unsigned char m_ViewTuning;
764
765 unsigned char m_SpeedupForce;
766 unsigned char m_SpeedupMaxSpeed;
767 short m_SpeedupAngle;
768
769 unsigned char m_SwitchNumber;
770 unsigned char m_SwitchDelay;
771 unsigned char m_ViewSwitch;
772
773 void AdjustBrushSpecialTiles(bool UseNextFree, int Adjust = 0);
774
775private:
776 CEditorMap m_Map;
777
778 CEditorHistory &ActiveHistory();
779
780 std::map<int, CPoint[5]> m_QuadDragOriginalPoints;
781};
782
783#endif
784