From 91b2a69d21e154a46e21f994c23f6214f89b31e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikael=20Degerf=C3=A4lt?= Date: Mon, 24 Jun 2019 00:17:27 +0200 Subject: [PATCH] Fixed text alignment for sub values with units --- NuEVI/menu.cpp | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/NuEVI/menu.cpp b/NuEVI/menu.cpp index 14cfb45..afde514 100644 --- a/NuEVI/menu.cpp +++ b/NuEVI/menu.cpp @@ -190,11 +190,25 @@ static const char* numToString(int16_t value, char* dest, bool plusSign = false) return dest; } -static void plotSubOption(const char* label) { - display.setTextSize(2); - int x_pos = 96-strlen(label)*6; - display.setCursor(x_pos,33); - display.println(label); +static void plotSubOption(const char* label, const char* unit = nullptr) { + int text_x, unit_x; + int label_pixel_width = strlen(label)*12; + + if(unit == nullptr) { + text_x = 96 - (label_pixel_width/2); + } else { + int unit_pixel_width = strlen(unit)*6; + int halfSum = (label_pixel_width + unit_pixel_width)/2; + text_x = 96 - halfSum; + unit_x = 96 + halfSum - unit_pixel_width; + display.setCursor(unit_x,40); + display.setTextSize(1); + display.println(unit); + } + + display.setTextSize(2); + display.setCursor(text_x,33); + display.println(label); } static bool drawSubMenu(const MenuPage *page) { @@ -209,16 +223,10 @@ static bool drawSubMenu(const MenuPage *page) { const MenuEntrySub* sub = (const MenuEntrySub*)subEntry; sub->getSubTextFunc(*sub, buffer, &labelPtr); - // If ECustom flag is set, we assume that the getSubTextFunc + // If EMenuEntryCustom flag is set, we assume that the getSubTextFunc // rendered by it self. if( !(sub->flags & EMenuEntryCustom)) { - plotSubOption(buffer); - if(labelPtr != nullptr) { - // TODO: handle this better, we should center text + label - display.setCursor(105,40); - display.setTextSize(1); - display.println(labelPtr); - } + plotSubOption(buffer, labelPtr); } } break;