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
Chesto::TextElement Class Reference
Inheritance diagram for Chesto::TextElement:
Inheritance graph
[legend]
Collaboration diagram for Chesto::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 Chesto::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 Chesto::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 addNode (std::unique_ptr< Element > node)
 
void remove (Element *element)
 
void removeAll ()
 
void position (int x, int y)
 position the element More...
 
void recalcPosition (Element *parent)
 
float getEffectiveScale () const
 
CST_Rect getBounds ()
 
CST_Renderer * getRenderer ()
 
ElementsetPosition (int x, int y)
 
ElementsetAction (std::function< void()> func)
 
ElementsetAbsolute (bool isAbs)
 
template<typename T , typename... Args>
T * createNode (Args &&... args)
 
Elementconstrain (int flags, int padding=0)
 
ElementconstrainToTarget (Element *target, 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 std::vector< std::pair< std::string, std::string > > getAvailableLanguages ()
 
static void loadI18nCache (std::string locale)
 
- Static Public Member Functions inherited from Chesto::Texture
static void wipeEntireCache ()
 Wipes the entire texture cache (generally, should only be used to reload entire theme / text) More...
 
static void wipeTextCache ()
 Similar to wipeEntireCache, but only wipes cached text textures (made by TextElement) More...
 

Public Attributes

std::string text = ""
 
std::string customFontPath = ""
 
- Public Attributes inherited from Chesto::Texture
int cornerRadius = 0
 Rounded corner radius (if >0, will round) More...
 
SDL_BlendMode blendMode = SDL_BLENDMODE_BLEND
 Blend mode to use for this texture. More...
 
- Public Attributes inherited from Chesto::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< std::unique_ptr< Element, std::function< void(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}
 
int backgroundOpacity = 0xff
 
bool isAbsolute = false
 
Elementparent = nullptr
 the parent element (reference only, not owned) 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 cornerRadius = 0
 
int tag = 0
 
std::vector< std::unique_ptr< Constraint > > constraints
 
std::vector< std::unique_ptr< 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::map< std::string, std::string > i18nCache = {}
 
static std::string curLang = "en-us"
 
static bool useSimplifiedChineseFont = false
 
static bool useKoreanFont = false
 
static bool useJapaneseFont = false
 
static std::map< std::string, int > forcedLangFonts = {}
 
- Static Public Attributes inherited from Chesto::Texture
static const std::string textElemPrefix = "(TextElement):"
 

Additional Inherited Members

- Public Types inherited from Chesto::Element
typedef Element super
 
- Protected Member Functions inherited from Chesto::Element
void addStackMember (Element *element)
 
- Protected Attributes inherited from Chesto::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 Chesto::Texture
static std::unordered_map< std::string, TextureDatatexCache
 Cache previously displayed textures. More...
 

Detailed Description

Definition at line 22 of file TextElement.hpp.

Constructor & Destructor Documentation

◆ TextElement() [1/2]

Chesto::TextElement::TextElement ( )

Definition at line 31 of file TextElement.cpp.

32{
33}

◆ TextElement() [2/2]

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

Definition at line 138 of file TextElement.cpp.

139{
140 std::string sText = text;
141 setText(sText);
142 setSize(size);
143 if (color) setColor(*color);
144 setFont(font_type);
145 setWrappedWidth(wrapped_width);
146 update();
147}
void update(bool forceUpdate=false)
update TextElement with changes

Member Function Documentation

◆ getAvailableLanguages()

std::vector< std::pair< std::string, std::string > > Chesto::TextElement::getAvailableLanguages ( )
static

Definition at line 36 of file TextElement.cpp.

36 {
37 std::vector<std::pair<std::string, std::string>> languages;
38 // read all files in RAMFS res/i18n and their 'meta.lang.name' entry
39 std::string i18nPath = RAMFS "res/i18n/";
40 DIR* dir = opendir(i18nPath.c_str());
41 if (dir) {
42 struct dirent* entry;
43 while ((entry = readdir(dir)) != NULL) {
44 std::string fileName = entry->d_name;;
45 if (fileName.length() > 4 && fileName.substr(fileName.length() - 4) == ".ini") {
46 std::string locale = fileName.substr(0, fileName.length() - 4);
47 // to lower case
48 std::transform(locale.begin(), locale.end(), locale.begin(), ::tolower);
49 // read the file to find meta.lang.name
50 std::ifstream file(i18nPath + fileName);
51 if (file.is_open()) {
52 std::string line;
53 while (std::getline(file, line)) {
54 if (line.find("meta.lang.name = ") == 0) {
55 std::string langName = line.substr(strlen("meta.lang.name = "));
56 languages.push_back({locale, langName});
57
58 // also store the language name with its forced font face
59 if (locale == "zh-cn") {
60 forcedLangFonts[langName] = SIMPLIFIED_CHINESE;
61 } else if (locale == "ko-kr") {
62 forcedLangFonts[langName] = KOREAN;
63 } else if (locale == "ja-jp") {
64 forcedLangFonts[langName] = JAPANESE;
65 } else {
66 forcedLangFonts[langName] = NORMAL;
67 }
68 break;
69 }
70 }
71 file.close();
72 }
73 }
74 }
75 closedir(dir);
76 }
77
78 return languages;
79}

◆ loadI18nCache()

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

Definition at line 103 of file TextElement.cpp.

103 {
104 std::transform(locale.begin(), locale.end(), locale.begin(), ::tolower);
105 TextElement::curLang = locale;
106
107 // clear existing cache
108 TextElement::i18nCache.clear();
109
110 // always use English as the base (fallback for missing translations)
111 std::string englishPath = RAMFS "res/i18n/en-us.ini";
112 loadI18nFile(englishPath, TextElement::i18nCache);
113
114 // overlay the target locale (if not English)
115 if (locale != "en-us") {
116 std::string localePath = RAMFS "res/i18n/" + locale + ".ini";
117 loadI18nFile(localePath, TextElement::i18nCache);
118 }
119
120 TextElement::useSimplifiedChineseFont = false;
121 TextElement::useKoreanFont = false;
122 TextElement::useJapaneseFont = false;
123
124 if (locale == "zh-cn") {
125 printf("Overriding font choice for Simplified Chinese\n");
126 TextElement::useSimplifiedChineseFont = true;
127 }
128 if (locale == "ko-kr") {
129 printf("Overriding font choice for Korean\n");
130 TextElement::useKoreanFont = true;
131 }
132 if (locale == "ja-jp") {
133 printf("Overriding font choice for Japanese\n");
134 TextElement::useJapaneseFont = true;
135 }
136}

◆ setColor()

void Chesto::TextElement::setColor ( const CST_Color &  color)

Definition at line 159 of file TextElement.cpp.

160{
161 this->textColor = color;
162}

◆ setFont()

void Chesto::TextElement::setFont ( int  font_type)

Definition at line 164 of file TextElement.cpp.

165{
166 this->textFont = font_type;
167}

◆ setSize()

void Chesto::TextElement::setSize ( int  size)

Definition at line 154 of file TextElement.cpp.

155{
156 this->textSize = size;
157}

◆ setText()

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

Definition at line 149 of file TextElement.cpp.

150{
151 this->text = text;
152}

◆ setWrappedWidth()

void Chesto::TextElement::setWrappedWidth ( int  wrapped_width)

Definition at line 169 of file TextElement.cpp.

170{
171 this->textWrappedWidth = wrapped_width;
172}

◆ update()

void Chesto::TextElement::update ( bool  forceUpdate = false)

update TextElement with changes

Definition at line 174 of file TextElement.cpp.

175{
176 std::string key = Texture::textElemPrefix + text + std::to_string(textSize);
177
178 clear();
179
180 if (!loadFromCache(key) || forceUpdate)
181 {
182 int actualFont = textFont;
183 if (TextElement::useSimplifiedChineseFont && textFont == NORMAL) {
184 actualFont = SIMPLIFIED_CHINESE;
185 }
186 if (TextElement::useKoreanFont && textFont == NORMAL) {
187 actualFont = KOREAN;
188 }
189 if (TextElement::useJapaneseFont && textFont == NORMAL) {
190 actualFont = JAPANESE;
191 }
192
193 // also, if the specific text string is in the map of force-lang strings, always use that font instead
194 if (forcedLangFonts.find(text) != forcedLangFonts.end()) {
195 actualFont = forcedLangFonts[text];
196 }
197
198 auto fontPath = fontPaths[actualFont % 7];
199 if (customFontPath != "") {
200 fontPath = customFontPath.c_str();
201 }
202
203 TTF_Font* font = TTF_OpenFont(fontPath, textSize);
204
205 if (font == NULL) {
206 printf("TTF_OpenFont failed for '%s' at size %d: %s\n", fontPath, textSize, TTF_GetError());
207 width = 0;
208 height = 0;
209 return;
210 }
211
212 CST_Surface *textSurface = ((actualFont == ICON) || (textWrappedWidth == 0)) ?
213 TTF_RenderUTF8_Blended(font, text.c_str(), textColor) :
214 TTF_RenderUTF8_Blended_Wrapped(font, text.c_str(), textColor, textWrappedWidth);
215 if(textSurface==NULL) printf("TTF_GetError: %s\n", TTF_GetError());
216
217 loadFromSurfaceSaveToCache(key, textSurface);
218
219 CST_FreeSurface(textSurface);
220 TTF_CloseFont(font);
221 }
222
223 getTextureSize(&width, &height);
224}
int width
width and height of this element (must be manually set, isn't usually calculated (but is in some case...
Definition: Element.hpp:132
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:96
void clear(void)
Reinitialize Texture Resets texture content, size and color.
Definition: Texture.cpp:13
bool loadFromCache(std::string &key)
Loads the texture from caches Returns true if successful.
Definition: Texture.cpp:81
void getTextureSize(int *w, int *h)
Return texture's original size.
Definition: Texture.cpp:230

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

Referenced by Chesto::AlertDialog::AlertDialog().

Member Data Documentation

◆ curLang

std::string Chesto::TextElement::curLang = "en-us"
static

Definition at line 46 of file TextElement.hpp.

◆ customFontPath

std::string Chesto::TextElement::customFontPath = ""

Definition at line 41 of file TextElement.hpp.

◆ forcedLangFonts

std::map< std::string, int > Chesto::TextElement::forcedLangFonts = {}
static

Definition at line 53 of file TextElement.hpp.

◆ i18nCache

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

Definition at line 43 of file TextElement.hpp.

◆ text

std::string Chesto::TextElement::text = ""

Definition at line 38 of file TextElement.hpp.

◆ useJapaneseFont

bool Chesto::TextElement::useJapaneseFont = false
static

Definition at line 51 of file TextElement.hpp.

◆ useKoreanFont

bool Chesto::TextElement::useKoreanFont = false
static

Definition at line 50 of file TextElement.hpp.

◆ useSimplifiedChineseFont

bool Chesto::TextElement::useSimplifiedChineseFont = false
static

Definition at line 49 of file TextElement.hpp.


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