From b100d9a1644522ff97095d27b2a233c23dbc53b2 Mon Sep 17 00:00:00 2001 From: jfrickmann Date: Thu, 2 Sep 2021 14:31:57 -0400 Subject: [PATCH] Add Lua function lcd.drawTextLines(...) drawing text across multiple lines inside bounding box --- radio/src/gui/colorlcd/draw_functions.cpp | 6 ++-- radio/src/gui/colorlcd/draw_functions.h | 2 +- radio/src/lua/api_colorlcd.cpp | 36 +++++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/radio/src/gui/colorlcd/draw_functions.cpp b/radio/src/gui/colorlcd/draw_functions.cpp index 82536ddde..148b2ae9a 100644 --- a/radio/src/gui/colorlcd/draw_functions.cpp +++ b/radio/src/gui/colorlcd/draw_functions.cpp @@ -504,15 +504,15 @@ void drawHexNumber(BitmapBuffer * dc, coord_t x, coord_t y, uint32_t val, LcdFla } } -void drawTextLines(BitmapBuffer * dc, coord_t left, coord_t top, coord_t width, coord_t height, char * str, LcdFlags flags) +void drawTextLines(BitmapBuffer * dc, coord_t left, coord_t top, coord_t width, coord_t height, const char * str, LcdFlags flags) { coord_t x = left; coord_t y = top; coord_t line = getFontHeight(flags & 0xFFFF); coord_t space = getTextWidth(" ", 1, flags); coord_t word; - char * eos = str + strlen(str); - char * nxt; + const char * eos = str + strlen(str); + const char * nxt; while (str < eos) { nxt = strstr(str, " "); diff --git a/radio/src/gui/colorlcd/draw_functions.h b/radio/src/gui/colorlcd/draw_functions.h index 802310be2..3262bdcf8 100644 --- a/radio/src/gui/colorlcd/draw_functions.h +++ b/radio/src/gui/colorlcd/draw_functions.h @@ -72,7 +72,7 @@ void drawGPSPosition(BitmapBuffer * dc, coord_t x, coord_t y, int32_t longitude, void drawDate(BitmapBuffer * dc, coord_t x, coord_t y, TelemetryItem & telemetryItem, LcdFlags flags = 0); void drawValueWithUnit(BitmapBuffer * dc, coord_t x, coord_t y, int val, uint8_t unit, LcdFlags flags = 0); void drawHexNumber(BitmapBuffer * dc, coord_t x, coord_t y, uint32_t val, LcdFlags flags = 0); -void drawTextLines(BitmapBuffer * dc, coord_t left, coord_t top, coord_t width, coord_t height, char * str, LcdFlags flags); +void drawTextLines(BitmapBuffer * dc, coord_t left, coord_t top, coord_t width, coord_t height, const char * str, LcdFlags flags); inline void drawChn(BitmapBuffer * dc, coord_t x, coord_t y, uint8_t idx, LcdFlags flags) { drawSource(dc, x, y, MIXSRC_CH1 + idx - 1, flags); diff --git a/radio/src/lua/api_colorlcd.cpp b/radio/src/lua/api_colorlcd.cpp index 5a9e71203..5213ac6cf 100644 --- a/radio/src/lua/api_colorlcd.cpp +++ b/radio/src/lua/api_colorlcd.cpp @@ -289,6 +289,41 @@ static int luaLcdSizeText(lua_State *L) return 2; } +/*luadoc +@function lcd.drawTextLines(x, y, w, h, text [, flags]) + +Draw text inside rectangle (x,y,w,h) with line breaks + +@param x,y (positive numbers) starting coordinate + +@param w,h (positive numbers) width and height of bounding rectangle + +@param text (string) text to display + +@param flags (optional) please see [Lcd functions overview](../lcd-functions-less-than-greater-than-luadoc-begin-lcd/lcd_functions-overview.html) for drawing flags and colors, and [Appendix](../../part_vii_-_appendix/fonts.md) for available characters in each font set. INVERS and BLINK are not implemented. + +@status current Introduced in 2.5.0 +*/ +static int luaLcdDrawTextLines(lua_State *L) +{ + if (!luaLcdAllowed || !luaLcdBuffer) + return 0; + + int x = luaL_checkinteger(L, 1); + int y = luaL_checkinteger(L, 2); + int w = luaL_checkinteger(L, 3); + int h = luaL_checkinteger(L, 4); + const char * s = luaL_checkstring(L, 5); + LcdFlags flags = luaL_optunsigned(L, 6, 0); + + // apply text offsets, needed to align 2.4.x to 2.3.x font baselines + x += getTextHorizontalOffset(flags); + y += getTextVerticalOffset(flags); + + drawTextLines(luaLcdBuffer, x, y, w, h, s, flags); + return 0; +} + /*luadoc @function lcd.drawTimer(x, y, value [, flags [, inversColor]]) @@ -1159,6 +1194,7 @@ const luaL_Reg lcdLib[] = { { "drawRectangle", luaLcdDrawRectangle }, { "drawFilledRectangle", luaLcdDrawFilledRectangle }, { "drawText", luaLcdDrawText }, + { "drawTextLines", luaLcdDrawTextLines }, { "sizeText", luaLcdSizeText }, { "drawTimer", luaLcdDrawTimer }, { "drawNumber", luaLcdDrawNumber },