diff --git a/src/UIComponent.cpp b/src/UIComponent.cpp index c426135..4c74fb2 100644 --- a/src/UIComponent.cpp +++ b/src/UIComponent.cpp @@ -162,23 +162,47 @@ std::uint8_t UIElement::ChildrenCount() { namespace WindowPool { UIElement pool[MAX_WINDOWS]; - int16_t poolIndex = 0; + bool inUse[MAX_WINDOWS] = {false}; + // int16_t poolIndex = 0; + + static inline int16_t getNextIndex() { + for (int k = 0; k < MAX_WINDOWS; k++) { + if (!inUse[k]) { + return k; + } + } + + return -1; + } int16_t Allocate() { - if (poolIndex >= MAX_WINDOWS) { + int16_t nextIndex = getNextIndex(); + if (nextIndex < 0) { Serial2.println("Failed to allocate comp: poolIndex exceeds MAX_WINDOWS"); - Serial2.print( poolIndex); + Serial2.print( nextIndex); Serial2.print( " >= "); - Serial2.print( MAX_WINDOWS); + Serial2.println( MAX_WINDOWS); return -1; } - return poolIndex; - poolIndex++; + // Mark it as in use + inUse[nextIndex] = true; + + return nextIndex; } UIElement* GetHandle(int16_t id) { - return &pool[poolIndex]; + return &pool[id]; + } + + void PrintInUse() { + for (int k = 0; k < MAX_WINDOWS; k++) { + if (inUse[k]) { + Serial2.print(k); + Serial2.print(" : "); + Serial2.println(inUse[k]); + } + } } } diff --git a/src/UIComponent.h b/src/UIComponent.h index 1d45030..fbf0af1 100644 --- a/src/UIComponent.h +++ b/src/UIComponent.h @@ -73,10 +73,19 @@ public: namespace WindowPool { extern UIElement pool[MAX_WINDOWS]; - extern int16_t poolIndex; + // extern int16_t poolIndex; + extern bool inUse[MAX_WINDOWS]; + + // I need to add a list of available IDs + // And maybe a list of unavailable IDs or just create a custome type + // then make a list of that custom and have a bool saying if its available or not + // Although having a list of available IDs is best int16_t Allocate(); UIElement* GetHandle(int16_t index); + + // Debugging + void PrintInUse(); }; #endif