2009-05-03 20:53:31 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* libwiigui
|
|
|
|
*
|
|
|
|
* Tantric 2009
|
|
|
|
*
|
|
|
|
* gui_text.cpp
|
|
|
|
*
|
|
|
|
* GUI class definitions
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "gui.h"
|
|
|
|
|
|
|
|
static int currentSize = 0;
|
2009-05-20 22:09:57 +02:00
|
|
|
static int currentWidescreen = 0;
|
2009-05-03 20:53:31 +02:00
|
|
|
static int presetSize = 0;
|
2009-05-26 23:54:30 +02:00
|
|
|
static GXColor presetColor = (GXColor){255, 255, 255, 255};
|
2009-05-03 20:53:31 +02:00
|
|
|
static int presetMaxWidth = 0;
|
2009-05-26 23:54:30 +02:00
|
|
|
static int presetWrapMode = 0;
|
|
|
|
static u16 presetStyle = 0;
|
2009-05-03 20:53:31 +02:00
|
|
|
static int presetAlignmentHor = 0;
|
|
|
|
static int presetAlignmentVert = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for the GuiText class.
|
|
|
|
*/
|
|
|
|
GuiText::GuiText(const char * t, int s, GXColor c)
|
|
|
|
{
|
|
|
|
text = NULL;
|
|
|
|
size = s;
|
|
|
|
color = c;
|
|
|
|
alpha = c.a;
|
|
|
|
style = FTGX_JUSTIFY_CENTER | FTGX_ALIGN_MIDDLE;
|
|
|
|
maxWidth = 0;
|
2009-05-23 00:36:55 +02:00
|
|
|
wrapMode = GuiText::WRAP;
|
|
|
|
scrollPos1 = 0;
|
|
|
|
scrollPos2 = 0;
|
|
|
|
scrollDelay = 0;
|
2009-05-26 23:54:30 +02:00
|
|
|
font = fontSystem;
|
2009-05-08 00:46:54 +02:00
|
|
|
widescreen = 0; //added
|
2009-05-03 20:53:31 +02:00
|
|
|
|
|
|
|
alignmentHor = ALIGN_CENTRE;
|
|
|
|
alignmentVert = ALIGN_MIDDLE;
|
|
|
|
|
|
|
|
if(t)
|
2009-05-26 23:54:30 +02:00
|
|
|
text = FreeTypeGX::charToWideChar((char *)t);
|
2009-05-03 20:53:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for the GuiText class, uses presets
|
|
|
|
*/
|
|
|
|
GuiText::GuiText(const char * t)
|
|
|
|
{
|
|
|
|
text = NULL;
|
|
|
|
size = presetSize;
|
|
|
|
color = presetColor;
|
|
|
|
alpha = presetColor.a;
|
|
|
|
style = presetStyle;
|
|
|
|
maxWidth = presetMaxWidth;
|
2009-05-23 00:36:55 +02:00
|
|
|
wrapMode = presetWrapMode;
|
|
|
|
scrollPos1 = 0;
|
|
|
|
scrollPos2 = 0;
|
|
|
|
scrollDelay = 0;
|
2009-05-26 23:54:30 +02:00
|
|
|
font = fontSystem;
|
2009-05-08 00:46:54 +02:00
|
|
|
widescreen = 0; //added
|
2009-05-03 20:53:31 +02:00
|
|
|
|
|
|
|
alignmentHor = presetAlignmentHor;
|
|
|
|
alignmentVert = presetAlignmentVert;
|
|
|
|
|
|
|
|
if(t)
|
2009-05-26 23:54:30 +02:00
|
|
|
text = FreeTypeGX::charToWideChar((char *)t);
|
2009-05-03 20:53:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor for the GuiText class.
|
|
|
|
*/
|
|
|
|
GuiText::~GuiText()
|
|
|
|
{
|
|
|
|
if(text)
|
|
|
|
{
|
2009-05-11 20:45:33 +02:00
|
|
|
delete [] text;
|
2009-05-03 20:53:31 +02:00
|
|
|
text = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiText::SetText(const char * t)
|
|
|
|
{
|
2009-05-07 00:36:28 +02:00
|
|
|
LOCK(this);
|
2009-05-03 20:53:31 +02:00
|
|
|
if(text)
|
2009-05-11 20:45:33 +02:00
|
|
|
delete [] text;
|
2009-05-03 20:53:31 +02:00
|
|
|
text = NULL;
|
|
|
|
|
|
|
|
if(t)
|
2009-05-26 23:54:30 +02:00
|
|
|
text = FreeTypeGX::charToWideChar((char *)t);
|
2009-05-23 00:36:55 +02:00
|
|
|
scrollPos2 = 0;
|
|
|
|
scrollDelay = 0;
|
2009-05-03 20:53:31 +02:00
|
|
|
}
|
|
|
|
|
2009-05-26 23:54:30 +02:00
|
|
|
void GuiText::SetPresets(int sz, GXColor c, int w, int wrap, u16 s, int h, int v)
|
2009-05-03 20:53:31 +02:00
|
|
|
{
|
|
|
|
presetSize = sz;
|
|
|
|
presetColor = c;
|
|
|
|
presetMaxWidth = w;
|
2009-05-26 23:54:30 +02:00
|
|
|
presetWrapMode = wrap;
|
|
|
|
presetStyle = s;
|
2009-05-03 20:53:31 +02:00
|
|
|
presetAlignmentHor = h;
|
|
|
|
presetAlignmentVert = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiText::SetFontSize(int s)
|
|
|
|
{
|
2009-05-07 00:36:28 +02:00
|
|
|
LOCK(this);
|
2009-05-03 20:53:31 +02:00
|
|
|
size = s;
|
|
|
|
}
|
|
|
|
|
2009-05-23 00:36:55 +02:00
|
|
|
void GuiText::SetMaxWidth(int w, short m/*=GuiText::WRAP*/)
|
2009-05-03 20:53:31 +02:00
|
|
|
{
|
2009-05-07 00:36:28 +02:00
|
|
|
LOCK(this);
|
2009-05-03 20:53:31 +02:00
|
|
|
maxWidth = w;
|
2009-05-23 00:36:55 +02:00
|
|
|
wrapMode = m;
|
2009-05-03 20:53:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiText::SetColor(GXColor c)
|
|
|
|
{
|
2009-05-07 00:36:28 +02:00
|
|
|
LOCK(this);
|
2009-05-03 20:53:31 +02:00
|
|
|
color = c;
|
|
|
|
alpha = c.a;
|
|
|
|
}
|
|
|
|
|
2009-05-26 23:54:30 +02:00
|
|
|
void GuiText::SetStyle(u16 s, u16 m/*=0xffff*/)
|
2009-05-03 20:53:31 +02:00
|
|
|
{
|
2009-05-07 00:36:28 +02:00
|
|
|
LOCK(this);
|
2009-05-26 23:54:30 +02:00
|
|
|
style &= ~m;
|
|
|
|
style |= s & m;
|
2009-05-03 20:53:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiText::SetAlignment(int hor, int vert)
|
|
|
|
{
|
2009-05-07 00:36:28 +02:00
|
|
|
LOCK(this);
|
2009-05-26 23:54:30 +02:00
|
|
|
style = FTGX_NULL;
|
2009-05-03 20:53:31 +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;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2009-05-04 23:50:46 +02:00
|
|
|
/**
|
|
|
|
* Set the Font
|
|
|
|
*/
|
|
|
|
void GuiText::SetFont(FreeTypeGX *f)
|
|
|
|
{
|
2009-05-07 00:36:28 +02:00
|
|
|
LOCK(this);
|
2009-05-26 23:54:30 +02:00
|
|
|
font = f ? f : fontSystem;
|
2009-05-04 23:50:46 +02:00
|
|
|
}
|
2009-05-07 00:36:28 +02:00
|
|
|
|
|
|
|
int GuiText::GetTextWidth()
|
|
|
|
{
|
|
|
|
LOCK(this);
|
|
|
|
int newSize = size*this->GetScale();
|
|
|
|
|
2009-05-20 22:09:57 +02:00
|
|
|
if(newSize != currentSize || currentWidescreen != widescreen)
|
2009-05-07 00:36:28 +02:00
|
|
|
{
|
2009-05-26 23:54:30 +02:00
|
|
|
font->changeSize(newSize, widescreen ? newSize*0.8 : 0);
|
2009-05-07 00:36:28 +02:00
|
|
|
currentSize = newSize;
|
2009-05-20 22:09:57 +02:00
|
|
|
currentWidescreen = widescreen;
|
2009-05-07 00:36:28 +02:00
|
|
|
}
|
2009-05-26 23:54:30 +02:00
|
|
|
return font->getWidth(text);
|
2009-05-07 00:36:28 +02:00
|
|
|
}
|
|
|
|
|
2009-05-20 22:09:57 +02:00
|
|
|
void GuiText::SetWidescreen(bool w)
|
|
|
|
{
|
|
|
|
LOCK(this);
|
|
|
|
widescreen = w;
|
|
|
|
}
|
2009-05-03 20:53:31 +02:00
|
|
|
/**
|
|
|
|
* Draw the text on screen
|
|
|
|
*/
|
|
|
|
void GuiText::Draw()
|
|
|
|
{
|
2009-05-07 00:36:28 +02:00
|
|
|
LOCK(this);
|
2009-05-03 20:53:31 +02:00
|
|
|
if(!text)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!this->IsVisible())
|
|
|
|
return;
|
|
|
|
|
|
|
|
GXColor c = color;
|
|
|
|
c.a = this->GetAlpha();
|
|
|
|
|
|
|
|
int newSize = size*this->GetScale();
|
|
|
|
|
2009-05-20 22:09:57 +02:00
|
|
|
if(newSize != currentSize || currentWidescreen != widescreen)
|
2009-05-03 20:53:31 +02:00
|
|
|
{
|
2009-05-26 23:54:30 +02:00
|
|
|
font->changeSize(newSize, widescreen ? newSize*0.8 : 0);
|
2009-05-03 20:53:31 +02:00
|
|
|
currentSize = newSize;
|
2009-05-20 22:09:57 +02:00
|
|
|
currentWidescreen = widescreen;
|
2009-05-03 20:53:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int voffset = 0;
|
|
|
|
|
2009-05-26 23:54:30 +02:00
|
|
|
// if(alignmentVert == ALIGN_MIDDLE)
|
|
|
|
// voffset = -newSize/2 + 2;
|
2009-05-03 20:53:31 +02:00
|
|
|
|
2009-05-26 23:54:30 +02:00
|
|
|
if(maxWidth > 0 && font->getWidth(text) > maxWidth)
|
2009-05-03 20:53:31 +02:00
|
|
|
{
|
2009-05-23 00:36:55 +02:00
|
|
|
if(wrapMode == GuiText::WRAP) // text wrapping
|
2009-05-03 20:53:31 +02:00
|
|
|
{
|
2009-05-23 00:36:55 +02:00
|
|
|
int lineheight = newSize + 6;
|
|
|
|
int strlen = wcslen(text);
|
|
|
|
int i = 0;
|
|
|
|
int ch = 0;
|
|
|
|
int linenum = 0;
|
|
|
|
int lastSpace = -1;
|
|
|
|
int lastSpaceIndex = -1;
|
|
|
|
wchar_t * tmptext[20];
|
|
|
|
|
|
|
|
while(ch < strlen)
|
|
|
|
{
|
|
|
|
if(i == 0)
|
|
|
|
tmptext[linenum] = new wchar_t[strlen + 1];
|
2009-05-03 20:53:31 +02:00
|
|
|
|
2009-05-23 00:36:55 +02:00
|
|
|
tmptext[linenum][i] = text[ch];
|
|
|
|
tmptext[linenum][i+1] = 0;
|
2009-05-03 20:53:31 +02:00
|
|
|
|
2009-05-23 00:36:55 +02:00
|
|
|
if(text[ch] == ' ' || ch == strlen-1)
|
2009-05-03 20:53:31 +02:00
|
|
|
{
|
2009-05-26 23:54:30 +02:00
|
|
|
if(font->getWidth(tmptext[linenum]) >= maxWidth)
|
2009-05-03 20:53:31 +02:00
|
|
|
{
|
2009-05-23 00:36:55 +02:00
|
|
|
if(lastSpace >= 0)
|
|
|
|
{
|
|
|
|
tmptext[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++;
|
|
|
|
i = -1;
|
|
|
|
}
|
|
|
|
else if(ch == strlen-1)
|
|
|
|
{
|
|
|
|
linenum++;
|
2009-05-03 20:53:31 +02:00
|
|
|
}
|
|
|
|
}
|
2009-05-23 00:36:55 +02:00
|
|
|
if(text[ch] == ' ' && i >= 0)
|
2009-05-03 20:53:31 +02:00
|
|
|
{
|
2009-05-23 00:36:55 +02:00
|
|
|
lastSpace = ch;
|
|
|
|
lastSpaceIndex = i;
|
2009-05-03 20:53:31 +02:00
|
|
|
}
|
2009-05-23 00:36:55 +02:00
|
|
|
ch++;
|
|
|
|
i++;
|
2009-05-03 20:53:31 +02:00
|
|
|
}
|
2009-05-23 00:36:55 +02:00
|
|
|
|
|
|
|
if(alignmentVert == ALIGN_MIDDLE)
|
|
|
|
voffset = voffset - (lineheight*linenum)/2 + lineheight/2;
|
|
|
|
|
|
|
|
for(i=0; i < linenum; i++)
|
2009-05-03 20:53:31 +02:00
|
|
|
{
|
2009-05-26 23:54:30 +02:00
|
|
|
font->drawText(this->GetLeft(), this->GetTop()+voffset+i*lineheight, tmptext[i], c, style);
|
2009-05-23 00:36:55 +02:00
|
|
|
delete tmptext[i];
|
2009-05-03 20:53:31 +02:00
|
|
|
}
|
|
|
|
}
|
2009-05-23 00:36:55 +02:00
|
|
|
else if(wrapMode == GuiText::DOTTED) // text dotted
|
2009-05-03 20:53:31 +02:00
|
|
|
{
|
2009-05-23 00:36:55 +02:00
|
|
|
wchar_t save[4];
|
|
|
|
int strlen = wcslen(text);
|
|
|
|
int dotPos=strlen-3;
|
|
|
|
int i;
|
|
|
|
bool drawed = false;
|
|
|
|
while(dotPos > 0 && drawed == false)
|
|
|
|
{
|
|
|
|
for(i=0; i<4; i++) // save Text for "..."
|
|
|
|
{
|
|
|
|
save[i] = text[dotPos+i];
|
|
|
|
text[dotPos+i] = (i != 3 ? _TEXT('.') : 0);
|
|
|
|
}
|
2009-05-26 23:54:30 +02:00
|
|
|
if((font->getWidth(text)) <= maxWidth)
|
2009-05-23 00:36:55 +02:00
|
|
|
{
|
2009-05-26 23:54:30 +02:00
|
|
|
font->drawText(this->GetLeft(), this->GetTop()+voffset, text, c, style);
|
2009-05-23 00:36:55 +02:00
|
|
|
drawed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0; i<4; i++) // write saved Text back
|
|
|
|
text[dotPos+i] = save[i];
|
|
|
|
dotPos--;
|
|
|
|
}
|
|
|
|
if(!drawed)
|
2009-05-26 23:54:30 +02:00
|
|
|
font->drawText(this->GetLeft(), this->GetTop()+voffset, text, c, style);
|
2009-05-03 20:53:31 +02:00
|
|
|
}
|
2009-05-23 00:36:55 +02:00
|
|
|
else if(wrapMode == GuiText::SCROLL) // text scroller
|
|
|
|
{
|
|
|
|
wchar_t save;
|
|
|
|
|
|
|
|
if(scrollPos2 == 0 || frameCount > scrollDelay+5)
|
|
|
|
{
|
|
|
|
scrollPos1 = 0;
|
2009-05-26 23:54:30 +02:00
|
|
|
scrollOffset = 0;
|
2009-05-23 00:36:55 +02:00
|
|
|
for(scrollPos2 = wcslen(text); scrollPos2 > 1; scrollPos2--)
|
|
|
|
{
|
|
|
|
save = text[scrollPos2]; // save Pos2
|
|
|
|
text[scrollPos2] = 0;
|
2009-05-26 23:54:30 +02:00
|
|
|
int textWidth = font->getWidth(text);
|
2009-05-23 00:36:55 +02:00
|
|
|
text[scrollPos2] = save; // restore Pos2
|
|
|
|
if(textWidth <= maxWidth)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
scrollDelay = frameCount+50; // wait 50 Frames before beginning with scrolling
|
|
|
|
}
|
|
|
|
else if(scrollPos2 > 0 && frameCount >= scrollDelay)
|
|
|
|
{
|
2009-05-26 23:54:30 +02:00
|
|
|
|
|
|
|
if(--scrollOffset < 0)
|
|
|
|
{
|
|
|
|
wchar_t tmp[] = { text[scrollPos1], text[scrollPos1+1], 0 };
|
|
|
|
scrollOffset += font->getWidth(tmp) - font->getWidth(tmp+1);
|
|
|
|
scrollPos1++;
|
|
|
|
}
|
|
|
|
|
2009-05-23 00:36:55 +02:00
|
|
|
int strlen = wcslen(text);
|
|
|
|
for(; scrollPos2 < strlen; scrollPos2++)
|
|
|
|
{
|
|
|
|
save = text[scrollPos2+1]; // save Pos2
|
|
|
|
text[scrollPos2+1] = 0;
|
2009-05-26 23:54:30 +02:00
|
|
|
int textWidth = font->getWidth(&text[scrollPos1]);
|
2009-05-23 00:36:55 +02:00
|
|
|
text[scrollPos2+1] = save; // restore Pos2
|
2009-05-26 23:54:30 +02:00
|
|
|
if(textWidth+scrollOffset > maxWidth)
|
2009-05-23 00:36:55 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(scrollPos2 == strlen)
|
|
|
|
{
|
|
|
|
scrollPos2 = -scrollPos2;
|
|
|
|
scrollDelay = frameCount+25; // when dir-change wait 25 Frames
|
|
|
|
}
|
|
|
|
else
|
2009-05-26 23:54:30 +02:00
|
|
|
scrollDelay = frameCount+1; // wait 1 Frames
|
2009-05-23 00:36:55 +02:00
|
|
|
}
|
|
|
|
else if(frameCount >= scrollDelay)
|
|
|
|
{
|
|
|
|
scrollPos2 = -scrollPos2;
|
2009-05-26 23:54:30 +02:00
|
|
|
|
|
|
|
scrollOffset++;
|
|
|
|
wchar_t tmp[] = { text[scrollPos1-1], text[scrollPos1], 0 };
|
|
|
|
int tmpOffset = font->getWidth(tmp) - font->getWidth(tmp+1);
|
|
|
|
if(scrollOffset >= tmpOffset)
|
|
|
|
{
|
|
|
|
scrollOffset -= tmpOffset;
|
|
|
|
scrollPos1--;
|
|
|
|
}
|
2009-05-23 00:36:55 +02:00
|
|
|
|
|
|
|
for(; scrollPos2 > scrollPos1; scrollPos2--)
|
|
|
|
{
|
|
|
|
save = text[scrollPos2]; // save Pos2
|
|
|
|
text[scrollPos2] = 0;
|
2009-05-26 23:54:30 +02:00
|
|
|
int textWidth = font->getWidth(&text[scrollPos1]);
|
2009-05-23 00:36:55 +02:00
|
|
|
text[scrollPos2] = save; // restore Pos2
|
2009-05-26 23:54:30 +02:00
|
|
|
if(textWidth+scrollOffset <= maxWidth)
|
2009-05-23 00:36:55 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(scrollPos1 == 0)
|
|
|
|
{
|
|
|
|
scrollPos2 = -scrollPos2;
|
|
|
|
scrollDelay = frameCount+25; // when dir-change wait 25 Frames
|
|
|
|
}
|
|
|
|
else
|
2009-05-26 23:54:30 +02:00
|
|
|
scrollDelay = frameCount+1; // wait 10 Frames
|
2009-05-23 00:36:55 +02:00
|
|
|
|
|
|
|
scrollPos2 = -scrollPos2;
|
|
|
|
}
|
2009-05-26 23:54:30 +02:00
|
|
|
|
|
|
|
uint16_t drawStyle = style;
|
|
|
|
uint16_t drawX = this->GetLeft() + scrollOffset;
|
|
|
|
|
|
|
|
if((drawStyle & FTGX_JUSTIFY_MASK) == FTGX_JUSTIFY_CENTER)
|
|
|
|
{
|
|
|
|
drawStyle = (drawStyle & ~FTGX_JUSTIFY_MASK) | FTGX_JUSTIFY_LEFT;
|
|
|
|
drawX -= maxWidth >> 1;
|
|
|
|
}
|
|
|
|
else if((drawStyle & FTGX_JUSTIFY_MASK) == FTGX_JUSTIFY_RIGHT)
|
|
|
|
{
|
|
|
|
drawStyle = (drawStyle & ~FTGX_JUSTIFY_MASK) | FTGX_JUSTIFY_LEFT;
|
|
|
|
drawX -= maxWidth;
|
|
|
|
}
|
|
|
|
|
2009-05-23 00:36:55 +02:00
|
|
|
save = text[abs(scrollPos2)]; // save Pos2
|
|
|
|
text[abs(scrollPos2)] = 0;
|
2009-05-26 23:54:30 +02:00
|
|
|
font->drawText(drawX, this->GetTop()+voffset, &text[scrollPos1], c, drawStyle);
|
2009-05-23 00:36:55 +02:00
|
|
|
text[abs(scrollPos2)] = save; // restore Pos2
|
|
|
|
}
|
2009-05-03 20:53:31 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-26 23:54:30 +02:00
|
|
|
font->drawText(this->GetLeft(), this->GetTop()+voffset, text, c, style);
|
2009-05-03 20:53:31 +02:00
|
|
|
}
|
|
|
|
this->UpdateEffects();
|
|
|
|
}
|