vbagx/source/gui/gui_text.cpp

481 lines
8.7 KiB
C++
Raw Normal View History

2009-04-07 04:57:49 +02:00
/****************************************************************************
2009-04-10 09:51:55 +02:00
* libwiigui
2009-04-07 04:57:49 +02:00
*
2009-04-10 09:51:55 +02:00
* Tantric 2009
2009-04-07 04:57:49 +02:00
*
* gui_text.cpp
*
* GUI class definitions
***************************************************************************/
#include "gui.h"
2010-03-22 00:49:59 +01:00
#include "../utils/gettext.h"
2009-04-07 04:57:49 +02:00
2010-01-25 08:36:48 +01:00
static GXColor presetColor = (GXColor){255, 255, 255, 255};
2009-04-07 04:57:49 +02: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;
#define TEXT_SCROLL_DELAY 8
#define TEXT_SCROLL_INITIAL_DELAY 6
2009-04-07 04:57:49 +02:00
/**
* Constructor for the GuiText class.
*/
GuiText::GuiText(const char * t, int s, GXColor c)
{
origText = NULL;
2009-04-07 04:57:49 +02:00
text = NULL;
size = s;
color = c;
alpha = c.a;
style = FTGX_JUSTIFY_CENTER | FTGX_ALIGN_MIDDLE;
maxWidth = 0;
wrap = false;
2010-03-22 09:11:14 +01:00
textDynNum = 0;
textScroll = SCROLL_NONE;
textScrollPos = 0;
textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
textScrollDelay = TEXT_SCROLL_DELAY;
2009-04-07 04:57:49 +02:00
alignmentHor = ALIGN_CENTRE;
alignmentVert = ALIGN_MIDDLE;
if(t)
{
origText = strdup(t);
2010-01-25 09:25:28 +01:00
text = charToWideChar(gettext(t));
}
2010-05-19 23:52:59 +02:00
for(int i=0; i < 20; i++)
textDyn[i] = NULL;
2009-04-07 04:57:49 +02:00
}
/**
* Constructor for the GuiText class, uses presets
*/
GuiText::GuiText(const char * t)
{
origText = NULL;
2009-04-07 04:57:49 +02:00
text = NULL;
size = presetSize;
color = presetColor;
alpha = presetColor.a;
style = presetStyle;
maxWidth = presetMaxWidth;
wrap = false;
2010-03-22 09:11:14 +01:00
textDynNum = 0;
textScroll = SCROLL_NONE;
textScrollPos = 0;
textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
textScrollDelay = TEXT_SCROLL_DELAY;
2009-04-07 04:57:49 +02:00
alignmentHor = presetAlignmentHor;
alignmentVert = presetAlignmentVert;
if(t)
{
origText = strdup(t);
2010-01-25 09:25:28 +01:00
text = charToWideChar(gettext(t));
}
2010-05-19 23:52:59 +02:00
for(int i=0; i < 20; i++)
textDyn[i] = NULL;
2009-04-07 04:57:49 +02:00
}
/**
* Destructor for the GuiText class.
*/
GuiText::~GuiText()
{
if(origText)
free(origText);
2009-04-07 04:57:49 +02:00
if(text)
2009-09-25 20:37:17 +02:00
delete[] text;
2010-03-22 09:11:14 +01:00
if(textDynNum > 0)
{
for(int i=0; i < textDynNum; i++)
2010-05-19 23:52:59 +02:00
if(textDyn[i])
delete[] textDyn[i];
2010-03-22 09:11:14 +01:00
}
2009-04-07 04:57:49 +02:00
}
void GuiText::SetText(const char * t)
{
if(origText)
free(origText);
2009-04-07 04:57:49 +02:00
if(text)
2009-09-25 20:37:17 +02:00
delete[] text;
2010-03-22 09:11:14 +01:00
if(textDynNum > 0)
{
for(int i=0; i < textDynNum; i++)
2010-05-19 23:52:59 +02:00
if(textDyn[i])
delete[] textDyn[i];
2010-03-22 09:11:14 +01:00
}
2009-04-07 04:57:49 +02:00
origText = NULL;
2009-04-07 04:57:49 +02:00
text = NULL;
2010-03-22 09:11:14 +01:00
textDynNum = 0;
textScrollPos = 0;
textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
2009-04-07 04:57:49 +02:00
if(t)
{
origText = strdup(t);
2010-01-25 09:25:28 +01:00
text = charToWideChar(gettext(t));
}
2009-04-07 04:57:49 +02:00
}
2010-05-19 23:52:59 +02:00
void GuiText::SetWText(wchar_t * t)
{
if(origText)
free(origText);
if(text)
delete[] text;
if(textDynNum > 0)
{
for(int i=0; i < textDynNum; i++)
if(textDyn[i])
delete[] textDyn[i];
}
origText = NULL;
text = NULL;
textDynNum = 0;
textScrollPos = 0;
textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
if(t)
text = wcsdup(t);
}
int GuiText::GetLength()
{
if(!text)
return 0;
return wcslen(text);
}
2009-04-07 04:57:49 +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;
}
void GuiText::SetFontSize(int s)
{
size = s;
}
void GuiText::SetMaxWidth(int width)
{
maxWidth = width;
2010-03-22 09:11:14 +01:00
2010-05-19 23:52:59 +02:00
for(int i=0; i < textDynNum; i++)
2010-03-22 09:11:14 +01:00
{
2010-05-19 23:52:59 +02:00
if(textDyn[i])
{
2010-03-22 09:11:14 +01:00
delete[] textDyn[i];
2010-05-19 23:52:59 +02:00
textDyn[i] = NULL;
}
2010-03-22 09:11:14 +01:00
}
2010-05-19 23:52:59 +02:00
2010-03-22 09:11:14 +01:00
textDynNum = 0;
}
2010-05-19 23:52:59 +02:00
int GuiText::GetTextWidth()
{
if(!text)
return 0;
if(currentSize != size)
{
ChangeFontSize(size);
if(!fontSystem[size])
fontSystem[size] = new FreeTypeGX(size);
currentSize = size;
}
return fontSystem[size]->getWidth(text);
}
void GuiText::SetWrap(bool w, int width)
2009-04-07 04:57:49 +02:00
{
wrap = w;
maxWidth = width;
2010-03-22 09:11:14 +01:00
2010-05-19 23:52:59 +02:00
for(int i=0; i < textDynNum; i++)
2010-03-22 09:11:14 +01:00
{
2010-05-19 23:52:59 +02:00
if(textDyn[i])
{
2010-03-22 09:11:14 +01:00
delete[] textDyn[i];
2010-05-19 23:52:59 +02:00
textDyn[i] = NULL;
}
2010-03-22 09:11:14 +01:00
}
2010-05-19 23:52:59 +02:00
2010-03-22 09:11:14 +01:00
textDynNum = 0;
}
void GuiText::SetScroll(int s)
{
if(textScroll == s)
return;
2010-05-19 23:52:59 +02:00
for(int i=0; i < textDynNum; i++)
{
2010-05-19 23:52:59 +02:00
if(textDyn[i])
{
2010-03-22 09:11:14 +01:00
delete[] textDyn[i];
2010-05-19 23:52:59 +02:00
textDyn[i] = NULL;
}
}
2010-05-19 23:52:59 +02:00
2010-03-22 09:11:14 +01:00
textDynNum = 0;
textScroll = s;
textScrollPos = 0;
textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
textScrollDelay = TEXT_SCROLL_DELAY;
2009-04-07 04:57:49 +02: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:25:28 +01:00
void GuiText::ResetText()
{
if(!origText)
return;
if(text)
delete[] text;
text = charToWideChar(gettext(origText));
2010-03-22 09:11:14 +01:00
2010-05-19 23:52:59 +02:00
for(int i=0; i < textDynNum; i++)
2010-03-22 09:11:14 +01:00
{
2010-05-19 23:52:59 +02:00
if(textDyn[i])
{
2010-03-22 09:11:14 +01:00
delete[] textDyn[i];
2010-05-19 23:52:59 +02:00
textDyn[i] = NULL;
}
2010-03-22 09:11:14 +01:00
}
2010-05-19 23:52:59 +02:00
2010-03-22 09:11:14 +01:00
textDynNum = 0;
2010-05-19 23:52:59 +02:00
currentSize = 0;
2010-01-25 09:25:28 +01:00
}
2009-04-07 04:57:49 +02: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 07:39:01 +02:00
if(newSize > MAX_FONT_SIZE)
newSize = MAX_FONT_SIZE;
2009-04-07 04:57:49 +02:00
if(newSize != currentSize)
{
ChangeFontSize(newSize);
if(!fontSystem[newSize])
fontSystem[newSize] = new FreeTypeGX(newSize);
2009-04-07 04:57:49 +02:00
currentSize = newSize;
}
if(maxWidth == 0)
2009-04-07 04:57:49 +02:00
{
fontSystem[currentSize]->drawText(this->GetLeft(), this->GetTop(), text, c, style);
2010-03-22 09:11:14 +01:00
this->UpdateEffects();
return;
}
2010-03-22 09:11:14 +01:00
u32 textlen = wcslen(text);
2009-04-07 04:57:49 +02:00
if(wrap)
{
2010-03-22 09:11:14 +01:00
if(textDynNum == 0)
{
2010-03-22 09:11:14 +01:00
u32 n = 0, ch = 0;
int linenum = 0;
int lastSpace = -1;
int lastSpaceIndex = -1;
2009-04-07 04:57:49 +02:00
2010-03-22 09:11:14 +01:00
while(ch < textlen && linenum < 20)
2009-04-07 04:57:49 +02:00
{
2010-03-22 09:11:14 +01:00
if(n == 0)
textDyn[linenum] = new wchar_t[textlen + 1];
textDyn[linenum][n] = text[ch];
textDyn[linenum][n+1] = 0;
if(text[ch] == ' ' || ch == textlen-1)
2009-04-07 04:57:49 +02:00
{
2010-05-19 23:52:59 +02:00
if(fontSystem[currentSize]->getWidth(textDyn[linenum]) > maxWidth)
2010-03-22 09:11:14 +01: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;
}
++linenum;
n = -1;
}
else if(ch == textlen-1)
{
2010-03-22 09:11:14 +01:00
++linenum;
}
}
2010-03-22 09:11:14 +01:00
if(text[ch] == ' ' && n >= 0)
{
2010-03-22 09:11:14 +01:00
lastSpace = ch;
lastSpaceIndex = n;
}
2010-03-22 09:11:14 +01:00
++ch;
++n;
}
2010-03-22 09:11:14 +01:00
textDynNum = linenum;
}
2010-03-22 09:11:14 +01:00
int lineheight = newSize + 6;
int voffset = 0;
if(alignmentVert == ALIGN_MIDDLE)
2010-03-22 09:11:14 +01:00
voffset = (lineheight >> 1) * (1-textDynNum);
int left = this->GetLeft();
int top = this->GetTop() + voffset;
2010-03-22 09:11:14 +01:00
for(int i=0; i < textDynNum; ++i)
fontSystem[currentSize]->drawText(left, top+i*lineheight, textDyn[i], c, style);
}
2010-03-22 09:11:14 +01:00
else
{
2010-03-22 09:11:14 +01:00
if(textDynNum == 0)
{
textDynNum = 1;
textDyn[0] = wcsdup(text);
2010-05-19 23:52:59 +02:00
int len = wcslen(textDyn[0]);
2010-05-19 23:52:59 +02:00
while(fontSystem[currentSize]->getWidth(textDyn[0]) > maxWidth)
textDyn[0][--len] = 0;
2010-03-22 09:11:14 +01:00
}
if(textScroll == SCROLL_HORIZONTAL)
{
2010-05-19 23:52:59 +02:00
if(fontSystem[currentSize]->getWidth(text) > maxWidth && (FrameTimer % textScrollDelay == 0))
{
2010-03-22 09:11:14 +01:00
if(textScrollInitialDelay)
2009-04-07 04:57:49 +02:00
{
2010-03-22 09:11:14 +01:00
--textScrollInitialDelay;
2009-04-07 04:57:49 +02:00
}
2010-03-22 09:11:14 +01:00
else
{
++textScrollPos;
if((u32)textScrollPos > textlen-1)
{
textScrollPos = 0;
textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
}
2009-08-21 09:20:13 +02:00
2010-05-19 23:52:59 +02:00
wcscpy(textDyn[0], &text[textScrollPos]);
2010-03-22 09:11:14 +01:00
u32 dynlen = wcslen(textDyn[0]);
2010-05-19 23:52:59 +02:00
if(dynlen+2 < textlen)
2010-03-22 09:11:14 +01:00
{
textDyn[0][dynlen] = ' ';
textDyn[0][dynlen+1] = ' ';
2010-05-19 23:52:59 +02:00
textDyn[0][dynlen+2] = 0;
dynlen += 2;
}
if(fontSystem[currentSize]->getWidth(textDyn[0]) > maxWidth)
{
while(fontSystem[currentSize]->getWidth(textDyn[0]) > maxWidth)
textDyn[0][--dynlen] = 0;
}
else
{
int i = 0;
while(fontSystem[currentSize]->getWidth(textDyn[0]) < maxWidth && dynlen+1 < textlen)
{
textDyn[0][dynlen] = text[i++];
textDyn[0][++dynlen] = 0;
}
if(fontSystem[currentSize]->getWidth(textDyn[0]) > maxWidth)
textDyn[0][dynlen-2] = 0;
else
textDyn[0][dynlen-1] = 0;
2010-03-22 09:11:14 +01:00
}
}
2009-04-07 04:57:49 +02:00
}
}
2010-03-22 09:11:14 +01:00
fontSystem[currentSize]->drawText(this->GetLeft(), this->GetTop(), textDyn[0], c, style);
2009-04-07 04:57:49 +02:00
}
this->UpdateEffects();
}