usbloadergx/source/libwiigui/gui_text.cpp

578 lines
12 KiB
C++
Raw Normal View History

/****************************************************************************
* libwiigui
*
* Tantric 2009
*
* gui_text.cpp
*
* GUI class definitions
***************************************************************************/
#include "gui.h"
#include "wstring.hpp"
#define MAX_LINES_TO_DRAW 9
static int presetSize = 18;
static int presetMaxWidth = 0;
static int presetAlignmentHor = 0;
static int presetAlignmentVert = 0;
static u16 presetStyle = 0;
2010-09-24 02:48:03 +02:00
static GXColor presetColor = ( GXColor )
{ 255, 255, 255, 255};
#define TEXT_SCROLL_DELAY 5
#define TEXT_SCROLL_INITIAL_DELAY 8
/**
* Constructor for the GuiText class.
*/
2010-09-24 02:48:03 +02:00
GuiText::GuiText(const char * t, int s, GXColor c)
{
text = NULL;
size = s;
currentSize = size;
color = c;
alpha = c.a;
style = FTGX_JUSTIFY_CENTER | FTGX_ALIGN_MIDDLE;
maxWidth = 0;
wrapMode = 0;
passChar = 0;
font = NULL;
linestodraw = MAX_LINES_TO_DRAW;
textScrollPos = 0;
textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
textScrollDelay = TEXT_SCROLL_DELAY;
alignmentHor = ALIGN_CENTRE;
alignmentVert = ALIGN_MIDDLE;
2010-09-24 02:48:03 +02:00
if (t)
{
2010-09-24 02:48:03 +02:00
text = charToWideChar(t);
if (!text) return;
2010-09-24 02:48:03 +02:00
textWidth = fontSystem->getWidth(text, currentSize);
}
}
2010-09-24 02:48:03 +02:00
GuiText::GuiText(const wchar_t * t, int s, GXColor c)
{
text = NULL;
size = s;
currentSize = size;
color = c;
alpha = c.a;
style = FTGX_JUSTIFY_CENTER | FTGX_ALIGN_MIDDLE;
maxWidth = 0;
wrapMode = 0;
passChar = 0;
font = NULL;
linestodraw = MAX_LINES_TO_DRAW;
textScrollPos = 0;
textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
textScrollDelay = TEXT_SCROLL_DELAY;
alignmentHor = ALIGN_CENTRE;
alignmentVert = ALIGN_MIDDLE;
2010-09-24 02:48:03 +02:00
if (t)
{
2010-09-24 02:48:03 +02:00
text = new (std::nothrow) wchar_t[wcslen(t) + 1];
if (!text) return;
2010-09-24 02:48:03 +02:00
wcscpy(text, t);
2010-09-24 02:48:03 +02:00
textWidth = fontSystem->getWidth(text, currentSize);
}
}
/**
* Constructor for the GuiText class, uses presets
*/
2010-09-24 02:48:03 +02:00
GuiText::GuiText(const char * t)
{
text = NULL;
size = presetSize;
currentSize = size;
color = presetColor;
alpha = presetColor.a;
style = presetStyle;
maxWidth = presetMaxWidth;
wrapMode = 0;
passChar = 0;
font = NULL;
linestodraw = MAX_LINES_TO_DRAW;
textScrollPos = 0;
textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
textScrollDelay = TEXT_SCROLL_DELAY;
alignmentHor = presetAlignmentHor;
alignmentVert = presetAlignmentVert;
2010-09-24 02:48:03 +02:00
if (t)
{
2010-09-24 02:48:03 +02:00
text = charToWideChar(t);
if (!text) return;
2010-09-24 02:48:03 +02:00
textWidth = fontSystem->getWidth(text, currentSize);
}
}
/**
* Destructor for the GuiText class.
*/
GuiText::~GuiText()
{
2010-09-24 02:48:03 +02:00
if (text) delete[] text;
text = NULL;
2010-09-24 02:48:03 +02:00
if (font)
{
delete font;
font = NULL;
}
ClearDynamicText();
}
2010-09-24 02:48:03 +02:00
void GuiText::SetText(const char * t)
{
LOCK( this );
2010-09-24 02:48:03 +02:00
if (text) delete[] text;
text = NULL;
ClearDynamicText();
textScrollPos = 0;
textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
2010-09-24 02:48:03 +02:00
if (t)
{
2010-09-24 02:48:03 +02:00
text = charToWideChar(t);
if (!text) return;
2010-09-24 02:48:03 +02:00
if (passChar != 0)
{
2010-09-24 02:48:03 +02:00
for (u8 i = 0; i < wcslen(text); i++)
text[i] = passChar;
}
2010-09-24 02:48:03 +02:00
textWidth = fontSystem->getWidth(text, currentSize);
}
}
2010-09-24 02:48:03 +02:00
void GuiText::SetTextf(const char *format, ...)
{
2010-09-24 02:48:03 +02:00
if (!format) SetText((char *) NULL);
char *tmp = 0;
va_list va;
va_start( va, format );
2010-09-24 02:48:03 +02:00
if ((vasprintf(&tmp, format, va) >= 0) && tmp)
{
2010-09-24 02:48:03 +02:00
SetText(tmp);
}
va_end( va );
2010-09-24 02:48:03 +02:00
if (tmp) free(tmp);
}
2010-09-24 02:48:03 +02:00
void GuiText::SetText(const wchar_t * t)
{
LOCK( this );
2010-09-24 02:48:03 +02:00
if (text) delete[] text;
text = NULL;
ClearDynamicText();
textScrollPos = 0;
textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
2010-09-24 02:48:03 +02:00
if (t)
{
2010-09-24 02:48:03 +02:00
text = new (std::nothrow) wchar_t[wcslen(t) + 1];
if (!text) return;
2010-09-24 02:48:03 +02:00
wcscpy(text, t);
2010-09-24 02:48:03 +02:00
if (passChar != 0)
{
2010-09-24 02:48:03 +02:00
for (u8 i = 0; i < wcslen(text); i++)
text[i] = passChar;
}
2010-09-24 02:48:03 +02:00
textWidth = fontSystem->getWidth(text, currentSize);
}
}
void GuiText::ClearDynamicText()
{
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 (textDyn[i]) delete[] textDyn[i];
}
textDyn.clear();
}
2010-09-24 02:48:03 +02:00
void GuiText::SetPresets(int sz, GXColor c, int w, u16 s, int h, int v)
{
presetSize = sz;
presetColor = c;
presetStyle = s;
presetMaxWidth = w;
presetAlignmentHor = h;
presetAlignmentVert = v;
}
2010-09-24 02:48:03 +02:00
void GuiText::SetFontSize(int s)
{
LOCK( this );
size = s;
}
2010-09-24 02:48:03 +02:00
void GuiText::SetMaxWidth(int width, int w)
{
LOCK( this );
maxWidth = width;
wrapMode = w;
2010-09-24 02:48:03 +02:00
if (w == SCROLL_HORIZONTAL)
{
textScrollPos = 0;
textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
textScrollDelay = TEXT_SCROLL_DELAY;
}
ClearDynamicText();
}
2010-09-24 02:48:03 +02:00
void GuiText::SetPassChar(wchar_t p)
{
LOCK( this );
passChar = p;
}
2010-09-24 02:48:03 +02:00
void GuiText::SetColor(GXColor c)
{
LOCK( this );
color = c;
alpha = c.a;
}
2010-09-24 02:48:03 +02:00
void GuiText::SetStyle(u16 s)
{
LOCK( this );
style = s;
}
2010-09-24 02:48:03 +02:00
void GuiText::SetAlignment(int hor, int vert)
{
LOCK( this );
style = 0;
2010-09-24 02:48:03 +02:00
switch (hor)
{
case ALIGN_LEFT:
style |= FTGX_JUSTIFY_LEFT;
break;
case ALIGN_RIGHT:
style |= FTGX_JUSTIFY_RIGHT;
break;
default:
style |= FTGX_JUSTIFY_CENTER;
break;
}
2010-09-24 02:48:03 +02:00
switch (vert)
{
case ALIGN_TOP:
style |= FTGX_ALIGN_TOP;
break;
case ALIGN_BOTTOM:
style |= FTGX_ALIGN_BOTTOM;
break;
default:
style |= FTGX_ALIGN_MIDDLE;
break;
}
alignmentHor = hor;
alignmentVert = vert;
}
2010-09-24 02:48:03 +02:00
void GuiText::SetLinesToDraw(int l)
{
linestodraw = l;
}
int GuiText::GetTextWidth()
{
2010-09-24 02:48:03 +02:00
if (!text) return 0;
2010-09-24 02:48:03 +02:00
return fontSystem->getWidth(text, currentSize);
}
2010-09-24 02:48:03 +02:00
int GuiText::GetTextWidth(int ind)
{
2010-09-24 02:48:03 +02:00
if (ind < 0 || ind >= (int) textDyn.size()) return this->GetTextWidth();
2010-09-24 02:48:03 +02:00
return fontSystem->getWidth(textDyn[ind], currentSize);
}
int GuiText::GetTextMaxWidth()
{
return maxWidth;
}
2010-09-24 02:48:03 +02:00
const wchar_t * GuiText::GetDynText(int ind)
{
2010-09-24 02:48:03 +02:00
if (ind < 0 || ind >= (int) textDyn.size()) return text;
return textDyn[ind];
}
const wchar_t * GuiText::GetText()
{
return text;
}
/**
* Change font
*/
2010-09-24 02:48:03 +02:00
bool GuiText::SetFont(const u8 *fontbuffer, const u32 filesize)
{
2010-09-24 02:48:03 +02:00
if (!fontbuffer || !filesize) return false;
LOCK( this );
2010-09-24 02:48:03 +02:00
if (font)
{
delete font;
font = NULL;
}
2010-09-24 02:48:03 +02:00
font = new FreeTypeGX(fontbuffer, filesize);
textWidth = font->getWidth(text, currentSize);
return true;
}
void GuiText::MakeDottedText()
{
int pos = textDyn.size();
2010-09-24 02:48:03 +02:00
textDyn.resize(pos + 1);
int i = 0, currentWidth = 0;
textDyn[pos] = new wchar_t[maxWidth];
2010-09-24 02:48:03 +02:00
while (text[i])
{
2010-09-24 02:48:03 +02:00
currentWidth += (font ? font : fontSystem)->getCharWidth(text[i], currentSize, i > 0 ? text[i - 1] : 0x0000);
if (currentWidth >= maxWidth)
{
2010-09-24 02:48:03 +02:00
if (i > 3)
{
2010-09-24 02:48:03 +02:00
textDyn[pos][i - 3] = '.';
textDyn[pos][i - 2] = '.';
textDyn[pos][i - 1] = '.';
}
break;
}
textDyn[pos][i] = text[i];
i++;
}
textDyn[pos][i] = 0;
}
void GuiText::ScrollText()
{
2010-09-24 02:48:03 +02:00
if (textDyn.size() == 0)
{
int pos = textDyn.size();
int i = 0, currentWidth = 0;
2010-09-24 02:48:03 +02:00
textDyn.resize(pos + 1);
textDyn[pos] = new wchar_t[maxWidth];
while (text[i] && currentWidth < maxWidth-40)
{
textDyn[pos][i] = text[i];
2010-09-24 02:48:03 +02:00
currentWidth
+= (font ? font : fontSystem)->getCharWidth(text[i], currentSize, i > 0 ? text[i - 1] : 0x0000);
++i;
}
textDyn[pos][i] = 0;
return;
}
2010-09-24 02:48:03 +02:00
if (frameCount % textScrollDelay != 0)
{
return;
}
2010-09-24 02:48:03 +02:00
if (textScrollInitialDelay)
{
--textScrollInitialDelay;
return;
}
2010-09-24 02:48:03 +02:00
int strlen = wcslen(text);
++textScrollPos;
2010-09-24 02:48:03 +02:00
if (textScrollPos > strlen)
{
textScrollPos = 0;
textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
}
int ch = textScrollPos;
int pos = textDyn.size() - 1;
2010-09-24 02:48:03 +02:00
if (textDyn[pos]) delete[] textDyn[pos];
textDyn[pos] = new wchar_t[maxWidth];
int i = 0, currentWidth = 0;
while (currentWidth < maxWidth-40)
{
2010-09-24 02:48:03 +02:00
if (ch > strlen - 1)
{
textDyn[pos][i++] = ' ';
textDyn[pos][i++] = ' ';
textDyn[pos][i++] = ' ';
ch = 0;
}
textDyn[pos][i] = text[ch];
++ch;
++i;
2010-09-24 02:48:03 +02:00
currentWidth += (font ? font : fontSystem)->getCharWidth(text[ch], currentSize, ch > 0 ? text[ch - 1] : 0x0000);
}
textDyn[pos][i] = 0;
}
void GuiText::WrapText()
{
2010-09-24 02:48:03 +02:00
if (textDyn.size() > 0) return;
int i = 0;
int ch = 0;
int linenum = 0;
int lastSpace = -1;
int lastSpaceIndex = -1;
int currentWidth = 0;
2010-09-24 02:48:03 +02:00
while (text[ch] && linenum < linestodraw)
{
2010-09-24 02:48:03 +02:00
if (linenum >= (int) textDyn.size())
{
2010-09-24 02:48:03 +02:00
textDyn.resize(linenum + 1);
textDyn[linenum] = new wchar_t[maxWidth];
}
textDyn[linenum][i] = text[ch];
2010-09-24 02:48:03 +02:00
textDyn[linenum][i + 1] = 0;
2010-09-24 02:48:03 +02:00
currentWidth += (font ? font : fontSystem)->getCharWidth(text[ch], currentSize, ch > 0 ? text[ch - 1] : 0x0000);
2010-09-24 02:48:03 +02:00
if (currentWidth >= maxWidth)
{
2010-09-24 02:48:03 +02:00
if (lastSpace >= 0)
{
textDyn[linenum][lastSpaceIndex] = 0; // discard space, and everything after
ch = lastSpace; // go backwards to the last space
lastSpace = -1; // we have used this space
lastSpaceIndex = -1;
}
2010-09-24 02:48:03 +02:00
if (linenum + 1 == linestodraw && text[ch + 1] != 0x0000)
{
2010-09-24 02:48:03 +02:00
textDyn[linenum][i - 2] = '.';
textDyn[linenum][i - 1] = '.';
textDyn[linenum][i] = '.';
2010-09-24 02:48:03 +02:00
textDyn[linenum][i + 1] = 0;
}
currentWidth = 0;
++linenum;
i = -1;
}
2010-09-24 02:48:03 +02:00
if (text[ch] == ' ' && i >= 0)
{
lastSpace = ch;
lastSpaceIndex = i;
}
++ch;
++i;
}
}
/**
* Draw the text on screen
*/
void GuiText::Draw()
{
2010-09-24 02:48:03 +02:00
if (!text) return;
2010-09-24 02:48:03 +02:00
if (!IsVisible()) return;
GXColor c = color;
c.a = GetAlpha();
int newSize = size * GetScale();
2010-09-24 02:48:03 +02:00
if (newSize != currentSize)
{
currentSize = newSize;
2010-09-24 02:48:03 +02:00
if (text) textWidth = (font ? font : fontSystem)->getWidth(text, currentSize);
}
2010-09-24 02:48:03 +02:00
if (maxWidth > 0 && maxWidth <= textWidth)
{
2010-09-24 02:48:03 +02:00
if (wrapMode == DOTTED) // text dotted
{
2010-09-24 02:48:03 +02:00
if (textDyn.size() == 0) MakeDottedText();
2010-09-24 02:48:03 +02:00
if (textDyn.size() > 0) (font ? font : fontSystem)->drawText(this->GetLeft(), this->GetTop(), 0,
textDyn[textDyn.size() - 1], currentSize, c, style);
}
2010-09-24 02:48:03 +02:00
else if (wrapMode == SCROLL_HORIZONTAL)
{
ScrollText();
2010-09-24 02:48:03 +02:00
if (textDyn.size() > 0) (font ? font : fontSystem)->drawText(this->GetLeft(), this->GetTop(), 0,
textDyn[textDyn.size() - 1], currentSize, c, style);
}
2010-09-24 02:48:03 +02:00
else if (wrapMode == WRAP)
{
int lineheight = currentSize + 6;
int voffset = 0;
2010-09-24 02:48:03 +02:00
if (alignmentVert == ALIGN_MIDDLE) voffset = -(lineheight * textDyn.size()) / 2 + lineheight / 2;
2010-09-24 02:48:03 +02:00
if (textDyn.size() == 0) WrapText();
2010-09-24 02:48:03 +02:00
for (u32 i = 0; i < textDyn.size(); i++)
{
2010-09-24 02:48:03 +02:00
(font ? font : fontSystem)->drawText(this->GetLeft(), this->GetTop() + voffset + i * lineheight, 0,
textDyn[i], currentSize, c, style);
}
}
}
else
{
2010-09-24 02:48:03 +02:00
(font ? font : fontSystem)->drawText(this->GetLeft(), this->GetTop(), 0, text, currentSize, c, style, textWidth);
}
this->UpdateEffects();
}