diff --git a/C/test.c b/C/test.c index 248997d..8b6b91d 100644 --- a/C/test.c +++ b/C/test.c @@ -6,16 +6,21 @@ int main(void) btui_t *bt = btui_enable(); if (!bt) return 1; int done = 0; - int x = 0, y = 0; + int x = 1, y = 1; while (!done) { const char *title = "BTUI C Demo"; - btui_set_attributes(bt, BTUI_FG_BLUE | BTUI_BOLD | BTUI_UNDERLINE); - btui_move_cursor(bt, (bt->width - (int)strlen(title)) / 2, 0); + btui_set_attributes(bt, BTUI_BG_BLUE | BTUI_FG_BLACK); + int w = (int)strlen(title); + int center = (bt->width - w) / 2; + btui_fill_box(bt, center-2, 0, w+4, 3); + btui_draw_shadow(bt, center-2, 0, w+4, 3); + + btui_move_cursor(bt, center, 1); btui_printf(bt, "%s", title); btui_set_attributes(bt, BTUI_NORMAL); btui_set_attributes(bt, BTUI_FG_NORMAL | BTUI_FAINT); - btui_move_cursor(bt, 0, bt->height-1); + btui_move_cursor(bt, bt->width-16, bt->height-2); btui_printf(bt, "Size = %dx%d", bt->width, bt->height); btui_set_attributes(bt, BTUI_NORMAL); btui_flush(bt); @@ -37,15 +42,19 @@ int main(void) break; default: { if (mouse_x != -1) { - x = mouse_x; + x = mouse_x - 15; y = mouse_y; } char buf[256] = {0}; btui_keyname(key, buf); + + btui_set_fg_hex(bt, 0xacff40); + btui_draw_linebox(bt, x, y, 30, 1); + btui_move_cursor(bt, x, y); - btui_set_bg_hex(bt, 0xacff40); - btui_set_attributes(bt, BTUI_FG_BLACK); + btui_set_fg_hex(bt, 0xacff40); btui_printf(bt, " Pressed: %s ", buf); + btui_set_attributes(bt, BTUI_NORMAL); fflush(bt->out); break; diff --git a/Lua/lbtui.c b/Lua/lbtui.c index 06a67cd..7bedea9 100644 --- a/Lua/lbtui.c +++ b/Lua/lbtui.c @@ -191,6 +191,56 @@ static int Lbtui_withbg(lua_State *L) return lua_gettop(L) - top; } +static int Lbtui_linebox(lua_State *L) +{ + btui_t **bt = (btui_t**)lua_touserdata(L, 1); + if (bt == NULL) luaL_error(L, "Not a BTUI object"); + int isnum; + lua_Integer x = lua_tointegerx(L, 2, &isnum); + if (!isnum) luaL_argerror(L, 2, "expected integer for x value"); + lua_Integer y = lua_tointegerx(L, 3, &isnum); + if (!isnum) luaL_argerror(L, 3, "expected integer for y value"); + lua_Integer w = lua_tointegerx(L, 4, &isnum); + if (!isnum) luaL_argerror(L, 4, "expected integer for w value"); + lua_Integer h = lua_tointegerx(L, 5, &isnum); + if (!isnum) luaL_argerror(L, 5, "expected integer for h value"); + btui_draw_linebox(*bt, x, y, w, h); + return 0; +} + +static int Lbtui_fillbox(lua_State *L) +{ + btui_t **bt = (btui_t**)lua_touserdata(L, 1); + if (bt == NULL) luaL_error(L, "Not a BTUI object"); + int isnum; + lua_Integer x = lua_tointegerx(L, 2, &isnum); + if (!isnum) luaL_argerror(L, 2, "expected integer for x value"); + lua_Integer y = lua_tointegerx(L, 3, &isnum); + if (!isnum) luaL_argerror(L, 3, "expected integer for y value"); + lua_Integer w = lua_tointegerx(L, 4, &isnum); + if (!isnum) luaL_argerror(L, 4, "expected integer for w value"); + lua_Integer h = lua_tointegerx(L, 5, &isnum); + if (!isnum) luaL_argerror(L, 5, "expected integer for h value"); + btui_fill_box(*bt, x, y, w, h); + return 0; +} +static int Lbtui_shadow(lua_State *L) +{ + btui_t **bt = (btui_t**)lua_touserdata(L, 1); + if (bt == NULL) luaL_error(L, "Not a BTUI object"); + int isnum; + lua_Integer x = lua_tointegerx(L, 2, &isnum); + if (!isnum) luaL_argerror(L, 2, "expected integer for x value"); + lua_Integer y = lua_tointegerx(L, 3, &isnum); + if (!isnum) luaL_argerror(L, 3, "expected integer for y value"); + lua_Integer w = lua_tointegerx(L, 4, &isnum); + if (!isnum) luaL_argerror(L, 4, "expected integer for w value"); + lua_Integer h = lua_tointegerx(L, 5, &isnum); + if (!isnum) luaL_argerror(L, 5, "expected integer for h value"); + btui_draw_shadow(*bt, x, y, w, h); + return 0; +} + static int Lbtui_setattributes(lua_State *L) { btui_t **bt = (btui_t**)lua_touserdata(L, 1); @@ -405,6 +455,9 @@ static const luaL_Reg Rclass_metamethods[] = {"setattributes", Lbtui_setattributes}, {"unsetattributes", Lbtui_unsetattributes}, {"suspend", Lbtui_suspend}, + {"linebox", Lbtui_linebox}, + {"fillbox", Lbtui_fillbox}, + {"shadow", Lbtui_shadow}, {"width", Lbtui_width}, {"height", Lbtui_height}, {NULL, NULL} diff --git a/Lua/test.lua b/Lua/test.lua index b676695..b79dea0 100644 --- a/Lua/test.lua +++ b/Lua/test.lua @@ -2,7 +2,7 @@ local btui = require("btui") btui(function(bt) local key = nil - local x, y = 0, 0 + local x, y = 1, 1 while key ~= "q" and key ~= "Ctrl-c" do if key == "?" then bt:withdisabled(function() @@ -15,19 +15,21 @@ btui(function(bt) end bt:clear() - bt:move(x, y) - bt:withbg(.8,.95,.2, function() - bt:withfg(0,0,0, function() - bt:print(" Pressed: ", key, " ") - end) + + bt:withfg(.8,.95,.2, function() + bt:linebox(x, y, 30, 1); + bt:move(x, y) + bt:print("Pressed: ", key) end) - local title = "Lua BTUI Demo" - bt:withattributes("bold", "underline", function() - bt:withfg(100,200,255, function() - bt:move(math.floor((bt:width()-#title)/2), 0) - bt:print(title) - end) + bt:withattributes("bg_blue", "fg_black", function() + local title = "Lua BTUI Demo" + local w = #title + local center = math.floor((bt:width() - w) / 2) + bt:fillbox(center-2, 0, w+4, 3) + bt:shadow(center-2, 0, w+4, 3) + bt:move(center, 1) + bt:print(title) end) local s = ("Size: (%dx%d)"):format(bt:width(), bt:height()) diff --git a/btui.h b/btui.h index 1dc6649..287a650 100644 --- a/btui.h +++ b/btui.h @@ -49,6 +49,9 @@ int btui_set_bg_hex(btui_t *bt, int hex); #define btui_clear_left(bt) fputs("\033[1K", (bt)->out) #define btui_clear_line(bt) fputs("\033[2K", (bt)->out) #define btui_suspend(bt) kill(getpid(), SIGTSTP) +void btui_draw_linebox(btui_t *bt, int x, int y, int w, int h); +void btui_fill_box(btui_t *bt, int x, int y, int w, int h); +void btui_draw_shadow(btui_t *bt, int x, int y, int w, int h); static btui_t current_bt; @@ -590,5 +593,54 @@ int btui_scroll(btui_t *bt, int firstline, int lastline, int scroll_amount) return 0; } +void btui_draw_linebox(btui_t *bt, int x, int y, int w, int h) +{ + btui_move_cursor(bt, x-1, y-1); + // Top row + fputs("\033(0l", bt->out); + for (int i = 0; i < w; i++) + fputc('q', bt->out); + fputc('k', bt->out); + // Side walls + for (int i = 0; i < h; i++) { + btui_move_cursor(bt, x-1, y + i); + fputc('x', bt->out); + btui_move_cursor(bt, x + w, y + i); + fputc('x', bt->out); + } + // Bottom row + btui_move_cursor(bt, x-1, y + h); + fputc('m', bt->out); + for (int i = 0; i < w; i++) + fputc('q', bt->out); + fputs("j\033(B", bt->out); +} + +void btui_fill_box(btui_t *bt, int x, int y, int w, int h) +{ + int left = x, bottom = y + h; + for ( ; y < bottom; y++) { + x = left; + btui_move_cursor(bt, x, y); + for ( ; x < left + w; x++) { + fputc(' ', bt->out); + } + } +} + +void btui_draw_shadow(btui_t *bt, int x, int y, int w, int h) +{ + fputs("\033(0", bt->out); + for (int i = 0; i < h-1; i++) { + btui_move_cursor(bt, x + w, y + 1 + i); + fputc('a', bt->out); + } + btui_move_cursor(bt, x + 1, y + h); + for (int i = 0; i < w; i++) { + fputc('a', bt->out); + } + fputs("\033(B", bt->out); +} + #endif // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1