added auto mapping feature by LordSkelethom
[twcon.git] / src / game / editor / editor.h
blob353b0774314fef3bff6eec065f8399deb540cda5
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
6 #include <math.h>
8 #include <base/math.h>
9 #include <base/system.h>
11 #include <base/tl/algorithm.h>
12 #include <base/tl/array.h>
13 #include <base/tl/sorted_array.h>
14 #include <base/tl/string.h>
16 #include <game/client/ui.h>
17 #include <game/mapitems.h>
18 #include <game/client/render.h>
20 #include <engine/shared/config.h>
21 #include <engine/shared/datafile.h>
22 #include <engine/editor.h>
23 #include <engine/graphics.h>
25 #include "auto_map.h"
27 typedef void (*INDEX_MODIFY_FUNC)(int *pIndex);
29 //CRenderTools m_RenderTools;
31 // CEditor SPECIFIC
32 enum
34 MODE_LAYERS=0,
35 MODE_IMAGES,
37 DIALOG_NONE=0,
38 DIALOG_FILE,
41 struct CEntity
43 CPoint m_Position;
44 int m_Type;
47 class CEnvelope
49 public:
50 int m_Channels;
51 array<CEnvPoint> m_lPoints;
52 char m_aName[32];
53 float m_Bottom, m_Top;
55 CEnvelope(int Chan)
57 m_Channels = Chan;
58 m_aName[0] = 0;
59 m_Bottom = 0;
60 m_Top = 0;
63 void Resort()
65 sort(m_lPoints.all());
66 FindTopBottom(0xf);
69 void FindTopBottom(int ChannelMask)
71 m_Top = -1000000000.0f;
72 m_Bottom = 1000000000.0f;
73 for(int i = 0; i < m_lPoints.size(); i++)
75 for(int c = 0; c < m_Channels; c++)
77 if(ChannelMask&(1<<c))
79 float v = fx2f(m_lPoints[i].m_aValues[c]);
80 if(v > m_Top) m_Top = v;
81 if(v < m_Bottom) m_Bottom = v;
87 int Eval(float Time, float *pResult)
89 CRenderTools::RenderEvalEnvelope(m_lPoints.base_ptr(), m_lPoints.size(), m_Channels, Time, pResult);
90 return m_Channels;
93 void AddPoint(int Time, int v0, int v1=0, int v2=0, int v3=0)
95 CEnvPoint p;
96 p.m_Time = Time;
97 p.m_aValues[0] = v0;
98 p.m_aValues[1] = v1;
99 p.m_aValues[2] = v2;
100 p.m_aValues[3] = v3;
101 p.m_Curvetype = CURVETYPE_LINEAR;
102 m_lPoints.add(p);
103 Resort();
106 float EndTime()
108 if(m_lPoints.size())
109 return m_lPoints[m_lPoints.size()-1].m_Time*(1.0f/1000.0f);
110 return 0;
115 class CLayer;
116 class CLayerGroup;
117 class CEditorMap;
119 class CLayer
121 public:
122 class CEditor *m_pEditor;
123 class IGraphics *Graphics();
124 class ITextRender *TextRender();
126 CLayer()
128 m_Type = LAYERTYPE_INVALID;
129 m_pTypeName = "(invalid)";
130 m_Visible = true;
131 m_Readonly = false;
132 m_SaveToMap = true;
133 m_Flags = 0;
134 m_pEditor = 0;
137 virtual ~CLayer()
142 virtual void BrushSelecting(CUIRect Rect) {}
143 virtual int BrushGrab(CLayerGroup *pBrush, CUIRect Rect) { return 0; }
144 virtual void FillSelection(bool Empty, CLayer *pBrush, CUIRect Rect) {}
145 virtual void BrushDraw(CLayer *pBrush, float x, float y) {}
146 virtual void BrushPlace(CLayer *pBrush, float x, float y) {}
147 virtual void BrushFlipX() {}
148 virtual void BrushFlipY() {}
149 virtual void BrushRotate(float Amount) {}
151 virtual void Render() {}
152 virtual int RenderProperties(CUIRect *pToolbox) { return 0; }
154 virtual void ModifyImageIndex(INDEX_MODIFY_FUNC pfnFunc) {}
155 virtual void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC pfnFunc) {}
157 virtual void GetSize(float *w, float *h) { *w = 0; *h = 0;}
159 const char *m_pTypeName;
160 int m_Type;
161 int m_Flags;
163 bool m_Readonly;
164 bool m_Visible;
165 bool m_SaveToMap;
168 class CLayerGroup
170 public:
171 class CEditorMap *m_pMap;
173 array<CLayer*> m_lLayers;
175 int m_OffsetX;
176 int m_OffsetY;
178 int m_ParallaxX;
179 int m_ParallaxY;
181 int m_UseClipping;
182 int m_ClipX;
183 int m_ClipY;
184 int m_ClipW;
185 int m_ClipH;
187 const char *m_pName;
188 bool m_GameGroup;
189 bool m_Visible;
190 bool m_SaveToMap;
192 CLayerGroup();
193 ~CLayerGroup();
195 void Convert(CUIRect *pRect);
196 void Render();
197 void MapScreen();
198 void Mapping(float *pPoints);
200 void GetSize(float *w, float *h);
202 void DeleteLayer(int Index);
203 int SwapLayers(int Index0, int Index1);
205 bool IsEmpty() const
207 return m_lLayers.size() == 0;
210 void Clear()
212 m_lLayers.delete_all();
215 void AddLayer(CLayer *l);
217 void ModifyImageIndex(INDEX_MODIFY_FUNC Func)
219 for(int i = 0; i < m_lLayers.size(); i++)
220 m_lLayers[i]->ModifyImageIndex(Func);
223 void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC Func)
225 for(int i = 0; i < m_lLayers.size(); i++)
226 m_lLayers[i]->ModifyEnvelopeIndex(Func);
230 class CEditorImage : public CImageInfo
232 public:
233 CEditor *m_pEditor;
235 CEditorImage(CEditor *pEditor)
236 : m_AutoMapper(pEditor)
238 m_pEditor = pEditor;
239 m_TexID = -1;
240 m_aName[0] = 0;
241 m_External = 0;
242 m_Width = 0;
243 m_Height = 0;
244 m_pData = 0;
245 m_Format = 0;
248 ~CEditorImage();
250 void AnalyseTileFlags();
252 int m_TexID;
253 int m_External;
254 char m_aName[128];
255 unsigned char m_aTileFlags[256];
256 class CAutoMapper m_AutoMapper;
259 class CEditorMap
261 void MakeGameGroup(CLayerGroup *pGroup);
262 void MakeGameLayer(CLayer *pLayer);
263 public:
264 CEditor *m_pEditor;
265 bool m_Modified;
267 CEditorMap()
269 Clean();
272 array<CLayerGroup*> m_lGroups;
273 array<CEditorImage*> m_lImages;
274 array<CEnvelope*> m_lEnvelopes;
276 class CLayerGame *m_pGameLayer;
277 CLayerGroup *m_pGameGroup;
279 CEnvelope *NewEnvelope(int Channels)
281 m_Modified = true;
282 CEnvelope *e = new CEnvelope(Channels);
283 m_lEnvelopes.add(e);
284 return e;
287 void DeleteEnvelope(int Index);
289 CLayerGroup *NewGroup()
291 m_Modified = true;
292 CLayerGroup *g = new CLayerGroup;
293 g->m_pMap = this;
294 m_lGroups.add(g);
295 return g;
298 int SwapGroups(int Index0, int Index1)
300 if(Index0 < 0 || Index0 >= m_lGroups.size()) return Index0;
301 if(Index1 < 0 || Index1 >= m_lGroups.size()) return Index0;
302 if(Index0 == Index1) return Index0;
303 m_Modified = true;
304 swap(m_lGroups[Index0], m_lGroups[Index1]);
305 return Index1;
308 void DeleteGroup(int Index)
310 if(Index < 0 || Index >= m_lGroups.size()) return;
311 m_Modified = true;
312 delete m_lGroups[Index];
313 m_lGroups.remove_index(Index);
316 void ModifyImageIndex(INDEX_MODIFY_FUNC pfnFunc)
318 m_Modified = true;
319 for(int i = 0; i < m_lGroups.size(); i++)
320 m_lGroups[i]->ModifyImageIndex(pfnFunc);
323 void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC pfnFunc)
325 m_Modified = true;
326 for(int i = 0; i < m_lGroups.size(); i++)
327 m_lGroups[i]->ModifyEnvelopeIndex(pfnFunc);
330 void Clean();
331 void CreateDefault(int EntitiesTexture);
333 // io
334 int Save(class IStorage *pStorage, const char *pFilename);
335 int Load(class IStorage *pStorage, const char *pFilename, int StorageType);
339 struct CProperty
341 const char *m_pName;
342 int m_Value;
343 int m_Type;
344 int m_Min;
345 int m_Max;
348 enum
350 PROPTYPE_NULL=0,
351 PROPTYPE_BOOL,
352 PROPTYPE_INT_STEP,
353 PROPTYPE_INT_SCROLL,
354 PROPTYPE_COLOR,
355 PROPTYPE_IMAGE,
356 PROPTYPE_ENVELOPE,
357 PROPTYPE_SHIFT,
360 typedef struct
362 int x, y;
363 int w, h;
364 } RECTi;
366 class CLayerTiles : public CLayer
368 public:
369 CLayerTiles(int w, int h);
370 ~CLayerTiles();
372 void Resize(int NewW, int NewH);
373 void Shift(int Direction);
375 void MakePalette();
376 virtual void Render();
378 int ConvertX(float x) const;
379 int ConvertY(float y) const;
380 void Convert(CUIRect Rect, RECTi *pOut);
381 void Snap(CUIRect *pRect);
382 void Clamp(RECTi *pRect);
384 virtual void BrushSelecting(CUIRect Rect);
385 virtual int BrushGrab(CLayerGroup *pBrush, CUIRect Rect);
386 virtual void FillSelection(bool Empty, CLayer *pBrush, CUIRect Rect);
387 virtual void BrushDraw(CLayer *pBrush, float wx, float wy);
388 virtual void BrushFlipX();
389 virtual void BrushFlipY();
390 virtual void BrushRotate(float Amount);
392 virtual void ShowInfo();
393 virtual int RenderProperties(CUIRect *pToolbox);
395 virtual void ModifyImageIndex(INDEX_MODIFY_FUNC pfnFunc);
396 virtual void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC pfnFunc);
398 void PrepareForSave();
400 void GetSize(float *w, float *h) { *w = m_Width*32.0f; *h = m_Height*32.0f; }
402 int m_TexID;
403 int m_Game;
404 int m_Image;
405 int m_Width;
406 int m_Height;
407 CColor m_Color;
408 CTile *m_pTiles;
411 class CLayerQuads : public CLayer
413 public:
414 CLayerQuads();
415 ~CLayerQuads();
417 virtual void Render();
418 CQuad *NewQuad();
420 virtual void BrushSelecting(CUIRect Rect);
421 virtual int BrushGrab(CLayerGroup *pBrush, CUIRect Rect);
422 virtual void BrushPlace(CLayer *pBrush, float wx, float wy);
423 virtual void BrushFlipX();
424 virtual void BrushFlipY();
425 virtual void BrushRotate(float Amount);
427 virtual int RenderProperties(CUIRect *pToolbox);
429 virtual void ModifyImageIndex(INDEX_MODIFY_FUNC pfnFunc);
430 virtual void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC pfnFunc);
432 void GetSize(float *w, float *h);
434 int m_Image;
435 array<CQuad> m_lQuads;
438 class CLayerGame : public CLayerTiles
440 public:
441 CLayerGame(int w, int h);
442 ~CLayerGame();
444 virtual int RenderProperties(CUIRect *pToolbox);
447 class CEditor : public IEditor
449 class IInput *m_pInput;
450 class IClient *m_pClient;
451 class IConsole *m_pConsole;
452 class IGraphics *m_pGraphics;
453 class ITextRender *m_pTextRender;
454 class IStorage *m_pStorage;
455 CRenderTools m_RenderTools;
456 CUI m_UI;
457 public:
458 class IInput *Input() { return m_pInput; };
459 class IClient *Client() { return m_pClient; };
460 class IConsole *Console() { return m_pConsole; };
461 class IGraphics *Graphics() { return m_pGraphics; };
462 class ITextRender *TextRender() { return m_pTextRender; };
463 class IStorage *Storage() { return m_pStorage; };
464 CUI *UI() { return &m_UI; }
465 CRenderTools *RenderTools() { return &m_RenderTools; }
467 CEditor() : m_TilesetPicker(16, 16)
469 m_pInput = 0;
470 m_pClient = 0;
471 m_pGraphics = 0;
472 m_pTextRender = 0;
474 m_Mode = MODE_LAYERS;
475 m_Dialog = 0;
476 m_pTooltip = 0;
478 m_aFileName[0] = 0;
479 m_aFileSaveName[0] = 0;
480 m_ValidSaveFilename = false;
482 m_PopupEventActivated = false;
484 m_FileDialogStorageType = 0;
485 m_pFileDialogTitle = 0;
486 m_pFileDialogButtonText = 0;
487 m_pFileDialogUser = 0;
488 m_aFileDialogFileName[0] = 0;
489 m_aFileDialogCurrentFolder[0] = 0;
490 m_aFileDialogCurrentLink[0] = 0;
491 m_pFileDialogPath = m_aFileDialogCurrentFolder;
492 m_aFileDialogActivate = false;
493 m_FileDialogScrollValue = 0.0f;
494 m_FilesSelectedIndex = -1;
495 m_FilesStartAt = 0;
496 m_FilesCur = 0;
497 m_FilesStopAt = 999;
499 m_WorldOffsetX = 0;
500 m_WorldOffsetY = 0;
501 m_EditorOffsetX = 0.0f;
502 m_EditorOffsetY = 0.0f;
504 m_WorldZoom = 1.0f;
505 m_ZoomLevel = 200;
506 m_LockMouse = false;
507 m_ShowMousePointer = true;
508 m_MouseDeltaX = 0;
509 m_MouseDeltaY = 0;
510 m_MouseDeltaWx = 0;
511 m_MouseDeltaWy = 0;
513 m_GuiActive = true;
514 m_ProofBorders = false;
516 m_ShowTileInfo = false;
517 m_ShowDetail = true;
518 m_Animate = false;
519 m_AnimateStart = 0;
520 m_AnimateTime = 0;
521 m_AnimateSpeed = 1;
523 m_ShowEnvelopeEditor = 0;
525 ms_CheckerTexture = 0;
526 ms_BackgroundTexture = 0;
527 ms_CursorTexture = 0;
528 ms_EntitiesTexture = 0;
530 ms_pUiGotContext = 0;
533 virtual void Init();
534 virtual void UpdateAndRender();
535 virtual bool HasUnsavedData() { return m_Map.m_Modified; }
537 void FilelistPopulate(int StorageType);
538 void InvokeFileDialog(int StorageType, int FileType, const char *pTitle, const char *pButtonText,
539 const char *pBasepath, const char *pDefaultName,
540 void (*pfnFunc)(const char *pFilename, int StorageType, void *pUser), void *pUser);
542 void Reset(bool CreateDefault=true);
543 int Save(const char *pFilename);
544 int Load(const char *pFilename, int StorageType);
545 int Append(const char *pFilename, int StorageType);
546 void Render();
548 CQuad *GetSelectedQuad();
549 CLayer *GetSelectedLayerType(int Index, int Type);
550 CLayer *GetSelectedLayer(int Index);
551 CLayerGroup *GetSelectedGroup();
553 int DoProperties(CUIRect *pToolbox, CProperty *pProps, int *pIDs, int *pNewVal);
555 int m_Mode;
556 int m_Dialog;
557 const char *m_pTooltip;
559 char m_aFileName[512];
560 char m_aFileSaveName[512];
561 bool m_ValidSaveFilename;
563 enum
565 POPEVENT_EXIT=0,
566 POPEVENT_LOAD,
567 POPEVENT_NEW,
568 POPEVENT_SAVE,
571 int m_PopupEventType;
572 int m_PopupEventActivated;
574 enum
576 FILETYPE_MAP,
577 FILETYPE_IMG,
579 MAX_PATH_LENGTH = 512
582 int m_FileDialogStorageType;
583 const char *m_pFileDialogTitle;
584 const char *m_pFileDialogButtonText;
585 void (*m_pfnFileDialogFunc)(const char *pFileName, int StorageType, void *pUser);
586 void *m_pFileDialogUser;
587 char m_aFileDialogFileName[MAX_PATH_LENGTH];
588 char m_aFileDialogCurrentFolder[MAX_PATH_LENGTH];
589 char m_aFileDialogCurrentLink[MAX_PATH_LENGTH];
590 char *m_pFileDialogPath;
591 bool m_aFileDialogActivate;
592 int m_FileDialogFileType;
593 float m_FileDialogScrollValue;
594 int m_FilesSelectedIndex;
595 char m_FileDialogNewFolderName[64];
596 char m_FileDialogErrString[64];
598 struct CFilelistItem
600 char m_aFilename[128];
601 char m_aName[128];
602 bool m_IsDir;
603 bool m_IsLink;
604 int m_StorageType;
606 bool operator<(const CFilelistItem &Other) { return !str_comp(m_aFilename, "..") ? true : !str_comp(Other.m_aFilename, "..") ? false :
607 m_IsDir && !Other.m_IsDir ? true : !m_IsDir && Other.m_IsDir ? false :
608 str_comp_filenames(m_aFilename, Other.m_aFilename) < 0; }
610 sorted_array<CFilelistItem> m_FileList;
611 int m_FilesStartAt;
612 int m_FilesCur;
613 int m_FilesStopAt;
615 float m_WorldOffsetX;
616 float m_WorldOffsetY;
617 float m_EditorOffsetX;
618 float m_EditorOffsetY;
619 float m_WorldZoom;
620 int m_ZoomLevel;
621 bool m_LockMouse;
622 bool m_ShowMousePointer;
623 bool m_GuiActive;
624 bool m_ProofBorders;
625 float m_MouseDeltaX;
626 float m_MouseDeltaY;
627 float m_MouseDeltaWx;
628 float m_MouseDeltaWy;
630 bool m_ShowTileInfo;
631 bool m_ShowDetail;
632 bool m_Animate;
633 int64 m_AnimateStart;
634 float m_AnimateTime;
635 float m_AnimateSpeed;
637 int m_ShowEnvelopeEditor;
638 bool m_ShowPicker;
640 int m_SelectedLayer;
641 int m_SelectedGroup;
642 int m_SelectedQuad;
643 int m_SelectedPoints;
644 int m_SelectedEnvelope;
645 int m_SelectedImage;
647 static int ms_CheckerTexture;
648 static int ms_BackgroundTexture;
649 static int ms_CursorTexture;
650 static int ms_EntitiesTexture;
652 CLayerGroup m_Brush;
653 CLayerTiles m_TilesetPicker;
655 static const void *ms_pUiGotContext;
657 CEditorMap m_Map;
659 void DoMapBorder();
660 int DoButton_Editor_Common(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
661 int DoButton_Editor(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
663 int DoButton_Tab(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
664 int DoButton_Ex(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip, int Corners);
665 int DoButton_ButtonDec(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
666 int DoButton_ButtonInc(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
668 int DoButton_File(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
670 int DoButton_Menu(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
671 int DoButton_MenuItem(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags=0, const char *pToolTip=0);
673 int DoEditBox(void *pID, const CUIRect *pRect, char *pStr, unsigned StrSize, float FontSize, bool Hidden=false);
675 void RenderBackground(CUIRect View, int Texture, float Size, float Brightness);
677 void UiInvokePopupMenu(void *pID, int Flags, float X, float Y, float W, float H, int (*pfnFunc)(CEditor *pEditor, CUIRect Rect), void *pExtra=0);
678 void UiDoPopupMenu();
680 int UiDoValueSelector(void *pID, CUIRect *pRect, const char *pLabel, int Current, int Min, int Max, int Step, float Scale, const char *pToolTip);
682 static int PopupGroup(CEditor *pEditor, CUIRect View);
683 static int PopupLayer(CEditor *pEditor, CUIRect View);
684 static int PopupQuad(CEditor *pEditor, CUIRect View);
685 static int PopupPoint(CEditor *pEditor, CUIRect View);
686 static int PopupNewFolder(CEditor *pEditor, CUIRect View);
687 static int PopupEvent(CEditor *pEditor, CUIRect View);
688 static int PopupSelectImage(CEditor *pEditor, CUIRect View);
689 static int PopupSelectGametileOp(CEditor *pEditor, CUIRect View);
690 static int PopupImage(CEditor *pEditor, CUIRect View);
691 static int PopupMenuFile(CEditor *pEditor, CUIRect View);
692 static int PopupSelectConfigAutoMap(CEditor *pEditor, CUIRect View);
694 static void CallbackOpenMap(const char *pFileName, int StorageType, void *pUser);
695 static void CallbackAppendMap(const char *pFileName, int StorageType, void *pUser);
696 static void CallbackSaveMap(const char *pFileName, int StorageType, void *pUser);
698 void PopupSelectImageInvoke(int Current, float x, float y);
699 int PopupSelectImageResult();
701 void PopupSelectGametileOpInvoke(float x, float y);
702 int PopupSelectGameTileOpResult();
704 void PopupSelectConfigAutoMapInvoke(float x, float y);
705 int PopupSelectConfigAutoMapResult();
707 vec4 ButtonColorMul(const void *pID);
709 void DoQuadPoint(CQuad *pQuad, int QuadIndex, int v);
710 void DoMapEditor(CUIRect View, CUIRect Toolbar);
711 void DoToolbar(CUIRect Toolbar);
712 void DoQuad(CQuad *pQuad, int Index);
713 float UiDoScrollbarV(const void *pID, const CUIRect *pRect, float Current);
714 vec4 GetButtonColor(const void *pID, int Checked);
716 static void ReplaceImage(const char *pFilename, int StorageType, void *pUser);
717 static void AddImage(const char *pFilename, int StorageType, void *pUser);
719 void RenderImages(CUIRect Toolbox, CUIRect Toolbar, CUIRect View);
720 void RenderLayers(CUIRect Toolbox, CUIRect Toolbar, CUIRect View);
721 void RenderModebar(CUIRect View);
722 void RenderStatusbar(CUIRect View);
723 void RenderEnvelopeEditor(CUIRect View);
725 void RenderMenubar(CUIRect Menubar);
726 void RenderFileDialog();
728 void AddFileDialogEntry(int Index, CUIRect *pView);
729 void SortImages();
730 static void ExtractName(const char *pFileName, char *pName, int BufferSize)
732 const char *pExtractedName = pFileName;
733 const char *pEnd = 0;
734 for(; *pFileName; ++pFileName)
736 if(*pFileName == '/' || *pFileName == '\\')
737 pExtractedName = pFileName+1;
738 else if(*pFileName == '.')
739 pEnd = pFileName;
742 int Length = pEnd > pExtractedName ? min(BufferSize, (int)(pEnd-pExtractedName+1)) : BufferSize;
743 str_copy(pName, pExtractedName, Length);
747 // make sure to inline this function
748 inline class IGraphics *CLayer::Graphics() { return m_pEditor->Graphics(); }
749 inline class ITextRender *CLayer::TextRender() { return m_pEditor->TextRender(); }
751 #endif