Skip to content

Commit

Permalink
Add text ellipsis for tooltips. Closes #196
Browse files Browse the repository at this point in the history
  • Loading branch information
Mignari committed Jun 7, 2017
1 parent 4eac2c3 commit d792de9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
26 changes: 19 additions & 7 deletions source/map_drawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1524,26 +1524,29 @@ 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<MapTooltip*>::const_iterator it = tooltips.begin(); it != tooltips.end(); ++it) {
MapTooltip* tooltip = (*it);
const char* text = tooltip->text.c_str();
float line_width = 0.0f;
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<float>(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;
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion source/map_drawer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand Down

0 comments on commit d792de9

Please sign in to comment.