2009-03-27 05:02:39 +01:00
|
|
|
/****************************************************************************
|
2009-04-10 09:50:10 +02:00
|
|
|
* libwiigui
|
2009-03-27 05:02:39 +01:00
|
|
|
*
|
2009-04-10 09:50:10 +02:00
|
|
|
* Tantric 2009
|
2009-03-27 05:02:39 +01:00
|
|
|
*
|
|
|
|
* gui_text.cpp
|
|
|
|
*
|
|
|
|
* GUI class definitions
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "gui.h"
|
2010-03-22 00:49:24 +01:00
|
|
|
#include "../utils/gettext.h"
|
2009-03-27 05:02:39 +01:00
|
|
|
|
2010-01-25 08:33:35 +01:00
|
|
|
static GXColor presetColor = (GXColor){255, 255, 255, 255};
|
2009-03-27 05:02:39 +01:00
|
|
|
static int currentSize = 0;
|
|
|
|
static int presetSize = 0;
|
|
|
|
static int presetMaxWidth = 0;
|
|
|
|
static int presetAlignmentHor = 0;
|
|
|
|
static int presetAlignmentVert = 0;
|
|
|
|
static u16 presetStyle = 0;
|
|
|
|
|
2009-06-27 21:12:47 +02:00
|
|
|
#define TEXT_SCROLL_DELAY 8
|
|
|
|
#define TEXT_SCROLL_INITIAL_DELAY 6
|
|
|
|
|
2009-03-27 05:02:39 +01:00
|
|
|
/**
|
|
|
|
* Constructor for the GuiText class.
|
|
|
|
*/
|
|
|
|
GuiText::GuiText(const char * t, int s, GXColor c)
|
|
|
|
{
|
2009-06-27 21:12:47 +02:00
|
|
|
origText = NULL;
|
2009-03-27 05:02:39 +01:00
|
|
|
text = NULL;
|
|
|
|
size = s;
|
|
|
|
color = c;
|
|
|
|
alpha = c.a;
|
|
|
|
style = FTGX_JUSTIFY_CENTER | FTGX_ALIGN_MIDDLE;
|
|
|
|
maxWidth = 0;
|
2009-06-27 21:12:47 +02:00
|
|
|
wrap = false;
|
|
|
|
textDyn = NULL;
|
|
|
|
textScroll = SCROLL_NONE;
|
|
|
|
textScrollPos = 0;
|
|
|
|
textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
|
|
|
|
textScrollDelay = TEXT_SCROLL_DELAY;
|
2009-03-27 05:02:39 +01:00
|
|
|
|
|
|
|
alignmentHor = ALIGN_CENTRE;
|
|
|
|
alignmentVert = ALIGN_MIDDLE;
|
|
|
|
|
|
|
|
if(t)
|
2009-06-27 21:12:47 +02:00
|
|
|
{
|
|
|
|
origText = strdup(t);
|
2010-01-25 09:23:03 +01:00
|
|
|
text = charToWideChar(gettext(t));
|
2009-06-27 21:12:47 +02:00
|
|
|
}
|
2009-03-27 05:02:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for the GuiText class, uses presets
|
|
|
|
*/
|
|
|
|
GuiText::GuiText(const char * t)
|
|
|
|
{
|
2009-06-27 21:12:47 +02:00
|
|
|
origText = NULL;
|
2009-03-27 05:02:39 +01:00
|
|
|
text = NULL;
|
|
|
|
size = presetSize;
|
|
|
|
color = presetColor;
|
|
|
|
alpha = presetColor.a;
|
|
|
|
style = presetStyle;
|
|
|
|
maxWidth = presetMaxWidth;
|
2009-06-27 21:12:47 +02:00
|
|
|
wrap = false;
|
|
|
|
textDyn = NULL;
|
|
|
|
textScroll = SCROLL_NONE;
|
|
|
|
textScrollPos = 0;
|
|
|
|
textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
|
|
|
|
textScrollDelay = TEXT_SCROLL_DELAY;
|
2009-03-27 05:02:39 +01:00
|
|
|
|
|
|
|
alignmentHor = presetAlignmentHor;
|
|
|
|
alignmentVert = presetAlignmentVert;
|
|
|
|
|
|
|
|
if(t)
|
2009-06-27 21:12:47 +02:00
|
|
|
{
|
|
|
|
origText = strdup(t);
|
2010-01-25 09:23:03 +01:00
|
|
|
text = charToWideChar(gettext(t));
|
2009-06-27 21:12:47 +02:00
|
|
|
}
|
2009-03-27 05:02:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor for the GuiText class.
|
|
|
|
*/
|
|
|
|
GuiText::~GuiText()
|
|
|
|
{
|
2009-06-27 21:12:47 +02:00
|
|
|
if(origText)
|
|
|
|
free(origText);
|
2009-03-27 05:02:39 +01:00
|
|
|
if(text)
|
2009-09-25 20:36:26 +02:00
|
|
|
delete[] text;
|
2009-06-27 21:12:47 +02:00
|
|
|
if(textDyn)
|
2009-09-25 20:36:26 +02:00
|
|
|
delete[] textDyn;
|
2009-03-27 05:02:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiText::SetText(const char * t)
|
|
|
|
{
|
2009-06-27 21:12:47 +02:00
|
|
|
if(origText)
|
|
|
|
free(origText);
|
2009-03-27 05:02:39 +01:00
|
|
|
if(text)
|
2009-09-25 20:36:26 +02:00
|
|
|
delete[] text;
|
2009-06-27 21:12:47 +02:00
|
|
|
if(textDyn)
|
2009-09-25 20:36:26 +02:00
|
|
|
delete[] textDyn;
|
2009-03-27 05:02:39 +01:00
|
|
|
|
2009-06-27 21:12:47 +02:00
|
|
|
origText = NULL;
|
2009-03-27 05:02:39 +01:00
|
|
|
text = NULL;
|
2009-06-27 21:12:47 +02:00
|
|
|
textDyn = NULL;
|
|
|
|
textScrollPos = 0;
|
|
|
|
textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
|
2009-03-27 05:02:39 +01:00
|
|
|
|
|
|
|
if(t)
|
2009-06-27 21:12:47 +02:00
|
|
|
{
|
|
|
|
origText = strdup(t);
|
2010-01-25 09:23:03 +01:00
|
|
|
text = charToWideChar(gettext(t));
|
2009-06-27 21:12:47 +02:00
|
|
|
}
|
2009-03-27 05:02:39 +01: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiText::SetFontSize(int s)
|
|
|
|
{
|
|
|
|
size = s;
|
|
|
|
}
|
|
|
|
|
2009-06-27 21:12:47 +02:00
|
|
|
void GuiText::SetMaxWidth(int width)
|
|
|
|
{
|
|
|
|
maxWidth = width;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiText::SetWrap(bool w, int width)
|
2009-03-27 05:02:39 +01:00
|
|
|
{
|
2009-06-27 21:12:47 +02:00
|
|
|
wrap = w;
|
|
|
|
maxWidth = width;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiText::SetScroll(int s)
|
|
|
|
{
|
|
|
|
if(textScroll == s)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(textDyn)
|
|
|
|
{
|
2009-09-25 20:36:26 +02:00
|
|
|
delete[] textDyn;
|
2009-06-27 21:12:47 +02:00
|
|
|
textDyn = NULL;
|
|
|
|
}
|
|
|
|
textScroll = s;
|
|
|
|
textScrollPos = 0;
|
|
|
|
textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
|
|
|
|
textScrollDelay = TEXT_SCROLL_DELAY;
|
2009-03-27 05:02:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiText::SetColor(GXColor c)
|
|
|
|
{
|
|
|
|
color = c;
|
|
|
|
alpha = c.a;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiText::SetStyle(u16 s)
|
|
|
|
{
|
|
|
|
style = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiText::SetAlignment(int hor, int vert)
|
|
|
|
{
|
|
|
|
style = 0;
|
|
|
|
|
|
|
|
switch(hor)
|
|
|
|
{
|
|
|
|
case ALIGN_LEFT:
|
|
|
|
style |= FTGX_JUSTIFY_LEFT;
|
|
|
|
break;
|
|
|
|
case ALIGN_RIGHT:
|
|
|
|
style |= FTGX_JUSTIFY_RIGHT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
style |= FTGX_JUSTIFY_CENTER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
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-01-25 09:23:03 +01:00
|
|
|
void GuiText::ResetText()
|
|
|
|
{
|
|
|
|
if(!origText)
|
|
|
|
return;
|
|
|
|
if(text)
|
|
|
|
delete[] text;
|
|
|
|
|
|
|
|
text = charToWideChar(gettext(origText));
|
|
|
|
}
|
|
|
|
|
2009-03-27 05:02:39 +01:00
|
|
|
/**
|
|
|
|
* Draw the text on screen
|
|
|
|
*/
|
|
|
|
void GuiText::Draw()
|
|
|
|
{
|
|
|
|
if(!text)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!this->IsVisible())
|
|
|
|
return;
|
|
|
|
|
|
|
|
GXColor c = color;
|
|
|
|
c.a = this->GetAlpha();
|
|
|
|
|
|
|
|
int newSize = size*this->GetScale();
|
|
|
|
|
2009-08-12 08:02:23 +02:00
|
|
|
if(newSize > MAX_FONT_SIZE)
|
|
|
|
newSize = MAX_FONT_SIZE;
|
|
|
|
|
2009-03-27 05:02:39 +01:00
|
|
|
if(newSize != currentSize)
|
|
|
|
{
|
2009-07-08 09:14:38 +02:00
|
|
|
ChangeFontSize(newSize);
|
|
|
|
if(!fontSystem[newSize])
|
|
|
|
fontSystem[newSize] = new FreeTypeGX(newSize);
|
2009-03-27 05:02:39 +01:00
|
|
|
currentSize = newSize;
|
|
|
|
}
|
2010-03-21 20:52:11 +01:00
|
|
|
|
|
|
|
u8 maxChar;
|
2009-03-27 05:02:39 +01:00
|
|
|
|
2010-03-21 20:52:11 +01:00
|
|
|
if(maxWidth == 0)
|
2009-03-27 05:02:39 +01:00
|
|
|
{
|
2010-03-21 20:52:11 +01:00
|
|
|
fontSystem[currentSize]->drawText(this->GetLeft(), this->GetTop(), text, c, style);
|
|
|
|
goto done;
|
|
|
|
}
|
2009-06-27 21:12:47 +02:00
|
|
|
|
2010-03-21 20:52:11 +01:00
|
|
|
maxChar = int((float((maxWidth<<1))) / (float(newSize))); // approximate
|
2009-03-27 05:02:39 +01:00
|
|
|
|
2010-03-21 20:52:11 +01:00
|
|
|
if(wrap)
|
|
|
|
{
|
|
|
|
int lineheight = newSize + 6;
|
|
|
|
int txtlen = wcslen(text);
|
|
|
|
int i = 0;
|
|
|
|
int ch = 0;
|
|
|
|
int linenum = 0;
|
|
|
|
int lastSpace = -1;
|
|
|
|
int lastSpaceIndex = -1;
|
|
|
|
wchar_t * textrow[20];
|
|
|
|
|
|
|
|
while(ch < txtlen)
|
2009-06-27 21:12:47 +02:00
|
|
|
{
|
2010-03-21 20:52:11 +01:00
|
|
|
if(i == 0)
|
|
|
|
textrow[linenum] = new wchar_t[txtlen + 1];
|
|
|
|
|
|
|
|
textrow[linenum][i] = text[ch];
|
|
|
|
textrow[linenum][i+1] = 0;
|
2009-03-27 05:02:39 +01:00
|
|
|
|
2010-03-21 20:52:11 +01:00
|
|
|
if(text[ch] == ' ' || ch == txtlen-1)
|
2009-03-27 05:02:39 +01:00
|
|
|
{
|
2010-03-21 20:52:11 +01:00
|
|
|
if(wcslen(textrow[linenum]) >= maxChar)
|
2009-03-27 05:02:39 +01:00
|
|
|
{
|
2010-03-21 20:52:11 +01:00
|
|
|
if(lastSpace >= 0)
|
2009-06-27 21:12:47 +02:00
|
|
|
{
|
2010-03-21 20:52:11 +01:00
|
|
|
textrow[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;
|
2009-06-27 21:12:47 +02:00
|
|
|
}
|
2010-03-21 20:52:11 +01:00
|
|
|
++linenum;
|
|
|
|
i = -1;
|
|
|
|
}
|
|
|
|
else if(ch == txtlen-1)
|
|
|
|
{
|
|
|
|
++linenum;
|
2009-06-27 21:12:47 +02:00
|
|
|
}
|
|
|
|
}
|
2010-03-21 20:52:11 +01:00
|
|
|
if(text[ch] == ' ' && i >= 0)
|
|
|
|
{
|
|
|
|
lastSpace = ch;
|
|
|
|
lastSpaceIndex = i;
|
|
|
|
}
|
|
|
|
++ch;
|
|
|
|
++i;
|
2009-06-27 21:12:47 +02:00
|
|
|
}
|
2010-03-21 20:52:11 +01:00
|
|
|
|
|
|
|
int voffset = 0;
|
|
|
|
|
|
|
|
if(alignmentVert == ALIGN_MIDDLE)
|
|
|
|
voffset = (lineheight >> 1) * (1-linenum);
|
|
|
|
|
|
|
|
int left = this->GetLeft();
|
|
|
|
int top = this->GetTop() + voffset;
|
|
|
|
|
|
|
|
for(i=0; i < linenum; ++i)
|
2009-06-27 21:12:47 +02:00
|
|
|
{
|
2010-03-21 20:52:11 +01:00
|
|
|
fontSystem[currentSize]->drawText(left, top+i*lineheight, textrow[i], c, style);
|
|
|
|
delete[] textrow[i];
|
|
|
|
}
|
|
|
|
goto done;
|
|
|
|
}
|
2009-06-27 21:12:47 +02:00
|
|
|
|
2010-03-21 20:52:11 +01:00
|
|
|
if(textScroll == SCROLL_HORIZONTAL)
|
|
|
|
{
|
|
|
|
char *tmpText = strdup(gettext(origText));
|
|
|
|
char *tmpText2 = strdup(tmpText);
|
|
|
|
int textlen = strlen(tmpText);
|
2009-06-27 21:12:47 +02:00
|
|
|
|
2010-03-21 20:52:11 +01:00
|
|
|
if(textlen > maxChar && (FrameTimer % textScrollDelay == 0))
|
|
|
|
{
|
|
|
|
if(textScrollInitialDelay)
|
|
|
|
{
|
|
|
|
--textScrollInitialDelay;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++textScrollPos;
|
|
|
|
if(textScrollPos > textlen-1)
|
2009-03-27 05:02:39 +01:00
|
|
|
{
|
2010-03-21 20:52:11 +01:00
|
|
|
textScrollPos = 0;
|
|
|
|
textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
|
2009-03-27 05:02:39 +01:00
|
|
|
}
|
2009-08-21 09:19:35 +02:00
|
|
|
|
2010-03-21 20:52:11 +01:00
|
|
|
strncpy(tmpText, &tmpText2[textScrollPos], maxChar-1);
|
|
|
|
tmpText[maxChar-1] = 0;
|
2010-01-25 08:33:35 +01:00
|
|
|
|
2010-03-21 20:52:11 +01:00
|
|
|
int dynlen = strlen(tmpText);
|
2009-06-27 21:12:47 +02:00
|
|
|
|
2010-03-21 20:52:11 +01:00
|
|
|
if(dynlen+2 < maxChar)
|
|
|
|
{
|
|
|
|
tmpText[dynlen] = ' ';
|
|
|
|
tmpText[dynlen+1] = ' ';
|
|
|
|
strncat(&tmpText[dynlen+2], tmpText2, maxChar - dynlen - 2);
|
|
|
|
}
|
|
|
|
if(textDyn) delete[] textDyn;
|
|
|
|
textDyn = charToWideChar(tmpText);
|
2009-03-27 05:02:39 +01:00
|
|
|
}
|
|
|
|
}
|
2009-06-27 21:12:47 +02:00
|
|
|
free(tmpText);
|
2010-03-21 20:52:11 +01:00
|
|
|
free(tmpText2);
|
2009-03-27 05:02:39 +01:00
|
|
|
}
|
2010-03-21 20:52:11 +01:00
|
|
|
|
|
|
|
if(!textDyn)
|
2009-03-27 05:02:39 +01:00
|
|
|
{
|
2010-03-21 20:52:11 +01:00
|
|
|
char *tmpText = strdup(gettext(origText));
|
|
|
|
if(strlen(tmpText) > maxChar)
|
|
|
|
tmpText[maxChar] = 0;
|
|
|
|
textDyn = charToWideChar(tmpText);
|
|
|
|
free(tmpText);
|
2009-03-27 05:02:39 +01:00
|
|
|
}
|
2010-03-21 20:52:11 +01:00
|
|
|
|
|
|
|
fontSystem[currentSize]->drawText(this->GetLeft(), this->GetTop(), textDyn, c, style);
|
|
|
|
done:
|
2009-03-27 05:02:39 +01:00
|
|
|
this->UpdateEffects();
|
|
|
|
}
|