Skip to content

Commit

Permalink
Merge pull request #589 from rennancockles/dev
Browse files Browse the repository at this point in the history
Scrollable Text Area changes
  • Loading branch information
rennancockles authored Dec 22, 2024
2 parents 50d0781 + 709f833 commit 966b7f1
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 215 deletions.
42 changes: 35 additions & 7 deletions src/core/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,40 @@ void padprint(double n, int digits, int16_t padx) {
}

void padprintln(const String &s, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.println(s);
if (s.isEmpty()) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.println(s);
return;
}

String buff;
size_t start = 0;
int _maxCharsInLine = (WIDTH - (padx+1) * BORDER_PAD_X) / (FP*LW);

// automatically split into multiple lines
while( !(buff = s.substring(start, start + _maxCharsInLine)).isEmpty() ){
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.println(buff);
start += buff.length();
}
}
void padprintln(const char str[], int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.println(str);
if (str == "") {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.println(str);
return;
}

String buff;
size_t start = 0;
int _maxCharsInLine = (WIDTH - (padx+1) * BORDER_PAD_X) / (FP*LW);

// automatically split into multiple lines
while( !(buff = String(str).substring(start, start + _maxCharsInLine)).isEmpty() ){
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.println(buff);
start += buff.length();
}
}
void padprintln(char c, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
Expand Down Expand Up @@ -407,7 +435,7 @@ Opt_Coord drawOptions(int index,std::vector<Option>& options, uint16_t fgcolor,
else tft.setTextColor(fgcolor,bgcolor);

String text="";
if(i==index) {
if(i==index) {
text+=">";
coord.x=WIDTH*0.10+5+FM*LW;
coord.y=tft.getCursorY()+4;
Expand Down Expand Up @@ -648,7 +676,7 @@ Opt_Coord listFiles(int index, std::vector<FileList> fileList) {
else if(fileList[i].operation==true) tft.setTextColor(ALCOLOR, bruceConfig.bgColor);
else { tft.setTextColor(bruceConfig.priColor,bruceConfig.bgColor); }

if (index==i) {
if (index==i) {
txt=">";
coord.x=10+FM*LW;
coord.y=tft.getCursorY();
Expand Down Expand Up @@ -863,7 +891,7 @@ bool showJpeg(FS fs, String filename, int x, int y, bool center) {
}

if (decoded) {
if(center) {
if(center) {
x=(WIDTH-JpegDec.width)/2;
y=(HEIGHT-JpegDec.height)/2;
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/menu_items/ConfigMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "core/settings.h"
#include "core/i2c_finder.h"
#include "core/wifi_common.h"
#include "core/utils.h"

void ConfigMenu::optionsMenu() {
options = {
Expand Down Expand Up @@ -31,6 +32,7 @@ void ConfigMenu::optionsMenu() {

void ConfigMenu::devMenu(){
options = {
{"Device Info", [=]() { showDeviceInfo(); }},
{"MAC Address", [=]() { checkMAC(); }},
{"I2C Finder", [=]() { find_i2c_addresses(); }},
{"Back", [=]() { optionsMenu(); }},
Expand Down
157 changes: 113 additions & 44 deletions src/core/scrollableTextArea.cpp
Original file line number Diff line number Diff line change
@@ -1,58 +1,127 @@
#include "scrollableTextArea.h"

// god, it's ugly
ScrollableTextArea::ScrollableTextArea(uint8_t fontSize,
int16_t startX,
int16_t startY,
int32_t width,
int32_t height)
: _startLine{0},
_redraw{true},
_fontSize(fontSize),
_startX(startX),
_startY(startY),
_width(width),
_height(height)
#if defined(HAS_SCREEN)
,_scrollBuffer(&tft)
#endif
ScrollableTextArea::ScrollableTextArea(const String& title) :
_startLine{0},
_redraw{true},
_title(title),
_fontSize(FP),
_startX(BORDER_PAD_X),
_startY(BORDER_PAD_Y),
_width(WIDTH - 2*BORDER_PAD_X),
_height(HEIGHT - BORDER_PAD_X - BORDER_PAD_Y)
#ifdef HAS_SCREEN
,_scrollBuffer(&tft)
#endif
{
#if defined(HAS_SCREEN)
_scrollBuffer.createSprite(_width, _height);
_scrollBuffer.setTextColor(bruceConfig.priColor);
_scrollBuffer.setTextSize(_fontSize);
_scrollBuffer.fillSprite(TFT_BLACK);

_maxCharsInLine = floor(width / _scrollBuffer.textWidth("w", _fontSize));
_pxlsPerLine = _scrollBuffer.fontHeight() + 2;
_maxLinesInArea = floor(_height / _pxlsPerLine);
#endif
drawMainBorder();

if (!_title.isEmpty()) {
printTitle(_title);
_startY = tft.getCursorY();
_height -= (_startY - BORDER_PAD_Y);
}

setup();
}

ScrollableTextArea::ScrollableTextArea(
uint8_t fontSize, int16_t startX, int16_t startY, int32_t width, int32_t height
) : _startLine{0},
_redraw{true},
_title(""),
_fontSize(fontSize),
_startX(startX),
_startY(startY),
_width(width),
_height(height)
#ifdef HAS_SCREEN
,_scrollBuffer(&tft)
#endif
{
drawMainBorder();
setup();
}

ScrollableTextArea::~ScrollableTextArea(){
#if defined(HAS_SCREEN)
_scrollBuffer.deleteSprite();
#endif
ScrollableTextArea::~ScrollableTextArea() {
#ifdef HAS_SCREEN
_scrollBuffer.deleteSprite();
#endif
}

void ScrollableTextArea::setup() {
#ifdef HAS_SCREEN
_scrollBuffer.createSprite(_width, _height);
_scrollBuffer.setTextColor(bruceConfig.priColor);
_scrollBuffer.setTextSize(_fontSize);
_scrollBuffer.fillSprite(bruceConfig.bgColor);

_maxCharsInLine = floor(_width / _scrollBuffer.textWidth("w", _fontSize));
_pxlsPerLine = _scrollBuffer.fontHeight() + 2;
_maxLinesInArea = floor(_height / _pxlsPerLine);
#endif
}

void ScrollableTextArea::scrollUp() {
if( _startLine ){
--_startLine;
if (_startLine) {
_startLine--;
_redraw = true;
}
}

void ScrollableTextArea::scrollDown() {
if (_startLine + _maxLinesInArea < _lines.size()) {
++_startLine;
if (_startLine + _maxLinesInArea <= _lines.size()) {
if (_startLine == 0) _startLine++;
_startLine++;
_redraw = true;
}
}

void ScrollableTextArea::show(bool force) {
draw(force);

delay(100);

while(checkSelPress()) { update(force); yield(); }
while(!checkSelPress()) { update(force); yield(); }
}

void ScrollableTextArea::update(bool force) {
if (checkPrevPress()) scrollUp();
else if (checkNextPress()) scrollDown();

draw(force);
delay(100);
}

void ScrollableTextArea::fromFile(File file) {
while (file.available()) addLine(file.readStringUntil('\n'));

draw(true);
delay(100);
draw(true);
}

void ScrollableTextArea::fromString(const String& text) {
int startIdx = 0;
int endIdx = 0;

while (endIdx < text.length()) {
if (text[endIdx] == '\n') {
addLine(text.substring(startIdx, endIdx));
startIdx = endIdx + 1;
}

endIdx++;
}
}

// for devices it will act as a scrollable text area
#if defined(HAS_SCREEN)
#ifdef HAS_SCREEN
void ScrollableTextArea::addLine(const String& text) {
if( text.isEmpty() ) return;
if (text.isEmpty()) {
_lines.emplace_back("");
return;
}

String buff;
size_t start{0};
Expand All @@ -67,34 +136,34 @@ void ScrollableTextArea::addLine(const String& text) {
}

void ScrollableTextArea::draw(bool force) {
if( !_redraw && !force ) return;
if (!_redraw && !force) return;

_scrollBuffer.fillSprite(TFT_BLACK);
_scrollBuffer.fillSprite(bruceConfig.bgColor);

uint16_t yOffset = 0;
uint16_t lines = 0;

// if there is text above
if( _startLine ){
if (_startLine) {
_scrollBuffer.drawString("...", 0, yOffset);
yOffset += _pxlsPerLine;
++lines;
lines++;
}

int32_t tmpHeight = _height;
// if there is text below
if( _lines.size() - _startLine > _maxLinesInArea ){
if (_lines.size() - _startLine >= _maxLinesInArea) {
_scrollBuffer.drawString("...", 0, _height - _pxlsPerLine);
tmpHeight -= _pxlsPerLine;
++lines;
lines++;
}

size_t idx{_startLine};
while( yOffset < tmpHeight && lines < _maxLinesInArea && idx < _lines.size() ){
_scrollBuffer.drawString(_lines[idx], 0, yOffset);
yOffset += _pxlsPerLine;
++lines;
++idx;
lines++;
idx++;
}

_scrollBuffer.pushSprite(_startX, _startY);
Expand Down
25 changes: 20 additions & 5 deletions src/core/scrollableTextArea.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

class ScrollableTextArea {
public:
ScrollableTextArea(const String& title = "");

ScrollableTextArea(
uint8_t fontSize,
int16_t startX,
int16_t startY,
int32_t width,
int32_t height);
uint8_t fontSize,
int16_t startX,
int16_t startY,
int32_t width,
int32_t height
);

~ScrollableTextArea();

Expand All @@ -17,8 +20,16 @@ class ScrollableTextArea {

void addLine(const String& text);

void fromString(const String& text);

void fromFile(File file);

void draw(bool force = false);

void show(bool force = false);

private:
String _title;
uint16_t _startLine;
bool _redraw;
uint8_t _fontSize;
Expand All @@ -35,4 +46,8 @@ class ScrollableTextArea {
SerialDisplayClass& _scrollBuffer = tft;
#endif

void setup();

void update(bool force = false);

};
Loading

0 comments on commit 966b7f1

Please sign in to comment.