mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 10:08:18 +00:00
Use lambdas for editor index modify and sort functions
For cleaner code with less global state variables.
This commit is contained in:
parent
7f100e2620
commit
1acb94ffbf
|
@ -4246,41 +4246,33 @@ void CEditor::SelectGameLayer()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool ImageNameLess(const CEditorImage *const &a, const CEditorImage *const &b)
|
|
||||||
{
|
|
||||||
return str_comp(a->m_aName, b->m_aName) < 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int *gs_pSortedIndex = nullptr;
|
|
||||||
static void ModifySortedIndex(int *pIndex)
|
|
||||||
{
|
|
||||||
if(*pIndex >= 0)
|
|
||||||
*pIndex = gs_pSortedIndex[*pIndex];
|
|
||||||
}
|
|
||||||
|
|
||||||
void CEditor::SortImages()
|
void CEditor::SortImages()
|
||||||
{
|
{
|
||||||
if(!std::is_sorted(m_Map.m_vpImages.begin(), m_Map.m_vpImages.end(), ImageNameLess))
|
static const auto &&s_ImageNameComparator = [](const CEditorImage *const &pLhs, const CEditorImage *const &pRhs) {
|
||||||
|
return str_comp(pLhs->m_aName, pRhs->m_aName) < 0;
|
||||||
|
};
|
||||||
|
if(!std::is_sorted(m_Map.m_vpImages.begin(), m_Map.m_vpImages.end(), s_ImageNameComparator))
|
||||||
{
|
{
|
||||||
std::vector<CEditorImage *> vpTemp = m_Map.m_vpImages;
|
const std::vector<CEditorImage *> vpTemp = m_Map.m_vpImages;
|
||||||
gs_pSortedIndex = new int[vpTemp.size()];
|
std::vector<int> vSortedIndex;
|
||||||
|
vSortedIndex.resize(vpTemp.size());
|
||||||
|
|
||||||
std::sort(m_Map.m_vpImages.begin(), m_Map.m_vpImages.end(), ImageNameLess);
|
std::sort(m_Map.m_vpImages.begin(), m_Map.m_vpImages.end(), s_ImageNameComparator);
|
||||||
for(size_t OldIndex = 0; OldIndex < vpTemp.size(); OldIndex++)
|
for(size_t OldIndex = 0; OldIndex < vpTemp.size(); OldIndex++)
|
||||||
{
|
{
|
||||||
for(size_t NewIndex = 0; NewIndex < m_Map.m_vpImages.size(); NewIndex++)
|
for(size_t NewIndex = 0; NewIndex < m_Map.m_vpImages.size(); NewIndex++)
|
||||||
{
|
{
|
||||||
if(vpTemp[OldIndex] == m_Map.m_vpImages[NewIndex])
|
if(vpTemp[OldIndex] == m_Map.m_vpImages[NewIndex])
|
||||||
{
|
{
|
||||||
gs_pSortedIndex[OldIndex] = NewIndex;
|
vSortedIndex[OldIndex] = NewIndex;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_Map.ModifyImageIndex(ModifySortedIndex);
|
m_Map.ModifyImageIndex([vSortedIndex](int *pIndex) {
|
||||||
|
if(*pIndex >= 0)
|
||||||
delete[] gs_pSortedIndex;
|
*pIndex = vSortedIndex[*pIndex];
|
||||||
gs_pSortedIndex = nullptr;
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,15 +21,14 @@
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <deque>
|
#include <deque>
|
||||||
|
#include <functional>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
typedef void (*INDEX_MODIFY_FUNC)(int *pIndex);
|
typedef std::function<void(int *pIndex)> FIndexModifyFunction;
|
||||||
|
|
||||||
// CRenderTools m_RenderTools;
|
|
||||||
|
|
||||||
// CEditor SPECIFIC
|
// CEditor SPECIFIC
|
||||||
enum
|
enum
|
||||||
|
@ -163,9 +162,9 @@ public:
|
||||||
virtual void Render(bool Tileset = false) {}
|
virtual void Render(bool Tileset = false) {}
|
||||||
virtual CUI::EPopupMenuFunctionResult RenderProperties(CUIRect *pToolbox) { return CUI::POPUP_KEEP_OPEN; }
|
virtual CUI::EPopupMenuFunctionResult RenderProperties(CUIRect *pToolbox) { return CUI::POPUP_KEEP_OPEN; }
|
||||||
|
|
||||||
virtual void ModifyImageIndex(INDEX_MODIFY_FUNC pfnFunc) {}
|
virtual void ModifyImageIndex(FIndexModifyFunction pfnFunc) {}
|
||||||
virtual void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC pfnFunc) {}
|
virtual void ModifyEnvelopeIndex(FIndexModifyFunction pfnFunc) {}
|
||||||
virtual void ModifySoundIndex(INDEX_MODIFY_FUNC pfnFunc) {}
|
virtual void ModifySoundIndex(FIndexModifyFunction pfnFunc) {}
|
||||||
|
|
||||||
virtual CLayer *Duplicate() const = 0;
|
virtual CLayer *Duplicate() const = 0;
|
||||||
|
|
||||||
|
@ -242,19 +241,19 @@ public:
|
||||||
|
|
||||||
void AddLayer(CLayer *pLayer);
|
void AddLayer(CLayer *pLayer);
|
||||||
|
|
||||||
void ModifyImageIndex(INDEX_MODIFY_FUNC Func)
|
void ModifyImageIndex(FIndexModifyFunction Func)
|
||||||
{
|
{
|
||||||
for(auto &pLayer : m_vpLayers)
|
for(auto &pLayer : m_vpLayers)
|
||||||
pLayer->ModifyImageIndex(Func);
|
pLayer->ModifyImageIndex(Func);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC Func)
|
void ModifyEnvelopeIndex(FIndexModifyFunction Func)
|
||||||
{
|
{
|
||||||
for(auto &pLayer : m_vpLayers)
|
for(auto &pLayer : m_vpLayers)
|
||||||
pLayer->ModifyEnvelopeIndex(Func);
|
pLayer->ModifyEnvelopeIndex(Func);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ModifySoundIndex(INDEX_MODIFY_FUNC Func)
|
void ModifySoundIndex(FIndexModifyFunction Func)
|
||||||
{
|
{
|
||||||
for(auto &pLayer : m_vpLayers)
|
for(auto &pLayer : m_vpLayers)
|
||||||
pLayer->ModifySoundIndex(Func);
|
pLayer->ModifySoundIndex(Func);
|
||||||
|
@ -424,21 +423,21 @@ public:
|
||||||
m_vpGroups.erase(m_vpGroups.begin() + Index);
|
m_vpGroups.erase(m_vpGroups.begin() + Index);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ModifyImageIndex(INDEX_MODIFY_FUNC pfnFunc)
|
void ModifyImageIndex(FIndexModifyFunction pfnFunc)
|
||||||
{
|
{
|
||||||
OnModify();
|
OnModify();
|
||||||
for(auto &pGroup : m_vpGroups)
|
for(auto &pGroup : m_vpGroups)
|
||||||
pGroup->ModifyImageIndex(pfnFunc);
|
pGroup->ModifyImageIndex(pfnFunc);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC pfnFunc)
|
void ModifyEnvelopeIndex(FIndexModifyFunction pfnFunc)
|
||||||
{
|
{
|
||||||
OnModify();
|
OnModify();
|
||||||
for(auto &pGroup : m_vpGroups)
|
for(auto &pGroup : m_vpGroups)
|
||||||
pGroup->ModifyEnvelopeIndex(pfnFunc);
|
pGroup->ModifyEnvelopeIndex(pfnFunc);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ModifySoundIndex(INDEX_MODIFY_FUNC pfnFunc)
|
void ModifySoundIndex(FIndexModifyFunction pfnFunc)
|
||||||
{
|
{
|
||||||
OnModify();
|
OnModify();
|
||||||
for(auto &pGroup : m_vpGroups)
|
for(auto &pGroup : m_vpGroups)
|
||||||
|
@ -620,8 +619,8 @@ public:
|
||||||
};
|
};
|
||||||
static CUI::EPopupMenuFunctionResult RenderCommonProperties(SCommonPropState &State, CEditor *pEditor, CUIRect *pToolbox, std::vector<CLayerTiles *> &vpLayers);
|
static CUI::EPopupMenuFunctionResult RenderCommonProperties(SCommonPropState &State, CEditor *pEditor, CUIRect *pToolbox, std::vector<CLayerTiles *> &vpLayers);
|
||||||
|
|
||||||
void ModifyImageIndex(INDEX_MODIFY_FUNC pfnFunc) override;
|
void ModifyImageIndex(FIndexModifyFunction pfnFunc) override;
|
||||||
void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC pfnFunc) override;
|
void ModifyEnvelopeIndex(FIndexModifyFunction pfnFunc) override;
|
||||||
|
|
||||||
void PrepareForSave();
|
void PrepareForSave();
|
||||||
|
|
||||||
|
@ -676,8 +675,8 @@ public:
|
||||||
|
|
||||||
CUI::EPopupMenuFunctionResult RenderProperties(CUIRect *pToolbox) override;
|
CUI::EPopupMenuFunctionResult RenderProperties(CUIRect *pToolbox) override;
|
||||||
|
|
||||||
void ModifyImageIndex(INDEX_MODIFY_FUNC pfnFunc) override;
|
void ModifyImageIndex(FIndexModifyFunction pfnFunc) override;
|
||||||
void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC pfnFunc) override;
|
void ModifyEnvelopeIndex(FIndexModifyFunction pfnFunc) override;
|
||||||
|
|
||||||
void GetSize(float *pWidth, float *pHeight) override;
|
void GetSize(float *pWidth, float *pHeight) override;
|
||||||
CLayer *Duplicate() const override;
|
CLayer *Duplicate() const override;
|
||||||
|
@ -1523,8 +1522,8 @@ public:
|
||||||
|
|
||||||
CUI::EPopupMenuFunctionResult RenderProperties(CUIRect *pToolbox) override;
|
CUI::EPopupMenuFunctionResult RenderProperties(CUIRect *pToolbox) override;
|
||||||
|
|
||||||
void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC pfnFunc) override;
|
void ModifyEnvelopeIndex(FIndexModifyFunction pfnFunc) override;
|
||||||
void ModifySoundIndex(INDEX_MODIFY_FUNC pfnFunc) override;
|
void ModifySoundIndex(FIndexModifyFunction pfnFunc) override;
|
||||||
|
|
||||||
CLayer *Duplicate() const override;
|
CLayer *Duplicate() const override;
|
||||||
|
|
||||||
|
|
|
@ -1010,13 +1010,6 @@ void CEditorMap::PerformSanityChecks(const std::function<void(const char *pError
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int gs_ModifyAddAmount = 0;
|
|
||||||
static void ModifyAdd(int *pIndex)
|
|
||||||
{
|
|
||||||
if(*pIndex >= 0)
|
|
||||||
*pIndex += gs_ModifyAddAmount;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CEditor::Append(const char *pFileName, int StorageType)
|
bool CEditor::Append(const char *pFileName, int StorageType)
|
||||||
{
|
{
|
||||||
CEditorMap NewMap;
|
CEditorMap NewMap;
|
||||||
|
@ -1030,14 +1023,15 @@ bool CEditor::Append(const char *pFileName, int StorageType)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// modify indices
|
// modify indices
|
||||||
gs_ModifyAddAmount = m_Map.m_vpImages.size();
|
static const auto &&s_ModifyAddIndex = [](int AddAmount) {
|
||||||
NewMap.ModifyImageIndex(ModifyAdd);
|
return [AddAmount](int *pIndex) {
|
||||||
|
if(*pIndex >= 0)
|
||||||
gs_ModifyAddAmount = m_Map.m_vpSounds.size();
|
*pIndex += AddAmount;
|
||||||
NewMap.ModifySoundIndex(ModifyAdd);
|
};
|
||||||
|
};
|
||||||
gs_ModifyAddAmount = m_Map.m_vpEnvelopes.size();
|
NewMap.ModifyImageIndex(s_ModifyAddIndex(m_Map.m_vpImages.size()));
|
||||||
NewMap.ModifyEnvelopeIndex(ModifyAdd);
|
NewMap.ModifySoundIndex(s_ModifyAddIndex(m_Map.m_vpSounds.size()));
|
||||||
|
NewMap.ModifyEnvelopeIndex(s_ModifyAddIndex(m_Map.m_vpEnvelopes.size()));
|
||||||
|
|
||||||
// transfer images
|
// transfer images
|
||||||
for(const auto &pImage : NewMap.m_vpImages)
|
for(const auto &pImage : NewMap.m_vpImages)
|
||||||
|
|
|
@ -250,12 +250,12 @@ CUI::EPopupMenuFunctionResult CLayerQuads::RenderProperties(CUIRect *pToolBox)
|
||||||
return CUI::POPUP_KEEP_OPEN;
|
return CUI::POPUP_KEEP_OPEN;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CLayerQuads::ModifyImageIndex(INDEX_MODIFY_FUNC Func)
|
void CLayerQuads::ModifyImageIndex(FIndexModifyFunction Func)
|
||||||
{
|
{
|
||||||
Func(&m_Image);
|
Func(&m_Image);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CLayerQuads::ModifyEnvelopeIndex(INDEX_MODIFY_FUNC Func)
|
void CLayerQuads::ModifyEnvelopeIndex(FIndexModifyFunction Func)
|
||||||
{
|
{
|
||||||
for(auto &Quad : m_vQuads)
|
for(auto &Quad : m_vQuads)
|
||||||
{
|
{
|
||||||
|
|
|
@ -214,12 +214,12 @@ CUI::EPopupMenuFunctionResult CLayerSounds::RenderProperties(CUIRect *pToolBox)
|
||||||
return CUI::POPUP_KEEP_OPEN;
|
return CUI::POPUP_KEEP_OPEN;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CLayerSounds::ModifySoundIndex(INDEX_MODIFY_FUNC Func)
|
void CLayerSounds::ModifySoundIndex(FIndexModifyFunction Func)
|
||||||
{
|
{
|
||||||
Func(&m_Sound);
|
Func(&m_Sound);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CLayerSounds::ModifyEnvelopeIndex(INDEX_MODIFY_FUNC Func)
|
void CLayerSounds::ModifyEnvelopeIndex(FIndexModifyFunction Func)
|
||||||
{
|
{
|
||||||
for(auto &Source : m_vSources)
|
for(auto &Source : m_vSources)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1081,7 +1081,7 @@ void CLayerTiles::FlagModified(int x, int y, int w, int h)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CLayerTiles::ModifyImageIndex(INDEX_MODIFY_FUNC Func)
|
void CLayerTiles::ModifyImageIndex(FIndexModifyFunction Func)
|
||||||
{
|
{
|
||||||
const auto ImgBefore = m_Image;
|
const auto ImgBefore = m_Image;
|
||||||
Func(&m_Image);
|
Func(&m_Image);
|
||||||
|
@ -1089,7 +1089,7 @@ void CLayerTiles::ModifyImageIndex(INDEX_MODIFY_FUNC Func)
|
||||||
m_Texture.Invalidate();
|
m_Texture.Invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CLayerTiles::ModifyEnvelopeIndex(INDEX_MODIFY_FUNC Func)
|
void CLayerTiles::ModifyEnvelopeIndex(FIndexModifyFunction Func)
|
||||||
{
|
{
|
||||||
Func(&m_ColorEnv);
|
Func(&m_ColorEnv);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1285,14 +1285,14 @@ CUI::EPopupMenuFunctionResult CEditor::PopupPoint(void *pContext, CUIRect View,
|
||||||
return CUI::POPUP_KEEP_OPEN;
|
return CUI::POPUP_KEEP_OPEN;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int gs_ModifyIndexDeletedIndex;
|
static const auto &&gs_ModifyIndexDeleted = [](int DeletedIndex) {
|
||||||
static void ModifyIndexDeleted(int *pIndex)
|
return [DeletedIndex](int *pIndex) {
|
||||||
{
|
if(*pIndex == DeletedIndex)
|
||||||
if(*pIndex == gs_ModifyIndexDeletedIndex)
|
*pIndex = -1;
|
||||||
*pIndex = -1;
|
else if(*pIndex > DeletedIndex)
|
||||||
else if(*pIndex > gs_ModifyIndexDeletedIndex)
|
*pIndex = *pIndex - 1;
|
||||||
*pIndex = *pIndex - 1;
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
CUI::EPopupMenuFunctionResult CEditor::PopupImage(void *pContext, CUIRect View, bool Active)
|
CUI::EPopupMenuFunctionResult CEditor::PopupImage(void *pContext, CUIRect View, bool Active)
|
||||||
{
|
{
|
||||||
|
@ -1377,8 +1377,7 @@ CUI::EPopupMenuFunctionResult CEditor::PopupImage(void *pContext, CUIRect View,
|
||||||
{
|
{
|
||||||
delete pImg;
|
delete pImg;
|
||||||
pEditor->m_Map.m_vpImages.erase(pEditor->m_Map.m_vpImages.begin() + pEditor->m_SelectedImage);
|
pEditor->m_Map.m_vpImages.erase(pEditor->m_Map.m_vpImages.begin() + pEditor->m_SelectedImage);
|
||||||
gs_ModifyIndexDeletedIndex = pEditor->m_SelectedImage;
|
pEditor->m_Map.ModifyImageIndex(gs_ModifyIndexDeleted(pEditor->m_SelectedImage));
|
||||||
pEditor->m_Map.ModifyImageIndex(ModifyIndexDeleted);
|
|
||||||
return CUI::POPUP_CLOSE_CURRENT;
|
return CUI::POPUP_CLOSE_CURRENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1444,8 +1443,7 @@ CUI::EPopupMenuFunctionResult CEditor::PopupSound(void *pContext, CUIRect View,
|
||||||
{
|
{
|
||||||
delete pSound;
|
delete pSound;
|
||||||
pEditor->m_Map.m_vpSounds.erase(pEditor->m_Map.m_vpSounds.begin() + pEditor->m_SelectedSound);
|
pEditor->m_Map.m_vpSounds.erase(pEditor->m_Map.m_vpSounds.begin() + pEditor->m_SelectedSound);
|
||||||
gs_ModifyIndexDeletedIndex = pEditor->m_SelectedSound;
|
pEditor->m_Map.ModifySoundIndex(gs_ModifyIndexDeleted(pEditor->m_SelectedSound));
|
||||||
pEditor->m_Map.ModifySoundIndex(ModifyIndexDeleted);
|
|
||||||
return CUI::POPUP_CLOSE_CURRENT;
|
return CUI::POPUP_CLOSE_CURRENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue