From a41ac82389e7d71ae2576832db9f677cfbcf5246 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Wed, 21 May 2025 18:10:50 +0100 Subject: [PATCH] Some layout changes --- lib/cursesAtHome/src/UIBar.cpp | 7 ++++--- lib/cursesAtHome/src/UIComponent.h | 4 +++- lib/cursesAtHome/src/UIDrawing.cpp | 21 +++++++++++++++++---- src/main.cpp | 12 +++++++----- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/lib/cursesAtHome/src/UIBar.cpp b/lib/cursesAtHome/src/UIBar.cpp index 1c3032d..a28fb58 100644 --- a/lib/cursesAtHome/src/UIBar.cpp +++ b/lib/cursesAtHome/src/UIBar.cpp @@ -5,7 +5,7 @@ UIBar::UIBar(Arduino_GFX *d, UIDimensions dims, UIDecorations *decor, char *title) : UIElement(d, dims, decor, title) {} -void UIBar::Update(char* v) { +void UIBar::Update(char *v) { unsigned long time = millis(); if ((time - this->lastUpdate) <= this->refreshRate) { return; @@ -92,11 +92,12 @@ void UIBar::Box() { this->display->setTextColor(RED); this->display->drawFastVLine(x, this->dims.y + this->dims.height, 7, RED); - this->display->setCursor(x, this->dims.y + this->dims.height + 7 + 2); - this->display->setTextColor(WHITE); char legend[5]; sprintf(legend, "%d", k); + 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->print(legend); } diff --git a/lib/cursesAtHome/src/UIComponent.h b/lib/cursesAtHome/src/UIComponent.h index 0d07855..5be5b2d 100644 --- a/lib/cursesAtHome/src/UIComponent.h +++ b/lib/cursesAtHome/src/UIComponent.h @@ -40,9 +40,11 @@ struct UIElement { uint16_t getTitleAreaWidth(); // Positioning - void horizontalCenter(); + void horizontalCenter(UIElement *reference); + void verticalCenter(UIElement *reference); void placeBelow(UIElement *reference); void placeRight(UIElement *reference); + void placeLeft(UIElement *reference); // Drawing void drawBox(); diff --git a/lib/cursesAtHome/src/UIDrawing.cpp b/lib/cursesAtHome/src/UIDrawing.cpp index 9b73376..66e5b6a 100644 --- a/lib/cursesAtHome/src/UIDrawing.cpp +++ b/lib/cursesAtHome/src/UIDrawing.cpp @@ -1,5 +1,6 @@ #include "UIDrawing.h" #include "Arduino_GFX.h" +#include "HardwareSerial.h" #include "UIComponent.h" #include @@ -7,7 +8,7 @@ * On the placement functions, change 5 to a variable or something */ -#define DEBUG +// #define DEBUG uint16_t calculateHeight(int16_t titleSize, int16_t textSize, int16_t nLines) { return CHR_HEIGHT(titleSize) + CHR_HEIGHT(textSize) + @@ -20,9 +21,17 @@ uint16_t calculateWidth(int16_t textSize, int16_t nChars) { (2 * (DEFAULT_MARGIN + DEFAULT_BORDER_THICKNESS)); } -void UIElement::horizontalCenter() { - uint16_t scrW = this->display->width(); - this->dims.x = (scrW - this->dims.width) / 2; +void UIElement::horizontalCenter(UIElement *ref) { + uint16_t middlePoint = 0; + + if (ref != nullptr) { + Serial2.println("We are here indeed!"); + middlePoint = ref->dims.x + (ref->dims.width / 2); + } else { + middlePoint = this->display->width() / 2; + } + + this->dims.x = middlePoint - (this->dims.width / 2); } // We can add a mode of alignment here, center, left, right, whatever @@ -36,6 +45,10 @@ void UIElement::placeRight(UIElement *ref) { this->dims.x = ref->dims.x + ref->dims.width + 5; } +void UIElement::placeLeft(UIElement *ref) { + this->dims.x = ref->dims.x - this->dims.width - 5; +} + void UIElement::drawBox() { // This is the border rectangle if (this->decor->hasBorder) { diff --git a/src/main.cpp b/src/main.cpp index 48066cb..2993e5e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -113,8 +113,9 @@ void setup() { // Setup the rpmBar box rpmBarDecor->textSize = 3; rpmBar.dims.height = 50; - rpmBar.dims.width = 350; - rpmBar.horizontalCenter(); + rpmBar.dims.width = 450; + rpmBar.horizontalCenter(nullptr); + rpmBar.dims.x += 10; rpmBar.range = 8; rpmBar.refreshRate = 50; ui->barTacho = &rpmBar; @@ -126,8 +127,8 @@ void setup() { rpmText.dims.height = calculateHeight(rpmTextDecor->titleSize, rpmTextDecor->textSize, 1); rpmText.dims.width = calculateWidth(rpmTextDecor->textSize, 5); - rpmText.horizontalCenter(); rpmText.placeBelow(&rpmBar); + rpmText.horizontalCenter(&rpmBar); rpmText.dims.y += 40; // We gotta fix how the bar declares its height instead // of doing this here rpmText.refreshRate = 100; @@ -154,7 +155,7 @@ void setup() { calculateHeight(gearTextDecor->titleSize, gearTextDecor->textSize, 1); gearText.dims.width = calculateWidth(gearTextDecor->textSize, 2); gearText.dims.y = rpmText.dims.height + 5; - gearText.placeBelow(&rpmText); + gearText.placeLeft(&rpmText); ui->digiGear = &gearText; ui->digiGear->drawBox(); ui->digiGear->Update("-"); @@ -164,7 +165,8 @@ void setup() { lapDelta.dims.height = calculateHeight(lapDeltaDecor->titleSize, lapDeltaDecor->textSize, 1); lapDelta.dims.width = calculateWidth(lapDeltaDecor->textSize, 6); - lapDelta.placeBelow(&gearText); + lapDelta.placeBelow(&rpmText); + lapDelta.horizontalCenter(&rpmText); ui->lapDelta = &lapDelta; ui->lapDelta->drawBox(); ui->lapDelta->Update("--.-");