Skip to content

Commit

Permalink
Add automatic line warping to the textbox function.
Browse files Browse the repository at this point in the history
  • Loading branch information
daid committed Aug 22, 2014
1 parent 19fd813 commit 4fb18be
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
9 changes: 4 additions & 5 deletions resources/factionInfo.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
neutral = FactionInfo():setName("Independent")
neutral:setGMColor(128, 128, 128)
neutral:setDescription([[The Independent are those with no strong affiliation for any of the other factions.
Despite being seen as a faction, they are not truely one. Most traders consider themselves independent, but certain voices have started to speak up about creating a merchant faction.]])
Despite being seen as a faction, they are not truely one. Most traders consider themselves independent, but certain voices have started to speak up about creating a merchant faction.]])

human = FactionInfo():setName("Human Navy")
human:setGMColor(255, 255, 255)
human:setDescription([[The remenants of the human navy.
While all other races where driven to the
stars out of greed or intressed in science
While all other races where driven to the stars out of greed or intressed in science
The humans where the only race to start
with galaxic exploration because their home world could no longer sustain their population]])
The humans where the only race to start with galaxic exploration because their home world could no longer sustain their population]])

spaceCow = FactionInfo():setName("SpaceCow")
spaceCow:setGMColor(255, 0, 0)
Expand Down
32 changes: 32 additions & 0 deletions src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,38 @@ void GUI::box(sf::FloatRect rect)

void GUI::textbox(sf::FloatRect rect, string text, EAlign align, float textSize, sf::Color color)
{
{
unsigned currentOffset = 0;
bool firstWord = true;
std::size_t wordBegining = 0;

for (std::size_t pos(0); pos < text.length(); ++pos)
{
char currentChar = text[pos];
if (currentChar == '\n')
{
currentOffset = 0;
firstWord = true;
continue;
} else if (currentChar == ' ')
{
wordBegining = pos;
firstWord = false;
}

sf::Glyph glyph = mainFont.getGlyph(currentChar, textSize, false);
currentOffset += glyph.advance;

if (!firstWord && currentOffset > rect.width - textSize * 2)
{
pos = wordBegining;
text[pos] = '\n';
firstWord = true;
currentOffset = 0;
}
}
}

box(rect);
GUI::text(sf::FloatRect(rect.left + textSize, rect.top + textSize, rect.width - textSize * 2, rect.height - textSize * 2), text, align, textSize, color);
}
Expand Down

0 comments on commit 4fb18be

Please sign in to comment.