Chesto 0.9
A declarative and element-based library for creating GUIs on homebrew'd consoles
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
TextElement Class Reference
Inheritance diagram for TextElement:
Inheritance graph
[legend]
Collaboration diagram for TextElement:
Collaboration graph
[legend]

Public Member Functions

 TextElement (std::string text, int size, CST_Color *color=0, int font_type=NORMAL, int wrapped_width=0)
 
void setText (const std::string &text)
 
void setSize (int size)
 
void setColor (const CST_Color &color)
 
void setFont (int font_type)
 
void setWrappedWidth (int wrapped_width)
 
void update (bool forceUpdate=false)
 update TextElement with changes More...
 
- Public Member Functions inherited from Texture
void clear (void)
 Reinitialize Texture Resets texture content, size and color. More...
 
bool loadFromSurface (CST_Surface *surface)
 Loads the texture from a surface Returns true if successful. More...
 
bool loadFromCache (std::string &key)
 Loads the texture from caches Returns true if successful. More...
 
bool loadFromSurfaceSaveToCache (std::string &key, CST_Surface *surface)
 Loads the texture from a surface and saves the results in caches Returns true if successful. More...
 
void render (Element *parent)
 Renders the texture. More...
 
void resize (int w, int h)
 Resizes the texture. More...
 
void setScaleMode (TextureScaleMode mode)
 Sets texture scaling mode. More...
 
void getTextureSize (int *w, int *h)
 Return texture's original size. More...
 
TexturesetSize (int w, int h)
 
bool saveTo (std::string &path)
 save this texture to the given file path as a PNG More...
 
void loadPath (std::string &path, bool forceReload=false)
 update and load or reload the texture More...
 
- Public Member Functions inherited from Element
virtual bool process (InputEvents *event)
 process any input that is received for this element More...
 
virtual void render (Element *parent)
 display the current state of the display More...
 
bool onTouchDown (InputEvents *event)
 
bool onTouchDrag (InputEvents *event)
 
bool onTouchUp (InputEvents *event)
 
void hide ()
 
void unhide ()
 
void renderBackground (bool fill=true)
 
void append (Element *element)
 
void remove (Element *element)
 
void removeAll (bool moveToTrash=false)
 
void position (int x, int y)
 position the element More...
 
void recalcPosition (Element *parent)
 
CST_Rect getBounds ()
 
CST_Renderer * getRenderer ()
 
void wipeAll (bool delSelf=false)
 
Elementchild (Element *child)
 
ElementsetPosition (int x, int y)
 
ElementsetAction (std::function< void()> func)
 
ElementcenterHorizontallyIn (Element *parent)
 
ElementcenterVerticallyIn (Element *parent)
 
ElementcenterIn (Element *parent)
 
ElementsetAbsolute (bool isAbs)
 
Elementconstrain (int flags, int padding=0)
 
Elementanimate (int durationIn, std::function< void(float)> onStep, std::function< void()> onFinish)
 
ElementmoveToFront ()
 
ElementsetTouchable (bool touchable)
 
void screenshot (std::string path)
 Take a screenshot of this element and its children, and save it to the given path. More...
 

Static Public Member Functions

static void loadI18nCache (std::string locale)
 

Public Attributes

std::string text = ""
 
std::string customFontPath = ""
 
- Public Attributes inherited from Texture
int cornerRadius = 0
 Rounded corner radius (if >0, will round) More...
 
- Public Attributes inherited from Element
std::function< void()> action = NULL
 the action to call (from binded callback) on touch or button selection https://stackoverflow.com/questions/14189440/c-class-member-callback-simple-examples More...
 
std::function< void(InputEvents *event)> actionWithEvents = NULL
 
std::vector< Element * > elements
 visible GUI child elements of this element More...
 
float scale = 1.0f
 
bool touchable = false
 whether or not this element can be touched (highlights bounds) More...
 
bool dragging = false
 whether or not this element is currently being dragged More...
 
bool needsRedraw = false
 whether or not this element needs the screen redrawn next time it's processed More...
 
int futureRedrawCounter = 0
 whether this element needs a redraw for the next X redraws (decreases each time) (0 is no redraws) More...
 
int lastMouseY = 0
 the last Y, X coordinate of the mouse (from a drag probably) More...
 
int lastMouseX = 0
 
bool hasBackground = false
 
rgb backgroundColor = {0, 0, 0}
 
bool isAbsolute = false
 
Elementparent = NULL
 the parent element (can sometimes be null if it isn't set) More...
 
bool hidden = false
 whether this element should skip rendering or not More...
 
bool isProtected = false
 
int elasticCounter = 0
 how much time is left in an elastic-type flick/scroll set by the last distance traveled in a scroll, and counts down every frame More...
 
int width = 0
 width and height of this element (must be manually set, isn't usually calculated (but is in some cases, like text or images)) More...
 
int height = 0
 
int x = 0
 
int y = 0
 
int xAbs = 0
 
int yAbs = 0
 
double angle = 0
 rotation angle in degrees More...
 
int xOff = 0
 
int yOff = 0
 
std::vector< Constraint * > constraints
 
std::vector< Animation * > animations
 
bool useColorMask = false
 whether or not to overlay a color mask on top of this element More...
 
CST_Color maskColor = {0,0,0,0}
 The color to overlay on top. More...
 

Static Public Attributes

static std::unordered_map< std::string, std::string > i18nCache = {}
 
static bool useSimplifiedChineseFont = false
 

Additional Inherited Members

- Public Types inherited from Element
typedef Element super
 
- Protected Attributes inherited from Texture
CST_Texture * mTexture = nullptr
 The actual texture. More...
 
int texW = 0
 The size of the texture. More...
 
int texH = 0
 
CST_Color texFirstPixel = {0,0,0,0}
 The color of the first pixel. More...
 
TextureScaleMode texScaleMode = SCALE_STRETCH
 Texture's scaling mode. More...
 
- Static Protected Attributes inherited from Texture
static std::unordered_map< std::string, TextureDatatexCache
 Cache previously displayed textures. More...
 

Detailed Description

Definition at line 15 of file TextElement.hpp.

Constructor & Destructor Documentation

◆ TextElement() [1/2]

TextElement::TextElement ( )

Definition at line 17 of file TextElement.cpp.

18{
19}

◆ TextElement() [2/2]

TextElement::TextElement ( std::string  text,
int  size,
CST_Color *  color = 0,
int  font_type = NORMAL,
int  wrapped_width = 0 
)

Definition at line 53 of file TextElement.cpp.

54{
55 std::string sText = text;
56 setText(sText);
57 setSize(size);
58 if (color) setColor(*color);
59 setFont(font_type);
60 setWrappedWidth(wrapped_width);
61 update();
62}
void update(bool forceUpdate=false)
update TextElement with changes
Definition: TextElement.cpp:89

Member Function Documentation

◆ loadI18nCache()

void TextElement::loadI18nCache ( std::string  locale)
static

Definition at line 22 of file TextElement.cpp.

22 {
23 // en-us, zh-cn
24 std::string localePath = RAMFS "res/i18n/" + locale + ".ini";
25 std::ifstream file(localePath);
26 // printf("Loading i18n cache from %s\n", localePath.c_str());
27 if (file.is_open()) {
28 std::string line;
29 while (std::getline(file, line)) {
30 size_t pos = line.find(" =");
31 if (pos == std::string::npos) {
32 continue; // bad format
33 }
34 std::string key = line.substr(0, pos);
35 pos = line.find("= ");
36 if (pos == std::string::npos) {
37 continue;
38 }
39 std::string value = line.substr(pos + 2);
40 TextElement::i18nCache[key] = value;
41 // printf("Loaded i18n key %s with value %s\n", key.c_str(), value.c_str());
42 }
43 file.close();
44
45 // if locale is zh-cn, we need to force the simple chinese font
46 if (locale == "zh-cn") {
47 printf("Overriding font choice\n");
48 TextElement::useSimplifiedChineseFont = true;
49 }
50 }
51}

◆ setColor()

void TextElement::setColor ( const CST_Color &  color)

Definition at line 74 of file TextElement.cpp.

75{
76 this->textColor = color;
77}

◆ setFont()

void TextElement::setFont ( int  font_type)

Definition at line 79 of file TextElement.cpp.

80{
81 this->textFont = font_type;
82}

◆ setSize()

void TextElement::setSize ( int  size)

Definition at line 69 of file TextElement.cpp.

70{
71 this->textSize = size;
72}

◆ setText()

void TextElement::setText ( const std::string &  text)

Definition at line 64 of file TextElement.cpp.

65{
66 this->text = text;
67}

◆ setWrappedWidth()

void TextElement::setWrappedWidth ( int  wrapped_width)

Definition at line 84 of file TextElement.cpp.

85{
86 this->textWrappedWidth = wrapped_width;
87}

◆ update()

void TextElement::update ( bool  forceUpdate = false)

update TextElement with changes

Definition at line 89 of file TextElement.cpp.

90{
91 std::string key = text + std::to_string(textSize);
92
93 clear();
94
95 if (!loadFromCache(key) || forceUpdate)
96 {
97 if (TextElement::useSimplifiedChineseFont && textFont == NORMAL) {
98 textFont = SIMPLIFIED_CHINESE;
99 }
100 auto fontPath = fontPaths[textFont % 5];
101 if (customFontPath != "") {
102 fontPath = customFontPath.c_str();
103 }
104 TTF_Font* font = TTF_OpenFont(fontPath, textSize);
105
106 CST_Surface *textSurface = ((textFont == ICON) || (textWrappedWidth == 0)) ?
107 TTF_RenderUTF8_Blended(font, text.c_str(), textColor) :
108 TTF_RenderUTF8_Blended_Wrapped(font, text.c_str(), textColor, textWrappedWidth);
109 if(textSurface==NULL) printf("TTF_GetError: %s\n", TTF_GetError());
110
111 loadFromSurfaceSaveToCache(key, textSurface);
112
113 CST_FreeSurface(textSurface);
114 TTF_CloseFont(font);
115 }
116
117 getTextureSize(&width, &height);
118}
int width
width and height of this element (must be manually set, isn't usually calculated (but is in some case...
Definition: Element.hpp:118
void getTextureSize(int *w, int *h)
Return texture's original size.
Definition: Texture.cpp:198
bool loadFromSurfaceSaveToCache(std::string &key, CST_Surface *surface)
Loads the texture from a surface and saves the results in caches Returns true if successful.
Definition: Texture.cpp:91
bool loadFromCache(std::string &key)
Loads the texture from caches Returns true if successful.
Definition: Texture.cpp:76
void clear(void)
Reinitialize Texture Resets texture content, size and color.
Definition: Texture.cpp:9

References Texture::clear(), Texture::getTextureSize(), Texture::loadFromCache(), Texture::loadFromSurfaceSaveToCache(), and Element::width.

Member Data Documentation

◆ customFontPath

std::string TextElement::customFontPath = ""

Definition at line 34 of file TextElement.hpp.

◆ i18nCache

std::unordered_map< std::string, std::string > TextElement::i18nCache = {}
static

Definition at line 36 of file TextElement.hpp.

◆ text

std::string TextElement::text = ""

Definition at line 31 of file TextElement.hpp.

◆ useSimplifiedChineseFont

bool TextElement::useSimplifiedChineseFont = false
static

Definition at line 40 of file TextElement.hpp.


The documentation for this class was generated from the following files: