Merge pull request #7095 from Marmare314/refactor-a1

Refactor editor mapitems
This commit is contained in:
Robert Müller 2023-09-01 18:47:51 +00:00 committed by GitHub
commit 1ebd209f90
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 214 additions and 185 deletions

View file

@ -2311,12 +2311,12 @@ if(CLIENT)
editor.cpp
editor.h
explanations.cpp
io.cpp
map_grid.cpp
map_grid.h
map_view.cpp
map_view.h
mapitems/image.cpp
mapitems/image.h
mapitems/layer_front.cpp
mapitems/layer_game.cpp
mapitems/layer_group.cpp
@ -2328,7 +2328,9 @@ if(CLIENT)
mapitems/layer_tiles.cpp
mapitems/layer_tune.cpp
mapitems/map.cpp
mapitems/map_io.cpp
mapitems/sound.cpp
mapitems/sound.h
popups.cpp
proof_mode.cpp
proof_mode.h

View file

@ -76,19 +76,19 @@ public:
/* Variable: width
Contains the width of the image */
int m_Width;
int m_Width = 0;
/* Variable: height
Contains the height of the image */
int m_Height;
int m_Height = 0;
/* Variable: format
Contains the format of the image. See <Image Formats> for more information. */
int m_Format;
int m_Format = FORMAT_RGB;
/* Variable: data
Pointer to the image data. */
void *m_pData;
void *m_pData = nullptr;
};
/*

View file

@ -5,8 +5,10 @@
#include <engine/shared/linereader.h>
#include <engine/storage.h>
#include <game/mapitems.h>
#include "auto_map.h"
#include "editor.h"
#include "editor.h" // TODO: only needs CLayerTiles
// Based on triple32inc from https://github.com/skeeto/hash-prospector/tree/79a6074062a84907df6e45b756134b74e2956760
static uint32_t HashUInt32(uint32_t Num)
@ -39,15 +41,14 @@ static int HashLocation(uint32_t Seed, uint32_t Run, uint32_t Rule, uint32_t X,
CAutoMapper::CAutoMapper(CEditor *pEditor)
{
m_pEditor = pEditor;
m_FileLoaded = false;
Init(pEditor);
}
void CAutoMapper::Load(const char *pTileName)
{
char aPath[IO_MAX_PATH_LENGTH];
str_format(aPath, sizeof(aPath), "editor/%s.rules", pTileName);
IOHANDLE RulesFile = m_pEditor->Storage()->OpenFile(aPath, IOFLAG_READ | IOFLAG_SKIP_BOM, IStorage::TYPE_ALL);
IOHANDLE RulesFile = Storage()->OpenFile(aPath, IOFLAG_READ | IOFLAG_SKIP_BOM, IStorage::TYPE_ALL);
if(!RulesFile)
return;
@ -362,7 +363,7 @@ void CAutoMapper::Load(const char *pTileName)
char aBuf[IO_MAX_PATH_LENGTH + 16];
str_format(aBuf, sizeof(aBuf), "loaded %s", aPath);
m_pEditor->Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "editor/automap", aBuf);
Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "editor/automap", aBuf);
m_FileLoaded = true;
}
@ -470,7 +471,7 @@ void CAutoMapper::Proceed(CLayerTiles *pLayer, int ConfigID, int Seed, int SeedO
for(int x = 0; x < pLayer->m_Width; x++)
{
CTile *pTile = &(pLayer->m_pTiles[y * pLayer->m_Width + x]);
m_pEditor->m_Map.OnModify();
Editor()->m_Map.OnModify();
for(size_t i = 0; i < pRun->m_vIndexRules.size(); ++i)
{

View file

@ -3,7 +3,9 @@
#include <vector>
class CAutoMapper
#include "component.h"
class CAutoMapper : public CEditorComponent
{
struct CIndexInfo
{
@ -55,7 +57,7 @@ class CAutoMapper
};
public:
CAutoMapper(class CEditor *pEditor);
explicit CAutoMapper(CEditor *pEditor);
void Load(const char *pTileName);
void ProceedLocalized(class CLayerTiles *pLayer, int ConfigID, int Seed = 0, int X = 0, int Y = 0, int Width = -1, int Height = -1);
@ -67,9 +69,8 @@ public:
bool IsLoaded() const { return m_FileLoaded; }
private:
std::vector<CConfiguration> m_vConfigs;
class CEditor *m_pEditor;
bool m_FileLoaded;
std::vector<CConfiguration> m_vConfigs = {};
bool m_FileLoaded = false;
};
#endif

View file

@ -31,6 +31,9 @@
#include <game/generated/client_data.h>
#include <game/localization.h>
#include <game/editor/mapitems/image.h>
#include <game/editor/mapitems/sound.h>
#include "auto_map.h"
#include "editor.h"
@ -7824,4 +7827,107 @@ void CEditor::LoadCurrentMap()
MapView()->SetWorldOffset(Center);
}
bool CEditor::Save(const char *pFilename)
{
// Check if file with this name is already being saved at the moment
if(std::any_of(std::begin(m_WriterFinishJobs), std::end(m_WriterFinishJobs), [pFilename](const std::shared_ptr<CDataFileWriterFinishJob> &Job) { return str_comp(pFilename, Job->GetRealFileName()) == 0; }))
return false;
return m_Map.Save(pFilename);
}
bool CEditor::HandleMapDrop(const char *pFileName, int StorageType)
{
if(HasUnsavedData())
{
str_copy(m_aFileNamePending, pFileName);
m_PopupEventType = CEditor::POPEVENT_LOADDROP;
m_PopupEventActivated = true;
return true;
}
else
{
return Load(pFileName, IStorage::TYPE_ALL_OR_ABSOLUTE);
}
}
bool CEditor::Load(const char *pFileName, int StorageType)
{
const auto &&ErrorHandler = [this](const char *pErrorMessage) {
ShowFileDialogError("%s", pErrorMessage);
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "editor/load", pErrorMessage);
};
Reset();
bool Result = m_Map.Load(pFileName, StorageType, std::move(ErrorHandler));
if(Result)
{
str_copy(m_aFileName, pFileName);
SortImages();
SelectGameLayer();
MapView()->OnMapLoad();
}
else
{
m_aFileName[0] = 0;
Reset();
}
return Result;
}
bool CEditor::Append(const char *pFileName, int StorageType)
{
CEditorMap NewMap;
NewMap.m_pEditor = this;
const auto &&ErrorHandler = [this](const char *pErrorMessage) {
ShowFileDialogError("%s", pErrorMessage);
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "editor/append", pErrorMessage);
};
if(!NewMap.Load(pFileName, StorageType, std::move(ErrorHandler)))
return false;
// modify indices
static const auto &&s_ModifyAddIndex = [](int AddAmount) {
return [AddAmount](int *pIndex) {
if(*pIndex >= 0)
*pIndex += AddAmount;
};
};
NewMap.ModifyImageIndex(s_ModifyAddIndex(m_Map.m_vpImages.size()));
NewMap.ModifySoundIndex(s_ModifyAddIndex(m_Map.m_vpSounds.size()));
NewMap.ModifyEnvelopeIndex(s_ModifyAddIndex(m_Map.m_vpEnvelopes.size()));
// transfer images
for(const auto &pImage : NewMap.m_vpImages)
m_Map.m_vpImages.push_back(pImage);
NewMap.m_vpImages.clear();
// transfer sounds
for(const auto &pSound : NewMap.m_vpSounds)
m_Map.m_vpSounds.push_back(pSound);
NewMap.m_vpSounds.clear();
// transfer envelopes
for(const auto &pEnvelope : NewMap.m_vpEnvelopes)
m_Map.m_vpEnvelopes.push_back(pEnvelope);
NewMap.m_vpEnvelopes.clear();
// transfer groups
for(const auto &pGroup : NewMap.m_vpGroups)
{
if(pGroup != NewMap.m_pGameGroup)
{
pGroup->m_pMap = &m_Map;
m_Map.m_vpGroups.push_back(pGroup);
}
}
NewMap.m_vpGroups.clear();
SortImages();
// all done \o/
return true;
}
IEditor *CreateEditor() { return new CEditor; }

View file

@ -329,58 +329,8 @@ public:
}
};
class CEditorImage : public CImageInfo
{
public:
CEditor *m_pEditor;
CEditorImage(CEditor *pEditor) :
m_AutoMapper(pEditor)
{
m_pEditor = pEditor;
m_aName[0] = 0;
m_Texture.Invalidate();
m_External = 0;
m_Width = 0;
m_Height = 0;
m_pData = nullptr;
m_Format = 0;
}
~CEditorImage();
void AnalyseTileFlags();
IGraphics::CTextureHandle m_Texture;
int m_External;
char m_aName[IO_MAX_PATH_LENGTH];
unsigned char m_aTileFlags[256];
class CAutoMapper m_AutoMapper;
};
class CEditorSound
{
public:
CEditor *m_pEditor;
CEditorSound(CEditor *pEditor)
{
m_pEditor = pEditor;
m_aName[0] = 0;
m_SoundID = 0;
m_pData = nullptr;
m_DataSize = 0;
}
~CEditorSound();
int m_SoundID;
char m_aName[IO_MAX_PATH_LENGTH];
void *m_pData;
unsigned m_DataSize;
};
class CEditorImage;
class CEditorSound;
class CEditorMap
{
@ -523,7 +473,6 @@ public:
// io
bool Save(const char *pFilename);
bool Load(const char *pFilename, int StorageType, const std::function<void(const char *pErrorMessage)> &ErrorHandler);
bool HandleMapDrop(const char *pFilename, int StorageType);
void PerformSanityChecks(const std::function<void(const char *pErrorMessage)> &ErrorHandler);
// DDRace

View file

@ -1,12 +1,28 @@
#include <game/editor/editor.h>
#include "image.h"
#include <game/mapitems.h>
CEditorImage::CEditorImage(CEditor *pEditor) :
m_AutoMapper(pEditor)
{
Init(pEditor);
m_Texture.Invalidate();
}
CEditorImage::~CEditorImage()
{
m_pEditor->Graphics()->UnloadTexture(&m_Texture);
Graphics()->UnloadTexture(&m_Texture);
free(m_pData);
m_pData = nullptr;
}
void CEditorImage::Init(CEditor *pEditor)
{
CEditorComponent::Init(pEditor);
RegisterSubComponent(m_AutoMapper);
InitSubComponents();
}
void CEditorImage::AnalyseTileFlags()
{
mem_zero(m_aTileFlags, sizeof(m_aTileFlags));

View file

@ -0,0 +1,25 @@
#ifndef GAME_EDITOR_MAPITEMS_IMAGE_H
#define GAME_EDITOR_MAPITEMS_IMAGE_H
#include <engine/graphics.h>
#include <game/editor/auto_map.h>
#include <game/editor/component.h>
class CEditorImage : public CImageInfo, public CEditorComponent
{
public:
explicit CEditorImage(CEditor *pEditor);
~CEditorImage();
void Init(CEditor *pEditor) override;
void AnalyseTileFlags();
IGraphics::CTextureHandle m_Texture;
int m_External = 0;
char m_aName[IO_MAX_PATH_LENGTH] = "";
unsigned char m_aTileFlags[256];
CAutoMapper m_AutoMapper;
};
#endif

View file

@ -2,6 +2,8 @@
/* If you are missing that file, acquire a complete release at teeworlds.com. */
#include <game/editor/editor.h>
#include "image.h"
CLayerQuads::CLayerQuads()
{
m_Type = LAYERTYPE_QUADS;

View file

@ -5,6 +5,8 @@
#include <engine/keys.h>
#include <engine/shared/map.h>
#include "image.h"
CLayerTiles::CLayerTiles(int w, int h)
{
m_Type = LAYERTYPE_TILES;

View file

@ -1,5 +1,7 @@
#include <game/editor/editor.h>
#include "image.h"
void CEditorMap::OnModify()
{
m_Modified = true;

View file

@ -1,6 +1,5 @@
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
#include "editor.h"
#include <game/editor/editor.h>
#include <engine/client.h>
#include <engine/console.h>
#include <engine/graphics.h>
@ -12,6 +11,9 @@
#include <game/gamecore.h>
#include <game/mapitems_ex.h>
#include "image.h"
#include "sound.h"
template<typename T>
static int MakeVersion(int i, const T &v)
{
@ -31,15 +33,6 @@ struct CSoundSource_DEPRECATED
int m_SoundEnvOffset;
};
bool CEditor::Save(const char *pFilename)
{
// Check if file with this name is already being saved at the moment
if(std::any_of(std::begin(m_WriterFinishJobs), std::end(m_WriterFinishJobs), [pFilename](const std::shared_ptr<CDataFileWriterFinishJob> &Job) { return str_comp(pFilename, Job->GetRealFileName()) == 0; }))
return false;
return m_Map.Save(pFilename);
}
bool CEditorMap::Save(const char *pFileName)
{
char aFileNameTmp[IO_MAX_PATH_LENGTH];
@ -426,50 +419,6 @@ bool CEditorMap::Save(const char *pFileName)
return true;
}
bool CEditor::HandleMapDrop(const char *pFileName, int StorageType)
{
return m_Map.HandleMapDrop(pFileName, IStorage::TYPE_ALL_OR_ABSOLUTE);
}
bool CEditorMap::HandleMapDrop(const char *pFileName, int StorageType)
{
if(m_pEditor->HasUnsavedData())
{
str_copy(m_pEditor->m_aFileNamePending, pFileName);
m_pEditor->m_PopupEventType = CEditor::POPEVENT_LOADDROP;
m_pEditor->m_PopupEventActivated = true;
return true;
}
else
{
return m_pEditor->Load(pFileName, IStorage::TYPE_ALL_OR_ABSOLUTE);
}
}
bool CEditor::Load(const char *pFileName, int StorageType)
{
const auto &&ErrorHandler = [this](const char *pErrorMessage) {
ShowFileDialogError("%s", pErrorMessage);
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "editor/load", pErrorMessage);
};
Reset();
bool Result = m_Map.Load(pFileName, StorageType, std::move(ErrorHandler));
if(Result)
{
str_copy(m_aFileName, pFileName);
SortImages();
SelectGameLayer();
MapView()->OnMapLoad();
}
else
{
m_aFileName[0] = 0;
Reset();
}
return Result;
}
bool CEditorMap::Load(const char *pFileName, int StorageType, const std::function<void(const char *pErrorMessage)> &ErrorHandler)
{
CDataFileReader DataFile;
@ -600,7 +549,6 @@ bool CEditorMap::Load(const char *pFileName, int StorageType, const std::functio
// copy base info
std::shared_ptr<CEditorSound> pSound = std::make_shared<CEditorSound>(m_pEditor);
if(pItem->m_External)
{
char aBuf[IO_MAX_PATH_LENGTH];
@ -1064,58 +1012,3 @@ void CEditorMap::PerformSanityChecks(const std::function<void(const char *pError
++ImageIndex;
}
}
bool CEditor::Append(const char *pFileName, int StorageType)
{
CEditorMap NewMap;
NewMap.m_pEditor = this;
const auto &&ErrorHandler = [this](const char *pErrorMessage) {
ShowFileDialogError("%s", pErrorMessage);
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "editor/append", pErrorMessage);
};
if(!NewMap.Load(pFileName, StorageType, std::move(ErrorHandler)))
return false;
// modify indices
static const auto &&s_ModifyAddIndex = [](int AddAmount) {
return [AddAmount](int *pIndex) {
if(*pIndex >= 0)
*pIndex += AddAmount;
};
};
NewMap.ModifyImageIndex(s_ModifyAddIndex(m_Map.m_vpImages.size()));
NewMap.ModifySoundIndex(s_ModifyAddIndex(m_Map.m_vpSounds.size()));
NewMap.ModifyEnvelopeIndex(s_ModifyAddIndex(m_Map.m_vpEnvelopes.size()));
// transfer images
for(const auto &pImage : NewMap.m_vpImages)
m_Map.m_vpImages.push_back(pImage);
NewMap.m_vpImages.clear();
// transfer sounds
for(const auto &pSound : NewMap.m_vpSounds)
m_Map.m_vpSounds.push_back(pSound);
NewMap.m_vpSounds.clear();
// transfer envelopes
for(const auto &pEnvelope : NewMap.m_vpEnvelopes)
m_Map.m_vpEnvelopes.push_back(pEnvelope);
NewMap.m_vpEnvelopes.clear();
// transfer groups
for(const auto &pGroup : NewMap.m_vpGroups)
{
if(pGroup != NewMap.m_pGameGroup)
{
pGroup->m_pMap = &m_Map;
m_Map.m_vpGroups.push_back(pGroup);
}
}
NewMap.m_vpGroups.clear();
SortImages();
// all done \o/
return true;
}

View file

@ -1,10 +1,15 @@
#include <game/editor/editor.h>
#include "sound.h"
#include <engine/sound.h>
CEditorSound::CEditorSound(CEditor *pEditor)
{
Init(pEditor);
}
CEditorSound::~CEditorSound()
{
m_pEditor->Sound()->UnloadSample(m_SoundID);
Sound()->UnloadSample(m_SoundID);
free(m_pData);
m_pData = nullptr;
}

View file

@ -0,0 +1,21 @@
#ifndef GAME_EDITOR_MAPITEMS_SOUND_H
#define GAME_EDITOR_MAPITEMS_SOUND_H
#include <base/system.h>
#include <game/editor/component.h>
class CEditorSound : public CEditorComponent
{
public:
explicit CEditorSound(CEditor *pEditor);
~CEditorSound();
int m_SoundID = 0;
char m_aName[IO_MAX_PATH_LENGTH] = "";
void *m_pData = nullptr;
unsigned m_DataSize = 0;
};
#endif

View file

@ -12,6 +12,8 @@
#include <limits>
#include <game/client/ui_scrollregion.h>
#include <game/editor/mapitems/image.h>
#include <game/editor/mapitems/sound.h>
#include "editor.h"

View file

@ -1,5 +1,7 @@
#include "editor.h"
#include <game/editor/mapitems/image.h>
#include <array>
bool operator<(const ColorRGBA &Left, const ColorRGBA &Right)