Fixed text alignment for sub values with units

This commit is contained in:
Mikael Degerfält 2019-06-24 00:17:27 +02:00
parent d2f3bba544
commit 91b2a69d21

View file

@ -190,11 +190,25 @@ static const char* numToString(int16_t value, char* dest, bool plusSign = false)
return dest; return dest;
} }
static void plotSubOption(const char* label) { static void plotSubOption(const char* label, const char* unit = nullptr) {
display.setTextSize(2); int text_x, unit_x;
int x_pos = 96-strlen(label)*6; int label_pixel_width = strlen(label)*12;
display.setCursor(x_pos,33);
display.println(label); 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) { static bool drawSubMenu(const MenuPage *page) {
@ -209,16 +223,10 @@ static bool drawSubMenu(const MenuPage *page) {
const MenuEntrySub* sub = (const MenuEntrySub*)subEntry; const MenuEntrySub* sub = (const MenuEntrySub*)subEntry;
sub->getSubTextFunc(*sub, buffer, &labelPtr); 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. // rendered by it self.
if( !(sub->flags & EMenuEntryCustom)) { if( !(sub->flags & EMenuEntryCustom)) {
plotSubOption(buffer); plotSubOption(buffer, labelPtr);
if(labelPtr != nullptr) {
// TODO: handle this better, we should center text + label
display.setCursor(105,40);
display.setTextSize(1);
display.println(labelPtr);
}
} }
} }
break; break;