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