| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525 |
- //
- // Urho3D Engine
- // Copyright (c) 2008-2011 Lasse Öörni
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "Font.h"
- #include "Log.h"
- #include "Profiler.h"
- #include "ResourceCache.h"
- #include "StringUtils.h"
- #include "Text.h"
- #include "Texture2D.h"
- #include "DebugNew.h"
- static const float MIN_ROW_SPACING = 0.5f;
- static const std::string horizontalAlignments[] =
- {
- "left",
- "center",
- "right"
- };
- Text::Text(const std::string& name, const std::string& text) :
- UIElement(name),
- mFontSize(DEFAULT_FONT_SIZE),
- mText(text),
- mTextAlignment(HA_LEFT),
- mRowSpacing(1.0f),
- mWordwrap(false),
- mSelectionStart(0),
- mSelectionLength(0),
- mSelectionColor(Color(0.0f, 0.0f, 0.0f, 0.0f)),
- mHoverColor(Color(0.0f, 0.0f, 0.0f, 0.0f)),
- mRowHeight(0)
- {
- }
- Text::~Text()
- {
- }
- void Text::setStyle(const XMLElement& element, ResourceCache* cache)
- {
- if (!cache)
- return;
-
- UIElement::setStyle(element, cache);
-
- // Recalculating the text is expensive, so do it only once at the end if something changed
- bool changed = false;
-
- if (element.hasChildElement("font"))
- {
- XMLElement fontElem = element.getChildElement("font");
- std::string fontName = fontElem.getString("name");
-
- if (cache->exists(fontName))
- {
- Font* font = cache->getResource<Font>(fontName);
- if (font)
- {
- mFont = font;
- mFontSize = max(fontElem.getInt("size"), 1);
- changed = true;
- }
- }
- else if (element.hasChildElement("fallbackfont"))
- {
- fontElem = element.getChildElement("fallbackfont");
- std::string fontName = fontElem.getString("name");
- Font* font = cache->getResource<Font>(fontName);
- if (font)
- {
- mFont = font;
- mFontSize = max(fontElem.getInt("size"), 1);
- changed = true;
- }
- }
- }
- if (element.hasChildElement("text"))
- {
- std::string text = element.getChildElement("text").getString("value");
- replaceInPlace(text, "\\n", "\n");
- mText = text;
- changed = true;
- }
- if (element.hasChildElement("textalignment"))
- {
- std::string horiz = element.getChildElement("textalignment").getStringLower("value");
- if (!horiz.empty())
- {
- mTextAlignment = (HorizontalAlignment)getIndexFromStringList(horiz, horizontalAlignments, 3, 0);
- changed = true;
- }
- }
- if (element.hasChildElement("rowspacing"))
- {
- mRowSpacing = max(element.getChildElement("rowspacing").getFloat("value"), MIN_ROW_SPACING);
- changed = true;
- }
- if (element.hasChildElement("wordwrap"))
- {
- mWordwrap = element.getChildElement("wordwrap").getBool("enable");
- changed = true;
- }
- if (element.hasChildElement("selection"))
- {
- XMLElement selectionElem = element.getChildElement("selection");
- mSelectionStart = selectionElem.getInt("start");
- mSelectionLength = selectionElem.getInt("length");
- changed = true;
- }
- if (element.hasChildElement("selectioncolor"))
- setSelectionColor(element.getChildElement("selectioncolor").getColor("value"));
- if (element.hasChildElement("hovercolor"))
- setHoverColor(element.getChildElement("hovercolor").getColor("value"));
-
- if (changed)
- {
- validateSelection();
- updateText();
- }
- }
- void Text::getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor)
- {
- // Hovering or whole selection batch
- if ((mHovering && (mHoverColor.mA > 0.0f)) || (mSelected && (mSelectionColor.mA > 0.0f)))
- {
- UIBatch batch;
- batch.begin(&quads);
- batch.mBlendMode = BLEND_ALPHA;
- batch.mScissor = currentScissor;
- batch.mTexture = 0;
- batch.addQuad(*this, 0, 0, getWidth(), getHeight(), 0, 0, 0, 0, (mSelected && (mSelectionColor.mA > 0.0f)) ? mSelectionColor :
- mHoverColor);
- UIBatch::addOrMerge(batch, batches);
- }
-
- // Partial selection batch
- if ((!mSelected) && (mSelectionLength) && (mCharSizes.size() >= mSelectionStart + mSelectionLength) && (mSelectionColor.mA > 0.0f))
- {
- UIBatch batch;
- batch.begin(&quads);
- batch.mBlendMode = BLEND_ALPHA;
- batch.mScissor = currentScissor;
- batch.mTexture = 0;
-
- IntVector2 currentStart = mCharPositions[mSelectionStart];
- IntVector2 currentEnd = currentStart;
- for (unsigned i = mSelectionStart; i < mSelectionStart + mSelectionLength; ++i)
- {
- // Check if row changes, and start a new quad in that case
- if ((mCharSizes[i].mX) && (mCharSizes[i].mY))
- {
- if (mCharPositions[i].mY != currentStart.mY)
- {
- batch.addQuad(*this, currentStart.mX, currentStart.mY, currentEnd.mX - currentStart.mX, currentEnd.mY - currentStart.mY,
- 0, 0, 0, 0, mSelectionColor);
- currentStart = mCharPositions[i];
- currentEnd = currentStart + mCharSizes[i];
- }
- else
- {
- currentEnd.mX += mCharSizes[i].mX;
- currentEnd.mY = max(currentStart.mY + mCharSizes[i].mY, currentEnd.mY);
- }
- }
- }
- if (currentEnd != currentStart)
- {
- batch.addQuad(*this, currentStart.mX, currentStart.mY, currentEnd.mX - currentStart.mX, currentEnd.mY - currentStart.mY,
- 0, 0, 0, 0, mSelectionColor);
- }
-
- UIBatch::addOrMerge(batch, batches);
- }
-
- // Text batch
- if (mFont)
- {
- const FontFace* face = mFont->getFace(mFontSize);
-
- UIBatch batch;
- batch.begin(&quads);
- batch.mBlendMode = BLEND_ALPHA;
- batch.mScissor = currentScissor;
- batch.mTexture = face->mTexture;
-
- unsigned rowIndex = 0;
- int x = getRowStartPosition(rowIndex);
- int y = 0;
-
- for (unsigned i = 0; i < mPrintText.length(); ++i)
- {
- unsigned char c = (unsigned char)mPrintText[i];
-
- if (c != '\n')
- {
- const FontGlyph& glyph = face->mGlyphs[face->mGlyphIndex[c]];
- if (c != ' ')
- batch.addQuad(*this, x + glyph.mOffsetX, y + glyph.mOffsetY, glyph.mWidth, glyph.mHeight, glyph.mX, glyph.mY);
- x += glyph.mAdvanceX;
- }
- else
- {
- rowIndex++;
- x = getRowStartPosition(rowIndex);
- y += mRowHeight;
- }
- }
-
- UIBatch::addOrMerge(batch, batches);
- }
-
- // Reset hovering for next frame
- mHovering = false;
- }
- void Text::onResize()
- {
- if (mWordwrap)
- updateText();
- }
- bool Text::setFont(Font* font, int size)
- {
- if (!font)
- {
- LOGERROR("Null font for Text");
- return false;
- }
-
- if ((font != mFont) || (size != mFontSize))
- {
- mFont = font;
- mFontSize = max(size, 1);
- updateText();
- }
-
- return true;
- }
- void Text::setText(const std::string& text)
- {
- mText = text;
-
- validateSelection();
- updateText();
- }
- void Text::setTextAlignment(HorizontalAlignment align)
- {
- if (align != mTextAlignment)
- {
- mTextAlignment = align;
- updateText();
- }
- }
- void Text::setRowSpacing(float spacing)
- {
- if (spacing != mRowSpacing)
- {
- mRowSpacing = max(spacing, MIN_ROW_SPACING);
- updateText();
- }
- }
- void Text::setWordwrap(bool enable)
- {
- if (enable != mWordwrap)
- {
- mWordwrap = enable;
- updateText();
- }
- }
- void Text::setSelection(unsigned start, unsigned length)
- {
- mSelectionStart = start;
- mSelectionLength = length;
- validateSelection();
- }
- void Text::clearSelection()
- {
- mSelectionStart = 0;
- mSelectionLength = 0;
- }
- void Text::setSelectionColor(const Color& color)
- {
- mSelectionColor = color;
- }
- void Text::setHoverColor(const Color& color)
- {
- mHoverColor = color;
- }
- void Text::updateText(bool inResize)
- {
- int width = 0;
- int height = 0;
-
- mRowWidths.clear();
- mPrintText.clear();
-
- static std::vector<unsigned> printToText;
- printToText.clear();
-
- if (mFont)
- {
- const FontFace* face = mFont->getFace(mFontSize);
- mRowHeight = face->mRowHeight;
- int rowWidth = 0;
- int rowHeight = (int)(mRowSpacing * mRowHeight);
-
- // First see if the text must be split up
- if (!mWordwrap)
- {
- mPrintText = mText;
- printToText.resize(mText.length());
- for (unsigned i = 0; i < mText.length(); ++i)
- printToText[i] = i;
- }
- else
- {
- int maxWidth = getWidth();
- unsigned nextBreak = 0;
- unsigned lineStart = 0;
- for (unsigned i = 0; i < mText.length(); ++i)
- {
- unsigned j;
- if (mText[i] != '\n')
- {
- bool ok = true;
-
- if (nextBreak <= i)
- {
- int futureRowWidth = rowWidth;
- for (j = i; j < mText.length(); ++j)
- {
- if ((mText[j] == ' ') || (mText[j] == '\n'))
- {
- nextBreak = j;
- break;
- }
- futureRowWidth += face->mGlyphs[face->mGlyphIndex[mText[j]]].mAdvanceX;
- if ((mText[j] == '-') && (futureRowWidth <= maxWidth))
- {
- nextBreak = j + 1;
- break;
- }
- if (futureRowWidth > maxWidth)
- {
- ok = false;
- break;
- }
- }
- }
-
- if (!ok)
- {
- // If did not find any breaks on the line, copy until j, or at least 1 char, to prevent infinite loop
- if (nextBreak == lineStart)
- {
- int copyLength = max(j - i, 1);
- mPrintText.append(mText.substr(i, copyLength));
- for (int k = 0; k < copyLength; ++k)
- {
- printToText.push_back(i);
- ++i;
- }
- }
- mPrintText += '\n';
- printToText.push_back(min((int)i, (int)mText.length() - 1));
- rowWidth = 0;
- nextBreak = lineStart = i;
- }
-
- if (i < mText.length())
- {
- // When copying a space, we may be over row width
- rowWidth += face->mGlyphs[face->mGlyphIndex[mText[i]]].mAdvanceX;
- if (rowWidth <= maxWidth)
- {
- mPrintText += mText[i];
- printToText.push_back(i);
- }
- }
- }
- else
- {
- mPrintText += '\n';
- printToText.push_back(min((int)i, (int)mText.length() - 1));
- rowWidth = 0;
- nextBreak = lineStart = i;
- }
- }
- }
-
- rowWidth = 0;
-
- for (unsigned i = 0; i < mPrintText.length(); ++i)
- {
- unsigned char c = (unsigned char)mPrintText[i];
-
- if (c != '\n')
- {
- const FontGlyph& glyph = face->mGlyphs[face->mGlyphIndex[c]];
- rowWidth += glyph.mAdvanceX;
- }
- else
- {
- width = max(width, rowWidth);
- height += rowHeight;
- mRowWidths.push_back(rowWidth);
- rowWidth = 0;
- }
- }
-
- if (rowWidth)
- {
- width = max(width, rowWidth);
- height += rowHeight;
- mRowWidths.push_back(rowWidth);
- }
-
- // Set row height even if text is empty
- if (!height)
- height = rowHeight;
-
- // Store position & size of each character
- mCharPositions.resize(mText.length() + 1);
- mCharSizes.resize(mText.length());
-
- unsigned rowIndex = 0;
- int x = getRowStartPosition(rowIndex);
- int y = 0;
- for (unsigned i = 0; i < mPrintText.length(); ++i)
- {
- mCharPositions[printToText[i]] = IntVector2(x, y);
- unsigned char c = (unsigned char)mPrintText[i];
- if (c != '\n')
- {
- const FontGlyph& glyph = face->mGlyphs[face->mGlyphIndex[c]];
- mCharSizes[printToText[i]] = IntVector2(glyph.mAdvanceX, mRowHeight);
- x += glyph.mAdvanceX;
- }
- else
- {
- mCharSizes[printToText[i]] = IntVector2::sZero;
- rowIndex++;
- x = getRowStartPosition(rowIndex);
- y += rowHeight;
- }
- }
- // Store the ending position
- mCharPositions[mText.length()] = IntVector2(x, y);
- }
-
- // Set minimum size according to the text size, but respect fixed width if set
- if (getMinWidth() != getMaxWidth())
- setMinWidth(width);
- setFixedHeight(height);
- }
- void Text::validateSelection()
- {
- unsigned textLength = mText.length();
-
- if (textLength)
- {
- if (mSelectionStart >= textLength)
- mSelectionStart = textLength - 1;
- if (mSelectionStart + mSelectionLength > textLength)
- mSelectionLength = textLength - mSelectionStart;
- }
- else
- {
- mSelectionStart = 0;
- mSelectionLength = 0;
- }
- }
- int Text::getRowStartPosition(unsigned rowIndex) const
- {
- int rowWidth = 0;
-
- if (rowIndex < mRowWidths.size())
- rowWidth = mRowWidths[rowIndex];
-
- switch (mTextAlignment)
- {
- default:
- return 0;
- case HA_CENTER:
- return (getSize().mX - rowWidth) / 2;
- case HA_RIGHT:
- return getSize().mX - rowWidth;
- }
- }
|