usbloadergx/source/libwiigui/Text.cpp

334 lines
6.5 KiB
C++
Raw Normal View History

#include "settings/CSettings.h"
#include "utils/tools.h"
#include "Text.hpp"
2010-09-24 02:48:03 +02:00
Text::Text(const char * t, int s, GXColor c) :
GuiText(t, s, c)
{
maxWidth = 400;
linestodraw = 9;
curLineStart = 0;
FirstLineOffset = 0;
wText = NULL;
2010-09-24 02:48:03 +02:00
if (!text) return;
2010-09-24 02:48:03 +02:00
wText = new (std::nothrow) wString(text);
if (!wText)
{
return;
}
2010-09-24 02:48:03 +02:00
if (wText->size() == 0)
{
2010-09-24 02:48:03 +02:00
wText->push_back(L' ');
wText->push_back(0);
}
2010-09-24 02:48:03 +02:00
textWidth = (font ? font : fontSystem)->getWidth(wText->data(), currentSize);
delete[] text;
text = NULL;
2010-09-24 02:48:03 +02:00
SetMaxWidth(maxWidth);
}
2010-09-24 02:48:03 +02:00
Text::Text(const wchar_t * t, int s, GXColor c) :
GuiText((wchar_t *) NULL, s, c)
{
maxWidth = 400;
linestodraw = 9;
curLineStart = 0;
FirstLineOffset = 0;
wText = NULL;
2010-09-24 02:48:03 +02:00
if (!t) return;
2010-09-24 02:48:03 +02:00
wText = new (std::nothrow) wString(t);
if (!wText)
{
return;
}
2010-09-24 02:48:03 +02:00
if (wText->size() == 0)
{
2010-09-24 02:48:03 +02:00
wText->push_back(L' ');
wText->push_back(0);
}
2010-09-24 02:48:03 +02:00
textWidth = (font ? font : fontSystem)->getWidth(wText->data(), currentSize);
2010-09-24 02:48:03 +02:00
SetMaxWidth(maxWidth);
}
Text::~Text()
{
2010-09-24 02:48:03 +02:00
if (wText) delete wText;
wText = NULL;
TextLines.clear();
ClearDynamicText();
}
2010-09-24 02:48:03 +02:00
void Text::SetText(const char * t)
{
2010-09-24 02:48:03 +02:00
wchar_t * tmp = charToWideChar(t);
if (!tmp) return;
2010-09-24 02:48:03 +02:00
if (wText) delete wText;
2010-09-24 02:48:03 +02:00
wText = new (std::nothrow) wString(tmp);
if (!wText)
{
return;
}
2010-09-24 02:48:03 +02:00
if (wText->size() == 0)
{
2010-09-24 02:48:03 +02:00
wText->push_back(L' ');
wText->push_back(0);
}
2010-09-24 02:48:03 +02:00
textWidth = (font ? font : fontSystem)->getWidth(wText->data(), currentSize);
2010-09-24 02:48:03 +02:00
delete[] tmp;
ClearDynamicText();
CalcLineOffsets();
}
2010-09-24 02:48:03 +02:00
void Text::SetText(const wchar_t * t)
{
2010-09-24 02:48:03 +02:00
if (!t) return;
2010-09-24 02:48:03 +02:00
if (wText) delete wText;
2010-09-24 02:48:03 +02:00
wText = new wString(t);
textWidth = (font ? font : fontSystem)->getWidth(wText->data(), currentSize);
CalcLineOffsets();
}
2010-09-24 02:48:03 +02:00
void Text::SetMaxWidth(int w)
{
maxWidth = w;
curLineStart = 0;
Refresh();
}
2010-09-24 02:48:03 +02:00
void Text::SetTextLine(int line)
{
2010-09-24 02:48:03 +02:00
if (line < 0)
line = 0;
2010-09-24 02:48:03 +02:00
else if (line > (int) TextLines.size() - 1) line = TextLines.size() - 1;
curLineStart = line;
FillRows();
2010-09-24 02:48:03 +02:00
while ((int) textDyn.size() < linestodraw && curLineStart > 0)
{
PreviousLine();
}
}
2010-09-24 02:48:03 +02:00
void Text::SetTextPos(int pos)
{
2010-09-24 02:48:03 +02:00
if (!wText) return;
int diff = 10000;
2010-09-24 02:48:03 +02:00
for (u32 i = 0; i < TextLines.size(); i++)
{
2010-09-24 02:48:03 +02:00
int curDiff = abs(TextLines[i].LineOffset - pos);
if (curDiff < diff)
{
diff = curDiff;
curLineStart = i;
}
}
FillRows();
2010-09-24 02:48:03 +02:00
while ((int) textDyn.size() < linestodraw && curLineStart > 0)
{
PreviousLine();
}
}
const wchar_t * Text::GetText()
{
return wText->c_str();
}
2010-09-24 02:48:03 +02:00
std::string Text::GetUTF8String(void) const
{
return wText->toUTF8();
}
2010-09-24 02:48:03 +02:00
int Text::GetLineOffset(int ind)
{
2010-09-24 02:48:03 +02:00
if (TextLines.size() == 0) return 0;
2010-09-24 02:48:03 +02:00
if (ind < 0) return TextLines[0].LineOffset;
2010-09-24 02:48:03 +02:00
if (ind >= (int) TextLines.size() - 1) return TextLines[TextLines.size() - 1].LineOffset;
return TextLines[ind].LineOffset;
}
2010-09-24 02:48:03 +02:00
const wchar_t * Text::GetTextLine(int ind)
{
2010-09-24 02:48:03 +02:00
if (filling || textDyn.size() == 0) return NULL;
2010-09-24 02:48:03 +02:00
if (ind < 0) return textDyn[0];
2010-09-24 02:48:03 +02:00
if (ind >= (int) textDyn.size()) return textDyn[textDyn.size() - 1];
return textDyn[ind];
}
void Text::Refresh()
{
CalcLineOffsets();
FillRows();
}
void Text::NextLine()
{
2010-09-24 02:48:03 +02:00
if (!wText || (curLineStart + 1 > ((int) TextLines.size() - linestodraw))) return;
++curLineStart;
FillRows();
}
void Text::PreviousLine()
{
2010-09-24 02:48:03 +02:00
if (!wText || curLineStart - 1 < 0) return;
--curLineStart;
FillRows();
}
void Text::FillRows()
{
2010-09-24 02:48:03 +02:00
if (!wText) return;
filling = true;
ClearDynamicText();
2010-09-24 02:48:03 +02:00
for (int i = 0; i < linestodraw && i < (int) TextLines.size(); i++)
{
2010-09-24 02:48:03 +02:00
if (i >= (int) textDyn.size())
{
2010-09-24 02:48:03 +02:00
textDyn.resize(i + 1);
textDyn[i] = new wchar_t[maxWidth];
}
2010-09-24 02:48:03 +02:00
int offset = TextLines[curLineStart + i].LineOffset;
int count = TextLines[curLineStart + i].CharCount + 1;
2010-09-24 02:48:03 +02:00
for (int n = 0; n < count && offset + n < (int) wText->size(); n++)
textDyn[i][n] = wText->at(offset + n);
textDyn[i][count] = 0;
}
filling = false;
return;
}
void Text::CalcLineOffsets()
{
2010-09-24 02:48:03 +02:00
if (!wText) return;
TextLines.clear();
TextLine TmpLine;
TmpLine.CharCount = 0;
TmpLine.LineOffset = 0;
TmpLine.width = 0;
const wchar_t * origTxt = wText->c_str();
int ch = 0;
int lastSpace = -1;
int lastSpaceIndex = -1;
int currWidth = 0;
int i = 0;
2010-09-24 02:48:03 +02:00
while (origTxt[ch])
{
2010-09-24 02:48:03 +02:00
currWidth += fontSystem->getCharWidth(origTxt[ch], currentSize, ch > 0 ? origTxt[ch - 1] : 0x0000);
2010-09-24 02:48:03 +02:00
if (currWidth >= maxWidth)
{
2010-09-24 02:48:03 +02:00
if (lastSpace > 0)
{
ch = lastSpace;
}
TmpLine.CharCount = ch - TmpLine.LineOffset;
TmpLine.width = currWidth;
2010-09-24 02:48:03 +02:00
TextLines.push_back(TmpLine);
currWidth = 0;
lastSpace = -1;
i = -1;
TmpLine.LineOffset = ch + 1;
}
2010-09-24 02:48:03 +02:00
else if (origTxt[ch] == '\n')
{
TmpLine.CharCount = ch - TmpLine.LineOffset;
TmpLine.width = currWidth;
2010-09-24 02:48:03 +02:00
TextLines.push_back(TmpLine);
currWidth = 0;
lastSpace = -1;
i = -1;
TmpLine.LineOffset = ch + 1;
}
2010-09-24 02:48:03 +02:00
else if (origTxt[ch] == ' ')
{
lastSpace = ch;
lastSpaceIndex = i;
}
ch++;
i++;
}
TmpLine.CharCount = ch - TmpLine.LineOffset;
TmpLine.width = currWidth;
if (TmpLine.CharCount-1 > 0)
{
TmpLine.CharCount -= 1;
TextLines.push_back(TmpLine);
}
}
void Text::Draw()
{
2010-09-24 02:48:03 +02:00
if (textDyn.size() == 0) return;
2010-09-24 02:48:03 +02:00
if (!this->IsVisible()) return;
GXColor c = color;
c.a = this->GetAlpha();
int newSize = (int) (size * GetScale() * Settings.FontScaleFactor);
2010-09-24 02:48:03 +02:00
if (newSize != currentSize)
{
currentSize = LIMIT(newSize, 1, 100);
2010-09-24 02:48:03 +02:00
if (wText) textWidth = (font ? font : fontSystem)->getWidth(wText->data(), currentSize);
}
u16 lineheight = newSize + 6;
2010-09-24 02:48:03 +02:00
for (u32 i = 0; i < textDyn.size(); i++)
{
2010-09-24 02:48:03 +02:00
if (!filling) (font ? font : fontSystem)->drawText(this->GetLeft(), this->GetTop() + i * lineheight, 0,
textDyn[i], currentSize, c, style, 0, maxWidth);
}
}