13static union TranslatorStorage
15 GuiTextTranslator translator;
16 constexpr TranslatorStorage() : translator() {}
17 ~TranslatorStorage() {}
21static int currentSize = 0;
22static int presetSize = 0;
23static int presetMaxWidth = 0;
24static ALIGN_H presetAlignmentHor = ALIGN_H::LEFT;
25static ALIGN_V presetAlignmentVert = ALIGN_V::TOP;
26static uint16_t presetStyle = 0;
28#define TEXT_SCROLL_DELAY 8
29#define TEXT_SCROLL_INITIAL_DELAY 6
41 style = GUI_TEXT_JUSTIFY_CENTER | GUI_TEXT_ALIGN_MIDDLE;
45 textScroll = SCROLL::NONE;
47 textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
48 textScrollDelay = TEXT_SCROLL_DELAY;
49 measuredW = measuredH = 0;
50 measuredSize = fullWidthSize = -1;
51 measuredGen = fullWidthGen = 0;
63 for(
int i=0; i < 20; i++)
76 alpha = presetColor.a;
78 maxWidth = presetMaxWidth;
81 textScroll = SCROLL::NONE;
83 textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
84 textScrollDelay = TEXT_SCROLL_DELAY;
85 measuredW = measuredH = 0;
86 measuredSize = fullWidthSize = -1;
87 measuredGen = fullWidthGen = 0;
99 for(
int i=0; i < 20; i++)
100 textDyn[i] =
nullptr;
115 for(
int i=0; i < textDynNum; i++)
121wchar_t* GuiText::getText(
const char *t)
const {
134 for(
int i=0; i < textDynNum; i++)
143 textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
144 this->forgetMeasurements();
148 origText = strdup(t);
162 for(
int i=0; i < textDynNum; i++)
171 textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
172 this->forgetMeasurements();
192 presetAlignmentHor = h;
193 presetAlignmentVert = v;
202void GuiText::measure()
207 fontSystem->setPixelSize(size);
208 measuredW = fontSystem->getWidth(text);
209 measuredH = fontSystem->getLineHeight();
214void GuiText::forgetMeasurements()
221int GuiText::getFullWidth(
int atSize)
225 fullWidth = fontSystem->
getWidth(text);
226 fullWidthSize = atSize;
236 for(
int i=0; i < textDynNum; i++)
241 textDyn[i] =
nullptr;
262 for(
int i=0; i < textDynNum; i++)
267 textDyn[i] =
nullptr;
279 for(
int i=0; i < textDynNum; i++)
284 textDyn[i] =
nullptr;
292 textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
293 textScrollDelay = TEXT_SCROLL_DELAY;
314 style |= GUI_TEXT_JUSTIFY_LEFT;
317 style |= GUI_TEXT_JUSTIFY_RIGHT;
320 style |= GUI_TEXT_JUSTIFY_CENTER;
326 style |= GUI_TEXT_ALIGN_TOP;
328 case ALIGN_V::BOTTOM:
329 style |= GUI_TEXT_ALIGN_BOTTOM;
332 style |= GUI_TEXT_ALIGN_MIDDLE;
347 text = getText(origText);
348 this->forgetMeasurements();
350 for(
int i=0; i < textDynNum; i++)
355 textDyn[i] =
nullptr;
366bool GuiText::scrollStepDue()
368 const uint32_t now = platform->getVideo()->getFrameTimer();
371 if(now < textScrollLastTick)
372 textScrollLastTick = now;
374 if(now - textScrollLastTick < (uint32_t)textScrollDelay)
377 textScrollLastTick = now;
395 int newSize = size*this->
getScale();
397 fontSystem->setPixelSize(newSize);
401 fontSystem->drawText(this->
getLeft(), this->
getTop() + nudgeY, text, c, style);
406 uint32_t textlen = wcslen(text);
416 int lastSpaceIndex = -1;
418 while(ch < textlen && linenum < 20)
421 textDyn[linenum] =
new wchar_t[textlen + 1];
423 textDyn[linenum][n] = text[ch];
424 textDyn[linenum][n+1] = 0;
426 if(text[ch] ==
' ' || ch == textlen-1)
428 if(fontSystem->getWidth(textDyn[linenum]) > maxWidth)
432 textDyn[linenum][lastSpaceIndex] = 0;
440 else if(ch == textlen-1)
445 if(text[ch] ==
' ' && n >= 0)
453 textDynNum = linenum;
456 int lineheight = newSize + lineSpacing;
460 voffset = (lineheight >> 1) * (1-textDynNum);
463 int top = this->
getTop() + voffset + nudgeY;
465 for(
int i=0; i < textDynNum; ++i)
466 fontSystem->drawText(left, top+i*lineheight, textDyn[i], c, style);
473 textDyn[0] = wcsdup(text);
476 textDyn[0][fontSystem->fitCount(textDyn[0], maxWidth)] = 0;
479 if(textScroll == SCROLL::HORIZONTAL)
481 if(getFullWidth(newSize) > maxWidth && scrollStepDue())
483 if(textScrollInitialDelay)
485 --textScrollInitialDelay;
490 if((uint32_t)textScrollPos > textlen-1)
493 textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
496 fontSystem->scrollWindow(text, textlen, textScrollPos, maxWidth, textDyn[0]);
500 fontSystem->drawText(this->
getLeft(), this->
getTop() + nudgeY, textDyn[0], c, style);
517 if(maxWidth > 0 && w > maxWidth)
Umbrella header for the platform-agnostic libgui core.
bool isVisible()
Checks whether or not the element is visible.
int getTop()
Gets the current topmost coordinate of the element Considers vertical alignment, y offset,...
int alpha
Element alpha value (0-255)
ALIGN_H alignmentHor
Horizontal element alignment, respective to parent element (LEFT, RIGHT, CENTRE)
void updateEffects()
Updates the element's effects (dynamic values) Called by Draw(), used for animation purposes.
int getLeft()
Gets the current leftmost coordinate of the element Considers horizontal alignment,...
ALIGN_V alignmentVert
Horizontal element alignment, respective to parent element (TOP, BOTTOM, MIDDLE)
float getScale()
Gets the element's current scale Considers scale, scaleDyn, and the parent element's GetScale() value...
int getAlpha()
Gets the element's alpha value Considers alpha, alphaDyn, and the parent element's GetAlpha() value.
static wchar_t * charToWideChar(const char *p)
static uint32_t getMetricsGeneration()
Changes whenever a measurement made earlier could now come out differently (a renderer was made or it...
uint16_t getWidth(const wchar_t *text)
Hashed msgid -> UTF-8 string table loaded from a single binary .lang blob (see loadLanguage()).
void setWrap(bool w, int width=0)
Enables/disables text wrapping.
void setColor(PixelColor c)
Sets the font color.
void resetText()
Updates the text to the selected language.
GuiText(const char *t, int s, PixelColor c)
Constructor.
void setScroll(SCROLL s)
Enables/disables text scrolling.
static void setPresets(int sz, PixelColor c, int w, uint16_t s, ALIGN_H h, ALIGN_V v)
Sets up preset values to be used by GuiText(t) Useful when printing multiple text elements,...
void getNaturalSize(int &w, int &h) override
Gets the text's size for layout: its rendered width and height on one line (at most the maximum width...
void setFontSize(int s)
Sets the font size.
void setText(const char *t)
Sets the text of the GuiText element.
int getTextWidth()
Gets the width of the text when rendered.
void draw() override
Constantly called to draw the text.
void setWText(wchar_t *t)
Sets the text of the GuiText element.
void setMaxWidth(int w)
Sets the maximum width of the drawn texture image.
int getLength()
Gets the translated text length of the GuiText element.
void setStyle(uint16_t s)
Sets the GuiTextRenderer style attributes.
void setAlignment(ALIGN_H hor, ALIGN_V vert)
Sets the text alignment.
SCROLL
Scrolling mode of a GuiText (none or horizontal).
ALIGN_V
Vertical alignment of a GuiElement relative to its parent.
ALIGN_H
Horizontal alignment of a GuiElement relative to its parent.
8-bit-per-channel RGBA color used by the video interfaces.
uint8_t a
Alpha component.