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