00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "gui.h"
00012
00013 static int currentSize = 0;
00014 static int presetSize = 0;
00015 static int presetMaxWidth = 0;
00016 static int presetAlignmentHor = 0;
00017 static int presetAlignmentVert = 0;
00018 static u16 presetStyle = 0;
00019 static GXColor presetColor = (GXColor){255, 255, 255, 255};
00020
00021 #define TEXT_SCROLL_DELAY 8
00022 #define TEXT_SCROLL_INITIAL_DELAY 6
00023
00024 GuiText::GuiText()
00025 {
00026 origText = NULL;
00027 text = NULL;
00028 style = FTGX_JUSTIFY_CENTER | FTGX_ALIGN_MIDDLE;
00029 maxWidth = 0;
00030 wrap = false;
00031 textDyn = NULL;
00032 textScroll = SCROLL_NONE;
00033 textScrollPos = 0;
00034 textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
00035 textScrollDelay = TEXT_SCROLL_DELAY;
00036
00037 alignmentHor = ALIGN_CENTRE;
00038 alignmentVert = ALIGN_MIDDLE;
00039 }
00040
00041
00042
00043
00044 GuiText::GuiText(const char * t, int s, GXColor c)
00045 {
00046 origText = NULL;
00047 text = NULL;
00048 size = s;
00049 color = c;
00050 alpha = c.a;
00051 style = FTGX_JUSTIFY_CENTER | FTGX_ALIGN_MIDDLE;
00052 maxWidth = 0;
00053 wrap = false;
00054 textDyn = NULL;
00055 textScroll = SCROLL_NONE;
00056 textScrollPos = 0;
00057 textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
00058 textScrollDelay = TEXT_SCROLL_DELAY;
00059
00060 alignmentHor = ALIGN_CENTRE;
00061 alignmentVert = ALIGN_MIDDLE;
00062
00063 if(t)
00064 {
00065 origText = strdup(t);
00066 text = charToWideChar(t);
00067 }
00068 }
00069
00070
00071
00072
00073 GuiText::GuiText(const char * t)
00074 {
00075 origText = NULL;
00076 text = NULL;
00077 size = presetSize;
00078 color = presetColor;
00079 alpha = presetColor.a;
00080 style = presetStyle;
00081 maxWidth = presetMaxWidth;
00082 wrap = false;
00083 textDyn = NULL;
00084 textScroll = SCROLL_NONE;
00085 textScrollPos = 0;
00086 textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
00087 textScrollDelay = TEXT_SCROLL_DELAY;
00088
00089 alignmentHor = presetAlignmentHor;
00090 alignmentVert = presetAlignmentVert;
00091
00092 if(t)
00093 {
00094 origText = strdup(t);
00095 text = charToWideChar(t);
00096 }
00097 }
00098
00099
00100
00101
00102 GuiText::~GuiText()
00103 {
00104 if(origText)
00105 free(origText);
00106 if(text)
00107 delete[] text;
00108 if(textDyn)
00109 delete[] textDyn;
00110 }
00111
00112 void GuiText::SetText(const char * t)
00113 {
00114 if(origText)
00115 free(origText);
00116 if(text)
00117 delete[] text;
00118 if(textDyn)
00119 delete[] textDyn;
00120
00121 origText = NULL;
00122 text = NULL;
00123 textDyn = NULL;
00124 textScrollPos = 0;
00125 textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
00126
00127 if(t)
00128 {
00129 origText = strdup(t);
00130 text = charToWideChar(t);
00131 }
00132 }
00133
00134 void GuiText::SetPresets(int sz, GXColor c, int w, u16 s, int h, int v)
00135 {
00136 presetSize = sz;
00137 presetColor = c;
00138 presetStyle = s;
00139 presetMaxWidth = w;
00140 presetAlignmentHor = h;
00141 presetAlignmentVert = v;
00142 }
00143
00144 void GuiText::SetFontSize(int s)
00145 {
00146 size = s;
00147 }
00148
00149 void GuiText::SetMaxWidth(int width)
00150 {
00151 maxWidth = width;
00152 }
00153
00154 void GuiText::SetWrap(bool w, int width)
00155 {
00156 wrap = w;
00157 maxWidth = width;
00158 }
00159
00160 void GuiText::SetScroll(int s)
00161 {
00162 if(textScroll == s)
00163 return;
00164
00165 if(textDyn)
00166 {
00167 delete[] textDyn;
00168 textDyn = NULL;
00169 }
00170 textScroll = s;
00171 textScrollPos = 0;
00172 textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
00173 textScrollDelay = TEXT_SCROLL_DELAY;
00174 }
00175
00176 void GuiText::SetColor(GXColor c)
00177 {
00178 color = c;
00179 alpha = c.a;
00180 }
00181
00182 void GuiText::SetStyle(u16 s)
00183 {
00184 style = s;
00185 }
00186
00187 void GuiText::SetAlignment(int hor, int vert)
00188 {
00189 style = 0;
00190
00191 switch(hor)
00192 {
00193 case ALIGN_LEFT:
00194 style |= FTGX_JUSTIFY_LEFT;
00195 break;
00196 case ALIGN_RIGHT:
00197 style |= FTGX_JUSTIFY_RIGHT;
00198 break;
00199 default:
00200 style |= FTGX_JUSTIFY_CENTER;
00201 break;
00202 }
00203 switch(vert)
00204 {
00205 case ALIGN_TOP:
00206 style |= FTGX_ALIGN_TOP;
00207 break;
00208 case ALIGN_BOTTOM:
00209 style |= FTGX_ALIGN_BOTTOM;
00210 break;
00211 default:
00212 style |= FTGX_ALIGN_MIDDLE;
00213 break;
00214 }
00215
00216 alignmentHor = hor;
00217 alignmentVert = vert;
00218 }
00219
00220
00221
00222
00223 void GuiText::Draw()
00224 {
00225 if(!text)
00226 return;
00227
00228 if(!this->IsVisible())
00229 return;
00230
00231 GXColor c = color;
00232 c.a = this->GetAlpha();
00233
00234 int newSize = size*this->GetScale();
00235
00236 if(newSize > MAX_FONT_SIZE)
00237 newSize = MAX_FONT_SIZE;
00238
00239 if(newSize != currentSize)
00240 {
00241 ChangeFontSize(newSize);
00242 if(!fontSystem[newSize])
00243 fontSystem[newSize] = new FreeTypeGX(newSize);
00244 currentSize = newSize;
00245 }
00246
00247 if(maxWidth > 0)
00248 {
00249 char * tmpText = strdup(origText);
00250 u8 maxChar = (maxWidth*2.0) / newSize;
00251
00252 if(!textDyn)
00253 {
00254 if(strlen(tmpText) > maxChar)
00255 tmpText[maxChar] = 0;
00256 textDyn = charToWideChar(tmpText);
00257 }
00258
00259 if(textScroll == SCROLL_HORIZONTAL)
00260 {
00261 int textlen = strlen(origText);
00262
00263 if(textlen > maxChar && (FrameTimer % textScrollDelay == 0))
00264 {
00265 if(textScrollInitialDelay)
00266 {
00267 textScrollInitialDelay--;
00268 }
00269 else
00270 {
00271 textScrollPos++;
00272 if(textScrollPos > textlen-1)
00273 {
00274 textScrollPos = 0;
00275 textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
00276 }
00277
00278 strncpy(tmpText, &origText[textScrollPos], maxChar-1);
00279 tmpText[maxChar-1] = 0;
00280
00281 int dynlen = strlen(tmpText);
00282
00283 if(dynlen+2 < maxChar)
00284 {
00285 tmpText[dynlen] = ' ';
00286 tmpText[dynlen+1] = ' ';
00287 strncat(&tmpText[dynlen+2], origText, maxChar - dynlen - 2);
00288 }
00289 if(textDyn) delete[] textDyn;
00290 textDyn = charToWideChar(tmpText);
00291 }
00292 }
00293 if(textDyn)
00294 fontSystem[currentSize]->drawText(this->GetLeft(), this->GetTop(), textDyn, c, style);
00295 }
00296 else if(wrap)
00297 {
00298 int lineheight = newSize + 6;
00299 int txtlen = wcslen(text);
00300 int i = 0;
00301 int ch = 0;
00302 int linenum = 0;
00303 int lastSpace = -1;
00304 int lastSpaceIndex = -1;
00305 wchar_t * textrow[20];
00306
00307 while(ch < txtlen)
00308 {
00309 if(i == 0)
00310 textrow[linenum] = new wchar_t[txtlen + 1];
00311
00312 textrow[linenum][i] = text[ch];
00313 textrow[linenum][i+1] = 0;
00314
00315 if(text[ch] == ' ' || ch == txtlen-1)
00316 {
00317 if(wcslen(textrow[linenum]) >= maxChar)
00318 {
00319 if(lastSpace >= 0)
00320 {
00321 textrow[linenum][lastSpaceIndex] = 0;
00322 ch = lastSpace;
00323 lastSpace = -1;
00324 lastSpaceIndex = -1;
00325 }
00326 linenum++;
00327 i = -1;
00328 }
00329 else if(ch == txtlen-1)
00330 {
00331 linenum++;
00332 }
00333 }
00334 if(text[ch] == ' ' && i >= 0)
00335 {
00336 lastSpace = ch;
00337 lastSpaceIndex = i;
00338 }
00339 ch++;
00340 i++;
00341 }
00342
00343 int voffset = 0;
00344
00345 if(alignmentVert == ALIGN_MIDDLE)
00346 voffset = -(lineheight*linenum)/2 + lineheight/2;
00347
00348 for(i=0; i < linenum; i++)
00349 {
00350 fontSystem[currentSize]->drawText(this->GetLeft(), this->GetTop()+voffset+i*lineheight, textrow[i], c, style);
00351 delete[] textrow[i];
00352 }
00353 }
00354 else
00355 {
00356 fontSystem[currentSize]->drawText(this->GetLeft(), this->GetTop(), textDyn, c, style);
00357 }
00358 free(tmpText);
00359 }
00360 else
00361 {
00362 fontSystem[currentSize]->drawText(this->GetLeft(), this->GetTop(), text, c, style);
00363 }
00364 this->UpdateEffects();
00365 }