Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» MarlinUI menu tweaks
Browse files Browse the repository at this point in the history
Changes in prep for #26339
  • Loading branch information
thinkyhead committed Oct 20, 2023
1 parent 4b0b00c commit b0ece8f
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 149 deletions.
6 changes: 3 additions & 3 deletions Marlin/src/lcd/HD44780/marlinui_HD44780.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1165,20 +1165,20 @@ void MarlinUI::draw_status_screen() {
#endif // ADVANCED_PAUSE_FEATURE

// Draw a static item with no left-right margin required. Centered by default.
void MenuItem_static::draw(const uint8_t row, FSTR_P const fstr, const uint8_t style/*=SS_DEFAULT*/, const char *vstr/*=nullptr*/) {
void MenuItem_static::draw(const uint8_t row, FSTR_P const ftpl, const uint8_t style/*=SS_DEFAULT*/, const char *vstr/*=nullptr*/) {
lcd_moveto(0, row);

int8_t n = LCD_WIDTH;
const bool center = bool(style & SS_CENTER), full = bool(style & SS_FULL);
const int8_t plen = fstr ? utf8_strlen(fstr) : 0,
const int8_t plen = ftpl ? utf8_strlen(ftpl) : 0,
vlen = vstr ? utf8_strlen(vstr) : 0;
int8_t pad = (center || full) ? n - plen - vlen : 0;

// SS_CENTER: Pad with half of the unused space first
if (center) for (int8_t lpad = pad / 2; lpad > 0; --lpad) { lcd_put_u8str(F(" ")); n--; }

// Draw as much of the label as fits
if (plen) n -= lcd_put_u8str(fstr, itemIndex, itemStringC, itemStringF, n - vlen);
if (plen) n -= lcd_put_u8str(ftpl, itemIndex, itemStringC, itemStringF, n - vlen);

if (vlen && n > 0) {
// SS_FULL: Pad with enough space to justify the value
Expand Down
10 changes: 5 additions & 5 deletions Marlin/src/lcd/TFTGLCD/marlinui_TFTGLCD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ void MarlinUI::draw_status_screen() {
#endif

// Draw a static item with no left-right margin required. Centered by default.
void MenuItem_static::draw(const uint8_t row, FSTR_P const fstr, const uint8_t style/*=SS_DEFAULT*/, const char *vstr/*=nullptr*/) {
void MenuItem_static::draw(const uint8_t row, FSTR_P const ftpl, const uint8_t style/*=SS_DEFAULT*/, const char *vstr/*=nullptr*/) {
if (!PanelDetected) return;
lcd_moveto(0, row);

This comment has been minimized.

Copy link
@classicrocker883

classicrocker883 Oct 22, 2023

Contributor

line 978, 986 needs fstr swapped to ftpl

Expand Down Expand Up @@ -1004,25 +1004,25 @@ void MarlinUI::draw_status_screen() {
}

// Draw a generic menu item with pre_char (if selected) and post_char
void MenuItemBase::_draw(const bool sel, const uint8_t row, FSTR_P const fstr, const char pre_char, const char post_char) {
void MenuItemBase::_draw(const bool sel, const uint8_t row, FSTR_P const ftpl, const char pre_char, const char post_char) {
if (!PanelDetected) return;
lcd_moveto(0, row);
lcd.write(sel ? pre_char : ' ');
uint8_t n = LCD_WIDTH - 2;
n -= lcd_put_u8str(fstr, itemIndex, itemStringC, itemStringF, n);
n -= lcd_put_u8str(ftpl, itemIndex, itemStringC, itemStringF, n);
for (; n; --n) lcd.write(' ');
lcd.write(post_char);
lcd.print_line();
}

// Draw a menu item with a (potentially) editable value
void MenuEditItemBase::draw(const bool sel, const uint8_t row, FSTR_P const fstr, const char * const inStr, const bool pgm) {
void MenuEditItemBase::draw(const bool sel, const uint8_t row, FSTR_P const ftpl, const char * const inStr, const bool pgm) {
if (!PanelDetected) return;
const uint8_t vlen = inStr ? (pgm ? utf8_strlen_P(inStr) : utf8_strlen(inStr)) : 0;
lcd_moveto(0, row);
lcd.write(sel ? LCD_STR_ARROW_RIGHT[0] : ' ');
uint8_t n = LCD_WIDTH - 2 - vlen;
n -= lcd_put_u8str(fstr, itemIndex, itemStringC, itemStringF, n);
n -= lcd_put_u8str(ftpl, itemIndex, itemStringC, itemStringF, n);
if (vlen) {
lcd.write(':');
for (; n; --n) lcd.write(' ');
Expand Down
107 changes: 53 additions & 54 deletions Marlin/src/lcd/dogm/marlinui_DOGM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,63 +411,62 @@ void MarlinUI::clear_lcd() { } // Automatically cleared by Picture Loop

// Draw a static line of text in the same idiom as a menu item
void MenuItem_static::draw(const uint8_t row, FSTR_P const ftpl, const uint8_t style/*=SS_DEFAULT*/, const char *vstr/*=nullptr*/) {

if (mark_as_selected(row, style & SS_INVERT)) {
pixel_len_t n = LCD_PIXEL_WIDTH; // pixel width of string allowed

const bool center = bool(style & SS_CENTER), full = bool(style & SS_FULL);
const int pwide = ftpl ? calculateWidth(ftpl) : 0,
vlen = vstr ? utf8_strlen(vstr) : 0;
int pad = (center || full) ? ((LCD_PIXEL_WIDTH) - pwide - vlen * (MENU_FONT_WIDTH)) / (MENU_FONT_WIDTH) : 0;

// SS_CENTER: Pad with half of the unused space first
if (center) for (int lpad = pad / 2; lpad > 0; --lpad) n -= lcd_put_u8str(F(" "));

// Draw as much of the label as fits
if (pwide) n -= lcd_put_u8str(ftpl, itemIndex, itemStringC, itemStringF, n / (MENU_FONT_WIDTH)) * (MENU_FONT_WIDTH);

if (vlen) {
// SS_FULL: Pad with enough space to justify the value
if (full && !center && n > MENU_FONT_WIDTH) {
// Move the leading colon from the value to the label
if (*vstr == ':') { n -= lcd_put_u8str(F(":")); vstr++; }
// Move spaces to the padding
while (*vstr == ' ') { vstr++; pad++; }
// Pad in-between
for (; pad > 0; --pad) n -= lcd_put_u8str(F(" "));
}
n -= lcd_put_u8str_max(vstr, n);
if (!mark_as_selected(row, style & SS_INVERT)) return;

pixel_len_t n = LCD_PIXEL_WIDTH; // pixel width of string allowed

const bool center = bool(style & SS_CENTER), full = bool(style & SS_FULL);
const int pwide = ftpl ? calculateWidth(ftpl) : 0,
vlen = vstr ? utf8_strlen(vstr) : 0;
int pad = (center || full) ? ((LCD_PIXEL_WIDTH) - pwide - vlen * (MENU_FONT_WIDTH)) / (MENU_FONT_WIDTH) : 0;

// SS_CENTER: Pad with half of the unused space first
if (center) for (int lpad = pad / 2; lpad > 0; --lpad) n -= lcd_put_u8str(F(" "));

// Draw as much of the label as fits
if (pwide) n -= lcd_put_u8str(ftpl, itemIndex, itemStringC, itemStringF, n / (MENU_FONT_WIDTH)) * (MENU_FONT_WIDTH);

if (vlen) {
// SS_FULL: Pad with enough space to justify the value
if (full && !center && n > MENU_FONT_WIDTH) {
// Move the leading colon from the value to the label
if (*vstr == ':') { n -= lcd_put_u8str(F(":")); vstr++; }
// Move spaces to the padding
while (*vstr == ' ') { vstr++; pad++; }
// Pad in-between
for (; pad > 0; --pad) n -= lcd_put_u8str(F(" "));
}
while (n > MENU_FONT_WIDTH) n -= lcd_put_u8str(F(" "));
n -= lcd_put_u8str_max(vstr, n);
}
while (n > MENU_FONT_WIDTH) n -= lcd_put_u8str(F(" "));
}

// Draw a generic menu item
void MenuItemBase::_draw(const bool sel, const uint8_t row, FSTR_P const ftpl, const char, const char post_char) {
if (mark_as_selected(row, sel)) {
uint8_t n = LCD_WIDTH - 1;
n -= lcd_put_u8str(ftpl, itemIndex, itemStringC, itemStringF, n);
for (; n; --n) lcd_put_u8str(F(" "));
lcd_put_lchar(LCD_PIXEL_WIDTH - (MENU_FONT_WIDTH), row_y2, post_char);
lcd_put_u8str(F(" "));
}
if (!mark_as_selected(row, sel)) return;

uint8_t n = LCD_WIDTH - 1;
n -= lcd_put_u8str(ftpl, itemIndex, itemStringC, itemStringF, n);
for (; n; --n) lcd_put_u8str(F(" "));
lcd_put_lchar(LCD_PIXEL_WIDTH - (MENU_FONT_WIDTH), row_y2, post_char);
lcd_put_u8str(F(" "));
}

// Draw a menu item with an editable value
void MenuEditItemBase::draw(const bool sel, const uint8_t row, FSTR_P const ftpl, const char * const inStr, const bool pgm) {
if (mark_as_selected(row, sel)) {
const uint8_t vallen = (pgm ? utf8_strlen_P(inStr) : utf8_strlen(inStr)),
pixelwidth = (pgm ? uxg_GetUtf8StrPixelWidthP(u8g.getU8g(), inStr) : uxg_GetUtf8StrPixelWidth(u8g.getU8g(), inStr));
const u8g_uint_t prop = USE_WIDE_GLYPH ? 2 : 1;

uint8_t n = LCD_WIDTH - 2 - vallen * prop;
n -= lcd_put_u8str(ftpl, itemIndex, itemStringC, itemStringF, n);
if (vallen) {
lcd_put_u8str(F(":"));
for (; n; --n) lcd_put_u8str(F(" "));
lcd_moveto(LCD_PIXEL_WIDTH - _MAX((MENU_FONT_WIDTH) * vallen, pixelwidth + 2), row_y2);
if (pgm) lcd_put_u8str_P(inStr); else lcd_put_u8str(inStr);
}
if (!mark_as_selected(row, sel)) return;

const uint8_t vallen = (pgm ? utf8_strlen_P(inStr) : utf8_strlen(inStr)),
pixelwidth = (pgm ? uxg_GetUtf8StrPixelWidthP(u8g.getU8g(), inStr) : uxg_GetUtf8StrPixelWidth(u8g.getU8g(), inStr));
const u8g_uint_t prop = USE_WIDE_GLYPH ? 2 : 1;

uint8_t n = LCD_WIDTH - 2 - vallen * prop;
n -= lcd_put_u8str(ftpl, itemIndex, itemStringC, itemStringF, n);
if (vallen) {
lcd_put_u8str(F(":"));
for (; n; --n) lcd_put_u8str(F(" "));
lcd_moveto(LCD_PIXEL_WIDTH - _MAX((MENU_FONT_WIDTH) * vallen, pixelwidth + 2), row_y2);
if (pgm) lcd_put_u8str_P(inStr); else lcd_put_u8str(inStr);
}
}

Expand Down Expand Up @@ -545,13 +544,13 @@ void MarlinUI::clear_lcd() { } // Automatically cleared by Picture Loop
#if HAS_MEDIA

void MenuItem_sdbase::draw(const bool sel, const uint8_t row, FSTR_P const, CardReader &theCard, const bool isDir) {
if (mark_as_selected(row, sel)) {
const uint8_t maxlen = LCD_WIDTH - isDir;
if (isDir) lcd_put_lchar(LCD_STR_FOLDER[0]);
const pixel_len_t pixw = maxlen * (MENU_FONT_WIDTH);
pixel_len_t n = pixw - lcd_put_u8str_max(ui.scrolled_filename(theCard, maxlen, row, sel), pixw);
for (; n > MENU_FONT_WIDTH; n -= MENU_FONT_WIDTH) lcd_put_u8str(F(" "));
}
if (!mark_as_selected(row, sel)) return;

const uint8_t maxlen = LCD_WIDTH - isDir;
if (isDir) lcd_put_lchar(LCD_STR_FOLDER[0]);
const pixel_len_t pixw = maxlen * (MENU_FONT_WIDTH);
pixel_len_t n = pixw - lcd_put_u8str_max(ui.scrolled_filename(theCard, maxlen, row, sel), pixw);
for (; n > MENU_FONT_WIDTH; n -= MENU_FONT_WIDTH) lcd_put_u8str(F(" "));
}

#endif // HAS_MEDIA
Expand Down
142 changes: 71 additions & 71 deletions Marlin/src/lcd/e3v2/marlinui/ui_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,91 +308,91 @@ void MarlinUI::draw_status_message(const bool blink) {
void MenuItem_static::draw(const uint8_t row, FSTR_P const ftpl, const uint8_t style/*=SS_DEFAULT*/, const char *vstr/*=nullptr*/) {
// Call mark_as_selected to draw a bigger selection box
// and draw the text without a background
if (mark_as_selected(row, (bool)(style & SS_INVERT), true)) {
ui.set_font(DWIN_FONT_MENU);
dwin_font.solid = false;
dwin_font.fg = COLOR_WHITE;
if (!mark_as_selected(row, (bool)(style & SS_INVERT), true)) return;

dwin_string.set();
ui.set_font(DWIN_FONT_MENU);
dwin_font.solid = false;
dwin_font.fg = COLOR_WHITE;

const bool center = bool(style & SS_CENTER), full = bool(style & SS_FULL);
const int8_t plen = ftpl ? utf8_strlen(ftpl) : 0,
vlen = vstr ? utf8_strlen(vstr) : 0;
int8_t pad = (center || full) ? (LCD_WIDTH) - 1 - plen - vlen : 0;
dwin_string.set();

// SS_CENTER: Pad with half of the unused space first
if (center) for (int8_t lpad = pad / 2; lpad > 0; --lpad) dwin_string.add(' ');
const bool center = bool(style & SS_CENTER), full = bool(style & SS_FULL);
const int8_t plen = ftpl ? utf8_strlen(ftpl) : 0,
vlen = vstr ? utf8_strlen(vstr) : 0;
int8_t pad = (center || full) ? (LCD_WIDTH) - 1 - plen - vlen : 0;

// Append the templated label string
if (plen) {
dwin_string.add(ftpl, itemIndex, itemStringC, itemStringF);
pad -= dwin_string.length - plen;
}
// SS_CENTER: Pad with half of the unused space first
if (center) for (int8_t lpad = pad / 2; lpad > 0; --lpad) dwin_string.add(' ');

// SS_FULL: Pad with enough space to justify the value
if (vlen) {
if (full && !center) {
// Move the leading colon from the value to the label
if (*vstr == ':') { dwin_string.add(':'); vstr++; }
// Move spaces to the padding
while (*vstr == ' ') { vstr++; pad++; }
// Pad in-between
for (; pad > 0; --pad) dwin_string.add(' ');
}
// Append the value
dwin_string.add(vstr);
// Append the templated label string
if (plen) {
dwin_string.add(ftpl, itemIndex, itemStringC, itemStringF);
pad -= dwin_string.length - plen;
}

// SS_FULL: Pad with enough space to justify the value
if (vlen) {
if (full && !center) {
// Move the leading colon from the value to the label
if (*vstr == ':') { dwin_string.add(':'); vstr++; }
// Move spaces to the padding
while (*vstr == ' ') { vstr++; pad++; }
// Pad in-between
for (; pad > 0; --pad) dwin_string.add(' ');
}
// Append the value
dwin_string.add(vstr);
}

// SS_CENTER: Pad the rest of the string
if (center) for (int8_t rpad = pad - (pad / 2); rpad > 0; --rpad) dwin_string.add(' ');
// SS_CENTER: Pad the rest of the string
if (center) for (int8_t rpad = pad - (pad / 2); rpad > 0; --rpad) dwin_string.add(' ');

lcd_moveto(1, row);
lcd_put_dwin_string();
}
lcd_moveto(1, row);
lcd_put_dwin_string();
}

// Draw a generic menu item
void MenuItemBase::_draw(const bool sel, const uint8_t row, FSTR_P const ftpl, const char, const char post_char) {
if (mark_as_selected(row, sel)) {
ui.set_font(DWIN_FONT_MENU);
dwin_font.solid = false;
dwin_font.fg = COLOR_WHITE;
if (!mark_as_selected(row, sel)) return;

dwin_string.set(ftpl, itemIndex, itemStringC, itemStringF);
ui.set_font(DWIN_FONT_MENU);
dwin_font.solid = false;
dwin_font.fg = COLOR_WHITE;

pixel_len_t n = LCD_WIDTH - 1 - dwin_string.length;
while (--n > 1) dwin_string.add(' ');
dwin_string.set(ftpl, itemIndex, itemStringC, itemStringF);

dwin_string.add(post_char);
pixel_len_t n = LCD_WIDTH - 1 - dwin_string.length;
while (--n > 1) dwin_string.add(' ');

lcd_moveto(1, row);
lcd_put_dwin_string();
}
dwin_string.add(post_char);

lcd_moveto(1, row);
lcd_put_dwin_string();
}

//
// Draw a menu item with an editable value
//
void MenuEditItemBase::draw(const bool sel, const uint8_t row, FSTR_P const ftpl, const char * const inStr, const bool pgm) {
if (mark_as_selected(row, sel)) {
ui.set_font(DWIN_FONT_MENU);
dwin_font.solid = false;
dwin_font.fg = COLOR_WHITE;
if (!mark_as_selected(row, sel)) return;

const uint8_t vallen = (pgm ? utf8_strlen_P(inStr) : utf8_strlen(S(inStr)));
ui.set_font(DWIN_FONT_MENU);
dwin_font.solid = false;
dwin_font.fg = COLOR_WHITE;

dwin_string.set(ftpl, itemIndex, itemStringC, itemStringF);
if (vallen) dwin_string.add(':');
const uint8_t vallen = (pgm ? utf8_strlen_P(inStr) : utf8_strlen(S(inStr)));

lcd_moveto(1, row);
lcd_put_dwin_string();
dwin_string.set(ftpl, itemIndex, itemStringC, itemStringF);
if (vallen) dwin_string.add(':');

if (vallen) {
dwin_font.fg = COLOR_YELLOW;
dwin_string.set(inStr);
lcd_moveto(LCD_WIDTH - vallen - 1, row);
lcd_put_dwin_string();
}
lcd_moveto(1, row);
lcd_put_dwin_string();

if (vallen) {
dwin_font.fg = COLOR_YELLOW;
dwin_string.set(inStr);
lcd_moveto(LCD_WIDTH - vallen - 1, row);
lcd_put_dwin_string();
}
}

Expand Down Expand Up @@ -464,21 +464,21 @@ void MarlinUI::draw_status_message(const bool blink) {
#if HAS_MEDIA

void MenuItem_sdbase::draw(const bool sel, const uint8_t row, FSTR_P const, CardReader &theCard, const bool isDir) {
if (mark_as_selected(row, sel)) {
dwin_string.set();
if (!mark_as_selected(row, sel)) return;

uint8_t maxlen = LCD_WIDTH - 1;
if (isDir) {
dwin_string.add(LCD_STR_FOLDER " ");
maxlen -= 2;
}
dwin_string.set();

dwin_string.add(ui.scrolled_filename(theCard, maxlen, row, sel), maxlen);
uint8_t n = maxlen - dwin_string.length;
while (n > 0) { dwin_string.add(' '); --n; }
lcd_moveto(1, row);
lcd_put_dwin_string();
uint8_t maxlen = LCD_WIDTH - 1;
if (isDir) {
dwin_string.add(LCD_STR_FOLDER " ");
maxlen -= 2;
}

dwin_string.add(ui.scrolled_filename(theCard, maxlen, row, sel), maxlen);
uint8_t n = maxlen - dwin_string.length;
while (n > 0) { dwin_string.add(' '); --n; }
lcd_moveto(1, row);
lcd_put_dwin_string();
}

#endif // HAS_MEDIA
Expand Down
Loading

0 comments on commit b0ece8f

Please sign in to comment.