Changing this to a more dynamic index allocation (need to add window destr)

This commit is contained in:
2025-11-12 23:31:25 +00:00
parent 12209f9789
commit 9750951dfd
2 changed files with 41 additions and 8 deletions
+31 -7
View File
@@ -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]);
}
}
}
}
+10 -1
View File
@@ -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