44#include " ../Configs/Configs.h"
55#include < functional>
66
7- #define IsType (type ) pVar->m_iType == typeid (type).hash_code()
8- #define SetType (type, cond )\
9- {\
10- if (pVar->As <type>()->Map .contains (cond))\
11- pVar->As <type>()->Value = pVar->As <type>()->Map [cond];\
7+ #define IsType (t ) pBase->m_iType == typeid (t).hash_code()
8+
9+ template <class T >
10+ static inline void SetMain (BaseVar*& pBase, int iBind)
11+ {
12+ auto pVar = pBase->As <T>();
13+
14+ if (pVar->Map .contains (iBind))
15+ pVar->Value = pVar->Map [iBind];
1216}
13- #define SetT ( type, cond ) if (IsType(type )) SetType(type, cond)
17+ #define Set ( t, b ) if (IsType(t )) SetMain<t>(pBase, b);
1418
1519static inline void LoopVars (int iBind, std::vector<BaseVar*>& vVars = G::Vars)
1620{
1721 const bool bDefault = iBind == DEFAULT_BIND ;
18- for (auto pVar : vVars)
22+ for (auto pBase : vVars)
1923 {
20- if (pVar ->m_iFlags & (NOSAVE | NOBIND ) && !bDefault)
24+ if (pBase ->m_iFlags & (NOSAVE | NOBIND ) && !bDefault)
2125 continue ;
2226
23- SetT (bool , iBind)
24- else SetT (int , iBind)
25- else SetT (float , iBind)
26- else SetT (IntRange_t, iBind)
27- else SetT (FloatRange_t, iBind)
28- else SetT (std::string, iBind)
29- else SetT (VA_LIST (std::vector<std::pair<std::string, Color_t>>), iBind)
30- else SetT (Color_t, iBind)
31- else SetT (Gradient_t, iBind)
32- else SetT (Vec3, iBind)
33- else SetT (DragBox_t, iBind)
34- else SetT (WindowBox_t, iBind)
27+ Set (bool , iBind)
28+ else Set (int , iBind)
29+ else Set (float , iBind)
30+ else Set (IntRange_t, iBind)
31+ else Set (FloatRange_t, iBind)
32+ else Set (std::string, iBind)
33+ else Set (VA_LIST (std::vector<std::pair<std::string, Color_t>>), iBind)
34+ else Set (Color_t, iBind)
35+ else Set (Gradient_t, iBind)
36+ else Set (Vec3, iBind)
37+ else Set (DragBox_t, iBind)
38+ else Set (WindowBox_t, iBind)
3539 }
3640}
3741
@@ -191,30 +195,33 @@ void CBinds::AddBind(int iBind, Bind_t& tBind)
191195 m_vBinds[iBind] = tBind;
192196}
193197
194- #define HasType (type, bind ) IsType(type) && pVar->As<type>()->contains (bind)
195-
196- #define RemoveType (type, bind )\
197- {\
198- std::unordered_map<int , type> mMap = {};\
199- for (auto it = pVar->As <type>()->Map .begin (); it != pVar->As <type>()->Map .end (); it++)\
200- {\
201- int iKey = it->first ;\
202- auto tVal = it->second ;\
203- if (bind == iKey)\
204- continue ;\
205- else if (bind < iKey)\
206- iKey--;\
207- mMap [iKey] = tVal;\
208- }\
209- pVar->As <type>()->Map = mMap ;\
198+ #define HasType (t, b ) IsType(t) && pBase->As<t>()->contains (b)
199+
200+ template <class T>
201+ static inline void RemoveMain(BaseVar*& pBase, int iBind)
202+ {
203+ auto pVar = pBase->As <T>();
204+
205+ std::unordered_map<int , T> mMap = {};
206+ for (auto it = pVar->Map .begin (); it != pVar->Map .end (); it++)
207+ {
208+ int iKey = it->first ;
209+ auto tVal = it->second ;
210+ if (iBind == iKey)
211+ continue ;
212+ else if (iBind < iKey)
213+ iKey--;
214+ mMap [iKey] = tVal;
215+ }
216+ pVar->Map = mMap ;
210217}
211- #define RemoveT ( type, bind ) if (IsType(type )) RemoveType(type, bind)
218+ #define Remove ( t, b ) if (IsType(t )) RemoveMain<t>(pBase, b);
212219
213220void CBinds::RemoveBind (int iBind, bool bForce)
214221{
215222 if (!bForce)
216223 {
217- for (auto pVar : G::Vars)
224+ for (auto & pBase : G::Vars)
218225 {
219226 if (HasType (bool , iBind)
220227 || HasType (int , iBind)
@@ -258,20 +265,20 @@ void CBinds::RemoveBind(int iBind, bool bForce)
258265 tBind.m_iParent --;
259266 }
260267
261- for (auto pVar : G::Vars)
268+ for (auto & pBase : G::Vars)
262269 {
263- RemoveT (bool , iIndex)
264- else RemoveT (int , iIndex)
265- else RemoveT (float , iIndex)
266- else RemoveT (IntRange_t, iIndex)
267- else RemoveT (FloatRange_t, iIndex)
268- else RemoveT (std::string, iIndex)
269- else RemoveT (VA_LIST (std::vector<std::pair<std::string, Color_t>>), iIndex)
270- else RemoveT (Color_t, iIndex)
271- else RemoveT (Gradient_t, iIndex)
272- else RemoveT (Vec3, iIndex)
273- else RemoveT (DragBox_t, iIndex)
274- else RemoveT (WindowBox_t, iIndex)
270+ Remove (bool , iIndex)
271+ else Remove (int , iIndex)
272+ else Remove (float , iIndex)
273+ else Remove (IntRange_t, iIndex)
274+ else Remove (FloatRange_t, iIndex)
275+ else Remove (std::string, iIndex)
276+ else Remove (VA_LIST (std::vector<std::pair<std::string, Color_t>>), iIndex)
277+ else Remove (Color_t, iIndex)
278+ else Remove (Gradient_t, iIndex)
279+ else Remove (Vec3, iIndex)
280+ else Remove (DragBox_t, iIndex)
281+ else Remove (WindowBox_t, iIndex)
275282 }
276283 };
277284 searchBinds (iBind);
@@ -306,4 +313,67 @@ bool CBinds::WillBeEnabled(int iBind)
306313 iBind = tBind.m_iParent ;
307314 }
308315 return true ;
316+ }
317+
318+ template <class T >
319+ static inline void SwapMain (BaseVar*& pBase, int iBind1, int iBind2)
320+ {
321+ auto pVar = pBase->As <T>();
322+
323+ bool bHas1 = pVar->contains (iBind1), bHas2 = pVar->contains (iBind2);
324+ if (bHas1 && bHas2)
325+ {
326+ auto & tVal1 = pVar->Map [iBind1];
327+ auto & tVal2 = pVar->Map [iBind2];
328+ auto tTemp = tVal1;
329+ tVal1 = tVal2;
330+ tVal2 = tTemp;
331+ }
332+ else if (bHas1)
333+ {
334+ pVar->Map [iBind2] = pVar->Map [iBind1];
335+ pVar->Map .erase (iBind1);
336+ }
337+ else if (bHas2)
338+ {
339+ pVar->Map [iBind1] = pVar->Map [iBind2];
340+ pVar->Map .erase (iBind2);
341+ }
342+ }
343+ #define Swap (t, i1, i2 ) if (IsType(t)) SwapMain<t>(pBase, i1, i2);
344+
345+ void CBinds::Move (int i1, int i2)
346+ {
347+ auto & tBind1 = m_vBinds[i1];
348+ auto & tBind2 = m_vBinds[i2];
349+ auto tTemp = tBind1;
350+ tBind1 = tBind2;
351+ tBind2 = tTemp;
352+
353+ std::vector<Bind_t*> vBinds1, vBinds2;
354+ for (auto & tBind : m_vBinds)
355+ {
356+ if (tBind.m_iParent == i1)
357+ vBinds1.push_back (&tBind);
358+ else if (tBind.m_iParent == i2)
359+ vBinds2.push_back (&tBind);
360+ }
361+ std::for_each (vBinds1.begin (), vBinds1.end (), [&](auto pBind) { pBind->m_iParent = i2; });
362+ std::for_each (vBinds2.begin (), vBinds2.end (), [&](auto pBind) { pBind->m_iParent = i1; });
363+
364+ for (auto & pBase : G::Vars)
365+ {
366+ Swap (bool , i1, i2)
367+ else Swap (int , i1, i2)
368+ else Swap (float , i1, i2)
369+ else Swap (IntRange_t, i1, i2)
370+ else Swap (FloatRange_t, i1, i2)
371+ else Swap (std::string, i1, i2)
372+ else Swap (VA_LIST (std::vector<std::pair<std::string, Color_t>>), i1, i2)
373+ else Swap (Color_t, i1, i2)
374+ else Swap (Gradient_t, i1, i2)
375+ else Swap (Vec3, i1, i2)
376+ else Swap (DragBox_t, i1, i2)
377+ else Swap (WindowBox_t, i1, i2)
378+ }
309379}
0 commit comments