diff --git a/src/UIBar.cpp b/src/UIBar.cpp index a28fb58..25737e6 100644 --- a/src/UIBar.cpp +++ b/src/UIBar.cpp @@ -1,7 +1,7 @@ #include "UIBar.h" #include "UIDrawing.h" -UIBar::UIBar(Arduino_GFX *d, UIDimensions dims, UIDecorations *decor, +UIBar::UIBar(Arduino_GFX *d, UIDimensions dims, UIDecorations decor, char *title) : UIElement(d, dims, decor, title) {} @@ -95,10 +95,10 @@ void UIBar::Box() { char legend[5]; sprintf(legend, "%d", k); - this->display->setCursor(x - (CHR_WIDTH(this->decor->textSize) / 2), + this->display->setCursor(x - (CHR_WIDTH(this->decor.textSize) / 2), this->dims.y + this->dims.height + 7 + 2); this->display->setTextColor(WHITE); - this->display->setTextSize(this->decor->textSize); + this->display->setTextSize(this->decor.textSize); this->display->print(legend); } } diff --git a/src/UIBar.h b/src/UIBar.h index cc17014..9321ba7 100644 --- a/src/UIBar.h +++ b/src/UIBar.h @@ -9,7 +9,7 @@ struct UIBar : UIElement { uint32_t value = 0; // current tach value uint8_t range = 0; // define the tach range, ie: 8 for 8000rpm - UIBar(Arduino_GFX *d, UIDimensions dims, UIDecorations *decor, char *title); + UIBar(Arduino_GFX *d, UIDimensions dims, UIDecorations decor, char *title); void Update(char *val); void DrawBar(uint32_t *oldValue, uint32_t *newValue); diff --git a/src/UIComponent.cpp b/src/UIComponent.cpp index 34b9e6a..c426135 100644 --- a/src/UIComponent.cpp +++ b/src/UIComponent.cpp @@ -11,19 +11,16 @@ #define DEBUG 1 UIElement::UIElement() - : display(nullptr), dims(UIDimensions(0, 0, 0, 0)), decor(nullptr) {} + : display(nullptr), dims(UIDimensions(0, 0, 0, 0)), decor(UIDecorations()) {} -UIElement::UIElement(Arduino_GFX *d, UIDimensions dims, UIDecorations *decor, +UIElement::UIElement(Arduino_GFX *d, UIDimensions dims, UIDecorations decor, char *title) : display(d), dims(dims), decor(decor) { strncpy(this->title, title, MAX_TITLE_LEN); } -UIDimensions::UIDimensions(uint16_t x, uint16_t y, uint16_t w, uint16_t h) - : x(x), y(y), width(w), height(h) {} - int16_t UIElement::getContentAreaX0() { - if (this->decor->hasBorder) { + if (this->decor.hasBorder) { return this->dims.x + DEFAULT_BORDER_THICKNESS + DEFAULT_MARGIN; } @@ -31,7 +28,7 @@ int16_t UIElement::getContentAreaX0() { } int16_t UIElement::getContentAreaY0() { - if (this->decor->hasBorder) { + if (this->decor.hasBorder) { return this->dims.y + this->getTitleAreaHeight() + DEFAULT_TITLE_CONTENT_SPACING; } @@ -58,7 +55,7 @@ uint16_t UIElement::getTitleAreaHeight() { int16_t x = 0, y = 0; uint16_t w = 0, h = 0; - this->display->setTextSize(this->decor->titleSize); + this->display->setTextSize(this->decor.titleSize); this->display->getTextBounds((const char *)this->title, 0, 0, &x, &y, &w, &h); return h; @@ -68,7 +65,7 @@ uint16_t UIElement::getTitleAreaWidth() { int16_t x = 0, y = 0; uint16_t w = 0, h = 0; - this->display->setTextSize(this->decor->titleSize); + this->display->setTextSize(this->decor.titleSize); this->display->getTextBounds((const char *)this->title, 0, 0, &x, &y, &w, &h); return w; @@ -81,6 +78,18 @@ void UIElement::SetTitle(char* format, ...) { va_end(args); } +void UIElement::SetUIDecorations(UIDecorations decor) { + this->decor = decor; +} + +void UIElement::SetUIDimensions(UIDimensions dims) { + this->dims = dims; +} + +void UIElement::SetDisplay(Arduino_GFX *d) { + this->display = d; +} + void UIElement::replaceString(char *oldVal, char *newVal) { size_t newValLen = strlen(newVal); size_t oldValLen = strlen(oldVal); @@ -90,16 +99,16 @@ void UIElement::replaceString(char *oldVal, char *newVal) { int16_t y0 = this->getContentAreaY0(); // <- Crashes here // Before here - this->display->setTextSize(this->decor->textSize); + this->display->setTextSize(this->decor.textSize); // If the new value is shorter, delete the extra in advance if (oldValLen > newValLen) { // If the old value is longer than the new value, we have to delete the // extra of the old value, which is whatever starts at the new value len for (std::size_t start = newValLen; start < oldValLen; start++) { - this->display->setCursor(x0 + start * (CHR_WIDTH(this->decor->textSize)), + this->display->setCursor(x0 + start * (CHR_WIDTH(this->decor.textSize)), y0); - this->display->setTextColor(this->decor->bgColor); + this->display->setTextColor(this->decor.bgColor); this->display->print(oldVal[start]); } } @@ -107,16 +116,16 @@ void UIElement::replaceString(char *oldVal, char *newVal) { for (std::size_t k = 0; k < newValLen; k++) { if (k <= oldValLen) { if (oldVal[k] != newVal[k]) { - this->display->setCursor(x0 + k * (CHR_WIDTH(this->decor->textSize)), + this->display->setCursor(x0 + k * (CHR_WIDTH(this->decor.textSize)), y0 + 0); - this->display->setTextColor(this->decor->bgColor); + this->display->setTextColor(this->decor.bgColor); this->display->print(oldVal[k]); } } - this->display->setCursor(x0 + k * (CHR_WIDTH(this->decor->textSize)), + this->display->setCursor(x0 + k * (CHR_WIDTH(this->decor.textSize)), y0 + 0); - this->display->setTextColor(this->decor->fgColor); + this->display->setTextColor(this->decor.fgColor); this->display->print(newVal[k]); } } diff --git a/src/UIComponent.h b/src/UIComponent.h index ffa0e1b..1d45030 100644 --- a/src/UIComponent.h +++ b/src/UIComponent.h @@ -24,14 +24,14 @@ private: public: Arduino_GFX *display; UIDimensions dims; - UIDecorations *decor; + UIDecorations decor; char title[MAX_TITLE_LEN]; uint64_t lastUpdate = 0; uint16_t refreshRate = 0; ComponentType type; UIElement(); - UIElement(Arduino_GFX *d, UIDimensions dims, UIDecorations *decor, + UIElement(Arduino_GFX *d, UIDimensions dims, UIDecorations decor, char *Title); // Children @@ -54,6 +54,9 @@ public: // Setters void SetTitle(char* format, ...); + void SetUIDecorations(UIDecorations decor); + void SetUIDimensions(UIDimensions dims); + void SetDisplay(Arduino_GFX *d); // Positioning void horizontalCenter(UIElement *reference); diff --git a/src/UIDecorations.cpp b/src/UIDecorations.cpp index e86d6d9..05a39d2 100644 --- a/src/UIDecorations.cpp +++ b/src/UIDecorations.cpp @@ -7,3 +7,15 @@ UIDecorations::UIDecorations() {} UIDecorations::UIDecorations(uint16_t bg, uint16_t fg, uint16_t title, uint16_t border, uint8_t titleSize, uint8_t textSize) {} + +UIDecorations& UIDecorations::operator=(const UIDecorations& other) { + this->textSize = other.textSize; + this->titleSize = other.titleSize; + this->titleColor = other.titleColor; + this->borderColor = other.borderColor; + this->bgColor = other.bgColor; + this->fgColor = other.fgColor; + this->hasBorder = other.hasBorder; + + return *this; +} diff --git a/src/UIDecorations.h b/src/UIDecorations.h index 9278704..9633a2c 100644 --- a/src/UIDecorations.h +++ b/src/UIDecorations.h @@ -13,6 +13,7 @@ struct UIDecorations { UIDecorations(); UIDecorations(uint16_t bg, uint16_t fg, uint16_t title, uint16_t border, uint8_t titleSize, uint8_t textSize); + UIDecorations& operator=(const UIDecorations& other); }; #endif diff --git a/src/UIDimensions.cpp b/src/UIDimensions.cpp new file mode 100644 index 0000000..cca7b66 --- /dev/null +++ b/src/UIDimensions.cpp @@ -0,0 +1,13 @@ +#include "UIDimensions.h" + +UIDimensions::UIDimensions(uint16_t x, uint16_t y, uint16_t w, uint16_t h) + : x(x), y(y), width(w), height(h) {} + +UIDimensions& UIDimensions::operator=(const UIDimensions& other) { + this->x = other.x; + this->y = other.y; + this->width = other.width; + this->height = other.height; + + return *this; +} diff --git a/src/UIDimensions.h b/src/UIDimensions.h index fbdf5c3..844f669 100644 --- a/src/UIDimensions.h +++ b/src/UIDimensions.h @@ -8,6 +8,7 @@ struct UIDimensions { uint16_t width = 0, height = 0; UIDimensions(uint16_t x, uint16_t y, uint16_t w, uint16_t h); + UIDimensions& operator=(const UIDimensions& other); }; #endif diff --git a/src/UIDrawing.cpp b/src/UIDrawing.cpp index db59afb..4a3edb8 100644 --- a/src/UIDrawing.cpp +++ b/src/UIDrawing.cpp @@ -51,11 +51,11 @@ void UIElement::placeLeft(UIElement *ref) { void UIElement::drawBox() { // This is the border rectangle - if (this->decor->hasBorder) { + if (this->decor.hasBorder) { for (int k = 0; k < DEFAULT_BORDER_THICKNESS; k++) { this->display->drawRect( this->dims.x + k, this->dims.y + k, this->dims.width - (k * 2), - this->dims.height - (k * 2), this->decor->borderColor); + this->dims.height - (k * 2), this->decor.borderColor); } } @@ -88,7 +88,7 @@ void UIElement::drawBox() { this->display->drawRect(x, y, w, h, BLUE); #endif - if (this->decor->hasBorder) { + if (this->decor.hasBorder) { // Remove the border line behind the title this->display->fillRect(titleTopLeftX - DEFAULT_MARGIN, this->dims.y, titleAreaWidth + DEFAULT_MARGIN, diff --git a/src/UIScreen.cpp b/src/UIScreen.cpp index 02365a8..49cf5da 100644 --- a/src/UIScreen.cpp +++ b/src/UIScreen.cpp @@ -3,10 +3,12 @@ #include "UIDecorations.h" #include "UIDimensions.h" -Curses::Screen::Screen(Arduino_GFX *d, UIDimensions dims, UIDecorations decor) - : display(d), dims(dims), decor(decor) {} +Curses::Screen::Screen(Arduino_GFX *d, UIDimensions dims, + UIDecorations decor) + : display(d), dims(dims), decor(decor) { +} -void Curses::Screen::Setup() { +void Curses::Screen::Setup(const char* title) { // This should always return the first available component this->mainWindowID = WindowPool::Allocate(); if (this->mainWindowID != 0) { @@ -18,4 +20,10 @@ void Curses::Screen::Setup() { Serial2.println("Program interrupted due to setup failure"); } } + + this->mainWindowHandle = WindowPool::GetHandle(this->mainWindowID); + this->mainWindowHandle->SetDisplay(this->display); + this->mainWindowHandle->SetTitle((char*)"%s [%2d]", title, this->mainWindowID); + this->mainWindowHandle->SetUIDecorations(this->decor); + this->mainWindowHandle->SetUIDimensions(this->dims); } diff --git a/src/UIScreen.h b/src/UIScreen.h index 9f072af..6562036 100644 --- a/src/UIScreen.h +++ b/src/UIScreen.h @@ -15,8 +15,9 @@ public: Arduino_GFX *display; int16_t mainWindowID; + UIElement* mainWindowHandle; Screen(Arduino_GFX *d, UIDimensions dims, UIDecorations decor); - void Setup(); + void Setup(const char* title); }; }; // namespace Curses diff --git a/src/UIString.cpp b/src/UIString.cpp index e8f73e4..da76821 100644 --- a/src/UIString.cpp +++ b/src/UIString.cpp @@ -3,12 +3,12 @@ #include "UIComponent.h" #include -UIString::UIString() : UIElement(nullptr, {0, 0, 0, 0}, nullptr, (char *)"") { +UIString::UIString() : UIElement(nullptr, {0, 0, 0, 0}, UIDecorations(), (char *)"") { this->type = STRING; memset(this->value, 0, this->bufferSize); } -UIString::UIString(Arduino_GFX *d, UIDimensions dims, UIDecorations *decor, +UIString::UIString(Arduino_GFX *d, UIDimensions dims, UIDecorations decor, char *title) : UIElement(d, dims, decor, title) { this->type = STRING; diff --git a/src/UIString.h b/src/UIString.h index 883664e..d294cdd 100644 --- a/src/UIString.h +++ b/src/UIString.h @@ -9,7 +9,7 @@ struct UIString : UIElement { char value[bufferSize]; UIString(); - UIString(Arduino_GFX *d, UIDimensions dims, UIDecorations *decor, + UIString(Arduino_GFX *d, UIDimensions dims, UIDecorations decor, char *title); void Update(const char *value); }; diff --git a/src/UITable.cpp b/src/UITable.cpp index 8c4ab22..ea760ed 100644 --- a/src/UITable.cpp +++ b/src/UITable.cpp @@ -6,7 +6,7 @@ #define DEBUG -UITable::UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations *decor, +UITable::UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations decor, int nRows, int nCols, int *colWidths, char *title) : UIElement(gfx, dims, decor, title) { this->type = TABLE; @@ -16,18 +16,18 @@ UITable::UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations *decor, this->colWidths = colWidths; // Default decoration for every cell - this->decor->textSize = 2; + this->decor.textSize = 2; for (int k = 0; k < ROWS * COLUMNS; k++) { - UIDecorations *cellDecor = new UIDecorations(); - cellDecor->textSize = this->decor->textSize; + UIDecorations cellDecor = UIDecorations(); + cellDecor.textSize = this->decor.textSize; UIDimensions dims = {0, 0, 0, 0}; this->tableData[k] = new UIString(display, dims, cellDecor, (char *)""); } - int cellH = CHR_HEIGHT(this->decor->textSize) + CELL_MARGIN; + int cellH = CHR_HEIGHT(this->decor.textSize) + CELL_MARGIN; // Initialize the table dimensions //// Calculate the height @@ -37,7 +37,7 @@ UITable::UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations *decor, //// Calculate the width for (int c = 0; c < this->nColumns; c++) { this->dims.width += - (CHR_WIDTH(this->decor->textSize) + CELL_MARGIN) * this->colWidths[c]; + (CHR_WIDTH(this->decor.textSize) + CELL_MARGIN) * this->colWidths[c]; } this->dims.width += (DEFAULT_BORDER_THICKNESS + DEFAULT_MARGIN) * 2; } @@ -52,7 +52,7 @@ uint16_t UITable::getContentAreaHeight() { * I recon its dynamic enough for now */ void UITable::setup() { - int cellH = CHR_HEIGHT(this->decor->textSize) + CELL_MARGIN; + int cellH = CHR_HEIGHT(this->decor.textSize) + CELL_MARGIN; // Initialize the cells uint16_t yOffset = this->getContentAreaY0(); @@ -60,18 +60,18 @@ void UITable::setup() { int16_t xOffset = this->getContentAreaX0(); for (int c = 0; c < COLUMNS; c++) { this->tableData[r * COLUMNS + c]->refreshRate = this->refreshRate; - this->tableData[r * COLUMNS + c]->decor->textSize = this->decor->textSize; - this->tableData[r * COLUMNS + c]->decor->hasBorder = false; + this->tableData[r * COLUMNS + c]->decor.textSize = this->decor.textSize; + this->tableData[r * COLUMNS + c]->decor.hasBorder = false; this->tableData[r * COLUMNS + c]->dims.x = xOffset + CELL_MARGIN; this->tableData[r * COLUMNS + c]->dims.y = (cellH * r) + yOffset; this->tableData[r * COLUMNS + c]->dims.height = cellH; this->tableData[r * COLUMNS + c]->dims.width = - this->colWidths[c] * (CHR_WIDTH(this->decor->textSize) + CELL_MARGIN); + this->colWidths[c] * (CHR_WIDTH(this->decor.textSize) + CELL_MARGIN); memset(this->tableData[r * COLUMNS + c]->value, 0, this->tableData[r * COLUMNS + c]->bufferSize); xOffset += - this->colWidths[c] * (CHR_WIDTH(this->decor->textSize) + CELL_MARGIN); + this->colWidths[c] * (CHR_WIDTH(this->decor.textSize) + CELL_MARGIN); #ifdef DEBUG this->tableData[r * COLUMNS + c]->drawBox(); diff --git a/src/UITable.h b/src/UITable.h index 8098358..8008918 100644 --- a/src/UITable.h +++ b/src/UITable.h @@ -18,7 +18,7 @@ struct UITable : UIElement { int *colWidths; UIString **tableData; - UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations *decor, int nR, + UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations decor, int nR, int nC, int *cWidth, char *t); // void Update(StandingLine standings[5]);