| 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_MAPITEMS_MAP_H |
| 4 | #define GAME_EDITOR_MAPITEMS_MAP_H |
| 5 | |
| 6 | #include <base/types.h> |
| 7 | |
| 8 | #include <engine/shared/datafile.h> |
| 9 | #include <engine/shared/jobs.h> |
| 10 | |
| 11 | #include <game/editor/editor_history.h> |
| 12 | #include <game/editor/editor_server_settings.h> |
| 13 | #include <game/editor/editor_trackers.h> |
| 14 | #include <game/editor/mapitems/envelope.h> |
| 15 | #include <game/editor/mapitems/layer.h> |
| 16 | |
| 17 | #include <functional> |
| 18 | #include <memory> |
| 19 | #include <vector> |
| 20 | |
| 21 | class CEditor; |
| 22 | class CEditorImage; |
| 23 | class CEditorSound; |
| 24 | class CLayerFront; |
| 25 | class CLayerGroup; |
| 26 | class CLayerGame; |
| 27 | class CLayerImage; |
| 28 | class CLayerSound; |
| 29 | class CLayerSpeedup; |
| 30 | class CLayerSwitch; |
| 31 | class CLayerTele; |
| 32 | class CLayerTune; |
| 33 | class CQuad; |
| 34 | class IEditorEnvelopeReference; |
| 35 | |
| 36 | class CDataFileWriterFinishJob : public IJob |
| 37 | { |
| 38 | char m_aRealFilename[IO_MAX_PATH_LENGTH]; |
| 39 | char m_aTempFilename[IO_MAX_PATH_LENGTH]; |
| 40 | CDataFileWriter m_Writer; |
| 41 | |
| 42 | void Run() override; |
| 43 | |
| 44 | public: |
| 45 | CDataFileWriterFinishJob(const char *pRealFilename, const char *pTempFilename, CDataFileWriter &&Writer); |
| 46 | const char *GetRealFilename() const { return m_aRealFilename; } |
| 47 | const char *GetTempFilename() const { return m_aTempFilename; } |
| 48 | }; |
| 49 | |
| 50 | using FErrorHandler = std::function<void(const char *pErrorMessage)>; |
| 51 | |
| 52 | class CEditorMap |
| 53 | { |
| 54 | public: |
| 55 | explicit CEditorMap(CEditor *pEditor) : |
| 56 | m_EditorHistory(this), |
| 57 | m_ServerSettingsHistory(this), |
| 58 | m_EnvelopeEditorHistory(this), |
| 59 | m_QuadTracker(this), |
| 60 | m_EnvOpTracker(this), |
| 61 | m_LayerGroupPropTracker(this), |
| 62 | m_LayerPropTracker(this), |
| 63 | m_LayerTilesCommonPropTracker(this), |
| 64 | m_LayerTilesPropTracker(this), |
| 65 | m_LayerQuadPropTracker(this), |
| 66 | m_LayerSoundsPropTracker(this), |
| 67 | m_SoundSourceOperationTracker(this), |
| 68 | m_SoundSourcePropTracker(this), |
| 69 | m_SoundSourceRectShapePropTracker(this), |
| 70 | m_SoundSourceCircleShapePropTracker(this), |
| 71 | m_pEditor(pEditor) |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | const CEditor *Editor() const { return m_pEditor; } |
| 76 | CEditor *Editor() { return m_pEditor; } |
| 77 | |
| 78 | /** |
| 79 | * Map has unsaved changes for manual save. |
| 80 | */ |
| 81 | bool m_Modified; |
| 82 | /** |
| 83 | * Map has unsaved changes for autosave. |
| 84 | */ |
| 85 | bool m_ModifiedAuto; |
| 86 | float m_LastModifiedTime; |
| 87 | float m_LastSaveTime; |
| 88 | void OnModify(); |
| 89 | void ResetModifiedState(); |
| 90 | |
| 91 | std::vector<std::shared_ptr<CLayerGroup>> m_vpGroups; |
| 92 | std::vector<std::shared_ptr<CEditorImage>> m_vpImages; |
| 93 | std::vector<std::shared_ptr<CEnvelope>> m_vpEnvelopes; |
| 94 | std::vector<std::shared_ptr<CEditorSound>> m_vpSounds; |
| 95 | std::vector<CEditorMapSetting> m_vSettings; |
| 96 | |
| 97 | std::shared_ptr<CLayerGroup> m_pGameGroup; |
| 98 | std::shared_ptr<CLayerGame> m_pGameLayer; |
| 99 | std::shared_ptr<CLayerTele> m_pTeleLayer; |
| 100 | std::shared_ptr<CLayerSpeedup> m_pSpeedupLayer; |
| 101 | std::shared_ptr<CLayerFront> m_pFrontLayer; |
| 102 | std::shared_ptr<CLayerSwitch> m_pSwitchLayer; |
| 103 | std::shared_ptr<CLayerTune> m_pTuneLayer; |
| 104 | |
| 105 | class CMapInfo |
| 106 | { |
| 107 | public: |
| 108 | char m_aAuthor[32]; |
| 109 | char m_aVersion[16]; |
| 110 | char m_aCredits[128]; |
| 111 | char m_aLicense[32]; |
| 112 | |
| 113 | void Reset(); |
| 114 | void Copy(const CMapInfo &Source); |
| 115 | }; |
| 116 | CMapInfo m_MapInfo; |
| 117 | CMapInfo m_MapInfoTmp; |
| 118 | |
| 119 | // Undo/Redo |
| 120 | CEditorHistory m_EditorHistory; |
| 121 | CEditorHistory m_ServerSettingsHistory; |
| 122 | CEditorHistory m_EnvelopeEditorHistory; |
| 123 | CQuadEditTracker m_QuadTracker; |
| 124 | CEnvelopeEditorOperationTracker m_EnvOpTracker; |
| 125 | CLayerGroupPropTracker m_LayerGroupPropTracker; |
| 126 | CLayerPropTracker m_LayerPropTracker; |
| 127 | CLayerTilesCommonPropTracker m_LayerTilesCommonPropTracker; |
| 128 | CLayerTilesPropTracker m_LayerTilesPropTracker; |
| 129 | CLayerQuadsPropTracker m_LayerQuadPropTracker; |
| 130 | CLayerSoundsPropTracker m_LayerSoundsPropTracker; |
| 131 | CSoundSourceOperationTracker m_SoundSourceOperationTracker; |
| 132 | CSoundSourcePropTracker m_SoundSourcePropTracker; |
| 133 | CSoundSourceRectShapePropTracker m_SoundSourceRectShapePropTracker; |
| 134 | CSoundSourceCircleShapePropTracker m_SoundSourceCircleShapePropTracker; |
| 135 | |
| 136 | int m_SelectedImage; |
| 137 | int m_SelectedSound; |
| 138 | |
| 139 | std::shared_ptr<CEnvelope> NewEnvelope(CEnvelope::EType Type); |
| 140 | void InsertEnvelope(int Index, std::shared_ptr<CEnvelope> &pEnvelope); |
| 141 | void UpdateEnvelopeReferences(int Index, std::shared_ptr<CEnvelope> &pEnvelope, std::vector<std::shared_ptr<IEditorEnvelopeReference>> &vpEditorObjectReferences); |
| 142 | std::vector<std::shared_ptr<IEditorEnvelopeReference>> DeleteEnvelope(int Index); |
| 143 | int MoveEnvelope(int IndexFrom, int IndexTo); |
| 144 | template<typename F> |
| 145 | std::vector<std::shared_ptr<IEditorEnvelopeReference>> VisitEnvelopeReferences(F &&Visitor); |
| 146 | |
| 147 | std::shared_ptr<CLayerGroup> NewGroup(); |
| 148 | int MoveGroup(int IndexFrom, int IndexTo); |
| 149 | void DeleteGroup(int Index); |
| 150 | void ModifyImageIndex(const FIndexModifyFunction &IndexModifyFunction); |
| 151 | void ModifyEnvelopeIndex(const FIndexModifyFunction &IndexModifyFunction); |
| 152 | void ModifySoundIndex(const FIndexModifyFunction &IndexModifyFunction); |
| 153 | |
| 154 | // Housekeeping |
| 155 | void Clean(); |
| 156 | void CreateDefault(); |
| 157 | void CheckIntegrity(); |
| 158 | |
| 159 | // io |
| 160 | bool Save(const char *pFilename, const FErrorHandler &ErrorHandler); |
| 161 | bool PerformPreSaveSanityChecks(const FErrorHandler &ErrorHandler); |
| 162 | bool Load(const char *pFilename, int StorageType, const FErrorHandler &ErrorHandler); |
| 163 | void PerformSanityChecks(const FErrorHandler &ErrorHandler); |
| 164 | |
| 165 | void MakeGameGroup(std::shared_ptr<CLayerGroup> pGroup); |
| 166 | void MakeGameLayer(const std::shared_ptr<CLayer> &pLayer); |
| 167 | void MakeTeleLayer(const std::shared_ptr<CLayer> &pLayer); |
| 168 | void MakeSpeedupLayer(const std::shared_ptr<CLayer> &pLayer); |
| 169 | void MakeFrontLayer(const std::shared_ptr<CLayer> &pLayer); |
| 170 | void MakeSwitchLayer(const std::shared_ptr<CLayer> &pLayer); |
| 171 | void MakeTuneLayer(const std::shared_ptr<CLayer> &pLayer); |
| 172 | |
| 173 | std::shared_ptr<CEditorImage> SelectedImage() const; |
| 174 | void SelectImage(const std::shared_ptr<CEditorImage> &pImage); |
| 175 | void SelectNextImage(); |
| 176 | void SelectPreviousImage(); |
| 177 | bool IsImageUsed(int ImageIndex) const; |
| 178 | std::vector<int> SortImages(); |
| 179 | |
| 180 | std::shared_ptr<CEditorSound> SelectedSound() const; |
| 181 | void SelectSound(const std::shared_ptr<CEditorSound> &pSound); |
| 182 | void SelectNextSound(); |
| 183 | void SelectPreviousSound(); |
| 184 | bool IsSoundUsed(int SoundIndex) const; |
| 185 | |
| 186 | private: |
| 187 | CEditor *m_pEditor; |
| 188 | }; |
| 189 | |
| 190 | #endif |
| 191 | |