From d792de9bed8d84f0252b6d2ee508ec8817d6557c Mon Sep 17 00:00:00 2001 From: Nailson Date: Tue, 6 Jun 2017 23:51:00 -0300 Subject: [PATCH] Add text ellipsis for tooltips. Closes #196 --- source/map_drawer.cpp | 26 +++++++++++++++++++------- source/map_drawer.h | 10 +++++++++- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/source/map_drawer.cpp b/source/map_drawer.cpp index d21391e9..04ec4d54 100644 --- a/source/map_drawer.cpp +++ b/source/map_drawer.cpp @@ -1524,8 +1524,6 @@ void MapDrawer::DrawBrushIndicator(int x, int y, Brush* brush, uint8_t r, uint8_ void MapDrawer::DrawTooltips() { - const int MAX_LINE_CHARS = 40; - for(std::vector::const_iterator it = tooltips.begin(); it != tooltips.end(); ++it) { MapTooltip* tooltip = (*it); const char* text = tooltip->text.c_str(); @@ -1533,17 +1531,22 @@ void MapDrawer::DrawTooltips() float width = 2.0f; float height = 14.0f; int char_count = 0; + int line_char_count = 0; for(const char* c = text; *c != '\0'; c++) { - if(*c == '\n' || (char_count >= MAX_LINE_CHARS && *c == ' ')) { + if(*c == '\n' || (line_char_count >= MapTooltip::MAX_CHARS_PER_LINE && *c == ' ')) { height += 14.0f; line_width = 0.0f; - char_count = 0; + line_char_count = 0; } else { line_width += glutBitmapWidth(GLUT_BITMAP_HELVETICA_12, *c); } width = std::max(width, line_width); char_count++; + line_char_count++; + + if(tooltip->ellipsis && char_count > (MapTooltip::MAX_CHARS + 3)) + break; } float scale = zoom < 1.0f ? zoom : 1.0f; @@ -1601,14 +1604,23 @@ void MapDrawer::DrawTooltips() glColor4ub(0, 0, 0, 255); glRasterPos2f(startx, starty); char_count = 0; + line_char_count = 0; for(const char* c = text; *c != '\0'; c++) { - if(*c == '\n' || (char_count >= MAX_LINE_CHARS && *c == ' ')) { + if(*c == '\n' || (line_char_count >= MapTooltip::MAX_CHARS_PER_LINE && *c == ' ')) { starty += (14.0f * scale); glRasterPos2f(startx, starty); - char_count = 0; + line_char_count = 0; } - glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *c); char_count++; + line_char_count++; + + if(tooltip->ellipsis && char_count >= MapTooltip::MAX_CHARS) { + glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, '.'); + if(char_count >= (MapTooltip::MAX_CHARS + 2)) + break; + } else { + glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *c); + } } } } diff --git a/source/map_drawer.h b/source/map_drawer.h index d5717c9a..a1d61097 100644 --- a/source/map_drawer.h +++ b/source/map_drawer.h @@ -6,8 +6,15 @@ class GameSprite; struct MapTooltip { + enum TextLength { + MAX_CHARS_PER_LINE = 40, + MAX_CHARS = 255, + }; + MapTooltip(int x, int y, std::string text, uint8_t r, uint8_t g, uint8_t b) : - x(x), y(y), text(text), r(r), g(g), b(b) {} + x(x), y(y), text(text), r(r), g(g), b(b) { + ellipsis = (text.length() - 3) > MAX_CHARS; + } void checkLineEnding() { if(text.at(text.size() - 1) == '\n') @@ -17,6 +24,7 @@ struct MapTooltip int x, y; std::string text; uint8_t r, g, b; + bool ellipsis; }; // Storage during drawing, for option caching