From 1acb94ffbf20d1654c576c8a7942ff32edb3258c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 2 Jul 2023 12:15:20 +0200 Subject: [PATCH] Use lambdas for editor index modify and sort functions For cleaner code with less global state variables. --- src/game/editor/editor.cpp | 34 ++++++++++++------------------- src/game/editor/editor.h | 35 ++++++++++++++++---------------- src/game/editor/io.cpp | 24 ++++++++-------------- src/game/editor/layer_quads.cpp | 4 ++-- src/game/editor/layer_sounds.cpp | 4 ++-- src/game/editor/layer_tiles.cpp | 4 ++-- src/game/editor/popups.cpp | 22 +++++++++----------- 7 files changed, 55 insertions(+), 72 deletions(-) diff --git a/src/game/editor/editor.cpp b/src/game/editor/editor.cpp index 6ec2e96da..fe85dca93 100644 --- a/src/game/editor/editor.cpp +++ b/src/game/editor/editor.cpp @@ -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 vpTemp = m_Map.m_vpImages; - gs_pSortedIndex = new int[vpTemp.size()]; + const std::vector vpTemp = m_Map.m_vpImages; + std::vector 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]; + }); } } diff --git a/src/game/editor/editor.h b/src/game/editor/editor.h index 49b3a639e..bf39e27f8 100644 --- a/src/game/editor/editor.h +++ b/src/game/editor/editor.h @@ -21,15 +21,14 @@ #include #include +#include #include #include #include using namespace std::chrono_literals; -typedef void (*INDEX_MODIFY_FUNC)(int *pIndex); - -// CRenderTools m_RenderTools; +typedef std::function 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 &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; diff --git a/src/game/editor/io.cpp b/src/game/editor/io.cpp index f885a636a..a8eefca30 100644 --- a/src/game/editor/io.cpp +++ b/src/game/editor/io.cpp @@ -1010,13 +1010,6 @@ void CEditorMap::PerformSanityChecks(const std::function= 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) diff --git a/src/game/editor/layer_quads.cpp b/src/game/editor/layer_quads.cpp index 27dc484f4..8482db09d 100644 --- a/src/game/editor/layer_quads.cpp +++ b/src/game/editor/layer_quads.cpp @@ -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) { diff --git a/src/game/editor/layer_sounds.cpp b/src/game/editor/layer_sounds.cpp index 1f20f1def..3048ac8d2 100644 --- a/src/game/editor/layer_sounds.cpp +++ b/src/game/editor/layer_sounds.cpp @@ -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) { diff --git a/src/game/editor/layer_tiles.cpp b/src/game/editor/layer_tiles.cpp index 0910c315a..f5cf66c67 100644 --- a/src/game/editor/layer_tiles.cpp +++ b/src/game/editor/layer_tiles.cpp @@ -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); } diff --git a/src/game/editor/popups.cpp b/src/game/editor/popups.cpp index 7584f5a97..146e3dcd7 100644 --- a/src/game/editor/popups.cpp +++ b/src/game/editor/popups.cpp @@ -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) { @@ -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; }