mirror of
https://github.com/edubart/otclient.git
synced 2025-10-17 13:03:27 +02:00
implement line wrap for text messages
This commit is contained in:
@@ -282,3 +282,56 @@ void Font::calculateGlyphsWidthsAutomatically(const Size& glyphSize)
|
||||
m_glyphsSize[glyph].resize(width, m_glyphHeight);
|
||||
}
|
||||
}
|
||||
|
||||
std::string Font::wrapText(const std::string& text, int maxWidth)
|
||||
{
|
||||
std::string outText;
|
||||
std::string line;
|
||||
std::vector<std::string> words;
|
||||
std::vector<std::string> wordsSplit = Fw::split(text);
|
||||
|
||||
// break huge words into small ones
|
||||
for(uint i=0;i<wordsSplit.size();++i) {
|
||||
const std::string& word = wordsSplit[i];
|
||||
int wordWidth = calculateTextRectSize(word).width();
|
||||
if(wordWidth > maxWidth) {
|
||||
std::string newWord;
|
||||
for(uint j=0;j<word.length();++j) {
|
||||
std::string candidate = newWord + word[j];
|
||||
if(j != word.length() - 1)
|
||||
candidate += "-";
|
||||
int candidateWidth = calculateTextRectSize(candidate).width();
|
||||
|
||||
if(candidateWidth > maxWidth) {
|
||||
newWord += "-";
|
||||
words.push_back(newWord);
|
||||
newWord = "";
|
||||
}
|
||||
|
||||
newWord += word[j];
|
||||
}
|
||||
|
||||
words.push_back(newWord);
|
||||
} else
|
||||
words.push_back(word);
|
||||
}
|
||||
|
||||
// compose lines
|
||||
for(uint i=0;i<words.size();++i) {
|
||||
std::string candidate = line + words[i];
|
||||
int candidateWidth = calculateTextRectSize(candidate).width();
|
||||
|
||||
if(candidateWidth > maxWidth) {
|
||||
if(!line.empty())
|
||||
outText += line.substr(0, line.length()-1) + "\n";
|
||||
line = "";
|
||||
}
|
||||
|
||||
line += words[i] + " ";
|
||||
}
|
||||
|
||||
outText += line;
|
||||
outText = outText.substr(0, outText.length()-1);
|
||||
|
||||
return outText;
|
||||
}
|
@@ -54,6 +54,8 @@ public:
|
||||
/// Simulate render and calculate text size
|
||||
Size calculateTextRectSize(const std::string& text);
|
||||
|
||||
std::string wrapText(const std::string& text, int maxWidth);
|
||||
|
||||
std::string getName() const { return m_name; }
|
||||
int getGlyphHeight() const { return m_glyphHeight; }
|
||||
const Rect* getGlyphsTextureCoords() const { return m_glyphsTextureCoords; }
|
||||
|
@@ -266,6 +266,7 @@ void Application::registerLuaFunctions()
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getImageBorderLeft", &UIWidget::getImageBorderLeft);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("resizeToText", &UIWidget::resizeToText);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("clearText", &UIWidget::clearText);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("wrapText", &UIWidget::wrapText);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setText", &UIWidget::setText);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setTextAlign", &UIWidget::setTextAlign);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setTextOffset", &UIWidget::setTextOffset);
|
||||
|
@@ -434,6 +434,7 @@ protected:
|
||||
public:
|
||||
void resizeToText() { setSize(getTextSize()); }
|
||||
void clearText() { setText(""); }
|
||||
void wrapText();
|
||||
|
||||
void setText(const std::string& text);
|
||||
void setTextAlign(Fw::AlignmentFlag align) { m_textAlign = align; m_textMustRecache = true; }
|
||||
|
@@ -84,6 +84,11 @@ void UIWidget::onFontChange(const std::string& font)
|
||||
callLuaField("onFontChange", font);
|
||||
}
|
||||
|
||||
void UIWidget::wrapText()
|
||||
{
|
||||
setText(m_font->wrapText(m_text, getWidth()));
|
||||
}
|
||||
|
||||
void UIWidget::setText(const std::string& text)
|
||||
{
|
||||
if(m_text == text)
|
||||
|
Reference in New Issue
Block a user