Use lambdas for editor index modify and sort functions

For cleaner code with less global state variables.
This commit is contained in:
Robert Müller 2023-07-02 12:15:20 +02:00
parent 7f100e2620
commit 1acb94ffbf
7 changed files with 55 additions and 72 deletions

View file

@ -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()
{
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;
gs_pSortedIndex = new int[vpTemp.size()];
const std::vector<CEditorImage *> vpTemp = m_Map.m_vpImages;
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 NewIndex = 0; NewIndex < m_Map.m_vpImages.size(); NewIndex++)
{
if(vpTemp[OldIndex] == m_Map.m_vpImages[NewIndex])
{
gs_pSortedIndex[OldIndex] = NewIndex;
vSortedIndex[OldIndex] = NewIndex;
break;
}
}
}
m_Map.ModifyImageIndex(ModifySortedIndex);
delete[] gs_pSortedIndex;
gs_pSortedIndex = nullptr;
m_Map.ModifyImageIndex([vSortedIndex](int *pIndex) {
if(*pIndex >= 0)
*pIndex = vSortedIndex[*pIndex];
});
}
}

View file

@ -21,15 +21,14 @@
#include <chrono>
#include <deque>
#include <functional>
#include <map>
#include <string>
#include <vector>
using namespace std::chrono_literals;
typedef void (*INDEX_MODIFY_FUNC)(int *pIndex);
// CRenderTools m_RenderTools;
typedef std::function<void(int *pIndex)> FIndexModifyFunction;
// CEditor SPECIFIC
enum
@ -163,9 +162,9 @@ public:
virtual void Render(bool Tileset = false) {}
virtual CUI::EPopupMenuFunctionResult RenderProperties(CUIRect *pToolbox) { return CUI::POPUP_KEEP_OPEN; }
virtual void ModifyImageIndex(INDEX_MODIFY_FUNC pfnFunc) {}
virtual void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC pfnFunc) {}
virtual void ModifySoundIndex(INDEX_MODIFY_FUNC pfnFunc) {}
virtual void ModifyImageIndex(FIndexModifyFunction pfnFunc) {}
virtual void ModifyEnvelopeIndex(FIndexModifyFunction pfnFunc) {}
virtual void ModifySoundIndex(FIndexModifyFunction pfnFunc) {}
virtual CLayer *Duplicate() const = 0;
@ -242,19 +241,19 @@ public:
void AddLayer(CLayer *pLayer);
void ModifyImageIndex(INDEX_MODIFY_FUNC Func)
void ModifyImageIndex(FIndexModifyFunction Func)
{
for(auto &pLayer : m_vpLayers)
pLayer->ModifyImageIndex(Func);
}
void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC Func)
void ModifyEnvelopeIndex(FIndexModifyFunction Func)
{
for(auto &pLayer : m_vpLayers)
pLayer->ModifyEnvelopeIndex(Func);
}
void ModifySoundIndex(INDEX_MODIFY_FUNC Func)
void ModifySoundIndex(FIndexModifyFunction Func)
{
for(auto &pLayer : m_vpLayers)
pLayer->ModifySoundIndex(Func);
@ -424,21 +423,21 @@ public:
m_vpGroups.erase(m_vpGroups.begin() + Index);
}
void ModifyImageIndex(INDEX_MODIFY_FUNC pfnFunc)
void ModifyImageIndex(FIndexModifyFunction pfnFunc)
{
OnModify();
for(auto &pGroup : m_vpGroups)
pGroup->ModifyImageIndex(pfnFunc);
}
void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC pfnFunc)
void ModifyEnvelopeIndex(FIndexModifyFunction pfnFunc)
{
OnModify();
for(auto &pGroup : m_vpGroups)
pGroup->ModifyEnvelopeIndex(pfnFunc);
}
void ModifySoundIndex(INDEX_MODIFY_FUNC pfnFunc)
void ModifySoundIndex(FIndexModifyFunction pfnFunc)
{
OnModify();
for(auto &pGroup : m_vpGroups)
@ -620,8 +619,8 @@ public:
};
static CUI::EPopupMenuFunctionResult RenderCommonProperties(SCommonPropState &State, CEditor *pEditor, CUIRect *pToolbox, std::vector<CLayerTiles *> &vpLayers);
void ModifyImageIndex(INDEX_MODIFY_FUNC pfnFunc) override;
void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC pfnFunc) override;
void ModifyImageIndex(FIndexModifyFunction pfnFunc) override;
void ModifyEnvelopeIndex(FIndexModifyFunction pfnFunc) override;
void PrepareForSave();
@ -676,8 +675,8 @@ public:
CUI::EPopupMenuFunctionResult RenderProperties(CUIRect *pToolbox) override;
void ModifyImageIndex(INDEX_MODIFY_FUNC pfnFunc) override;
void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC pfnFunc) override;
void ModifyImageIndex(FIndexModifyFunction pfnFunc) override;
void ModifyEnvelopeIndex(FIndexModifyFunction pfnFunc) override;
void GetSize(float *pWidth, float *pHeight) override;
CLayer *Duplicate() const override;
@ -1523,8 +1522,8 @@ public:
CUI::EPopupMenuFunctionResult RenderProperties(CUIRect *pToolbox) override;
void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC pfnFunc) override;
void ModifySoundIndex(INDEX_MODIFY_FUNC pfnFunc) override;
void ModifyEnvelopeIndex(FIndexModifyFunction pfnFunc) override;
void ModifySoundIndex(FIndexModifyFunction pfnFunc) override;
CLayer *Duplicate() const override;

View file

@ -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)
{
CEditorMap NewMap;
@ -1030,14 +1023,15 @@ bool CEditor::Append(const char *pFileName, int StorageType)
return false;
// modify indices
gs_ModifyAddAmount = m_Map.m_vpImages.size();
NewMap.ModifyImageIndex(ModifyAdd);
gs_ModifyAddAmount = m_Map.m_vpSounds.size();
NewMap.ModifySoundIndex(ModifyAdd);
gs_ModifyAddAmount = m_Map.m_vpEnvelopes.size();
NewMap.ModifyEnvelopeIndex(ModifyAdd);
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)

View file

@ -250,12 +250,12 @@ CUI::EPopupMenuFunctionResult CLayerQuads::RenderProperties(CUIRect *pToolBox)
return CUI::POPUP_KEEP_OPEN;
}
void CLayerQuads::ModifyImageIndex(INDEX_MODIFY_FUNC Func)
void CLayerQuads::ModifyImageIndex(FIndexModifyFunction Func)
{
Func(&m_Image);
}
void CLayerQuads::ModifyEnvelopeIndex(INDEX_MODIFY_FUNC Func)
void CLayerQuads::ModifyEnvelopeIndex(FIndexModifyFunction Func)
{
for(auto &Quad : m_vQuads)
{

View file

@ -214,12 +214,12 @@ CUI::EPopupMenuFunctionResult CLayerSounds::RenderProperties(CUIRect *pToolBox)
return CUI::POPUP_KEEP_OPEN;
}
void CLayerSounds::ModifySoundIndex(INDEX_MODIFY_FUNC Func)
void CLayerSounds::ModifySoundIndex(FIndexModifyFunction Func)
{
Func(&m_Sound);
}
void CLayerSounds::ModifyEnvelopeIndex(INDEX_MODIFY_FUNC Func)
void CLayerSounds::ModifyEnvelopeIndex(FIndexModifyFunction Func)
{
for(auto &Source : m_vSources)
{

View file

@ -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;
Func(&m_Image);
@ -1089,7 +1089,7 @@ void CLayerTiles::ModifyImageIndex(INDEX_MODIFY_FUNC Func)
m_Texture.Invalidate();
}
void CLayerTiles::ModifyEnvelopeIndex(INDEX_MODIFY_FUNC Func)
void CLayerTiles::ModifyEnvelopeIndex(FIndexModifyFunction Func)
{
Func(&m_ColorEnv);
}

View file

@ -1285,14 +1285,14 @@ CUI::EPopupMenuFunctionResult CEditor::PopupPoint(void *pContext, CUIRect View,
return CUI::POPUP_KEEP_OPEN;
}
static int gs_ModifyIndexDeletedIndex;
static void ModifyIndexDeleted(int *pIndex)
{
if(*pIndex == gs_ModifyIndexDeletedIndex)
static const auto &&gs_ModifyIndexDeleted = [](int DeletedIndex) {
return [DeletedIndex](int *pIndex) {
if(*pIndex == DeletedIndex)
*pIndex = -1;
else if(*pIndex > gs_ModifyIndexDeletedIndex)
else if(*pIndex > DeletedIndex)
*pIndex = *pIndex - 1;
}
};
};
CUI::EPopupMenuFunctionResult CEditor::PopupImage(void *pContext, CUIRect View, bool Active)
{
@ -1377,8 +1377,7 @@ CUI::EPopupMenuFunctionResult CEditor::PopupImage(void *pContext, CUIRect View,
{
delete pImg;
pEditor->m_Map.m_vpImages.erase(pEditor->m_Map.m_vpImages.begin() + pEditor->m_SelectedImage);
gs_ModifyIndexDeletedIndex = pEditor->m_SelectedImage;
pEditor->m_Map.ModifyImageIndex(ModifyIndexDeleted);
pEditor->m_Map.ModifyImageIndex(gs_ModifyIndexDeleted(pEditor->m_SelectedImage));
return CUI::POPUP_CLOSE_CURRENT;
}
@ -1444,8 +1443,7 @@ CUI::EPopupMenuFunctionResult CEditor::PopupSound(void *pContext, CUIRect View,
{
delete pSound;
pEditor->m_Map.m_vpSounds.erase(pEditor->m_Map.m_vpSounds.begin() + pEditor->m_SelectedSound);
gs_ModifyIndexDeletedIndex = pEditor->m_SelectedSound;
pEditor->m_Map.ModifySoundIndex(ModifyIndexDeleted);
pEditor->m_Map.ModifySoundIndex(gs_ModifyIndexDeleted(pEditor->m_SelectedSound));
return CUI::POPUP_CLOSE_CURRENT;
}