Add Lua function lcd.drawTextLines(...) drawing text across multiple lines inside bounding box

This commit is contained in:
jfrickmann
2021-09-02 14:33:14 -04:00
parent 88b8ac86fb
commit b100d9a164
3 changed files with 40 additions and 4 deletions
+3 -3
View File
@@ -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, " ");
+1 -1
View File
@@ -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);
+36
View File
@@ -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 },