[ADD] Word wrapping.
This commit is contained in:
parent
6fe0ea2b24
commit
72e1b90df7
@ -57,7 +57,7 @@ void Player::HandleInput(void) {
|
|||||||
|
|
||||||
void Player::Update(void) {
|
void Player::Update(void) {
|
||||||
Move();
|
Move();
|
||||||
AddSpeachBubble("Woot, My name is Allanis\n, welcome to my home");
|
AddSpeachBubble("Woot, My name is Allanis, welcome to my home. Just testing some more text to see if this works..");
|
||||||
|
|
||||||
// For now The camera will be static.
|
// For now The camera will be static.
|
||||||
SetCamera();
|
SetCamera();
|
||||||
|
@ -55,7 +55,8 @@ void Character::LoadSprites(string filename, int wArg, int hArg) {
|
|||||||
void Character::AddSpeachBubble(string text) {
|
void Character::AddSpeachBubble(string text) {
|
||||||
_speachBubble.push_back(text);
|
_speachBubble.push_back(text);
|
||||||
|
|
||||||
_speachBubbleText.SetTextBlended(text, small, 0, 0, 0);
|
_speachBubbleText.SetLineWidth(200);
|
||||||
|
_speachBubbleText.SetTextBlended(text, small, 0, 0, 0, true);
|
||||||
|
|
||||||
if(_speachBubbleTimer.IsStarted() == false)
|
if(_speachBubbleTimer.IsStarted() == false)
|
||||||
_speachBubbleTimer.Start();
|
_speachBubbleTimer.Start();
|
||||||
|
@ -13,6 +13,7 @@ Text::Text(void) {
|
|||||||
y=0;
|
y=0;
|
||||||
w=0;
|
w=0;
|
||||||
h=0;
|
h=0;
|
||||||
|
lineWidth=50;
|
||||||
}
|
}
|
||||||
|
|
||||||
Text::~Text(void) {
|
Text::~Text(void) {
|
||||||
@ -59,7 +60,7 @@ void Text::SetXY(int xArg, int yArg) {
|
|||||||
y = yArg;
|
y = yArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Text::SetTextBlended(string textArg, textSizes_t size, SDL_Color colour) {
|
int Text::SetTextBlended(string textArg, textSizes_t size, SDL_Color colour,bool wordWrap) {
|
||||||
_textContents = textArg;
|
_textContents = textArg;
|
||||||
|
|
||||||
if(!_lines.empty()) {
|
if(!_lines.empty()) {
|
||||||
@ -69,10 +70,29 @@ int Text::SetTextBlended(string textArg, textSizes_t size, SDL_Color colour) {
|
|||||||
_lines.clear();
|
_lines.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TTF_Font* font = NULL;
|
||||||
|
if(size == vsmall) {
|
||||||
|
font = vSmallFont;
|
||||||
|
} else if(size == small) {
|
||||||
|
font = smallFont;
|
||||||
|
} else if(size == medium) {
|
||||||
|
font = mediumFont;
|
||||||
|
} else if(size == large) {
|
||||||
|
font = largeFont;
|
||||||
|
} else {
|
||||||
|
font = vLargeFont;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string finalTextContents = textArg;
|
||||||
|
|
||||||
|
if(wordWrap) {
|
||||||
|
finalTextContents = DoWordWrap(font, finalTextContents);
|
||||||
|
}
|
||||||
|
|
||||||
std::list<std::string> lines;
|
std::list<std::string> lines;
|
||||||
std::string line;
|
std::string line;
|
||||||
for(int i = 0; i < (int)textArg.size(); i++) {
|
for(int i = 0; i < (int)finalTextContents.size(); i++) {
|
||||||
char c = textArg.at(i);
|
char c = finalTextContents.at(i);
|
||||||
if(c=='\n') {
|
if(c=='\n') {
|
||||||
lines.push_back(line);
|
lines.push_back(line);
|
||||||
line.clear();
|
line.clear();
|
||||||
@ -85,68 +105,30 @@ int Text::SetTextBlended(string textArg, textSizes_t size, SDL_Color colour) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for(std::list<std::string>::iterator it = lines.begin(); it != lines.end(); ++it) {
|
for(std::list<std::string>::iterator it = lines.begin(); it != lines.end(); ++it) {
|
||||||
SDL_Surface* lineSurf = NULL;
|
SDL_Surface* lineSurf = TTF_RenderText_Blended(font, it->c_str(), colour);
|
||||||
if(size == vsmall) {
|
|
||||||
lineSurf = TTF_RenderText_Blended(vSmallFont, textArg.c_str(), colour);
|
|
||||||
int linePixelWidth;
|
|
||||||
int linePixelHeight;
|
|
||||||
TTF_SizeText(vSmallFont, textArg.c_str(), &linePixelWidth, &linePixelHeight);
|
|
||||||
if(linePixelWidth > w) {
|
|
||||||
w = linePixelWidth;
|
|
||||||
}
|
|
||||||
h += linePixelHeight + lineSpacing;
|
|
||||||
}
|
|
||||||
else if(size == small) {
|
|
||||||
lineSurf = TTF_RenderText_Blended(smallFont, it->c_str(), colour);
|
|
||||||
int linePixelWidth;
|
|
||||||
int linePixelHeight;
|
|
||||||
TTF_SizeText(smallFont, it->c_str(), &linePixelWidth, &linePixelHeight);
|
|
||||||
if(linePixelWidth > w) {
|
|
||||||
w = linePixelWidth;
|
|
||||||
}
|
|
||||||
h += linePixelHeight + lineSpacing;
|
|
||||||
}
|
|
||||||
else if(size == medium) {
|
|
||||||
lineSurf = TTF_RenderText_Blended(mediumFont, it->c_str(), colour);
|
|
||||||
int linePixelWidth;
|
|
||||||
int linePixelHeight;
|
|
||||||
TTF_SizeText(mediumFont, it->c_str(), &linePixelWidth, &linePixelHeight);
|
|
||||||
if(linePixelWidth > w) {
|
|
||||||
w = linePixelWidth;
|
|
||||||
}
|
|
||||||
h += linePixelHeight + lineSpacing;
|
|
||||||
}
|
|
||||||
else if(size == large) {
|
|
||||||
lineSurf = TTF_RenderText_Blended(largeFont, it->c_str(), colour);
|
|
||||||
int linePixelWidth;
|
|
||||||
int linePixelHeight;
|
|
||||||
TTF_SizeText(largeFont, it->c_str(), &linePixelWidth, &linePixelHeight);
|
|
||||||
if(linePixelWidth > w) {
|
|
||||||
w = linePixelWidth;
|
|
||||||
}
|
|
||||||
h += linePixelHeight + lineSpacing;
|
|
||||||
} else {
|
|
||||||
lineSurf = TTF_RenderText_Blended(vLargeFont, it->c_str(), colour);
|
|
||||||
int linePixelWidth;
|
|
||||||
int linePixelHeight;
|
|
||||||
TTF_SizeText(vLargeFont, it->c_str(), &linePixelWidth, &linePixelHeight);
|
|
||||||
if(linePixelWidth > w) {
|
|
||||||
w = linePixelWidth;
|
|
||||||
}
|
|
||||||
h += linePixelHeight + lineSpacing;
|
|
||||||
}
|
|
||||||
_lines.push_back(lineSurf);
|
|
||||||
|
|
||||||
|
int linePixelWidth;
|
||||||
|
int linePixelHeight;
|
||||||
|
TTF_SizeText(font, it->c_str(), &linePixelWidth, &linePixelHeight);
|
||||||
|
|
||||||
|
if(linePixelWidth > w) {
|
||||||
|
w = linePixelWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
h += linePixelHeight + lineSpacing;
|
||||||
|
|
||||||
|
_lines.push_back(lineSurf);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Text::SetTextBlended(string textArg, textSizes_t size, Uint8 r, Uint8 g, Uint8 b) {
|
int Text::SetTextBlended(string textArg, textSizes_t size, Uint8 r, Uint8 g, Uint8 b, bool wordWrap) {
|
||||||
SDL_Color f = { r, g, b };
|
SDL_Color f = { r, g, b };
|
||||||
return SetTextBlended(textArg, size, f);
|
return SetTextBlended(textArg, size, f, wordWrap);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Text::SetTextShaded(string textArg, textSizes_t size, SDL_Color colour, SDL_Color bgColour) {
|
int Text::SetTextShaded(string textArg, textSizes_t size, SDL_Color colour, SDL_Color bgColour, bool wordWrap) {
|
||||||
_textContents = textArg;
|
_textContents = textArg;
|
||||||
|
|
||||||
if(!_lines.empty()) {
|
if(!_lines.empty()) {
|
||||||
@ -156,10 +138,29 @@ int Text::SetTextShaded(string textArg, textSizes_t size, SDL_Color colour, SDL_
|
|||||||
_lines.clear();
|
_lines.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TTF_Font* font = NULL;
|
||||||
|
if(size == vsmall) {
|
||||||
|
font = vSmallFont;
|
||||||
|
} else if(size == small) {
|
||||||
|
font = smallFont;
|
||||||
|
} else if(size == medium) {
|
||||||
|
font = mediumFont;
|
||||||
|
} else if(size == large) {
|
||||||
|
font = largeFont;
|
||||||
|
} else {
|
||||||
|
font = vLargeFont;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string finalTextContents = textArg;
|
||||||
|
|
||||||
|
if(wordWrap) {
|
||||||
|
finalTextContents = DoWordWrap(font, finalTextContents);
|
||||||
|
}
|
||||||
|
|
||||||
std::list<std::string> lines;
|
std::list<std::string> lines;
|
||||||
std::string line;
|
std::string line;
|
||||||
for(int i = 0; i < (int)textArg.size(); i++) {
|
for(int i = 0; i < (int)finalTextContents.size(); i++) {
|
||||||
char c = textArg.at(i);
|
char c = finalTextContents.at(i);
|
||||||
if(c=='\n') {
|
if(c=='\n') {
|
||||||
lines.push_back(line);
|
lines.push_back(line);
|
||||||
line.clear();
|
line.clear();
|
||||||
@ -172,65 +173,28 @@ int Text::SetTextShaded(string textArg, textSizes_t size, SDL_Color colour, SDL_
|
|||||||
}
|
}
|
||||||
|
|
||||||
for(std::list<std::string>::iterator it = lines.begin(); it != lines.end(); ++it) {
|
for(std::list<std::string>::iterator it = lines.begin(); it != lines.end(); ++it) {
|
||||||
SDL_Surface* lineSurf;
|
SDL_Surface* lineSurf = TTF_RenderText_Shaded(font, it->c_str(), colour, bgColour);
|
||||||
if(size == vsmall) {
|
|
||||||
lineSurf = TTF_RenderText_Shaded(vSmallFont, it->c_str(), colour, bgColour);
|
int linePixelWidth;
|
||||||
int linePixelWidth;
|
int linePixelHeight;
|
||||||
int linePixelHeight;
|
TTF_SizeText(font, it->c_str(), &linePixelWidth, &linePixelHeight);
|
||||||
TTF_SizeText(vSmallFont, it->c_str(), &linePixelWidth, &linePixelHeight);
|
|
||||||
if(linePixelWidth > w) {
|
if(linePixelWidth > w) {
|
||||||
w = linePixelWidth;
|
w = linePixelWidth;
|
||||||
}
|
|
||||||
h += linePixelHeight + lineSpacing;
|
|
||||||
}
|
|
||||||
else if(size == small) {
|
|
||||||
lineSurf = TTF_RenderText_Shaded(smallFont, it->c_str(), colour, bgColour);
|
|
||||||
int linePixelWidth;
|
|
||||||
int linePixelHeight;
|
|
||||||
TTF_SizeText(smallFont, it->c_str(), &linePixelWidth, &linePixelHeight);
|
|
||||||
if(linePixelWidth > w) {
|
|
||||||
w = linePixelWidth;
|
|
||||||
}
|
|
||||||
h += linePixelHeight + lineSpacing;
|
|
||||||
}
|
|
||||||
else if(size == medium) {
|
|
||||||
lineSurf = TTF_RenderText_Shaded(mediumFont, it->c_str(), colour, bgColour);
|
|
||||||
int linePixelWidth;
|
|
||||||
int linePixelHeight;
|
|
||||||
TTF_SizeText(mediumFont, it->c_str(), &linePixelWidth, &linePixelHeight);
|
|
||||||
if(linePixelWidth > w) {
|
|
||||||
w = linePixelWidth;
|
|
||||||
}
|
|
||||||
h += linePixelHeight + lineSpacing;
|
|
||||||
}
|
|
||||||
else if(size == large) {
|
|
||||||
lineSurf = TTF_RenderText_Shaded(largeFont, it->c_str(), colour, bgColour);
|
|
||||||
int linePixelWidth;
|
|
||||||
int linePixelHeight;
|
|
||||||
TTF_SizeText(largeFont, it->c_str(), &linePixelWidth, &linePixelHeight);
|
|
||||||
if(linePixelWidth > w) {
|
|
||||||
w = linePixelWidth;
|
|
||||||
}
|
|
||||||
h += linePixelHeight + lineSpacing;
|
|
||||||
} else {
|
|
||||||
lineSurf = TTF_RenderText_Shaded(vLargeFont, it->c_str(), colour, bgColour);
|
|
||||||
int linePixelWidth;
|
|
||||||
int linePixelHeight;
|
|
||||||
TTF_SizeText(vLargeFont, it->c_str(), &linePixelWidth, &linePixelHeight);
|
|
||||||
if(linePixelWidth > w) {
|
|
||||||
w = linePixelWidth;
|
|
||||||
}
|
|
||||||
h += linePixelHeight + lineSpacing;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h += linePixelHeight + lineSpacing;
|
||||||
|
|
||||||
_lines.push_back(lineSurf);
|
_lines.push_back(lineSurf);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Text::SetTextShaded(string textArg, textSizes_t size, Uint8 rF, Uint8 gF, Uint8 bF, Uint8 rB, Uint8 gB, Uint8 bB) {
|
int Text::SetTextShaded(string textArg, textSizes_t size, Uint8 rF, Uint8 gF, Uint8 bF, Uint8 rB, Uint8 gB, Uint8 bB, bool wordWrap) {
|
||||||
SDL_Color f = { rF, gF, bF };
|
SDL_Color f = { rF, gF, bF };
|
||||||
SDL_Color b = { rB, gB, bB };
|
SDL_Color b = { rB, gB, bB };
|
||||||
return SetTextShaded(textArg, size, f, b);
|
return SetTextShaded(textArg, size, f, b, wordWrap);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Text::Render(void) {
|
void Text::Render(void) {
|
||||||
@ -268,3 +232,38 @@ void Text::RenderLiteral(int xArg, int yArg) {
|
|||||||
yOffset += lineSurf->h + lineSpacing;
|
yOffset += lineSurf->h + lineSpacing;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string Text::DoWordWrap(TTF_Font* fontArg, const std::string& textArg) {
|
||||||
|
int leftSpace = lineWidth;
|
||||||
|
|
||||||
|
char* tokenizedText = strdup(textArg.c_str());
|
||||||
|
char* tokenizedTextOrigin = tokenizedText;
|
||||||
|
|
||||||
|
std::string wrappedText(textArg);
|
||||||
|
int offsetInWrappedText = 0;
|
||||||
|
|
||||||
|
int spaceWidth;
|
||||||
|
TTF_SizeText(fontArg, " ", &spaceWidth, NULL);
|
||||||
|
|
||||||
|
char* word = strtok(tokenizedText, " ");
|
||||||
|
|
||||||
|
while (word) {
|
||||||
|
int wordWidth;
|
||||||
|
TTF_SizeText(fontArg, word, &wordWidth, NULL);
|
||||||
|
|
||||||
|
if ((wordWidth + spaceWidth) > leftSpace) {
|
||||||
|
wrappedText.insert((word - tokenizedTextOrigin) + offsetInWrappedText, "\n");
|
||||||
|
offsetInWrappedText++;
|
||||||
|
|
||||||
|
leftSpace = lineWidth - wordWidth;
|
||||||
|
} else {
|
||||||
|
leftSpace -= wordWidth + spaceWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
word = strtok(NULL, " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete[] tokenizedText;
|
||||||
|
|
||||||
|
return wrappedText;
|
||||||
|
}
|
||||||
|
@ -30,10 +30,13 @@ public:
|
|||||||
|
|
||||||
void SetXY(int xArg, int yArg);
|
void SetXY(int xArg, int yArg);
|
||||||
|
|
||||||
int SetTextBlended(string textArg, textSizes_t size, SDL_Color);
|
int GetLineWidth() { return lineWidth; }
|
||||||
int SetTextBlended(string textArg, textSizes_t size, Uint8 r, Uint8 g, Uint8 b);
|
void SetLineWidth(int lineWidthArg) { lineWidth = lineWidthArg; }
|
||||||
int SetTextShaded(string textArg, textSizes_t size, SDL_Color, SDL_Color);
|
|
||||||
int SetTextShaded(string textArg, textSizes_t size, Uint8 rF, Uint8 gF, Uint8 bF, Uint8 rB, Uint8 gB, Uint8 bB);
|
int SetTextBlended(string textArg, textSizes_t size, SDL_Color, bool wordWrap=false);
|
||||||
|
int SetTextBlended(string textArg, textSizes_t size, Uint8 r, Uint8 g, Uint8 b, bool wordWrap=false);
|
||||||
|
int SetTextShaded(string textArg, textSizes_t size, SDL_Color, SDL_Color, bool wordWrap=false);
|
||||||
|
int SetTextShaded(string textArg, textSizes_t size, Uint8 rF, Uint8 gF, Uint8 bF, Uint8 rB, Uint8 gB, Uint8 bB, bool wordWrap=false);
|
||||||
|
|
||||||
string GetText(void) { return _textContents; }
|
string GetText(void) { return _textContents; }
|
||||||
|
|
||||||
@ -44,11 +47,14 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
int x, y, w, h;
|
int x, y, w, h;
|
||||||
|
int lineWidth;
|
||||||
|
|
||||||
string _textContents;
|
string _textContents;
|
||||||
SDL_Color _textColour;
|
SDL_Color _textColour;
|
||||||
std::list<SDL_Surface*> _lines;
|
std::list<SDL_Surface*> _lines;
|
||||||
|
|
||||||
|
std::string DoWordWrap(TTF_Font* fontArg, const std::string& textArg);
|
||||||
|
|
||||||
static TTF_Font* vSmallFont;
|
static TTF_Font* vSmallFont;
|
||||||
static TTF_Font* smallFont;
|
static TTF_Font* smallFont;
|
||||||
static TTF_Font* mediumFont;
|
static TTF_Font* mediumFont;
|
||||||
|
Loading…
Reference in New Issue
Block a user