Skip to content

Instantly share code, notes, and snippets.

@jenschr
Created November 25, 2020 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jenschr/8261c66b4ac4357348eae9db6957b221 to your computer and use it in GitHub Desktop.
Save jenschr/8261c66b4ac4357348eae9db6957b221 to your computer and use it in GitHub Desktop.
#ifndef GENERIC_TEXTBLOCK_H
#define GENERIC_TEXTBLOCK_H
#define MAXCHARS_TEXTBLOCK 50
#define MAXCHARS_TEXTBLOCK_TF 30
class TextBlock
{
public:
void begin( RA8875 &tft, uint8_t fontSize, uint16_t fontColor = 0 ){
_tft = &tft;
_tft->setFontScale(0);
_fontSize = fontSize;
_fontColor = fontColor;
}
void setDirty(){
_isDirty = true;
}
void draw()
{
if( _isDirty )
{
_tft->setFontScale(0);
_tft->setFontSize(X16);
_tft->setTextColor(_fontColor);
// out with the old ...
_tft->fillRect(_oldx, _oldy-7, int(_width-_oldx), int(_fontSize+20), _clearColor);
// ... in with the new
_tft->setCursor(_x, _y);
_tft->print(_text);
// When text is written, we need to save the last metrics
_tft->getCursor(_width, _height);
_oldx = _x;
_oldy = _y;
// Always end by setting as clean
_isDirty = false;
}
}
bool getDirty()
{
return _isDirty;
}
void setPos(int x, int y)
{
if( _x != x){
_isDirty = true;
_x = x;
}
if( _y != y){
_isDirty = true;
_y = y;
}
}
void setText(char const * newText)
{
uint8_t comp = strcmp(_text,newText);
if( comp != 0){
strlcpy(_oldtext,_text,_textLength); // copy the old text so we can overwrite it
_textLength = strlen(newText)+1; // always add one to include NUL-character
// Check that we don't go over max length
if (_textLength > MAXCHARS_TEXTBLOCK_TF)
_textLength = MAXCHARS_TEXTBLOCK_TF;
strlcpy(_text,newText,_textLength); // save the new text for display
_isDirty = true;
}
}
void setText(int newText)
{
_tft->print(newText);
}
protected:
uint8_t _fontSize;
uint16_t _fontColor;
uint8_t _textLength = 0;
int _available_width;
char _text[MAXCHARS_TEXTBLOCK_TF+1] = {};
char _oldtext[MAXCHARS_TEXTBLOCK_TF+1] = {};
char *_name;
int16_t _x = 0;
int16_t _y = 0;
int16_t _width;
int16_t _height;
int16_t _oldx = 800;
int16_t _oldy = 800;
bool _isDirty = true;
int _clearColor = RA8875_WHITE;
RA8875 *_tft;
};
#endif /*GENERIC_TEXTBLOCK_H*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment