1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2026-08-18 18:32:30 +02:00

Fix for drawButtonCenter()

This commit is contained in:
Norbert Walter
2026-02-28 23:14:38 +00:00
parent 75724b29eb
commit 2b2d2e3fd6
+10 -18
View File
@@ -487,41 +487,33 @@ std::vector<String> wordwrap(String &line, uint16_t maxwidth) {
// Draw centered text // Draw centered text
void drawTextCenter(int16_t cx, int16_t cy, String text) { void drawTextCenter(int16_t cx, int16_t cy, String text) {
#ifdef DISPLAY_ST7796
uint16_t w = getdisplay().textWidth(text);
uint16_t h = getdisplay().fontHeight();
getdisplay().setCursor(cx - static_cast<int16_t>(w / 2), cy + static_cast<int16_t>(h / 2));
getdisplay().print(text);
#else
int16_t x1, y1; int16_t x1, y1;
uint16_t w, h; uint16_t w, h;
getdisplay().getTextBounds(text, 0, 150, &x1, &y1, &w, &h); displayGetTextBounds(text, 0, 0, &x1, &y1, &w, &h);
getdisplay().setCursor(cx - w / 2, cy + h / 2); int16_t cursorX = cx - (x1 + static_cast<int16_t>(w / 2));
int16_t cursorY = cy - (y1 + static_cast<int16_t>(h / 2));
getdisplay().setCursor(cursorX, cursorY);
getdisplay().print(text); getdisplay().print(text);
#endif
} }
// Draw centered botton with centered text // Draw centered botton with centered text
void drawButtonCenter(int16_t cx, int16_t cy, int8_t sx, int8_t sy, String text, uint16_t fg, uint16_t bg, bool inverted) { void drawButtonCenter(int16_t cx, int16_t cy, int8_t sx, int8_t sy, String text, uint16_t fg, uint16_t bg, bool inverted) {
uint16_t w, h;
#ifdef DISPLAY_ST7796
w = getdisplay().textWidth(text);
h = getdisplay().fontHeight();
#else
int16_t x1, y1; int16_t x1, y1;
getdisplay().getTextBounds(text, cx, cy, &x1, &y1, &w, &h); // Find text center uint16_t w, h;
#endif displayGetTextBounds(text, 0, 0, &x1, &y1, &w, &h);
int16_t cursorX = cx - (x1 + static_cast<int16_t>(w / 2));
int16_t cursorY = cy - (y1 + static_cast<int16_t>(h / 2));
//getdisplay().drawPixel(cx, cy, fg); // Debug pixel for center position //getdisplay().drawPixel(cx, cy, fg); // Debug pixel for center position
if (inverted) { if (inverted) {
getdisplay().fillRoundRect(cx - sx / 2, cy - sy / 2, sx, sy, 5, fg); // Draw button getdisplay().fillRoundRect(cx - sx / 2, cy - sy / 2, sx, sy, 5, fg); // Draw button
getdisplay().setTextColor(bg); getdisplay().setTextColor(bg);
getdisplay().setCursor(cx - w/2, cy + h/2); // Set cursor to center getdisplay().setCursor(cursorX, cursorY); // Set cursor to center
getdisplay().print(text); // Draw text getdisplay().print(text); // Draw text
} }
else{ else{
getdisplay().drawRoundRect(cx - sx / 2, cy - sy / 2, sx, sy, 5, fg); // Draw button getdisplay().drawRoundRect(cx - sx / 2, cy - sy / 2, sx, sy, 5, fg); // Draw button
getdisplay().setTextColor(fg); getdisplay().setTextColor(fg);
getdisplay().setCursor(cx - w/2, cy + h/2); // Set cursor to center getdisplay().setCursor(cursorX, cursorY); // Set cursor to center
getdisplay().print(text); // Draw text getdisplay().print(text); // Draw text
} }
} }