libgui/include/gui/GuiText.h

190 lines
5.1 KiB
C
Raw Normal View History

2017-10-29 10:28:14 +01:00
/****************************************************************************
* Copyright (C) 2015 Dimok
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#ifndef GUI_TEXT_H_
#define GUI_TEXT_H_
2018-06-21 20:44:58 +02:00
#include <gui/GuiElement.h>
2020-08-13 14:05:28 +02:00
#include <mutex>
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
//!Forward declaration
class FreeTypeGX;
//!Display, manage, and manipulate text in the GUI
2018-06-21 20:44:58 +02:00
class GuiText : public GuiElement {
2017-10-29 10:28:14 +01:00
public:
//!Constructor
GuiText();
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
//!\param t Text
//!\param s Font size
//!\param c Font color
2020-08-13 12:38:07 +02:00
GuiText(const char *t, int32_t s, const glm::vec4 &c);
2017-10-29 10:28:14 +01:00
//!\overload
//!\param t Text
//!\param s Font size
//!\param c Font color
2020-08-13 12:38:07 +02:00
GuiText(const wchar_t *t, int32_t s, const glm::vec4 &c);
2017-10-29 10:28:14 +01:00
//!\overload
//!\Assumes SetPresets() has been called to setup preferred text attributes
//!\param t Text
2020-08-13 12:38:07 +02:00
GuiText(const char *t);
2017-10-29 10:28:14 +01:00
//!Destructor
virtual ~GuiText();
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
//!Sets the text of the GuiText element
//!\param t Text
2020-08-13 12:38:07 +02:00
virtual void setText(const char *t);
virtual void setText(const wchar_t *t);
virtual void setTextf(const char *format, ...) __attribute__((format(printf, 2, 3)));
2017-10-29 10:28:14 +01:00
//!Sets up preset values to be used by GuiText(t)
//!Useful when printing multiple text elements, all with the same attributes set
//!\param sz Font size
//!\param c Font color
//!\param w Maximum width of texture image (for text wrapping)
//!\param wrap Wrapmode when w>0
//!\param a Text alignment
2020-08-13 12:38:07 +02:00
static void setPresets(int32_t sz, const glm::vec4 &c, int32_t w, int32_t a);
2017-10-29 10:28:14 +01:00
static void setPresetFont(FreeTypeGX *font);
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
//!Sets the font size
//!\param s Font size
2018-06-21 20:44:58 +02:00
void setFontSize(int32_t s);
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
//!Sets the maximum width of the drawn texture image
//!If the text exceeds this, it is wrapped to the next line
//!\param w Maximum width
//!\param m WrapMode
2018-06-21 20:44:58 +02:00
void setMaxWidth(int32_t w = 0, int32_t m = WRAP);
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
//!Sets the font color
//!\param c Font color
2020-08-13 12:38:07 +02:00
void setColor(const glm::vec4 &c);
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
void setBlurGlowColor(float blurIntensity, const glm::vec4 &c);
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
void setTextBlur(float blur) {
defaultBlur = blur;
}
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
//!Get the original text as char
2020-08-13 12:38:07 +02:00
virtual const wchar_t *getText() const {
2018-06-21 20:44:58 +02:00
return text;
}
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
virtual std::string toUTF8(void) const;
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
//!Get the Horizontal Size of Text
2020-08-13 13:39:17 +02:00
int32_t getTextWidth();
2020-08-13 12:38:07 +02:00
2018-06-21 20:44:58 +02:00
int32_t getTextWidth(int32_t ind);
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
//!Get the max textwidth
2018-06-21 20:44:58 +02:00
int32_t getTextMaxWidth() {
return maxWidth;
}
2020-08-13 12:38:07 +02:00
2020-08-13 13:26:45 +02:00
void setSSAA(int32_t ssaa) {
this->internalSSAA = ssaa;
};
//!Get fontsize
2018-06-21 20:44:58 +02:00
int32_t getFontSize() {
return currentSize;
2018-06-21 20:44:58 +02:00
};
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
//!Set max lines to draw
2018-06-21 20:44:58 +02:00
void setLinesToDraw(int32_t l) {
linestodraw = l;
}
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
//!Get current Textline (for position calculation)
2020-08-13 12:38:07 +02:00
const wchar_t *getDynText(int32_t ind = 0);
virtual const wchar_t *getTextLine(int32_t ind) {
2018-06-21 20:44:58 +02:00
return getDynText(ind);
};
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
//!Change the font
bool setFont(FreeTypeGX *font);
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
//! virtual function used in child classes
2018-06-21 20:44:58 +02:00
virtual int32_t getStartWidth() {
return 0;
};
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
//!Constantly called to draw the text
void draw(CVideo *pVideo);
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
//! text enums
2018-06-21 20:44:58 +02:00
enum {
2017-10-29 10:28:14 +01:00
WRAP,
DOTTED,
SCROLL_HORIZONTAL,
SCROLL_NONE
};
protected:
2020-08-13 12:38:07 +02:00
static FreeTypeGX *presentFont;
2020-08-13 13:26:45 +02:00
static int32_t presetSSAA;
2018-06-21 20:44:58 +02:00
static int32_t presetSize;
static int32_t presetMaxWidth;
2017-10-29 10:28:14 +01:00
static float presetInternalRenderingScale;
2018-06-21 20:44:58 +02:00
static int32_t presetAlignment;
2017-10-29 10:28:14 +01:00
static GX2ColorF32 presetColor;
//!Clear the dynamic text
void clearDynamicText();
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
//!Create a dynamic dotted text if the text is too long
void makeDottedText();
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
//!Scroll the text once
2018-06-21 20:44:58 +02:00
void scrollText(uint32_t frameCount);
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
//!Wrap the text to several lines
void wrapText();
2020-08-13 14:05:28 +02:00
mutable std::recursive_mutex textMutex;
2020-08-13 12:38:07 +02:00
wchar_t *text;
2017-10-29 10:28:14 +01:00
std::vector<wchar_t *> textDyn;
std::vector<uint16_t> textDynWidth;
2018-06-21 20:44:58 +02:00
int32_t wrapMode; //!< Wrapping toggle
int32_t textScrollPos; //!< Current starting index of text string for scrolling
int32_t textScrollInitialDelay; //!< Delay to wait before starting to scroll
int32_t textScrollDelay; //!< Scrolling speed
int32_t maxWidth; //!< Maximum width of the generated text object (for text wrapping)
2017-10-29 10:28:14 +01:00
FreeTypeGX *font;
2018-06-21 20:44:58 +02:00
int32_t currentSize;
int32_t linestodraw;
2017-10-29 10:28:14 +01:00
glm::vec4 color;
float defaultBlur;
float blurGlowIntensity;
float blurAlpha;
glm::vec4 blurGlowColor;
2020-08-13 13:26:45 +02:00
int32_t internalSSAA;
2017-10-29 10:28:14 +01:00
};
#endif