Skip to content

Commit

Permalink
Use lambdas for editor index modify and sort functions
Browse files Browse the repository at this point in the history
For cleaner code with less global state variables.
  • Loading branch information
Robyt3 committed Jul 16, 2023
1 parent 7f100e2 commit 1acb94f
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 72 deletions.
34 changes: 13 additions & 21 deletions src/game/editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
}
}

Expand Down
35 changes: 17 additions & 18 deletions src/game/editor/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -163,9 +162,9 @@ class CLayer
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;

Expand Down Expand Up @@ -242,19 +241,19 @@ class CLayerGroup

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);
Expand Down Expand Up @@ -424,21 +423,21 @@ class CEditorMap
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)
Expand Down Expand Up @@ -620,8 +619,8 @@ class CLayerTiles : public CLayer
};
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();

Expand Down Expand Up @@ -676,8 +675,8 @@ class CLayerQuads : public CLayer

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;
Expand Down Expand Up @@ -1523,8 +1522,8 @@ class CLayerSounds : public CLayer

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;

Expand Down
24 changes: 9 additions & 15 deletions src/game/editor/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/game/editor/layer_quads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions src/game/editor/layer_sounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions src/game/editor/layer_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1081,15 +1081,15 @@ 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);
if(m_Image != ImgBefore)
m_Texture.Invalidate();
}

void CLayerTiles::ModifyEnvelopeIndex(INDEX_MODIFY_FUNC Func)
void CLayerTiles::ModifyEnvelopeIndex(FIndexModifyFunction Func)
{
Func(&m_ColorEnv);
}
Expand Down
22 changes: 10 additions & 12 deletions src/game/editor/popups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*pIndex = -1;
else if(*pIndex > gs_ModifyIndexDeletedIndex)
*pIndex = *pIndex - 1;
}
static const auto &&gs_ModifyIndexDeleted = [](int DeletedIndex) {
return [DeletedIndex](int *pIndex) {
if(*pIndex == DeletedIndex)
*pIndex = -1;
else if(*pIndex > DeletedIndex)
*pIndex = *pIndex - 1;
};
};

CUI::EPopupMenuFunctionResult CEditor::PopupImage(void *pContext, CUIRect View, bool Active)
{
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 1acb94f

Please sign in to comment.