From 2d0611adfee9acb79df43dba17d0500794fda692 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Sat, 3 Dec 2016 15:36:10 -0500 Subject: [PATCH 01/52] Fix point size in multi-color rect --- src/nuklear_love.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index f43a447..7df796d 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -299,9 +299,8 @@ static void nk_love_draw_rect_multi_color(int x, int y, unsigned int w, lua_pushnumber(L, 255); lua_pushnumber(L, 255); lua_call(L, 3, 0); - lua_getfield(L, -1, "setPointSize"); - lua_pushnumber(L, 0); + lua_pushnumber(L, 1); lua_call(L, 1, 0); struct nk_color X1, X2, Y; From 8c0a93e6c5c11043e460319638c7e1836626a38d Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Thu, 15 Dec 2016 08:52:32 -0500 Subject: [PATCH 02/52] Fix windowsIsHovered -> windowIsHovered (#7) --- src/nuklear_love.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 7df796d..d0f3b26 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -3356,7 +3356,7 @@ LUALIB_API int luaopen_nuklear(lua_State *L) NK_LOVE_REGISTER("window_is_hovered", nk_love_window_is_hovered); NK_LOVE_REGISTER("windowIsHovered", nk_love_window_is_hovered); NK_LOVE_REGISTER("window_is_any_hovered", nk_love_window_is_any_hovered); - NK_LOVE_REGISTER("windowsIsAnyHovered", nk_love_window_is_any_hovered); + NK_LOVE_REGISTER("windowIsAnyHovered", nk_love_window_is_any_hovered); NK_LOVE_REGISTER("item_is_any_active", nk_love_item_is_any_active); NK_LOVE_REGISTER("itemIsAnyActive", nk_love_item_is_any_active); NK_LOVE_REGISTER("window_set_bounds", nk_love_window_set_bounds); From 0b794f35381050bab00a020bd0e90795bd69d80b Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Thu, 15 Dec 2016 12:58:00 -0500 Subject: [PATCH 03/52] Increase style stack sizes (#6) --- src/nuklear_love.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index d0f3b26..bd046a1 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -1,6 +1,5 @@ /* - * Nuklear - v1.00 - public domain - * no warrenty implied; use at your own risk. + * LOVE-Nuklear - MIT Licensed; no warranty implied; use at your own risk. * authored from 2015-2016 by Micha Mettke * adapted to LOVE in 2016 by Kevin Harrison */ @@ -17,6 +16,13 @@ #define NK_INCLUDE_STANDARD_VARARGS #define NK_INCLUDE_DEFAULT_ALLOCATOR #define NK_PRIVATE +#define NK_BUTTON_BEHAVIOR_STACK_SIZE 32 +#define NK_FONT_STACK_SIZE 32 +#define NK_STYLE_ITEM_STACK_SIZE 256 +#define NK_FLOAT_STACK_SIZE 256 +#define NK_VECTOR_STACK_SIZE 128 +#define NK_FLAGS_STACK_SIZE 64 +#define NK_COLOR_STACK_SIZE 256 #include "nuklear/nuklear.h" /* From 47a7a6044d5a8df27c080fb87166f103267767b3 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Fri, 16 Dec 2016 21:59:22 -0500 Subject: [PATCH 04/52] Add drawing and input functions (#8) * nk.widgetHasMousePressed * nk.widgetHasMouseReleased * nk.widgetIsMousePressed * nk.widgetIsMouseReleased * nk.line * nk.curve * nk.polygon * nk.circle * nk.ellipse * nk.arc * nk.rectMultiColor * nk.scissor * nk.image * nk.text * nk.inputHasMousePressed * nk.inputHasMouseReleased * nk.inputIsMousePressed * nk.inputIsMouseReleased * nk.inputWasHovered * nk.inputIsHovered --- example/draw.lua | 21 +++ example/main.lua | 3 + src/nuklear_love.c | 426 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 444 insertions(+), 6 deletions(-) create mode 100644 example/draw.lua diff --git a/example/draw.lua b/example/draw.lua new file mode 100644 index 0000000..c7bab22 --- /dev/null +++ b/example/draw.lua @@ -0,0 +1,21 @@ +local nk = require "nuklear" + +local img = love.graphics.newImage 'skin/button.png' + +return function () + if nk.windowBegin('Draw Example', 300, 300, 200, 200, 'title', 'movable', 'border') then + local x, y, w, h = nk.windowGetBounds() + love.graphics.setColor(255, 0, 0) + nk.line(x + 10, y + 40, x + 50, y + 40, x + 50, y + 80) + nk.curve(x + 50, y + 80, x + 80, y + 40, x + 100, y + 80, x + 80, y + 80) + nk.polygon('line', x + 100, y + 150, x + 60, y + 140, x + 70, y + 70) + nk.circle('line', x + 130, y + 140, 50) + nk.ellipse('fill', x + 30, y + 150, 20, 40) + nk.arc('fill', x + 150, y + 80, 40, 3 * math.pi / 2, 2 * math.pi); + nk.rectMultiColor(x + 95, y + 50, 50, 50, '#ff0000', '#00ff00', '#0000ff', '#000000') + love.graphics.setColor(255, 255, 255) + nk.image(img, x + 120, y + 120, 70, 50) + nk.text('DRAW TEXT', x + 15, y + 75, 100, 100) + end + nk.windowEnd() +end diff --git a/example/main.lua b/example/main.lua index d24b48b..c6b72b6 100644 --- a/example/main.lua +++ b/example/main.lua @@ -3,6 +3,7 @@ local nk = require 'nuklear' local calculator = require 'calculator' +local draw = require 'draw' local overview = require 'overview' local style = require 'style' local skin = require 'skin' @@ -16,12 +17,14 @@ function love.update(dt) calculator() style() overview() + draw() skin() nk.frameEnd() end function love.draw() nk.draw() + love.graphics.print("Current FPS: "..tostring(love.timer.getFPS( )), 10, 10) end function love.keypressed(key, scancode, isrepeat) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index bd046a1..9949f88 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -1,5 +1,5 @@ /* - * LOVE-Nuklear - MIT Licensed; no warranty implied; use at your own risk. + * LOVE-Nuklear - MIT licensed; no warranty implied; use at your own risk. * authored from 2015-2016 by Micha Mettke * adapted to LOVE in 2016 by Kevin Harrison */ @@ -33,7 +33,7 @@ * =============================================================== */ -#define NK_LOVE_MAX_POINTS 256 +#define NK_LOVE_MAX_POINTS 1024 #define NK_LOVE_EDIT_BUFFER_LEN (1024 * 1024) #define NK_LOVE_COMBOBOX_MAX_ITEMS 1024 #define NK_LOVE_MAX_FONTS 1024 @@ -48,6 +48,7 @@ static const char **combobox_items; static struct nk_cursor cursors[NK_CURSOR_COUNT]; static float *layout_ratios; static int layout_ratio_count; +static float *points; static void nk_love_configureGraphics(int line_thickness, struct nk_color col) { @@ -67,6 +68,23 @@ static void nk_love_configureGraphics(int line_thickness, struct nk_color col) lua_call(L, 4, 0); } +static void nk_love_getGraphics(float *line_thickness, struct nk_color *color) +{ + lua_getglobal(L, "love"); + lua_getfield(L, -1, "graphics"); + lua_getfield(L, -1, "getLineWidth"); + lua_call(L, 0, 1); + *line_thickness = lua_tonumber(L, -1); + lua_pop(L, 1); + lua_getfield(L, -1, "getColor"); + lua_call(L, 0, 4); + color->r = lua_tointeger(L, -4); + color->g = lua_tointeger(L, -3); + color->b = lua_tointeger(L, -2); + color->a = lua_tointeger(L, -1); + lua_pop(L, 6); +} + static void nk_love_scissor(int x, int y, int w, int h) { lua_getglobal(L, "love"); @@ -211,7 +229,7 @@ static void nk_love_draw_curve(struct nk_vec2i p1, struct nk_vec2i p2, lua_pushnumber(L, x); lua_pushnumber(L, y); } - lua_call(L, (num_segments + 1) * 2, 0); + lua_call(L, num_segments * 2, 0); lua_pop(L, 1); } @@ -804,6 +822,8 @@ static int nk_love_init(lua_State *luaState) nk_love_assert(combobox_items != NULL, "nk.init: out of memory"); layout_ratios = malloc(sizeof(float) * NK_LOVE_MAX_RATIOS); nk_love_assert(layout_ratios != NULL, "nk.init: out of memory"); + points = malloc(sizeof(float) * NK_LOVE_MAX_POINTS * 2); + nk_love_assert(points != NULL, "nk.init: out of memory"); return 0; } @@ -822,6 +842,8 @@ static int nk_love_shutdown(lua_State *luaState) combobox_items = NULL; free(layout_ratios); layout_ratios = NULL; + free(points); + points = NULL; return 0; } @@ -1869,11 +1891,28 @@ static int nk_love_label(lua_State *L) static int nk_love_image(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.image: wrong number of arguments"); + int argc = lua_gettop(L); + nk_love_assert(argc == 1 || argc == 5, "nk.image: wrong number of arguments"); nk_love_assert_type(1, "Image", "nk.image: arg 1 should be an image"); struct nk_image image; + lua_pushvalue(L, 1); nk_love_toimage(&image); - nk_image(&context, image); + if (argc == 1) { + nk_image(&context, image); + } else { + nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.image: arg 2 should be a number"); + nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.image: arg 3 should be a number"); + nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.image: arg 4 should be a number"); + nk_love_assert(lua_type(L, 5) == LUA_TNUMBER, "nk.image: arg 5 should be a number"); + float x = lua_tonumber(L, 2); + float y = lua_tonumber(L, 3); + float w = lua_tonumber(L, 4); + float h = lua_tonumber(L, 5); + float line_thickness; + struct nk_color color; + nk_love_getGraphics(&line_thickness, &color); + nk_draw_image(&context.current->buffer, nk_rect(x, y, w, h), &image, color); + } return 0; } @@ -3277,7 +3316,8 @@ static int nk_love_widget_is_mouse_clicked(lua_State *L) nk_love_error("nk.widgetIsMouseClicked: arg 1 should be a button"); } } - int clicked = nk_widget_is_mouse_clicked(&context, button); + int clicked = (context.active == context.current) && + nk_input_is_mouse_pressed(&context.input, button); lua_pushboolean(L, clicked); return 1; } @@ -3304,6 +3344,59 @@ static int nk_love_widget_has_mouse_click(lua_State *L) return 1; } +#define NK_LOVE_WIDGET_HAS_MOUSE(name, down) \ + int argc = lua_gettop(L); \ + nk_love_assert(argc >= 0 && argc <= 1, name ": wrong number of arguments"); \ + enum nk_buttons button = NK_BUTTON_LEFT; \ + if (argc >= 1 && lua_type(L, 1) != LUA_TNIL) { \ + nk_love_assert(lua_type(L, 1) == LUA_TSTRING, name ": arg 1 should be a button"); \ + if (!nk_love_parse_button(lua_tostring(L, 1), &button)) { \ + nk_love_error(name ": arg 1 should be a button"); \ + } \ + } \ + int ret = nk_widget_has_mouse_click_down(&context, button, down); \ + lua_pushboolean(L, ret); \ + return 1 + +static int nk_love_widget_has_mouse_pressed(lua_State *L) +{ + NK_LOVE_WIDGET_HAS_MOUSE("nk.widgetHasMousePressed", nk_true); +} + +static int nk_love_widget_has_mouse_released(lua_State *L) +{ + NK_LOVE_WIDGET_HAS_MOUSE("nk.widgetHasMouseReleased", nk_false); +} + +#undef NK_LOVE_WIDGET_HAS_MOUSE + +#define NK_LOVE_WIDGET_IS_MOUSE(name, down) \ + int argc = lua_gettop(L); \ + nk_love_assert(argc >= 0 && argc <= 1, name ": wrong number of arguments"); \ + enum nk_buttons button = NK_BUTTON_LEFT; \ + if (argc >= 1 && lua_type(L, 1) != LUA_TNIL) { \ + nk_love_assert(lua_type(L, 1) == LUA_TSTRING, name ": arg 1 should be a button"); \ + if (!nk_love_parse_button(lua_tostring(L, 1), &button)) { \ + nk_love_error(name ": arg 1 should be a button"); \ + } \ + } \ + struct nk_rect bounds = nk_widget_bounds(&context); \ + int ret = nk_input_is_mouse_click_down_in_rect(&context.input, button, bounds, down); \ + lua_pushboolean(L, ret); \ + return 1 + +static int nk_love_widget_is_mouse_pressed(lua_State *L) +{ + NK_LOVE_WIDGET_IS_MOUSE("nk.widgetIsMousePressed", nk_true); +} + +static int nk_love_widget_is_mouse_released(lua_State *L) +{ + NK_LOVE_WIDGET_IS_MOUSE("nk.widgetIsMouseReleased", nk_false); +} + +#undef NK_LOVE_WIDGET_IS_MOUSE + static int nk_love_spacing(lua_State *L) { nk_love_assert(lua_gettop(L) == 1, "nk.spacing: wrong number of arguments"); @@ -3313,6 +3406,305 @@ static int nk_love_spacing(lua_State *L) return 0; } +static int nk_love_line(lua_State *L) +{ + int argc = lua_gettop(L); + nk_love_assert(argc >= 4 && argc % 2 == 0, "nk.line: wrong number of arguments"); + int i; + for (i = 0; i < argc; ++i) { + nk_love_assert(lua_type(L, i + 1) == LUA_TNUMBER, "nk.line: point coordinates should be numbers"); + points[i] = lua_tointeger(L, i + 1); + } + float line_thickness; + struct nk_color color; + nk_love_getGraphics(&line_thickness, &color); + nk_stroke_polyline(&context.current->buffer, points, argc / 2, line_thickness, color); + return 0; +} + +static int nk_love_curve(lua_State *L) +{ + nk_love_assert(lua_gettop(L) == 8, "nk.curve: wrong number of arguments"); + int i; + for (i = 1; i <= 8; ++i) { + nk_love_assert(lua_type(L, i) == LUA_TNUMBER, "nk.curve: point coordinates should be numbers"); + } + float ax = lua_tonumber(L, 1); + float ay = lua_tonumber(L, 2); + float ctrl0x = lua_tonumber(L, 3); + float ctrl0y = lua_tonumber(L, 4); + float ctrl1x = lua_tonumber(L, 5); + float ctrl1y = lua_tonumber(L, 6); + float bx = lua_tonumber(L, 7); + float by = lua_tonumber(L, 8); + float line_thickness; + struct nk_color color; + nk_love_getGraphics(&line_thickness, &color); + nk_stroke_curve(&context.current->buffer, ax, ay, ctrl0x, ctrl0y, ctrl1x, ctrl1y, bx, by, line_thickness, color); + return 0; +} + +static int nk_love_polygon(lua_State *L) +{ + int argc = lua_gettop(L); + nk_love_assert(argc >= 7 && argc % 2 == 1, "nk.polygon: wrong number of arguments"); + nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.polygon: arg 1 should be a draw mode"); + const char *mode = lua_tostring(L, 1); + int i; + for (i = 0; i < argc - 1; ++i) { + nk_love_assert(lua_type(L, i + 2) == LUA_TNUMBER, "nk.polygon: point coordinates should be numbers"); + points[i] = lua_tonumber(L, i + 2); + } + float line_thickness; + struct nk_color color; + nk_love_getGraphics(&line_thickness, &color); + if (!strcmp(mode, "fill")) { + nk_fill_polygon(&context.current->buffer, points, (argc - 1) / 2, color); + } else if (!strcmp(mode, "line")) { + nk_stroke_polygon(&context.current->buffer, points, (argc - 1) / 2, line_thickness, color); + } else { + nk_love_error("nk.polygon: arg 1 should be a draw mode"); + } + return 0; +} + +static int nk_love_circle(lua_State *L) +{ + nk_love_assert(lua_gettop(L) == 4, "nk.circle: wrong number of arguments"); + nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.circle: arg 1 should be a draw mode"); + nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.circle: arg 2 should be a number"); + nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.circle: arg 3 should be a number"); + nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.circle: arg 4 should be a number"); + const char *mode = lua_tostring(L, 1); + float x = lua_tonumber(L, 2); + float y = lua_tonumber(L, 3); + float r = lua_tonumber(L, 4); + float line_thickness; + struct nk_color color; + nk_love_getGraphics(&line_thickness, &color); + if (!strcmp(mode, "fill")) { + nk_fill_circle(&context.current->buffer, nk_rect(x - r, y - r, r * 2, r * 2), color); + } else if (!strcmp(mode, "line")) { + nk_stroke_circle(&context.current->buffer, nk_rect(x - r, y - r, r * 2, r * 2), line_thickness, color); + } else { + nk_love_error("nk.circle: arg 1 should be a draw mode"); + } + return 0; +} + +static int nk_love_ellipse(lua_State *L) +{ + nk_love_assert(lua_gettop(L) == 5, "nk.ellipse: wrong number of arguments"); + nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.ellipse: arg 1 should be a draw mode"); + nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.ellipse: arg 2 should be a number"); + nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.ellipse: arg 3 should be a number"); + nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.ellipse: arg 4 should be a number"); + nk_love_assert(lua_type(L, 5) == LUA_TNUMBER, "nk.ellipse: arg 5 should be a number"); + const char *mode = lua_tostring(L, 1); + float x = lua_tonumber(L, 2); + float y = lua_tonumber(L, 3); + float rx = lua_tonumber(L, 4); + float ry = lua_tonumber(L, 5); + float line_thickness; + struct nk_color color; + nk_love_getGraphics(&line_thickness, &color); + if (!strcmp(mode, "fill")) { + nk_fill_circle(&context.current->buffer, nk_rect(x - rx, y - ry, rx * 2, ry * 2), color); + } else if (!strcmp(mode, "line")) { + nk_stroke_circle(&context.current->buffer, nk_rect(x - rx, y - ry, rx * 2, ry * 2), line_thickness, color); + } else { + nk_love_error("nk.ellipse: arg 1 should be a draw mode"); + } + return 0; +} + +static int nk_love_arc(lua_State *L) +{ + nk_love_assert(lua_gettop(L) == 6, "nk.arc: wrong number of arguments"); + nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.arc: arg 1 should be a draw mode"); + nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.arc: arg 2 should be a number"); + nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.arc: arg 3 should be a number"); + nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.arc: arg 4 should be a number"); + nk_love_assert(lua_type(L, 5) == LUA_TNUMBER, "nk.arc: arg 5 should be a number"); + nk_love_assert(lua_type(L, 6) == LUA_TNUMBER, "nk.arc: arg 6 should be a number"); + const char *mode = lua_tostring(L, 1); + float cx = lua_tonumber(L, 2); + float cy = lua_tonumber(L, 3); + float r = lua_tonumber(L, 4); + float a0 = lua_tonumber(L, 5); + float a1 = lua_tonumber(L, 6); + float line_thickness; + struct nk_color color; + nk_love_getGraphics(&line_thickness, &color); + if (!strcmp(mode, "fill")) { + nk_fill_arc(&context.current->buffer, cx, cy, r, a0, a1, color); + } else if (!strcmp(mode, "line")) { + nk_stroke_arc(&context.current->buffer, cx, cy, r, a0, a1, line_thickness, color); + } else { + nk_love_error("nk.arc: arg 1 should be a draw mode"); + } + return 0; +} + +static int nk_love_rect_multi_color(lua_State *L) +{ + nk_love_assert(lua_gettop(L) == 8, "nk.rectMultiColor: wrong number of arguments"); + nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.rectMultiColor: arg 1 should be a number"); + nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.rectMultiColor: arg 2 should be a number"); + nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.rectMultiColor: arg 3 should be a number"); + nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.rectMultiColor: arg 4 should be a number"); + nk_love_assert_color(5, "nk.rectMultiColor: arg 5 should be a color string"); + nk_love_assert_color(6, "nk.rectMultiColor: arg 6 should be a color string"); + nk_love_assert_color(7, "nk.rectMultiColor: arg 7 should be a color string"); + nk_love_assert_color(8, "nk.rectMultiColor: arg 8 should be a color string"); + float x = lua_tonumber(L, 1); + float y = lua_tonumber(L, 2); + float w = lua_tonumber(L, 3); + float h = lua_tonumber(L, 4); + struct nk_color topLeft = nk_love_color_parse(lua_tostring(L, 5)); + struct nk_color topRight = nk_love_color_parse(lua_tostring(L, 6)); + struct nk_color bottomLeft = nk_love_color_parse(lua_tostring(L, 7)); + struct nk_color bottomRight = nk_love_color_parse(lua_tostring(L, 8)); + nk_fill_rect_multi_color(&context.current->buffer, nk_rect(x, y, w, h), topLeft, topRight, bottomLeft, bottomRight); + return 0; +} + +static int nk_love_push_scissor(lua_State *L) +{ + nk_love_assert(lua_gettop(L) == 4, "nk.scissor: wrong number of arguments"); + nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.scissor: arg 1 should be a number"); + nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.scissor: arg 2 should be a number"); + nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.scissor: arg 3 should be a number"); + nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.scissor: arg 4 should be a number"); + float x = lua_tonumber(L, 1); + float y = lua_tonumber(L, 2); + float w = lua_tonumber(L, 3); + float h = lua_tonumber(L, 4); + nk_push_scissor(&context.current->buffer, nk_rect(x, y, w, h)); + return 0; +} + +static int nk_love_text(lua_State *L) +{ + nk_love_assert(lua_gettop(L) == 5, "nk.text: wrong number of arguments"); + nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.text: arg 1 should be a string"); + nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.text: arg 2 should be a number"); + nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.text: arg 3 should be a number"); + nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.text: arg 4 should be a number"); + nk_love_assert(lua_type(L, 5) == LUA_TNUMBER, "nk.text: arg 5 should be a number"); + const char *text = lua_tostring(L, 1); + float x = lua_tonumber(L, 2); + float y = lua_tonumber(L, 3); + float w = lua_tonumber(L, 4); + float h = lua_tonumber(L, 5); + lua_getglobal(L, "love"); + lua_getfield(L, -1, "graphics"); + lua_getfield(L, -1, "getFont"); + lua_call(L, 0, 1); + nk_love_tofont(&fonts[font_count]); + float line_thickness; + struct nk_color color; + nk_love_getGraphics(&line_thickness, &color); + nk_draw_text(&context.current->buffer, nk_rect(x, y, w, h), text, strlen(text), &fonts[font_count++], nk_rgba(0, 0, 0, 0), color); + return 0; +} + +#define NK_LOVE_INPUT_HAS_MOUSE(name, down) \ + int argc = lua_gettop(L); \ + nk_love_assert(argc == 5, name ": wrong number of arguments"); \ + nk_love_assert(lua_type(L, 1) == LUA_TSTRING, name ": arg 1 should be a button"); \ + enum nk_buttons button; \ + if (!nk_love_parse_button(lua_tostring(L, 1), &button)) { \ + nk_love_error(name ": arg 1 should be a button"); \ + } \ + nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, name ": arg 2 should be a number"); \ + nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, name ": arg 3 should be a number"); \ + nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, name ": arg 4 should be a number"); \ + nk_love_assert(lua_type(L, 5) == LUA_TNUMBER, name ": arg 5 should be a number"); \ + float x = lua_tonumber(L, 2); \ + float y = lua_tonumber(L, 3); \ + float w = lua_tonumber(L, 4); \ + float h = lua_tonumber(L, 5); \ + int ret = nk_input_has_mouse_click_down_in_rect(&context.input, button, nk_rect(x, y, w, h), down); \ + lua_pushboolean(L, ret); \ + return 1 + +static int nk_love_input_has_mouse_pressed(lua_State *L) +{ + NK_LOVE_INPUT_HAS_MOUSE("nk.inputHasMousePressed", nk_true); +} + +static int nk_love_input_has_mouse_released(lua_State *L) +{ + NK_LOVE_INPUT_HAS_MOUSE("nk.inputHasMouseReleased", nk_false); +} + +#undef NK_LOVE_INPUT_HAS_MOUSE + +#define NK_LOVE_INPUT_IS_MOUSE(name, down) \ + int argc = lua_gettop(L); \ + nk_love_assert(argc == 5, name ": wrong number of arguments"); \ + enum nk_buttons button; \ + nk_love_assert(lua_type(L, 1) == LUA_TSTRING, name ": arg 1 should be a button"); \ + if (!nk_love_parse_button(lua_tostring(L, 1), &button)) { \ + nk_love_error(name ": arg 1 should be a button"); \ + } \ + nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, name ": arg 2 should be a number"); \ + nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, name ": arg 3 should be a number"); \ + nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, name ": arg 4 should be a number"); \ + nk_love_assert(lua_type(L, 5) == LUA_TNUMBER, name ": arg 5 should be a number"); \ + float x = lua_tonumber(L, 2); \ + float y = lua_tonumber(L, 3); \ + float w = lua_tonumber(L, 4); \ + float h = lua_tonumber(L, 5); \ + int ret = nk_input_is_mouse_click_down_in_rect(&context.input, button, nk_rect(x, y, w, h), down); \ + lua_pushboolean(L, ret); \ + return 1 + +static int nk_love_input_is_mouse_pressed(lua_State *L) +{ + NK_LOVE_INPUT_IS_MOUSE("nk.inputIsMousePressed", nk_true); +} + +static int nk_love_input_is_mouse_released(lua_State *L) +{ + NK_LOVE_INPUT_IS_MOUSE("nk.inputIsMouseReleased", nk_false); +} + +#undef NK_LOVE_INPUT_IS_MOUSE + +static int nk_love_input_was_hovered(lua_State *L) +{ + nk_love_assert(lua_gettop(L) == 4, "nk.inputWasHovered: wrong number of arguments"); + nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.inputWasHovered: arg 1 should be a number"); + nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.inputWasHovered: arg 2 should be a number"); + nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.inputWasHovered: arg 3 should be a number"); + nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.inputWasHovered: arg 4 should be a number"); + float x = lua_tonumber(L, 1); + float y = lua_tonumber(L, 2); + float w = lua_tonumber(L, 3); + float h = lua_tonumber(L, 4); + int was_hovered = nk_input_is_mouse_prev_hovering_rect(&context.input, nk_rect(x, y, w, h)); + lua_pushboolean(L, was_hovered); + return 1; +} + +static int nk_love_input_is_hovered(lua_State *L) +{ + nk_love_assert(lua_gettop(L) == 4, "nk.inputIsHovered: wrong number of arguments"); + nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.inputIsHovered: arg 1 should be a number"); + nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.inputIsHovered: arg 2 should be a number"); + nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.inputIsHovered: arg 3 should be a number"); + nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.inputIsHovered: arg 4 should be a number"); + float x = lua_tonumber(L, 1); + float y = lua_tonumber(L, 2); + float w = lua_tonumber(L, 3); + float h = lua_tonumber(L, 4); + int is_hovered = nk_input_is_mouse_hovering_rect(&context.input, nk_rect(x, y, w, h)); + lua_pushboolean(L, is_hovered); + return 1; +} + #define NK_LOVE_REGISTER(name, func) \ lua_pushcfunction(L, func); \ lua_setfield(L, -2, name) @@ -3516,8 +3908,30 @@ LUALIB_API int luaopen_nuklear(lua_State *L) NK_LOVE_REGISTER("widgetIsMouseClicked", nk_love_widget_is_mouse_clicked); NK_LOVE_REGISTER("widget_has_mouse_click", nk_love_widget_has_mouse_click); NK_LOVE_REGISTER("widgetHasMouseClick", nk_love_widget_has_mouse_click); + NK_LOVE_REGISTER("widgetHasMousePressed", nk_love_widget_has_mouse_pressed); + NK_LOVE_REGISTER("widgetHasMouseReleased", nk_love_widget_has_mouse_released); + NK_LOVE_REGISTER("widgetIsMousePressed", nk_love_widget_is_mouse_pressed); + NK_LOVE_REGISTER("widgetIsMouseReleased", nk_love_widget_is_mouse_released); NK_LOVE_REGISTER("spacing", nk_love_spacing); + NK_LOVE_REGISTER("line", nk_love_line); + NK_LOVE_REGISTER("curve", nk_love_curve); + NK_LOVE_REGISTER("polygon", nk_love_polygon); + NK_LOVE_REGISTER("circle", nk_love_circle); + NK_LOVE_REGISTER("ellipse", nk_love_ellipse); + NK_LOVE_REGISTER("arc", nk_love_arc); + NK_LOVE_REGISTER("rectMultiColor", nk_love_rect_multi_color); + NK_LOVE_REGISTER("scissor", nk_love_push_scissor); + /* image */ + NK_LOVE_REGISTER("text", nk_love_text); + + NK_LOVE_REGISTER("inputHasMousePressed", nk_love_input_has_mouse_pressed); + NK_LOVE_REGISTER("inputHasMouseReleased", nk_love_input_has_mouse_released); + NK_LOVE_REGISTER("inputIsMousePressed", nk_love_input_is_mouse_pressed); + NK_LOVE_REGISTER("inputIsMouseReleased", nk_love_input_is_mouse_released); + NK_LOVE_REGISTER("inputWasHovered", nk_love_input_was_hovered); + NK_LOVE_REGISTER("inputIsHovered", nk_love_input_is_hovered); + return 1; } From 1d86aa51f43789ca143c150552628573de660ef7 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Sat, 17 Dec 2016 10:37:35 -0500 Subject: [PATCH 05/52] Replace spaces with tabs --- src/nuklear_love.c | 380 ++++++++++++++++++++++----------------------- 1 file changed, 190 insertions(+), 190 deletions(-) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 9949f88..a527e4e 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -625,8 +625,8 @@ static void nk_love_assert_type(int index, const char *type, const char *message static void nk_love_assert_hex(char c, const char *message) { nk_love_assert((c >= '0' && c <= '9') - || (c >= 'a' && c <= 'f') - || (c >= 'A' && c <= 'F'), message); + || (c >= 'a' && c <= 'f') + || (c >= 'A' && c <= 'F'), message); } static void nk_love_assert_color(int index, const char *message) @@ -3706,213 +3706,213 @@ static int nk_love_input_is_hovered(lua_State *L) } #define NK_LOVE_REGISTER(name, func) \ - lua_pushcfunction(L, func); \ - lua_setfield(L, -2, name) + lua_pushcfunction(L, func); \ + lua_setfield(L, -2, name) LUALIB_API int luaopen_nuklear(lua_State *L) { lua_newtable(L); - NK_LOVE_REGISTER("init", nk_love_init); - NK_LOVE_REGISTER("shutdown", nk_love_shutdown); + NK_LOVE_REGISTER("init", nk_love_init); + NK_LOVE_REGISTER("shutdown", nk_love_shutdown); - NK_LOVE_REGISTER("keypressed", nk_love_keypressed); - NK_LOVE_REGISTER("keyreleased", nk_love_keyreleased); - NK_LOVE_REGISTER("mousepressed", nk_love_mousepressed); - NK_LOVE_REGISTER("mousereleased", nk_love_mousereleased); - NK_LOVE_REGISTER("mousemoved", nk_love_mousemoved); - NK_LOVE_REGISTER("textinput", nk_love_textinput); - NK_LOVE_REGISTER("wheelmoved", nk_love_wheelmoved); + NK_LOVE_REGISTER("keypressed", nk_love_keypressed); + NK_LOVE_REGISTER("keyreleased", nk_love_keyreleased); + NK_LOVE_REGISTER("mousepressed", nk_love_mousepressed); + NK_LOVE_REGISTER("mousereleased", nk_love_mousereleased); + NK_LOVE_REGISTER("mousemoved", nk_love_mousemoved); + NK_LOVE_REGISTER("textinput", nk_love_textinput); + NK_LOVE_REGISTER("wheelmoved", nk_love_wheelmoved); - NK_LOVE_REGISTER("draw", nk_love_draw); + NK_LOVE_REGISTER("draw", nk_love_draw); - NK_LOVE_REGISTER("frame_begin", nk_love_frame_begin); - NK_LOVE_REGISTER("frameBegin", nk_love_frame_begin); - NK_LOVE_REGISTER("frame_end", nk_love_frame_end); - NK_LOVE_REGISTER("frameEnd", nk_love_frame_end); + NK_LOVE_REGISTER("frame_begin", nk_love_frame_begin); + NK_LOVE_REGISTER("frameBegin", nk_love_frame_begin); + NK_LOVE_REGISTER("frame_end", nk_love_frame_end); + NK_LOVE_REGISTER("frameEnd", nk_love_frame_end); - NK_LOVE_REGISTER("window_begin", nk_love_window_begin); - NK_LOVE_REGISTER("windowBegin", nk_love_window_begin); - NK_LOVE_REGISTER("window_end", nk_love_window_end); - NK_LOVE_REGISTER("windowEnd", nk_love_window_end); - NK_LOVE_REGISTER("window_get_bounds", nk_love_window_get_bounds); - NK_LOVE_REGISTER("windowGetBounds", nk_love_window_get_bounds); - NK_LOVE_REGISTER("window_get_position", nk_love_window_get_position); - NK_LOVE_REGISTER("windowGetPosition", nk_love_window_get_position); - NK_LOVE_REGISTER("window_get_size", nk_love_window_get_size); - NK_LOVE_REGISTER("windowGetSize", nk_love_window_get_size); - NK_LOVE_REGISTER("window_get_content_region", nk_love_window_get_content_region); - NK_LOVE_REGISTER("windowGetContentRegion", nk_love_window_get_content_region); - NK_LOVE_REGISTER("window_has_focus", nk_love_window_has_focus); - NK_LOVE_REGISTER("windowHasFocus", nk_love_window_has_focus); - NK_LOVE_REGISTER("window_is_collapsed", nk_love_window_is_collapsed); - NK_LOVE_REGISTER("windowIsCollapsed", nk_love_window_is_collapsed); - NK_LOVE_REGISTER("window_is_hidden", nk_love_window_is_hidden); - NK_LOVE_REGISTER("windowIsHidden", nk_love_window_is_hidden); - NK_LOVE_REGISTER("window_is_active", nk_love_window_is_active); - NK_LOVE_REGISTER("windowIsActive", nk_love_window_is_active); - NK_LOVE_REGISTER("window_is_hovered", nk_love_window_is_hovered); - NK_LOVE_REGISTER("windowIsHovered", nk_love_window_is_hovered); - NK_LOVE_REGISTER("window_is_any_hovered", nk_love_window_is_any_hovered); - NK_LOVE_REGISTER("windowIsAnyHovered", nk_love_window_is_any_hovered); - NK_LOVE_REGISTER("item_is_any_active", nk_love_item_is_any_active); - NK_LOVE_REGISTER("itemIsAnyActive", nk_love_item_is_any_active); - NK_LOVE_REGISTER("window_set_bounds", nk_love_window_set_bounds); - NK_LOVE_REGISTER("windowSetBounds", nk_love_window_set_bounds); - NK_LOVE_REGISTER("window_set_position", nk_love_window_set_position); - NK_LOVE_REGISTER("windowSetPosition", nk_love_window_set_position); - NK_LOVE_REGISTER("window_set_size", nk_love_window_set_size); - NK_LOVE_REGISTER("windowSetSize", nk_love_window_set_size); - NK_LOVE_REGISTER("window_set_focus", nk_love_window_set_focus); - NK_LOVE_REGISTER("windowSetFocus", nk_love_window_set_focus); - NK_LOVE_REGISTER("window_close", nk_love_window_close); - NK_LOVE_REGISTER("windowClose", nk_love_window_close); - NK_LOVE_REGISTER("window_collapse", nk_love_window_collapse); - NK_LOVE_REGISTER("windowCollapse", nk_love_window_collapse); - NK_LOVE_REGISTER("window_expand", nk_love_window_expand); - NK_LOVE_REGISTER("windowExpand", nk_love_window_expand); - NK_LOVE_REGISTER("window_show", nk_love_window_show); - NK_LOVE_REGISTER("windowShow", nk_love_window_show); - NK_LOVE_REGISTER("window_hide", nk_love_window_hide); - NK_LOVE_REGISTER("windowHide", nk_love_window_hide); + NK_LOVE_REGISTER("window_begin", nk_love_window_begin); + NK_LOVE_REGISTER("windowBegin", nk_love_window_begin); + NK_LOVE_REGISTER("window_end", nk_love_window_end); + NK_LOVE_REGISTER("windowEnd", nk_love_window_end); + NK_LOVE_REGISTER("window_get_bounds", nk_love_window_get_bounds); + NK_LOVE_REGISTER("windowGetBounds", nk_love_window_get_bounds); + NK_LOVE_REGISTER("window_get_position", nk_love_window_get_position); + NK_LOVE_REGISTER("windowGetPosition", nk_love_window_get_position); + NK_LOVE_REGISTER("window_get_size", nk_love_window_get_size); + NK_LOVE_REGISTER("windowGetSize", nk_love_window_get_size); + NK_LOVE_REGISTER("window_get_content_region", nk_love_window_get_content_region); + NK_LOVE_REGISTER("windowGetContentRegion", nk_love_window_get_content_region); + NK_LOVE_REGISTER("window_has_focus", nk_love_window_has_focus); + NK_LOVE_REGISTER("windowHasFocus", nk_love_window_has_focus); + NK_LOVE_REGISTER("window_is_collapsed", nk_love_window_is_collapsed); + NK_LOVE_REGISTER("windowIsCollapsed", nk_love_window_is_collapsed); + NK_LOVE_REGISTER("window_is_hidden", nk_love_window_is_hidden); + NK_LOVE_REGISTER("windowIsHidden", nk_love_window_is_hidden); + NK_LOVE_REGISTER("window_is_active", nk_love_window_is_active); + NK_LOVE_REGISTER("windowIsActive", nk_love_window_is_active); + NK_LOVE_REGISTER("window_is_hovered", nk_love_window_is_hovered); + NK_LOVE_REGISTER("windowIsHovered", nk_love_window_is_hovered); + NK_LOVE_REGISTER("window_is_any_hovered", nk_love_window_is_any_hovered); + NK_LOVE_REGISTER("windowIsAnyHovered", nk_love_window_is_any_hovered); + NK_LOVE_REGISTER("item_is_any_active", nk_love_item_is_any_active); + NK_LOVE_REGISTER("itemIsAnyActive", nk_love_item_is_any_active); + NK_LOVE_REGISTER("window_set_bounds", nk_love_window_set_bounds); + NK_LOVE_REGISTER("windowSetBounds", nk_love_window_set_bounds); + NK_LOVE_REGISTER("window_set_position", nk_love_window_set_position); + NK_LOVE_REGISTER("windowSetPosition", nk_love_window_set_position); + NK_LOVE_REGISTER("window_set_size", nk_love_window_set_size); + NK_LOVE_REGISTER("windowSetSize", nk_love_window_set_size); + NK_LOVE_REGISTER("window_set_focus", nk_love_window_set_focus); + NK_LOVE_REGISTER("windowSetFocus", nk_love_window_set_focus); + NK_LOVE_REGISTER("window_close", nk_love_window_close); + NK_LOVE_REGISTER("windowClose", nk_love_window_close); + NK_LOVE_REGISTER("window_collapse", nk_love_window_collapse); + NK_LOVE_REGISTER("windowCollapse", nk_love_window_collapse); + NK_LOVE_REGISTER("window_expand", nk_love_window_expand); + NK_LOVE_REGISTER("windowExpand", nk_love_window_expand); + NK_LOVE_REGISTER("window_show", nk_love_window_show); + NK_LOVE_REGISTER("windowShow", nk_love_window_show); + NK_LOVE_REGISTER("window_hide", nk_love_window_hide); + NK_LOVE_REGISTER("windowHide", nk_love_window_hide); - NK_LOVE_REGISTER("layout_row", nk_love_layout_row); - NK_LOVE_REGISTER("layoutRow", nk_love_layout_row); - NK_LOVE_REGISTER("layout_row_begin", nk_love_layout_row_begin); - NK_LOVE_REGISTER("layoutRowBegin", nk_love_layout_row_begin); - NK_LOVE_REGISTER("layout_row_push", nk_love_layout_row_push); - NK_LOVE_REGISTER("layoutRowPush", nk_love_layout_row_push); - NK_LOVE_REGISTER("layout_row_end", nk_love_layout_row_end); - NK_LOVE_REGISTER("layoutRowEnd", nk_love_layout_row_end); - NK_LOVE_REGISTER("layout_space_begin", nk_love_layout_space_begin); - NK_LOVE_REGISTER("layoutSpaceBegin", nk_love_layout_space_begin); - NK_LOVE_REGISTER("layout_space_push", nk_love_layout_space_push); - NK_LOVE_REGISTER("layoutSpacePush", nk_love_layout_space_push); - NK_LOVE_REGISTER("layout_space_end", nk_love_layout_space_end); - NK_LOVE_REGISTER("layoutSpaceEnd", nk_love_layout_space_end); - NK_LOVE_REGISTER("layout_space_bounds", nk_love_layout_space_bounds); - NK_LOVE_REGISTER("layoutSpaceBounds", nk_love_layout_space_bounds); - NK_LOVE_REGISTER("layout_space_to_screen", nk_love_layout_space_to_screen); - NK_LOVE_REGISTER("layoutSpaceToScreen", nk_love_layout_space_to_screen); - NK_LOVE_REGISTER("layout_space_to_local", nk_love_layout_space_to_local); - NK_LOVE_REGISTER("layoutSpaceToLocal", nk_love_layout_space_to_local); - NK_LOVE_REGISTER("layout_space_rect_to_screen", nk_love_layout_space_rect_to_screen); - NK_LOVE_REGISTER("layoutSpaceRectToScreen", nk_love_layout_space_rect_to_screen); - NK_LOVE_REGISTER("layout_space_rect_to_local", nk_love_layout_space_rect_to_local); - NK_LOVE_REGISTER("layoutSpaceRectToLocal", nk_love_layout_space_rect_to_local); - NK_LOVE_REGISTER("layout_ratio_from_pixel", nk_love_layout_ratio_from_pixel); - NK_LOVE_REGISTER("layoutRatioFromPixel", nk_love_layout_ratio_from_pixel); + NK_LOVE_REGISTER("layout_row", nk_love_layout_row); + NK_LOVE_REGISTER("layoutRow", nk_love_layout_row); + NK_LOVE_REGISTER("layout_row_begin", nk_love_layout_row_begin); + NK_LOVE_REGISTER("layoutRowBegin", nk_love_layout_row_begin); + NK_LOVE_REGISTER("layout_row_push", nk_love_layout_row_push); + NK_LOVE_REGISTER("layoutRowPush", nk_love_layout_row_push); + NK_LOVE_REGISTER("layout_row_end", nk_love_layout_row_end); + NK_LOVE_REGISTER("layoutRowEnd", nk_love_layout_row_end); + NK_LOVE_REGISTER("layout_space_begin", nk_love_layout_space_begin); + NK_LOVE_REGISTER("layoutSpaceBegin", nk_love_layout_space_begin); + NK_LOVE_REGISTER("layout_space_push", nk_love_layout_space_push); + NK_LOVE_REGISTER("layoutSpacePush", nk_love_layout_space_push); + NK_LOVE_REGISTER("layout_space_end", nk_love_layout_space_end); + NK_LOVE_REGISTER("layoutSpaceEnd", nk_love_layout_space_end); + NK_LOVE_REGISTER("layout_space_bounds", nk_love_layout_space_bounds); + NK_LOVE_REGISTER("layoutSpaceBounds", nk_love_layout_space_bounds); + NK_LOVE_REGISTER("layout_space_to_screen", nk_love_layout_space_to_screen); + NK_LOVE_REGISTER("layoutSpaceToScreen", nk_love_layout_space_to_screen); + NK_LOVE_REGISTER("layout_space_to_local", nk_love_layout_space_to_local); + NK_LOVE_REGISTER("layoutSpaceToLocal", nk_love_layout_space_to_local); + NK_LOVE_REGISTER("layout_space_rect_to_screen", nk_love_layout_space_rect_to_screen); + NK_LOVE_REGISTER("layoutSpaceRectToScreen", nk_love_layout_space_rect_to_screen); + NK_LOVE_REGISTER("layout_space_rect_to_local", nk_love_layout_space_rect_to_local); + NK_LOVE_REGISTER("layoutSpaceRectToLocal", nk_love_layout_space_rect_to_local); + NK_LOVE_REGISTER("layout_ratio_from_pixel", nk_love_layout_ratio_from_pixel); + NK_LOVE_REGISTER("layoutRatioFromPixel", nk_love_layout_ratio_from_pixel); - NK_LOVE_REGISTER("group_begin", nk_love_group_begin); - NK_LOVE_REGISTER("groupBegin", nk_love_group_begin); - NK_LOVE_REGISTER("group_end", nk_love_group_end); - NK_LOVE_REGISTER("groupEnd", nk_love_group_end); + NK_LOVE_REGISTER("group_begin", nk_love_group_begin); + NK_LOVE_REGISTER("groupBegin", nk_love_group_begin); + NK_LOVE_REGISTER("group_end", nk_love_group_end); + NK_LOVE_REGISTER("groupEnd", nk_love_group_end); - NK_LOVE_REGISTER("tree_push", nk_love_tree_push); - NK_LOVE_REGISTER("treePush", nk_love_tree_push); - NK_LOVE_REGISTER("tree_pop", nk_love_tree_pop); - NK_LOVE_REGISTER("treePop", nk_love_tree_pop); + NK_LOVE_REGISTER("tree_push", nk_love_tree_push); + NK_LOVE_REGISTER("treePush", nk_love_tree_push); + NK_LOVE_REGISTER("tree_pop", nk_love_tree_pop); + NK_LOVE_REGISTER("treePop", nk_love_tree_pop); - NK_LOVE_REGISTER("color_rgba", nk_love_color_rgba); - NK_LOVE_REGISTER("colorRGBA", nk_love_color_rgba); - NK_LOVE_REGISTER("color_hsva", nk_love_color_hsva); - NK_LOVE_REGISTER("colorHSVA", nk_love_color_hsva); - NK_LOVE_REGISTER("color_parse_rgba", nk_love_color_parse_rgba); - NK_LOVE_REGISTER("colorParseRGBA", nk_love_color_parse_rgba); - NK_LOVE_REGISTER("color_parse_hsva", nk_love_color_parse_hsva); - NK_LOVE_REGISTER("colorParseHSVA", nk_love_color_parse_hsva); + NK_LOVE_REGISTER("color_rgba", nk_love_color_rgba); + NK_LOVE_REGISTER("colorRGBA", nk_love_color_rgba); + NK_LOVE_REGISTER("color_hsva", nk_love_color_hsva); + NK_LOVE_REGISTER("colorHSVA", nk_love_color_hsva); + NK_LOVE_REGISTER("color_parse_rgba", nk_love_color_parse_rgba); + NK_LOVE_REGISTER("colorParseRGBA", nk_love_color_parse_rgba); + NK_LOVE_REGISTER("color_parse_hsva", nk_love_color_parse_hsva); + NK_LOVE_REGISTER("colorParseHSVA", nk_love_color_parse_hsva); - NK_LOVE_REGISTER("label", nk_love_label); - NK_LOVE_REGISTER("image", nk_love_image); - NK_LOVE_REGISTER("button", nk_love_button); - NK_LOVE_REGISTER("button_set_behavior", nk_love_button_set_behavior); - NK_LOVE_REGISTER("buttonSetBehavior", nk_love_button_set_behavior); - NK_LOVE_REGISTER("button_push_behavior", nk_love_button_push_behavior); - NK_LOVE_REGISTER("buttonPushBehavior", nk_love_button_push_behavior); - NK_LOVE_REGISTER("button_pop_behavior", nk_love_button_pop_behavior); - NK_LOVE_REGISTER("buttonPopBehavior", nk_love_button_pop_behavior); - NK_LOVE_REGISTER("checkbox", nk_love_checkbox); - NK_LOVE_REGISTER("radio", nk_love_radio); - NK_LOVE_REGISTER("selectable", nk_love_selectable); - NK_LOVE_REGISTER("slider", nk_love_slider); - NK_LOVE_REGISTER("progress", nk_love_progress); - NK_LOVE_REGISTER("color_picker", nk_love_color_picker); - NK_LOVE_REGISTER("colorPicker", nk_love_color_picker); - NK_LOVE_REGISTER("property", nk_love_property); - NK_LOVE_REGISTER("edit", nk_love_edit); - NK_LOVE_REGISTER("popup_begin", nk_love_popup_begin); - NK_LOVE_REGISTER("popupBegin", nk_love_popup_begin); - NK_LOVE_REGISTER("popup_close", nk_love_popup_close); - NK_LOVE_REGISTER("popupClose", nk_love_popup_close); - NK_LOVE_REGISTER("popup_end", nk_love_popup_end); - NK_LOVE_REGISTER("popupEnd", nk_love_popup_end); - NK_LOVE_REGISTER("combobox", nk_love_combobox); - NK_LOVE_REGISTER("combobox_begin", nk_love_combobox_begin); - NK_LOVE_REGISTER("comboboxBegin", nk_love_combobox_begin); - NK_LOVE_REGISTER("combobox_item", nk_love_combobox_item); - NK_LOVE_REGISTER("comboboxItem", nk_love_combobox_item); - NK_LOVE_REGISTER("combobox_close", nk_love_combobox_close); - NK_LOVE_REGISTER("comboboxClose", nk_love_combobox_close); - NK_LOVE_REGISTER("combobox_end", nk_love_combobox_end); - NK_LOVE_REGISTER("comboboxEnd", nk_love_combobox_end); - NK_LOVE_REGISTER("contextual_begin", nk_love_contextual_begin); - NK_LOVE_REGISTER("contextualBegin", nk_love_contextual_begin); - NK_LOVE_REGISTER("contextual_item", nk_love_contextual_item); - NK_LOVE_REGISTER("contextualItem", nk_love_contextual_item); - NK_LOVE_REGISTER("contextual_close", nk_love_contextual_close); - NK_LOVE_REGISTER("contextualClose", nk_love_contextual_close); - NK_LOVE_REGISTER("contextual_end", nk_love_contextual_end); - NK_LOVE_REGISTER("contextualEnd", nk_love_contextual_end); - NK_LOVE_REGISTER("tooltip", nk_love_tooltip); - NK_LOVE_REGISTER("tooltip_begin", nk_love_tooltip_begin); - NK_LOVE_REGISTER("tooltipBegin", nk_love_tooltip_begin); - NK_LOVE_REGISTER("tooltip_end", nk_love_tooltip_end); - NK_LOVE_REGISTER("tooltipEnd", nk_love_tooltip_end); - NK_LOVE_REGISTER("menubar_begin", nk_love_menubar_begin); - NK_LOVE_REGISTER("menubarBegin", nk_love_menubar_begin); - NK_LOVE_REGISTER("menubar_end", nk_love_menubar_end); - NK_LOVE_REGISTER("menubarEnd", nk_love_menubar_end); - NK_LOVE_REGISTER("menu_begin", nk_love_menu_begin); - NK_LOVE_REGISTER("menuBegin", nk_love_menu_begin); - NK_LOVE_REGISTER("menu_item", nk_love_menu_item); - NK_LOVE_REGISTER("menuItem", nk_love_menu_item); - NK_LOVE_REGISTER("menu_close", nk_love_menu_close); - NK_LOVE_REGISTER("menuClose", nk_love_menu_close); - NK_LOVE_REGISTER("menu_end", nk_love_menu_end); - NK_LOVE_REGISTER("menuEnd", nk_love_menu_end); + NK_LOVE_REGISTER("label", nk_love_label); + NK_LOVE_REGISTER("image", nk_love_image); + NK_LOVE_REGISTER("button", nk_love_button); + NK_LOVE_REGISTER("button_set_behavior", nk_love_button_set_behavior); + NK_LOVE_REGISTER("buttonSetBehavior", nk_love_button_set_behavior); + NK_LOVE_REGISTER("button_push_behavior", nk_love_button_push_behavior); + NK_LOVE_REGISTER("buttonPushBehavior", nk_love_button_push_behavior); + NK_LOVE_REGISTER("button_pop_behavior", nk_love_button_pop_behavior); + NK_LOVE_REGISTER("buttonPopBehavior", nk_love_button_pop_behavior); + NK_LOVE_REGISTER("checkbox", nk_love_checkbox); + NK_LOVE_REGISTER("radio", nk_love_radio); + NK_LOVE_REGISTER("selectable", nk_love_selectable); + NK_LOVE_REGISTER("slider", nk_love_slider); + NK_LOVE_REGISTER("progress", nk_love_progress); + NK_LOVE_REGISTER("color_picker", nk_love_color_picker); + NK_LOVE_REGISTER("colorPicker", nk_love_color_picker); + NK_LOVE_REGISTER("property", nk_love_property); + NK_LOVE_REGISTER("edit", nk_love_edit); + NK_LOVE_REGISTER("popup_begin", nk_love_popup_begin); + NK_LOVE_REGISTER("popupBegin", nk_love_popup_begin); + NK_LOVE_REGISTER("popup_close", nk_love_popup_close); + NK_LOVE_REGISTER("popupClose", nk_love_popup_close); + NK_LOVE_REGISTER("popup_end", nk_love_popup_end); + NK_LOVE_REGISTER("popupEnd", nk_love_popup_end); + NK_LOVE_REGISTER("combobox", nk_love_combobox); + NK_LOVE_REGISTER("combobox_begin", nk_love_combobox_begin); + NK_LOVE_REGISTER("comboboxBegin", nk_love_combobox_begin); + NK_LOVE_REGISTER("combobox_item", nk_love_combobox_item); + NK_LOVE_REGISTER("comboboxItem", nk_love_combobox_item); + NK_LOVE_REGISTER("combobox_close", nk_love_combobox_close); + NK_LOVE_REGISTER("comboboxClose", nk_love_combobox_close); + NK_LOVE_REGISTER("combobox_end", nk_love_combobox_end); + NK_LOVE_REGISTER("comboboxEnd", nk_love_combobox_end); + NK_LOVE_REGISTER("contextual_begin", nk_love_contextual_begin); + NK_LOVE_REGISTER("contextualBegin", nk_love_contextual_begin); + NK_LOVE_REGISTER("contextual_item", nk_love_contextual_item); + NK_LOVE_REGISTER("contextualItem", nk_love_contextual_item); + NK_LOVE_REGISTER("contextual_close", nk_love_contextual_close); + NK_LOVE_REGISTER("contextualClose", nk_love_contextual_close); + NK_LOVE_REGISTER("contextual_end", nk_love_contextual_end); + NK_LOVE_REGISTER("contextualEnd", nk_love_contextual_end); + NK_LOVE_REGISTER("tooltip", nk_love_tooltip); + NK_LOVE_REGISTER("tooltip_begin", nk_love_tooltip_begin); + NK_LOVE_REGISTER("tooltipBegin", nk_love_tooltip_begin); + NK_LOVE_REGISTER("tooltip_end", nk_love_tooltip_end); + NK_LOVE_REGISTER("tooltipEnd", nk_love_tooltip_end); + NK_LOVE_REGISTER("menubar_begin", nk_love_menubar_begin); + NK_LOVE_REGISTER("menubarBegin", nk_love_menubar_begin); + NK_LOVE_REGISTER("menubar_end", nk_love_menubar_end); + NK_LOVE_REGISTER("menubarEnd", nk_love_menubar_end); + NK_LOVE_REGISTER("menu_begin", nk_love_menu_begin); + NK_LOVE_REGISTER("menuBegin", nk_love_menu_begin); + NK_LOVE_REGISTER("menu_item", nk_love_menu_item); + NK_LOVE_REGISTER("menuItem", nk_love_menu_item); + NK_LOVE_REGISTER("menu_close", nk_love_menu_close); + NK_LOVE_REGISTER("menuClose", nk_love_menu_close); + NK_LOVE_REGISTER("menu_end", nk_love_menu_end); + NK_LOVE_REGISTER("menuEnd", nk_love_menu_end); - NK_LOVE_REGISTER("style_default", nk_love_style_default); - NK_LOVE_REGISTER("styleDefault", nk_love_style_default); - NK_LOVE_REGISTER("style_load_colors", nk_love_style_load_colors); - NK_LOVE_REGISTER("styleLoadColors", nk_love_style_load_colors); - NK_LOVE_REGISTER("style_set_font", nk_love_style_set_font); - NK_LOVE_REGISTER("styleSetFont", nk_love_style_set_font); - NK_LOVE_REGISTER("style_push", nk_love_style_push); - NK_LOVE_REGISTER("stylePush", nk_love_style_push); - NK_LOVE_REGISTER("style_pop", nk_love_style_pop); - NK_LOVE_REGISTER("stylePop", nk_love_style_pop); + NK_LOVE_REGISTER("style_default", nk_love_style_default); + NK_LOVE_REGISTER("styleDefault", nk_love_style_default); + NK_LOVE_REGISTER("style_load_colors", nk_love_style_load_colors); + NK_LOVE_REGISTER("styleLoadColors", nk_love_style_load_colors); + NK_LOVE_REGISTER("style_set_font", nk_love_style_set_font); + NK_LOVE_REGISTER("styleSetFont", nk_love_style_set_font); + NK_LOVE_REGISTER("style_push", nk_love_style_push); + NK_LOVE_REGISTER("stylePush", nk_love_style_push); + NK_LOVE_REGISTER("style_pop", nk_love_style_pop); + NK_LOVE_REGISTER("stylePop", nk_love_style_pop); - NK_LOVE_REGISTER("widget_bounds", nk_love_widget_bounds); - NK_LOVE_REGISTER("widgetBounds", nk_love_widget_bounds); - NK_LOVE_REGISTER("widget_position", nk_love_widget_position); - NK_LOVE_REGISTER("widgetPosition", nk_love_widget_position); - NK_LOVE_REGISTER("widget_size", nk_love_widget_size); - NK_LOVE_REGISTER("widgetSize", nk_love_widget_size); - NK_LOVE_REGISTER("widget_width", nk_love_widget_width); - NK_LOVE_REGISTER("widgetWidth", nk_love_widget_width); - NK_LOVE_REGISTER("widget_height", nk_love_widget_height); - NK_LOVE_REGISTER("widgetHeight", nk_love_widget_height); - NK_LOVE_REGISTER("widget_is_hovered", nk_love_widget_is_hovered); - NK_LOVE_REGISTER("widgetIsHovered", nk_love_widget_is_hovered); - NK_LOVE_REGISTER("widget_is_mouse_clicked", nk_love_widget_is_mouse_clicked); - NK_LOVE_REGISTER("widgetIsMouseClicked", nk_love_widget_is_mouse_clicked); - NK_LOVE_REGISTER("widget_has_mouse_click", nk_love_widget_has_mouse_click); - NK_LOVE_REGISTER("widgetHasMouseClick", nk_love_widget_has_mouse_click); + NK_LOVE_REGISTER("widget_bounds", nk_love_widget_bounds); + NK_LOVE_REGISTER("widgetBounds", nk_love_widget_bounds); + NK_LOVE_REGISTER("widget_position", nk_love_widget_position); + NK_LOVE_REGISTER("widgetPosition", nk_love_widget_position); + NK_LOVE_REGISTER("widget_size", nk_love_widget_size); + NK_LOVE_REGISTER("widgetSize", nk_love_widget_size); + NK_LOVE_REGISTER("widget_width", nk_love_widget_width); + NK_LOVE_REGISTER("widgetWidth", nk_love_widget_width); + NK_LOVE_REGISTER("widget_height", nk_love_widget_height); + NK_LOVE_REGISTER("widgetHeight", nk_love_widget_height); + NK_LOVE_REGISTER("widget_is_hovered", nk_love_widget_is_hovered); + NK_LOVE_REGISTER("widgetIsHovered", nk_love_widget_is_hovered); + NK_LOVE_REGISTER("widget_is_mouse_clicked", nk_love_widget_is_mouse_clicked); + NK_LOVE_REGISTER("widgetIsMouseClicked", nk_love_widget_is_mouse_clicked); + NK_LOVE_REGISTER("widget_has_mouse_click", nk_love_widget_has_mouse_click); + NK_LOVE_REGISTER("widgetHasMouseClick", nk_love_widget_has_mouse_click); NK_LOVE_REGISTER("widgetHasMousePressed", nk_love_widget_has_mouse_pressed); NK_LOVE_REGISTER("widgetHasMouseReleased", nk_love_widget_has_mouse_released); NK_LOVE_REGISTER("widgetIsMousePressed", nk_love_widget_is_mouse_pressed); NK_LOVE_REGISTER("widgetIsMouseReleased", nk_love_widget_is_mouse_released); - NK_LOVE_REGISTER("spacing", nk_love_spacing); + NK_LOVE_REGISTER("spacing", nk_love_spacing); NK_LOVE_REGISTER("line", nk_love_line); NK_LOVE_REGISTER("curve", nk_love_curve); @@ -3932,7 +3932,7 @@ LUALIB_API int luaopen_nuklear(lua_State *L) NK_LOVE_REGISTER("inputWasHovered", nk_love_input_was_hovered); NK_LOVE_REGISTER("inputIsHovered", nk_love_input_is_hovered); - return 1; + return 1; } #undef NK_LOVE_REGISTER From 8aa93de8036f21b27491c45c7aeb66b052781df4 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Sun, 18 Dec 2016 20:08:51 -0500 Subject: [PATCH 06/52] Replace `layout_ratios` and `points` with single `floats` array --- src/nuklear_love.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index a527e4e..8b31f92 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -46,9 +46,8 @@ static int font_count; static char *edit_buffer; static const char **combobox_items; static struct nk_cursor cursors[NK_CURSOR_COUNT]; -static float *layout_ratios; +static float *floats; static int layout_ratio_count; -static float *points; static void nk_love_configureGraphics(int line_thickness, struct nk_color col) { @@ -820,10 +819,8 @@ static int nk_love_init(lua_State *luaState) nk_love_assert(edit_buffer != NULL, "nk.init: out of memory"); combobox_items = malloc(sizeof(char*) * NK_LOVE_COMBOBOX_MAX_ITEMS); nk_love_assert(combobox_items != NULL, "nk.init: out of memory"); - layout_ratios = malloc(sizeof(float) * NK_LOVE_MAX_RATIOS); - nk_love_assert(layout_ratios != NULL, "nk.init: out of memory"); - points = malloc(sizeof(float) * NK_LOVE_MAX_POINTS * 2); - nk_love_assert(points != NULL, "nk.init: out of memory"); + floats = malloc(sizeof(float) * NK_MAX(NK_LOVE_MAX_RATIOS, NK_LOVE_MAX_POINTS * 2)); + nk_love_assert(floats != NULL, "nk.init: out of memory"); return 0; } @@ -840,10 +837,8 @@ static int nk_love_shutdown(lua_State *luaState) edit_buffer = NULL; free(combobox_items); combobox_items = NULL; - free(layout_ratios); - layout_ratios = NULL; - free(points); - points = NULL; + free(floats); + floats = NULL; return 0; } @@ -1491,10 +1486,10 @@ static int nk_love_layout_row(lua_State *L) int i, j; for (i = 1, j = layout_ratio_count; i <= cols && j < NK_LOVE_MAX_RATIOS; ++i, ++j) { lua_rawgeti(L, -1, i); - layout_ratios[j] = lua_tonumber(L, -1); + floats[j] = lua_tonumber(L, -1); lua_pop(L, 1); } - nk_layout_row(&context, format, height, cols, layout_ratios + layout_ratio_count); + nk_layout_row(&context, format, height, cols, floats + layout_ratio_count); layout_ratio_count += cols; } return 0; @@ -3413,12 +3408,12 @@ static int nk_love_line(lua_State *L) int i; for (i = 0; i < argc; ++i) { nk_love_assert(lua_type(L, i + 1) == LUA_TNUMBER, "nk.line: point coordinates should be numbers"); - points[i] = lua_tointeger(L, i + 1); + floats[i] = lua_tonumber(L, i + 1); } float line_thickness; struct nk_color color; nk_love_getGraphics(&line_thickness, &color); - nk_stroke_polyline(&context.current->buffer, points, argc / 2, line_thickness, color); + nk_stroke_polyline(&context.current->buffer, floats, argc / 2, line_thickness, color); return 0; } @@ -3453,15 +3448,15 @@ static int nk_love_polygon(lua_State *L) int i; for (i = 0; i < argc - 1; ++i) { nk_love_assert(lua_type(L, i + 2) == LUA_TNUMBER, "nk.polygon: point coordinates should be numbers"); - points[i] = lua_tonumber(L, i + 2); + floats[i] = lua_tonumber(L, i + 2); } float line_thickness; struct nk_color color; nk_love_getGraphics(&line_thickness, &color); if (!strcmp(mode, "fill")) { - nk_fill_polygon(&context.current->buffer, points, (argc - 1) / 2, color); + nk_fill_polygon(&context.current->buffer, floats, (argc - 1) / 2, color); } else if (!strcmp(mode, "line")) { - nk_stroke_polygon(&context.current->buffer, points, (argc - 1) / 2, line_thickness, color); + nk_stroke_polygon(&context.current->buffer, floats, (argc - 1) / 2, line_thickness, color); } else { nk_love_error("nk.polygon: arg 1 should be a draw mode"); } From c2fefb0601244445baaaa386849e812b78d3a7a7 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Mon, 19 Dec 2016 12:50:04 -0500 Subject: [PATCH 07/52] Improve error handling --- src/nuklear_love.c | 2091 ++++++++++++++++++++------------------------ 1 file changed, 930 insertions(+), 1161 deletions(-) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 8b31f92..7ee6353 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -590,58 +590,118 @@ static int nk_love_wheelmoved_event(int x, int y) * =============================================================== */ -static void nk_love_error(const char *msg) +static int nk_love_is_type(int index, const char *type) { - lua_getglobal(L, "error"); - lua_pushstring(L, msg); - lua_call(L, 1, 0); -} - -static void nk_love_assert(int pass, const char *msg) -{ - if (!pass) { - nk_love_error(msg); - } -} - -static void nk_love_assert_type(int index, const char *type, const char *message) -{ - if (index < 0) { + if (index < 0) index += lua_gettop(L) + 1; - } - nk_love_assert(lua_type(L, index) == LUA_TUSERDATA, message); - lua_getfield(L, index, "typeOf"); - nk_love_assert(lua_type(L, -1) == LUA_TFUNCTION, message); - lua_pushvalue(L, index); - lua_pushstring(L, type); - lua_call(L, 2, 1); - nk_love_assert(lua_type(L, -1) == LUA_TBOOLEAN, message); - int is_type = lua_toboolean(L, -1); - nk_love_assert(is_type, message); - lua_pop(L, 1); -} - -static void nk_love_assert_hex(char c, const char *message) -{ - nk_love_assert((c >= '0' && c <= '9') - || (c >= 'a' && c <= 'f') - || (c >= 'A' && c <= 'F'), message); -} - -static void nk_love_assert_color(int index, const char *message) -{ - nk_love_assert(lua_type(L, index) == LUA_TSTRING, message); - const char *color_string = lua_tostring(L, index); - size_t len = strlen(color_string); - if (len == 7 || len == 9) { - nk_love_assert(color_string[0] == '#', message); - int i; - for (i = 1; i < len; ++i) { - nk_love_assert_hex(color_string[i], message); + if (lua_isuserdata(L, index)) { + lua_getfield(L, index, "typeOf"); + if (lua_isfunction(L, -1)) { + lua_pushvalue(L, index); + lua_pushstring(L, type); + lua_call(L, 2, 1); + if (lua_isboolean(L, -1)) { + int is_type = lua_toboolean(L, -1); + lua_pop(L, 1); + return is_type; + } } - } else { - nk_love_error(message); } + return 0; +} + +static void nk_love_checkFont(int index, struct nk_user_font *font) +{ + if (index < 0) + index += lua_gettop(L) + 1; + if (!nk_love_is_type(index, "Font")) + luaL_typerror(L, index, "Font"); + lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); + lua_getfield(L, -1, "font"); + lua_pushvalue(L, index); + int ref = luaL_ref(L, -2); + lua_getfield(L, index, "getHeight"); + lua_pushvalue(L, index); + lua_call(L, 1, 1); + float height = lua_tonumber(L, -1); + font->userdata = nk_handle_id(ref); + font->height = height; + font->width = nk_love_get_text_width; + lua_pop(L, 3); +} + +static void nk_love_checkImage(int index, struct nk_image *image) +{ + if (index < 0) + index += lua_gettop(L) + 1; + if (!nk_love_is_type(index, "Image")) + luaL_typerror(L, index, "Image"); + lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); + lua_getfield(L, -1, "image"); + lua_pushvalue(L, index); + int ref = luaL_ref(L, -2); + lua_getfield(L, index, "getDimensions"); + lua_pushvalue(L, index); + lua_call(L, 1, 2); + int width = lua_tointeger(L, -2); + int height = lua_tointeger(L, -1); + image->handle = nk_handle_id(ref); + image->w = width; + image->h = height; + image->region[0] = 0; + image->region[1] = 0; + image->region[2] = width; + image->region[3] = height; + lua_pop(L, 4); +} + +static int nk_love_is_hex(char c) +{ + return (c >= '0' && c <= '9') + || (c >= 'a' && c <= 'f') + || (c >= 'A' && c <= 'F'); +} + +static int nk_love_is_color(int index) +{ + if (index < 0) + index += lua_gettop(L) + 1; + if (lua_isstring(L, index)) { + size_t len; + const char *color_string = lua_tolstring(L, index, &len); + if ((len == 7 || len == 9) && color_string[0] == '#') { + int i; + for (i = 1; i < len; ++i) { + if (!nk_love_is_hex(color_string[i])) + return 0; + } + return 1; + } + } + return 0; +} + +static struct nk_color nk_love_checkcolor(int index) +{ + if (index < 0) + index += lua_gettop(L) + 1; + if (!nk_love_is_color(index)) { + if (lua_isstring(L, index)){ + const char *msg = lua_pushfstring(L, "bad color string '%s'", lua_tostring(L, index)); + luaL_argerror(L, index, msg); + } else { + luaL_typerror(L, index, "color string"); + } + } + size_t len; + const char *color_string = lua_tolstring(L, index, &len); + int r, g, b, a = 255; + sscanf(color_string, "#%02x%02x%02x", &r, &g, &b); + if (len == 9) { + sscanf(color_string + 7, "%02x", &a); + } + struct nk_color color = {r, g, b, a}; + return color; } static nk_flags nk_love_parse_window_flags(int flags_begin) { @@ -649,8 +709,7 @@ static nk_flags nk_love_parse_window_flags(int flags_begin) { nk_flags flags = NK_WINDOW_NO_SCROLLBAR; int i; for (i = flags_begin; i <= argc; ++i) { - nk_love_assert(lua_type(L, i) == LUA_TSTRING, "window flags must be strings"); - const char *flag = lua_tostring(L, i); + const char *flag = luaL_checkstring(L, i); if (!strcmp(flag, "border")) flags |= NK_WINDOW_BORDER; else if (!strcmp(flag, "movable")) @@ -669,130 +728,253 @@ static nk_flags nk_love_parse_window_flags(int flags_begin) { flags |= NK_WINDOW_SCROLL_AUTO_HIDE; else if (!strcmp(flag, "background")) flags |= NK_WINDOW_BACKGROUND; - else - nk_love_error("unrecognized window flag"); - + else { + const char *msg = lua_pushfstring(L, "unrecognized window flag '%s'", flag); + return luaL_argerror(L, i, msg); + } } return flags; } -static void nk_love_tofont(struct nk_user_font *font) -{ - lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); - lua_getfield(L, -1, "font"); - lua_pushvalue(L, -3); - int ref = luaL_ref(L, -2); - lua_getfield(L, -3, "getHeight"); - lua_pushvalue(L, -4); - lua_call(L, 1, 1); - float height = lua_tonumber(L, -1); - font->userdata = nk_handle_id(ref); - font->height = height; - font->width = nk_love_get_text_width; - lua_pop(L, 4); -} - -static void nk_love_toimage(struct nk_image *image) -{ - lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); - lua_getfield(L, -1, "image"); - lua_pushvalue(L, -3); - int ref = luaL_ref(L, -2); - lua_getfield(L, -3, "getDimensions"); - lua_pushvalue(L, -4); - lua_call(L, 1, 2); - int width = lua_tointeger(L, -2); - int height = lua_tointeger(L, -1); - image->handle = nk_handle_id(ref); - image->w = width; - image->h = height; - image->region[0] = 0; - image->region[1] = 0; - image->region[2] = width; - image->region[3] = height; - lua_pop(L, 5); -} - -static int nk_love_parse_symbol(const char *s, enum nk_symbol_type *symbol) +static enum nk_symbol_type nk_love_checksymbol(int index) { + if (index < 0) + index += lua_gettop(L) + 1; + const char *s = luaL_checkstring(L, index); if (!strcmp(s, "none")) { - *symbol = NK_SYMBOL_NONE; + return NK_SYMBOL_NONE; } else if (!strcmp(s, "x")) { - *symbol = NK_SYMBOL_X; + return NK_SYMBOL_X; } else if (!strcmp(s, "underscore")) { - *symbol = NK_SYMBOL_UNDERSCORE; + return NK_SYMBOL_UNDERSCORE; } else if (!strcmp(s, "circle solid")) { - *symbol = NK_SYMBOL_CIRCLE_SOLID; + return NK_SYMBOL_CIRCLE_SOLID; } else if (!strcmp(s, "circle outline")) { - *symbol = NK_SYMBOL_CIRCLE_OUTLINE; + return NK_SYMBOL_CIRCLE_OUTLINE; } else if (!strcmp(s, "rect solid")) { - *symbol = NK_SYMBOL_RECT_SOLID; + return NK_SYMBOL_RECT_SOLID; } else if (!strcmp(s, "rect outline")) { - *symbol = NK_SYMBOL_RECT_OUTLINE; + return NK_SYMBOL_RECT_OUTLINE; } else if (!strcmp(s, "triangle up")) { - *symbol = NK_SYMBOL_TRIANGLE_UP; + return NK_SYMBOL_TRIANGLE_UP; } else if (!strcmp(s, "triangle down")) { - *symbol = NK_SYMBOL_TRIANGLE_DOWN; + return NK_SYMBOL_TRIANGLE_DOWN; } else if (!strcmp(s, "triangle left")) { - *symbol = NK_SYMBOL_TRIANGLE_LEFT; + return NK_SYMBOL_TRIANGLE_LEFT; } else if (!strcmp(s, "triangle right")) { - *symbol = NK_SYMBOL_TRIANGLE_RIGHT; + return NK_SYMBOL_TRIANGLE_RIGHT; } else if (!strcmp(s, "plus")) { - *symbol = NK_SYMBOL_PLUS; + return NK_SYMBOL_PLUS; } else if (!strcmp(s, "minus")) { - *symbol = NK_SYMBOL_MINUS; + return NK_SYMBOL_MINUS; } else if (!strcmp(s, "max")) { - *symbol = NK_SYMBOL_MAX; + return NK_SYMBOL_MAX; } else { - return 0; + const char *msg = lua_pushfstring(L, "unrecognized symbol type '%s'", s); + return luaL_argerror(L, index, msg); } - return 1; } -static int nk_love_parse_align(const char *s, nk_flags *align) +static nk_flags nk_love_checkalign(int index) { + if (index < 0) + index += lua_gettop(L) + 1; + const char *s = luaL_checkstring(L, index); if (!strcmp(s, "left")) { - *align = NK_TEXT_LEFT; + return NK_TEXT_LEFT; } else if (!strcmp(s, "centered")) { - *align = NK_TEXT_CENTERED; + return NK_TEXT_CENTERED; } else if (!strcmp(s, "right")) { - *align = NK_TEXT_RIGHT; + return NK_TEXT_RIGHT; } else if (!strcmp(s, "top left")) { - *align = NK_TEXT_ALIGN_TOP | NK_TEXT_ALIGN_LEFT; + return NK_TEXT_ALIGN_TOP | NK_TEXT_ALIGN_LEFT; } else if (!strcmp(s, "top centered")) { - *align = NK_TEXT_ALIGN_TOP | NK_TEXT_ALIGN_CENTERED; + return NK_TEXT_ALIGN_TOP | NK_TEXT_ALIGN_CENTERED; } else if (!strcmp(s, "top right")) { - *align = NK_TEXT_ALIGN_TOP | NK_TEXT_ALIGN_RIGHT; + return NK_TEXT_ALIGN_TOP | NK_TEXT_ALIGN_RIGHT; } else if (!strcmp(s, "bottom left")) { - *align = NK_TEXT_ALIGN_BOTTOM | NK_TEXT_ALIGN_LEFT; + return NK_TEXT_ALIGN_BOTTOM | NK_TEXT_ALIGN_LEFT; } else if (!strcmp(s, "bottom centered")) { - *align = NK_TEXT_ALIGN_BOTTOM | NK_TEXT_ALIGN_CENTERED; + return NK_TEXT_ALIGN_BOTTOM | NK_TEXT_ALIGN_CENTERED; } else if (!strcmp(s, "bottom right")) { - *align = NK_TEXT_ALIGN_BOTTOM | NK_TEXT_ALIGN_RIGHT; + return NK_TEXT_ALIGN_BOTTOM | NK_TEXT_ALIGN_RIGHT; } else { - return 0; + const char *msg = lua_pushfstring(L, "unrecognized alignment '%s'", s); + return luaL_argerror(L, index, msg); } - return 1; } -static int nk_love_parse_button(const char *s, enum nk_buttons *button) +static enum nk_buttons nk_love_checkbutton(int index) { + if (index < 0) + index += lua_gettop(L) + 1; + const char *s = luaL_checkstring(L, index); if (!strcmp(s, "left")) { - *button = NK_BUTTON_LEFT; + return NK_BUTTON_LEFT; } else if (!strcmp(s, "right")) { - *button = NK_BUTTON_RIGHT; + return NK_BUTTON_RIGHT; } else if (!strcmp(s, "middle")) { - *button = NK_BUTTON_MIDDLE; + return NK_BUTTON_MIDDLE; } else { - return 0; + const char *msg = lua_pushfstring(L, "unrecognized mouse button '%s'", s); + return luaL_argerror(L, index, msg); } - return 1; +} + +static enum nk_layout_format nk_love_checkformat(int index) { + if (index < 0) + index += lua_gettop(L) + 1; + const char *type = luaL_checkstring(L, index); + if (!strcmp(type, "dynamic")) { + return NK_DYNAMIC; + } else if (!strcmp(type, "static")) { + return NK_STATIC; + } else { + const char *msg = lua_pushfstring(L, "unrecognized layout format '%s'", type); + return luaL_argerror(L, index, msg); + } +} + +static enum nk_tree_type nk_love_checktree(int index) { + if (index < 0) + index += lua_gettop(L) + 1; + const char *type_string = luaL_checkstring(L, index); + if (!strcmp(type_string, "node")) { + return NK_TREE_NODE; + } else if (!strcmp(type_string, "tab")) { + return NK_TREE_TAB; + } else { + const char *msg = lua_pushfstring(L, "unrecognized tree type '%s'", type_string); + return luaL_argerror(L, index, msg); + } +} + +static enum nk_collapse_states nk_love_checkstate(int index) { + if (index < 0) + index += lua_gettop(L) + 1; + const char *state_string = luaL_checkstring(L, index); + if (!strcmp(state_string, "collapsed")) { + return NK_MINIMIZED; + } else if (!strcmp(state_string, "expanded")) { + return NK_MAXIMIZED; + } else { + const char *msg = lua_pushfstring(L, "unrecognized tree state '%s'", state_string); + return luaL_argerror(L, index, msg); + } +} + +static enum nk_button_behavior nk_love_checkbehavior(int index) { + if (index < 0) + index += lua_gettop(L) + 1; + const char *behavior_string = luaL_checkstring(L, index); + if (!strcmp(behavior_string, "default")) + return NK_BUTTON_DEFAULT; + else if (!strcmp(behavior_string, "repeater")) + return NK_BUTTON_REPEATER; + else { + const char *msg = lua_pushfstring(L, "unrecognized button behavior '%s'", behavior_string); + return luaL_argerror(L, index, msg); + } +} + +static enum nk_color_format nk_love_checkcolorformat(int index) { + if (index < 0) + index += lua_gettop(L) + 1; + const char *format_string = luaL_checkstring(L, index); + if (!strcmp(format_string, "RGB")) { + return NK_RGB; + } else if (!strcmp(format_string, "RGBA")) { + return NK_RGBA; + } else { + const char *msg = lua_pushfstring(L, "unrecognized color format '%s'", format_string); + return luaL_argerror(L, index, msg); + } +} + +static nk_flags nk_love_checkedittype(int index) { + if (index < 0) + index += lua_gettop(L) + 1; + const char *type_string = luaL_checkstring(L, index); + if (!strcmp(type_string, "simple")) { + return NK_EDIT_SIMPLE; + } else if (!strcmp(type_string, "field")) { + return NK_EDIT_FIELD; + } else if (!strcmp(type_string, "box")) { + return NK_EDIT_BOX; + } else { + const char *msg = lua_pushfstring(L, "unrecognized edit type '%s'", type_string); + return luaL_argerror(L, index, msg); + } +} + +static enum nk_popup_type nk_love_checkpopup(int index) { + if (index < 0) + index += lua_gettop(L) + 1; + const char *popup_type = luaL_checkstring(L, index); + if (!strcmp(popup_type, "dynamic")) { + return NK_POPUP_DYNAMIC; + } else if (!strcmp(popup_type, "static")) { + return NK_POPUP_STATIC; + } else { + const char *msg = lua_pushfstring(L, "unrecognized popup type '%s'", popup_type); + return luaL_argerror(L, index, msg); + } +} + +enum nk_love_draw_mode {NK_LOVE_FILL, NK_LOVE_LINE}; + +static enum nk_love_draw_mode nk_love_checkdraw(int index) { + if (index < 0) + index += lua_gettop(L) + 1; + const char *mode = luaL_checkstring(L, index); + if (!strcmp(mode, "fill")) { + return NK_LOVE_FILL; + } else if (!strcmp(mode, "line")) { + return NK_LOVE_LINE; + } else { + const char *msg = lua_pushfstring(L, "unrecognized draw mode '%s'", mode); + return luaL_argerror(L, index, msg); + } +} + +static int nk_love_checkboolean(lua_State *L, int index) +{ + if (index < 0) + index += lua_gettop(L) + 1; + luaL_checktype(L, index, LUA_TBOOLEAN); + return lua_toboolean(L, index); +} + +static void nk_love_assert(int pass, const char *msg) { + if (!pass) { + lua_Debug ar; + ar.name = NULL; + if (lua_getstack(L, 0, &ar)) + lua_getinfo(L, "n", &ar); + if (ar.name == NULL) + ar.name = "?"; + luaL_error(L, msg, ar.name); + } +} + +static void nk_love_assert_argc(int pass) { + nk_love_assert(pass, "wrong number of arguments to '%s'"); +} + +static void nk_love_assert_alloc(void *mem) { + nk_love_assert(mem != NULL, "out of memory in '%s'"); +} + +static void *nk_love_malloc(size_t size) { + void *mem = malloc(size); + nk_love_assert_alloc(mem); + return mem; } static int nk_love_init(lua_State *luaState) { L = luaState; - nk_love_assert(lua_gettop(L) == 0, "nk.init: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); lua_newtable(L); lua_pushvalue(L, -1); lua_setfield(L, LUA_REGISTRYINDEX, "nuklear"); @@ -802,31 +984,27 @@ static int nk_love_init(lua_State *luaState) lua_setfield(L, -2, "image"); lua_newtable(L); lua_setfield(L, -2, "stack"); - fonts = malloc(sizeof(struct nk_user_font) * NK_LOVE_MAX_FONTS); - nk_love_assert(fonts != NULL, "nk.init: out of memory"); + fonts = nk_love_malloc(sizeof(struct nk_user_font) * NK_LOVE_MAX_FONTS); lua_getglobal(L, "love"); - nk_love_assert(lua_type(L, -1) == LUA_TTABLE, "nk.init: requires LOVE environment"); + nk_love_assert(lua_istable(L, -1), "LOVE-Nuklear requires LOVE environment"); lua_getfield(L, -1, "graphics"); lua_getfield(L, -1, "getFont"); lua_call(L, 0, 1); - nk_love_tofont(&fonts[0]); + nk_love_checkFont(-1, &fonts[0]); nk_init_default(&context, &fonts[0]); font_count = 1; context.clip.copy = nk_love_clipbard_copy; context.clip.paste = nk_love_clipbard_paste; context.clip.userdata = nk_handle_ptr(0); - edit_buffer = malloc(NK_LOVE_EDIT_BUFFER_LEN); - nk_love_assert(edit_buffer != NULL, "nk.init: out of memory"); - combobox_items = malloc(sizeof(char*) * NK_LOVE_COMBOBOX_MAX_ITEMS); - nk_love_assert(combobox_items != NULL, "nk.init: out of memory"); - floats = malloc(sizeof(float) * NK_MAX(NK_LOVE_MAX_RATIOS, NK_LOVE_MAX_POINTS * 2)); - nk_love_assert(floats != NULL, "nk.init: out of memory"); + edit_buffer = nk_love_malloc(NK_LOVE_EDIT_BUFFER_LEN); + combobox_items = nk_love_malloc(sizeof(char*) * NK_LOVE_COMBOBOX_MAX_ITEMS); + floats = nk_love_malloc(sizeof(float) * NK_MAX(NK_LOVE_MAX_RATIOS, NK_LOVE_MAX_POINTS * 2)); return 0; } static int nk_love_shutdown(lua_State *luaState) { - nk_love_assert(lua_gettop(L) == 0, "nk.shutdown: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_free(&context); lua_pushnil(L); lua_setfield(L, LUA_REGISTRYINDEX, "nuklear"); @@ -844,13 +1022,10 @@ static int nk_love_shutdown(lua_State *luaState) static int nk_love_keypressed(lua_State *L) { - nk_love_assert(lua_gettop(L) == 3, "nk.keypressed: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.keypressed: arg 1 should be a string"); - nk_love_assert(lua_type(L, 2) == LUA_TSTRING, "nk.keypressed: arg 2 should be a string"); - nk_love_assert(lua_type(L, 3) == LUA_TBOOLEAN, "nk.keypressed: arg 3 should be a boolean"); - const char *key = lua_tostring(L, 1); - const char *scancode = lua_tostring(L, 2); - int isrepeat = lua_toboolean(L, 3); + nk_love_assert_argc(lua_gettop(L) == 3); + const char *key = luaL_checkstring(L, 1); + const char *scancode = luaL_checkstring(L, 2); + int isrepeat = nk_love_checkboolean(L, 3); int consume = nk_love_keyevent(key, scancode, isrepeat, 1); lua_pushboolean(L, consume); return 1; @@ -858,11 +1033,9 @@ static int nk_love_keypressed(lua_State *L) static int nk_love_keyreleased(lua_State *L) { - nk_love_assert(lua_gettop(L) == 2, "nk.keyreleased: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.keyreleased: arg 1 should be a string"); - nk_love_assert(lua_type(L, 2) == LUA_TSTRING, "nk.keyreleased: arg 2 should be a string"); - const char *key = lua_tostring(L, 1); - const char *scancode = lua_tostring(L, 2); + nk_love_assert_argc(lua_gettop(L) == 2); + const char *key = luaL_checkstring(L, 1); + const char *scancode = luaL_checkstring(L, 2); int consume = nk_love_keyevent(key, scancode, 0, 0); lua_pushboolean(L, consume); return 1; @@ -870,15 +1043,11 @@ static int nk_love_keyreleased(lua_State *L) static int nk_love_mousepressed(lua_State *L) { - nk_love_assert(lua_gettop(L) == 4, "nk.mousepressed: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.mousepressed: arg 1 should be a number"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.mousepressed: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.mousepressed: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TBOOLEAN, "nk.mousepressed: arg 4 should be a boolean"); - int x = lua_tointeger(L, 1); - int y = lua_tointeger(L, 2); - int button = lua_tointeger(L, 3); - int istouch = lua_toboolean(L, 4); + nk_love_assert_argc(lua_gettop(L) == 4); + int x = luaL_checkint(L, 1); + int y = luaL_checkint(L, 2); + int button = luaL_checkint(L, 3); + int istouch = nk_love_checkboolean(L, 4); int consume = nk_love_clickevent(x, y, button, istouch, 1); lua_pushboolean(L, consume); return 1; @@ -886,15 +1055,11 @@ static int nk_love_mousepressed(lua_State *L) static int nk_love_mousereleased(lua_State *L) { - nk_love_assert(lua_gettop(L) == 4, "nk.mousereleased: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.mousereleased: arg 1 should be a number"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.mousereleased: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.mousereleased: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TBOOLEAN, "nk.mousereleased: arg 4 should be a boolean"); - int x = lua_tointeger(L, 1); - int y = lua_tointeger(L, 2); - int button = lua_tointeger(L, 3); - int istouch = lua_toboolean(L, 4); + nk_love_assert_argc(lua_gettop(L) == 4); + int x = luaL_checkint(L, 1); + int y = luaL_checkint(L, 2); + int button = luaL_checkint(L, 3); + int istouch = nk_love_checkboolean(L, 4); int consume = nk_love_clickevent(x, y, button, istouch, 0); lua_pushboolean(L, consume); return 1; @@ -902,17 +1067,12 @@ static int nk_love_mousereleased(lua_State *L) static int nk_love_mousemoved(lua_State *L) { - nk_love_assert(lua_gettop(L) == 5, "nk.mousemoved: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.mousemoved: arg 1 should be a number"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.mousemoved: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.mousemoved: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.mousemoved: arg 4 should be a number"); - nk_love_assert(lua_type(L, 5) == LUA_TBOOLEAN, "nk.mousemoved: arg 5 should be a boolean"); - int x = lua_tointeger(L, 1); - int y = lua_tointeger(L, 2); - int dx = lua_tointeger(L, 3); - int dy = lua_tointeger(L, 4); - int istouch = lua_toboolean(L, 5); + nk_love_assert_argc(lua_gettop(L) == 5); + int x = luaL_checkint(L, 1); + int y = luaL_checkint(L, 2); + int dx = luaL_checkint(L, 3); + int dy = luaL_checkint(L, 4); + int istouch = nk_love_checkboolean(L, 5); int consume = nk_love_mousemoved_event(x, y, dx, dy, istouch); lua_pushboolean(L, consume); return 1; @@ -920,9 +1080,8 @@ static int nk_love_mousemoved(lua_State *L) static int nk_love_textinput(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.textinput: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.textinput: arg 1 should be a string"); - const char *text = lua_tostring(L, 1); + nk_love_assert_argc(lua_gettop(L) == 1); + const char *text = luaL_checkstring(L, 1); int consume = nk_love_textinput_event(text); lua_pushboolean(L, consume); return 1; @@ -930,11 +1089,9 @@ static int nk_love_textinput(lua_State *L) static int nk_love_wheelmoved(lua_State *L) { - nk_love_assert(lua_gettop(L) == 2, "nk.wheelmoved: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.wheelmoved: arg 1 should be a number"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.wheelmoved: arg 2 should be a number"); - int x = lua_tointeger(L, 1); - int y = lua_tointeger(L, 2); + nk_love_assert_argc(lua_gettop(L) == 2); + int x = luaL_checkint(L, 1); + int y = luaL_checkint(L, 2); int consume = nk_love_wheelmoved_event(x, y); lua_pushboolean(L, consume); return 1; @@ -942,7 +1099,7 @@ static int nk_love_wheelmoved(lua_State *L) static int nk_love_draw(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.draw: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); lua_getglobal(L, "love"); lua_getfield(L, -1, "graphics"); @@ -1046,7 +1203,8 @@ static void nk_love_preserve(struct nk_style_item *item) { if (item->type == NK_STYLE_ITEM_IMAGE) { lua_rawgeti(L, -1, item->data.image.handle.id); - nk_love_toimage(&item->data.image); + nk_love_checkImage(-1, &item->data.image); + lua_pop(L, 1); } } @@ -1172,7 +1330,7 @@ static void nk_love_preserve_all(void) static int nk_love_frame_begin(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.frameBegin: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_input_end(&context); lua_getglobal(L, "love"); lua_getfield(L, -1, "timer"); @@ -1191,13 +1349,15 @@ static int nk_love_frame_begin(lua_State *L) lua_setfield(L, -3, "font"); font_count = 0; lua_rawgeti(L, -1, context.style.font->userdata.id); - nk_love_tofont(&fonts[font_count]); + nk_love_checkFont(-1, &fonts[font_count]); + lua_pop(L, 1); context.style.font = &fonts[font_count++]; int i; for (i = 0; i < context.stacks.fonts.head; ++i) { struct nk_config_stack_user_font_element *element = &context.stacks.fonts.elements[i]; lua_rawgeti(L, -1, element->old_value->userdata.id); - nk_love_tofont(&fonts[font_count]); + nk_love_checkFont(-1, &fonts[font_count]); + lua_pop(L, 1); context.stacks.fonts.elements[i].old_value = &fonts[font_count++]; } layout_ratio_count = 0; @@ -1206,7 +1366,7 @@ static int nk_love_frame_begin(lua_State *L) static int nk_love_frame_end(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.frameEnd: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_input_begin(&context); return 0; } @@ -1215,31 +1375,21 @@ static int nk_love_window_begin(lua_State *L) { const char *name, *title; int bounds_begin; - if (lua_type(L, 2) == LUA_TNUMBER) { - nk_love_assert(lua_gettop(L) >= 5, "nk.windowBegin: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.windowBegin: arg 1 should be a string"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.windowBegin: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.windowBegin: arg 4 should be a number"); - nk_love_assert(lua_type(L, 5) == LUA_TNUMBER, "nk.windowBegin: arg 5 should be a number"); - name = title = lua_tostring(L, 1); + if (lua_isnumber(L, 2)) { + nk_love_assert_argc(lua_gettop(L) >= 5); + name = title = luaL_checkstring(L, 1); bounds_begin = 2; } else { - nk_love_assert(lua_gettop(L) >= 6, "nk.windowBegin: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.windowBegin: arg 1 should be a string"); - nk_love_assert(lua_type(L, 2) == LUA_TSTRING, "nk.windowBegin: arg 2 should be a string"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.windowBegin: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.windowBegin: arg 4 should be a number"); - nk_love_assert(lua_type(L, 5) == LUA_TNUMBER, "nk.windowBegin: arg 5 should be a number"); - nk_love_assert(lua_type(L, 6) == LUA_TNUMBER, "nk.windowBegin: arg 6 should be a number"); - name = lua_tostring(L, 1); - title = lua_tostring(L, 2); + nk_love_assert_argc(lua_gettop(L) >= 6); + name = luaL_checkstring(L, 1); + title = luaL_checkstring(L, 2); bounds_begin = 3; } nk_flags flags = nk_love_parse_window_flags(bounds_begin + 4); - float x = lua_tonumber(L, bounds_begin); - float y = lua_tonumber(L, bounds_begin + 1); - float width = lua_tonumber(L, bounds_begin + 2); - float height = lua_tonumber(L, bounds_begin + 3); + float x = luaL_checknumber(L, bounds_begin); + float y = luaL_checknumber(L, bounds_begin + 1); + float width = luaL_checknumber(L, bounds_begin + 2); + float height = luaL_checknumber(L, bounds_begin + 3); int open = nk_begin_titled(&context, name, title, nk_rect(x, y, width, height), flags); lua_pushboolean(L, open); return 1; @@ -1247,14 +1397,14 @@ static int nk_love_window_begin(lua_State *L) static int nk_love_window_end(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.windowEnd: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_end(&context); return 0; } static int nk_love_window_get_bounds(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.windowGetBounds: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); struct nk_rect rect = nk_window_get_bounds(&context); lua_pushnumber(L, rect.x); lua_pushnumber(L, rect.y); @@ -1265,7 +1415,7 @@ static int nk_love_window_get_bounds(lua_State *L) static int nk_love_window_get_position(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.windowGetPosition: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); struct nk_vec2 pos = nk_window_get_position(&context); lua_pushnumber(L, pos.x); lua_pushnumber(L, pos.y); @@ -1274,7 +1424,7 @@ static int nk_love_window_get_position(lua_State *L) static int nk_love_window_get_size(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.windowGetSize: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); struct nk_vec2 size = nk_window_get_size(&context); lua_pushnumber(L, size.x); lua_pushnumber(L, size.y); @@ -1283,7 +1433,7 @@ static int nk_love_window_get_size(lua_State *L) static int nk_love_window_get_content_region(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.windowGetContentRegion: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); struct nk_rect rect = nk_window_get_content_region(&context); lua_pushnumber(L, rect.x); lua_pushnumber(L, rect.y); @@ -1294,7 +1444,7 @@ static int nk_love_window_get_content_region(lua_State *L) static int nk_love_window_has_focus(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.windowHasFocus: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); int has_focus = nk_window_has_focus(&context); lua_pushboolean(L, has_focus); return 1; @@ -1302,9 +1452,8 @@ static int nk_love_window_has_focus(lua_State *L) static int nk_love_window_is_collapsed(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.windowIsCollapsed: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.windowIsCollapsed: arg 1 should be a string"); - const char *name = lua_tostring(L, 1); + nk_love_assert_argc(lua_gettop(L) == 1); + const char *name = luaL_checkstring(L, 1); int is_collapsed = nk_window_is_collapsed(&context, name); lua_pushboolean(L, is_collapsed); return 1; @@ -1312,18 +1461,16 @@ static int nk_love_window_is_collapsed(lua_State *L) static int nk_love_window_is_hidden(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.windowIsHidden: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.windowIsHidden: arg 1 should be a string"); - const char *name = lua_tostring(L, 1); + nk_love_assert_argc(lua_gettop(L) == 1); + const char *name = luaL_checkstring(L, 1); int is_hidden = nk_window_is_hidden(&context, name); lua_pushboolean(L, is_hidden); return 1; } static int nk_love_window_is_active(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.windowIsActive: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.windowIsActive: arg 1 should be a string"); - const char *name = lua_tostring(L, 1); + nk_love_assert_argc(lua_gettop(L) == 1); + const char *name = luaL_checkstring(L, 1); int is_active = nk_window_is_active(&context, name); lua_pushboolean(L, is_active); return 1; @@ -1331,7 +1478,7 @@ static int nk_love_window_is_active(lua_State *L) { static int nk_love_window_is_hovered(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.windowIsHovered: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); int is_hovered = nk_window_is_hovered(&context); lua_pushboolean(L, is_hovered); return 1; @@ -1339,7 +1486,7 @@ static int nk_love_window_is_hovered(lua_State *L) static int nk_love_window_is_any_hovered(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.windowIsAnyHovered: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); int is_any_hovered = nk_window_is_any_hovered(&context); lua_pushboolean(L, is_any_hovered); return 1; @@ -1347,101 +1494,87 @@ static int nk_love_window_is_any_hovered(lua_State *L) static int nk_love_item_is_any_active(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.itemIsAnyActive: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); lua_pushboolean(L, nk_love_is_active(&context)); return 1; } static int nk_love_window_set_bounds(lua_State *L) { - nk_love_assert(lua_gettop(L) == 4, "nk.windowSetBounds: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.windowSetBounds: arg 1 should be a number"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.windowSetBounds: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.windowSetBounds: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.windowSetBounds: arg 4 should be a number"); + nk_love_assert_argc(lua_gettop(L) == 4); struct nk_rect bounds; - bounds.x = lua_tonumber(L, 1); - bounds.y = lua_tonumber(L, 2); - bounds.w = lua_tonumber(L, 3); - bounds.h = lua_tonumber(L, 4); + bounds.x = luaL_checknumber(L, 1); + bounds.y = luaL_checknumber(L, 2); + bounds.w = luaL_checknumber(L, 3); + bounds.h = luaL_checknumber(L, 4); nk_window_set_bounds(&context, bounds); return 0; } static int nk_love_window_set_position(lua_State *L) { - nk_love_assert(lua_gettop(L) == 2, "nk.windowSetPosition: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.windowSetPosition: arg 1 should be a number"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.windowSetPosition: arg 2 should be a number"); + nk_love_assert_argc(lua_gettop(L) == 2); struct nk_vec2 pos; - pos.x = lua_tonumber(L, 1); - pos.y = lua_tonumber(L, 2); + pos.x = luaL_checknumber(L, 1); + pos.y = luaL_checknumber(L, 2); nk_window_set_position(&context, pos); return 0; } static int nk_love_window_set_size(lua_State *L) { - nk_love_assert(lua_gettop(L) == 2, "nk.windowSetSize: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.windowSetSize: arg 1 should be a number"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.windowSetSize: arg 2 should be a number"); + nk_love_assert_argc(lua_gettop(L) == 2); struct nk_vec2 size; - size.x = lua_tonumber(L, 1); - size.y = lua_tonumber(L, 2); + size.x = luaL_checknumber(L, 1); + size.y = luaL_checknumber(L, 2); nk_window_set_size(&context, size); return 0; } static int nk_love_window_set_focus(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.windowSetFocus: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.windowSetFocus: arg 1 should be a string"); - const char *name = lua_tostring(L, 1); + nk_love_assert_argc(lua_gettop(L) == 1); + const char *name = luaL_checkstring(L, 1); nk_window_set_focus(&context, name); return 0; } static int nk_love_window_close(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.windowClose: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.windowClose: arg 1 should be a string"); - const char *name = lua_tostring(L, 1); + nk_love_assert_argc(lua_gettop(L) == 1); + const char *name = luaL_checkstring(L, 1); nk_window_close(&context, name); return 0; } static int nk_love_window_collapse(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.windowCollapse: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.windowCollapse: arg 1 should be a string"); - const char *name = lua_tostring(L, 1); + nk_love_assert_argc(lua_gettop(L) == 1); + const char *name = luaL_checkstring(L, 1); nk_window_collapse(&context, name, NK_MINIMIZED); return 0; } static int nk_love_window_expand(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.windowExpand: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.windowExpand: arg 1 should be a string"); - const char *name = lua_tostring(L, 1); + nk_love_assert_argc(lua_gettop(L) == 1); + const char *name = luaL_checkstring(L, 1); nk_window_collapse(&context, name, NK_MAXIMIZED); return 0; } static int nk_love_window_show(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.windowShow: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.windowShow: arg 1 should be a string"); - const char *name = lua_tostring(L, 1); + nk_love_assert_argc(lua_gettop(L) == 1); + const char *name = luaL_checkstring(L, 1); nk_window_show(&context, name, NK_SHOWN); return 0; } static int nk_love_window_hide(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.windowHide: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.windowHide: arg 1 should be a string"); - const char *name = lua_tostring(L, 1); + nk_love_assert_argc(lua_gettop(L) == 1); + const char *name = luaL_checkstring(L, 1); nk_window_show(&context, name, NK_HIDDEN); return 0; } @@ -1449,43 +1582,38 @@ static int nk_love_window_hide(lua_State *L) static int nk_love_layout_row(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc == 3 || argc == 4, "nk.layoutRow: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.layoutRow: arg 1 should be a string"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.layoutRow: arg 2 should be a number"); - const char *type = lua_tostring(L, 1); - float height = lua_tonumber(L, 2); - enum nk_layout_format format; + nk_love_assert_argc(argc >= 3 && argc <= 4); + enum nk_layout_format format = nk_love_checkformat(1); + float height = luaL_checknumber(L, 2); int use_ratios = 0; - if (!strcmp(type, "dynamic")) { - nk_love_assert(argc == 3, "nk.layoutRow: wrong number of arguments"); - if (lua_type(L, 3) == LUA_TNUMBER) { - int cols = lua_tointeger(L, 3); + if (format == NK_DYNAMIC) { + nk_love_assert_argc(argc == 3); + if (lua_isnumber(L, 3)) { + int cols = luaL_checkint(L, 3); nk_layout_row_dynamic(&context, height, cols); } else { - nk_love_assert(lua_type(L, 3) == LUA_TTABLE, "nk.layoutRow: arg 3 should be a number or table"); - format = NK_DYNAMIC; + if (!lua_istable(L, 3)) + luaL_argerror(L, 3, "should be a number or table"); use_ratios = 1; } - } else if (!strcmp(type, "static")) { + } else if (format == NK_STATIC) { if (argc == 4) { - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.layoutRow: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.layoutRow: arg 4 should be a number"); - int item_width = lua_tointeger(L, 3); - int cols = lua_tointeger(L, 4); + int item_width = luaL_checkint(L, 3); + int cols = luaL_checkint(L, 4); nk_layout_row_static(&context, height, item_width, cols); } else { - nk_love_assert(lua_type(L, 3) == LUA_TTABLE, "nk.layoutRow: arg 3 should be a number or table"); - format = NK_STATIC; + if (!lua_istable(L, 3)) + luaL_argerror(L, 3, "should be a number or table"); use_ratios = 1; } - } else { - nk_love_error("nk.layoutRow: arg 1 should be 'dynamic' or 'static'"); } if (use_ratios) { int cols = lua_objlen(L, -1); int i, j; for (i = 1, j = layout_ratio_count; i <= cols && j < NK_LOVE_MAX_RATIOS; ++i, ++j) { lua_rawgeti(L, -1, i); + if (!lua_isnumber(L, -1)) + luaL_argerror(L, lua_gettop(L) - 1, "should contain numbers only"); floats[j] = lua_tonumber(L, -1); lua_pop(L, 1); } @@ -1497,88 +1625,61 @@ static int nk_love_layout_row(lua_State *L) static int nk_love_layout_row_begin(lua_State *L) { - nk_love_assert(lua_gettop(L) == 3, "nk.layoutRowBegin: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.layoutRowBegin: arg 1 should be a string"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.layoutRowBegin: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.layoutRowBegin: arg 3 should be a number"); - const char *type = lua_tostring(L, 1); - float height = lua_tonumber(L, 2); - int cols = lua_tointeger(L, 3); - enum nk_layout_format format; - if (!strcmp(type, "dynamic")) { - format = NK_DYNAMIC; - } else if (!strcmp(type, "static")) { - format = NK_STATIC; - } else { - nk_love_error("nk.layoutRowBegin: arg 1 should be 'dynamic' or 'static'"); - } + nk_love_assert_argc(lua_gettop(L) == 3); + enum nk_layout_format format = nk_love_checkformat(1); + float height = luaL_checknumber(L, 2); + int cols = luaL_checkint(L, 3); nk_layout_row_begin(&context, format, height, cols); return 0; } static int nk_love_layout_row_push(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.layoutRowPush: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.layoutRowPush: arg 1 should be a number"); - float value = lua_tonumber(L, 1); + nk_love_assert_argc(lua_gettop(L) == 1); + float value = luaL_checknumber(L, 1); nk_layout_row_push(&context, value); return 0; } static int nk_love_layout_row_end(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.layoutRowEnd: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_layout_row_end(&context); return 0; } static int nk_love_layout_space_begin(lua_State *L) { - nk_love_assert(lua_gettop(L) == 3, "nk.layoutSpaceBegin: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.layoutSpaceBegin: arg 1 should be a string"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.layoutSpaceBegin: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.layoutSpaceBegin: arg 3 should be a number"); - const char *type = lua_tostring(L, 1); - float height = lua_tonumber(L, 2); - int widget_count = lua_tointeger(L, 3); - enum nk_layout_format format; - if (!strcmp(type, "dynamic")) { - format = NK_LAYOUT_DYNAMIC; - } else if (!strcmp(type, "static")) { - format = NK_LAYOUT_STATIC; - } else { - nk_love_error("nk.layoutSpaceBegin: arg 1 should be 'dynamic' or 'static'"); - } + nk_love_assert_argc(lua_gettop(L) == 3); + enum nk_layout_format format = nk_love_checkformat(1); + float height = luaL_checknumber(L, 2); + int widget_count = luaL_checkint(L, 3); nk_layout_space_begin(&context, format, height, widget_count); return 0; } static int nk_love_layout_space_push(lua_State *L) { - nk_love_assert(lua_gettop(L) == 4, "nk.layoutSpacePush: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.layoutSpacePush: arg 1 should be a number"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.layoutSpacePush: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.layoutSpacePush: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.layoutSpacePush: arg 4 should be a number"); - float x = lua_tonumber(L, 1); - float y = lua_tonumber(L, 2); - float width = lua_tonumber(L, 3); - float height = lua_tonumber(L, 4); + nk_love_assert_argc(lua_gettop(L) == 4); + float x = luaL_checknumber(L, 1); + float y = luaL_checknumber(L, 2); + float width = luaL_checknumber(L, 3); + float height = luaL_checknumber(L, 4); nk_layout_space_push(&context, nk_rect(x, y, width, height)); return 0; } static int nk_love_layout_space_end(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.layoutSpaceEnd: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_layout_space_end(&context); return 0; } static int nk_love_layout_space_bounds(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.layoutSpaceBounds: wrong number of arguments"); - struct nk_rect bounds = nk_layout_space_bounds( &context); + nk_love_assert_argc(lua_gettop(L) == 0); + struct nk_rect bounds = nk_layout_space_bounds(&context); lua_pushnumber(L, bounds.x); lua_pushnumber(L, bounds.y); lua_pushnumber(L, bounds.w); @@ -1588,12 +1689,10 @@ static int nk_love_layout_space_bounds(lua_State *L) static int nk_love_layout_space_to_screen(lua_State *L) { - nk_love_assert(lua_gettop(L) == 2, "nk.layoutSpaceToScreen: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.layoutSpaceToScreen: arg 1 should be a number"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.layoutSpaceToScreen: arg 2 should be a number"); + nk_love_assert_argc(lua_gettop(L) == 2); struct nk_vec2 local; - local.x = lua_tonumber(L, 1); - local.y = lua_tonumber(L, 2); + local.x = luaL_checknumber(L, 1); + local.y = luaL_checknumber(L, 2); struct nk_vec2 screen = nk_layout_space_to_screen(&context, local); lua_pushnumber(L, screen.x); lua_pushnumber(L, screen.y); @@ -1602,12 +1701,10 @@ static int nk_love_layout_space_to_screen(lua_State *L) static int nk_love_layout_space_to_local(lua_State *L) { - nk_love_assert(lua_gettop(L) == 2, "nk.layoutSpaceToLocal: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.layoutSpaceToLocal: arg 1 should be a number"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.layoutSpaceToLocal: arg 2 should be a number"); + nk_love_assert_argc(lua_gettop(L) == 2); struct nk_vec2 screen; - screen.x = lua_tonumber(L, 1); - screen.y = lua_tonumber(L, 2); + screen.x = luaL_checknumber(L, 1); + screen.y = luaL_checknumber(L, 2); struct nk_vec2 local = nk_layout_space_to_local(&context, screen); lua_pushnumber(L, local.x); lua_pushnumber(L, local.y); @@ -1616,16 +1713,12 @@ static int nk_love_layout_space_to_local(lua_State *L) static int nk_love_layout_space_rect_to_screen(lua_State *L) { - nk_love_assert(lua_gettop(L) == 4, "nk.layoutSpaceRectToScreen: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.layoutSpaceRectToScreen: arg 1 should be a number"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.layoutSpaceRectToScreen: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.layoutSpaceRectToScreen: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.layoutSpaceRectToScreen: arg 4 should be a number"); + nk_love_assert_argc(lua_gettop(L) == 4); struct nk_rect local; - local.x = lua_tonumber(L, 1); - local.y = lua_tonumber(L, 2); - local.w = lua_tonumber(L, 3); - local.h = lua_tonumber(L, 4); + local.x = luaL_checknumber(L, 1); + local.y = luaL_checknumber(L, 2); + local.w = luaL_checknumber(L, 3); + local.h = luaL_checknumber(L, 4); struct nk_rect screen = nk_layout_space_rect_to_screen(&context, local); lua_pushnumber(L, screen.x); lua_pushnumber(L, screen.y); @@ -1636,16 +1729,12 @@ static int nk_love_layout_space_rect_to_screen(lua_State *L) static int nk_love_layout_space_rect_to_local(lua_State *L) { - nk_love_assert(lua_gettop(L) == 4, "nk.layoutSpaceRectToLocal: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.layoutSpaceRectToLocal: arg 1 should be a number"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.layoutSpaceRectToLocal: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.layoutSpaceRectToLocal: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.layoutSpaceRectToLocal: arg 4 should be a number"); + nk_love_assert_argc(lua_gettop(L) == 4); struct nk_rect screen; - screen.x = lua_tonumber(L, 1); - screen.y = lua_tonumber(L, 2); - screen.w = lua_tonumber(L, 3); - screen.h = lua_tonumber(L, 4); + screen.x = luaL_checknumber(L, 1); + screen.y = luaL_checknumber(L, 2); + screen.w = luaL_checknumber(L, 3); + screen.h = luaL_checknumber(L, 4); struct nk_rect local = nk_layout_space_rect_to_screen(&context, screen); lua_pushnumber(L, local.x); lua_pushnumber(L, local.y); @@ -1656,9 +1745,8 @@ static int nk_love_layout_space_rect_to_local(lua_State *L) static int nk_love_layout_ratio_from_pixel(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.layoutRatioFromPixel: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.layoutRatioFromPixel: arg 1 should be a number"); - float pixel_width = lua_tonumber(L, 1); + nk_love_assert_argc(lua_gettop(L) == 1); + float pixel_width = luaL_checknumber(L, 1); float ratio = nk_layout_ratio_from_pixel(&context, pixel_width); lua_pushnumber(L, ratio); return 1; @@ -1666,9 +1754,8 @@ static int nk_love_layout_ratio_from_pixel(lua_State *L) static int nk_love_group_begin(lua_State *L) { - nk_love_assert(lua_gettop(L) >= 1, "nk.groupBegin: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.groupBegin: arg 1 should be a string"); - const char *title = lua_tostring(L, 1); + nk_love_assert_argc(lua_gettop(L) >= 1); + const char *title = luaL_checkstring(L, 1); nk_flags flags = nk_love_parse_window_flags(2); int open = nk_group_begin(&context, title, flags); lua_pushboolean(L, open); @@ -1677,7 +1764,7 @@ static int nk_love_group_begin(lua_State *L) static int nk_love_group_end(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.groupEnd: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_group_end(&context); return 0; } @@ -1685,57 +1772,34 @@ static int nk_love_group_end(lua_State *L) static int nk_love_tree_push(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc >= 2 && argc <= 4, "nk.treePush: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.treePush: arg 1 should be a string"); - const char *type_string = lua_tostring(L, 1); - enum nk_tree_type type; - if (!strcmp(type_string, "node")) { - type = NK_TREE_NODE; - } else if (!strcmp(type_string, "tab")) { - type = NK_TREE_TAB; - } else { - nk_love_error("nk.treePush: arg 1 should be 'node' or 'tab'"); - } - nk_love_assert(lua_type(L, 2) == LUA_TSTRING, "nk.treePush: arg 2 should be a string"); - const char *title = lua_tostring(L, 2); + nk_love_assert_argc(argc >= 2 && argc <= 4); + enum nk_tree_type type = nk_love_checktree(1); + const char *title = luaL_checkstring(L, 2); struct nk_image image; int use_image = 0; - if (argc >= 3 && lua_type(L, 3) != LUA_TNIL) { - nk_love_assert_type(3, "Image", "nk.treePush: arg 3 should be an image"); - lua_pushvalue(L, 3); - nk_love_toimage(&image); + if (argc >= 3 && !lua_isnil(L, 3)) { + nk_love_checkImage(3, &image); use_image = 1; } - const char *state_string = "collapsed"; - if (argc >= 4) { - nk_love_assert(lua_type(L, 4) == LUA_TSTRING, "nk.treePush: arg 4 should be a string"); - state_string = lua_tostring(L, 4); - } - enum nk_collapse_states state; - if (!strcmp(state_string, "collapsed")) { - state = NK_MINIMIZED; - } else if (!strcmp(state_string, "expanded")) { - state = NK_MAXIMIZED; - } else { - nk_love_error("nk.treePush: arg 4 should be 'collapsed' or 'expanded'"); - } + enum nk_collapse_states state = NK_MINIMIZED; + if (argc >= 4) + state = nk_love_checkstate(4); lua_Debug ar; lua_getstack(L, 1, &ar); lua_getinfo(L, "l", &ar); int id = ar.currentline; int open = 0; - if (use_image) { + if (use_image) open = nk_tree_image_push_hashed(&context, type, image, title, state, title, strlen(title), id); - } else { + else open = nk_tree_push_hashed(&context, type, title, state, title, strlen(title), id); - } lua_pushboolean(L, open); return 1; } static int nk_love_tree_pop(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.treePop: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_tree_pop(&context); return 0; } @@ -1758,20 +1822,13 @@ static void nk_love_color(int r, int g, int b, int a, char *color_string) static int nk_love_color_rgba(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc == 3 || argc == 4, "nk.colorRGBA: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.colorRGBA: arg 1 should be a number"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.colorRGBA: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.colorRGBA: arg 3 should be a number"); - - int r = lua_tointeger(L, 1); - int g = lua_tointeger(L, 2); - int b = lua_tointeger(L, 3); + nk_love_assert_argc(argc == 3 || argc == 4); + int r = luaL_checkint(L, 1); + int g = luaL_checkint(L, 2); + int b = luaL_checkint(L, 3); int a = 255; - if (argc == 4) { - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.colorRGBA: arg 4 should be a number"); - a = lua_tointeger(L, 4); - } - + if (argc == 4) + a = luaL_checkint(L, 4); char color_string[10]; nk_love_color(r, g, b, a, color_string); lua_pushstring(L, color_string); @@ -1781,20 +1838,13 @@ static int nk_love_color_rgba(lua_State *L) static int nk_love_color_hsva(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc == 3 || argc == 4, "nk.colorHSVA: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.colorHSVA: arg 1 should be a number"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.colorHSVA: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.colorHSVA: arg 3 should be a number"); - - int h = NK_CLAMP(0, lua_tointeger(L, 1), 255); - int s = NK_CLAMP(0, lua_tointeger(L, 2), 255); - int v = NK_CLAMP(0, lua_tointeger(L, 3), 255); + nk_love_assert_argc(argc == 3 || argc == 4); + int h = NK_CLAMP(0, luaL_checkint(L, 1), 255); + int s = NK_CLAMP(0, luaL_checkint(L, 2), 255); + int v = NK_CLAMP(0, luaL_checkint(L, 3), 255); int a = 255; - if (argc == 4) { - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.colorHSVA: arg 4 should be a number"); - a = NK_CLAMP(0, lua_tointeger(L, 4), 255); - } - + if (argc == 4) + a = NK_CLAMP(0, luaL_checkint(L, 4), 255); struct nk_color rgba = nk_hsva(h, s, v, a); char color_string[10]; nk_love_color(rgba.r, rgba.g, rgba.b, rgba.a, color_string); @@ -1802,24 +1852,10 @@ static int nk_love_color_hsva(lua_State *L) return 1; } -static struct nk_color nk_love_color_parse(const char *color_string) -{ - int r, g, b, a = 255; - sscanf(color_string, "#%02x%02x%02x", &r, &g, &b); - if (strlen(color_string) == 9) { - sscanf(color_string + 7, "%02x", &a); - } - struct nk_color color = {r, g, b, a}; - return color; -} - - static int nk_love_color_parse_rgba(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.colorParseRGBA: wrong number of arguments"); - nk_love_assert_color(1, "nk.colorParseRGBA: arg 1 should be a color string"); - const char *color_string = lua_tostring(L, 1); - struct nk_color rgba = nk_love_color_parse(color_string); + nk_love_assert_argc(lua_gettop(L) == 1); + struct nk_color rgba = nk_love_checkcolor(1); lua_pushnumber(L, rgba.r); lua_pushnumber(L, rgba.g); lua_pushnumber(L, rgba.b); @@ -1829,10 +1865,8 @@ static int nk_love_color_parse_rgba(lua_State *L) static int nk_love_color_parse_hsva(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.colorParseHSVA: wrong number of arguments"); - nk_love_assert_color(1, "nk.colorParseHSVA: arg 1 should be a color string"); - const char *color_string = lua_tostring(L, 1); - struct nk_color rgba = nk_love_color_parse(color_string); + nk_love_assert_argc(lua_gettop(L) == 1); + struct nk_color rgba = nk_love_checkcolor(1); int h, s, v, a2; nk_color_hsva_i(&h, &s, &v, &a2, rgba); lua_pushnumber(L, h); @@ -1845,41 +1879,33 @@ static int nk_love_color_parse_hsva(lua_State *L) static int nk_love_label(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc >= 1 && argc <= 3, "nk.label: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING || lua_type(L, 1) == LUA_TNUMBER, "nk.label: arg 1 should be a string"); - const char *text = lua_tostring(L, 1); - const char *align_string = "left"; - const char *color_string = NULL; - if (argc >= 2) { - nk_love_assert(lua_type(L, 2) == LUA_TSTRING, "nk.label: arg 2 should be a string"); - align_string = lua_tostring(L, 2); - if (argc >= 3) { - nk_love_assert_color(3, "nk.label: arg 3 should be a color string"); - color_string = lua_tostring(L, 3); - } - } - nk_flags align; + nk_love_assert_argc(argc >= 1 && argc <= 3); + const char *text = luaL_checkstring(L, 1); + nk_flags align = NK_TEXT_LEFT; int wrap = 0; - if (!strcmp(align_string, "wrap")) { - wrap = 1; - } else if (!nk_love_parse_align(align_string, &align)) { - nk_love_error("nk.label: arg 2 should be an alignment or 'wrap'"); - } - struct nk_color color; - if (color_string != NULL) { - color = nk_love_color_parse(color_string); - if (wrap) { + int use_color = 0; + if (argc >= 2) { + const char *align_string = luaL_checkstring(L, 2); + if (!strcmp(align_string, "wrap")) + wrap = 1; + else + align = nk_love_checkalign(2); + if (argc >= 3) { + color = nk_love_checkcolor(3); + use_color = 1; + } + } + if (use_color) { + if (wrap) nk_label_colored_wrap(&context, text, color); - } else { + else nk_label_colored(&context, text, align, color); - } } else { - if (wrap) { + if (wrap) nk_label_wrap(&context, text); - } else { + else nk_label(&context, text, align); - } } return 0; } @@ -1887,22 +1913,16 @@ static int nk_love_label(lua_State *L) static int nk_love_image(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc == 1 || argc == 5, "nk.image: wrong number of arguments"); - nk_love_assert_type(1, "Image", "nk.image: arg 1 should be an image"); + nk_love_assert_argc(argc == 1 || argc == 5); struct nk_image image; - lua_pushvalue(L, 1); - nk_love_toimage(&image); + nk_love_checkImage(1, &image); if (argc == 1) { nk_image(&context, image); } else { - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.image: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.image: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.image: arg 4 should be a number"); - nk_love_assert(lua_type(L, 5) == LUA_TNUMBER, "nk.image: arg 5 should be a number"); - float x = lua_tonumber(L, 2); - float y = lua_tonumber(L, 3); - float w = lua_tonumber(L, 4); - float h = lua_tonumber(L, 5); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float w = luaL_checknumber(L, 4); + float h = luaL_checknumber(L, 5); float line_thickness; struct nk_color color; nk_love_getGraphics(&line_thickness, &color); @@ -1914,53 +1934,47 @@ static int nk_love_image(lua_State *L) static int nk_love_button(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc == 1 || argc == 2, "nk.button: wrong number of arguments"); + nk_love_assert_argc(argc >= 1 && argc <= 2); const char *title = NULL; - if (lua_type(L, 1) != LUA_TNIL) { - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.button: arg 1 should be a string"); - title = lua_tostring(L, 1); - } + if (!lua_isnil(L, 1)) + title = luaL_checkstring(L, 1); int use_color = 0, use_image = 0; struct nk_color color; enum nk_symbol_type symbol = NK_SYMBOL_NONE; struct nk_image image; - if (argc >= 2 && lua_type(L, 2) != LUA_TNIL) { - if (lua_type(L, 2) == LUA_TSTRING) { - const char *s = lua_tostring(L, 2); - if (!nk_love_parse_symbol(s, &symbol)) { - nk_love_assert_color(2, "nk.button: arg 2 should be a color string, symbol type, or image"); - color = nk_love_color_parse(s); + if (argc >= 2 && !lua_isnil(L, 2)) { + if (lua_isstring(L, 2)) { + if (nk_love_is_color(2)) { + color = nk_love_checkcolor(2); use_color = 1; + } else { + symbol = nk_love_checksymbol(2); } } else { - nk_love_assert_type(2, "Image", "nk.button: arg 2 should be a color string, symbol type, or image"); - lua_pushvalue(L, 2); - nk_love_toimage(&image); + nk_love_checkImage(2, &image); use_image = 1; } } nk_flags align = context.style.button.text_alignment; int activated = 0; if (title != NULL) { - if (use_color) { - nk_love_error("nk.button: color buttons can't have titles"); - } else if (symbol != NK_SYMBOL_NONE) { + if (use_color) + nk_love_assert(0, "%s: color buttons can't have titles"); + else if (symbol != NK_SYMBOL_NONE) activated = nk_button_symbol_label(&context, symbol, title, align); - } else if (use_image) { + else if (use_image) activated = nk_button_image_label(&context, image, title, align); - } else { + else activated = nk_button_label(&context, title); - } } else { - if (use_color) { + if (use_color) activated = nk_button_color(&context, color); - } else if (symbol != NK_SYMBOL_NONE) { + else if (symbol != NK_SYMBOL_NONE) activated = nk_button_symbol(&context, symbol); - } else if (use_image) { + else if (use_image) activated = nk_button_image(&context, image); - } else { - nk_love_error("nk.button: must specify a title, color, symbol, and/or image"); - } + else + nk_love_assert(0, "%s: must specify a title, color, symbol, and/or image"); } lua_pushboolean(L, activated); return 1; @@ -1968,55 +1982,36 @@ static int nk_love_button(lua_State *L) static int nk_love_button_set_behavior(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.buttonSetBehavior: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.buttonSetBehavior: arg 1 should be 'default' or 'repeater'"); - const char *behavior_string = lua_tostring(L, 1); - enum nk_button_behavior behavior; - if (!strcmp(behavior_string, "default")) { - behavior = NK_BUTTON_DEFAULT; - } else if (!strcmp(behavior_string, "repeater")) { - behavior = NK_BUTTON_REPEATER; - } else { - nk_love_error("nk.buttonSetBehavior: arg 1 should be 'default' or 'repeater'"); - } + nk_love_assert_argc(lua_gettop(L) == 1); + enum nk_button_behavior behavior = nk_love_checkbehavior(1); nk_button_set_behavior(&context, behavior); return 0; } static int nk_love_button_push_behavior(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.buttonPushBehavior: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.buttonPushBehavior: arg 1 should be 'default' or 'repeater'"); - const char *behavior_string = lua_tostring(L, 1); - enum nk_button_behavior behavior; - if (!strcmp(behavior_string, "default")) { - behavior = NK_BUTTON_DEFAULT; - } else if (!strcmp(behavior_string, "repeater")) { - behavior = NK_BUTTON_REPEATER; - } else { - nk_love_error("nk.buttonPushBehavior: arg 1 should be 'default' or 'repeater'"); - } + nk_love_assert_argc(lua_gettop(L) == 1); + enum nk_button_behavior behavior = nk_love_checkbehavior(1); nk_button_push_behavior(&context, behavior); return 0; } static int nk_love_button_pop_behavior(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.buttonPopBehavior: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_button_pop_behavior(&context); return 0; } static int nk_love_checkbox(lua_State *L) { - nk_love_assert(lua_gettop(L) == 2, "nk.checkbox: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.checkbox: arg 1 should be a string"); - const char *text = lua_tostring(L, 1); - if (lua_type(L, 2) == LUA_TBOOLEAN) { + nk_love_assert_argc(lua_gettop(L) == 2); + const char *text = luaL_checkstring(L, 1); + if (lua_isboolean(L, 2)) { int value = lua_toboolean(L, 2); value = nk_check_label(&context, text, value); lua_pushboolean(L, value); - } else if (lua_type(L, 2) == LUA_TTABLE) { + } else if (lua_istable(L, 2)) { lua_getfield(L, 2, "value"); int value = lua_toboolean(L, -1); int changed = nk_checkbox_label(&context, text, &value); @@ -2026,7 +2021,7 @@ static int nk_love_checkbox(lua_State *L) } lua_pushboolean(L, changed); } else { - nk_love_error("nk.checkbox: arg 2 should be a boolean or table"); + luaL_typerror(L, 2, "boolean or table"); } return 1; } @@ -2034,27 +2029,25 @@ static int nk_love_checkbox(lua_State *L) static int nk_love_radio(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc == 2 || argc == 3, "nk.radio: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.radio: arg 1 should be a string"); - const char *name = lua_tostring(L, 1); + nk_love_assert_argc(argc == 2 || argc == 3); + const char *name = luaL_checkstring(L, 1); const char *text; - if (argc == 3) { - nk_love_assert(lua_type(L, 2) == LUA_TSTRING, "nk.radio: arg 2 should be a string"); - text = lua_tostring(L, 2); - } else { - text = lua_tostring(L, 1); - } - if (lua_type(L, -1) == LUA_TSTRING) { + if (argc == 3) + text = luaL_checkstring(L, 2); + else + text = luaL_checkstring(L, 1); + if (lua_isstring(L, -1)) { const char *value = lua_tostring(L, -1); int active = !strcmp(value, name); active = nk_option_label(&context, text, active); - if (active) { + if (active) lua_pushstring(L, name); - } else { + else lua_pushstring(L, value); - } - } else if (lua_type(L, -1) == LUA_TTABLE) { + } else if (lua_istable(L, -1)) { lua_getfield(L, -1, "value"); + if (!lua_isstring(L, -1)) + luaL_argerror(L, argc, "should have a string value"); const char *value = lua_tostring(L, -1); int active = !strcmp(value, name); int changed = nk_radio_label(&context, text, &active); @@ -2064,11 +2057,7 @@ static int nk_love_radio(lua_State *L) } lua_pushboolean(L, changed); } else { - if (argc == 2) { - nk_love_error("nk.radio: arg 2 should be a boolean or table"); - } else { - nk_love_error("nk.radio: arg 3 should be a boolean or table"); - } + luaL_typerror(L, argc, "string or table"); } return 1; } @@ -2076,74 +2065,59 @@ static int nk_love_radio(lua_State *L) static int nk_love_selectable(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc >= 2 && argc <= 4, "nk.selectable: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.selectable: arg 1 should be a string"); - const char *text = lua_tostring(L, 1); + nk_love_assert_argc(argc >= 2 && argc <= 4); + const char *text = luaL_checkstring(L, 1); struct nk_image image; int use_image = 0; - if (argc >= 3 && lua_type(L, 2) != LUA_TNIL) { - nk_love_assert_type(2, "Image", "nk.selectable: arg 2 should be an image"); - lua_pushvalue(L, 2); - nk_love_toimage(&image); + if (argc >= 3 && !lua_isnil(L, 2)) { + nk_love_checkImage(2, &image); use_image = 1; } nk_flags align = NK_TEXT_LEFT; - if (argc >= 4) { - nk_love_assert(lua_type(L, 3) == LUA_TSTRING, "nk.selectable: arg 3 should be a string"); - const char *align_text = lua_tostring(L, 3); - if (!nk_love_parse_align(align_text, &align)) { - nk_love_error("nk.selectable: arg 3 should be an alignment"); - } - } - if (lua_type(L, -1) == LUA_TBOOLEAN) { + if (argc >= 4) + align = nk_love_checkalign(3); + if (lua_isboolean(L, -1)) { int value = lua_toboolean(L, -1); - if (use_image) { + if (use_image) value = nk_select_image_label(&context, image, text, align, value); - } else { + else value = nk_select_label(&context, text, align, value); - } lua_pushboolean(L, value); - } else if (lua_type(L, -1) == LUA_TTABLE) { + } else if (lua_istable(L, -1)) { lua_getfield(L, -1, "value"); + if (!lua_isboolean(L, -1)) + luaL_argerror(L, argc, "should have a boolean value"); int value = lua_toboolean(L, -1); int changed; - if (use_image) { + if (use_image) changed = nk_selectable_image_label(&context, image, text, align, &value); - } else { + else changed = nk_selectable_label(&context, text, align, &value); - } if (changed) { lua_pushboolean(L, value); lua_setfield(L, -3, "value"); } lua_pushboolean(L, changed); } else { - if (argc == 2) { - nk_love_error("nk.selectable: arg 2 should be a boolean or table"); - } else if (argc == 3) { - nk_love_error("nk.selectable: arg 3 should be a boolean or table"); - } else { - nk_love_error("nk.selectable: arg 4 should be a boolean or table"); - } + luaL_typerror(L, argc, "boolean or table"); } return 1; } static int nk_love_slider(lua_State *L) { - nk_love_assert(lua_gettop(L) == 4, "nk.slider: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.slider: arg 1 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.slider: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.slider: arg 4 should be a number"); - float min = lua_tonumber(L, 1); - float max = lua_tonumber(L, 3); - float step = lua_tonumber(L, 4); - if (lua_type(L, 2) == LUA_TNUMBER) { + nk_love_assert_argc(lua_gettop(L) == 4); + float min = luaL_checknumber(L, 1); + float max = luaL_checknumber(L, 3); + float step = luaL_checknumber(L, 4); + if (lua_isnumber(L, 2)) { float value = lua_tonumber(L, 2); value = nk_slide_float(&context, min, value, max, step); lua_pushnumber(L, value); - } else if (lua_type(L, 2) == LUA_TTABLE) { + } else if (lua_istable(L, 2)) { lua_getfield(L, 2, "value"); + if (!lua_isnumber(L, -1)) + luaL_argerror(L, 2, "should have a number value"); float value = lua_tonumber(L, -1); int changed = nk_slider_float(&context, min, &value, max, step); if (changed) { @@ -2152,7 +2126,7 @@ static int nk_love_slider(lua_State *L) } lua_pushboolean(L, changed); } else { - nk_love_error("nk.slider: arg 2 should be a number or table"); + luaL_typerror(L, 2, "number or table"); } return 1; } @@ -2160,21 +2134,20 @@ static int nk_love_slider(lua_State *L) static int nk_love_progress(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc == 2 || argc == 3, "nk.progress: wrong number of arguments"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.progress: arg 2 should be a number"); - nk_size max = lua_tonumber(L, 2); + nk_love_assert_argc(argc >= 2 || argc <= 3); + nk_size max = luaL_checklong(L, 2); int modifiable = 0; - if (argc == 3) { - nk_love_assert(lua_type(L, 3) == LUA_TBOOLEAN || lua_type(L, 3) == LUA_TNIL, "nk.progress: arg 3 should be a boolean"); - modifiable = lua_toboolean(L, 3); - } - if (lua_type(L, 1) == LUA_TNUMBER) { + if (argc >= 3 && !lua_isnil(L, 3)) + modifiable = nk_love_checkboolean(L, 3); + if (lua_isnumber(L, 1)) { nk_size value = lua_tonumber(L, 1); value = nk_prog(&context, value, max, modifiable); lua_pushnumber(L, value); - } else if (lua_type(L, 1) == LUA_TTABLE) { + } else if (lua_istable(L, 1)) { lua_getfield(L, 1, "value"); - nk_size value = lua_tonumber(L, -1); + if (!lua_isnumber(L, -1)) + luaL_argerror(L, 1, "should have a number value"); + nk_size value = (nk_size) lua_tonumber(L, -1); int changed = nk_progress(&context, &value, max, modifiable); if (changed) { lua_pushnumber(L, value); @@ -2182,7 +2155,7 @@ static int nk_love_progress(lua_State *L) } lua_pushboolean(L, changed); } else { - nk_love_error("nk.progress: arg 2 should be a number or table"); + luaL_typerror(L, 1, "number or table"); } return 1; } @@ -2190,33 +2163,21 @@ static int nk_love_progress(lua_State *L) static int nk_love_color_picker(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc == 1 || argc == 2, "nk.colorPicker: wrong number of arguments"); - const char *format_string = "RGB"; - if (argc == 2) { - nk_love_assert(lua_type(L, 2) == LUA_TSTRING, "nk.colorPicker: arg 2 should be a string"); - format_string = lua_tostring(L, 2); - } - enum nk_color_format format; - if (!strcmp(format_string, "RGB")) { - format = NK_RGB; - } else if (!strcmp(format_string, "RGBA")) { - format = NK_RGBA; - } else { - nk_love_error("nk.colorPicker: arg 2 should be 'RGB' or 'RGBA'"); - } - if (lua_type(L, 1) == LUA_TSTRING) { - nk_love_assert_color(1, "nk.colorPicker: arg 1 should be a color string"); - const char *color_string = lua_tostring(L, 1); - struct nk_color color = nk_love_color_parse(color_string); + nk_love_assert_argc(argc >= 1 && argc <= 2); + enum nk_color_format format = NK_RGB; + if (argc >= 2) + format = nk_love_checkcolorformat(2); + if (lua_isstring(L, 1)) { + struct nk_color color = nk_love_checkcolor(1); color = nk_color_picker(&context, color, format); char new_color_string[10]; nk_love_color(color.r, color.g, color.b, color.a, new_color_string); lua_pushstring(L, new_color_string); - } else if (lua_type(L, 1) == LUA_TTABLE) { + } else if (lua_istable(L, 1)) { lua_getfield(L, 1, "value"); - nk_love_assert_color(-1, "nk.colorPicker: arg 1 should have a color string value"); - const char *color_string = lua_tostring(L, -1); - struct nk_color color = nk_love_color_parse(color_string); + if (!nk_love_is_color(-1)) + luaL_argerror(L, 1, "should have a color string value"); + struct nk_color color = nk_love_checkcolor(-1); int changed = nk_color_pick(&context, &color, format); if (changed) { char new_color_string[10]; @@ -2225,30 +2186,28 @@ static int nk_love_color_picker(lua_State *L) lua_setfield(L, 1, "value"); } lua_pushboolean(L, changed); + } else { + luaL_typerror(L, 1, "string or table"); } return 1; } static int nk_love_property(lua_State *L) { - nk_love_assert(lua_gettop(L) == 6, "nk.property: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.property: arg 1 should be a string"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.property: arg 2 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.property: arg 4 should be a number"); - nk_love_assert(lua_type(L, 5) == LUA_TNUMBER, "nk.property: arg 5 should be a number"); - nk_love_assert(lua_type(L, 6) == LUA_TNUMBER, "nk.property: arg 6 should be a number"); - const char *name = lua_tostring(L, 1); - double min = lua_tonumber(L, 2); - double max = lua_tonumber(L, 4); - double step = lua_tonumber(L, 5); - float inc_per_pixel = lua_tonumber(L, 6); - if (lua_type(L, 3) == LUA_TNUMBER) { + nk_love_assert_argc(lua_gettop(L) == 6); + const char *name = luaL_checkstring(L, 1); + double min = luaL_checknumber(L, 2); + double max = luaL_checknumber(L, 4); + double step = luaL_checknumber(L, 5); + float inc_per_pixel = luaL_checknumber(L, 6); + if (lua_isnumber(L, 3)) { double value = lua_tonumber(L, 3); value = nk_propertyd(&context, name, min, value, max, step, inc_per_pixel); lua_pushnumber(L, value); - } else if (lua_type(L, 3) == LUA_TTABLE) { + } else if (lua_istable(L, 3)) { lua_getfield(L, 3, "value"); - nk_love_assert(lua_type(L, -1) == LUA_TNUMBER, "nk.property: arg 3 should have a number value"); + if (!lua_isnumber(L, -1)) + luaL_argerror(L, 3, "should have a number value"); double value = lua_tonumber(L, -1); double old = value; nk_property_double(&context, name, min, &value, max, step, inc_per_pixel); @@ -2259,29 +2218,20 @@ static int nk_love_property(lua_State *L) } lua_pushboolean(L, changed); } else { - nk_love_error("nk.property: arg 3 should be a number or table"); + luaL_typerror(L, 3, "number or table"); } return 1; } static int nk_love_edit(lua_State *L) { - nk_love_assert(lua_gettop(L) == 2, "nk.edit: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.edit: arg 1 should be a string"); - const char *type_string = lua_tostring(L, 1); - nk_flags flags; - if (!strcmp(type_string, "simple")) { - flags = NK_EDIT_SIMPLE; - } else if (!strcmp(type_string, "field")) { - flags = NK_EDIT_FIELD; - } else if (!strcmp(type_string, "box")) { - flags = NK_EDIT_BOX; - } else { - nk_love_error("nk.edit: arg 1 must be an editor type"); - } - nk_love_assert(lua_type(L, 2) == LUA_TTABLE, "nk.edit: arg 2 should be a table"); + nk_love_assert_argc(lua_gettop(L) == 2); + nk_flags flags = nk_love_checkedittype(1); + if (!lua_istable(L, 2)) + luaL_typerror(L, 2, "table"); lua_getfield(L, 2, "value"); - nk_love_assert(lua_type(L, -1) == LUA_TSTRING, "nk.edit: arg 2 should have a string value"); + if (!lua_isstring(L, -1)) + luaL_argerror(L, 2, "should have a string value"); const char *value = lua_tostring(L, -1); size_t len = NK_CLAMP(0, strlen(value), NK_LOVE_EDIT_BUFFER_LEN - 1); memcpy(edit_buffer, value, len); @@ -2291,47 +2241,32 @@ static int nk_love_edit(lua_State *L) lua_pushvalue(L, -1); lua_setfield(L, 2, "value"); int changed = !lua_equal(L, -1, -2); - if (event & NK_EDIT_COMMITED) { + if (event & NK_EDIT_COMMITED) lua_pushstring(L, "commited"); - } else if (event & NK_EDIT_ACTIVATED) { + else if (event & NK_EDIT_ACTIVATED) lua_pushstring(L, "activated"); - } else if (event & NK_EDIT_DEACTIVATED) { + else if (event & NK_EDIT_DEACTIVATED) lua_pushstring(L, "deactivated"); - } else if (event & NK_EDIT_ACTIVE) { + else if (event & NK_EDIT_ACTIVE) lua_pushstring(L, "active"); - } else if (event & NK_EDIT_INACTIVE) { + else if (event & NK_EDIT_INACTIVE) lua_pushstring(L, "inactive"); - } else { + else lua_pushnil(L); - } lua_pushboolean(L, changed); return 2; } static int nk_love_popup_begin(lua_State *L) { - nk_love_assert(lua_gettop(L) >= 6, "nk.popupBegin: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.popupBegin: arg 1 should be a string"); - nk_love_assert(lua_type(L, 2) == LUA_TSTRING, "nk.popupBegin: arg 2 should be a string"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.popupBegin: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.popupBegin: arg 4 should be a number"); - nk_love_assert(lua_type(L, 5) == LUA_TNUMBER, "nk.popupBegin: arg 5 should be a number"); - nk_love_assert(lua_type(L, 6) == LUA_TNUMBER, "nk.popupBegin: arg 6 should be a number"); - const char *type_string = lua_tostring(L, 1); - enum nk_popup_type type; - if (!strcmp(type_string, "dynamic")) { - type = NK_POPUP_DYNAMIC; - } else if (!strcmp(type_string, "static")) { - type = NK_POPUP_STATIC; - } else { - nk_love_error("nk.popupBegin: arg 1 should be 'dynamic' or 'static'"); - } - const char *title = lua_tostring(L, 2); + nk_love_assert_argc(lua_gettop(L) >= 6); + enum nk_popup_type type = nk_love_checkpopup(1); + const char *title = luaL_checkstring(L, 2); struct nk_rect bounds; - bounds.x = lua_tonumber(L, 3); - bounds.y = lua_tonumber(L, 4); - bounds.w = lua_tonumber(L, 5); - bounds.h = lua_tonumber(L, 6); + bounds.x = luaL_checknumber(L, 3); + bounds.y = luaL_checknumber(L, 4); + bounds.w = luaL_checknumber(L, 5); + bounds.h = luaL_checknumber(L, 6); nk_flags flags = nk_love_parse_window_flags(7); int open = nk_popup_begin(&context, type, title, flags, bounds); lua_pushboolean(L, open); @@ -2340,14 +2275,14 @@ static int nk_love_popup_begin(lua_State *L) static int nk_love_popup_close(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.popupClose: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_popup_close(&context); return 0; } static int nk_love_popup_end(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.popupEnd: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_popup_end(&context); return 0; } @@ -2355,41 +2290,36 @@ static int nk_love_popup_end(lua_State *L) static int nk_love_combobox(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc >= 2 && argc <= 5, "nk.combobox: wrong number of arguments"); - nk_love_assert(lua_type(L, 2) == LUA_TTABLE, "nk.combobox: arg 2 should be a table"); + nk_love_assert_argc(argc >= 2 && argc <= 5); + if (!lua_istable(L, 2)) + luaL_typerror(L, 2, "table"); int i; for (i = 0; i < NK_LOVE_COMBOBOX_MAX_ITEMS && lua_checkstack(L, 4); ++i) { lua_rawgeti(L, 2, i + 1); - if (lua_type(L, -1) == LUA_TSTRING) { + if (lua_isstring(L, -1)) combobox_items[i] = lua_tostring(L, -1); - } else if (lua_type(L, -1) == LUA_TNIL) { + else if (lua_isnil(L, -1)) break; - } else { - nk_love_error("nk.combobox: items must be strings"); - } + else + luaL_argerror(L, 2, "items must be strings"); } struct nk_rect bounds = nk_widget_bounds(&context); int item_height = bounds.h; - if (argc >= 3 && lua_type(L, 3) != LUA_TNIL) { - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.combobox: arg 3 should be a number"); - item_height = lua_tointeger(L, 3); - } + if (argc >= 3 && !lua_isnil(L, 3)) + item_height = luaL_checkint(L, 3); struct nk_vec2 size = nk_vec2(bounds.w, item_height * 8); - if (argc >= 4 && lua_type(L, 4) != LUA_TNIL) { - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.combobox: arg 4 should be a number"); - size.x = lua_tonumber(L, 4); - } - if (argc >= 5 && lua_type(L, 5) != LUA_TNIL) { - nk_love_assert(lua_type(L, 5) == LUA_TNUMBER, "nk.combobox: arg 5 should be a number"); - size.y = lua_tonumber(L, 5); - } - if (lua_type(L, 1) == LUA_TNUMBER) { + if (argc >= 4 && !lua_isnil(L, 4)) + size.x = luaL_checknumber(L, 4); + if (argc >= 5 && !lua_isnil(L, 5)) + size.y = luaL_checknumber(L, 5); + if (lua_isnumber(L, 1)) { int value = lua_tointeger(L, 1) - 1; value = nk_combo(&context, combobox_items, i, value, item_height, size); lua_pushnumber(L, value + 1); - } else if (lua_type(L, 1) == LUA_TTABLE) { + } else if (lua_istable(L, 1)) { lua_getfield(L, 1, "value"); - nk_love_assert(lua_type(L, -1) == LUA_TNUMBER, "nk.combobox: arg 1 should have a number value"); + if (!lua_isnumber(L, -1)) + luaL_argerror(L, 1, "should have a number value"); int value = lua_tointeger(L, -1) - 1; int old = value; nk_combobox(&context, combobox_items, i, &value, item_height, size); @@ -2400,7 +2330,7 @@ static int nk_love_combobox(lua_State *L) } lua_pushboolean(L, changed); } else { - nk_love_error("nk.combobox: arg 1 should be a number or table"); + luaL_typerror(L, 1, "number or table"); } return 1; } @@ -2408,60 +2338,53 @@ static int nk_love_combobox(lua_State *L) static int nk_love_combobox_begin(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc >= 1 && argc <= 4, "nk.comboboxBegin: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING || lua_type(L, 1) == LUA_TNUMBER || lua_type(L, 1) == LUA_TNIL, "nk.comboboxBegin: arg 1 should be a string"); - const char *text = lua_tostring(L, 1); + nk_love_assert_argc(argc >= 1 && argc <= 4); + const char *text = NULL; + if (!lua_isnil(L, 1)) + text = luaL_checkstring(L, 1); struct nk_color color; int use_color = 0; enum nk_symbol_type symbol = NK_SYMBOL_NONE; struct nk_image image; int use_image = 0; - if (argc >= 2 && lua_type(L, 2) != LUA_TNIL) { - if (lua_type(L, 2) == LUA_TSTRING) { - const char *s = lua_tostring(L, 2); - if (!nk_love_parse_symbol(s, &symbol)) { - nk_love_assert_color(2, "nk.comboboxBegin: arg 1 should be a color string, symbol type, or image"); - color = nk_love_color_parse(s); + if (argc >= 2 && !lua_isnil(L, 2)) { + if (lua_isstring(L, 2)) { + if (nk_love_is_color(2)) { + color = nk_love_checkcolor(2); use_color = 1; + } else { + symbol = nk_love_checksymbol(2); } } else { - nk_love_assert_type(2, "Image", "nk.comboboxBegin: arg 1 should be a color string, symbol type, or image"); - lua_pushvalue(L, 2); - nk_love_toimage(&image); + nk_love_checkImage(2, &image); use_image = 1; } } struct nk_rect bounds = nk_widget_bounds(&context); struct nk_vec2 size = nk_vec2(bounds.w, bounds.h * 8); - if (argc >= 3 && lua_type(L, 3) != LUA_TNIL) { - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.comboboxBegin: arg 3 should be a number"); - size.x = lua_tonumber(L, 3); - } - if (argc >= 4 && lua_type(L, 4) != LUA_TNIL) { - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.comboboxBegin: arg 4 should be a number"); - size.y = lua_tonumber(L, 4); - } + if (argc >= 3 && !lua_isnil(L, 3)) + size.x = luaL_checknumber(L, 3); + if (argc >= 4 && !lua_isnil(L, 4)) + size.y = luaL_checknumber(L, 4); int open = 0; if (text != NULL) { - if (use_color) { - nk_love_error("nk.comboboxBegin: color comboboxes can't have titles"); - } else if (symbol != NK_SYMBOL_NONE) { + if (use_color) + nk_love_assert(0, "%s: color comboboxes can't have titles"); + else if (symbol != NK_SYMBOL_NONE) open = nk_combo_begin_symbol_label(&context, text, symbol, size); - } else if (use_image) { + else if (use_image) open = nk_combo_begin_image_label(&context, text, image, size); - } else { + else open = nk_combo_begin_label(&context, text, size); - } } else { - if (use_color) { + if (use_color) open = nk_combo_begin_color(&context, color, size); - } else if (symbol != NK_SYMBOL_NONE) { + else if (symbol != NK_SYMBOL_NONE) open = nk_combo_begin_symbol(&context, symbol, size); - } else if (use_image) { + else if (use_image) open = nk_combo_begin_image(&context, image, size); - } else { - nk_love_error("nk.comboboxBegin: must specify color, symbol, image, and/or title"); - } + else + nk_love_assert(0, "%s: must specify color, symbol, image, and/or title"); } lua_pushboolean(L, open); return 1; @@ -2470,74 +2393,58 @@ static int nk_love_combobox_begin(lua_State *L) static int nk_love_combobox_item(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc >= 1 && argc <= 3, "nk.comboboxItem: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING || lua_type(L, 1) == LUA_TNUMBER, "nk.comboboxItem: arg 1 should be a string"); - const char *text = lua_tostring(L, 1); + nk_love_assert_argc(argc >= 1 && argc <= 3); + const char *text = luaL_checkstring(L, 1); enum nk_symbol_type symbol = NK_SYMBOL_NONE; struct nk_image image; int use_image = 0; - if (argc >= 2 && lua_type(L, 2) != LUA_TNIL) { - if (lua_type(L, 2) == LUA_TSTRING) { - const char *s = lua_tostring(L, 2); - nk_love_assert(nk_love_parse_symbol(s, &symbol), "nk.comboboxItem: arg 2 should be a symbol type or image"); + if (argc >= 2 && !lua_isnil(L, 2)) { + if (lua_isstring(L, 2)) { + symbol = nk_love_checksymbol(2); } else { - nk_love_assert_type(2, "Image", "nk.comboboxItem: arg 1 should be a symbol type or image"); - lua_pushvalue(L, 2); - nk_love_toimage(&image); + nk_love_checkImage(2, &image); use_image = 1; } } nk_flags align = NK_TEXT_LEFT; - if (argc >= 3 && lua_type(L, 3) != LUA_TNIL) { - nk_love_assert(lua_type(L, 3) == LUA_TSTRING, "nk.comboboxItem: arg 3 should be a string"); - const char *align_string = lua_tostring(L, 3); - if (!nk_love_parse_align(align_string, &align)) { - nk_love_error("nk.comboboxItem: arg 3 should be an alignment"); - } - } + if (argc >= 3 && !lua_isnil(L, 3)) + align = nk_love_checkalign(3); int activated = 0; - if (symbol != NK_SYMBOL_NONE) { + if (symbol != NK_SYMBOL_NONE) activated = nk_combo_item_symbol_label(&context, symbol, text, align); - } else if (use_image) { + else if (use_image) activated = nk_combo_item_image_label(&context, image, text, align); - } else { + else activated = nk_combo_item_label(&context, text, align); - } lua_pushboolean(L, activated); return 1; } static int nk_love_combobox_close(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.comboboxClose: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_combo_close(&context); return 0; } static int nk_love_combobox_end(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.comboboxEnd: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_combo_end(&context); return 0; } static int nk_love_contextual_begin(lua_State *L) { - nk_love_assert(lua_gettop(L) >= 6, "nk.contextualBegin: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.contextualBegin: arg 1 should be a number"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.contextualBegin: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.contextualBegin: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.contextualBegin: arg 4 should be a number"); - nk_love_assert(lua_type(L, 5) == LUA_TNUMBER, "nk.contextualBegin: arg 5 should be a number"); - nk_love_assert(lua_type(L, 6) == LUA_TNUMBER, "nk.contextualBegin: arg 6 should be a number"); + nk_love_assert_argc(lua_gettop(L) >= 6); struct nk_vec2 size; - size.x = lua_tonumber(L, 1); - size.y = lua_tonumber(L, 2); + size.x = luaL_checknumber(L, 1); + size.y = luaL_checknumber(L, 2); struct nk_rect trigger; - trigger.x = lua_tonumber(L, 3); - trigger.y = lua_tonumber(L, 4); - trigger.w = lua_tonumber(L, 5); - trigger.h = lua_tonumber(L, 6); + trigger.x = luaL_checknumber(L, 3); + trigger.y = luaL_checknumber(L, 4); + trigger.w = luaL_checknumber(L, 5); + trigger.h = luaL_checknumber(L, 6); nk_flags flags = nk_love_parse_window_flags(7); int open = nk_contextual_begin(&context, flags, size, trigger); lua_pushboolean(L, open); @@ -2547,73 +2454,59 @@ static int nk_love_contextual_begin(lua_State *L) static int nk_love_contextual_item(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc >= 1 && argc <= 3, "nk.contextualItem: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.contextualItem: arg 1 should be a string"); - const char *text = lua_tostring(L, 1); + nk_love_assert_argc(argc >= 1 && argc <= 3); + const char *text = luaL_checkstring(L, 1); enum nk_symbol_type symbol = NK_SYMBOL_NONE; struct nk_image image; int use_image = 0; - if (argc >= 2 && lua_type(L, 2) != LUA_TNIL) { - if (lua_type(L, 2) == LUA_TSTRING) { - const char *symbol_string = lua_tostring(L, 2); - if (!nk_love_parse_symbol(symbol_string, &symbol)) { - nk_love_error("nk.contextualItem: arg 1 should be a symbol type or image"); - } + if (argc >= 2 && !lua_isnil(L, 2)) { + if (lua_isstring(L, 2)) { + symbol = nk_love_checksymbol(2); } else { - nk_love_assert_type(2, "Image", "nk.contextualItem: arg 1 should be a symbol type or image"); - lua_pushvalue(L, 2); - nk_love_toimage(&image); + nk_love_checkImage(2, &image); use_image = 1; } } nk_flags align = NK_TEXT_LEFT; - if (argc >= 3 && lua_type(L, 3) != LUA_TNIL) { - nk_love_assert(lua_type(L, 3) == LUA_TSTRING, "nk.contextualItem: arg 3 should be a string"); - const char *align_string = lua_tostring(L, 3); - if (!nk_love_parse_align(align_string, &align)) { - nk_love_error("nk.contextualItem: arg 3 should be an alignment"); - } - } + if (argc >= 3 && !lua_isnil(L, 3)) + align = nk_love_checkalign(3); int activated; - if (symbol != NK_SYMBOL_NONE) { + if (symbol != NK_SYMBOL_NONE) activated = nk_contextual_item_symbol_label(&context, symbol, text, align); - } else if (use_image) { + else if (use_image) activated = nk_contextual_item_image_label(&context, image, text, align); - } else { + else activated = nk_contextual_item_label(&context, text, align); - } lua_pushboolean(L, activated); return 1; } static int nk_love_contextual_close(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.contextualClose: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_contextual_close(&context); return 0; } static int nk_love_contextual_end(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.contextualEnd: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_contextual_end(&context); return 0; } static int nk_love_tooltip(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.tooltip: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.tooltip: arg 1 should be a string"); - const char *text = lua_tostring(L, 1); + nk_love_assert_argc(lua_gettop(L) == 1); + const char *text = luaL_checkstring(L, 1); nk_tooltip(&context, text); return 0; } static int nk_love_tooltip_begin(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.tooltipBegin: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.tooltipBegin: arg 1 should be a number"); - float width = lua_tonumber(L, 1); + nk_love_assert_argc(lua_gettop(L) == 1); + float width = luaL_checknumber(L, 1); int open = nk_tooltip_begin(&context, width); lua_pushnumber(L, open); return 1; @@ -2621,21 +2514,21 @@ static int nk_love_tooltip_begin(lua_State *L) static int nk_love_tooltip_end(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.tooltipEnd: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_tooltip_end(&context); return 0; } static int nk_love_menubar_begin(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.menubarBegin: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_menubar_begin(&context); return 0; } static int nk_love_menubar_end(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.menubarEnd: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_menubar_end(&context); return 0; } @@ -2643,44 +2536,30 @@ static int nk_love_menubar_end(lua_State *L) static int nk_love_menu_begin(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc == 4 || argc == 5, "nk.menuBegin: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.menuBegin: arg 1 should be a string"); - const char *text = lua_tostring(L, 1); + nk_love_assert_argc(argc >= 4 && argc <= 5); + const char *text = luaL_checkstring(L, 1); enum nk_symbol_type symbol = NK_SYMBOL_NONE; struct nk_image image; int use_image = 0; - if (lua_type(L, 2) == LUA_TSTRING) { - const char *symbol_string = lua_tostring(L, 2); - if (!nk_love_parse_symbol(symbol_string, &symbol)) { - nk_love_error("nk.menuBegin: arg 2 should be a symbol type or image"); - } - } else if (lua_type(L, 2) != LUA_TNIL) { - nk_love_assert_type(2, "Image", "nk.menuBegin: arg 2 should be a symbol type or image"); - lua_pushvalue(L, 2); - nk_love_toimage(&image); + if (lua_isstring(L, 2)) { + symbol = nk_love_checksymbol(2); + } else if (!lua_isnil(L, 2)) { + nk_love_checkImage(2, &image); use_image = 1; } struct nk_vec2 size; - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.menuBegin: arg 3 should be a number"); - size.x = lua_tonumber(L, 3); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.menuBegin: arg 4 should be a number"); - size.y = lua_tonumber(L, 4); + size.x = luaL_checknumber(L, 3); + size.y = luaL_checknumber(L, 4); nk_flags align = NK_TEXT_LEFT; - if (argc == 5 && lua_type(L, 5) != LUA_TNIL) { - nk_love_assert(lua_type(L, 5) == LUA_TSTRING, "nk.menuBegin: arg 5 should be a string"); - const char *align_string = lua_tostring(L, 5); - if (!nk_love_parse_align(align_string, &align)) { - nk_love_error("nk.menuBegin: arg 5 should be an alignment"); - } - } + if (argc >= 5 && !lua_isnil(L, 5)) + align = nk_love_checkalign(5); int open; - if (symbol != NK_SYMBOL_NONE) { + if (symbol != NK_SYMBOL_NONE) open = nk_menu_begin_symbol_label(&context, text, align, symbol, size); - } else if (use_image) { + else if (use_image) open = nk_menu_begin_image_label(&context, text, align, image, size); - } else { + else open = nk_menu_begin_label(&context, text, align, size); - } lua_pushboolean(L, open); return 1; } @@ -2688,76 +2567,65 @@ static int nk_love_menu_begin(lua_State *L) static int nk_love_menu_item(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc >= 1 && argc <= 3, "nk.menuItem: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.menuItem: arg 1 should be a string"); - const char *text = lua_tostring(L, 1); + nk_love_assert_argc(argc >= 1 && argc <= 3); + const char *text = luaL_checkstring(L, 1); enum nk_symbol_type symbol = NK_SYMBOL_NONE; struct nk_image image; int use_image = 0; - if (argc >= 2 && lua_type(L, 2) != LUA_TNIL) { - if (lua_type(L, 2) == LUA_TSTRING) { - const char *symbol_string = lua_tostring(L, 2); - if (!nk_love_parse_symbol(symbol_string, &symbol)) { - nk_love_error("nk.menuItem: arg 2 should be a symbol type or image"); - } + if (argc >= 2 && !lua_isnil(L, 2)) { + if (lua_isstring(L, 2)) { + symbol = nk_love_checksymbol(2); } else { - nk_love_assert_type(2, "Image", "nk.menuItem: arg 2 should be a symbol type or image"); - lua_pushvalue(L, 2); - nk_love_toimage(&image); + nk_love_checkImage(2, &image); use_image = 1; } } nk_flags align = NK_TEXT_LEFT; - if (argc >= 3 && lua_type(L, 3) != LUA_TNIL) { - nk_love_assert(lua_type(L, 3) == LUA_TSTRING, "nk.menuItem: arg 3 should be a string"); - const char *align_string = lua_tostring(L, 3); - if (!nk_love_parse_align(align_string, &align)) { - nk_love_error("nk.menuItem: arg 3 should be an alignment"); - } - } + if (argc >= 3 && !lua_isnil(L, 3)) + align = nk_love_checkalign(3); int activated; - if (symbol != NK_SYMBOL_NONE) { + if (symbol != NK_SYMBOL_NONE) activated = nk_menu_item_symbol_label(&context, symbol, text, align); - } else if (use_image) { + else if (use_image) activated = nk_menu_item_image_label(&context, image, text, align); - } else { + else activated = nk_menu_item_label(&context, text, align); - } lua_pushboolean(L, activated); return 1; } static int nk_love_menu_close(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.menuClose: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_menu_close(&context); return 0; } static int nk_love_menu_end(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.menuEnd: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_menu_end(&context); return 0; } static int nk_love_style_default(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.styleDefault: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); nk_style_default(&context); return 0; } #define NK_LOVE_LOAD_COLOR(type) \ lua_getfield(L, -1, (type)); \ - nk_love_assert_color(-1, "nk.styleLoadColors: table missing color value for '" type "'"); \ - colors[index++] = nk_love_color_parse(lua_tostring(L, -1)); \ + nk_love_assert(nk_love_is_color(-1), "%s: table missing color value for '" type "'"); \ + colors[index++] = nk_love_checkcolor(-1); \ lua_pop(L, 1); static int nk_love_style_load_colors(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.styleLoadColors: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TTABLE, "nk.styleLoadColors: arg 1 should be a table"); + nk_love_assert_argc(lua_gettop(L) == 1); + if (!lua_istable(L, 1)) + luaL_typerror(L, 1, "table"); struct nk_color colors[NK_COLOR_COUNT]; int index = 0; NK_LOVE_LOAD_COLOR("text"); @@ -2794,18 +2662,19 @@ static int nk_love_style_load_colors(lua_State *L) static int nk_love_style_set_font(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.styleSetFont: wrong number of arguments"); - nk_love_assert_type(1, "Font", "nk.styleSetFont: arg 1 should be a font"); - nk_love_tofont(&fonts[font_count]); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_checkFont(1, &fonts[font_count]); nk_style_set_font(&context, &fonts[font_count++]); return 0; } static int nk_love_style_push_color(struct nk_color *field) { - nk_love_assert_color(-1, "nk.stylePush: color fields must be color strings"); - const char *color_string = lua_tostring(L, -1); - struct nk_color color = nk_love_color_parse(color_string); + if (!nk_love_is_color(-1)) { + const char *msg = lua_pushfstring(L, "%%s: bad color string '%s'", lua_tostring(L, -1)); + nk_love_assert(0, msg); + } + struct nk_color color = nk_love_checkcolor(-1); int success = nk_style_push_color(&context, field, color); if (success) { lua_pushstring(L, "color"); @@ -2817,11 +2686,12 @@ static int nk_love_style_push_color(struct nk_color *field) static int nk_love_style_push_vec2(struct nk_vec2 *field) { - nk_love_assert(lua_type(L, -1) == LUA_TTABLE, "nk.stylePush: vec2 fields must have x and y components"); + static const char *msg = "%s: vec2 fields must have x and y components"; + nk_love_assert(lua_istable(L, -1), msg); lua_getfield(L, -1, "x"); - nk_love_assert(lua_type(L, -1) == LUA_TNUMBER, "nk.stylePush: vec2 fields must have x and y components"); + nk_love_assert(lua_isnumber(L, -1), msg); lua_getfield(L, -2, "y"); - nk_love_assert(lua_type(L, -1) == LUA_TNUMBER, "nk.stylePush: vec2 fields must have x and y components"); + nk_love_assert(lua_isnumber(L, -1), msg); struct nk_vec2 vec2; vec2.x = lua_tonumber(L, -2); vec2.y = lua_tonumber(L, -1); @@ -2838,16 +2708,16 @@ static int nk_love_style_push_vec2(struct nk_vec2 *field) static int nk_love_style_push_item(struct nk_style_item *field) { struct nk_style_item item; - if (lua_type(L, -1) == LUA_TSTRING) { - nk_love_assert_color(-1, "nk.stylePush: item fields must be color strings or images"); - const char *color_string = lua_tostring(L, -1); + if (lua_isstring(L, -1)) { + if (!nk_love_is_color(-1)) { + const char *msg = lua_pushfstring(L, "%%s: bad color string '%s'", lua_tostring(L, -1)); + nk_love_assert(0, msg); + } item.type = NK_STYLE_ITEM_COLOR; - item.data.color = nk_love_color_parse(color_string); + item.data.color = nk_love_checkcolor(-1); } else { - nk_love_assert_type(-1, "Image", "nk.stylePush: item fields must be color strings or images"); - lua_pushvalue(L, -1); item.type = NK_STYLE_ITEM_IMAGE; - nk_love_toimage(&item.data.image); + nk_love_checkImage(-1, &item.data.image); } int success = nk_style_push_style_item(&context, field, item); if (success) { @@ -2860,12 +2730,7 @@ static int nk_love_style_push_item(struct nk_style_item *field) static int nk_love_style_push_align(nk_flags *field) { - nk_love_assert(lua_type(L, -1) == LUA_TSTRING, "nk.stylePush: alignment fields must be alignments"); - const char *align_string = lua_tostring(L, -1); - nk_flags align; - if (!nk_love_parse_align(align_string, &align)) { - nk_love_error("nk.stylePush: alignment fields must be alignments"); - } + nk_flags align = nk_love_checkalign(-1); int success = nk_style_push_flags(&context, field, align); if (success) { lua_pushstring(L, "flags"); @@ -2876,8 +2741,7 @@ static int nk_love_style_push_align(nk_flags *field) } static int nk_love_style_push_float(float *field) { - nk_love_assert(lua_type(L, -1) == LUA_TNUMBER, "nk.stylePush: float fields must be numbers"); - float f = lua_tonumber(L, -1); + float f = luaL_checknumber(L, -1); int success = nk_style_push_float(&context, field, f); if (success) { lua_pushstring(L, "float"); @@ -2889,9 +2753,7 @@ static int nk_love_style_push_float(float *field) { static int nk_love_style_push_font(const struct nk_user_font **field) { - nk_love_assert_type(-1, "Font", "nk.stylePush: font fields must be fonts"); - lua_pushvalue(L, -1); - nk_love_tofont(&fonts[font_count]); + nk_love_checkFont(-1, &fonts[font_count]); int success = nk_style_push_font(&context, &fonts[font_count++]); if (success) { lua_pushstring(L, "font"); @@ -2902,22 +2764,22 @@ static int nk_love_style_push_font(const struct nk_user_font **field) } #define NK_LOVE_STYLE_PUSH(name, type, field) \ - nk_love_assert(lua_type(L, -1) == LUA_TTABLE, "nk.stylePush: " name " field must be a table"); \ + nk_love_assert(lua_istable(L, -1), "%s: " name " field must be a table"); \ lua_getfield(L, -1, name); \ - if (lua_type(L, -1) != LUA_TNIL) \ + if (!lua_isnil(L, -1)) \ nk_love_style_push_##type(field); \ lua_pop(L, 1); static void nk_love_style_push_text(struct nk_style_text *style) { - nk_love_assert(lua_type(L, -1) == LUA_TTABLE, "text style must be a table"); + nk_love_assert(lua_istable(L, -1), "%s: text style must be a table"); NK_LOVE_STYLE_PUSH("color", color, &style->color); NK_LOVE_STYLE_PUSH("padding", vec2, &style->padding); } static void nk_love_style_push_button(struct nk_style_button *style) { - nk_love_assert(lua_type(L, -1) == LUA_TTABLE, "button style must be a table"); + nk_love_assert(lua_istable(L, -1), "%s: button style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("active", item, &style->active); @@ -2936,7 +2798,7 @@ static void nk_love_style_push_button(struct nk_style_button *style) static void nk_love_style_push_scrollbar(struct nk_style_scrollbar *style) { - nk_love_assert(lua_type(L, -1) == LUA_TTABLE, "scrollbar style must be a table"); + nk_love_assert(lua_istable(L, -1), "%s: scrollbar style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("active", item, &style->active); @@ -2954,7 +2816,7 @@ static void nk_love_style_push_scrollbar(struct nk_style_scrollbar *style) static void nk_love_style_push_edit(struct nk_style_edit *style) { - nk_love_assert(lua_type(L, -1) == LUA_TTABLE, "edit style must be a table"); + nk_love_assert(lua_istable(L, -1), "%s: edit style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("active", item, &style->active); @@ -2981,7 +2843,7 @@ static void nk_love_style_push_edit(struct nk_style_edit *style) static void nk_love_style_push_toggle(struct nk_style_toggle *style) { - nk_love_assert(lua_type(L, -1) == LUA_TTABLE, "toggle style must be a table"); + nk_love_assert(lua_istable(L, -1), "%s: toggle style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("active", item, &style->active); @@ -3001,7 +2863,7 @@ static void nk_love_style_push_toggle(struct nk_style_toggle *style) static void nk_love_style_push_selectable(struct nk_style_selectable *style) { - nk_love_assert(lua_type(L, -1) == LUA_TTABLE, "selectable style must be a table"); + nk_love_assert(lua_istable(L, -1), "%s: selectable style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("pressed", item, &style->pressed); @@ -3024,7 +2886,7 @@ static void nk_love_style_push_selectable(struct nk_style_selectable *style) static void nk_love_style_push_slider(struct nk_style_slider *style) { - nk_love_assert(lua_type(L, -1) == LUA_TTABLE, "slider style must be a table"); + nk_love_assert(lua_istable(L, -1), "%s: slider style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("active", item, &style->active); @@ -3045,7 +2907,7 @@ static void nk_love_style_push_slider(struct nk_style_slider *style) static void nk_love_style_push_progress(struct nk_style_progress *style) { - nk_love_assert(lua_type(L, -1) == LUA_TTABLE, "progress style must be a table"); + nk_love_assert(lua_istable(L, -1), "%s: progress style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("active", item, &style->active); @@ -3063,7 +2925,7 @@ static void nk_love_style_push_progress(struct nk_style_progress *style) static void nk_love_style_push_property(struct nk_style_property *style) { - nk_love_assert(lua_type(L, -1) == LUA_TTABLE, "property style must be a table"); + nk_love_assert(lua_istable(L, -1), "%s: property style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("active", item, &style->active); @@ -3081,7 +2943,7 @@ static void nk_love_style_push_property(struct nk_style_property *style) static void nk_love_style_push_chart(struct nk_style_chart *style) { - nk_love_assert(lua_type(L, -1) == LUA_TTABLE, "chart style must be a table"); + nk_love_assert(lua_istable(L, -1), "%s: chart style must be a table"); NK_LOVE_STYLE_PUSH("background", item, &style->background); NK_LOVE_STYLE_PUSH("border color", color, &style->border_color); NK_LOVE_STYLE_PUSH("selected color", color, &style->selected_color); @@ -3093,7 +2955,7 @@ static void nk_love_style_push_chart(struct nk_style_chart *style) static void nk_love_style_push_tab(struct nk_style_tab *style) { - nk_love_assert(lua_type(L, -1) == LUA_TTABLE, "tab style must be a table"); + nk_love_assert(lua_istable(L, -1), "%s: tab style must be a table"); NK_LOVE_STYLE_PUSH("background", item, &style->background); NK_LOVE_STYLE_PUSH("border color", color, &style->border_color); NK_LOVE_STYLE_PUSH("text", color, &style->text); @@ -3110,7 +2972,7 @@ static void nk_love_style_push_tab(struct nk_style_tab *style) static void nk_love_style_push_combo(struct nk_style_combo *style) { - nk_love_assert(lua_type(L, -1) == LUA_TTABLE, "combo style must be a table"); + nk_love_assert(lua_istable(L, -1), "%s: combo style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("active", item, &style->active); @@ -3131,7 +2993,7 @@ static void nk_love_style_push_combo(struct nk_style_combo *style) static void nk_love_style_push_window_header(struct nk_style_window_header *style) { - nk_love_assert(lua_type(L, -1) == LUA_TTABLE, "window header style must be a table"); + nk_love_assert(lua_istable(L, -1), "%s: window header style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("active", item, &style->active); @@ -3147,7 +3009,7 @@ static void nk_love_style_push_window_header(struct nk_style_window_header *styl static void nk_love_style_push_window(struct nk_style_window *style) { - nk_love_assert(lua_type(L, -1) == LUA_TTABLE, "window style must be a table"); + nk_love_assert(lua_istable(L, -1), "%s: window style must be a table"); NK_LOVE_STYLE_PUSH("header", window_header, &style->header); NK_LOVE_STYLE_PUSH("fixed background", item, &style->fixed_background); NK_LOVE_STYLE_PUSH("background", color, &style->background); @@ -3181,8 +3043,9 @@ static void nk_love_style_push_window(struct nk_style_window *style) static int nk_love_style_push(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.stylePush: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TTABLE, "nk.stylePush: arg 1 should be a table"); + nk_love_assert_argc(lua_gettop(L) == 1); + if (!lua_istable(L, 1)) + luaL_typerror(L, 1, "table"); lua_newtable(L); lua_insert(L, 1); NK_LOVE_STYLE_PUSH("font", font, &context.style.font); @@ -3214,7 +3077,7 @@ static int nk_love_style_push(lua_State *L) static int nk_love_style_pop(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.stylePop: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); lua_getfield(L, -1, "stack"); size_t stack_size = lua_objlen(L, -1); @@ -3239,7 +3102,8 @@ static int nk_love_style_pop(lua_State *L) } else if (!strcmp(type, "font")) { nk_style_pop_font(&context); } else { - nk_love_error("nk.pop: bad style item type"); + const char *msg = lua_pushfstring(L, "%%s: bad style item type '%s'", lua_tostring(L, -1)); + nk_love_assert(0, msg); } lua_pop(L, 1); } @@ -3248,7 +3112,7 @@ static int nk_love_style_pop(lua_State *L) static int nk_love_widget_bounds(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.widgetBounds: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); struct nk_rect bounds = nk_widget_bounds(&context); lua_pushnumber(L, bounds.x); lua_pushnumber(L, bounds.y); @@ -3259,7 +3123,7 @@ static int nk_love_widget_bounds(lua_State *L) static int nk_love_widget_position(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.widgetPosition: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); struct nk_vec2 pos = nk_widget_position(&context); lua_pushnumber(L, pos.x); lua_pushnumber(L, pos.y); @@ -3268,7 +3132,7 @@ static int nk_love_widget_position(lua_State *L) static int nk_love_widget_size(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.widgetSize: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); struct nk_vec2 pos = nk_widget_size(&context); lua_pushnumber(L, pos.x); lua_pushnumber(L, pos.y); @@ -3277,7 +3141,7 @@ static int nk_love_widget_size(lua_State *L) static int nk_love_widget_width(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.widgetWidth: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); float width = nk_widget_width(&context); lua_pushnumber(L, width); return 1; @@ -3285,7 +3149,7 @@ static int nk_love_widget_width(lua_State *L) static int nk_love_widget_height(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.widgetHeight: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); float height = nk_widget_height(&context); lua_pushnumber(L, height); return 1; @@ -3293,7 +3157,7 @@ static int nk_love_widget_height(lua_State *L) static int nk_love_widget_is_hovered(lua_State *L) { - nk_love_assert(lua_gettop(L) == 0, "nk.widgetIsHovered: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 0); int hovered = nk_widget_is_hovered(&context); lua_pushboolean(L, hovered); return 1; @@ -3302,15 +3166,10 @@ static int nk_love_widget_is_hovered(lua_State *L) static int nk_love_widget_is_mouse_clicked(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc == 0 || argc == 1, "nk.widgetIsMouseClicked: wrong number of arguments"); + nk_love_assert_argc(argc >= 0 && argc <= 1); enum nk_buttons button = NK_BUTTON_LEFT; - if (argc == 1 && lua_type(L, 1) != LUA_TNIL) { - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.widgetIsMouseClicked: arg 1 should be a string"); - const char *button_string = lua_tostring(L, 1); - if (!nk_love_parse_button(button_string, &button)) { - nk_love_error("nk.widgetIsMouseClicked: arg 1 should be a button"); - } - } + if (argc >= 1 && !lua_isnil(L, 1)) + button = nk_love_checkbutton(1); int clicked = (context.active == context.current) && nk_input_is_mouse_pressed(&context.input, button); lua_pushboolean(L, clicked); @@ -3320,83 +3179,65 @@ static int nk_love_widget_is_mouse_clicked(lua_State *L) static int nk_love_widget_has_mouse_click(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc >= 0 && argc <= 2, "nk.widgetHasMouseClick: wrong number of arguments"); + nk_love_assert_argc(argc >= 0 && argc <= 2); enum nk_buttons button = NK_BUTTON_LEFT; - if (argc >= 1 && lua_type(L, 1) != LUA_TNIL) { - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.widgetHasMouseClick: arg 1 should be a string"); - const char *button_string = lua_tostring(L, 1); - if (!nk_love_parse_button(button_string, &button)) { - nk_love_error("nk.widgetHasMouseClick: arg 1 should be a button"); - } - } + if (argc >= 1 && !lua_isnil(L, 1)) + button = nk_love_checkbutton(1); int down = 1; - if (argc >= 2 && lua_type(L, 2) != LUA_TNIL) { - nk_love_assert(lua_type(L, 2) == LUA_TBOOLEAN, "nk.widgetHasMouseClick: arg 2 should be a boolean"); - down = lua_toboolean(L, 2); - } + if (argc >= 2 && !lua_isnil(L, 2)) + down = nk_love_checkboolean(L, 2); int has_click = nk_widget_has_mouse_click_down(&context, button, down); lua_pushboolean(L, has_click); return 1; } -#define NK_LOVE_WIDGET_HAS_MOUSE(name, down) \ - int argc = lua_gettop(L); \ - nk_love_assert(argc >= 0 && argc <= 1, name ": wrong number of arguments"); \ - enum nk_buttons button = NK_BUTTON_LEFT; \ - if (argc >= 1 && lua_type(L, 1) != LUA_TNIL) { \ - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, name ": arg 1 should be a button"); \ - if (!nk_love_parse_button(lua_tostring(L, 1), &button)) { \ - nk_love_error(name ": arg 1 should be a button"); \ - } \ - } \ - int ret = nk_widget_has_mouse_click_down(&context, button, down); \ - lua_pushboolean(L, ret); \ - return 1 +static int nk_love_widget_has_mouse(lua_State *L, int down) { + int argc = lua_gettop(L); + nk_love_assert_argc(argc >= 0 && argc <= 1); + enum nk_buttons button = NK_BUTTON_LEFT; + if (argc >= 1 && !lua_isnil(L, 1)) + button = nk_love_checkbutton(1); + int ret = nk_widget_has_mouse_click_down(&context, button, down); + lua_pushboolean(L, ret); + return 1; +} static int nk_love_widget_has_mouse_pressed(lua_State *L) { - NK_LOVE_WIDGET_HAS_MOUSE("nk.widgetHasMousePressed", nk_true); + return nk_love_widget_has_mouse(L, nk_true); } static int nk_love_widget_has_mouse_released(lua_State *L) { - NK_LOVE_WIDGET_HAS_MOUSE("nk.widgetHasMouseReleased", nk_false); + return nk_love_widget_has_mouse(L, nk_false); } -#undef NK_LOVE_WIDGET_HAS_MOUSE - -#define NK_LOVE_WIDGET_IS_MOUSE(name, down) \ - int argc = lua_gettop(L); \ - nk_love_assert(argc >= 0 && argc <= 1, name ": wrong number of arguments"); \ - enum nk_buttons button = NK_BUTTON_LEFT; \ - if (argc >= 1 && lua_type(L, 1) != LUA_TNIL) { \ - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, name ": arg 1 should be a button"); \ - if (!nk_love_parse_button(lua_tostring(L, 1), &button)) { \ - nk_love_error(name ": arg 1 should be a button"); \ - } \ - } \ - struct nk_rect bounds = nk_widget_bounds(&context); \ - int ret = nk_input_is_mouse_click_down_in_rect(&context.input, button, bounds, down); \ - lua_pushboolean(L, ret); \ - return 1 +static int nk_love_widget_is_mouse(lua_State *L, int down) { + int argc = lua_gettop(L); + nk_love_assert_argc(argc >= 0 && argc <= 1); + enum nk_buttons button = NK_BUTTON_LEFT; + if (argc >= 1 && !lua_isnil(L, 1)) + button = nk_love_checkbutton(1); + struct nk_rect bounds = nk_widget_bounds(&context); + int ret = nk_input_is_mouse_click_down_in_rect(&context.input, button, bounds, down); + lua_pushboolean(L, ret); + return 1; +} static int nk_love_widget_is_mouse_pressed(lua_State *L) { - NK_LOVE_WIDGET_IS_MOUSE("nk.widgetIsMousePressed", nk_true); + return nk_love_widget_is_mouse(L, nk_true); } static int nk_love_widget_is_mouse_released(lua_State *L) { - NK_LOVE_WIDGET_IS_MOUSE("nk.widgetIsMouseReleased", nk_false); + return nk_love_widget_is_mouse(L, nk_false); } -#undef NK_LOVE_WIDGET_IS_MOUSE - static int nk_love_spacing(lua_State *L) { - nk_love_assert(lua_gettop(L) == 1, "nk.spacing: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.spacing: arg 1 should be a number"); - int cols = lua_tointeger(L, 1); + nk_love_assert_argc(lua_gettop(L) == 1); + int cols = luaL_checkint(L, 1); nk_spacing(&context, cols); return 0; } @@ -3404,10 +3245,10 @@ static int nk_love_spacing(lua_State *L) static int nk_love_line(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc >= 4 && argc % 2 == 0, "nk.line: wrong number of arguments"); + nk_love_assert_argc(argc >= 4 && argc % 2 == 0); int i; for (i = 0; i < argc; ++i) { - nk_love_assert(lua_type(L, i + 1) == LUA_TNUMBER, "nk.line: point coordinates should be numbers"); + nk_love_assert(lua_isnumber(L, i + 1), "%s: point coordinates should be numbers"); floats[i] = lua_tonumber(L, i + 1); } float line_thickness; @@ -3419,19 +3260,16 @@ static int nk_love_line(lua_State *L) static int nk_love_curve(lua_State *L) { - nk_love_assert(lua_gettop(L) == 8, "nk.curve: wrong number of arguments"); + nk_love_assert_argc(lua_gettop(L) == 8); int i; - for (i = 1; i <= 8; ++i) { - nk_love_assert(lua_type(L, i) == LUA_TNUMBER, "nk.curve: point coordinates should be numbers"); - } - float ax = lua_tonumber(L, 1); - float ay = lua_tonumber(L, 2); - float ctrl0x = lua_tonumber(L, 3); - float ctrl0y = lua_tonumber(L, 4); - float ctrl1x = lua_tonumber(L, 5); - float ctrl1y = lua_tonumber(L, 6); - float bx = lua_tonumber(L, 7); - float by = lua_tonumber(L, 8); + float ax = luaL_checknumber(L, 1); + float ay = luaL_checknumber(L, 2); + float ctrl0x = luaL_checknumber(L, 3); + float ctrl0y = luaL_checknumber(L, 4); + float ctrl1x = luaL_checknumber(L, 5); + float ctrl1y = luaL_checknumber(L, 6); + float bx = luaL_checknumber(L, 7); + float by = luaL_checknumber(L, 8); float line_thickness; struct nk_color color; nk_love_getGraphics(&line_thickness, &color); @@ -3442,161 +3280,116 @@ static int nk_love_curve(lua_State *L) static int nk_love_polygon(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert(argc >= 7 && argc % 2 == 1, "nk.polygon: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.polygon: arg 1 should be a draw mode"); - const char *mode = lua_tostring(L, 1); + nk_love_assert_argc(argc >= 7 && argc % 2 == 1); + enum nk_love_draw_mode mode = nk_love_checkdraw(1); int i; for (i = 0; i < argc - 1; ++i) { - nk_love_assert(lua_type(L, i + 2) == LUA_TNUMBER, "nk.polygon: point coordinates should be numbers"); + nk_love_assert(lua_isnumber(L, i + 2), "%s: point coordinates should be numbers"); floats[i] = lua_tonumber(L, i + 2); } float line_thickness; struct nk_color color; nk_love_getGraphics(&line_thickness, &color); - if (!strcmp(mode, "fill")) { + if (mode == NK_LOVE_FILL) nk_fill_polygon(&context.current->buffer, floats, (argc - 1) / 2, color); - } else if (!strcmp(mode, "line")) { + else if (mode == NK_LOVE_LINE) nk_stroke_polygon(&context.current->buffer, floats, (argc - 1) / 2, line_thickness, color); - } else { - nk_love_error("nk.polygon: arg 1 should be a draw mode"); - } return 0; } static int nk_love_circle(lua_State *L) { - nk_love_assert(lua_gettop(L) == 4, "nk.circle: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.circle: arg 1 should be a draw mode"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.circle: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.circle: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.circle: arg 4 should be a number"); - const char *mode = lua_tostring(L, 1); - float x = lua_tonumber(L, 2); - float y = lua_tonumber(L, 3); - float r = lua_tonumber(L, 4); + nk_love_assert_argc(lua_gettop(L) == 4); + enum nk_love_draw_mode mode = nk_love_checkdraw(1); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float r = luaL_checknumber(L, 4); float line_thickness; struct nk_color color; nk_love_getGraphics(&line_thickness, &color); - if (!strcmp(mode, "fill")) { + if (mode == NK_LOVE_FILL) nk_fill_circle(&context.current->buffer, nk_rect(x - r, y - r, r * 2, r * 2), color); - } else if (!strcmp(mode, "line")) { + else if (mode == NK_LOVE_LINE) nk_stroke_circle(&context.current->buffer, nk_rect(x - r, y - r, r * 2, r * 2), line_thickness, color); - } else { - nk_love_error("nk.circle: arg 1 should be a draw mode"); - } return 0; } static int nk_love_ellipse(lua_State *L) { - nk_love_assert(lua_gettop(L) == 5, "nk.ellipse: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.ellipse: arg 1 should be a draw mode"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.ellipse: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.ellipse: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.ellipse: arg 4 should be a number"); - nk_love_assert(lua_type(L, 5) == LUA_TNUMBER, "nk.ellipse: arg 5 should be a number"); - const char *mode = lua_tostring(L, 1); - float x = lua_tonumber(L, 2); - float y = lua_tonumber(L, 3); - float rx = lua_tonumber(L, 4); - float ry = lua_tonumber(L, 5); + nk_love_assert_argc(lua_gettop(L) == 5); + enum nk_love_draw_mode mode = nk_love_checkdraw(1); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float rx = luaL_checknumber(L, 4); + float ry = luaL_checknumber(L, 5); float line_thickness; struct nk_color color; nk_love_getGraphics(&line_thickness, &color); - if (!strcmp(mode, "fill")) { + if (mode == NK_LOVE_FILL) nk_fill_circle(&context.current->buffer, nk_rect(x - rx, y - ry, rx * 2, ry * 2), color); - } else if (!strcmp(mode, "line")) { + else if (mode == NK_LOVE_LINE) nk_stroke_circle(&context.current->buffer, nk_rect(x - rx, y - ry, rx * 2, ry * 2), line_thickness, color); - } else { - nk_love_error("nk.ellipse: arg 1 should be a draw mode"); - } return 0; } static int nk_love_arc(lua_State *L) { - nk_love_assert(lua_gettop(L) == 6, "nk.arc: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.arc: arg 1 should be a draw mode"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.arc: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.arc: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.arc: arg 4 should be a number"); - nk_love_assert(lua_type(L, 5) == LUA_TNUMBER, "nk.arc: arg 5 should be a number"); - nk_love_assert(lua_type(L, 6) == LUA_TNUMBER, "nk.arc: arg 6 should be a number"); - const char *mode = lua_tostring(L, 1); - float cx = lua_tonumber(L, 2); - float cy = lua_tonumber(L, 3); - float r = lua_tonumber(L, 4); - float a0 = lua_tonumber(L, 5); - float a1 = lua_tonumber(L, 6); + nk_love_assert_argc(lua_gettop(L) == 6); + enum nk_love_draw_mode mode = nk_love_checkdraw(1); + float cx = luaL_checknumber(L, 2); + float cy = luaL_checknumber(L, 3); + float r = luaL_checknumber(L, 4); + float a0 = luaL_checknumber(L, 5); + float a1 = luaL_checknumber(L, 6); float line_thickness; struct nk_color color; nk_love_getGraphics(&line_thickness, &color); - if (!strcmp(mode, "fill")) { + if (mode == NK_LOVE_FILL) nk_fill_arc(&context.current->buffer, cx, cy, r, a0, a1, color); - } else if (!strcmp(mode, "line")) { + else if (mode == NK_LOVE_LINE) nk_stroke_arc(&context.current->buffer, cx, cy, r, a0, a1, line_thickness, color); - } else { - nk_love_error("nk.arc: arg 1 should be a draw mode"); - } return 0; } static int nk_love_rect_multi_color(lua_State *L) { - nk_love_assert(lua_gettop(L) == 8, "nk.rectMultiColor: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.rectMultiColor: arg 1 should be a number"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.rectMultiColor: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.rectMultiColor: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.rectMultiColor: arg 4 should be a number"); - nk_love_assert_color(5, "nk.rectMultiColor: arg 5 should be a color string"); - nk_love_assert_color(6, "nk.rectMultiColor: arg 6 should be a color string"); - nk_love_assert_color(7, "nk.rectMultiColor: arg 7 should be a color string"); - nk_love_assert_color(8, "nk.rectMultiColor: arg 8 should be a color string"); - float x = lua_tonumber(L, 1); - float y = lua_tonumber(L, 2); - float w = lua_tonumber(L, 3); - float h = lua_tonumber(L, 4); - struct nk_color topLeft = nk_love_color_parse(lua_tostring(L, 5)); - struct nk_color topRight = nk_love_color_parse(lua_tostring(L, 6)); - struct nk_color bottomLeft = nk_love_color_parse(lua_tostring(L, 7)); - struct nk_color bottomRight = nk_love_color_parse(lua_tostring(L, 8)); + nk_love_assert_argc(lua_gettop(L) == 8); + float x = luaL_checknumber(L, 1); + float y = luaL_checknumber(L, 2); + float w = luaL_checknumber(L, 3); + float h = luaL_checknumber(L, 4); + struct nk_color topLeft = nk_love_checkcolor(5); + struct nk_color topRight = nk_love_checkcolor(6); + struct nk_color bottomLeft = nk_love_checkcolor(7); + struct nk_color bottomRight = nk_love_checkcolor(8); nk_fill_rect_multi_color(&context.current->buffer, nk_rect(x, y, w, h), topLeft, topRight, bottomLeft, bottomRight); return 0; } static int nk_love_push_scissor(lua_State *L) { - nk_love_assert(lua_gettop(L) == 4, "nk.scissor: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.scissor: arg 1 should be a number"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.scissor: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.scissor: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.scissor: arg 4 should be a number"); - float x = lua_tonumber(L, 1); - float y = lua_tonumber(L, 2); - float w = lua_tonumber(L, 3); - float h = lua_tonumber(L, 4); + nk_love_assert_argc(lua_gettop(L) == 4); + float x = luaL_checknumber(L, 1); + float y = luaL_checknumber(L, 2); + float w = luaL_checknumber(L, 3); + float h = luaL_checknumber(L, 4); nk_push_scissor(&context.current->buffer, nk_rect(x, y, w, h)); return 0; } static int nk_love_text(lua_State *L) { - nk_love_assert(lua_gettop(L) == 5, "nk.text: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, "nk.text: arg 1 should be a string"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.text: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.text: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.text: arg 4 should be a number"); - nk_love_assert(lua_type(L, 5) == LUA_TNUMBER, "nk.text: arg 5 should be a number"); - const char *text = lua_tostring(L, 1); - float x = lua_tonumber(L, 2); - float y = lua_tonumber(L, 3); - float w = lua_tonumber(L, 4); - float h = lua_tonumber(L, 5); + nk_love_assert_argc(lua_gettop(L) == 5); + const char *text = luaL_checkstring(L, 1); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float w = luaL_checknumber(L, 4); + float h = luaL_checknumber(L, 5); lua_getglobal(L, "love"); lua_getfield(L, -1, "graphics"); lua_getfield(L, -1, "getFont"); lua_call(L, 0, 1); - nk_love_tofont(&fonts[font_count]); + nk_love_checkFont(-1, &fonts[font_count]); float line_thickness; struct nk_color color; nk_love_getGraphics(&line_thickness, &color); @@ -3604,81 +3397,61 @@ static int nk_love_text(lua_State *L) return 0; } -#define NK_LOVE_INPUT_HAS_MOUSE(name, down) \ - int argc = lua_gettop(L); \ - nk_love_assert(argc == 5, name ": wrong number of arguments"); \ - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, name ": arg 1 should be a button"); \ - enum nk_buttons button; \ - if (!nk_love_parse_button(lua_tostring(L, 1), &button)) { \ - nk_love_error(name ": arg 1 should be a button"); \ - } \ - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, name ": arg 2 should be a number"); \ - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, name ": arg 3 should be a number"); \ - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, name ": arg 4 should be a number"); \ - nk_love_assert(lua_type(L, 5) == LUA_TNUMBER, name ": arg 5 should be a number"); \ - float x = lua_tonumber(L, 2); \ - float y = lua_tonumber(L, 3); \ - float w = lua_tonumber(L, 4); \ - float h = lua_tonumber(L, 5); \ - int ret = nk_input_has_mouse_click_down_in_rect(&context.input, button, nk_rect(x, y, w, h), down); \ - lua_pushboolean(L, ret); \ - return 1 +static int nk_love_input_has_mouse(int down) +{ + int argc = lua_gettop(L); + nk_love_assert_argc(argc == 5); + enum nk_buttons button = nk_love_checkbutton(1); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float w = luaL_checknumber(L, 4); + float h = luaL_checknumber(L, 5); + int ret = nk_input_has_mouse_click_down_in_rect(&context.input, button, nk_rect(x, y, w, h), down); + lua_pushboolean(L, ret); + return 1; +} static int nk_love_input_has_mouse_pressed(lua_State *L) { - NK_LOVE_INPUT_HAS_MOUSE("nk.inputHasMousePressed", nk_true); + return nk_love_input_has_mouse(nk_true); } static int nk_love_input_has_mouse_released(lua_State *L) { - NK_LOVE_INPUT_HAS_MOUSE("nk.inputHasMouseReleased", nk_false); + return nk_love_input_has_mouse(nk_false); } -#undef NK_LOVE_INPUT_HAS_MOUSE - -#define NK_LOVE_INPUT_IS_MOUSE(name, down) \ - int argc = lua_gettop(L); \ - nk_love_assert(argc == 5, name ": wrong number of arguments"); \ - enum nk_buttons button; \ - nk_love_assert(lua_type(L, 1) == LUA_TSTRING, name ": arg 1 should be a button"); \ - if (!nk_love_parse_button(lua_tostring(L, 1), &button)) { \ - nk_love_error(name ": arg 1 should be a button"); \ - } \ - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, name ": arg 2 should be a number"); \ - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, name ": arg 3 should be a number"); \ - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, name ": arg 4 should be a number"); \ - nk_love_assert(lua_type(L, 5) == LUA_TNUMBER, name ": arg 5 should be a number"); \ - float x = lua_tonumber(L, 2); \ - float y = lua_tonumber(L, 3); \ - float w = lua_tonumber(L, 4); \ - float h = lua_tonumber(L, 5); \ - int ret = nk_input_is_mouse_click_down_in_rect(&context.input, button, nk_rect(x, y, w, h), down); \ - lua_pushboolean(L, ret); \ - return 1 +static int nk_love_input_is_mouse(int down) +{ + int argc = lua_gettop(L); + nk_love_assert_argc(argc == 5); + enum nk_buttons button = nk_love_checkbutton(1); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float w = luaL_checknumber(L, 4); + float h = luaL_checknumber(L, 5); + int ret = nk_input_is_mouse_click_down_in_rect(&context.input, button, nk_rect(x, y, w, h), down); + lua_pushboolean(L, ret); + return 1; +} static int nk_love_input_is_mouse_pressed(lua_State *L) { - NK_LOVE_INPUT_IS_MOUSE("nk.inputIsMousePressed", nk_true); + nk_love_input_is_mouse(nk_true); } static int nk_love_input_is_mouse_released(lua_State *L) { - NK_LOVE_INPUT_IS_MOUSE("nk.inputIsMouseReleased", nk_false); + nk_love_input_is_mouse(nk_false); } -#undef NK_LOVE_INPUT_IS_MOUSE - static int nk_love_input_was_hovered(lua_State *L) { - nk_love_assert(lua_gettop(L) == 4, "nk.inputWasHovered: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.inputWasHovered: arg 1 should be a number"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.inputWasHovered: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.inputWasHovered: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.inputWasHovered: arg 4 should be a number"); - float x = lua_tonumber(L, 1); - float y = lua_tonumber(L, 2); - float w = lua_tonumber(L, 3); - float h = lua_tonumber(L, 4); + nk_love_assert_argc(lua_gettop(L) == 4); + float x = luaL_checknumber(L, 1); + float y = luaL_checknumber(L, 2); + float w = luaL_checknumber(L, 3); + float h = luaL_checknumber(L, 4); int was_hovered = nk_input_is_mouse_prev_hovering_rect(&context.input, nk_rect(x, y, w, h)); lua_pushboolean(L, was_hovered); return 1; @@ -3686,15 +3459,11 @@ static int nk_love_input_was_hovered(lua_State *L) static int nk_love_input_is_hovered(lua_State *L) { - nk_love_assert(lua_gettop(L) == 4, "nk.inputIsHovered: wrong number of arguments"); - nk_love_assert(lua_type(L, 1) == LUA_TNUMBER, "nk.inputIsHovered: arg 1 should be a number"); - nk_love_assert(lua_type(L, 2) == LUA_TNUMBER, "nk.inputIsHovered: arg 2 should be a number"); - nk_love_assert(lua_type(L, 3) == LUA_TNUMBER, "nk.inputIsHovered: arg 3 should be a number"); - nk_love_assert(lua_type(L, 4) == LUA_TNUMBER, "nk.inputIsHovered: arg 4 should be a number"); - float x = lua_tonumber(L, 1); - float y = lua_tonumber(L, 2); - float w = lua_tonumber(L, 3); - float h = lua_tonumber(L, 4); + nk_love_assert_argc(lua_gettop(L) == 4); + float x = luaL_checknumber(L, 1); + float y = luaL_checknumber(L, 2); + float w = luaL_checknumber(L, 3); + float h = luaL_checknumber(L, 4); int is_hovered = nk_input_is_mouse_hovering_rect(&context.input, nk_rect(x, y, w, h)); lua_pushboolean(L, is_hovered); return 1; From 47128fed66ed802a36e1eaa2c6693752dae88acd Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Mon, 19 Dec 2016 22:27:42 -0500 Subject: [PATCH 08/52] Allow multiple contexts to persist at once --- README.md | 48 +- example/calculator.lua | 24 +- example/draw.lua | 28 +- example/main.lua | 36 +- example/overview.lua | 165 +-- example/skin.lua | 28 +- example/style.lua | 41 +- src/nuklear_love.c | 3070 ++++++++++++++++++++-------------------- 8 files changed, 1748 insertions(+), 1692 deletions(-) diff --git a/README.md b/README.md index 42b6010..4e687ea 100644 --- a/README.md +++ b/README.md @@ -8,68 +8,70 @@ Provides a lightweight immediate mode GUI for LÖVE games. ```lua -- Simple UI example. -local nk = require 'nuklear' +local nuklear = require 'nuklear' + +local ui function love.load() - nk.init() + ui = nuklear.init() end local combo = {value = 1, items = {'A', 'B', 'C'}} function love.update(dt) - nk.frameBegin() - if nk.windowBegin('Simple Example', 100, 100, 200, 160, + ui:frameBegin() + if ui:windowBegin('Simple Example', 100, 100, 200, 160, 'border', 'title', 'movable') then - nk.layoutRow('dynamic', 30, 1) - nk.label('Hello, world!') - nk.layoutRow('dynamic', 30, 2) - nk.label('Combo box:') - if nk.combobox(combo, combo.items) then + ui:layoutRow('dynamic', 30, 1) + ui:label('Hello, world!') + ui:layoutRow('dynamic', 30, 2) + ui:label('Combo box:') + if ui:combobox(combo, combo.items) then print('Combo!', combo.items[combo.value]) end - nk.layoutRow('dynamic', 30, 3) - nk.label('Buttons:') - if nk.button('Sample') then + ui:layoutRow('dynamic', 30, 3) + ui:label('Buttons:') + if ui:button('Sample') then print('Sample!') end - if nk.button('Button') then + if ui:button('Button') then print('Button!') end end - nk.windowEnd() - nk.frameEnd() + ui:windowEnd() + ui:frameEnd() end function love.draw() - nk.draw() + ui:draw() end function love.keypressed(key, scancode, isrepeat) - nk.keypressed(key, scancode, isrepeat) + ui:keypressed(key, scancode, isrepeat) end function love.keyreleased(key, scancode) - nk.keyreleased(key, scancode) + ui:keyreleased(key, scancode) end function love.mousepressed(x, y, button, istouch) - nk.mousepressed(x, y, button, istouch) + ui:mousepressed(x, y, button, istouch) end function love.mousereleased(x, y, button, istouch) - nk.mousereleased(x, y, button, istouch) + ui:mousereleased(x, y, button, istouch) end function love.mousemoved(x, y, dx, dy, istouch) - nk.mousemoved(x, y, dx, dy, istouch) + ui:mousemoved(x, y, dx, dy, istouch) end function love.textinput(text) - nk.textinput(text) + ui:textinput(text) end function love.wheelmoved(x, y) - nk.wheelmoved(x, y) + ui:wheelmoved(x, y) end ``` diff --git a/example/calculator.lua b/example/calculator.lua index c6a6383..d1eecf1 100644 --- a/example/calculator.lua +++ b/example/calculator.lua @@ -1,7 +1,5 @@ -- Simple calculator example, adapted from the original nuklear demo. -local nk = require 'nuklear' - local ops = {'+', '-', '*', '/'} local a, b, op = '0' @@ -60,36 +58,36 @@ local function display() return b or a end -return function () - if nk.windowBegin('Calculator', 50, 50, 180, 250, 'border', 'movable', 'title') then - nk.layoutRow('dynamic', 35, 1) - nk.label(display(), 'right') - nk.layoutRow('dynamic', 35, 4) +return function (ui) + if ui:windowBegin('Calculator', 50, 50, 180, 250, 'border', 'movable', 'title') then + ui:layoutRow('dynamic', 35, 1) + ui:label(display(), 'right') + ui:layoutRow('dynamic', 35, 4) for i=1,16 do if i >= 13 and i < 16 then if i == 13 then - if nk.button('C') then + if ui:button('C') then clear() end - if nk.button('0') then + if ui:button('0') then digit('0') end - if nk.button('=') then + if ui:button('=') then equals() end end elseif i % 4 ~= 0 then local d = tostring(math.floor(i / 4) * 3 + (i % 4)) - if nk.button(d) then + if ui:button(d) then digit(d) end else local o = ops[math.floor(i / 4)] - if nk.button(o) then + if ui:button(o) then operator(o) end end end end - nk.windowEnd() + ui:windowEnd() end diff --git a/example/draw.lua b/example/draw.lua index c7bab22..c276f47 100644 --- a/example/draw.lua +++ b/example/draw.lua @@ -1,21 +1,19 @@ -local nk = require "nuklear" - local img = love.graphics.newImage 'skin/button.png' -return function () - if nk.windowBegin('Draw Example', 300, 300, 200, 200, 'title', 'movable', 'border') then - local x, y, w, h = nk.windowGetBounds() +return function (ui) + if ui:windowBegin('Draw Example', 300, 300, 200, 200, 'title', 'movable', 'border') then + local x, y, w, h = ui:windowGetBounds() love.graphics.setColor(255, 0, 0) - nk.line(x + 10, y + 40, x + 50, y + 40, x + 50, y + 80) - nk.curve(x + 50, y + 80, x + 80, y + 40, x + 100, y + 80, x + 80, y + 80) - nk.polygon('line', x + 100, y + 150, x + 60, y + 140, x + 70, y + 70) - nk.circle('line', x + 130, y + 140, 50) - nk.ellipse('fill', x + 30, y + 150, 20, 40) - nk.arc('fill', x + 150, y + 80, 40, 3 * math.pi / 2, 2 * math.pi); - nk.rectMultiColor(x + 95, y + 50, 50, 50, '#ff0000', '#00ff00', '#0000ff', '#000000') + ui:line(x + 10, y + 40, x + 50, y + 40, x + 50, y + 80) + ui:curve(x + 50, y + 80, x + 80, y + 40, x + 100, y + 80, x + 80, y + 80) + ui:polygon('line', x + 100, y + 150, x + 60, y + 140, x + 70, y + 70) + ui:circle('line', x + 130, y + 140, 50) + ui:ellipse('fill', x + 30, y + 150, 20, 40) + ui:arc('fill', x + 150, y + 80, 40, 3 * math.pi / 2, 2 * math.pi); + ui:rectMultiColor(x + 95, y + 50, 50, 50, '#ff0000', '#00ff00', '#0000ff', '#000000') love.graphics.setColor(255, 255, 255) - nk.image(img, x + 120, y + 120, 70, 50) - nk.text('DRAW TEXT', x + 15, y + 75, 100, 100) + ui:image(img, x + 120, y + 120, 70, 50) + ui:text('DRAW TEXT', x + 15, y + 75, 100, 100) end - nk.windowEnd() + ui:windowEnd() end diff --git a/example/main.lua b/example/main.lua index c6b72b6..583ea1a 100644 --- a/example/main.lua +++ b/example/main.lua @@ -1,6 +1,6 @@ -- Several simple examples. -local nk = require 'nuklear' +local nuklear = require 'nuklear' local calculator = require 'calculator' local draw = require 'draw' @@ -8,49 +8,51 @@ local overview = require 'overview' local style = require 'style' local skin = require 'skin' +local ui + function love.load() - nk.init() + ui = nuklear.init() end function love.update(dt) - nk.frameBegin() - calculator() - style() - overview() - draw() - skin() - nk.frameEnd() + ui:frameBegin() + calculator(ui) + style(ui) + overview(ui) + draw(ui) + skin(ui) + ui:frameEnd() end function love.draw() - nk.draw() + ui:draw() love.graphics.print("Current FPS: "..tostring(love.timer.getFPS( )), 10, 10) end function love.keypressed(key, scancode, isrepeat) - nk.keypressed(key, scancode, isrepeat) + ui:keypressed(key, scancode, isrepeat) end function love.keyreleased(key, scancode) - nk.keyreleased(key, scancode) + ui:keyreleased(key, scancode) end function love.mousepressed(x, y, button, istouch) - nk.mousepressed(x, y, button, istouch) + ui:mousepressed(x, y, button, istouch) end function love.mousereleased(x, y, button, istouch) - nk.mousereleased(x, y, button, istouch) + ui:mousereleased(x, y, button, istouch) end function love.mousemoved(x, y, dx, dy, istouch) - nk.mousemoved(x, y, dx, dy, istouch) + ui:mousemoved(x, y, dx, dy, istouch) end function love.textinput(text) - nk.textinput(text) + ui:textinput(text) end function love.wheelmoved(x, y) - nk.wheelmoved(x, y) + ui:wheelmoved(x, y) end diff --git a/example/overview.lua b/example/overview.lua index a0721ee..c24db18 100644 --- a/example/overview.lua +++ b/example/overview.lua @@ -1,7 +1,5 @@ -- An overview of most of the supported widgets. -local nk = require 'nuklear' - local checkA = {value = false} local checkB = {value = true} local radio = {value = 'A'} @@ -14,93 +12,96 @@ local property = {value = 6} local edit = {value = 'Edit text'} local comboA = {value = 1, items = {'A', 'B', 'C'}} -return function () - if nk.windowBegin('Overview', 100, 100, 600, 450, 'border', 'movable', 'title') then - nk.menubarBegin() - nk.layoutRow('dynamic', 30, 1) - if nk.menuBegin('Menu', nil, 120, 90) then - nk.layoutRow('dynamic', 40, 1) - nk.menuItem('Item A') - nk.menuItem('Item B') - nk.menuItem('Item C') - nk.menuEnd() +return function (ui) + if ui:windowBegin('Overview', 100, 100, 600, 450, 'border', 'movable', 'title') then + ui:menubarBegin() + ui:layoutRow('dynamic', 30, 1) + if ui:menuBegin('Menu', nil, 120, 90) then + ui:layoutRow('dynamic', 40, 1) + ui:menuItem('Item A') + ui:menuItem('Item B') + ui:menuItem('Item C') + ui:menuEnd() end - nk.menubarEnd() - nk.layoutRow('dynamic', 400, 3) - nk.groupBegin('Group 1', 'border') - nk.layoutRow('dynamic', 30, 1) - nk.label('Left label') - nk.label('Centered label', 'centered') - nk.label('Right label', 'right') - nk.label('Colored label', 'left', '#ff0000') - if nk.treePush('tab', 'Tree Tab') then - if nk.treePush('node', 'Tree Node 1') then - nk.label('Label 1') - nk.treePop() + ui:menubarEnd() + ui:layoutRow('dynamic', 400, 3) + if ui:groupBegin('Group 1', 'border') then + ui:layoutRow('dynamic', 30, 1) + ui:label('Left label') + ui:label('Centered label', 'centered') + ui:label('Right label', 'right') + ui:label('Colored label', 'left', '#ff0000') + if ui:treePush('tab', 'Tree Tab') then + if ui:treePush('node', 'Tree Node 1') then + ui:label('Label 1') + ui:treePop() end - if nk.treePush('node', 'Tree Node 2') then - nk.label('Label 2') - nk.treePop() + if ui:treePush('node', 'Tree Node 2') then + ui:label('Label 2') + ui:treePop() end - nk.treePop() + ui:treePop() end - nk.spacing(1) - if nk.button('Button') then + ui:spacing(1) + if ui:button('Button') then print('button pressed!') end - nk.spacing(1) - nk.checkbox('Checkbox A', checkA) - nk.checkbox('Checkbox B', checkB) - nk.groupEnd() - nk.groupBegin('Group 2', 'border') - nk.layoutRow('dynamic', 30, 1) - nk.label('Radio buttons:') - nk.layoutRow('dynamic', 30, 3) - nk.radio('A', radio) - nk.radio('B', radio) - nk.radio('C', radio) - nk.layoutRow('dynamic', 30, 1) - nk.selectable('Selectable A', selectA) - nk.selectable('Selectable B', selectB) - nk.layoutRow('dynamic', 30, {.35, .65}) - nk.label('Slider:') - nk.slider(0, slider, 1, 0.05) - nk.label('Progress:') - nk.progress(progress, 10, true) - nk.layoutRow('dynamic', 30, 2) - nk.spacing(2) - nk.label('Color picker:') - nk.button(nil, colorPicker.value) - nk.layoutRow('dynamic', 90, 1) - nk.colorPicker(colorPicker) - nk.groupEnd() - nk.groupBegin('Group 3', 'border') - nk.layoutRow('dynamic', 30, 1) - nk.property('Property', 0, property, 10, 0.25, 0.05) - nk.spacing(1) - nk.label('Edit:') - nk.layoutRow('dynamic', 90, 1) - nk.edit('box', edit) - nk.layoutRow('dynamic', 5, 1) - nk.spacing(1) - nk.layoutRow('dynamic', 30, 1) - nk.label('Combobox:') - nk.combobox(comboA, comboA.items) - nk.layoutRow('dynamic', 5, 1) - nk.spacing(1) - nk.layoutRow('dynamic', 30, 1) - if nk.widgetIsHovered() then - nk.tooltip('Test tooltip') + ui:spacing(1) + ui:checkbox('Checkbox A', checkA) + ui:checkbox('Checkbox B', checkB) + ui:groupEnd() + end + if ui:groupBegin('Group 2', 'border') then + ui:layoutRow('dynamic', 30, 1) + ui:label('Radio buttons:') + ui:layoutRow('dynamic', 30, 3) + ui:radio('A', radio) + ui:radio('B', radio) + ui:radio('C', radio) + ui:layoutRow('dynamic', 30, 1) + ui:selectable('Selectable A', selectA) + ui:selectable('Selectable B', selectB) + ui:layoutRow('dynamic', 30, {.35, .65}) + ui:label('Slider:') + ui:slider(0, slider, 1, 0.05) + ui:label('Progress:') + ui:progress(progress, 10, true) + ui:layoutRow('dynamic', 30, 2) + ui:spacing(2) + ui:label('Color picker:') + ui:button(nil, colorPicker.value) + ui:layoutRow('dynamic', 90, 1) + ui:colorPicker(colorPicker) + ui:groupEnd() + end + if ui:groupBegin('Group 3', 'border') then + ui:layoutRow('dynamic', 30, 1) + ui:property('Property', 0, property, 10, 0.25, 0.05) + ui:spacing(1) + ui:label('Edit:') + ui:layoutRow('dynamic', 90, 1) + ui:edit('box', edit) + ui:layoutRow('dynamic', 5, 1) + ui:spacing(1) + ui:layoutRow('dynamic', 30, 1) + ui:label('Combobox:') + ui:combobox(comboA, comboA.items) + ui:layoutRow('dynamic', 5, 1) + ui:spacing(1) + ui:layoutRow('dynamic', 30, 1) + if ui:widgetIsHovered() then + ui:tooltip('Test tooltip') end - local x, y, w, h = nk.widgetBounds() - if nk.contextualBegin(100, 100, x, y, w, h) then - nk.layoutRow('dynamic', 30, 1) - nk.contextualItem('Item A') - nk.contextualItem('Item B') - nk.contextualEnd() + local x, y, w, h = ui:widgetBounds() + if ui:contextualBegin(100, 100, x, y, w, h) then + ui:layoutRow('dynamic', 30, 1) + ui:contextualItem('Item A') + ui:contextualItem('Item B') + ui:contextualEnd() end - nk.label('Contextual (Right click me)') - nk.groupEnd() + ui:label('Contextual (Right click me)') + ui:groupEnd() + end end - nk.windowEnd() + ui:windowEnd() end diff --git a/example/skin.lua b/example/skin.lua index 779a58c..b189491 100644 --- a/example/skin.lua +++ b/example/skin.lua @@ -1,5 +1,3 @@ -local nk = require 'nuklear' - local windowHeader = love.graphics.newImage 'skin/window_header.png' local checkboxSkin = love.graphics.newImage 'skin/checkbox_false.png' local checkboxCheck = love.graphics.newImage 'skin/checkbox_true.png' @@ -45,18 +43,18 @@ local style = { local check = {value = false} -return function () - nk.stylePush(style) - if nk.windowBegin('Skin Example', 200, 200, 350, 200, 'title', 'movable') then - nk.layoutSpaceBegin('dynamic', 150, 3) - nk.layoutSpacePush(0.14, 0.15, 0.72, 0.3) - nk.label('Skin example! Styles can change skins, colors, padding, font, and more.', 'wrap') - nk.layoutSpacePush(0.2, 0.55, 0.2, 0.2) - nk.button('Button') - nk.layoutSpacePush(0.55, 0.55, 0.3, 0.2) - nk.checkbox('Checkbox', check) - nk.layoutSpaceEnd() +return function (ui) + ui:stylePush(style) + if ui:windowBegin('Skin Example', 200, 200, 350, 200, 'title', 'movable') then + ui:layoutSpaceBegin('dynamic', 150, 3) + ui:layoutSpacePush(0.14, 0.15, 0.72, 0.3) + ui:label('Skin example! Styles can change skins, colors, padding, font, and more.', 'wrap') + ui:layoutSpacePush(0.2, 0.55, 0.2, 0.2) + ui:button('Button') + ui:layoutSpacePush(0.55, 0.55, 0.3, 0.2) + ui:checkbox('Checkbox', check) + ui:layoutSpaceEnd() end - nk.windowEnd() - nk.stylePop() + ui:windowEnd() + ui:stylePop() end diff --git a/example/style.lua b/example/style.lua index 91eee25..88d84c6 100644 --- a/example/style.lua +++ b/example/style.lua @@ -1,6 +1,6 @@ -- Show off some of the styling options. -local nk = require 'nuklear' +local nuklear = require "nuklear" local colors = { ['text'] = '#afafaf', @@ -41,28 +41,29 @@ end table.sort(colorNames) -return function () - nk.styleLoadColors(colors) - nk.windowBegin('Style', 400, 50, 350, 450, 'border', 'movable', 'title', 'scrollbar') - nk.layoutRow('dynamic', 25, 2) +return function (ui) + ui:styleLoadColors(colors) + if ui:windowBegin('Style', 400, 50, 350, 450, 'border', 'movable', 'title', 'scrollbar') then + ui:layoutRow('dynamic', 25, 2) for _,name in ipairs(colorNames) do - nk.label(name..':') + ui:label(name..':') local color = colors[name] - if nk.comboboxBegin(nil, color, 200, 200) then - nk.layoutRow('dynamic', 90, 1) - color = nk.colorPicker(color) + if ui:comboboxBegin(nil, color, 200, 200) then + ui:layoutRow('dynamic', 90, 1) + color = ui:colorPicker(color) colors[name] = color - local r, g, b = nk.colorParseRGBA(color) - nk.layoutRow('dynamic', 25, {.25, .75}) - nk.label('R: '..r) - r = nk.slider(0, r, 255, 1) - nk.label('G: '..g) - g = nk.slider(0, g, 255, 1) - nk.label('B: '..b) - b = nk.slider(0, b, 255, 1) - colors[name] = nk.colorRGBA(r, g, b) - nk.comboboxEnd() + local r, g, b = nuklear.colorParseRGBA(color) + ui:layoutRow('dynamic', 25, {.25, .75}) + ui:label('R: '..r) + r = ui:slider(0, r, 255, 1) + ui:label('G: '..g) + g = ui:slider(0, g, 255, 1) + ui:label('B: '..b) + b = ui:slider(0, b, 255, 1) + colors[name] = nuklear.colorRGBA(r, g, b) + ui:comboboxEnd() end end - nk.windowEnd() + end + ui:windowEnd() end diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 7ee6353..d688c9c 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -40,556 +40,80 @@ #define NK_LOVE_MAX_RATIOS 1024 static lua_State *L; -static struct nk_context context; -static struct nk_user_font *fonts; -static int font_count; static char *edit_buffer; static const char **combobox_items; -static struct nk_cursor cursors[NK_CURSOR_COUNT]; -static float *floats; -static int layout_ratio_count; +static float *points; -static void nk_love_configureGraphics(int line_thickness, struct nk_color col) +struct nk_love_context { + struct nk_context nkctx; + struct nk_user_font *fonts; + int font_count; + float *layout_ratios; + int layout_ratio_count; +} *context; + +static void nk_love_assert(int pass, const char *msg) { - lua_getglobal(L, "love"); - lua_getfield(L, -1, "graphics"); - lua_remove(L, -2); - if (line_thickness >= 0) { - lua_getfield(L, -1, "setLineWidth"); - lua_pushnumber(L, line_thickness); - lua_call(L, 1, 0); + if (!pass) { + lua_Debug ar; + ar.name = NULL; + if (lua_getstack(L, 0, &ar)) + lua_getinfo(L, "n", &ar); + if (ar.name == NULL) + ar.name = "?"; + luaL_error(L, msg, ar.name); } - lua_getfield(L, -1, "setColor"); - lua_pushnumber(L, col.r); - lua_pushnumber(L, col.g); - lua_pushnumber(L, col.b); - lua_pushnumber(L, col.a); - lua_call(L, 4, 0); } -static void nk_love_getGraphics(float *line_thickness, struct nk_color *color) +static void nk_love_assert_argc(int pass) { - lua_getglobal(L, "love"); - lua_getfield(L, -1, "graphics"); - lua_getfield(L, -1, "getLineWidth"); - lua_call(L, 0, 1); - *line_thickness = lua_tonumber(L, -1); - lua_pop(L, 1); - lua_getfield(L, -1, "getColor"); - lua_call(L, 0, 4); - color->r = lua_tointeger(L, -4); - color->g = lua_tointeger(L, -3); - color->b = lua_tointeger(L, -2); - color->a = lua_tointeger(L, -1); - lua_pop(L, 6); + nk_love_assert(pass, "wrong number of arguments to '%s'"); } -static void nk_love_scissor(int x, int y, int w, int h) +static void nk_love_assert_alloc(void *mem) { - lua_getglobal(L, "love"); - lua_getfield(L, -1, "graphics"); - lua_getfield(L, -1, "setScissor"); - lua_pushnumber(L, x); - lua_pushnumber(L, y); - lua_pushnumber(L, w); - lua_pushnumber(L, h); - lua_call(L, 4, 0); - lua_pop(L, 2); + nk_love_assert(mem != NULL, "out of memory in '%s'"); } -static void nk_love_draw_line(int x0, int y0, int x1, int y1, - int line_thickness, struct nk_color col) +static void *nk_love_malloc(size_t size) { - nk_love_configureGraphics(line_thickness, col); - lua_getfield(L, -1, "line"); - lua_pushnumber(L, x0); - lua_pushnumber(L, y0); - lua_pushnumber(L, x1); - lua_pushnumber(L, y1); - lua_call(L, 4, 0); - lua_pop(L, 1); + void *mem = malloc(size); + nk_love_assert_alloc(mem); + return mem; } -static void nk_love_draw_rect(int x, int y, unsigned int w, - unsigned int h, unsigned int r, int line_thickness, - struct nk_color col) +static struct nk_love_context *nk_love_checkcontext(int index) { - nk_love_configureGraphics(line_thickness, col); - lua_getfield(L, -1, "rectangle"); - if (line_thickness >= 0) { - lua_pushstring(L, "line"); - } else { - lua_pushstring(L, "fill"); + if (index < 0) + index += lua_gettop(L) + 1; + if (lua_isuserdata(L, index)) { + lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); + lua_getfield(L, -1, "metatable"); + lua_getmetatable(L, index); + int is_context = lua_equal(L, -1, -2); + lua_pop(L, 3); + if (is_context) + return lua_touserdata(L, index); } - lua_pushnumber(L, x); - lua_pushnumber(L, y); - lua_pushnumber(L, w); - lua_pushnumber(L, h); - lua_pushnumber(L, r); - lua_pushnumber(L, r); - lua_call(L, 7, 0); - lua_pop(L, 1); + luaL_typerror(L, index, "Nuklear context"); } -static void nk_love_draw_triangle(int x0, int y0, int x1, int y1, - int x2, int y2, int line_thickness, struct nk_color col) +static void nk_love_assert_context(int index) { - nk_love_configureGraphics(line_thickness, col); - lua_getfield(L, -1, "polygon"); - if (line_thickness >= 0) { - lua_pushstring(L, "line"); - } else { - lua_pushstring(L, "fill"); - } - lua_pushnumber(L, x0); - lua_pushnumber(L, y0); - lua_pushnumber(L, x1); - lua_pushnumber(L, y1); - lua_pushnumber(L, x2); - lua_pushnumber(L, y2); - lua_call(L, 7, 0); - lua_pop(L, 1); + struct nk_love_context *ctx = nk_love_checkcontext(index); + nk_love_assert(ctx == context, "%s: UI calls must reside between ui:frameBegin and ui:frameEnd"); } -static void nk_love_draw_polygon(const struct nk_vec2i *pnts, int count, - int line_thickness, struct nk_color col) -{ - nk_love_configureGraphics(line_thickness, col); - lua_getfield(L, -1, "polygon"); - if (line_thickness >= 0) { - lua_pushstring(L, "line"); - } else { - lua_pushstring(L, "fill"); - } - int i; - for (i = 0; (i < count) && (i < NK_LOVE_MAX_POINTS); ++i) { - lua_pushnumber(L, pnts[i].x); - lua_pushnumber(L, pnts[i].y); - } - lua_call(L, 1 + count * 2, 0); - lua_pop(L, 1); -} - -static void nk_love_draw_polyline(const struct nk_vec2i *pnts, - int count, int line_thickness, struct nk_color col) -{ - nk_love_configureGraphics(line_thickness, col); - lua_getfield(L, -1, "line"); - int i; - for (i = 0; (i < count) && (i < NK_LOVE_MAX_POINTS); ++i) { - lua_pushnumber(L, pnts[i].x); - lua_pushnumber(L, pnts[i].y); - } - lua_call(L, count * 2, 0); - lua_pop(L, 1); -} - -static void nk_love_draw_circle(int x, int y, unsigned int w, - unsigned int h, int line_thickness, struct nk_color col) -{ - nk_love_configureGraphics(line_thickness, col); - lua_getfield(L, -1, "ellipse"); - if (line_thickness >= 0) { - lua_pushstring(L, "line"); - } else { - lua_pushstring(L, "fill"); - } - lua_pushnumber(L, x + w / 2); - lua_pushnumber(L, y + h / 2); - lua_pushnumber(L, w / 2); - lua_pushnumber(L, h / 2); - lua_call(L, 5, 0); - lua_pop(L, 1); -} - -static void nk_love_draw_curve(struct nk_vec2i p1, struct nk_vec2i p2, - struct nk_vec2i p3, struct nk_vec2i p4, unsigned int num_segments, - int line_thickness, struct nk_color col) -{ - unsigned int i_step; - float t_step; - struct nk_vec2i last = p1; - - if (num_segments < 1) { - num_segments = 1; - } - t_step = 1.0f/(float)num_segments; - nk_love_configureGraphics(line_thickness, col); - lua_getfield(L, -1, "line"); - for (i_step = 1; i_step <= num_segments; ++i_step) { - float t = t_step * (float)i_step; - float u = 1.0f - t; - float w1 = u*u*u; - float w2 = 3*u*u*t; - float w3 = 3*u*t*t; - float w4 = t * t *t; - float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x; - float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y; - lua_pushnumber(L, x); - lua_pushnumber(L, y); - } - lua_call(L, num_segments * 2, 0); - lua_pop(L, 1); -} - -static float nk_love_get_text_width(nk_handle handle, float height, - const char *text, int len) +static void nk_love_pushregistry(const char *name) { lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); - lua_getfield(L, -1, "font"); - lua_rawgeti(L, -1, handle.id); - lua_getfield(L, -1, "getWidth"); - lua_insert(L, -2); - lua_pushlstring(L, text, len); - lua_call(L, 2, 1); - float width = lua_tonumber(L, -1); - lua_pop(L, 3); - return width; -} - -static void nk_love_draw_text(int fontref, struct nk_color cbg, - struct nk_color cfg, int x, int y, unsigned int w, unsigned int h, - float height, int len, const char *text) -{ - lua_getglobal(L, "love"); - lua_getfield(L, -1, "graphics"); - - lua_getfield(L, -1, "setColor"); - lua_pushnumber(L, cbg.r); - lua_pushnumber(L, cbg.g); - lua_pushnumber(L, cbg.b); - lua_pushnumber(L, cbg.a); - lua_call(L, 4, 0); - - lua_getfield(L, -1, "rectangle"); - lua_pushstring(L, "fill"); - lua_pushnumber(L, x); - lua_pushnumber(L, y); - lua_pushnumber(L, w); - lua_pushnumber(L, h); - lua_call(L, 5, 0); - - lua_getfield(L, -1, "setColor"); - lua_pushnumber(L, cfg.r); - lua_pushnumber(L, cfg.g); - lua_pushnumber(L, cfg.b); - lua_pushnumber(L, cfg.a); - lua_call(L, 4, 0); - - lua_getfield(L, -1, "setFont"); - lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); - lua_getfield(L, -1, "font"); - lua_rawgeti(L, -1, fontref); + lua_pushlightuserdata(L, context); + lua_gettable(L, -2); + lua_getfield(L, -1, name); lua_replace(L, -3); lua_pop(L, 1); - lua_call(L, 1, 0); - - lua_getfield(L, -1, "print"); - lua_pushlstring(L, text, len); - lua_pushnumber(L, x); - lua_pushnumber(L, y); - lua_call(L, 3, 0); - - lua_pop(L, 2); } -static void interpolate_color(struct nk_color c1, struct nk_color c2, - struct nk_color *result, float fraction) -{ - float r = c1.r + (c2.r - c1.r) * fraction; - float g = c1.g + (c2.g - c1.g) * fraction; - float b = c1.b + (c2.b - c1.b) * fraction; - float a = c1.a + (c2.a - c1.a) * fraction; - - result->r = (nk_byte)NK_CLAMP(0, r, 255); - result->g = (nk_byte)NK_CLAMP(0, g, 255); - result->b = (nk_byte)NK_CLAMP(0, b, 255); - result->a = (nk_byte)NK_CLAMP(0, a, 255); -} - -static void nk_love_draw_rect_multi_color(int x, int y, unsigned int w, - unsigned int h, struct nk_color left, struct nk_color top, - struct nk_color right, struct nk_color bottom) -{ - lua_getglobal(L, "love"); - lua_getfield(L, -1, "graphics"); - - lua_getfield(L, -1, "push"); - lua_pushstring(L, "all"); - lua_call(L, 1, 0); - lua_getfield(L, -1, "setColor"); - lua_pushnumber(L, 255); - lua_pushnumber(L, 255); - lua_pushnumber(L, 255); - lua_call(L, 3, 0); - lua_getfield(L, -1, "setPointSize"); - lua_pushnumber(L, 1); - lua_call(L, 1, 0); - - struct nk_color X1, X2, Y; - float fraction_x, fraction_y; - int i,j; - - lua_getfield(L, -1, "points"); - lua_createtable(L, w * h, 0); - - for (j = 0; j < h; j++) { - fraction_y = ((float)j) / h; - for (i = 0; i < w; i++) { - fraction_x = ((float)i) / w; - interpolate_color(left, top, &X1, fraction_x); - interpolate_color(right, bottom, &X2, fraction_x); - interpolate_color(X1, X2, &Y, fraction_y); - lua_createtable(L, 6, 0); - lua_pushnumber(L, x + i); - lua_rawseti(L, -2, 1); - lua_pushnumber(L, y + j); - lua_rawseti(L, -2, 2); - lua_pushnumber(L, Y.r); - lua_rawseti(L, -2, 3); - lua_pushnumber(L, Y.g); - lua_rawseti(L, -2, 4); - lua_pushnumber(L, Y.b); - lua_rawseti(L, -2, 5); - lua_pushnumber(L, Y.a); - lua_rawseti(L, -2, 6); - lua_rawseti(L, -2, i + j * w + 1); - } - } - - lua_call(L, 1, 0); - lua_getfield(L, -1, "pop"); - lua_call(L, 0, 0); - lua_pop(L, 2); -} - -static void nk_love_clear(struct nk_color col) -{ - lua_getglobal(L, "love"); - lua_getfield(L, -1, "graphics"); - lua_getfield(L, -1, "clear"); - lua_pushnumber(L, col.r); - lua_pushnumber(L, col.g); - lua_pushnumber(L, col.b); - lua_pushnumber(L, col.a); - lua_call(L, 4, 0); - lua_pop(L, 2); -} - -static void nk_love_blit() -{ - lua_getglobal(L, "love"); - lua_getfield(L, -1, "graphics"); - lua_getfield(L, -1, "present"); - lua_call(L, 0, 0); - lua_pop(L, 2); -} - -static void nk_love_draw_image(int x, int y, unsigned int w, unsigned int h, - struct nk_image image, struct nk_color color) -{ - nk_love_configureGraphics(-1, color); - lua_getfield(L, -1, "draw"); - lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); - lua_getfield(L, -1, "image"); - lua_rawgeti(L, -1, image.handle.id); - lua_getfield(L, -5, "newQuad"); - lua_pushnumber(L, image.region[0]); - lua_pushnumber(L, image.region[1]); - lua_pushnumber(L, image.region[2]); - lua_pushnumber(L, image.region[3]); - lua_pushnumber(L, image.w); - lua_pushnumber(L, image.h); - lua_call(L, 6, 1); - lua_replace(L, -3); - lua_replace(L, -3); - lua_pushnumber(L, x); - lua_pushnumber(L, y); - lua_pushnumber(L, 0); - lua_pushnumber(L, (double) w / image.region[2]); - lua_pushnumber(L, (double) h / image.region[3]); - lua_call(L, 7, 0); - lua_pop(L, 1); -} - -static void nk_love_draw_arc(int cx, int cy, unsigned int r, - int line_thickness, float a1, float a2, struct nk_color color) -{ - nk_love_configureGraphics(line_thickness, color); - lua_getfield(L, -1, "arc"); - if (line_thickness >= 0) { - lua_pushstring(L, "line"); - } else { - lua_pushstring(L, "fill"); - } - lua_pushnumber(L, cx); - lua_pushnumber(L, cy); - lua_pushnumber(L, r); - lua_pushnumber(L, a1); - lua_pushnumber(L, a2); - lua_call(L, 6, 0); - lua_pop(L, 1); -} - -static void nk_love_clipbard_paste(nk_handle usr, struct nk_text_edit *edit) -{ - (void)usr; - lua_getglobal(L, "love"); - lua_getfield(L, -1, "system"); - lua_getfield(L, -1, "getClipboardText"); - lua_call(L, 0, 1); - const char *text = lua_tostring(L, -1); - if (text) nk_textedit_paste(edit, text, nk_strlen(text)); - lua_pop(L, 3); -} - -static void nk_love_clipbard_copy(nk_handle usr, const char *text, int len) -{ - (void)usr; - char *str = 0; - if (!len) return; - str = (char*)malloc((size_t)len+1); - if (!str) return; - memcpy(str, text, (size_t)len); - str[len] = '\0'; - lua_getglobal(L, "love"); - lua_getfield(L, -1, "system"); - lua_getfield(L, -1, "setClipboardText"); - lua_pushstring(L, str); - free(str); - lua_call(L, 1, 0); - lua_pop(L, 2); -} - -static int nk_love_is_active(struct nk_context *ctx) -{ - struct nk_window *iter; - NK_ASSERT(ctx); - if (!ctx) return 0; - iter = ctx->begin; - while (iter) { - /* check if window is being hovered */ - if (iter->flags & NK_WINDOW_MINIMIZED) { - struct nk_rect header = iter->bounds; - header.h = ctx->style.font->height + 2 * ctx->style.window.header.padding.y; - if (nk_input_is_mouse_hovering_rect(&ctx->input, header)) - return 1; - } else if (nk_input_is_mouse_hovering_rect(&ctx->input, iter->bounds)) { - return 1; - } - /* check if window popup is being hovered */ - if (iter->popup.active && iter->popup.win && nk_input_is_mouse_hovering_rect(&ctx->input, iter->popup.win->bounds)) - return 1; - if (iter->edit.active & NK_EDIT_ACTIVE) - return 1; - iter = iter->next; - } - return 0; -} - -static int nk_love_keyevent(const char *key, const char *scancode, - int isrepeat, int down) -{ - lua_getglobal(L, "love"); - lua_getfield(L, -1, "keyboard"); - lua_getfield(L, -1, "isScancodeDown"); - lua_pushstring(L, "lctrl"); - lua_call(L, 1, 1); - int lctrl = lua_toboolean(L, -1); - lua_pop(L, 3); - - if (!strcmp(key, "rshift") || !strcmp(key, "lshift")) - nk_input_key(&context, NK_KEY_SHIFT, down); - else if (!strcmp(key, "delete")) - nk_input_key(&context, NK_KEY_DEL, down); - else if (!strcmp(key, "return")) - nk_input_key(&context, NK_KEY_ENTER, down); - else if (!strcmp(key, "tab")) - nk_input_key(&context, NK_KEY_TAB, down); - else if (!strcmp(key, "backspace")) - nk_input_key(&context, NK_KEY_BACKSPACE, down); - else if (!strcmp(key, "home")) { - nk_input_key(&context, NK_KEY_TEXT_LINE_START, down); - } else if (!strcmp(key, "end")) { - nk_input_key(&context, NK_KEY_TEXT_LINE_END, down); - } else if (!strcmp(key, "pagedown")) { - nk_input_key(&context, NK_KEY_SCROLL_DOWN, down); - } else if (!strcmp(key, "pageup")) { - nk_input_key(&context, NK_KEY_SCROLL_UP, down); - } else if (!strcmp(key, "z")) - nk_input_key(&context, NK_KEY_TEXT_UNDO, down && lctrl); - else if (!strcmp(key, "r")) - nk_input_key(&context, NK_KEY_TEXT_REDO, down && lctrl); - else if (!strcmp(key, "c")) - nk_input_key(&context, NK_KEY_COPY, down && lctrl); - else if (!strcmp(key, "v")) - nk_input_key(&context, NK_KEY_PASTE, down && lctrl); - else if (!strcmp(key, "x")) - nk_input_key(&context, NK_KEY_CUT, down && lctrl); - else if (!strcmp(key, "b")) - nk_input_key(&context, NK_KEY_TEXT_LINE_START, down && lctrl); - else if (!strcmp(key, "e")) - nk_input_key(&context, NK_KEY_TEXT_LINE_END, down && lctrl); - else if (!strcmp(key, "left")) { - if (lctrl) - nk_input_key(&context, NK_KEY_TEXT_WORD_LEFT, down); - else - nk_input_key(&context, NK_KEY_LEFT, down); - } else if (!strcmp(key, "right")) { - if (lctrl) - nk_input_key(&context, NK_KEY_TEXT_WORD_RIGHT, down); - else - nk_input_key(&context, NK_KEY_RIGHT, down); - } else if (!strcmp(key, "up")) - nk_input_key(&context, NK_KEY_UP, down); - else if (!strcmp(key, "down")) - nk_input_key(&context, NK_KEY_DOWN, down); - else - return 0; - return nk_love_is_active(&context); -} - -static int nk_love_clickevent(int x, int y, int button, int istouch, int down) -{ - if (button == 1) - nk_input_button(&context, NK_BUTTON_LEFT, x, y, down); - else if (button == 3) - nk_input_button(&context, NK_BUTTON_MIDDLE, x, y, down); - else if (button == 2) - nk_input_button(&context, NK_BUTTON_RIGHT, x, y, down); - else - return 0; - return nk_window_is_any_hovered(&context); -} - -static int nk_love_mousemoved_event(int x, int y, int dx, int dy, int istouch) -{ - nk_input_motion(&context, x, y); - return nk_window_is_any_hovered(&context); -} - -static int nk_love_textinput_event(const char *text) -{ - nk_rune rune; - nk_utf_decode(text, &rune, strlen(text)); - nk_input_unicode(&context, rune); - return nk_love_is_active(&context); -} - -static int nk_love_wheelmoved_event(int x, int y) -{ - nk_input_scroll(&context,(float)y); - return nk_window_is_any_hovered(&context); -} - -/* - * =============================================================== - * - * WRAPPER - * - * =============================================================== - */ - static int nk_love_is_type(int index, const char *type) { if (index < 0) @@ -610,14 +134,27 @@ static int nk_love_is_type(int index, const char *type) return 0; } +static float nk_love_get_text_width(nk_handle handle, float height, + const char *text, int len) +{ + nk_love_pushregistry("font"); + lua_rawgeti(L, -1, handle.id); + lua_getfield(L, -1, "getWidth"); + lua_replace(L, -3); + lua_pushlstring(L, text, len); + lua_call(L, 2, 1); + float width = lua_tonumber(L, -1); + lua_pop(L, 1); + return width; +} + static void nk_love_checkFont(int index, struct nk_user_font *font) { if (index < 0) index += lua_gettop(L) + 1; if (!nk_love_is_type(index, "Font")) luaL_typerror(L, index, "Font"); - lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); - lua_getfield(L, -1, "font"); + nk_love_pushregistry("font"); lua_pushvalue(L, index); int ref = luaL_ref(L, -2); lua_getfield(L, index, "getHeight"); @@ -627,7 +164,7 @@ static void nk_love_checkFont(int index, struct nk_user_font *font) font->userdata = nk_handle_id(ref); font->height = height; font->width = nk_love_get_text_width; - lua_pop(L, 3); + lua_pop(L, 2); } static void nk_love_checkImage(int index, struct nk_image *image) @@ -636,8 +173,7 @@ static void nk_love_checkImage(int index, struct nk_image *image) index += lua_gettop(L) + 1; if (!nk_love_is_type(index, "Image")) luaL_typerror(L, index, "Image"); - lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); - lua_getfield(L, -1, "image"); + nk_love_pushregistry("image"); lua_pushvalue(L, index); int ref = luaL_ref(L, -2); lua_getfield(L, index, "getDimensions"); @@ -652,7 +188,7 @@ static void nk_love_checkImage(int index, struct nk_image *image) image->region[1] = 0; image->region[2] = width; image->region[3] = height; - lua_pop(L, 4); + lua_pop(L, 3); } static int nk_love_is_hex(char c) @@ -704,7 +240,8 @@ static struct nk_color nk_love_checkcolor(int index) return color; } -static nk_flags nk_love_parse_window_flags(int flags_begin) { +static nk_flags nk_love_parse_window_flags(int flags_begin) +{ int argc = lua_gettop(L); nk_flags flags = NK_WINDOW_NO_SCROLLBAR; int i; @@ -821,7 +358,8 @@ static enum nk_buttons nk_love_checkbutton(int index) } } -static enum nk_layout_format nk_love_checkformat(int index) { +static enum nk_layout_format nk_love_checkformat(int index) +{ if (index < 0) index += lua_gettop(L) + 1; const char *type = luaL_checkstring(L, index); @@ -835,7 +373,8 @@ static enum nk_layout_format nk_love_checkformat(int index) { } } -static enum nk_tree_type nk_love_checktree(int index) { +static enum nk_tree_type nk_love_checktree(int index) +{ if (index < 0) index += lua_gettop(L) + 1; const char *type_string = luaL_checkstring(L, index); @@ -849,7 +388,8 @@ static enum nk_tree_type nk_love_checktree(int index) { } } -static enum nk_collapse_states nk_love_checkstate(int index) { +static enum nk_collapse_states nk_love_checkstate(int index) +{ if (index < 0) index += lua_gettop(L) + 1; const char *state_string = luaL_checkstring(L, index); @@ -863,7 +403,8 @@ static enum nk_collapse_states nk_love_checkstate(int index) { } } -static enum nk_button_behavior nk_love_checkbehavior(int index) { +static enum nk_button_behavior nk_love_checkbehavior(int index) +{ if (index < 0) index += lua_gettop(L) + 1; const char *behavior_string = luaL_checkstring(L, index); @@ -877,7 +418,8 @@ static enum nk_button_behavior nk_love_checkbehavior(int index) { } } -static enum nk_color_format nk_love_checkcolorformat(int index) { +static enum nk_color_format nk_love_checkcolorformat(int index) +{ if (index < 0) index += lua_gettop(L) + 1; const char *format_string = luaL_checkstring(L, index); @@ -891,7 +433,8 @@ static enum nk_color_format nk_love_checkcolorformat(int index) { } } -static nk_flags nk_love_checkedittype(int index) { +static nk_flags nk_love_checkedittype(int index) +{ if (index < 0) index += lua_gettop(L) + 1; const char *type_string = luaL_checkstring(L, index); @@ -907,7 +450,8 @@ static nk_flags nk_love_checkedittype(int index) { } } -static enum nk_popup_type nk_love_checkpopup(int index) { +static enum nk_popup_type nk_love_checkpopup(int index) +{ if (index < 0) index += lua_gettop(L) + 1; const char *popup_type = luaL_checkstring(L, index); @@ -923,7 +467,8 @@ static enum nk_popup_type nk_love_checkpopup(int index) { enum nk_love_draw_mode {NK_LOVE_FILL, NK_LOVE_LINE}; -static enum nk_love_draw_mode nk_love_checkdraw(int index) { +static enum nk_love_draw_mode nk_love_checkdraw(int index) +{ if (index < 0) index += lua_gettop(L) + 1; const char *mode = luaL_checkstring(L, index); @@ -945,161 +490,640 @@ static int nk_love_checkboolean(lua_State *L, int index) return lua_toboolean(L, index); } -static void nk_love_assert(int pass, const char *msg) { - if (!pass) { - lua_Debug ar; - ar.name = NULL; - if (lua_getstack(L, 0, &ar)) - lua_getinfo(L, "n", &ar); - if (ar.name == NULL) - ar.name = "?"; - luaL_error(L, msg, ar.name); - } -} - -static void nk_love_assert_argc(int pass) { - nk_love_assert(pass, "wrong number of arguments to '%s'"); -} - -static void nk_love_assert_alloc(void *mem) { - nk_love_assert(mem != NULL, "out of memory in '%s'"); -} - -static void *nk_love_malloc(size_t size) { - void *mem = malloc(size); - nk_love_assert_alloc(mem); - return mem; -} - -static int nk_love_init(lua_State *luaState) +static void nk_love_configureGraphics(int line_thickness, struct nk_color col) +{ + lua_getglobal(L, "love"); + lua_getfield(L, -1, "graphics"); + lua_remove(L, -2); + if (line_thickness >= 0) { + lua_getfield(L, -1, "setLineWidth"); + lua_pushnumber(L, line_thickness); + lua_call(L, 1, 0); + } + lua_getfield(L, -1, "setColor"); + lua_pushnumber(L, col.r); + lua_pushnumber(L, col.g); + lua_pushnumber(L, col.b); + lua_pushnumber(L, col.a); + lua_call(L, 4, 0); +} + +static void nk_love_getGraphics(float *line_thickness, struct nk_color *color) +{ + lua_getglobal(L, "love"); + lua_getfield(L, -1, "graphics"); + lua_getfield(L, -1, "getLineWidth"); + lua_call(L, 0, 1); + *line_thickness = lua_tonumber(L, -1); + lua_pop(L, 1); + lua_getfield(L, -1, "getColor"); + lua_call(L, 0, 4); + color->r = lua_tointeger(L, -4); + color->g = lua_tointeger(L, -3); + color->b = lua_tointeger(L, -2); + color->a = lua_tointeger(L, -1); + lua_pop(L, 6); +} + +static void nk_love_scissor(int x, int y, int w, int h) +{ + lua_getglobal(L, "love"); + lua_getfield(L, -1, "graphics"); + lua_getfield(L, -1, "setScissor"); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + lua_pushnumber(L, w); + lua_pushnumber(L, h); + lua_call(L, 4, 0); + lua_pop(L, 2); +} + +static void nk_love_draw_line(int x0, int y0, int x1, int y1, + int line_thickness, struct nk_color col) +{ + nk_love_configureGraphics(line_thickness, col); + lua_getfield(L, -1, "line"); + lua_pushnumber(L, x0); + lua_pushnumber(L, y0); + lua_pushnumber(L, x1); + lua_pushnumber(L, y1); + lua_call(L, 4, 0); + lua_pop(L, 1); +} + +static void nk_love_draw_rect(int x, int y, unsigned int w, + unsigned int h, unsigned int r, int line_thickness, + struct nk_color col) +{ + nk_love_configureGraphics(line_thickness, col); + lua_getfield(L, -1, "rectangle"); + if (line_thickness >= 0) + lua_pushstring(L, "line"); + else + lua_pushstring(L, "fill"); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + lua_pushnumber(L, w); + lua_pushnumber(L, h); + lua_pushnumber(L, r); + lua_pushnumber(L, r); + lua_call(L, 7, 0); + lua_pop(L, 1); +} + +static void nk_love_draw_triangle(int x0, int y0, int x1, int y1, + int x2, int y2, int line_thickness, struct nk_color col) +{ + nk_love_configureGraphics(line_thickness, col); + lua_getfield(L, -1, "polygon"); + if (line_thickness >= 0) + lua_pushstring(L, "line"); + else + lua_pushstring(L, "fill"); + lua_pushnumber(L, x0); + lua_pushnumber(L, y0); + lua_pushnumber(L, x1); + lua_pushnumber(L, y1); + lua_pushnumber(L, x2); + lua_pushnumber(L, y2); + lua_call(L, 7, 0); + lua_pop(L, 1); +} + +static void nk_love_draw_polygon(const struct nk_vec2i *pnts, int count, + int line_thickness, struct nk_color col) +{ + nk_love_configureGraphics(line_thickness, col); + lua_getfield(L, -1, "polygon"); + if (line_thickness >= 0) + lua_pushstring(L, "line"); + else + lua_pushstring(L, "fill"); + int i; + for (i = 0; (i < count) && (i < NK_LOVE_MAX_POINTS); ++i) { + lua_pushnumber(L, pnts[i].x); + lua_pushnumber(L, pnts[i].y); + } + lua_call(L, 1 + count * 2, 0); + lua_pop(L, 1); +} + +static void nk_love_draw_polyline(const struct nk_vec2i *pnts, + int count, int line_thickness, struct nk_color col) +{ + nk_love_configureGraphics(line_thickness, col); + lua_getfield(L, -1, "line"); + int i; + for (i = 0; (i < count) && (i < NK_LOVE_MAX_POINTS); ++i) { + lua_pushnumber(L, pnts[i].x); + lua_pushnumber(L, pnts[i].y); + } + lua_call(L, count * 2, 0); + lua_pop(L, 1); +} + +static void nk_love_draw_circle(int x, int y, unsigned int w, + unsigned int h, int line_thickness, struct nk_color col) +{ + nk_love_configureGraphics(line_thickness, col); + lua_getfield(L, -1, "ellipse"); + if (line_thickness >= 0) + lua_pushstring(L, "line"); + else + lua_pushstring(L, "fill"); + lua_pushnumber(L, x + w / 2); + lua_pushnumber(L, y + h / 2); + lua_pushnumber(L, w / 2); + lua_pushnumber(L, h / 2); + lua_call(L, 5, 0); + lua_pop(L, 1); +} + +static void nk_love_draw_curve(struct nk_vec2i p1, struct nk_vec2i p2, + struct nk_vec2i p3, struct nk_vec2i p4, unsigned int num_segments, + int line_thickness, struct nk_color col) +{ + unsigned int i_step; + float t_step; + struct nk_vec2i last = p1; + + if (num_segments < 1) + num_segments = 1; + t_step = 1.0f/(float)num_segments; + nk_love_configureGraphics(line_thickness, col); + lua_getfield(L, -1, "line"); + for (i_step = 1; i_step <= num_segments; ++i_step) { + float t = t_step * (float)i_step; + float u = 1.0f - t; + float w1 = u*u*u; + float w2 = 3*u*u*t; + float w3 = 3*u*t*t; + float w4 = t * t *t; + float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x; + float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y; + lua_pushnumber(L, x); + lua_pushnumber(L, y); + } + lua_call(L, num_segments * 2, 0); + lua_pop(L, 1); +} + +static void nk_love_draw_text(int fontref, struct nk_color cbg, + struct nk_color cfg, int x, int y, unsigned int w, unsigned int h, + float height, int len, const char *text) +{ + lua_getglobal(L, "love"); + lua_getfield(L, -1, "graphics"); + + lua_getfield(L, -1, "setColor"); + lua_pushnumber(L, cbg.r); + lua_pushnumber(L, cbg.g); + lua_pushnumber(L, cbg.b); + lua_pushnumber(L, cbg.a); + lua_call(L, 4, 0); + + lua_getfield(L, -1, "rectangle"); + lua_pushstring(L, "fill"); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + lua_pushnumber(L, w); + lua_pushnumber(L, h); + lua_call(L, 5, 0); + + lua_getfield(L, -1, "setColor"); + lua_pushnumber(L, cfg.r); + lua_pushnumber(L, cfg.g); + lua_pushnumber(L, cfg.b); + lua_pushnumber(L, cfg.a); + lua_call(L, 4, 0); + + lua_getfield(L, -1, "setFont"); + nk_love_pushregistry("font"); + lua_rawgeti(L, -1, fontref); + lua_replace(L, -2); + lua_call(L, 1, 0); + + lua_getfield(L, -1, "print"); + lua_pushlstring(L, text, len); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + lua_call(L, 3, 0); + + lua_pop(L, 2); +} + +static void interpolate_color(struct nk_color c1, struct nk_color c2, + struct nk_color *result, float fraction) +{ + float r = c1.r + (c2.r - c1.r) * fraction; + float g = c1.g + (c2.g - c1.g) * fraction; + float b = c1.b + (c2.b - c1.b) * fraction; + float a = c1.a + (c2.a - c1.a) * fraction; + + result->r = (nk_byte)NK_CLAMP(0, r, 255); + result->g = (nk_byte)NK_CLAMP(0, g, 255); + result->b = (nk_byte)NK_CLAMP(0, b, 255); + result->a = (nk_byte)NK_CLAMP(0, a, 255); +} + +static void nk_love_draw_rect_multi_color(int x, int y, unsigned int w, + unsigned int h, struct nk_color left, struct nk_color top, + struct nk_color right, struct nk_color bottom) +{ + lua_getglobal(L, "love"); + lua_getfield(L, -1, "graphics"); + + lua_getfield(L, -1, "push"); + lua_pushstring(L, "all"); + lua_call(L, 1, 0); + lua_getfield(L, -1, "setColor"); + lua_pushnumber(L, 255); + lua_pushnumber(L, 255); + lua_pushnumber(L, 255); + lua_call(L, 3, 0); + lua_getfield(L, -1, "setPointSize"); + lua_pushnumber(L, 1); + lua_call(L, 1, 0); + + struct nk_color X1, X2, Y; + float fraction_x, fraction_y; + int i,j; + + lua_getfield(L, -1, "points"); + lua_createtable(L, w * h, 0); + + for (j = 0; j < h; j++) { + fraction_y = ((float)j) / h; + for (i = 0; i < w; i++) { + fraction_x = ((float)i) / w; + interpolate_color(left, top, &X1, fraction_x); + interpolate_color(right, bottom, &X2, fraction_x); + interpolate_color(X1, X2, &Y, fraction_y); + lua_createtable(L, 6, 0); + lua_pushnumber(L, x + i); + lua_rawseti(L, -2, 1); + lua_pushnumber(L, y + j); + lua_rawseti(L, -2, 2); + lua_pushnumber(L, Y.r); + lua_rawseti(L, -2, 3); + lua_pushnumber(L, Y.g); + lua_rawseti(L, -2, 4); + lua_pushnumber(L, Y.b); + lua_rawseti(L, -2, 5); + lua_pushnumber(L, Y.a); + lua_rawseti(L, -2, 6); + lua_rawseti(L, -2, i + j * w + 1); + } + } + + lua_call(L, 1, 0); + lua_getfield(L, -1, "pop"); + lua_call(L, 0, 0); + lua_pop(L, 2); +} + +static void nk_love_draw_image(int x, int y, unsigned int w, unsigned int h, + struct nk_image image, struct nk_color color) +{ + nk_love_configureGraphics(-1, color); + lua_getfield(L, -1, "draw"); + nk_love_pushregistry("image"); + lua_rawgeti(L, -1, image.handle.id); + lua_replace(L, -2); + lua_getfield(L, -3, "newQuad"); + lua_pushnumber(L, image.region[0]); + lua_pushnumber(L, image.region[1]); + lua_pushnumber(L, image.region[2]); + lua_pushnumber(L, image.region[3]); + lua_pushnumber(L, image.w); + lua_pushnumber(L, image.h); + lua_call(L, 6, 1); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + lua_pushnumber(L, 0); + lua_pushnumber(L, (double) w / image.region[2]); + lua_pushnumber(L, (double) h / image.region[3]); + lua_call(L, 7, 0); + lua_pop(L, 1); +} + +static void nk_love_draw_arc(int cx, int cy, unsigned int r, + int line_thickness, float a1, float a2, struct nk_color color) +{ + nk_love_configureGraphics(line_thickness, color); + lua_getfield(L, -1, "arc"); + if (line_thickness >= 0) + lua_pushstring(L, "line"); + else + lua_pushstring(L, "fill"); + lua_pushnumber(L, cx); + lua_pushnumber(L, cy); + lua_pushnumber(L, r); + lua_pushnumber(L, a1); + lua_pushnumber(L, a2); + lua_call(L, 6, 0); + lua_pop(L, 1); +} + +static void nk_love_clipbard_paste(nk_handle usr, struct nk_text_edit *edit) +{ + (void)usr; + lua_getglobal(L, "love"); + lua_getfield(L, -1, "system"); + lua_getfield(L, -1, "getClipboardText"); + lua_call(L, 0, 1); + const char *text = lua_tostring(L, -1); + if (text) nk_textedit_paste(edit, text, nk_strlen(text)); + lua_pop(L, 3); +} + +static void nk_love_clipbard_copy(nk_handle usr, const char *text, int len) +{ + (void)usr; + char *str = 0; + if (!len) return; + str = (char*)malloc((size_t)len+1); + if (!str) return; + memcpy(str, text, (size_t)len); + str[len] = '\0'; + lua_getglobal(L, "love"); + lua_getfield(L, -1, "system"); + lua_getfield(L, -1, "setClipboardText"); + lua_pushstring(L, str); + free(str); + lua_call(L, 1, 0); + lua_pop(L, 2); +} + +static int nk_love_is_active(struct nk_context *ctx) +{ + struct nk_window *iter; + iter = ctx->begin; + while (iter) { + /* check if window is being hovered */ + if (iter->flags & NK_WINDOW_MINIMIZED) { + struct nk_rect header = iter->bounds; + header.h = ctx->style.font->height + 2 * ctx->style.window.header.padding.y; + if (nk_input_is_mouse_hovering_rect(&ctx->input, header)) + return 1; + } else if (nk_input_is_mouse_hovering_rect(&ctx->input, iter->bounds)) { + return 1; + } + /* check if window popup is being hovered */ + if (iter->popup.active && iter->popup.win && nk_input_is_mouse_hovering_rect(&ctx->input, iter->popup.win->bounds)) + return 1; + if (iter->edit.active & NK_EDIT_ACTIVE) + return 1; + iter = iter->next; + } + return 0; +} + +static int nk_love_keyevent(struct nk_context *ctx, const char *key, + const char *scancode, int isrepeat, int down) +{ + lua_getglobal(L, "love"); + lua_getfield(L, -1, "keyboard"); + lua_getfield(L, -1, "isScancodeDown"); + lua_pushstring(L, "lctrl"); + lua_call(L, 1, 1); + int lctrl = lua_toboolean(L, -1); + lua_pop(L, 3); + + if (!strcmp(key, "rshift") || !strcmp(key, "lshift")) + nk_input_key(ctx, NK_KEY_SHIFT, down); + else if (!strcmp(key, "delete")) + nk_input_key(ctx, NK_KEY_DEL, down); + else if (!strcmp(key, "return")) + nk_input_key(ctx, NK_KEY_ENTER, down); + else if (!strcmp(key, "tab")) + nk_input_key(ctx, NK_KEY_TAB, down); + else if (!strcmp(key, "backspace")) + nk_input_key(ctx, NK_KEY_BACKSPACE, down); + else if (!strcmp(key, "home")) { + nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down); + } else if (!strcmp(key, "end")) { + nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down); + } else if (!strcmp(key, "pagedown")) { + nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down); + } else if (!strcmp(key, "pageup")) { + nk_input_key(ctx, NK_KEY_SCROLL_UP, down); + } else if (!strcmp(key, "z")) + nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && lctrl); + else if (!strcmp(key, "r")) + nk_input_key(ctx, NK_KEY_TEXT_REDO, down && lctrl); + else if (!strcmp(key, "c")) + nk_input_key(ctx, NK_KEY_COPY, down && lctrl); + else if (!strcmp(key, "v")) + nk_input_key(ctx, NK_KEY_PASTE, down && lctrl); + else if (!strcmp(key, "x")) + nk_input_key(ctx, NK_KEY_CUT, down && lctrl); + else if (!strcmp(key, "b")) + nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && lctrl); + else if (!strcmp(key, "e")) + nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && lctrl); + else if (!strcmp(key, "left")) { + if (lctrl) + nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down); + else + nk_input_key(ctx, NK_KEY_LEFT, down); + } else if (!strcmp(key, "right")) { + if (lctrl) + nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down); + else + nk_input_key(ctx, NK_KEY_RIGHT, down); + } else if (!strcmp(key, "up")) + nk_input_key(ctx, NK_KEY_UP, down); + else if (!strcmp(key, "down")) + nk_input_key(ctx, NK_KEY_DOWN, down); + else + return 0; + return nk_love_is_active(ctx); +} + +static int nk_love_clickevent(struct nk_context *ctx, int x, int y, + int button, int istouch, int down) +{ + if (button == 1) + nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down); + else if (button == 3) + nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down); + else if (button == 2) + nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down); + else + return 0; + return nk_window_is_any_hovered(ctx); +} + +static int nk_love_mousemoved_event(struct nk_context *ctx, int x, int y, + int dx, int dy, int istouch) +{ + nk_input_motion(ctx, x, y); + return nk_window_is_any_hovered(ctx); +} + +static int nk_love_textinput_event(struct nk_context *ctx, const char *text) +{ + nk_rune rune; + nk_utf_decode(text, &rune, strlen(text)); + nk_input_unicode(ctx, rune); + return nk_love_is_active(ctx); +} + +static int nk_love_wheelmoved_event(struct nk_context *ctx, int x, int y) +{ + nk_input_scroll(ctx,(float)y); + return nk_window_is_any_hovered(ctx); +} + +/* + * =============================================================== + * + * WRAPPER + * + * =============================================================== + */ + +static int nk_love_init(lua_State *L) { - L = luaState; nk_love_assert_argc(lua_gettop(L) == 0); + lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); + struct nk_love_context *ctx = lua_newuserdata(L, sizeof(struct nk_love_context)); + nk_love_assert_alloc(ctx); + lua_pushlightuserdata(L, ctx); lua_newtable(L); - lua_pushvalue(L, -1); - lua_setfield(L, LUA_REGISTRYINDEX, "nuklear"); lua_newtable(L); lua_setfield(L, -2, "font"); lua_newtable(L); lua_setfield(L, -2, "image"); lua_newtable(L); lua_setfield(L, -2, "stack"); - fonts = nk_love_malloc(sizeof(struct nk_user_font) * NK_LOVE_MAX_FONTS); + lua_settable(L, -4); + lua_getfield(L, -2, "metatable"); + lua_setmetatable(L, -2); + ctx->fonts = nk_love_malloc(sizeof(struct nk_user_font) * NK_LOVE_MAX_FONTS); lua_getglobal(L, "love"); nk_love_assert(lua_istable(L, -1), "LOVE-Nuklear requires LOVE environment"); lua_getfield(L, -1, "graphics"); lua_getfield(L, -1, "getFont"); lua_call(L, 0, 1); - nk_love_checkFont(-1, &fonts[0]); - nk_init_default(&context, &fonts[0]); - font_count = 1; - context.clip.copy = nk_love_clipbard_copy; - context.clip.paste = nk_love_clipbard_paste; - context.clip.userdata = nk_handle_ptr(0); - edit_buffer = nk_love_malloc(NK_LOVE_EDIT_BUFFER_LEN); - combobox_items = nk_love_malloc(sizeof(char*) * NK_LOVE_COMBOBOX_MAX_ITEMS); - floats = nk_love_malloc(sizeof(float) * NK_MAX(NK_LOVE_MAX_RATIOS, NK_LOVE_MAX_POINTS * 2)); - return 0; + struct nk_love_context *current = context; + context = ctx; + nk_love_checkFont(-1, &ctx->fonts[0]); + context = current; + nk_init_default(&ctx->nkctx, &ctx->fonts[0]); + ctx->font_count = 1; + ctx->nkctx.clip.copy = nk_love_clipbard_copy; + ctx->nkctx.clip.paste = nk_love_clipbard_paste; + ctx->nkctx.clip.userdata = nk_handle_ptr(0); + ctx->layout_ratios = nk_love_malloc(sizeof(float) * NK_LOVE_MAX_RATIOS); + ctx->layout_ratio_count = 0; + lua_pop(L, 3); + return 1; } static int nk_love_shutdown(lua_State *luaState) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_free(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + struct nk_love_context *ctx = nk_love_checkcontext(1); + nk_free(&ctx->nkctx); + free(ctx->fonts); + free(ctx->layout_ratios); + lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); + lua_pushlightuserdata(L, context); lua_pushnil(L); - lua_setfield(L, LUA_REGISTRYINDEX, "nuklear"); - L = NULL; - free(fonts); - fonts = NULL; - free(edit_buffer); - edit_buffer = NULL; - free(combobox_items); - combobox_items = NULL; - free(floats); - floats = NULL; + lua_settable(L, -3); return 0; } static int nk_love_keypressed(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 3); - const char *key = luaL_checkstring(L, 1); - const char *scancode = luaL_checkstring(L, 2); - int isrepeat = nk_love_checkboolean(L, 3); - int consume = nk_love_keyevent(key, scancode, isrepeat, 1); + nk_love_assert_argc(lua_gettop(L) == 4); + struct nk_context *ctx = &nk_love_checkcontext(1)->nkctx; + const char *key = luaL_checkstring(L, 2); + const char *scancode = luaL_checkstring(L, 3); + int isrepeat = nk_love_checkboolean(L, 4); + int consume = nk_love_keyevent(ctx, key, scancode, isrepeat, 1); lua_pushboolean(L, consume); return 1; } static int nk_love_keyreleased(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - const char *key = luaL_checkstring(L, 1); - const char *scancode = luaL_checkstring(L, 2); - int consume = nk_love_keyevent(key, scancode, 0, 0); + nk_love_assert_argc(lua_gettop(L) == 3); + struct nk_context *ctx = &nk_love_checkcontext(1)->nkctx; + const char *key = luaL_checkstring(L, 2); + const char *scancode = luaL_checkstring(L, 3); + int consume = nk_love_keyevent(ctx, key, scancode, 0, 0); lua_pushboolean(L, consume); return 1; } static int nk_love_mousepressed(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 4); - int x = luaL_checkint(L, 1); - int y = luaL_checkint(L, 2); - int button = luaL_checkint(L, 3); - int istouch = nk_love_checkboolean(L, 4); - int consume = nk_love_clickevent(x, y, button, istouch, 1); + nk_love_assert_argc(lua_gettop(L) == 5); + struct nk_context *ctx = &nk_love_checkcontext(1)->nkctx; + int x = luaL_checkint(L, 2); + int y = luaL_checkint(L, 3); + int button = luaL_checkint(L, 4); + int istouch = nk_love_checkboolean(L, 5); + int consume = nk_love_clickevent(ctx, x, y, button, istouch, 1); lua_pushboolean(L, consume); return 1; } static int nk_love_mousereleased(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 4); - int x = luaL_checkint(L, 1); - int y = luaL_checkint(L, 2); - int button = luaL_checkint(L, 3); - int istouch = nk_love_checkboolean(L, 4); - int consume = nk_love_clickevent(x, y, button, istouch, 0); + nk_love_assert_argc(lua_gettop(L) == 5); + struct nk_context *ctx = &nk_love_checkcontext(1)->nkctx; + int x = luaL_checkint(L, 2); + int y = luaL_checkint(L, 3); + int button = luaL_checkint(L, 4); + int istouch = nk_love_checkboolean(L, 5); + int consume = nk_love_clickevent(ctx, x, y, button, istouch, 0); lua_pushboolean(L, consume); return 1; } static int nk_love_mousemoved(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 5); - int x = luaL_checkint(L, 1); - int y = luaL_checkint(L, 2); - int dx = luaL_checkint(L, 3); - int dy = luaL_checkint(L, 4); - int istouch = nk_love_checkboolean(L, 5); - int consume = nk_love_mousemoved_event(x, y, dx, dy, istouch); + nk_love_assert_argc(lua_gettop(L) == 6); + struct nk_context *ctx = &nk_love_checkcontext(1)->nkctx; + int x = luaL_checkint(L, 2); + int y = luaL_checkint(L, 3); + int dx = luaL_checkint(L, 4); + int dy = luaL_checkint(L, 5); + int istouch = nk_love_checkboolean(L, 6); + int consume = nk_love_mousemoved_event(ctx, x, y, dx, dy, istouch); lua_pushboolean(L, consume); return 1; } static int nk_love_textinput(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - const char *text = luaL_checkstring(L, 1); - int consume = nk_love_textinput_event(text); + nk_love_assert_argc(lua_gettop(L) == 2); + struct nk_context *ctx = &nk_love_checkcontext(1)->nkctx; + const char *text = luaL_checkstring(L, 2); + int consume = nk_love_textinput_event(ctx, text); lua_pushboolean(L, consume); return 1; } static int nk_love_wheelmoved(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - int x = luaL_checkint(L, 1); - int y = luaL_checkint(L, 2); - int consume = nk_love_wheelmoved_event(x, y); + nk_love_assert_argc(lua_gettop(L) == 3); + struct nk_context *ctx = &nk_love_checkcontext(1)->nkctx; + int x = luaL_checkint(L, 2); + int y = luaL_checkint(L, 3); + int consume = nk_love_wheelmoved_event(ctx, x, y); lua_pushboolean(L, consume); return 1; } static int nk_love_draw(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); + nk_love_assert_argc(lua_gettop(L) == 1); + context = nk_love_checkcontext(1); lua_getglobal(L, "love"); lua_getfield(L, -1, "graphics"); @@ -1108,8 +1132,11 @@ static int nk_love_draw(lua_State *L) lua_pushstring(L, "all"); lua_call(L, 1, 0); + lua_getfield(L, -1, "origin"); + lua_call(L, 0, 0); + const struct nk_command *cmd; - nk_foreach(cmd, &context) + nk_foreach(cmd, &context->nkctx) { switch (cmd->type) { case NK_COMMAND_NOP: break; @@ -1195,7 +1222,7 @@ static int nk_love_draw(lua_State *L) lua_getfield(L, -1, "pop"); lua_call(L, 0, 0); lua_pop(L, 2); - nk_clear(&context); + nk_clear(&context->nkctx); return 0; } @@ -1210,135 +1237,138 @@ static void nk_love_preserve(struct nk_style_item *item) static void nk_love_preserve_all(void) { - nk_love_preserve(&context.style.button.normal); - nk_love_preserve(&context.style.button.hover); - nk_love_preserve(&context.style.button.active); + nk_love_preserve(&context->nkctx.style.button.normal); + nk_love_preserve(&context->nkctx.style.button.hover); + nk_love_preserve(&context->nkctx.style.button.active); - nk_love_preserve(&context.style.contextual_button.normal); - nk_love_preserve(&context.style.contextual_button.hover); - nk_love_preserve(&context.style.contextual_button.active); + nk_love_preserve(&context->nkctx.style.contextual_button.normal); + nk_love_preserve(&context->nkctx.style.contextual_button.hover); + nk_love_preserve(&context->nkctx.style.contextual_button.active); - nk_love_preserve(&context.style.menu_button.normal); - nk_love_preserve(&context.style.menu_button.hover); - nk_love_preserve(&context.style.menu_button.active); + nk_love_preserve(&context->nkctx.style.menu_button.normal); + nk_love_preserve(&context->nkctx.style.menu_button.hover); + nk_love_preserve(&context->nkctx.style.menu_button.active); - nk_love_preserve(&context.style.option.normal); - nk_love_preserve(&context.style.option.hover); - nk_love_preserve(&context.style.option.active); - nk_love_preserve(&context.style.option.cursor_normal); - nk_love_preserve(&context.style.option.cursor_hover); + nk_love_preserve(&context->nkctx.style.option.normal); + nk_love_preserve(&context->nkctx.style.option.hover); + nk_love_preserve(&context->nkctx.style.option.active); + nk_love_preserve(&context->nkctx.style.option.cursor_normal); + nk_love_preserve(&context->nkctx.style.option.cursor_hover); - nk_love_preserve(&context.style.checkbox.normal); - nk_love_preserve(&context.style.checkbox.hover); - nk_love_preserve(&context.style.checkbox.active); - nk_love_preserve(&context.style.checkbox.cursor_normal); - nk_love_preserve(&context.style.checkbox.cursor_hover); + nk_love_preserve(&context->nkctx.style.checkbox.normal); + nk_love_preserve(&context->nkctx.style.checkbox.hover); + nk_love_preserve(&context->nkctx.style.checkbox.active); + nk_love_preserve(&context->nkctx.style.checkbox.cursor_normal); + nk_love_preserve(&context->nkctx.style.checkbox.cursor_hover); - nk_love_preserve(&context.style.selectable.normal); - nk_love_preserve(&context.style.selectable.hover); - nk_love_preserve(&context.style.selectable.pressed); - nk_love_preserve(&context.style.selectable.normal_active); - nk_love_preserve(&context.style.selectable.hover_active); - nk_love_preserve(&context.style.selectable.pressed_active); + nk_love_preserve(&context->nkctx.style.selectable.normal); + nk_love_preserve(&context->nkctx.style.selectable.hover); + nk_love_preserve(&context->nkctx.style.selectable.pressed); + nk_love_preserve(&context->nkctx.style.selectable.normal_active); + nk_love_preserve(&context->nkctx.style.selectable.hover_active); + nk_love_preserve(&context->nkctx.style.selectable.pressed_active); - nk_love_preserve(&context.style.slider.normal); - nk_love_preserve(&context.style.slider.hover); - nk_love_preserve(&context.style.slider.active); - nk_love_preserve(&context.style.slider.cursor_normal); - nk_love_preserve(&context.style.slider.cursor_hover); - nk_love_preserve(&context.style.slider.cursor_active); + nk_love_preserve(&context->nkctx.style.slider.normal); + nk_love_preserve(&context->nkctx.style.slider.hover); + nk_love_preserve(&context->nkctx.style.slider.active); + nk_love_preserve(&context->nkctx.style.slider.cursor_normal); + nk_love_preserve(&context->nkctx.style.slider.cursor_hover); + nk_love_preserve(&context->nkctx.style.slider.cursor_active); - nk_love_preserve(&context.style.progress.normal); - nk_love_preserve(&context.style.progress.hover); - nk_love_preserve(&context.style.progress.active); - nk_love_preserve(&context.style.progress.cursor_normal); - nk_love_preserve(&context.style.progress.cursor_hover); - nk_love_preserve(&context.style.progress.cursor_active); + nk_love_preserve(&context->nkctx.style.progress.normal); + nk_love_preserve(&context->nkctx.style.progress.hover); + nk_love_preserve(&context->nkctx.style.progress.active); + nk_love_preserve(&context->nkctx.style.progress.cursor_normal); + nk_love_preserve(&context->nkctx.style.progress.cursor_hover); + nk_love_preserve(&context->nkctx.style.progress.cursor_active); - nk_love_preserve(&context.style.property.normal); - nk_love_preserve(&context.style.property.hover); - nk_love_preserve(&context.style.property.active); - nk_love_preserve(&context.style.property.edit.normal); - nk_love_preserve(&context.style.property.edit.hover); - nk_love_preserve(&context.style.property.edit.active); - nk_love_preserve(&context.style.property.inc_button.normal); - nk_love_preserve(&context.style.property.inc_button.hover); - nk_love_preserve(&context.style.property.inc_button.active); - nk_love_preserve(&context.style.property.dec_button.normal); - nk_love_preserve(&context.style.property.dec_button.hover); - nk_love_preserve(&context.style.property.dec_button.active); + nk_love_preserve(&context->nkctx.style.property.normal); + nk_love_preserve(&context->nkctx.style.property.hover); + nk_love_preserve(&context->nkctx.style.property.active); + nk_love_preserve(&context->nkctx.style.property.edit.normal); + nk_love_preserve(&context->nkctx.style.property.edit.hover); + nk_love_preserve(&context->nkctx.style.property.edit.active); + nk_love_preserve(&context->nkctx.style.property.inc_button.normal); + nk_love_preserve(&context->nkctx.style.property.inc_button.hover); + nk_love_preserve(&context->nkctx.style.property.inc_button.active); + nk_love_preserve(&context->nkctx.style.property.dec_button.normal); + nk_love_preserve(&context->nkctx.style.property.dec_button.hover); + nk_love_preserve(&context->nkctx.style.property.dec_button.active); - nk_love_preserve(&context.style.edit.normal); - nk_love_preserve(&context.style.edit.hover); - nk_love_preserve(&context.style.edit.active); - nk_love_preserve(&context.style.edit.scrollbar.normal); - nk_love_preserve(&context.style.edit.scrollbar.hover); - nk_love_preserve(&context.style.edit.scrollbar.active); - nk_love_preserve(&context.style.edit.scrollbar.cursor_normal); - nk_love_preserve(&context.style.edit.scrollbar.cursor_hover); - nk_love_preserve(&context.style.edit.scrollbar.cursor_active); + nk_love_preserve(&context->nkctx.style.edit.normal); + nk_love_preserve(&context->nkctx.style.edit.hover); + nk_love_preserve(&context->nkctx.style.edit.active); + nk_love_preserve(&context->nkctx.style.edit.scrollbar.normal); + nk_love_preserve(&context->nkctx.style.edit.scrollbar.hover); + nk_love_preserve(&context->nkctx.style.edit.scrollbar.active); + nk_love_preserve(&context->nkctx.style.edit.scrollbar.cursor_normal); + nk_love_preserve(&context->nkctx.style.edit.scrollbar.cursor_hover); + nk_love_preserve(&context->nkctx.style.edit.scrollbar.cursor_active); - nk_love_preserve(&context.style.chart.background); + nk_love_preserve(&context->nkctx.style.chart.background); - nk_love_preserve(&context.style.scrollh.normal); - nk_love_preserve(&context.style.scrollh.hover); - nk_love_preserve(&context.style.scrollh.active); - nk_love_preserve(&context.style.scrollh.cursor_normal); - nk_love_preserve(&context.style.scrollh.cursor_hover); - nk_love_preserve(&context.style.scrollh.cursor_active); + nk_love_preserve(&context->nkctx.style.scrollh.normal); + nk_love_preserve(&context->nkctx.style.scrollh.hover); + nk_love_preserve(&context->nkctx.style.scrollh.active); + nk_love_preserve(&context->nkctx.style.scrollh.cursor_normal); + nk_love_preserve(&context->nkctx.style.scrollh.cursor_hover); + nk_love_preserve(&context->nkctx.style.scrollh.cursor_active); - nk_love_preserve(&context.style.scrollv.normal); - nk_love_preserve(&context.style.scrollv.hover); - nk_love_preserve(&context.style.scrollv.active); - nk_love_preserve(&context.style.scrollv.cursor_normal); - nk_love_preserve(&context.style.scrollv.cursor_hover); - nk_love_preserve(&context.style.scrollv.cursor_active); + nk_love_preserve(&context->nkctx.style.scrollv.normal); + nk_love_preserve(&context->nkctx.style.scrollv.hover); + nk_love_preserve(&context->nkctx.style.scrollv.active); + nk_love_preserve(&context->nkctx.style.scrollv.cursor_normal); + nk_love_preserve(&context->nkctx.style.scrollv.cursor_hover); + nk_love_preserve(&context->nkctx.style.scrollv.cursor_active); - nk_love_preserve(&context.style.tab.background); - nk_love_preserve(&context.style.tab.tab_maximize_button.normal); - nk_love_preserve(&context.style.tab.tab_maximize_button.hover); - nk_love_preserve(&context.style.tab.tab_maximize_button.active); - nk_love_preserve(&context.style.tab.tab_minimize_button.normal); - nk_love_preserve(&context.style.tab.tab_minimize_button.hover); - nk_love_preserve(&context.style.tab.tab_minimize_button.active); - nk_love_preserve(&context.style.tab.node_maximize_button.normal); - nk_love_preserve(&context.style.tab.node_maximize_button.hover); - nk_love_preserve(&context.style.tab.node_maximize_button.active); - nk_love_preserve(&context.style.tab.node_minimize_button.normal); - nk_love_preserve(&context.style.tab.node_minimize_button.hover); - nk_love_preserve(&context.style.tab.node_minimize_button.active); + nk_love_preserve(&context->nkctx.style.tab.background); + nk_love_preserve(&context->nkctx.style.tab.tab_maximize_button.normal); + nk_love_preserve(&context->nkctx.style.tab.tab_maximize_button.hover); + nk_love_preserve(&context->nkctx.style.tab.tab_maximize_button.active); + nk_love_preserve(&context->nkctx.style.tab.tab_minimize_button.normal); + nk_love_preserve(&context->nkctx.style.tab.tab_minimize_button.hover); + nk_love_preserve(&context->nkctx.style.tab.tab_minimize_button.active); + nk_love_preserve(&context->nkctx.style.tab.node_maximize_button.normal); + nk_love_preserve(&context->nkctx.style.tab.node_maximize_button.hover); + nk_love_preserve(&context->nkctx.style.tab.node_maximize_button.active); + nk_love_preserve(&context->nkctx.style.tab.node_minimize_button.normal); + nk_love_preserve(&context->nkctx.style.tab.node_minimize_button.hover); + nk_love_preserve(&context->nkctx.style.tab.node_minimize_button.active); - nk_love_preserve(&context.style.combo.normal); - nk_love_preserve(&context.style.combo.hover); - nk_love_preserve(&context.style.combo.active); - nk_love_preserve(&context.style.combo.button.normal); - nk_love_preserve(&context.style.combo.button.hover); - nk_love_preserve(&context.style.combo.button.active); + nk_love_preserve(&context->nkctx.style.combo.normal); + nk_love_preserve(&context->nkctx.style.combo.hover); + nk_love_preserve(&context->nkctx.style.combo.active); + nk_love_preserve(&context->nkctx.style.combo.button.normal); + nk_love_preserve(&context->nkctx.style.combo.button.hover); + nk_love_preserve(&context->nkctx.style.combo.button.active); - nk_love_preserve(&context.style.window.fixed_background); - nk_love_preserve(&context.style.window.scaler); - nk_love_preserve(&context.style.window.header.normal); - nk_love_preserve(&context.style.window.header.hover); - nk_love_preserve(&context.style.window.header.active); - nk_love_preserve(&context.style.window.header.close_button.normal); - nk_love_preserve(&context.style.window.header.close_button.hover); - nk_love_preserve(&context.style.window.header.close_button.active); - nk_love_preserve(&context.style.window.header.minimize_button.normal); - nk_love_preserve(&context.style.window.header.minimize_button.hover); - nk_love_preserve(&context.style.window.header.minimize_button.active); + nk_love_preserve(&context->nkctx.style.window.fixed_background); + nk_love_preserve(&context->nkctx.style.window.scaler); + nk_love_preserve(&context->nkctx.style.window.header.normal); + nk_love_preserve(&context->nkctx.style.window.header.hover); + nk_love_preserve(&context->nkctx.style.window.header.active); + nk_love_preserve(&context->nkctx.style.window.header.close_button.normal); + nk_love_preserve(&context->nkctx.style.window.header.close_button.hover); + nk_love_preserve(&context->nkctx.style.window.header.close_button.active); + nk_love_preserve(&context->nkctx.style.window.header.minimize_button.normal); + nk_love_preserve(&context->nkctx.style.window.header.minimize_button.hover); + nk_love_preserve(&context->nkctx.style.window.header.minimize_button.active); } static int nk_love_frame_begin(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_input_end(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + context = nk_love_checkcontext(1); + nk_input_end(&context->nkctx); lua_getglobal(L, "love"); lua_getfield(L, -1, "timer"); lua_getfield(L, -1, "getDelta"); lua_call(L, 0, 1); float dt = lua_tonumber(L, -1); - context.delta_time_seconds = dt; + context->nkctx.delta_time_seconds = dt; lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); + lua_pushlightuserdata(L, context); + lua_gettable(L, -2); lua_getfield(L, -1, "image"); lua_newtable(L); lua_setfield(L, -3, "image"); @@ -1347,65 +1377,71 @@ static int nk_love_frame_begin(lua_State *L) lua_getfield(L, -1, "font"); lua_newtable(L); lua_setfield(L, -3, "font"); - font_count = 0; - lua_rawgeti(L, -1, context.style.font->userdata.id); - nk_love_checkFont(-1, &fonts[font_count]); + context->font_count = 0; + lua_rawgeti(L, -1, context->nkctx.style.font->userdata.id); + nk_love_checkFont(-1, &context->fonts[context->font_count]); lua_pop(L, 1); - context.style.font = &fonts[font_count++]; + context->nkctx.style.font = &context->fonts[context->font_count++]; int i; - for (i = 0; i < context.stacks.fonts.head; ++i) { - struct nk_config_stack_user_font_element *element = &context.stacks.fonts.elements[i]; + for (i = 0; i < context->nkctx.stacks.fonts.head; ++i) { + struct nk_config_stack_user_font_element *element = &context->nkctx.stacks.fonts.elements[i]; lua_rawgeti(L, -1, element->old_value->userdata.id); - nk_love_checkFont(-1, &fonts[font_count]); + nk_love_checkFont(-1, &context->fonts[context->font_count]); lua_pop(L, 1); - context.stacks.fonts.elements[i].old_value = &fonts[font_count++]; + context->nkctx.stacks.fonts.elements[i].old_value = &context->fonts[context->font_count++]; } - layout_ratio_count = 0; + context->layout_ratio_count = 0; return 0; } static int nk_love_frame_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_input_begin(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_input_begin(&context->nkctx); + context = NULL; return 0; } static int nk_love_window_begin(lua_State *L) { + nk_love_assert_argc(lua_gettop(L) >= 1); + nk_love_assert_context(1); const char *name, *title; int bounds_begin; - if (lua_isnumber(L, 2)) { - nk_love_assert_argc(lua_gettop(L) >= 5); - name = title = luaL_checkstring(L, 1); - bounds_begin = 2; - } else { + if (lua_isnumber(L, 3)) { nk_love_assert_argc(lua_gettop(L) >= 6); - name = luaL_checkstring(L, 1); - title = luaL_checkstring(L, 2); + name = title = luaL_checkstring(L, 2); bounds_begin = 3; + } else { + nk_love_assert_argc(lua_gettop(L) >= 7); + name = luaL_checkstring(L, 2); + title = luaL_checkstring(L, 3); + bounds_begin = 4; } nk_flags flags = nk_love_parse_window_flags(bounds_begin + 4); float x = luaL_checknumber(L, bounds_begin); float y = luaL_checknumber(L, bounds_begin + 1); float width = luaL_checknumber(L, bounds_begin + 2); float height = luaL_checknumber(L, bounds_begin + 3); - int open = nk_begin_titled(&context, name, title, nk_rect(x, y, width, height), flags); + int open = nk_begin_titled(&context->nkctx, name, title, nk_rect(x, y, width, height), flags); lua_pushboolean(L, open); return 1; } static int nk_love_window_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_end(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_end(&context->nkctx); return 0; } static int nk_love_window_get_bounds(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - struct nk_rect rect = nk_window_get_bounds(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + struct nk_rect rect = nk_window_get_bounds(&context->nkctx); lua_pushnumber(L, rect.x); lua_pushnumber(L, rect.y); lua_pushnumber(L, rect.w); @@ -1415,8 +1451,9 @@ static int nk_love_window_get_bounds(lua_State *L) static int nk_love_window_get_position(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - struct nk_vec2 pos = nk_window_get_position(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + struct nk_vec2 pos = nk_window_get_position(&context->nkctx); lua_pushnumber(L, pos.x); lua_pushnumber(L, pos.y); return 2; @@ -1424,8 +1461,9 @@ static int nk_love_window_get_position(lua_State *L) static int nk_love_window_get_size(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - struct nk_vec2 size = nk_window_get_size(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + struct nk_vec2 size = nk_window_get_size(&context->nkctx); lua_pushnumber(L, size.x); lua_pushnumber(L, size.y); return 2; @@ -1433,8 +1471,9 @@ static int nk_love_window_get_size(lua_State *L) static int nk_love_window_get_content_region(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - struct nk_rect rect = nk_window_get_content_region(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + struct nk_rect rect = nk_window_get_content_region(&context->nkctx); lua_pushnumber(L, rect.x); lua_pushnumber(L, rect.y); lua_pushnumber(L, rect.w); @@ -1444,242 +1483,267 @@ static int nk_love_window_get_content_region(lua_State *L) static int nk_love_window_has_focus(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - int has_focus = nk_window_has_focus(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + int has_focus = nk_window_has_focus(&context->nkctx); lua_pushboolean(L, has_focus); return 1; } static int nk_love_window_is_collapsed(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - const char *name = luaL_checkstring(L, 1); - int is_collapsed = nk_window_is_collapsed(&context, name); + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + const char *name = luaL_checkstring(L, 2); + int is_collapsed = nk_window_is_collapsed(&context->nkctx, name); lua_pushboolean(L, is_collapsed); return 1; } static int nk_love_window_is_hidden(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - const char *name = luaL_checkstring(L, 1); - int is_hidden = nk_window_is_hidden(&context, name); + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + const char *name = luaL_checkstring(L, 2); + int is_hidden = nk_window_is_hidden(&context->nkctx, name); lua_pushboolean(L, is_hidden); return 1; } -static int nk_love_window_is_active(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - const char *name = luaL_checkstring(L, 1); - int is_active = nk_window_is_active(&context, name); +static int nk_love_window_is_active(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + const char *name = luaL_checkstring(L, 2); + int is_active = nk_window_is_active(&context->nkctx, name); lua_pushboolean(L, is_active); return 1; } static int nk_love_window_is_hovered(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - int is_hovered = nk_window_is_hovered(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + int is_hovered = nk_window_is_hovered(&context->nkctx); lua_pushboolean(L, is_hovered); return 1; } static int nk_love_window_is_any_hovered(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - int is_any_hovered = nk_window_is_any_hovered(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + int is_any_hovered = nk_window_is_any_hovered(&context->nkctx); lua_pushboolean(L, is_any_hovered); return 1; } static int nk_love_item_is_any_active(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - lua_pushboolean(L, nk_love_is_active(&context)); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + lua_pushboolean(L, nk_love_is_active(&context->nkctx)); return 1; } static int nk_love_window_set_bounds(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 4); + nk_love_assert_argc(lua_gettop(L) == 5); + nk_love_assert_context(1); struct nk_rect bounds; - bounds.x = luaL_checknumber(L, 1); - bounds.y = luaL_checknumber(L, 2); - bounds.w = luaL_checknumber(L, 3); - bounds.h = luaL_checknumber(L, 4); - nk_window_set_bounds(&context, bounds); + bounds.x = luaL_checknumber(L, 2); + bounds.y = luaL_checknumber(L, 3); + bounds.w = luaL_checknumber(L, 4); + bounds.h = luaL_checknumber(L, 5); + nk_window_set_bounds(&context->nkctx, bounds); return 0; } static int nk_love_window_set_position(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_argc(lua_gettop(L) == 3); + nk_love_assert_context(1); struct nk_vec2 pos; - pos.x = luaL_checknumber(L, 1); - pos.y = luaL_checknumber(L, 2); - nk_window_set_position(&context, pos); + pos.x = luaL_checknumber(L, 2); + pos.y = luaL_checknumber(L, 3); + nk_window_set_position(&context->nkctx, pos); return 0; } static int nk_love_window_set_size(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_argc(lua_gettop(L) == 3); + nk_love_assert_context(1); struct nk_vec2 size; - size.x = luaL_checknumber(L, 1); - size.y = luaL_checknumber(L, 2); - nk_window_set_size(&context, size); + size.x = luaL_checknumber(L, 2); + size.y = luaL_checknumber(L, 3); + nk_window_set_size(&context->nkctx, size); return 0; } static int nk_love_window_set_focus(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - const char *name = luaL_checkstring(L, 1); - nk_window_set_focus(&context, name); + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + const char *name = luaL_checkstring(L, 2); + nk_window_set_focus(&context->nkctx, name); return 0; } static int nk_love_window_close(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - const char *name = luaL_checkstring(L, 1); - nk_window_close(&context, name); + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + const char *name = luaL_checkstring(L, 2); + nk_window_close(&context->nkctx, name); return 0; } static int nk_love_window_collapse(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - const char *name = luaL_checkstring(L, 1); - nk_window_collapse(&context, name, NK_MINIMIZED); + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + const char *name = luaL_checkstring(L, 2); + nk_window_collapse(&context->nkctx, name, NK_MINIMIZED); return 0; } static int nk_love_window_expand(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - const char *name = luaL_checkstring(L, 1); - nk_window_collapse(&context, name, NK_MAXIMIZED); + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + const char *name = luaL_checkstring(L, 2); + nk_window_collapse(&context->nkctx, name, NK_MAXIMIZED); return 0; } static int nk_love_window_show(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - const char *name = luaL_checkstring(L, 1); - nk_window_show(&context, name, NK_SHOWN); + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + const char *name = luaL_checkstring(L, 2); + nk_window_show(&context->nkctx, name, NK_SHOWN); return 0; } static int nk_love_window_hide(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - const char *name = luaL_checkstring(L, 1); - nk_window_show(&context, name, NK_HIDDEN); + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + const char *name = luaL_checkstring(L, 2); + nk_window_show(&context->nkctx, name, NK_HIDDEN); return 0; } static int nk_love_layout_row(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 3 && argc <= 4); - enum nk_layout_format format = nk_love_checkformat(1); - float height = luaL_checknumber(L, 2); + nk_love_assert_argc(argc >= 4 && argc <= 5); + nk_love_assert_context(1); + enum nk_layout_format format = nk_love_checkformat(2); + float height = luaL_checknumber(L, 3); int use_ratios = 0; if (format == NK_DYNAMIC) { - nk_love_assert_argc(argc == 3); - if (lua_isnumber(L, 3)) { - int cols = luaL_checkint(L, 3); - nk_layout_row_dynamic(&context, height, cols); + nk_love_assert_argc(argc == 4); + if (lua_isnumber(L, 4)) { + int cols = luaL_checkint(L, 4); + nk_layout_row_dynamic(&context->nkctx, height, cols); } else { - if (!lua_istable(L, 3)) - luaL_argerror(L, 3, "should be a number or table"); + if (!lua_istable(L, 4)) + luaL_argerror(L, 4, "should be a number or table"); use_ratios = 1; } } else if (format == NK_STATIC) { - if (argc == 4) { - int item_width = luaL_checkint(L, 3); - int cols = luaL_checkint(L, 4); - nk_layout_row_static(&context, height, item_width, cols); + if (argc == 5) { + int item_width = luaL_checkint(L, 4); + int cols = luaL_checkint(L, 5); + nk_layout_row_static(&context->nkctx, height, item_width, cols); } else { - if (!lua_istable(L, 3)) - luaL_argerror(L, 3, "should be a number or table"); + if (!lua_istable(L, 4)) + luaL_argerror(L, 4, "should be a number or table"); use_ratios = 1; } } if (use_ratios) { int cols = lua_objlen(L, -1); int i, j; - for (i = 1, j = layout_ratio_count; i <= cols && j < NK_LOVE_MAX_RATIOS; ++i, ++j) { + for (i = 1, j = context->layout_ratio_count; i <= cols && j < NK_LOVE_MAX_RATIOS; ++i, ++j) { lua_rawgeti(L, -1, i); if (!lua_isnumber(L, -1)) luaL_argerror(L, lua_gettop(L) - 1, "should contain numbers only"); - floats[j] = lua_tonumber(L, -1); + context->layout_ratios[j] = lua_tonumber(L, -1); lua_pop(L, 1); } - nk_layout_row(&context, format, height, cols, floats + layout_ratio_count); - layout_ratio_count += cols; + nk_layout_row(&context->nkctx, format, height, cols, context->layout_ratios + context->layout_ratio_count); + context->layout_ratio_count += cols; } return 0; } static int nk_love_layout_row_begin(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 3); - enum nk_layout_format format = nk_love_checkformat(1); - float height = luaL_checknumber(L, 2); - int cols = luaL_checkint(L, 3); - nk_layout_row_begin(&context, format, height, cols); + nk_love_assert_argc(lua_gettop(L) == 4); + nk_love_assert_context(1); + enum nk_layout_format format = nk_love_checkformat(2); + float height = luaL_checknumber(L, 3); + int cols = luaL_checkint(L, 4); + nk_layout_row_begin(&context->nkctx, format, height, cols); return 0; } static int nk_love_layout_row_push(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - float value = luaL_checknumber(L, 1); - nk_layout_row_push(&context, value); + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + float value = luaL_checknumber(L, 2); + nk_layout_row_push(&context->nkctx, value); return 0; } static int nk_love_layout_row_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_layout_row_end(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_layout_row_end(&context->nkctx); return 0; } static int nk_love_layout_space_begin(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 3); - enum nk_layout_format format = nk_love_checkformat(1); - float height = luaL_checknumber(L, 2); - int widget_count = luaL_checkint(L, 3); - nk_layout_space_begin(&context, format, height, widget_count); + nk_love_assert_argc(lua_gettop(L) == 4); + nk_love_assert_context(1); + enum nk_layout_format format = nk_love_checkformat(2); + float height = luaL_checknumber(L, 3); + int widget_count = luaL_checkint(L, 4); + nk_layout_space_begin(&context->nkctx, format, height, widget_count); return 0; } static int nk_love_layout_space_push(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 4); - float x = luaL_checknumber(L, 1); - float y = luaL_checknumber(L, 2); - float width = luaL_checknumber(L, 3); - float height = luaL_checknumber(L, 4); - nk_layout_space_push(&context, nk_rect(x, y, width, height)); + nk_love_assert_argc(lua_gettop(L) == 5); + nk_love_assert_context(1); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float width = luaL_checknumber(L, 4); + float height = luaL_checknumber(L, 5); + nk_layout_space_push(&context->nkctx, nk_rect(x, y, width, height)); return 0; } static int nk_love_layout_space_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_layout_space_end(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_layout_space_end(&context->nkctx); return 0; } static int nk_love_layout_space_bounds(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - struct nk_rect bounds = nk_layout_space_bounds(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + struct nk_rect bounds = nk_layout_space_bounds(&context->nkctx); lua_pushnumber(L, bounds.x); lua_pushnumber(L, bounds.y); lua_pushnumber(L, bounds.w); @@ -1689,11 +1753,12 @@ static int nk_love_layout_space_bounds(lua_State *L) static int nk_love_layout_space_to_screen(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_argc(lua_gettop(L) == 3); + nk_love_assert_context(1); struct nk_vec2 local; - local.x = luaL_checknumber(L, 1); - local.y = luaL_checknumber(L, 2); - struct nk_vec2 screen = nk_layout_space_to_screen(&context, local); + local.x = luaL_checknumber(L, 2); + local.y = luaL_checknumber(L, 3); + struct nk_vec2 screen = nk_layout_space_to_screen(&context->nkctx, local); lua_pushnumber(L, screen.x); lua_pushnumber(L, screen.y); return 2; @@ -1701,11 +1766,12 @@ static int nk_love_layout_space_to_screen(lua_State *L) static int nk_love_layout_space_to_local(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_argc(lua_gettop(L) == 3); + nk_love_assert_context(1); struct nk_vec2 screen; - screen.x = luaL_checknumber(L, 1); - screen.y = luaL_checknumber(L, 2); - struct nk_vec2 local = nk_layout_space_to_local(&context, screen); + screen.x = luaL_checknumber(L, 2); + screen.y = luaL_checknumber(L, 3); + struct nk_vec2 local = nk_layout_space_to_local(&context->nkctx, screen); lua_pushnumber(L, local.x); lua_pushnumber(L, local.y); return 2; @@ -1713,13 +1779,14 @@ static int nk_love_layout_space_to_local(lua_State *L) static int nk_love_layout_space_rect_to_screen(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 4); + nk_love_assert_argc(lua_gettop(L) == 5); + nk_love_assert_context(1); struct nk_rect local; - local.x = luaL_checknumber(L, 1); - local.y = luaL_checknumber(L, 2); - local.w = luaL_checknumber(L, 3); - local.h = luaL_checknumber(L, 4); - struct nk_rect screen = nk_layout_space_rect_to_screen(&context, local); + local.x = luaL_checknumber(L, 2); + local.y = luaL_checknumber(L, 3); + local.w = luaL_checknumber(L, 4); + local.h = luaL_checknumber(L, 5); + struct nk_rect screen = nk_layout_space_rect_to_screen(&context->nkctx, local); lua_pushnumber(L, screen.x); lua_pushnumber(L, screen.y); lua_pushnumber(L, screen.w); @@ -1729,13 +1796,14 @@ static int nk_love_layout_space_rect_to_screen(lua_State *L) static int nk_love_layout_space_rect_to_local(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 4); + nk_love_assert_argc(lua_gettop(L) == 5); + nk_love_assert_context(1); struct nk_rect screen; - screen.x = luaL_checknumber(L, 1); - screen.y = luaL_checknumber(L, 2); - screen.w = luaL_checknumber(L, 3); - screen.h = luaL_checknumber(L, 4); - struct nk_rect local = nk_layout_space_rect_to_screen(&context, screen); + screen.x = luaL_checknumber(L, 2); + screen.y = luaL_checknumber(L, 3); + screen.w = luaL_checknumber(L, 4); + screen.h = luaL_checknumber(L, 5); + struct nk_rect local = nk_layout_space_rect_to_screen(&context->nkctx, screen); lua_pushnumber(L, local.x); lua_pushnumber(L, local.y); lua_pushnumber(L, local.w); @@ -1745,62 +1813,67 @@ static int nk_love_layout_space_rect_to_local(lua_State *L) static int nk_love_layout_ratio_from_pixel(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - float pixel_width = luaL_checknumber(L, 1); - float ratio = nk_layout_ratio_from_pixel(&context, pixel_width); + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + float pixel_width = luaL_checknumber(L, 2); + float ratio = nk_layout_ratio_from_pixel(&context->nkctx, pixel_width); lua_pushnumber(L, ratio); return 1; } static int nk_love_group_begin(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) >= 1); - const char *title = luaL_checkstring(L, 1); - nk_flags flags = nk_love_parse_window_flags(2); - int open = nk_group_begin(&context, title, flags); + nk_love_assert_argc(lua_gettop(L) >= 2); + nk_love_assert_context(1); + const char *title = luaL_checkstring(L, 2); + nk_flags flags = nk_love_parse_window_flags(3); + int open = nk_group_begin(&context->nkctx, title, flags); lua_pushboolean(L, open); return 1; } static int nk_love_group_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_group_end(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_group_end(&context->nkctx); return 0; } static int nk_love_tree_push(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 2 && argc <= 4); - enum nk_tree_type type = nk_love_checktree(1); - const char *title = luaL_checkstring(L, 2); + nk_love_assert_argc(argc >= 3 && argc <= 5); + nk_love_assert_context(1); + enum nk_tree_type type = nk_love_checktree(2); + const char *title = luaL_checkstring(L, 3); struct nk_image image; int use_image = 0; - if (argc >= 3 && !lua_isnil(L, 3)) { - nk_love_checkImage(3, &image); + if (argc >= 4 && !lua_isnil(L, 4)) { + nk_love_checkImage(4, &image); use_image = 1; } enum nk_collapse_states state = NK_MINIMIZED; - if (argc >= 4) - state = nk_love_checkstate(4); + if (argc >= 5 && !lua_isnil(L, 5)) + state = nk_love_checkstate(5); lua_Debug ar; lua_getstack(L, 1, &ar); lua_getinfo(L, "l", &ar); int id = ar.currentline; int open = 0; if (use_image) - open = nk_tree_image_push_hashed(&context, type, image, title, state, title, strlen(title), id); + open = nk_tree_image_push_hashed(&context->nkctx, type, image, title, state, title, strlen(title), id); else - open = nk_tree_push_hashed(&context, type, title, state, title, strlen(title), id); + open = nk_tree_push_hashed(&context->nkctx, type, title, state, title, strlen(title), id); lua_pushboolean(L, open); return 1; } static int nk_love_tree_pop(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_tree_pop(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_tree_pop(&context->nkctx); return 0; } @@ -1879,33 +1952,34 @@ static int nk_love_color_parse_hsva(lua_State *L) static int nk_love_label(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 1 && argc <= 3); - const char *text = luaL_checkstring(L, 1); + nk_love_assert_argc(argc >= 2 && argc <= 4); + nk_love_assert_context(1); + const char *text = luaL_checkstring(L, 2); nk_flags align = NK_TEXT_LEFT; int wrap = 0; struct nk_color color; int use_color = 0; - if (argc >= 2) { - const char *align_string = luaL_checkstring(L, 2); + if (argc >= 3) { + const char *align_string = luaL_checkstring(L, 3); if (!strcmp(align_string, "wrap")) wrap = 1; else - align = nk_love_checkalign(2); - if (argc >= 3) { - color = nk_love_checkcolor(3); + align = nk_love_checkalign(3); + if (argc >= 4) { + color = nk_love_checkcolor(4); use_color = 1; } } if (use_color) { if (wrap) - nk_label_colored_wrap(&context, text, color); + nk_label_colored_wrap(&context->nkctx, text, color); else - nk_label_colored(&context, text, align, color); + nk_label_colored(&context->nkctx, text, align, color); } else { if (wrap) - nk_label_wrap(&context, text); + nk_label_wrap(&context->nkctx, text); else - nk_label(&context, text, align); + nk_label(&context->nkctx, text, align); } return 0; } @@ -1913,20 +1987,21 @@ static int nk_love_label(lua_State *L) static int nk_love_image(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc == 1 || argc == 5); + nk_love_assert_argc(argc == 2 || argc == 6); + nk_love_assert_context(1); struct nk_image image; - nk_love_checkImage(1, &image); - if (argc == 1) { - nk_image(&context, image); + nk_love_checkImage(2, &image); + if (argc == 2) { + nk_image(&context->nkctx, image); } else { - float x = luaL_checknumber(L, 2); - float y = luaL_checknumber(L, 3); - float w = luaL_checknumber(L, 4); - float h = luaL_checknumber(L, 5); + float x = luaL_checknumber(L, 3); + float y = luaL_checknumber(L, 4); + float w = luaL_checknumber(L, 5); + float h = luaL_checknumber(L, 6); float line_thickness; struct nk_color color; nk_love_getGraphics(&line_thickness, &color); - nk_draw_image(&context.current->buffer, nk_rect(x, y, w, h), &image, color); + nk_draw_image(&context->nkctx.current->buffer, nk_rect(x, y, w, h), &image, color); } return 0; } @@ -1934,45 +2009,46 @@ static int nk_love_image(lua_State *L) static int nk_love_button(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 1 && argc <= 2); + nk_love_assert_argc(argc >= 2 && argc <= 3); + nk_love_assert_context(1); const char *title = NULL; - if (!lua_isnil(L, 1)) - title = luaL_checkstring(L, 1); + if (!lua_isnil(L, 2)) + title = luaL_checkstring(L, 2); int use_color = 0, use_image = 0; struct nk_color color; enum nk_symbol_type symbol = NK_SYMBOL_NONE; struct nk_image image; - if (argc >= 2 && !lua_isnil(L, 2)) { - if (lua_isstring(L, 2)) { - if (nk_love_is_color(2)) { - color = nk_love_checkcolor(2); + if (argc >= 3 && !lua_isnil(L, 3)) { + if (lua_isstring(L, 3)) { + if (nk_love_is_color(3)) { + color = nk_love_checkcolor(3); use_color = 1; } else { - symbol = nk_love_checksymbol(2); + symbol = nk_love_checksymbol(3); } } else { - nk_love_checkImage(2, &image); + nk_love_checkImage(3, &image); use_image = 1; } } - nk_flags align = context.style.button.text_alignment; + nk_flags align = context->nkctx.style.button.text_alignment; int activated = 0; if (title != NULL) { if (use_color) nk_love_assert(0, "%s: color buttons can't have titles"); else if (symbol != NK_SYMBOL_NONE) - activated = nk_button_symbol_label(&context, symbol, title, align); + activated = nk_button_symbol_label(&context->nkctx, symbol, title, align); else if (use_image) - activated = nk_button_image_label(&context, image, title, align); + activated = nk_button_image_label(&context->nkctx, image, title, align); else - activated = nk_button_label(&context, title); + activated = nk_button_label(&context->nkctx, title); } else { if (use_color) - activated = nk_button_color(&context, color); + activated = nk_button_color(&context->nkctx, color); else if (symbol != NK_SYMBOL_NONE) - activated = nk_button_symbol(&context, symbol); + activated = nk_button_symbol(&context->nkctx, symbol); else if (use_image) - activated = nk_button_image(&context, image); + activated = nk_button_image(&context->nkctx, image); else nk_love_assert(0, "%s: must specify a title, color, symbol, and/or image"); } @@ -1982,46 +2058,52 @@ static int nk_love_button(lua_State *L) static int nk_love_button_set_behavior(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - enum nk_button_behavior behavior = nk_love_checkbehavior(1); - nk_button_set_behavior(&context, behavior); + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + enum nk_button_behavior behavior = nk_love_checkbehavior(2); + nk_button_set_behavior(&context->nkctx, behavior); return 0; } static int nk_love_button_push_behavior(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - enum nk_button_behavior behavior = nk_love_checkbehavior(1); - nk_button_push_behavior(&context, behavior); + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + enum nk_button_behavior behavior = nk_love_checkbehavior(2); + nk_button_push_behavior(&context->nkctx, behavior); return 0; } static int nk_love_button_pop_behavior(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_button_pop_behavior(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_button_pop_behavior(&context->nkctx); return 0; } static int nk_love_checkbox(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - const char *text = luaL_checkstring(L, 1); - if (lua_isboolean(L, 2)) { - int value = lua_toboolean(L, 2); - value = nk_check_label(&context, text, value); + nk_love_assert_argc(lua_gettop(L) == 3); + nk_love_assert_context(1); + const char *text = luaL_checkstring(L, 2); + if (lua_isboolean(L, 3)) { + int value = lua_toboolean(L, 3); + value = nk_check_label(&context->nkctx, text, value); lua_pushboolean(L, value); - } else if (lua_istable(L, 2)) { - lua_getfield(L, 2, "value"); + } else if (lua_istable(L, 3)) { + lua_getfield(L, 3, "value"); + if (!lua_isboolean(L, -1)) + luaL_argerror(L, 3, "should have a boolean value"); int value = lua_toboolean(L, -1); - int changed = nk_checkbox_label(&context, text, &value); + int changed = nk_checkbox_label(&context->nkctx, text, &value); if (changed) { lua_pushboolean(L, value); - lua_setfield(L, 2, "value"); + lua_setfield(L, 3, "value"); } lua_pushboolean(L, changed); } else { - luaL_typerror(L, 2, "boolean or table"); + luaL_typerror(L, 3, "boolean or table"); } return 1; } @@ -2029,17 +2111,16 @@ static int nk_love_checkbox(lua_State *L) static int nk_love_radio(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc == 2 || argc == 3); - const char *name = luaL_checkstring(L, 1); - const char *text; - if (argc == 3) - text = luaL_checkstring(L, 2); - else - text = luaL_checkstring(L, 1); + nk_love_assert_argc(argc == 3 || argc == 4); + nk_love_assert_context(1); + const char *name = luaL_checkstring(L, 2); + const char *text = name; + if (argc == 4) + text = luaL_checkstring(L, 3); if (lua_isstring(L, -1)) { const char *value = lua_tostring(L, -1); int active = !strcmp(value, name); - active = nk_option_label(&context, text, active); + active = nk_option_label(&context->nkctx, text, active); if (active) lua_pushstring(L, name); else @@ -2050,7 +2131,7 @@ static int nk_love_radio(lua_State *L) luaL_argerror(L, argc, "should have a string value"); const char *value = lua_tostring(L, -1); int active = !strcmp(value, name); - int changed = nk_radio_label(&context, text, &active); + int changed = nk_radio_label(&context->nkctx, text, &active); if (changed && active) { lua_pushstring(L, name); lua_setfield(L, -3, "value"); @@ -2065,23 +2146,24 @@ static int nk_love_radio(lua_State *L) static int nk_love_selectable(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 2 && argc <= 4); - const char *text = luaL_checkstring(L, 1); + nk_love_assert_argc(argc >= 3 && argc <= 5); + nk_love_assert_context(1); + const char *text = luaL_checkstring(L, 2); struct nk_image image; int use_image = 0; - if (argc >= 3 && !lua_isnil(L, 2)) { - nk_love_checkImage(2, &image); + if (argc >= 4 && !lua_isnil(L, 3)) { + nk_love_checkImage(3, &image); use_image = 1; } nk_flags align = NK_TEXT_LEFT; - if (argc >= 4) - align = nk_love_checkalign(3); + if (argc >= 5) + align = nk_love_checkalign(4); if (lua_isboolean(L, -1)) { int value = lua_toboolean(L, -1); if (use_image) - value = nk_select_image_label(&context, image, text, align, value); + value = nk_select_image_label(&context->nkctx, image, text, align, value); else - value = nk_select_label(&context, text, align, value); + value = nk_select_label(&context->nkctx, text, align, value); lua_pushboolean(L, value); } else if (lua_istable(L, -1)) { lua_getfield(L, -1, "value"); @@ -2090,9 +2172,9 @@ static int nk_love_selectable(lua_State *L) int value = lua_toboolean(L, -1); int changed; if (use_image) - changed = nk_selectable_image_label(&context, image, text, align, &value); + changed = nk_selectable_image_label(&context->nkctx, image, text, align, &value); else - changed = nk_selectable_label(&context, text, align, &value); + changed = nk_selectable_label(&context->nkctx, text, align, &value); if (changed) { lua_pushboolean(L, value); lua_setfield(L, -3, "value"); @@ -2106,112 +2188,21 @@ static int nk_love_selectable(lua_State *L) static int nk_love_slider(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 4); - float min = luaL_checknumber(L, 1); - float max = luaL_checknumber(L, 3); - float step = luaL_checknumber(L, 4); - if (lua_isnumber(L, 2)) { - float value = lua_tonumber(L, 2); - value = nk_slide_float(&context, min, value, max, step); - lua_pushnumber(L, value); - } else if (lua_istable(L, 2)) { - lua_getfield(L, 2, "value"); - if (!lua_isnumber(L, -1)) - luaL_argerror(L, 2, "should have a number value"); - float value = lua_tonumber(L, -1); - int changed = nk_slider_float(&context, min, &value, max, step); - if (changed) { - lua_pushnumber(L, value); - lua_setfield(L, 2, "value"); - } - lua_pushboolean(L, changed); - } else { - luaL_typerror(L, 2, "number or table"); - } - return 1; -} - -static int nk_love_progress(lua_State *L) -{ - int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 2 || argc <= 3); - nk_size max = luaL_checklong(L, 2); - int modifiable = 0; - if (argc >= 3 && !lua_isnil(L, 3)) - modifiable = nk_love_checkboolean(L, 3); - if (lua_isnumber(L, 1)) { - nk_size value = lua_tonumber(L, 1); - value = nk_prog(&context, value, max, modifiable); - lua_pushnumber(L, value); - } else if (lua_istable(L, 1)) { - lua_getfield(L, 1, "value"); - if (!lua_isnumber(L, -1)) - luaL_argerror(L, 1, "should have a number value"); - nk_size value = (nk_size) lua_tonumber(L, -1); - int changed = nk_progress(&context, &value, max, modifiable); - if (changed) { - lua_pushnumber(L, value); - lua_setfield(L, 1, "value"); - } - lua_pushboolean(L, changed); - } else { - luaL_typerror(L, 1, "number or table"); - } - return 1; -} - -static int nk_love_color_picker(lua_State *L) -{ - int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 1 && argc <= 2); - enum nk_color_format format = NK_RGB; - if (argc >= 2) - format = nk_love_checkcolorformat(2); - if (lua_isstring(L, 1)) { - struct nk_color color = nk_love_checkcolor(1); - color = nk_color_picker(&context, color, format); - char new_color_string[10]; - nk_love_color(color.r, color.g, color.b, color.a, new_color_string); - lua_pushstring(L, new_color_string); - } else if (lua_istable(L, 1)) { - lua_getfield(L, 1, "value"); - if (!nk_love_is_color(-1)) - luaL_argerror(L, 1, "should have a color string value"); - struct nk_color color = nk_love_checkcolor(-1); - int changed = nk_color_pick(&context, &color, format); - if (changed) { - char new_color_string[10]; - nk_love_color(color.r, color.g, color.b, color.a, new_color_string); - lua_pushstring(L, new_color_string); - lua_setfield(L, 1, "value"); - } - lua_pushboolean(L, changed); - } else { - luaL_typerror(L, 1, "string or table"); - } - return 1; -} - -static int nk_love_property(lua_State *L) -{ - nk_love_assert_argc(lua_gettop(L) == 6); - const char *name = luaL_checkstring(L, 1); - double min = luaL_checknumber(L, 2); - double max = luaL_checknumber(L, 4); - double step = luaL_checknumber(L, 5); - float inc_per_pixel = luaL_checknumber(L, 6); + nk_love_assert_argc(lua_gettop(L) == 5); + nk_love_assert_context(1); + float min = luaL_checknumber(L, 2); + float max = luaL_checknumber(L, 4); + float step = luaL_checknumber(L, 5); if (lua_isnumber(L, 3)) { - double value = lua_tonumber(L, 3); - value = nk_propertyd(&context, name, min, value, max, step, inc_per_pixel); + float value = lua_tonumber(L, 3); + value = nk_slide_float(&context->nkctx, min, value, max, step); lua_pushnumber(L, value); } else if (lua_istable(L, 3)) { lua_getfield(L, 3, "value"); if (!lua_isnumber(L, -1)) luaL_argerror(L, 3, "should have a number value"); - double value = lua_tonumber(L, -1); - double old = value; - nk_property_double(&context, name, min, &value, max, step, inc_per_pixel); - int changed = value != old; + float value = lua_tonumber(L, -1); + int changed = nk_slider_float(&context->nkctx, min, &value, max, step); if (changed) { lua_pushnumber(L, value); lua_setfield(L, 3, "value"); @@ -2223,23 +2214,119 @@ static int nk_love_property(lua_State *L) return 1; } +static int nk_love_progress(lua_State *L) +{ + int argc = lua_gettop(L); + nk_love_assert_argc(argc >= 3 || argc <= 4); + nk_love_assert_context(1); + nk_size max = luaL_checklong(L, 3); + int modifiable = 0; + if (argc >= 4 && !lua_isnil(L, 4)) + modifiable = nk_love_checkboolean(L, 4); + if (lua_isnumber(L, 2)) { + nk_size value = lua_tonumber(L, 2); + value = nk_prog(&context->nkctx, value, max, modifiable); + lua_pushnumber(L, value); + } else if (lua_istable(L, 2)) { + lua_getfield(L, 2, "value"); + if (!lua_isnumber(L, -1)) + luaL_argerror(L, 2, "should have a number value"); + nk_size value = (nk_size) lua_tonumber(L, -1); + int changed = nk_progress(&context->nkctx, &value, max, modifiable); + if (changed) { + lua_pushnumber(L, value); + lua_setfield(L, 2, "value"); + } + lua_pushboolean(L, changed); + } else { + luaL_typerror(L, 2, "number or table"); + } + return 1; +} + +static int nk_love_color_picker(lua_State *L) +{ + int argc = lua_gettop(L); + nk_love_assert_argc(argc >= 2 && argc <= 3); + nk_love_assert_context(1); + enum nk_color_format format = NK_RGB; + if (argc >= 3) + format = nk_love_checkcolorformat(3); + if (lua_isstring(L, 2)) { + struct nk_color color = nk_love_checkcolor(2); + color = nk_color_picker(&context->nkctx, color, format); + char new_color_string[10]; + nk_love_color(color.r, color.g, color.b, color.a, new_color_string); + lua_pushstring(L, new_color_string); + } else if (lua_istable(L, 2)) { + lua_getfield(L, 2, "value"); + if (!nk_love_is_color(-1)) + luaL_argerror(L, 2, "should have a color string value"); + struct nk_color color = nk_love_checkcolor(-1); + int changed = nk_color_pick(&context->nkctx, &color, format); + if (changed) { + char new_color_string[10]; + nk_love_color(color.r, color.g, color.b, color.a, new_color_string); + lua_pushstring(L, new_color_string); + lua_setfield(L, 2, "value"); + } + lua_pushboolean(L, changed); + } else { + luaL_typerror(L, 2, "string or table"); + } + return 1; +} + +static int nk_love_property(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) == 7); + nk_love_assert_context(1); + const char *name = luaL_checkstring(L, 2); + double min = luaL_checknumber(L, 3); + double max = luaL_checknumber(L, 5); + double step = luaL_checknumber(L, 6); + float inc_per_pixel = luaL_checknumber(L, 7); + if (lua_isnumber(L, 4)) { + double value = lua_tonumber(L, 4); + value = nk_propertyd(&context->nkctx, name, min, value, max, step, inc_per_pixel); + lua_pushnumber(L, value); + } else if (lua_istable(L, 4)) { + lua_getfield(L, 4, "value"); + if (!lua_isnumber(L, -1)) + luaL_argerror(L, 4, "should have a number value"); + double value = lua_tonumber(L, -1); + double old = value; + nk_property_double(&context->nkctx, name, min, &value, max, step, inc_per_pixel); + int changed = value != old; + if (changed) { + lua_pushnumber(L, value); + lua_setfield(L, 4, "value"); + } + lua_pushboolean(L, changed); + } else { + luaL_typerror(L, 4, "number or table"); + } + return 1; +} + static int nk_love_edit(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_flags flags = nk_love_checkedittype(1); - if (!lua_istable(L, 2)) - luaL_typerror(L, 2, "table"); - lua_getfield(L, 2, "value"); + nk_love_assert_argc(lua_gettop(L) == 3); + nk_love_assert_context(1); + nk_flags flags = nk_love_checkedittype(2); + if (!lua_istable(L, 3)) + luaL_typerror(L, 3, "table"); + lua_getfield(L, 3, "value"); if (!lua_isstring(L, -1)) - luaL_argerror(L, 2, "should have a string value"); + luaL_argerror(L, 3, "should have a string value"); const char *value = lua_tostring(L, -1); size_t len = NK_CLAMP(0, strlen(value), NK_LOVE_EDIT_BUFFER_LEN - 1); memcpy(edit_buffer, value, len); edit_buffer[len] = '\0'; - nk_flags event = nk_edit_string_zero_terminated(&context, flags, edit_buffer, NK_LOVE_EDIT_BUFFER_LEN - 1, nk_filter_default); + nk_flags event = nk_edit_string_zero_terminated(&context->nkctx, flags, edit_buffer, NK_LOVE_EDIT_BUFFER_LEN - 1, nk_filter_default); lua_pushstring(L, edit_buffer); lua_pushvalue(L, -1); - lua_setfield(L, 2, "value"); + lua_setfield(L, 3, "value"); int changed = !lua_equal(L, -1, -2); if (event & NK_EDIT_COMMITED) lua_pushstring(L, "commited"); @@ -2259,78 +2346,82 @@ static int nk_love_edit(lua_State *L) static int nk_love_popup_begin(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) >= 6); - enum nk_popup_type type = nk_love_checkpopup(1); - const char *title = luaL_checkstring(L, 2); + nk_love_assert_argc(lua_gettop(L) >= 7); + nk_love_assert_context(1); + enum nk_popup_type type = nk_love_checkpopup(2); + const char *title = luaL_checkstring(L, 3); struct nk_rect bounds; - bounds.x = luaL_checknumber(L, 3); - bounds.y = luaL_checknumber(L, 4); - bounds.w = luaL_checknumber(L, 5); - bounds.h = luaL_checknumber(L, 6); - nk_flags flags = nk_love_parse_window_flags(7); - int open = nk_popup_begin(&context, type, title, flags, bounds); + bounds.x = luaL_checknumber(L, 4); + bounds.y = luaL_checknumber(L, 5); + bounds.w = luaL_checknumber(L, 6); + bounds.h = luaL_checknumber(L, 7); + nk_flags flags = nk_love_parse_window_flags(8); + int open = nk_popup_begin(&context->nkctx, type, title, flags, bounds); lua_pushboolean(L, open); return 1; } static int nk_love_popup_close(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_popup_close(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_popup_close(&context->nkctx); return 0; } static int nk_love_popup_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_popup_end(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_popup_end(&context->nkctx); return 0; } static int nk_love_combobox(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 2 && argc <= 5); - if (!lua_istable(L, 2)) - luaL_typerror(L, 2, "table"); + nk_love_assert_argc(argc >= 3 && argc <= 6); + nk_love_assert_context(1); + if (!lua_istable(L, 3)) + luaL_typerror(L, 3, "table"); int i; for (i = 0; i < NK_LOVE_COMBOBOX_MAX_ITEMS && lua_checkstack(L, 4); ++i) { - lua_rawgeti(L, 2, i + 1); + lua_rawgeti(L, 3, i + 1); if (lua_isstring(L, -1)) combobox_items[i] = lua_tostring(L, -1); else if (lua_isnil(L, -1)) break; else - luaL_argerror(L, 2, "items must be strings"); + luaL_argerror(L, 3, "items must be strings"); } - struct nk_rect bounds = nk_widget_bounds(&context); + struct nk_rect bounds = nk_widget_bounds(&context->nkctx); int item_height = bounds.h; - if (argc >= 3 && !lua_isnil(L, 3)) - item_height = luaL_checkint(L, 3); - struct nk_vec2 size = nk_vec2(bounds.w, item_height * 8); if (argc >= 4 && !lua_isnil(L, 4)) - size.x = luaL_checknumber(L, 4); + item_height = luaL_checkint(L, 4); + struct nk_vec2 size = nk_vec2(bounds.w, item_height * 8); if (argc >= 5 && !lua_isnil(L, 5)) - size.y = luaL_checknumber(L, 5); - if (lua_isnumber(L, 1)) { - int value = lua_tointeger(L, 1) - 1; - value = nk_combo(&context, combobox_items, i, value, item_height, size); + size.x = luaL_checknumber(L, 5); + if (argc >= 6 && !lua_isnil(L, 6)) + size.y = luaL_checknumber(L, 6); + if (lua_isnumber(L, 2)) { + int value = lua_tointeger(L, 2) - 1; + value = nk_combo(&context->nkctx, combobox_items, i, value, item_height, size); lua_pushnumber(L, value + 1); - } else if (lua_istable(L, 1)) { - lua_getfield(L, 1, "value"); + } else if (lua_istable(L, 2)) { + lua_getfield(L, 2, "value"); if (!lua_isnumber(L, -1)) - luaL_argerror(L, 1, "should have a number value"); + luaL_argerror(L, 2, "should have a number value"); int value = lua_tointeger(L, -1) - 1; int old = value; - nk_combobox(&context, combobox_items, i, &value, item_height, size); + nk_combobox(&context->nkctx, combobox_items, i, &value, item_height, size); int changed = value != old; if (changed) { lua_pushnumber(L, value + 1); - lua_setfield(L, 1, "value"); + lua_setfield(L, 2, "value"); } lua_pushboolean(L, changed); } else { - luaL_typerror(L, 1, "number or table"); + luaL_typerror(L, 2, "number or table"); } return 1; } @@ -2338,51 +2429,52 @@ static int nk_love_combobox(lua_State *L) static int nk_love_combobox_begin(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 1 && argc <= 4); + nk_love_assert_argc(argc >= 2 && argc <= 5); + nk_love_assert_context(1); const char *text = NULL; - if (!lua_isnil(L, 1)) - text = luaL_checkstring(L, 1); + if (!lua_isnil(L, 2)) + text = luaL_checkstring(L, 2); struct nk_color color; int use_color = 0; enum nk_symbol_type symbol = NK_SYMBOL_NONE; struct nk_image image; int use_image = 0; - if (argc >= 2 && !lua_isnil(L, 2)) { - if (lua_isstring(L, 2)) { - if (nk_love_is_color(2)) { - color = nk_love_checkcolor(2); + if (argc >= 3 && !lua_isnil(L, 3)) { + if (lua_isstring(L, 3)) { + if (nk_love_is_color(3)) { + color = nk_love_checkcolor(3); use_color = 1; } else { - symbol = nk_love_checksymbol(2); + symbol = nk_love_checksymbol(3); } } else { - nk_love_checkImage(2, &image); + nk_love_checkImage(3, &image); use_image = 1; } } - struct nk_rect bounds = nk_widget_bounds(&context); + struct nk_rect bounds = nk_widget_bounds(&context->nkctx); struct nk_vec2 size = nk_vec2(bounds.w, bounds.h * 8); - if (argc >= 3 && !lua_isnil(L, 3)) - size.x = luaL_checknumber(L, 3); if (argc >= 4 && !lua_isnil(L, 4)) - size.y = luaL_checknumber(L, 4); + size.x = luaL_checknumber(L, 4); + if (argc >= 5 && !lua_isnil(L, 5)) + size.y = luaL_checknumber(L, 5); int open = 0; if (text != NULL) { if (use_color) nk_love_assert(0, "%s: color comboboxes can't have titles"); else if (symbol != NK_SYMBOL_NONE) - open = nk_combo_begin_symbol_label(&context, text, symbol, size); + open = nk_combo_begin_symbol_label(&context->nkctx, text, symbol, size); else if (use_image) - open = nk_combo_begin_image_label(&context, text, image, size); + open = nk_combo_begin_image_label(&context->nkctx, text, image, size); else - open = nk_combo_begin_label(&context, text, size); + open = nk_combo_begin_label(&context->nkctx, text, size); } else { if (use_color) - open = nk_combo_begin_color(&context, color, size); + open = nk_combo_begin_color(&context->nkctx, color, size); else if (symbol != NK_SYMBOL_NONE) - open = nk_combo_begin_symbol(&context, symbol, size); + open = nk_combo_begin_symbol(&context->nkctx, symbol, size); else if (use_image) - open = nk_combo_begin_image(&context, image, size); + open = nk_combo_begin_image(&context->nkctx, image, size); else nk_love_assert(0, "%s: must specify color, symbol, image, and/or title"); } @@ -2393,60 +2485,64 @@ static int nk_love_combobox_begin(lua_State *L) static int nk_love_combobox_item(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 1 && argc <= 3); - const char *text = luaL_checkstring(L, 1); + nk_love_assert_argc(argc >= 2 && argc <= 4); + nk_love_assert_context(1); + const char *text = luaL_checkstring(L, 2); enum nk_symbol_type symbol = NK_SYMBOL_NONE; struct nk_image image; int use_image = 0; - if (argc >= 2 && !lua_isnil(L, 2)) { - if (lua_isstring(L, 2)) { - symbol = nk_love_checksymbol(2); + if (argc >= 3 && !lua_isnil(L, 3)) { + if (lua_isstring(L, 3)) { + symbol = nk_love_checksymbol(3); } else { - nk_love_checkImage(2, &image); + nk_love_checkImage(3, &image); use_image = 1; } } nk_flags align = NK_TEXT_LEFT; - if (argc >= 3 && !lua_isnil(L, 3)) - align = nk_love_checkalign(3); + if (argc >= 4 && !lua_isnil(L, 4)) + align = nk_love_checkalign(4); int activated = 0; if (symbol != NK_SYMBOL_NONE) - activated = nk_combo_item_symbol_label(&context, symbol, text, align); + activated = nk_combo_item_symbol_label(&context->nkctx, symbol, text, align); else if (use_image) - activated = nk_combo_item_image_label(&context, image, text, align); + activated = nk_combo_item_image_label(&context->nkctx, image, text, align); else - activated = nk_combo_item_label(&context, text, align); + activated = nk_combo_item_label(&context->nkctx, text, align); lua_pushboolean(L, activated); return 1; } static int nk_love_combobox_close(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_combo_close(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_combo_close(&context->nkctx); return 0; } static int nk_love_combobox_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_combo_end(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_combo_end(&context->nkctx); return 0; } static int nk_love_contextual_begin(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) >= 6); + nk_love_assert_argc(lua_gettop(L) >= 7); + nk_love_assert_context(1); struct nk_vec2 size; - size.x = luaL_checknumber(L, 1); - size.y = luaL_checknumber(L, 2); + size.x = luaL_checknumber(L, 2); + size.y = luaL_checknumber(L, 3); struct nk_rect trigger; - trigger.x = luaL_checknumber(L, 3); - trigger.y = luaL_checknumber(L, 4); - trigger.w = luaL_checknumber(L, 5); - trigger.h = luaL_checknumber(L, 6); - nk_flags flags = nk_love_parse_window_flags(7); - int open = nk_contextual_begin(&context, flags, size, trigger); + trigger.x = luaL_checknumber(L, 4); + trigger.y = luaL_checknumber(L, 5); + trigger.w = luaL_checknumber(L, 6); + trigger.h = luaL_checknumber(L, 7); + nk_flags flags = nk_love_parse_window_flags(8); + int open = nk_contextual_begin(&context->nkctx, flags, size, trigger); lua_pushboolean(L, open); return 1; } @@ -2454,112 +2550,121 @@ static int nk_love_contextual_begin(lua_State *L) static int nk_love_contextual_item(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 1 && argc <= 3); - const char *text = luaL_checkstring(L, 1); + nk_love_assert_argc(argc >= 2 && argc <= 4); + nk_love_assert_context(1); + const char *text = luaL_checkstring(L, 2); enum nk_symbol_type symbol = NK_SYMBOL_NONE; struct nk_image image; int use_image = 0; - if (argc >= 2 && !lua_isnil(L, 2)) { - if (lua_isstring(L, 2)) { - symbol = nk_love_checksymbol(2); + if (argc >= 3 && !lua_isnil(L, 3)) { + if (lua_isstring(L, 3)) { + symbol = nk_love_checksymbol(3); } else { - nk_love_checkImage(2, &image); + nk_love_checkImage(3, &image); use_image = 1; } } nk_flags align = NK_TEXT_LEFT; - if (argc >= 3 && !lua_isnil(L, 3)) - align = nk_love_checkalign(3); + if (argc >= 4 && !lua_isnil(L, 4)) + align = nk_love_checkalign(4); int activated; if (symbol != NK_SYMBOL_NONE) - activated = nk_contextual_item_symbol_label(&context, symbol, text, align); + activated = nk_contextual_item_symbol_label(&context->nkctx, symbol, text, align); else if (use_image) - activated = nk_contextual_item_image_label(&context, image, text, align); + activated = nk_contextual_item_image_label(&context->nkctx, image, text, align); else - activated = nk_contextual_item_label(&context, text, align); + activated = nk_contextual_item_label(&context->nkctx, text, align); lua_pushboolean(L, activated); return 1; } static int nk_love_contextual_close(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_contextual_close(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_contextual_close(&context->nkctx); return 0; } static int nk_love_contextual_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_contextual_end(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_contextual_end(&context->nkctx); return 0; } static int nk_love_tooltip(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - const char *text = luaL_checkstring(L, 1); - nk_tooltip(&context, text); + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + const char *text = luaL_checkstring(L, 2); + nk_tooltip(&context->nkctx, text); return 0; } static int nk_love_tooltip_begin(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - float width = luaL_checknumber(L, 1); - int open = nk_tooltip_begin(&context, width); + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + float width = luaL_checknumber(L, 2); + int open = nk_tooltip_begin(&context->nkctx, width); lua_pushnumber(L, open); return 1; } static int nk_love_tooltip_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_tooltip_end(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_tooltip_end(&context->nkctx); return 0; } static int nk_love_menubar_begin(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_menubar_begin(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_menubar_begin(&context->nkctx); return 0; } static int nk_love_menubar_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_menubar_end(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_menubar_end(&context->nkctx); return 0; } static int nk_love_menu_begin(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 4 && argc <= 5); - const char *text = luaL_checkstring(L, 1); + nk_love_assert_argc(argc >= 5 && argc <= 6); + nk_love_assert_context(1); + const char *text = luaL_checkstring(L, 2); enum nk_symbol_type symbol = NK_SYMBOL_NONE; struct nk_image image; int use_image = 0; - if (lua_isstring(L, 2)) { - symbol = nk_love_checksymbol(2); - } else if (!lua_isnil(L, 2)) { - nk_love_checkImage(2, &image); + if (lua_isstring(L, 3)) { + symbol = nk_love_checksymbol(3); + } else if (!lua_isnil(L, 3)) { + nk_love_checkImage(3, &image); use_image = 1; } struct nk_vec2 size; - size.x = luaL_checknumber(L, 3); - size.y = luaL_checknumber(L, 4); + size.x = luaL_checknumber(L, 4); + size.y = luaL_checknumber(L, 5); nk_flags align = NK_TEXT_LEFT; - if (argc >= 5 && !lua_isnil(L, 5)) - align = nk_love_checkalign(5); + if (argc >= 6 && !lua_isnil(L, 6)) + align = nk_love_checkalign(6); int open; if (symbol != NK_SYMBOL_NONE) - open = nk_menu_begin_symbol_label(&context, text, align, symbol, size); + open = nk_menu_begin_symbol_label(&context->nkctx, text, align, symbol, size); else if (use_image) - open = nk_menu_begin_image_label(&context, text, align, image, size); + open = nk_menu_begin_image_label(&context->nkctx, text, align, image, size); else - open = nk_menu_begin_label(&context, text, align, size); + open = nk_menu_begin_label(&context->nkctx, text, align, size); lua_pushboolean(L, open); return 1; } @@ -2567,65 +2672,73 @@ static int nk_love_menu_begin(lua_State *L) static int nk_love_menu_item(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 1 && argc <= 3); - const char *text = luaL_checkstring(L, 1); + nk_love_assert_argc(argc >= 2 && argc <= 4); + nk_love_assert_context(1); + const char *text = luaL_checkstring(L, 2); enum nk_symbol_type symbol = NK_SYMBOL_NONE; struct nk_image image; int use_image = 0; - if (argc >= 2 && !lua_isnil(L, 2)) { - if (lua_isstring(L, 2)) { - symbol = nk_love_checksymbol(2); + if (argc >= 3 && !lua_isnil(L, 3)) { + if (lua_isstring(L, 3)) { + symbol = nk_love_checksymbol(3); } else { - nk_love_checkImage(2, &image); + nk_love_checkImage(3, &image); use_image = 1; } } nk_flags align = NK_TEXT_LEFT; - if (argc >= 3 && !lua_isnil(L, 3)) - align = nk_love_checkalign(3); + if (argc >= 4 && !lua_isnil(L, 4)) + align = nk_love_checkalign(4); int activated; if (symbol != NK_SYMBOL_NONE) - activated = nk_menu_item_symbol_label(&context, symbol, text, align); + activated = nk_menu_item_symbol_label(&context->nkctx, symbol, text, align); else if (use_image) - activated = nk_menu_item_image_label(&context, image, text, align); + activated = nk_menu_item_image_label(&context->nkctx, image, text, align); else - activated = nk_menu_item_label(&context, text, align); + activated = nk_menu_item_label(&context->nkctx, text, align); lua_pushboolean(L, activated); return 1; } static int nk_love_menu_close(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_menu_close(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_menu_close(&context->nkctx); return 0; } static int nk_love_menu_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_menu_end(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_menu_end(&context->nkctx); return 0; } static int nk_love_style_default(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - nk_style_default(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + struct nk_love_context *ctx = nk_love_checkcontext(1); + nk_style_default(&ctx->nkctx); return 0; } #define NK_LOVE_LOAD_COLOR(type) \ lua_getfield(L, -1, (type)); \ - nk_love_assert(nk_love_is_color(-1), "%s: table missing color value for '" type "'"); \ + if (!nk_love_is_color(-1)) { \ + const char *msg = lua_pushfstring(L, "%%s: table missing color value for '%s'", type); \ + nk_love_assert(0, msg); \ + } \ colors[index++] = nk_love_checkcolor(-1); \ - lua_pop(L, 1); + lua_pop(L, 1) static int nk_love_style_load_colors(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - if (!lua_istable(L, 1)) - luaL_typerror(L, 1, "table"); + nk_love_assert_argc(lua_gettop(L) == 2); + struct nk_love_context *ctx = nk_love_checkcontext(1); + if (!lua_istable(L, 2)) + luaL_typerror(L, 2, "table"); struct nk_color colors[NK_COLOR_COUNT]; int index = 0; NK_LOVE_LOAD_COLOR("text"); @@ -2656,36 +2769,39 @@ static int nk_love_style_load_colors(lua_State *L) NK_LOVE_LOAD_COLOR("scrollbar cursor hover"); NK_LOVE_LOAD_COLOR("scrollbar cursor active"); NK_LOVE_LOAD_COLOR("tab header"); - nk_style_from_table(&context, colors); + nk_style_from_table(&ctx->nkctx, colors); return 0; } static int nk_love_style_set_font(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_checkFont(1, &fonts[font_count]); - nk_style_set_font(&context, &fonts[font_count++]); + nk_love_assert_argc(lua_gettop(L) == 2); + struct nk_love_context *ctx = nk_love_checkcontext(1); + nk_love_checkFont(2, &ctx->fonts[ctx->font_count]); + nk_style_set_font(&ctx->nkctx, &ctx->fonts[ctx->font_count++]); return 0; } static int nk_love_style_push_color(struct nk_color *field) { + struct nk_love_context *ctx = nk_love_checkcontext(1); if (!nk_love_is_color(-1)) { const char *msg = lua_pushfstring(L, "%%s: bad color string '%s'", lua_tostring(L, -1)); nk_love_assert(0, msg); } struct nk_color color = nk_love_checkcolor(-1); - int success = nk_style_push_color(&context, field, color); + int success = nk_style_push_color(&ctx->nkctx, field, color); if (success) { lua_pushstring(L, "color"); - size_t stack_size = lua_objlen(L, 1); - lua_rawseti(L, 1, stack_size + 1); + size_t stack_size = lua_objlen(L, 2); + lua_rawseti(L, 2, stack_size + 1); } return success; } static int nk_love_style_push_vec2(struct nk_vec2 *field) { + struct nk_love_context *ctx = nk_love_checkcontext(1); static const char *msg = "%s: vec2 fields must have x and y components"; nk_love_assert(lua_istable(L, -1), msg); lua_getfield(L, -1, "x"); @@ -2696,17 +2812,18 @@ static int nk_love_style_push_vec2(struct nk_vec2 *field) vec2.x = lua_tonumber(L, -2); vec2.y = lua_tonumber(L, -1); lua_pop(L, 2); - int success = nk_style_push_vec2(&context, field, vec2); + int success = nk_style_push_vec2(&ctx->nkctx, field, vec2); if (success) { lua_pushstring(L, "vec2"); - size_t stack_size = lua_objlen(L, 1); - lua_rawseti(L, 1, stack_size + 1); + size_t stack_size = lua_objlen(L, 2); + lua_rawseti(L, 2, stack_size + 1); } return success; } static int nk_love_style_push_item(struct nk_style_item *field) { + struct nk_love_context *ctx = nk_love_checkcontext(1); struct nk_style_item item; if (lua_isstring(L, -1)) { if (!nk_love_is_color(-1)) { @@ -2719,46 +2836,50 @@ static int nk_love_style_push_item(struct nk_style_item *field) item.type = NK_STYLE_ITEM_IMAGE; nk_love_checkImage(-1, &item.data.image); } - int success = nk_style_push_style_item(&context, field, item); + int success = nk_style_push_style_item(&ctx->nkctx, field, item); if (success) { lua_pushstring(L, "item"); - size_t stack_size = lua_objlen(L, 1); - lua_rawseti(L, 1, stack_size + 1); + size_t stack_size = lua_objlen(L, 2); + lua_rawseti(L, 2, stack_size + 1); } return success; } static int nk_love_style_push_align(nk_flags *field) { + struct nk_love_context *ctx = nk_love_checkcontext(1); nk_flags align = nk_love_checkalign(-1); - int success = nk_style_push_flags(&context, field, align); + int success = nk_style_push_flags(&ctx->nkctx, field, align); if (success) { lua_pushstring(L, "flags"); - size_t stack_size = lua_objlen(L, 1); - lua_rawseti(L, 1, stack_size + 1); + size_t stack_size = lua_objlen(L, 2); + lua_rawseti(L, 2, stack_size + 1); } return success; } -static int nk_love_style_push_float(float *field) { +static int nk_love_style_push_float(float *field) +{ + struct nk_love_context *ctx = nk_love_checkcontext(1); float f = luaL_checknumber(L, -1); - int success = nk_style_push_float(&context, field, f); + int success = nk_style_push_float(&ctx->nkctx, field, f); if (success) { lua_pushstring(L, "float"); - size_t stack_size = lua_objlen(L, 1); - lua_rawseti(L, 1, stack_size + 1); + size_t stack_size = lua_objlen(L, 2); + lua_rawseti(L, 2, stack_size + 1); } return success; } static int nk_love_style_push_font(const struct nk_user_font **field) { - nk_love_checkFont(-1, &fonts[font_count]); - int success = nk_style_push_font(&context, &fonts[font_count++]); + struct nk_love_context *ctx = nk_love_checkcontext(1); + nk_love_checkFont(-1, &context->fonts[context->font_count]); + int success = nk_style_push_font(&ctx->nkctx, &context->fonts[context->font_count++]); if (success) { lua_pushstring(L, "font"); - size_t stack_size = lua_objlen(L, 1); - lua_rawseti(L, 1, stack_size + 1); + size_t stack_size = lua_objlen(L, 2); + lua_rawseti(L, 2, stack_size + 1); } return success; } @@ -3043,42 +3164,48 @@ static void nk_love_style_push_window(struct nk_style_window *style) static int nk_love_style_push(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - if (!lua_istable(L, 1)) - luaL_typerror(L, 1, "table"); + nk_love_assert_argc(lua_gettop(L) == 2); + struct nk_love_context *ctx = nk_love_checkcontext(1); + if (!lua_istable(L, 2)) + luaL_typerror(L, 2, "table"); lua_newtable(L); - lua_insert(L, 1); - NK_LOVE_STYLE_PUSH("font", font, &context.style.font); - NK_LOVE_STYLE_PUSH("text", text, &context.style.text); - NK_LOVE_STYLE_PUSH("button", button, &context.style.button); - NK_LOVE_STYLE_PUSH("contextual button", button, &context.style.contextual_button); - NK_LOVE_STYLE_PUSH("menu button", button, &context.style.menu_button); - NK_LOVE_STYLE_PUSH("option", toggle, &context.style.option); - NK_LOVE_STYLE_PUSH("checkbox", toggle, &context.style.checkbox); - NK_LOVE_STYLE_PUSH("selectable", selectable, &context.style.selectable); - NK_LOVE_STYLE_PUSH("slider", slider, &context.style.slider); - NK_LOVE_STYLE_PUSH("progress", progress, &context.style.progress); - NK_LOVE_STYLE_PUSH("property", property, &context.style.property); - NK_LOVE_STYLE_PUSH("edit", edit, &context.style.edit); - NK_LOVE_STYLE_PUSH("chart", chart, &context.style.chart); - NK_LOVE_STYLE_PUSH("scrollh", scrollbar, &context.style.scrollh); - NK_LOVE_STYLE_PUSH("scrollv", scrollbar, &context.style.scrollv); - NK_LOVE_STYLE_PUSH("tab", tab, &context.style.tab); - NK_LOVE_STYLE_PUSH("combo", combo, &context.style.combo); - NK_LOVE_STYLE_PUSH("window", window, &context.style.window); + lua_insert(L, 2); + NK_LOVE_STYLE_PUSH("font", font, &ctx->nkctx.style.font); + NK_LOVE_STYLE_PUSH("text", text, &ctx->nkctx.style.text); + NK_LOVE_STYLE_PUSH("button", button, &ctx->nkctx.style.button); + NK_LOVE_STYLE_PUSH("contextual button", button, &ctx->nkctx.style.contextual_button); + NK_LOVE_STYLE_PUSH("menu button", button, &ctx->nkctx.style.menu_button); + NK_LOVE_STYLE_PUSH("option", toggle, &ctx->nkctx.style.option); + NK_LOVE_STYLE_PUSH("checkbox", toggle, &ctx->nkctx.style.checkbox); + NK_LOVE_STYLE_PUSH("selectable", selectable, &ctx->nkctx.style.selectable); + NK_LOVE_STYLE_PUSH("slider", slider, &ctx->nkctx.style.slider); + NK_LOVE_STYLE_PUSH("progress", progress, &ctx->nkctx.style.progress); + NK_LOVE_STYLE_PUSH("property", property, &ctx->nkctx.style.property); + NK_LOVE_STYLE_PUSH("edit", edit, &ctx->nkctx.style.edit); + NK_LOVE_STYLE_PUSH("chart", chart, &ctx->nkctx.style.chart); + NK_LOVE_STYLE_PUSH("scrollh", scrollbar, &ctx->nkctx.style.scrollh); + NK_LOVE_STYLE_PUSH("scrollv", scrollbar, &ctx->nkctx.style.scrollv); + NK_LOVE_STYLE_PUSH("tab", tab, &ctx->nkctx.style.tab); + NK_LOVE_STYLE_PUSH("combo", combo, &ctx->nkctx.style.combo); + NK_LOVE_STYLE_PUSH("window", window, &ctx->nkctx.style.window); lua_pop(L, 1); lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); + lua_pushlightuserdata(L, ctx); + lua_gettable(L, -2); lua_getfield(L, -1, "stack"); size_t stack_size = lua_objlen(L, -1); - lua_pushvalue(L, 1); + lua_pushvalue(L, 2); lua_rawseti(L, -2, stack_size + 1); return 0; } static int nk_love_style_pop(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); + nk_love_assert_argc(lua_gettop(L) == 1); + struct nk_love_context *ctx = nk_love_checkcontext(1); lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); + lua_pushlightuserdata(L, ctx); + lua_gettable(L, -2); lua_getfield(L, -1, "stack"); size_t stack_size = lua_objlen(L, -1); lua_rawgeti(L, -1, stack_size); @@ -3090,17 +3217,17 @@ static int nk_love_style_pop(lua_State *L) lua_rawgeti(L, -1, i); const char *type = lua_tostring(L, -1); if (!strcmp(type, "color")) { - nk_style_pop_color(&context); + nk_style_pop_color(&ctx->nkctx); } else if (!strcmp(type, "vec2")) { - nk_style_pop_vec2(&context); + nk_style_pop_vec2(&ctx->nkctx); } else if (!strcmp(type, "item")) { - nk_style_pop_style_item(&context); + nk_style_pop_style_item(&ctx->nkctx); } else if (!strcmp(type, "flags")) { - nk_style_pop_flags(&context); + nk_style_pop_flags(&ctx->nkctx); } else if (!strcmp(type, "float")) { - nk_style_pop_float(&context); + nk_style_pop_float(&ctx->nkctx); } else if (!strcmp(type, "font")) { - nk_style_pop_font(&context); + nk_style_pop_font(&ctx->nkctx); } else { const char *msg = lua_pushfstring(L, "%%s: bad style item type '%s'", lua_tostring(L, -1)); nk_love_assert(0, msg); @@ -3112,8 +3239,9 @@ static int nk_love_style_pop(lua_State *L) static int nk_love_widget_bounds(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - struct nk_rect bounds = nk_widget_bounds(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + struct nk_rect bounds = nk_widget_bounds(&context->nkctx); lua_pushnumber(L, bounds.x); lua_pushnumber(L, bounds.y); lua_pushnumber(L, bounds.w); @@ -3123,8 +3251,9 @@ static int nk_love_widget_bounds(lua_State *L) static int nk_love_widget_position(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - struct nk_vec2 pos = nk_widget_position(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + struct nk_vec2 pos = nk_widget_position(&context->nkctx); lua_pushnumber(L, pos.x); lua_pushnumber(L, pos.y); return 2; @@ -3132,8 +3261,9 @@ static int nk_love_widget_position(lua_State *L) static int nk_love_widget_size(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - struct nk_vec2 pos = nk_widget_size(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + struct nk_vec2 pos = nk_widget_size(&context->nkctx); lua_pushnumber(L, pos.x); lua_pushnumber(L, pos.y); return 2; @@ -3141,63 +3271,40 @@ static int nk_love_widget_size(lua_State *L) static int nk_love_widget_width(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - float width = nk_widget_width(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + float width = nk_widget_width(&context->nkctx); lua_pushnumber(L, width); return 1; } static int nk_love_widget_height(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - float height = nk_widget_height(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + float height = nk_widget_height(&context->nkctx); lua_pushnumber(L, height); return 1; } static int nk_love_widget_is_hovered(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); - int hovered = nk_widget_is_hovered(&context); + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + int hovered = nk_widget_is_hovered(&context->nkctx); lua_pushboolean(L, hovered); return 1; } -static int nk_love_widget_is_mouse_clicked(lua_State *L) +static int nk_love_widget_has_mouse(lua_State *L, int down) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 0 && argc <= 1); + nk_love_assert_argc(argc >= 1 && argc <= 2); + nk_love_assert_context(1); enum nk_buttons button = NK_BUTTON_LEFT; - if (argc >= 1 && !lua_isnil(L, 1)) - button = nk_love_checkbutton(1); - int clicked = (context.active == context.current) && - nk_input_is_mouse_pressed(&context.input, button); - lua_pushboolean(L, clicked); - return 1; -} - -static int nk_love_widget_has_mouse_click(lua_State *L) -{ - int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 0 && argc <= 2); - enum nk_buttons button = NK_BUTTON_LEFT; - if (argc >= 1 && !lua_isnil(L, 1)) - button = nk_love_checkbutton(1); - int down = 1; if (argc >= 2 && !lua_isnil(L, 2)) - down = nk_love_checkboolean(L, 2); - int has_click = nk_widget_has_mouse_click_down(&context, button, down); - lua_pushboolean(L, has_click); - return 1; -} - -static int nk_love_widget_has_mouse(lua_State *L, int down) { - int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 0 && argc <= 1); - enum nk_buttons button = NK_BUTTON_LEFT; - if (argc >= 1 && !lua_isnil(L, 1)) - button = nk_love_checkbutton(1); - int ret = nk_widget_has_mouse_click_down(&context, button, down); + button = nk_love_checkbutton(2); + int ret = nk_widget_has_mouse_click_down(&context->nkctx, button, down); lua_pushboolean(L, ret); return 1; } @@ -3212,14 +3319,16 @@ static int nk_love_widget_has_mouse_released(lua_State *L) return nk_love_widget_has_mouse(L, nk_false); } -static int nk_love_widget_is_mouse(lua_State *L, int down) { +static int nk_love_widget_is_mouse(lua_State *L, int down) +{ int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 0 && argc <= 1); + nk_love_assert_argc(argc >= 1 && argc <= 2); + nk_love_assert_context(1); enum nk_buttons button = NK_BUTTON_LEFT; - if (argc >= 1 && !lua_isnil(L, 1)) - button = nk_love_checkbutton(1); - struct nk_rect bounds = nk_widget_bounds(&context); - int ret = nk_input_is_mouse_click_down_in_rect(&context.input, button, bounds, down); + if (argc >= 2 && !lua_isnil(L, 2)) + button = nk_love_checkbutton(2); + struct nk_rect bounds = nk_widget_bounds(&context->nkctx); + int ret = nk_input_is_mouse_click_down_in_rect(&context->nkctx.input, button, bounds, down); lua_pushboolean(L, ret); return 1; } @@ -3236,177 +3345,188 @@ static int nk_love_widget_is_mouse_released(lua_State *L) static int nk_love_spacing(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - int cols = luaL_checkint(L, 1); - nk_spacing(&context, cols); + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + int cols = luaL_checkint(L, 2); + nk_spacing(&context->nkctx, cols); return 0; } static int nk_love_line(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 4 && argc % 2 == 0); + nk_love_assert_argc(argc >= 5 && argc % 2 == 1); + nk_love_assert_context(1); int i; - for (i = 0; i < argc; ++i) { - nk_love_assert(lua_isnumber(L, i + 1), "%s: point coordinates should be numbers"); - floats[i] = lua_tonumber(L, i + 1); + for (i = 0; i < argc - 1; ++i) { + nk_love_assert(lua_isnumber(L, i + 2), "%s: point coordinates should be numbers"); + points[i] = lua_tonumber(L, i + 2); } float line_thickness; struct nk_color color; nk_love_getGraphics(&line_thickness, &color); - nk_stroke_polyline(&context.current->buffer, floats, argc / 2, line_thickness, color); + nk_stroke_polyline(&context->nkctx.current->buffer, points, (argc - 1) / 2, line_thickness, color); return 0; } static int nk_love_curve(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 8); + nk_love_assert_argc(lua_gettop(L) == 9); + nk_love_assert_context(1); int i; - float ax = luaL_checknumber(L, 1); - float ay = luaL_checknumber(L, 2); - float ctrl0x = luaL_checknumber(L, 3); - float ctrl0y = luaL_checknumber(L, 4); - float ctrl1x = luaL_checknumber(L, 5); - float ctrl1y = luaL_checknumber(L, 6); - float bx = luaL_checknumber(L, 7); - float by = luaL_checknumber(L, 8); + float ax = luaL_checknumber(L, 2); + float ay = luaL_checknumber(L, 3); + float ctrl0x = luaL_checknumber(L, 4); + float ctrl0y = luaL_checknumber(L, 5); + float ctrl1x = luaL_checknumber(L, 6); + float ctrl1y = luaL_checknumber(L, 7); + float bx = luaL_checknumber(L, 8); + float by = luaL_checknumber(L, 9); float line_thickness; struct nk_color color; nk_love_getGraphics(&line_thickness, &color); - nk_stroke_curve(&context.current->buffer, ax, ay, ctrl0x, ctrl0y, ctrl1x, ctrl1y, bx, by, line_thickness, color); + nk_stroke_curve(&context->nkctx.current->buffer, ax, ay, ctrl0x, ctrl0y, ctrl1x, ctrl1y, bx, by, line_thickness, color); return 0; } static int nk_love_polygon(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 7 && argc % 2 == 1); - enum nk_love_draw_mode mode = nk_love_checkdraw(1); + nk_love_assert_argc(argc >= 8 && argc % 2 == 0); + nk_love_assert_context(1); + enum nk_love_draw_mode mode = nk_love_checkdraw(2); int i; - for (i = 0; i < argc - 1; ++i) { - nk_love_assert(lua_isnumber(L, i + 2), "%s: point coordinates should be numbers"); - floats[i] = lua_tonumber(L, i + 2); + for (i = 0; i < argc - 2; ++i) { + nk_love_assert(lua_isnumber(L, i + 3), "%s: point coordinates should be numbers"); + points[i] = lua_tonumber(L, i + 3); } float line_thickness; struct nk_color color; nk_love_getGraphics(&line_thickness, &color); if (mode == NK_LOVE_FILL) - nk_fill_polygon(&context.current->buffer, floats, (argc - 1) / 2, color); + nk_fill_polygon(&context->nkctx.current->buffer, points, (argc - 2) / 2, color); else if (mode == NK_LOVE_LINE) - nk_stroke_polygon(&context.current->buffer, floats, (argc - 1) / 2, line_thickness, color); + nk_stroke_polygon(&context->nkctx.current->buffer, points, (argc - 2) / 2, line_thickness, color); return 0; } static int nk_love_circle(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 4); - enum nk_love_draw_mode mode = nk_love_checkdraw(1); - float x = luaL_checknumber(L, 2); - float y = luaL_checknumber(L, 3); - float r = luaL_checknumber(L, 4); + nk_love_assert_argc(lua_gettop(L) == 5); + nk_love_assert_context(1); + enum nk_love_draw_mode mode = nk_love_checkdraw(2); + float x = luaL_checknumber(L, 3); + float y = luaL_checknumber(L, 4); + float r = luaL_checknumber(L, 5); float line_thickness; struct nk_color color; nk_love_getGraphics(&line_thickness, &color); if (mode == NK_LOVE_FILL) - nk_fill_circle(&context.current->buffer, nk_rect(x - r, y - r, r * 2, r * 2), color); + nk_fill_circle(&context->nkctx.current->buffer, nk_rect(x - r, y - r, r * 2, r * 2), color); else if (mode == NK_LOVE_LINE) - nk_stroke_circle(&context.current->buffer, nk_rect(x - r, y - r, r * 2, r * 2), line_thickness, color); + nk_stroke_circle(&context->nkctx.current->buffer, nk_rect(x - r, y - r, r * 2, r * 2), line_thickness, color); return 0; } static int nk_love_ellipse(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 5); - enum nk_love_draw_mode mode = nk_love_checkdraw(1); - float x = luaL_checknumber(L, 2); - float y = luaL_checknumber(L, 3); - float rx = luaL_checknumber(L, 4); - float ry = luaL_checknumber(L, 5); + nk_love_assert_argc(lua_gettop(L) == 6); + nk_love_assert_context(1); + enum nk_love_draw_mode mode = nk_love_checkdraw(2); + float x = luaL_checknumber(L, 3); + float y = luaL_checknumber(L, 4); + float rx = luaL_checknumber(L, 5); + float ry = luaL_checknumber(L, 6); float line_thickness; struct nk_color color; nk_love_getGraphics(&line_thickness, &color); if (mode == NK_LOVE_FILL) - nk_fill_circle(&context.current->buffer, nk_rect(x - rx, y - ry, rx * 2, ry * 2), color); + nk_fill_circle(&context->nkctx.current->buffer, nk_rect(x - rx, y - ry, rx * 2, ry * 2), color); else if (mode == NK_LOVE_LINE) - nk_stroke_circle(&context.current->buffer, nk_rect(x - rx, y - ry, rx * 2, ry * 2), line_thickness, color); + nk_stroke_circle(&context->nkctx.current->buffer, nk_rect(x - rx, y - ry, rx * 2, ry * 2), line_thickness, color); return 0; } static int nk_love_arc(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 6); - enum nk_love_draw_mode mode = nk_love_checkdraw(1); - float cx = luaL_checknumber(L, 2); - float cy = luaL_checknumber(L, 3); - float r = luaL_checknumber(L, 4); - float a0 = luaL_checknumber(L, 5); - float a1 = luaL_checknumber(L, 6); + nk_love_assert_argc(lua_gettop(L) == 7); + nk_love_assert_context(1); + enum nk_love_draw_mode mode = nk_love_checkdraw(2); + float cx = luaL_checknumber(L, 3); + float cy = luaL_checknumber(L, 4); + float r = luaL_checknumber(L, 5); + float a0 = luaL_checknumber(L, 6); + float a1 = luaL_checknumber(L, 7); float line_thickness; struct nk_color color; nk_love_getGraphics(&line_thickness, &color); if (mode == NK_LOVE_FILL) - nk_fill_arc(&context.current->buffer, cx, cy, r, a0, a1, color); + nk_fill_arc(&context->nkctx.current->buffer, cx, cy, r, a0, a1, color); else if (mode == NK_LOVE_LINE) - nk_stroke_arc(&context.current->buffer, cx, cy, r, a0, a1, line_thickness, color); + nk_stroke_arc(&context->nkctx.current->buffer, cx, cy, r, a0, a1, line_thickness, color); return 0; } static int nk_love_rect_multi_color(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 8); - float x = luaL_checknumber(L, 1); - float y = luaL_checknumber(L, 2); - float w = luaL_checknumber(L, 3); - float h = luaL_checknumber(L, 4); - struct nk_color topLeft = nk_love_checkcolor(5); - struct nk_color topRight = nk_love_checkcolor(6); - struct nk_color bottomLeft = nk_love_checkcolor(7); - struct nk_color bottomRight = nk_love_checkcolor(8); - nk_fill_rect_multi_color(&context.current->buffer, nk_rect(x, y, w, h), topLeft, topRight, bottomLeft, bottomRight); + nk_love_assert_argc(lua_gettop(L) == 9); + nk_love_assert_context(1); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float w = luaL_checknumber(L, 4); + float h = luaL_checknumber(L, 5); + struct nk_color topLeft = nk_love_checkcolor(6); + struct nk_color topRight = nk_love_checkcolor(7); + struct nk_color bottomLeft = nk_love_checkcolor(8); + struct nk_color bottomRight = nk_love_checkcolor(9); + nk_fill_rect_multi_color(&context->nkctx.current->buffer, nk_rect(x, y, w, h), topLeft, topRight, bottomLeft, bottomRight); return 0; } static int nk_love_push_scissor(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 4); - float x = luaL_checknumber(L, 1); - float y = luaL_checknumber(L, 2); - float w = luaL_checknumber(L, 3); - float h = luaL_checknumber(L, 4); - nk_push_scissor(&context.current->buffer, nk_rect(x, y, w, h)); + nk_love_assert_argc(lua_gettop(L) == 5); + nk_love_assert_context(1); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float w = luaL_checknumber(L, 4); + float h = luaL_checknumber(L, 5); + nk_push_scissor(&context->nkctx.current->buffer, nk_rect(x, y, w, h)); return 0; } static int nk_love_text(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 5); - const char *text = luaL_checkstring(L, 1); - float x = luaL_checknumber(L, 2); - float y = luaL_checknumber(L, 3); - float w = luaL_checknumber(L, 4); - float h = luaL_checknumber(L, 5); + nk_love_assert_argc(lua_gettop(L) == 6); + nk_love_assert_context(1); + const char *text = luaL_checkstring(L, 2); + float x = luaL_checknumber(L, 3); + float y = luaL_checknumber(L, 4); + float w = luaL_checknumber(L, 5); + float h = luaL_checknumber(L, 6); lua_getglobal(L, "love"); lua_getfield(L, -1, "graphics"); lua_getfield(L, -1, "getFont"); lua_call(L, 0, 1); - nk_love_checkFont(-1, &fonts[font_count]); + nk_love_checkFont(-1, &context->fonts[context->font_count]); float line_thickness; struct nk_color color; nk_love_getGraphics(&line_thickness, &color); - nk_draw_text(&context.current->buffer, nk_rect(x, y, w, h), text, strlen(text), &fonts[font_count++], nk_rgba(0, 0, 0, 0), color); + nk_draw_text(&context->nkctx.current->buffer, nk_rect(x, y, w, h), text, strlen(text), &context->fonts[context->font_count++], nk_rgba(0, 0, 0, 0), color); return 0; } static int nk_love_input_has_mouse(int down) { int argc = lua_gettop(L); - nk_love_assert_argc(argc == 5); - enum nk_buttons button = nk_love_checkbutton(1); - float x = luaL_checknumber(L, 2); - float y = luaL_checknumber(L, 3); - float w = luaL_checknumber(L, 4); - float h = luaL_checknumber(L, 5); - int ret = nk_input_has_mouse_click_down_in_rect(&context.input, button, nk_rect(x, y, w, h), down); + nk_love_assert_argc(argc == 6); + nk_love_assert_context(1); + enum nk_buttons button = nk_love_checkbutton(2); + float x = luaL_checknumber(L, 3); + float y = luaL_checknumber(L, 4); + float w = luaL_checknumber(L, 5); + float h = luaL_checknumber(L, 6); + int ret = nk_input_has_mouse_click_down_in_rect(&context->nkctx.input, button, nk_rect(x, y, w, h), down); lua_pushboolean(L, ret); return 1; } @@ -3424,13 +3544,14 @@ static int nk_love_input_has_mouse_released(lua_State *L) static int nk_love_input_is_mouse(int down) { int argc = lua_gettop(L); - nk_love_assert_argc(argc == 5); - enum nk_buttons button = nk_love_checkbutton(1); - float x = luaL_checknumber(L, 2); - float y = luaL_checknumber(L, 3); - float w = luaL_checknumber(L, 4); - float h = luaL_checknumber(L, 5); - int ret = nk_input_is_mouse_click_down_in_rect(&context.input, button, nk_rect(x, y, w, h), down); + nk_love_assert_argc(argc == 6); + nk_love_assert_context(1); + enum nk_buttons button = nk_love_checkbutton(2); + float x = luaL_checknumber(L, 3); + float y = luaL_checknumber(L, 4); + float w = luaL_checknumber(L, 5); + float h = luaL_checknumber(L, 6); + int ret = nk_input_is_mouse_click_down_in_rect(&context->nkctx.input, button, nk_rect(x, y, w, h), down); lua_pushboolean(L, ret); return 1; } @@ -3447,24 +3568,26 @@ static int nk_love_input_is_mouse_released(lua_State *L) static int nk_love_input_was_hovered(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 4); - float x = luaL_checknumber(L, 1); - float y = luaL_checknumber(L, 2); - float w = luaL_checknumber(L, 3); - float h = luaL_checknumber(L, 4); - int was_hovered = nk_input_is_mouse_prev_hovering_rect(&context.input, nk_rect(x, y, w, h)); + nk_love_assert_argc(lua_gettop(L) == 5); + nk_love_assert_context(1); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float w = luaL_checknumber(L, 4); + float h = luaL_checknumber(L, 5); + int was_hovered = nk_input_is_mouse_prev_hovering_rect(&context->nkctx.input, nk_rect(x, y, w, h)); lua_pushboolean(L, was_hovered); return 1; } static int nk_love_input_is_hovered(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 4); - float x = luaL_checknumber(L, 1); - float y = luaL_checknumber(L, 2); - float w = luaL_checknumber(L, 3); - float h = luaL_checknumber(L, 4); - int is_hovered = nk_input_is_mouse_hovering_rect(&context.input, nk_rect(x, y, w, h)); + nk_love_assert_argc(lua_gettop(L) == 5); + nk_love_assert_context(1); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float w = luaL_checknumber(L, 4); + float h = luaL_checknumber(L, 5); + int is_hovered = nk_input_is_mouse_hovering_rect(&context->nkctx.input, nk_rect(x, y, w, h)); lua_pushboolean(L, is_hovered); return 1; } @@ -3473,12 +3596,19 @@ static int nk_love_input_is_hovered(lua_State *L) lua_pushcfunction(L, func); \ lua_setfield(L, -2, name) -LUALIB_API int luaopen_nuklear(lua_State *L) +LUALIB_API int luaopen_nuklear(lua_State *luaState) { - lua_newtable(L); + L = luaState; + edit_buffer = nk_love_malloc(NK_LOVE_EDIT_BUFFER_LEN); + combobox_items = nk_love_malloc(sizeof(char*) * NK_LOVE_COMBOBOX_MAX_ITEMS); + points = nk_love_malloc(sizeof(float) * NK_LOVE_MAX_POINTS * 2); - NK_LOVE_REGISTER("init", nk_love_init); - NK_LOVE_REGISTER("shutdown", nk_love_shutdown); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, LUA_REGISTRYINDEX, "nuklear"); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -3, "methods"); NK_LOVE_REGISTER("keypressed", nk_love_keypressed); NK_LOVE_REGISTER("keyreleased", nk_love_keyreleased); @@ -3490,188 +3620,100 @@ LUALIB_API int luaopen_nuklear(lua_State *L) NK_LOVE_REGISTER("draw", nk_love_draw); - NK_LOVE_REGISTER("frame_begin", nk_love_frame_begin); NK_LOVE_REGISTER("frameBegin", nk_love_frame_begin); - NK_LOVE_REGISTER("frame_end", nk_love_frame_end); NK_LOVE_REGISTER("frameEnd", nk_love_frame_end); - NK_LOVE_REGISTER("window_begin", nk_love_window_begin); NK_LOVE_REGISTER("windowBegin", nk_love_window_begin); - NK_LOVE_REGISTER("window_end", nk_love_window_end); NK_LOVE_REGISTER("windowEnd", nk_love_window_end); - NK_LOVE_REGISTER("window_get_bounds", nk_love_window_get_bounds); NK_LOVE_REGISTER("windowGetBounds", nk_love_window_get_bounds); - NK_LOVE_REGISTER("window_get_position", nk_love_window_get_position); NK_LOVE_REGISTER("windowGetPosition", nk_love_window_get_position); - NK_LOVE_REGISTER("window_get_size", nk_love_window_get_size); NK_LOVE_REGISTER("windowGetSize", nk_love_window_get_size); - NK_LOVE_REGISTER("window_get_content_region", nk_love_window_get_content_region); NK_LOVE_REGISTER("windowGetContentRegion", nk_love_window_get_content_region); - NK_LOVE_REGISTER("window_has_focus", nk_love_window_has_focus); NK_LOVE_REGISTER("windowHasFocus", nk_love_window_has_focus); - NK_LOVE_REGISTER("window_is_collapsed", nk_love_window_is_collapsed); NK_LOVE_REGISTER("windowIsCollapsed", nk_love_window_is_collapsed); - NK_LOVE_REGISTER("window_is_hidden", nk_love_window_is_hidden); NK_LOVE_REGISTER("windowIsHidden", nk_love_window_is_hidden); - NK_LOVE_REGISTER("window_is_active", nk_love_window_is_active); NK_LOVE_REGISTER("windowIsActive", nk_love_window_is_active); - NK_LOVE_REGISTER("window_is_hovered", nk_love_window_is_hovered); NK_LOVE_REGISTER("windowIsHovered", nk_love_window_is_hovered); - NK_LOVE_REGISTER("window_is_any_hovered", nk_love_window_is_any_hovered); NK_LOVE_REGISTER("windowIsAnyHovered", nk_love_window_is_any_hovered); - NK_LOVE_REGISTER("item_is_any_active", nk_love_item_is_any_active); NK_LOVE_REGISTER("itemIsAnyActive", nk_love_item_is_any_active); - NK_LOVE_REGISTER("window_set_bounds", nk_love_window_set_bounds); NK_LOVE_REGISTER("windowSetBounds", nk_love_window_set_bounds); - NK_LOVE_REGISTER("window_set_position", nk_love_window_set_position); NK_LOVE_REGISTER("windowSetPosition", nk_love_window_set_position); - NK_LOVE_REGISTER("window_set_size", nk_love_window_set_size); NK_LOVE_REGISTER("windowSetSize", nk_love_window_set_size); - NK_LOVE_REGISTER("window_set_focus", nk_love_window_set_focus); NK_LOVE_REGISTER("windowSetFocus", nk_love_window_set_focus); - NK_LOVE_REGISTER("window_close", nk_love_window_close); NK_LOVE_REGISTER("windowClose", nk_love_window_close); - NK_LOVE_REGISTER("window_collapse", nk_love_window_collapse); NK_LOVE_REGISTER("windowCollapse", nk_love_window_collapse); - NK_LOVE_REGISTER("window_expand", nk_love_window_expand); NK_LOVE_REGISTER("windowExpand", nk_love_window_expand); - NK_LOVE_REGISTER("window_show", nk_love_window_show); NK_LOVE_REGISTER("windowShow", nk_love_window_show); - NK_LOVE_REGISTER("window_hide", nk_love_window_hide); NK_LOVE_REGISTER("windowHide", nk_love_window_hide); - NK_LOVE_REGISTER("layout_row", nk_love_layout_row); NK_LOVE_REGISTER("layoutRow", nk_love_layout_row); - NK_LOVE_REGISTER("layout_row_begin", nk_love_layout_row_begin); NK_LOVE_REGISTER("layoutRowBegin", nk_love_layout_row_begin); - NK_LOVE_REGISTER("layout_row_push", nk_love_layout_row_push); NK_LOVE_REGISTER("layoutRowPush", nk_love_layout_row_push); - NK_LOVE_REGISTER("layout_row_end", nk_love_layout_row_end); NK_LOVE_REGISTER("layoutRowEnd", nk_love_layout_row_end); - NK_LOVE_REGISTER("layout_space_begin", nk_love_layout_space_begin); NK_LOVE_REGISTER("layoutSpaceBegin", nk_love_layout_space_begin); - NK_LOVE_REGISTER("layout_space_push", nk_love_layout_space_push); NK_LOVE_REGISTER("layoutSpacePush", nk_love_layout_space_push); - NK_LOVE_REGISTER("layout_space_end", nk_love_layout_space_end); NK_LOVE_REGISTER("layoutSpaceEnd", nk_love_layout_space_end); - NK_LOVE_REGISTER("layout_space_bounds", nk_love_layout_space_bounds); NK_LOVE_REGISTER("layoutSpaceBounds", nk_love_layout_space_bounds); - NK_LOVE_REGISTER("layout_space_to_screen", nk_love_layout_space_to_screen); NK_LOVE_REGISTER("layoutSpaceToScreen", nk_love_layout_space_to_screen); - NK_LOVE_REGISTER("layout_space_to_local", nk_love_layout_space_to_local); NK_LOVE_REGISTER("layoutSpaceToLocal", nk_love_layout_space_to_local); - NK_LOVE_REGISTER("layout_space_rect_to_screen", nk_love_layout_space_rect_to_screen); NK_LOVE_REGISTER("layoutSpaceRectToScreen", nk_love_layout_space_rect_to_screen); - NK_LOVE_REGISTER("layout_space_rect_to_local", nk_love_layout_space_rect_to_local); NK_LOVE_REGISTER("layoutSpaceRectToLocal", nk_love_layout_space_rect_to_local); - NK_LOVE_REGISTER("layout_ratio_from_pixel", nk_love_layout_ratio_from_pixel); NK_LOVE_REGISTER("layoutRatioFromPixel", nk_love_layout_ratio_from_pixel); - NK_LOVE_REGISTER("group_begin", nk_love_group_begin); NK_LOVE_REGISTER("groupBegin", nk_love_group_begin); - NK_LOVE_REGISTER("group_end", nk_love_group_end); NK_LOVE_REGISTER("groupEnd", nk_love_group_end); - NK_LOVE_REGISTER("tree_push", nk_love_tree_push); NK_LOVE_REGISTER("treePush", nk_love_tree_push); - NK_LOVE_REGISTER("tree_pop", nk_love_tree_pop); NK_LOVE_REGISTER("treePop", nk_love_tree_pop); - NK_LOVE_REGISTER("color_rgba", nk_love_color_rgba); - NK_LOVE_REGISTER("colorRGBA", nk_love_color_rgba); - NK_LOVE_REGISTER("color_hsva", nk_love_color_hsva); - NK_LOVE_REGISTER("colorHSVA", nk_love_color_hsva); - NK_LOVE_REGISTER("color_parse_rgba", nk_love_color_parse_rgba); - NK_LOVE_REGISTER("colorParseRGBA", nk_love_color_parse_rgba); - NK_LOVE_REGISTER("color_parse_hsva", nk_love_color_parse_hsva); - NK_LOVE_REGISTER("colorParseHSVA", nk_love_color_parse_hsva); - NK_LOVE_REGISTER("label", nk_love_label); NK_LOVE_REGISTER("image", nk_love_image); NK_LOVE_REGISTER("button", nk_love_button); - NK_LOVE_REGISTER("button_set_behavior", nk_love_button_set_behavior); NK_LOVE_REGISTER("buttonSetBehavior", nk_love_button_set_behavior); - NK_LOVE_REGISTER("button_push_behavior", nk_love_button_push_behavior); NK_LOVE_REGISTER("buttonPushBehavior", nk_love_button_push_behavior); - NK_LOVE_REGISTER("button_pop_behavior", nk_love_button_pop_behavior); NK_LOVE_REGISTER("buttonPopBehavior", nk_love_button_pop_behavior); NK_LOVE_REGISTER("checkbox", nk_love_checkbox); NK_LOVE_REGISTER("radio", nk_love_radio); NK_LOVE_REGISTER("selectable", nk_love_selectable); NK_LOVE_REGISTER("slider", nk_love_slider); NK_LOVE_REGISTER("progress", nk_love_progress); - NK_LOVE_REGISTER("color_picker", nk_love_color_picker); NK_LOVE_REGISTER("colorPicker", nk_love_color_picker); NK_LOVE_REGISTER("property", nk_love_property); NK_LOVE_REGISTER("edit", nk_love_edit); - NK_LOVE_REGISTER("popup_begin", nk_love_popup_begin); NK_LOVE_REGISTER("popupBegin", nk_love_popup_begin); - NK_LOVE_REGISTER("popup_close", nk_love_popup_close); NK_LOVE_REGISTER("popupClose", nk_love_popup_close); - NK_LOVE_REGISTER("popup_end", nk_love_popup_end); NK_LOVE_REGISTER("popupEnd", nk_love_popup_end); NK_LOVE_REGISTER("combobox", nk_love_combobox); - NK_LOVE_REGISTER("combobox_begin", nk_love_combobox_begin); NK_LOVE_REGISTER("comboboxBegin", nk_love_combobox_begin); - NK_LOVE_REGISTER("combobox_item", nk_love_combobox_item); NK_LOVE_REGISTER("comboboxItem", nk_love_combobox_item); - NK_LOVE_REGISTER("combobox_close", nk_love_combobox_close); NK_LOVE_REGISTER("comboboxClose", nk_love_combobox_close); - NK_LOVE_REGISTER("combobox_end", nk_love_combobox_end); NK_LOVE_REGISTER("comboboxEnd", nk_love_combobox_end); - NK_LOVE_REGISTER("contextual_begin", nk_love_contextual_begin); NK_LOVE_REGISTER("contextualBegin", nk_love_contextual_begin); - NK_LOVE_REGISTER("contextual_item", nk_love_contextual_item); NK_LOVE_REGISTER("contextualItem", nk_love_contextual_item); - NK_LOVE_REGISTER("contextual_close", nk_love_contextual_close); NK_LOVE_REGISTER("contextualClose", nk_love_contextual_close); - NK_LOVE_REGISTER("contextual_end", nk_love_contextual_end); NK_LOVE_REGISTER("contextualEnd", nk_love_contextual_end); NK_LOVE_REGISTER("tooltip", nk_love_tooltip); - NK_LOVE_REGISTER("tooltip_begin", nk_love_tooltip_begin); NK_LOVE_REGISTER("tooltipBegin", nk_love_tooltip_begin); - NK_LOVE_REGISTER("tooltip_end", nk_love_tooltip_end); NK_LOVE_REGISTER("tooltipEnd", nk_love_tooltip_end); - NK_LOVE_REGISTER("menubar_begin", nk_love_menubar_begin); NK_LOVE_REGISTER("menubarBegin", nk_love_menubar_begin); - NK_LOVE_REGISTER("menubar_end", nk_love_menubar_end); NK_LOVE_REGISTER("menubarEnd", nk_love_menubar_end); - NK_LOVE_REGISTER("menu_begin", nk_love_menu_begin); NK_LOVE_REGISTER("menuBegin", nk_love_menu_begin); - NK_LOVE_REGISTER("menu_item", nk_love_menu_item); NK_LOVE_REGISTER("menuItem", nk_love_menu_item); - NK_LOVE_REGISTER("menu_close", nk_love_menu_close); NK_LOVE_REGISTER("menuClose", nk_love_menu_close); - NK_LOVE_REGISTER("menu_end", nk_love_menu_end); NK_LOVE_REGISTER("menuEnd", nk_love_menu_end); - NK_LOVE_REGISTER("style_default", nk_love_style_default); NK_LOVE_REGISTER("styleDefault", nk_love_style_default); - NK_LOVE_REGISTER("style_load_colors", nk_love_style_load_colors); NK_LOVE_REGISTER("styleLoadColors", nk_love_style_load_colors); - NK_LOVE_REGISTER("style_set_font", nk_love_style_set_font); NK_LOVE_REGISTER("styleSetFont", nk_love_style_set_font); - NK_LOVE_REGISTER("style_push", nk_love_style_push); NK_LOVE_REGISTER("stylePush", nk_love_style_push); - NK_LOVE_REGISTER("style_pop", nk_love_style_pop); NK_LOVE_REGISTER("stylePop", nk_love_style_pop); - NK_LOVE_REGISTER("widget_bounds", nk_love_widget_bounds); NK_LOVE_REGISTER("widgetBounds", nk_love_widget_bounds); - NK_LOVE_REGISTER("widget_position", nk_love_widget_position); NK_LOVE_REGISTER("widgetPosition", nk_love_widget_position); - NK_LOVE_REGISTER("widget_size", nk_love_widget_size); NK_LOVE_REGISTER("widgetSize", nk_love_widget_size); - NK_LOVE_REGISTER("widget_width", nk_love_widget_width); NK_LOVE_REGISTER("widgetWidth", nk_love_widget_width); - NK_LOVE_REGISTER("widget_height", nk_love_widget_height); NK_LOVE_REGISTER("widgetHeight", nk_love_widget_height); - NK_LOVE_REGISTER("widget_is_hovered", nk_love_widget_is_hovered); NK_LOVE_REGISTER("widgetIsHovered", nk_love_widget_is_hovered); - NK_LOVE_REGISTER("widget_is_mouse_clicked", nk_love_widget_is_mouse_clicked); - NK_LOVE_REGISTER("widgetIsMouseClicked", nk_love_widget_is_mouse_clicked); - NK_LOVE_REGISTER("widget_has_mouse_click", nk_love_widget_has_mouse_click); - NK_LOVE_REGISTER("widgetHasMouseClick", nk_love_widget_has_mouse_click); NK_LOVE_REGISTER("widgetHasMousePressed", nk_love_widget_has_mouse_pressed); NK_LOVE_REGISTER("widgetHasMouseReleased", nk_love_widget_has_mouse_released); NK_LOVE_REGISTER("widgetIsMousePressed", nk_love_widget_is_mouse_pressed); @@ -3696,6 +3738,20 @@ LUALIB_API int luaopen_nuklear(lua_State *L) NK_LOVE_REGISTER("inputWasHovered", nk_love_input_was_hovered); NK_LOVE_REGISTER("inputIsHovered", nk_love_input_is_hovered); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -4, "metatable"); + NK_LOVE_REGISTER("__gc", nk_love_shutdown); + lua_pushvalue(L, -2); + lua_setfield(L, -2, "__index"); + + lua_newtable(L); + NK_LOVE_REGISTER("init", nk_love_init); + NK_LOVE_REGISTER("colorRGBA", nk_love_color_rgba); + NK_LOVE_REGISTER("colorHSVA", nk_love_color_hsva); + NK_LOVE_REGISTER("colorParseRGBA", nk_love_color_parse_rgba); + NK_LOVE_REGISTER("colorParseHSVA", nk_love_color_parse_hsva); + return 1; } From 8b5482e74f3ab9283189936e67135aa96ad158ca Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Tue, 20 Dec 2016 15:48:07 -0500 Subject: [PATCH 09/52] Fix frameBegin to use correct context --- src/nuklear_love.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index d688c9c..882efce 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -1030,7 +1030,7 @@ static int nk_love_shutdown(lua_State *luaState) free(ctx->fonts); free(ctx->layout_ratios); lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); - lua_pushlightuserdata(L, context); + lua_pushlightuserdata(L, ctx); lua_pushnil(L); lua_settable(L, -3); return 0; From 26f05c82e44048a907278f2c2bcb3bb882c93a0a Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Thu, 29 Dec 2016 23:17:13 -0500 Subject: [PATCH 10/52] Allow per-context transformations (#9) --- example/draw.lua | 2 + example/main.lua | 43 +++-- example/overview.lua | 2 +- example/skin.lua | 2 + example/transform.lua | 14 ++ src/nuklear_love.c | 409 ++++++++++++++++++++++++++++++++++-------- 6 files changed, 377 insertions(+), 95 deletions(-) create mode 100644 example/transform.lua diff --git a/example/draw.lua b/example/draw.lua index c276f47..3a5611b 100644 --- a/example/draw.lua +++ b/example/draw.lua @@ -1,3 +1,5 @@ +-- Demonstrate a number of custom drawing functions. + local img = love.graphics.newImage 'skin/button.png' return function (ui) diff --git a/example/main.lua b/example/main.lua index 583ea1a..6c60133 100644 --- a/example/main.lua +++ b/example/main.lua @@ -7,52 +7,61 @@ local draw = require 'draw' local overview = require 'overview' local style = require 'style' local skin = require 'skin' +local transform = require 'transform' -local ui +local ui1, ui2 function love.load() - ui = nuklear.init() + ui1, ui2 = nuklear.newUI(), nuklear.newUI() end function love.update(dt) - ui:frameBegin() - calculator(ui) - style(ui) - overview(ui) - draw(ui) - skin(ui) - ui:frameEnd() + ui1:frameBegin() + calculator(ui1) + style(ui1) + overview(ui1) + draw(ui1) + skin(ui1) + ui1:frameEnd() + ui2:frameBegin() + transform(ui2) + ui2:frameEnd() end function love.draw() - ui:draw() + ui1:draw() + ui2:draw() love.graphics.print("Current FPS: "..tostring(love.timer.getFPS( )), 10, 10) end +local function input(name, ...) + return ui2[name](ui2, ...) or ui1[name](ui1, ...) +end + function love.keypressed(key, scancode, isrepeat) - ui:keypressed(key, scancode, isrepeat) + input('keypressed', key, scancode, isrepeat) end function love.keyreleased(key, scancode) - ui:keyreleased(key, scancode) + input('keyreleased', key, scancode) end function love.mousepressed(x, y, button, istouch) - ui:mousepressed(x, y, button, istouch) + input('mousepressed', x, y, button, istouch) end function love.mousereleased(x, y, button, istouch) - ui:mousereleased(x, y, button, istouch) + input('mousereleased', x, y, button, istouch) end function love.mousemoved(x, y, dx, dy, istouch) - ui:mousemoved(x, y, dx, dy, istouch) + input('mousemoved', x, y, dx, dy, istouch) end function love.textinput(text) - ui:textinput(text) + input('textinput', text) end function love.wheelmoved(x, y) - ui:wheelmoved(x, y) + input('wheelmoved', x, y) end diff --git a/example/overview.lua b/example/overview.lua index c24db18..5909c77 100644 --- a/example/overview.lua +++ b/example/overview.lua @@ -24,7 +24,7 @@ return function (ui) ui:menuEnd() end ui:menubarEnd() - ui:layoutRow('dynamic', 400, 3) + ui:layoutRow('dynamic', 375, 3) if ui:groupBegin('Group 1', 'border') then ui:layoutRow('dynamic', 30, 1) ui:label('Left label') diff --git a/example/skin.lua b/example/skin.lua index b189491..968e0a8 100644 --- a/example/skin.lua +++ b/example/skin.lua @@ -1,3 +1,5 @@ +-- Basic skinning example. + local windowHeader = love.graphics.newImage 'skin/window_header.png' local checkboxSkin = love.graphics.newImage 'skin/checkbox_false.png' local checkboxCheck = love.graphics.newImage 'skin/checkbox_true.png' diff --git a/example/transform.lua b/example/transform.lua new file mode 100644 index 0000000..9efcc5b --- /dev/null +++ b/example/transform.lua @@ -0,0 +1,14 @@ +-- Apply transformations to a basic UI. + +return function(ui) + local t = love.timer.getTime() + ui:translate(350 + 100 * math.cos(t / 4), 350 + 100 * math.sin(t / 4)) + ui:rotate(t / 8) + ui:scale(1 + math.sin(t / 4) / 2, 1 + math.cos(t / 4) / 2) + ui:shear(math.cos(t / 8) / 4, math.sin(t / 8) / 4) + if ui:windowBegin('Transform', 0, 0, 200, 200, 'border', 'title') then + ui:layoutRow('dynamic', 150, 1) + ui:label('You can apply transformations to the UI using ui:rotate, ui:scale, ui:shear, and ui:translate.', 'wrap') + end + ui:windowEnd() +end diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 882efce..5896e95 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -4,6 +4,7 @@ * adapted to LOVE in 2016 by Kevin Harrison */ +#include #include #include @@ -38,18 +39,22 @@ #define NK_LOVE_COMBOBOX_MAX_ITEMS 1024 #define NK_LOVE_MAX_FONTS 1024 #define NK_LOVE_MAX_RATIOS 1024 +#define NK_LOVE_GRADIENT_RESOLUTION 32 static lua_State *L; static char *edit_buffer; static const char **combobox_items; static float *points; -struct nk_love_context { +static struct nk_love_context { struct nk_context nkctx; struct nk_user_font *fonts; int font_count; float *layout_ratios; int layout_ratio_count; + float T[9]; + float Ti[9]; + int transform_allowed; } *context; static void nk_love_assert(int pass, const char *msg) @@ -101,9 +106,17 @@ static struct nk_love_context *nk_love_checkcontext(int index) static void nk_love_assert_context(int index) { struct nk_love_context *ctx = nk_love_checkcontext(index); + ctx->transform_allowed = 0; nk_love_assert(ctx == context, "%s: UI calls must reside between ui:frameBegin and ui:frameEnd"); } +static void nk_love_assert_transform(void) +{ + struct nk_love_context *ctx = nk_love_checkcontext(1); + nk_love_assert(ctx == context && ctx->transform_allowed, + "%s: UI transformations must occur directly after ui:frameBegin"); +} + static void nk_love_pushregistry(const char *name) { lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); @@ -240,6 +253,21 @@ static struct nk_color nk_love_checkcolor(int index) return color; } +static void nk_love_color(int r, int g, int b, int a, char *color_string) +{ + r = NK_CLAMP(0, r, 255); + g = NK_CLAMP(0, g, 255); + b = NK_CLAMP(0, b, 255); + a = NK_CLAMP(0, a, 255); + const char *format_string; + if (a < 255) { + format_string = "#%02x%02x%02x%02x"; + } else { + format_string = "#%02x%02x%02x"; + } + sprintf(color_string, format_string, r, g, b, a); +} + static nk_flags nk_love_parse_window_flags(int flags_begin) { int argc = lua_gettop(L); @@ -490,6 +518,15 @@ static int nk_love_checkboolean(lua_State *L, int index) return lua_toboolean(L, index); } +static void nk_love_transform(float *T, int *x, int *y) +{ + float rx, ry; + rx = *x * T[0] + *y * T[3] + T[6]; + ry = *x * T[1] + *y * T[4] + T[7]; + *x = (int) rx; + *y = (int) ry; +} + static void nk_love_configureGraphics(int line_thickness, struct nk_color col) { lua_getglobal(L, "love"); @@ -530,10 +567,19 @@ static void nk_love_scissor(int x, int y, int w, int h) lua_getglobal(L, "love"); lua_getfield(L, -1, "graphics"); lua_getfield(L, -1, "setScissor"); - lua_pushnumber(L, x); - lua_pushnumber(L, y); - lua_pushnumber(L, w); - lua_pushnumber(L, h); + int x1 = x, y1 = y, x2 = x + w, y2 = y, x3 = x, y3 = y + h, x4 = x + w, y4 = y + h; + nk_love_transform(context->T, &x1, &y1); + nk_love_transform(context->T, &x2, &y2); + nk_love_transform(context->T, &x3, &y3); + nk_love_transform(context->T, &x4, &y4); + int left = NK_MIN(NK_MIN(x1, x2), NK_MIN(x3, x4)); + int top = NK_MIN(NK_MIN(y1, y2), NK_MIN(y3, y4)); + int right = NK_MAX(NK_MAX(x1, x2), NK_MAX(x3, x4)); + int bottom = NK_MAX(NK_MAX(y1, y2), NK_MAX(y3, y4)); + lua_pushnumber(L, left); + lua_pushnumber(L, top); + lua_pushnumber(L, right - left); + lua_pushnumber(L, bottom - top); lua_call(L, 4, 0); lua_pop(L, 2); } @@ -727,59 +773,57 @@ static void interpolate_color(struct nk_color c1, struct nk_color c2, } static void nk_love_draw_rect_multi_color(int x, int y, unsigned int w, - unsigned int h, struct nk_color left, struct nk_color top, - struct nk_color right, struct nk_color bottom) + unsigned int h, struct nk_color top_left, struct nk_color top_right, + struct nk_color bottom_left, struct nk_color bottom_right) { lua_getglobal(L, "love"); lua_getfield(L, -1, "graphics"); - - lua_getfield(L, -1, "push"); - lua_pushstring(L, "all"); - lua_call(L, 1, 0); lua_getfield(L, -1, "setColor"); lua_pushnumber(L, 255); lua_pushnumber(L, 255); lua_pushnumber(L, 255); lua_call(L, 3, 0); - lua_getfield(L, -1, "setPointSize"); - lua_pushnumber(L, 1); - lua_call(L, 1, 0); + lua_pop(L, 2); - struct nk_color X1, X2, Y; - float fraction_x, fraction_y; - int i,j; - - lua_getfield(L, -1, "points"); - lua_createtable(L, w * h, 0); - - for (j = 0; j < h; j++) { - fraction_y = ((float)j) / h; - for (i = 0; i < w; i++) { - fraction_x = ((float)i) / w; - interpolate_color(left, top, &X1, fraction_x); - interpolate_color(right, bottom, &X2, fraction_x); - interpolate_color(X1, X2, &Y, fraction_y); - lua_createtable(L, 6, 0); - lua_pushnumber(L, x + i); - lua_rawseti(L, -2, 1); - lua_pushnumber(L, y + j); - lua_rawseti(L, -2, 2); - lua_pushnumber(L, Y.r); - lua_rawseti(L, -2, 3); - lua_pushnumber(L, Y.g); - lua_rawseti(L, -2, 4); - lua_pushnumber(L, Y.b); - lua_rawseti(L, -2, 5); - lua_pushnumber(L, Y.a); - lua_rawseti(L, -2, 6); - lua_rawseti(L, -2, i + j * w + 1); + lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); + lua_getfield(L, -1, "gradientData"); + int row, col; + for (row = 0; row < NK_LOVE_GRADIENT_RESOLUTION; ++row) { + float row_ratio = (float) row / NK_LOVE_GRADIENT_RESOLUTION; + struct nk_color left, right; + interpolate_color(top_left, bottom_left, &left, row_ratio); + interpolate_color(top_right, bottom_right, &right, row_ratio); + for (col = 0; col < NK_LOVE_GRADIENT_RESOLUTION; ++col) { + float col_ratio = (float) col / NK_LOVE_GRADIENT_RESOLUTION; + struct nk_color pixel; + interpolate_color(left, right, &pixel, col_ratio); + lua_getfield(L, -1, "setPixel"); + lua_pushvalue(L, -2); + lua_pushnumber(L, col); + lua_pushnumber(L, row); + lua_pushnumber(L, pixel.r); + lua_pushnumber(L, pixel.g); + lua_pushnumber(L, pixel.b); + lua_pushnumber(L, pixel.a); + lua_call(L, 7, 0); } } - + lua_pop(L, 1); + lua_getfield(L, -1, "gradient"); + lua_getfield(L, -1, "refresh"); + lua_pushvalue(L, -2); lua_call(L, 1, 0); - lua_getfield(L, -1, "pop"); - lua_call(L, 0, 0); + lua_getglobal(L, "love"); + lua_getfield(L, -1, "graphics"); + lua_getfield(L, -1, "draw"); + lua_replace(L, -5); lua_pop(L, 2); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + lua_pushnumber(L, 0); + lua_pushnumber(L, (float) w / NK_LOVE_GRADIENT_RESOLUTION); + lua_pushnumber(L, (float) h / NK_LOVE_GRADIENT_RESOLUTION); + lua_call(L, 6, 0); } static void nk_love_draw_image(int x, int y, unsigned int w, unsigned int h, @@ -941,25 +985,29 @@ static int nk_love_keyevent(struct nk_context *ctx, const char *key, return nk_love_is_active(ctx); } -static int nk_love_clickevent(struct nk_context *ctx, int x, int y, +static int nk_love_clickevent(struct nk_love_context *ctx, int x, int y, int button, int istouch, int down) { + nk_love_transform(ctx->Ti, &x, &y); + struct nk_context *nkctx = &ctx->nkctx; if (button == 1) - nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down); + nk_input_button(nkctx, NK_BUTTON_LEFT, x, y, down); else if (button == 3) - nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down); + nk_input_button(nkctx, NK_BUTTON_MIDDLE, x, y, down); else if (button == 2) - nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down); + nk_input_button(nkctx, NK_BUTTON_RIGHT, x, y, down); else return 0; - return nk_window_is_any_hovered(ctx); + return nk_window_is_any_hovered(nkctx); } -static int nk_love_mousemoved_event(struct nk_context *ctx, int x, int y, +static int nk_love_mousemoved_event(struct nk_love_context *ctx, int x, int y, int dx, int dy, int istouch) { - nk_input_motion(ctx, x, y); - return nk_window_is_any_hovered(ctx); + nk_love_transform(ctx->Ti, &x, &y); + struct nk_context *nkctx = &ctx->nkctx; + nk_input_motion(nkctx, x, y); + return nk_window_is_any_hovered(nkctx); } static int nk_love_textinput_event(struct nk_context *ctx, const char *text) @@ -984,7 +1032,7 @@ static int nk_love_wheelmoved_event(struct nk_context *ctx, int x, int y) * =============================================================== */ -static int nk_love_init(lua_State *L) +static int nk_love_new_ui(lua_State *L) { nk_love_assert_argc(lua_gettop(L) == 0); lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); @@ -1022,7 +1070,7 @@ static int nk_love_init(lua_State *L) return 1; } -static int nk_love_shutdown(lua_State *luaState) +static int nk_love_destroy(lua_State *luaState) { nk_love_assert_argc(lua_gettop(L) == 1); struct nk_love_context *ctx = nk_love_checkcontext(1); @@ -1062,7 +1110,7 @@ static int nk_love_keyreleased(lua_State *L) static int nk_love_mousepressed(lua_State *L) { nk_love_assert_argc(lua_gettop(L) == 5); - struct nk_context *ctx = &nk_love_checkcontext(1)->nkctx; + struct nk_love_context *ctx = nk_love_checkcontext(1); int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); int button = luaL_checkint(L, 4); @@ -1075,7 +1123,7 @@ static int nk_love_mousepressed(lua_State *L) static int nk_love_mousereleased(lua_State *L) { nk_love_assert_argc(lua_gettop(L) == 5); - struct nk_context *ctx = &nk_love_checkcontext(1)->nkctx; + struct nk_love_context *ctx = nk_love_checkcontext(1); int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); int button = luaL_checkint(L, 4); @@ -1088,7 +1136,7 @@ static int nk_love_mousereleased(lua_State *L) static int nk_love_mousemoved(lua_State *L) { nk_love_assert_argc(lua_gettop(L) == 6); - struct nk_context *ctx = &nk_love_checkcontext(1)->nkctx; + struct nk_love_context *ctx = nk_love_checkcontext(1); int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); int dx = luaL_checkint(L, 4); @@ -1135,6 +1183,21 @@ static int nk_love_draw(lua_State *L) lua_getfield(L, -1, "origin"); lua_call(L, 0, 0); + nk_love_pushregistry("transform"); + size_t transform_count = lua_objlen(L, -1); + size_t i, j; + for (i = 1; i <= transform_count; ++i) { + lua_rawgeti(L, -1, i); + lua_rawgeti(L, -1, 1); + lua_gettable(L, -4); + size_t transform_size = lua_objlen(L, -2); + for (j = 2; j <= transform_size; ++j) + lua_rawgeti(L, -j, j); + lua_call(L, transform_size - 1, 0); + lua_pop(L, 1); + } + lua_pop(L, 1); + const struct nk_command *cmd; nk_foreach(cmd, &context->nkctx) { @@ -1167,7 +1230,7 @@ static int nk_love_draw(lua_State *L) nk_love_draw_circle(c->x, c->y, c->w, c->h, -1, c->color); } break; case NK_COMMAND_TRIANGLE: { - const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd; + const struct nk_command_triangle *t = (const struct nk_command_triangle*)cmd; nk_love_draw_triangle(t->a.x, t->a.y, t->b.x, t->b.y, t->c.x, t->c.y, t->line_thickness, t->color); } break; @@ -1200,7 +1263,7 @@ static int nk_love_draw(lua_State *L) } break; case NK_COMMAND_RECT_MULTI_COLOR: { const struct nk_command_rect_multi_color *r = (const struct nk_command_rect_multi_color *)cmd; - nk_love_draw_rect_multi_color(r->x, r->y, r->w, r->h, r->left, r->top, r->right, r->bottom); + nk_love_draw_rect_multi_color(r->x, r->y, r->w, r->h, r->left, r->top, r->bottom, r->right); } break; case NK_COMMAND_IMAGE: { const struct nk_command_image *i = (const struct nk_command_image *)cmd; @@ -1223,6 +1286,7 @@ static int nk_love_draw(lua_State *L) lua_call(L, 0, 0); lua_pop(L, 2); nk_clear(&context->nkctx); + context = NULL; return 0; } @@ -1358,6 +1422,7 @@ static void nk_love_preserve_all(void) static int nk_love_frame_begin(lua_State *L) { nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert(context == NULL, "%s: missing ui:frameEnd for previous frame"); context = nk_love_checkcontext(1); nk_input_end(&context->nkctx); lua_getglobal(L, "love"); @@ -1390,7 +1455,13 @@ static int nk_love_frame_begin(lua_State *L) lua_pop(L, 1); context->nkctx.stacks.fonts.elements[i].old_value = &context->fonts[context->font_count++]; } + lua_pop(L, 1); context->layout_ratio_count = 0; + for (i = 0; i < 9; ++i) + context->T[i] = context->Ti[i] = (i % 3 == i / 3); + context->transform_allowed = 1; + lua_newtable(L); + lua_setfield(L, -2, "transform"); return 0; } @@ -1403,6 +1474,183 @@ static int nk_love_frame_end(lua_State *L) return 0; } +/* +cos -sin 0 | cos sin 0 +sin cos 0 | -sin cos 0 +0 0 1 | 0 0 1 +*/ +static int nk_love_rotate(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_transform(); + float angle = luaL_checknumber(L, 2); + nk_love_pushregistry("transform"); + size_t len = lua_objlen(L, -1); + lua_newtable(L); + lua_pushstring(L, "rotate"); + lua_rawseti(L, -2, 1); + lua_pushnumber(L, angle); + lua_rawseti(L, -2, 2); + lua_rawseti(L, -2, len + 1); + float *T = context->T, *Ti = context->Ti; + float c = cosf(angle); + float s = sinf(angle); + int i; + float R[9]; + R[0] = T[0] * c + T[3] * s; + R[1] = T[1] * c + T[4] * s; + R[2] = T[2] * c + T[5] * s; + R[3] = T[0] * -s + T[3] * c; + R[4] = T[1] * -s + T[4] * c; + R[5] = T[2] * -s + T[5] * c; + R[6] = T[6]; + R[7] = T[7]; + R[8] = T[8]; + for (i = 0; i < 9; ++i) + T[i] = R[i]; + R[0] = c * Ti[0] + s * Ti[1]; + R[1] = -s * Ti[0] + c * Ti[1]; + R[2] = Ti[2]; + R[3] = c * Ti[3] + s * Ti[4]; + R[4] = -s * Ti[3] + c * Ti[4]; + R[5] = Ti[5]; + R[6] = c * Ti[6] + s * Ti[7]; + R[7] = -s * Ti[6] + c * Ti[7]; + R[8] = Ti[8]; + for (i = 0; i < 9; ++i) + Ti[i] = R[i]; + return 0; +} + +/* +sx 0 0 | 1/sx 0 0 +0 sy 0 | 0 1/sy 0 +0 0 1 | 0 0 1 +*/ +static int nk_love_scale(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) >= 2 && lua_gettop(L) <= 3); + nk_love_assert_transform(); + float sx = luaL_checknumber(L, 2); + float sy = luaL_optnumber(L, 3, sx); + nk_love_pushregistry("transform"); + size_t len = lua_objlen(L, -1); + lua_newtable(L); + lua_pushstring(L, "scale"); + lua_rawseti(L, -2, 1); + lua_pushnumber(L, sx); + lua_rawseti(L, -2, 2); + lua_pushnumber(L, sy); + lua_rawseti(L, -2, 3); + lua_rawseti(L, -2, len + 1); + float *T = context->T, *Ti = context->Ti; + T[0] *= sx; + T[1] *= sx; + T[2] *= sx; + T[3] *= sy; + T[4] *= sy; + T[5] *= sy; + Ti[0] /= sx; + Ti[1] /= sy; + Ti[3] /= sx; + Ti[4] /= sy; + Ti[6] /= sx; + Ti[7] /= sy; + return 0; +} + +/* +1 kx 0 | 1/(1-kx*ky) kx/(kx*ky-1) 0 +ky 1 0 | ky/(kx*ky-1) 1/(1-kx*ky) 0 +0 0 1 | 0 0 1 +*/ +static int nk_love_shear(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) == 3); + nk_love_assert_transform(); + float kx = luaL_checknumber(L, 2); + float ky = luaL_checknumber(L, 3); + nk_love_pushregistry("transform"); + size_t len = lua_objlen(L, -1); + lua_newtable(L); + lua_pushstring(L, "shear"); + lua_rawseti(L, -2, 1); + lua_pushnumber(L, kx); + lua_rawseti(L, -2, 2); + lua_pushnumber(L, ky); + lua_rawseti(L, -2, 3); + lua_rawseti(L, -2, len + 1); + float *T = context->T, *Ti = context->Ti; + float R[9]; + R[0] = T[0] + T[3] * ky; + R[1] = T[1] + T[4] * ky; + R[2] = T[2] + T[5] * ky; + R[3] = T[0] * kx + T[3]; + R[4] = T[1] * kx + T[4]; + R[5] = T[2] * kx + T[5]; + R[6] = T[6]; + R[7] = T[7]; + R[8] = T[8]; + int i; + for (i = 0; i < 9; ++i) + T[i] = R[i]; + float a = 1.0f / (1 - kx * ky); + float b = 1.0f / (kx * ky - 1); + R[0] = a * Ti[0] + kx * b * Ti[1]; + R[1] = ky * b * Ti[0] + a * Ti[1]; + R[2] = Ti[2]; + R[3] = a * Ti[3] + kx * b * Ti[4]; + R[4] = ky * b * Ti[3] + a * Ti[4]; + R[5] = Ti[5]; + R[6] = a * Ti[6] + kx * b * Ti[7]; + R[7] = ky * b * Ti[6] + a * Ti[7]; + R[8] = Ti[8]; + for (i = 0; i < 9; ++i) + Ti[i] = R[i]; + return 0; +} + +/* +1 0 dx | 1 0 -dx +0 1 dy | 0 1 -dy +0 0 1 | 0 0 1 +*/ +static int nk_love_translate(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) == 3); + nk_love_assert_transform(); + float dx = luaL_checknumber(L, 2); + float dy = luaL_checknumber(L, 3); + nk_love_pushregistry("transform"); + size_t len = lua_objlen(L, -1); + lua_newtable(L); + lua_pushstring(L, "translate"); + lua_rawseti(L, -2, 1); + lua_pushnumber(L, dx); + lua_rawseti(L, -2, 2); + lua_pushnumber(L, dy); + lua_rawseti(L, -2, 3); + lua_rawseti(L, -2, len + 1); + float *T = context->T, *Ti = context->Ti; + float R[9]; + T[6] += T[0] * dx + T[3] * dy; + T[7] += T[1] * dx + T[4] * dy; + T[8] += T[2] * dx + T[5] * dy; + R[0] = Ti[0] - dx * Ti[2]; + R[1] = Ti[1] - dy * Ti[2]; + R[2] = Ti[2]; + R[3] = Ti[3] - dx * Ti[5]; + R[4] = Ti[4] - dy * Ti[5]; + R[5] = Ti[5]; + R[6] = Ti[6] - dx * Ti[8]; + R[7] = Ti[7] - dy * Ti[8]; + R[8] = Ti[8]; + int i; + for (i = 0; i < 9; ++i) + Ti[i] = R[i]; + return 0; +} + static int nk_love_window_begin(lua_State *L) { nk_love_assert_argc(lua_gettop(L) >= 1); @@ -1877,21 +2125,6 @@ static int nk_love_tree_pop(lua_State *L) return 0; } -static void nk_love_color(int r, int g, int b, int a, char *color_string) -{ - r = NK_CLAMP(0, r, 255); - g = NK_CLAMP(0, g, 255); - b = NK_CLAMP(0, b, 255); - a = NK_CLAMP(0, a, 255); - const char *format_string; - if (a < 255) { - format_string = "#%02x%02x%02x%02x"; - } else { - format_string = "#%02x%02x%02x"; - } - sprintf(color_string, format_string, r, g, b, a); -} - static int nk_love_color_rgba(lua_State *L) { int argc = lua_gettop(L); @@ -3606,6 +3839,23 @@ LUALIB_API int luaopen_nuklear(lua_State *luaState) lua_newtable(L); lua_pushvalue(L, -1); lua_setfield(L, LUA_REGISTRYINDEX, "nuklear"); + + lua_getglobal(L, "love"); + nk_love_assert(lua_istable(L, -1), "LOVE-Nuklear requires LOVE environment"); + lua_getfield(L, -1, "image"); + lua_getfield(L, -1, "newImageData"); + lua_pushnumber(L, NK_LOVE_GRADIENT_RESOLUTION); + lua_pushnumber(L, NK_LOVE_GRADIENT_RESOLUTION); + lua_call(L, 2, 1); + lua_getfield(L, -3, "graphics"); + lua_getfield(L, -1, "newImage"); + lua_pushvalue(L, -3); + lua_call(L, 1, 1); + lua_setfield(L, -6, "gradient"); + lua_pop(L, 1); + lua_setfield(L, -4, "gradientData"); + lua_pop(L, 2); + lua_newtable(L); lua_pushvalue(L, -1); lua_setfield(L, -3, "methods"); @@ -3623,6 +3873,11 @@ LUALIB_API int luaopen_nuklear(lua_State *luaState) NK_LOVE_REGISTER("frameBegin", nk_love_frame_begin); NK_LOVE_REGISTER("frameEnd", nk_love_frame_end); + NK_LOVE_REGISTER("rotate", nk_love_rotate); + NK_LOVE_REGISTER("scale", nk_love_scale); + NK_LOVE_REGISTER("shear", nk_love_shear); + NK_LOVE_REGISTER("translate", nk_love_translate); + NK_LOVE_REGISTER("windowBegin", nk_love_window_begin); NK_LOVE_REGISTER("windowEnd", nk_love_window_end); NK_LOVE_REGISTER("windowGetBounds", nk_love_window_get_bounds); @@ -3741,12 +3996,12 @@ LUALIB_API int luaopen_nuklear(lua_State *luaState) lua_newtable(L); lua_pushvalue(L, -1); lua_setfield(L, -4, "metatable"); - NK_LOVE_REGISTER("__gc", nk_love_shutdown); + NK_LOVE_REGISTER("__gc", nk_love_destroy); lua_pushvalue(L, -2); lua_setfield(L, -2, "__index"); lua_newtable(L); - NK_LOVE_REGISTER("init", nk_love_init); + NK_LOVE_REGISTER("newUI", nk_love_new_ui); NK_LOVE_REGISTER("colorRGBA", nk_love_color_rgba); NK_LOVE_REGISTER("colorHSVA", nk_love_color_hsva); NK_LOVE_REGISTER("colorParseRGBA", nk_love_color_parse_rgba); From b51de073dae502459f304f05489e785db092dca7 Mon Sep 17 00:00:00 2001 From: Martin Gerhardy Date: Fri, 5 Jan 2018 17:05:36 +0100 Subject: [PATCH 11/52] Fixed warnings and removed unused methods and global vars --- src/nuklear_love.c | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 7ee6353..7308f2e 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -45,7 +45,6 @@ static struct nk_user_font *fonts; static int font_count; static char *edit_buffer; static const char **combobox_items; -static struct nk_cursor cursors[NK_CURSOR_COUNT]; static float *floats; static int layout_ratio_count; @@ -363,28 +362,6 @@ static void nk_love_draw_rect_multi_color(int x, int y, unsigned int w, lua_pop(L, 2); } -static void nk_love_clear(struct nk_color col) -{ - lua_getglobal(L, "love"); - lua_getfield(L, -1, "graphics"); - lua_getfield(L, -1, "clear"); - lua_pushnumber(L, col.r); - lua_pushnumber(L, col.g); - lua_pushnumber(L, col.b); - lua_pushnumber(L, col.a); - lua_call(L, 4, 0); - lua_pop(L, 2); -} - -static void nk_love_blit() -{ - lua_getglobal(L, "love"); - lua_getfield(L, -1, "graphics"); - lua_getfield(L, -1, "present"); - lua_call(L, 0, 0); - lua_pop(L, 2); -} - static void nk_love_draw_image(int x, int y, unsigned int w, unsigned int h, struct nk_image image, struct nk_color color) { @@ -3437,12 +3414,12 @@ static int nk_love_input_is_mouse(int down) static int nk_love_input_is_mouse_pressed(lua_State *L) { - nk_love_input_is_mouse(nk_true); + return nk_love_input_is_mouse(nk_true); } static int nk_love_input_is_mouse_released(lua_State *L) { - nk_love_input_is_mouse(nk_false); + return nk_love_input_is_mouse(nk_false); } static int nk_love_input_was_hovered(lua_State *L) From d8234fcc1c0bf108a1ea23f70a14266e8cc2d71b Mon Sep 17 00:00:00 2001 From: James Nelson Date: Wed, 18 Apr 2018 14:23:09 -0600 Subject: [PATCH 12/52] Change color scale from 0 - 255 to 0 - 1.0 --- src/nuklear_love.c | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 7ee6353..e7e15b1 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -60,10 +60,10 @@ static void nk_love_configureGraphics(int line_thickness, struct nk_color col) lua_call(L, 1, 0); } lua_getfield(L, -1, "setColor"); - lua_pushnumber(L, col.r); - lua_pushnumber(L, col.g); - lua_pushnumber(L, col.b); - lua_pushnumber(L, col.a); + lua_pushnumber(L, col.r / 255.0); + lua_pushnumber(L, col.g / 255.0); + lua_pushnumber(L, col.b / 255.0); + lua_pushnumber(L, col.a / 255.0); lua_call(L, 4, 0); } @@ -77,10 +77,10 @@ static void nk_love_getGraphics(float *line_thickness, struct nk_color *color) lua_pop(L, 1); lua_getfield(L, -1, "getColor"); lua_call(L, 0, 4); - color->r = lua_tointeger(L, -4); - color->g = lua_tointeger(L, -3); - color->b = lua_tointeger(L, -2); - color->a = lua_tointeger(L, -1); + color->r = lua_tointeger(L, -4) * 255.0; + color->g = lua_tointeger(L, -3) * 255.0; + color->b = lua_tointeger(L, -2) * 255.0; + color->a = lua_tointeger(L, -1) * 255.0; lua_pop(L, 6); } @@ -255,10 +255,10 @@ static void nk_love_draw_text(int fontref, struct nk_color cbg, lua_getfield(L, -1, "graphics"); lua_getfield(L, -1, "setColor"); - lua_pushnumber(L, cbg.r); - lua_pushnumber(L, cbg.g); - lua_pushnumber(L, cbg.b); - lua_pushnumber(L, cbg.a); + lua_pushnumber(L, cbg.r / 255.0); + lua_pushnumber(L, cbg.g / 255.0); + lua_pushnumber(L, cbg.b / 255.0); + lua_pushnumber(L, cbg.a / 255.0); lua_call(L, 4, 0); lua_getfield(L, -1, "rectangle"); @@ -270,10 +270,10 @@ static void nk_love_draw_text(int fontref, struct nk_color cbg, lua_call(L, 5, 0); lua_getfield(L, -1, "setColor"); - lua_pushnumber(L, cfg.r); - lua_pushnumber(L, cfg.g); - lua_pushnumber(L, cfg.b); - lua_pushnumber(L, cfg.a); + lua_pushnumber(L, cfg.r / 255.0); + lua_pushnumber(L, cfg.g / 255.0); + lua_pushnumber(L, cfg.b / 255.0); + lua_pushnumber(L, cfg.a / 255.0); lua_call(L, 4, 0); lua_getfield(L, -1, "setFont"); @@ -318,9 +318,9 @@ static void nk_love_draw_rect_multi_color(int x, int y, unsigned int w, lua_pushstring(L, "all"); lua_call(L, 1, 0); lua_getfield(L, -1, "setColor"); - lua_pushnumber(L, 255); - lua_pushnumber(L, 255); - lua_pushnumber(L, 255); + lua_pushnumber(L, 1.0); + lua_pushnumber(L, 1.0); + lua_pushnumber(L, 1.0); lua_call(L, 3, 0); lua_getfield(L, -1, "setPointSize"); lua_pushnumber(L, 1); @@ -368,10 +368,10 @@ static void nk_love_clear(struct nk_color col) lua_getglobal(L, "love"); lua_getfield(L, -1, "graphics"); lua_getfield(L, -1, "clear"); - lua_pushnumber(L, col.r); - lua_pushnumber(L, col.g); - lua_pushnumber(L, col.b); - lua_pushnumber(L, col.a); + lua_pushnumber(L, col.r / 255.0); + lua_pushnumber(L, col.g / 255.0); + lua_pushnumber(L, col.b / 255.0); + lua_pushnumber(L, col.a / 255.0); lua_call(L, 4, 0); lua_pop(L, 2); } From 4d001728a0f3c01567a70f6b9842bd90cdb8b982 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Fri, 29 Jun 2018 15:32:06 -0400 Subject: [PATCH 13/52] Add proper build instructions to README.md --- README.md | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 42b6010..8597fc5 100644 --- a/README.md +++ b/README.md @@ -75,14 +75,53 @@ end ## Building -Windows and Linux binaries are available for each [release](https://github.com/keharriso/love-nuklear/releases). +Windows binaries are available for each [release](https://github.com/keharriso/love-nuklear/releases). To build the library yourself, grab the code with: ```sh $ git clone --recursive git@github.com:keharriso/love-nuklear.git ``` -Compile with CMake (I recommend using the MinGW generator on Windows). You'll need to tell CMake where to find the LuaJIT headers and binaries. The end result is a native Lua module. +Next, you need to compile the code to a native Lua module. + +### Compiling with CMake on Linux + +1. First, ensure you have the `cmake` and `luajit` packages installed. +2. Create a new folder next to `love-nuklear` called `love-nuklear-build`. +3. Open a terminal inside `love-nuklear-build`. +4. Compile with +```sh +$ cmake ../love-nuklear +$ make +``` +5. Locate `nuklear.so` in the build folder. + +### Compiling with CMake and MinGW on Windows + +1. Install [CMake](https://cmake.org/download/) and [MinGW](http://mingw.org/) or [MinGW-w64](https://mingw-w64.org/doku.php). +2. Download the source code for [LuaJIT](http://luajit.org/download.html). +3. Open a command window inside the LuaJIT folder (the one that contains "README"). +4. Compile LuaJIT with +```sh +$ mingw32-make +``` +5. Remember the path to `lua51.dll` inside the LuaJIT `src` folder. +6. Run the CMake GUI. +7. Click "Browse Source" at the top right, then select the `love-nuklear` folder. +8. Enter a path for the build folder. It should be separate from the source folder. +9. Press "Configure" at the bottom. +10. Select "MinGW Makefiles" from the generator drop list, then click "Finish". +11. You should receive an error. This is normal. +12. Open the LUA tree by clicking the triangle on the left. +13. Replace "LUA_INCLUDE_DIR-NOTFOUND" with the path to the LuaJIT `src` folder. +14. Replace "LUA_LIBRARY-NOTFOUND" with the path to `lua51.dll` inside the LuaJIT `src` folder. +15. Click "Generate" at the bottom. +16. Open a command window inside the build folder. +17. Compile with +```sh +$ mingw32-make +``` +18. Locate `nuklear.dll` inside the build folder. ## Documentation From d4f4980ae882cf56c64538e9ad1939bf1130ea81 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Thu, 13 Dec 2018 14:29:32 -0500 Subject: [PATCH 14/52] Incorporate changes from master --- src/nuklear_love.c | 128 +++++++++++++++++++++++---------------------- 1 file changed, 65 insertions(+), 63 deletions(-) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 5896e95..defa8fd 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -538,10 +538,10 @@ static void nk_love_configureGraphics(int line_thickness, struct nk_color col) lua_call(L, 1, 0); } lua_getfield(L, -1, "setColor"); - lua_pushnumber(L, col.r); - lua_pushnumber(L, col.g); - lua_pushnumber(L, col.b); - lua_pushnumber(L, col.a); + lua_pushnumber(L, col.r / 255.0); + lua_pushnumber(L, col.g / 255.0); + lua_pushnumber(L, col.b / 255.0); + lua_pushnumber(L, col.a / 255.0); lua_call(L, 4, 0); } @@ -555,10 +555,10 @@ static void nk_love_getGraphics(float *line_thickness, struct nk_color *color) lua_pop(L, 1); lua_getfield(L, -1, "getColor"); lua_call(L, 0, 4); - color->r = lua_tointeger(L, -4); - color->g = lua_tointeger(L, -3); - color->b = lua_tointeger(L, -2); - color->a = lua_tointeger(L, -1); + color->r = (int) (lua_tonumber(L, -4) * 255.0); + color->g = (int) (lua_tonumber(L, -3) * 255.0); + color->b = (int) (lua_tonumber(L, -2) * 255.0); + color->a = (int) (lua_tonumber(L, -1) * 255.0); lua_pop(L, 6); } @@ -722,10 +722,10 @@ static void nk_love_draw_text(int fontref, struct nk_color cbg, lua_getfield(L, -1, "graphics"); lua_getfield(L, -1, "setColor"); - lua_pushnumber(L, cbg.r); - lua_pushnumber(L, cbg.g); - lua_pushnumber(L, cbg.b); - lua_pushnumber(L, cbg.a); + lua_pushnumber(L, cbg.r / 255.0); + lua_pushnumber(L, cbg.g / 255.0); + lua_pushnumber(L, cbg.b / 255.0); + lua_pushnumber(L, cbg.a / 255.0); lua_call(L, 4, 0); lua_getfield(L, -1, "rectangle"); @@ -737,10 +737,10 @@ static void nk_love_draw_text(int fontref, struct nk_color cbg, lua_call(L, 5, 0); lua_getfield(L, -1, "setColor"); - lua_pushnumber(L, cfg.r); - lua_pushnumber(L, cfg.g); - lua_pushnumber(L, cfg.b); - lua_pushnumber(L, cfg.a); + lua_pushnumber(L, cfg.r / 255.0); + lua_pushnumber(L, cfg.g / 255.0); + lua_pushnumber(L, cfg.b / 255.0); + lua_pushnumber(L, cfg.a / 255.0); lua_call(L, 4, 0); lua_getfield(L, -1, "setFont"); @@ -773,57 +773,59 @@ static void interpolate_color(struct nk_color c1, struct nk_color c2, } static void nk_love_draw_rect_multi_color(int x, int y, unsigned int w, - unsigned int h, struct nk_color top_left, struct nk_color top_right, - struct nk_color bottom_left, struct nk_color bottom_right) + unsigned int h, struct nk_color left, struct nk_color top, + struct nk_color right, struct nk_color bottom) { lua_getglobal(L, "love"); lua_getfield(L, -1, "graphics"); - lua_getfield(L, -1, "setColor"); - lua_pushnumber(L, 255); - lua_pushnumber(L, 255); - lua_pushnumber(L, 255); - lua_call(L, 3, 0); - lua_pop(L, 2); - lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); - lua_getfield(L, -1, "gradientData"); - int row, col; - for (row = 0; row < NK_LOVE_GRADIENT_RESOLUTION; ++row) { - float row_ratio = (float) row / NK_LOVE_GRADIENT_RESOLUTION; - struct nk_color left, right; - interpolate_color(top_left, bottom_left, &left, row_ratio); - interpolate_color(top_right, bottom_right, &right, row_ratio); - for (col = 0; col < NK_LOVE_GRADIENT_RESOLUTION; ++col) { - float col_ratio = (float) col / NK_LOVE_GRADIENT_RESOLUTION; - struct nk_color pixel; - interpolate_color(left, right, &pixel, col_ratio); - lua_getfield(L, -1, "setPixel"); - lua_pushvalue(L, -2); - lua_pushnumber(L, col); - lua_pushnumber(L, row); - lua_pushnumber(L, pixel.r); - lua_pushnumber(L, pixel.g); - lua_pushnumber(L, pixel.b); - lua_pushnumber(L, pixel.a); - lua_call(L, 7, 0); + lua_getfield(L, -1, "push"); + lua_pushstring(L, "all"); + lua_call(L, 1, 0); + lua_getfield(L, -1, "setColor"); + lua_pushnumber(L, 1.0); + lua_pushnumber(L, 1.0); + lua_pushnumber(L, 1.0); + lua_call(L, 3, 0); + lua_getfield(L, -1, "setPointSize"); + lua_pushnumber(L, 1); + lua_call(L, 1, 0); + + struct nk_color X1, X2, Y; + float fraction_x, fraction_y; + int i,j; + + lua_getfield(L, -1, "points"); + lua_createtable(L, w * h, 0); + + for (j = 0; j < h; j++) { + fraction_y = ((float)j) / h; + for (i = 0; i < w; i++) { + fraction_x = ((float)i) / w; + interpolate_color(left, top, &X1, fraction_x); + interpolate_color(right, bottom, &X2, fraction_x); + interpolate_color(X1, X2, &Y, fraction_y); + lua_createtable(L, 6, 0); + lua_pushnumber(L, x + i); + lua_rawseti(L, -2, 1); + lua_pushnumber(L, y + j); + lua_rawseti(L, -2, 2); + lua_pushnumber(L, Y.r / 255.0); + lua_rawseti(L, -2, 3); + lua_pushnumber(L, Y.g / 255.0); + lua_rawseti(L, -2, 4); + lua_pushnumber(L, Y.b / 255.0); + lua_rawseti(L, -2, 5); + lua_pushnumber(L, Y.a / 255.0); + lua_rawseti(L, -2, 6); + lua_rawseti(L, -2, i + j * w + 1); } } - lua_pop(L, 1); - lua_getfield(L, -1, "gradient"); - lua_getfield(L, -1, "refresh"); - lua_pushvalue(L, -2); + lua_call(L, 1, 0); - lua_getglobal(L, "love"); - lua_getfield(L, -1, "graphics"); - lua_getfield(L, -1, "draw"); - lua_replace(L, -5); + lua_getfield(L, -1, "pop"); + lua_call(L, 0, 0); lua_pop(L, 2); - lua_pushnumber(L, x); - lua_pushnumber(L, y); - lua_pushnumber(L, 0); - lua_pushnumber(L, (float) w / NK_LOVE_GRADIENT_RESOLUTION); - lua_pushnumber(L, (float) h / NK_LOVE_GRADIENT_RESOLUTION); - lua_call(L, 6, 0); } static void nk_love_draw_image(int x, int y, unsigned int w, unsigned int h, @@ -869,7 +871,7 @@ static void nk_love_draw_arc(int cx, int cy, unsigned int r, lua_pop(L, 1); } -static void nk_love_clipbard_paste(nk_handle usr, struct nk_text_edit *edit) +static void nk_love_clipboard_paste(nk_handle usr, struct nk_text_edit *edit) { (void)usr; lua_getglobal(L, "love"); @@ -881,7 +883,7 @@ static void nk_love_clipbard_paste(nk_handle usr, struct nk_text_edit *edit) lua_pop(L, 3); } -static void nk_love_clipbard_copy(nk_handle usr, const char *text, int len) +static void nk_love_clipboard_copy(nk_handle usr, const char *text, int len) { (void)usr; char *str = 0; @@ -1061,8 +1063,8 @@ static int nk_love_new_ui(lua_State *L) context = current; nk_init_default(&ctx->nkctx, &ctx->fonts[0]); ctx->font_count = 1; - ctx->nkctx.clip.copy = nk_love_clipbard_copy; - ctx->nkctx.clip.paste = nk_love_clipbard_paste; + ctx->nkctx.clip.copy = nk_love_clipboard_copy; + ctx->nkctx.clip.paste = nk_love_clipboard_paste; ctx->nkctx.clip.userdata = nk_handle_ptr(0); ctx->layout_ratios = nk_love_malloc(sizeof(float) * NK_LOVE_MAX_RATIOS); ctx->layout_ratio_count = 0; From 569591e6eb730f412ac7f671ebfffe0f68d5097f Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Thu, 13 Dec 2018 16:03:52 -0500 Subject: [PATCH 15/52] Add button to transform example --- example/transform.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/example/transform.lua b/example/transform.lua index 9efcc5b..f26d3e2 100644 --- a/example/transform.lua +++ b/example/transform.lua @@ -6,9 +6,11 @@ return function(ui) ui:rotate(t / 8) ui:scale(1 + math.sin(t / 4) / 2, 1 + math.cos(t / 4) / 2) ui:shear(math.cos(t / 8) / 4, math.sin(t / 8) / 4) - if ui:windowBegin('Transform', 0, 0, 200, 200, 'border', 'title') then - ui:layoutRow('dynamic', 150, 1) + if ui:windowBegin('Transform', 0, 0, 200, 200, 'border', 'movable', 'title') then + ui:layoutRow('dynamic', 100, 1) ui:label('You can apply transformations to the UI using ui:rotate, ui:scale, ui:shear, and ui:translate.', 'wrap') + ui:layoutRow('dynamic', 30, 1) + ui:button('Try and catch me!') end ui:windowEnd() end From d7ed3d0fb7aaada5ded5c9a0a235918b532cab76 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Thu, 13 Dec 2018 17:15:03 -0500 Subject: [PATCH 16/52] Update nuklear to 181cfd8 --- src/nuklear | 2 +- src/nuklear_love.c | 50 ++++++++++++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/nuklear b/src/nuklear index ca1c9b3..181cfd8 160000 --- a/src/nuklear +++ b/src/nuklear @@ -1 +1 @@ -Subproject commit ca1c9b3275cf8e31b01fcd61c4d5116f88d4e24d +Subproject commit 181cfd86c47ae83eceabaf4e640587b844e613b6 diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 98eef40..288b020 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -253,6 +253,10 @@ static struct nk_color nk_love_checkcolor(int index) return color; } +static struct nk_colorf nk_love_checkcolorf(int index) { + return nk_color_cf(nk_love_checkcolor(index)); +} + static void nk_love_color(int r, int g, int b, int a, char *color_string) { r = NK_CLAMP(0, r, 255); @@ -1022,7 +1026,10 @@ static int nk_love_textinput_event(struct nk_context *ctx, const char *text) static int nk_love_wheelmoved_event(struct nk_context *ctx, int x, int y) { - nk_input_scroll(ctx,(float)y); + struct nk_vec2 scroll; + scroll.x = x; + scroll.y = y; + nk_input_scroll(ctx, scroll); return nk_window_is_any_hovered(ctx); } @@ -1798,36 +1805,39 @@ static int nk_love_item_is_any_active(lua_State *L) static int nk_love_window_set_bounds(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 5); + nk_love_assert_argc(lua_gettop(L) == 6); nk_love_assert_context(1); + const char *name = luaL_checkstring(L, 2); struct nk_rect bounds; - bounds.x = luaL_checknumber(L, 2); - bounds.y = luaL_checknumber(L, 3); - bounds.w = luaL_checknumber(L, 4); - bounds.h = luaL_checknumber(L, 5); - nk_window_set_bounds(&context->nkctx, bounds); + bounds.x = luaL_checknumber(L, 3); + bounds.y = luaL_checknumber(L, 4); + bounds.w = luaL_checknumber(L, 5); + bounds.h = luaL_checknumber(L, 6); + nk_window_set_bounds(&context->nkctx, name, bounds); return 0; } static int nk_love_window_set_position(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 3); + nk_love_assert_argc(lua_gettop(L) == 4); nk_love_assert_context(1); + const char *name = luaL_checkstring(L, 2); struct nk_vec2 pos; - pos.x = luaL_checknumber(L, 2); - pos.y = luaL_checknumber(L, 3); - nk_window_set_position(&context->nkctx, pos); + pos.x = luaL_checknumber(L, 3); + pos.y = luaL_checknumber(L, 4); + nk_window_set_position(&context->nkctx, name, pos); return 0; } static int nk_love_window_set_size(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 3); + nk_love_assert_argc(lua_gettop(L) == 4); nk_love_assert_context(1); + const char *name = luaL_checkstring(L, 2); struct nk_vec2 size; - size.x = luaL_checknumber(L, 2); - size.y = luaL_checknumber(L, 3); - nk_window_set_size(&context->nkctx, size); + size.x = luaL_checknumber(L, 3); + size.y = luaL_checknumber(L, 4); + nk_window_set_size(&context->nkctx, name, size); return 0; } @@ -2488,20 +2498,22 @@ static int nk_love_color_picker(lua_State *L) if (argc >= 3) format = nk_love_checkcolorformat(3); if (lua_isstring(L, 2)) { - struct nk_color color = nk_love_checkcolor(2); + struct nk_colorf color = nk_love_checkcolorf(2); color = nk_color_picker(&context->nkctx, color, format); char new_color_string[10]; - nk_love_color(color.r, color.g, color.b, color.a, new_color_string); + nk_love_color((int) (color.r * 255), (int) (color.g * 255), + (int) (color.b * 255), (int) (color.a * 255), new_color_string); lua_pushstring(L, new_color_string); } else if (lua_istable(L, 2)) { lua_getfield(L, 2, "value"); if (!nk_love_is_color(-1)) luaL_argerror(L, 2, "should have a color string value"); - struct nk_color color = nk_love_checkcolor(-1); + struct nk_colorf color = nk_love_checkcolorf(-1); int changed = nk_color_pick(&context->nkctx, &color, format); if (changed) { char new_color_string[10]; - nk_love_color(color.r, color.g, color.b, color.a, new_color_string); + nk_love_color((int) (color.r * 255), (int) (color.g * 255), + (int) (color.b * 255), (int) (color.a * 255), new_color_string); lua_pushstring(L, new_color_string); lua_setfield(L, 2, "value"); } From d73fde8fb1ad7bbeee1cb2c0a814bf2384e108a5 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Thu, 13 Dec 2018 17:49:02 -0500 Subject: [PATCH 17/52] Add section header comments --- src/nuklear_love.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 288b020..506c709 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -566,6 +566,14 @@ static void nk_love_getGraphics(float *line_thickness, struct nk_color *color) lua_pop(L, 6); } +/* + * =============================================================== + * + * GRAPHICS + * + * =============================================================== + */ + static void nk_love_scissor(int x, int y, int w, int h) { lua_getglobal(L, "love"); @@ -875,6 +883,14 @@ static void nk_love_draw_arc(int cx, int cy, unsigned int r, lua_pop(L, 1); } +/* + * =============================================================== + * + * INPUT + * + * =============================================================== + */ + static void nk_love_clipboard_paste(nk_handle usr, struct nk_text_edit *edit) { (void)usr; @@ -1483,6 +1499,14 @@ static int nk_love_frame_end(lua_State *L) return 0; } +/* + * =============================================================== + * + * TRANSFORM + * + * =============================================================== + */ + /* cos -sin 0 | cos sin 0 sin cos 0 | -sin cos 0 @@ -1660,6 +1684,14 @@ static int nk_love_translate(lua_State *L) return 0; } +/* + * =============================================================== + * + * WINDOW + * + * =============================================================== + */ + static int nk_love_window_begin(lua_State *L) { nk_love_assert_argc(lua_gettop(L) >= 1); @@ -1895,6 +1927,14 @@ static int nk_love_window_hide(lua_State *L) return 0; } +/* + * =============================================================== + * + * LAYOUT + * + * =============================================================== + */ + static int nk_love_layout_row(lua_State *L) { int argc = lua_gettop(L); @@ -2081,6 +2121,14 @@ static int nk_love_layout_ratio_from_pixel(lua_State *L) return 1; } +/* + * =============================================================== + * + * WIDGETS + * + * =============================================================== + */ + static int nk_love_group_begin(lua_State *L) { nk_love_assert_argc(lua_gettop(L) >= 2); @@ -2963,6 +3011,14 @@ static int nk_love_menu_end(lua_State *L) return 0; } +/* + * =============================================================== + * + * STYLE + * + * =============================================================== + */ + static int nk_love_style_default(lua_State *L) { nk_love_assert_argc(lua_gettop(L) == 1); @@ -3484,6 +3540,14 @@ static int nk_love_style_pop(lua_State *L) return 0; } +/* + * =============================================================== + * + * CUSTOM WIDGETS + * + * =============================================================== + */ + static int nk_love_widget_bounds(lua_State *L) { nk_love_assert_argc(lua_gettop(L) == 1); @@ -3839,6 +3903,14 @@ static int nk_love_input_is_hovered(lua_State *L) return 1; } +/* + * =============================================================== + * + * REGISTER + * + * =============================================================== + */ + #define NK_LOVE_REGISTER(name, func) \ lua_pushcfunction(L, func); \ lua_setfield(L, -2, name) From dfeed19486bd751bd1671b8fbb1a49fe4589f025 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Thu, 13 Dec 2018 18:36:55 -0500 Subject: [PATCH 18/52] Update README.md to use nuklear.newUI --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ed61d34..1a3648f 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ local nuklear = require 'nuklear' local ui function love.load() - ui = nuklear.init() + ui = nuklear.newUI() end local combo = {value = 1, items = {'A', 'B', 'C'}} From a3d532378b18352f21bc8e198c56b9dc5c1c4ec0 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Thu, 13 Dec 2018 18:36:55 -0500 Subject: [PATCH 19/52] Allow proper 1-pixel width lines (#22) --- src/nuklear_love.c | 48 +++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 506c709..7f7ced0 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -531,6 +531,14 @@ static void nk_love_transform(float *T, int *x, int *y) *y = (int) ry; } +/* + * =============================================================== + * + * GRAPHICS + * + * =============================================================== + */ + static void nk_love_configureGraphics(int line_thickness, struct nk_color col) { lua_getglobal(L, "love"); @@ -566,14 +574,6 @@ static void nk_love_getGraphics(float *line_thickness, struct nk_color *color) lua_pop(L, 6); } -/* - * =============================================================== - * - * GRAPHICS - * - * =============================================================== - */ - static void nk_love_scissor(int x, int y, int w, int h) { lua_getglobal(L, "love"); @@ -601,10 +601,10 @@ static void nk_love_draw_line(int x0, int y0, int x1, int y1, { nk_love_configureGraphics(line_thickness, col); lua_getfield(L, -1, "line"); - lua_pushnumber(L, x0); - lua_pushnumber(L, y0); - lua_pushnumber(L, x1); - lua_pushnumber(L, y1); + lua_pushnumber(L, x0 + 0.5); + lua_pushnumber(L, y0 + 0.5); + lua_pushnumber(L, x1 + 0.5); + lua_pushnumber(L, y1 + 0.5); lua_call(L, 4, 0); lua_pop(L, 1); } @@ -619,8 +619,8 @@ static void nk_love_draw_rect(int x, int y, unsigned int w, lua_pushstring(L, "line"); else lua_pushstring(L, "fill"); - lua_pushnumber(L, x); - lua_pushnumber(L, y); + lua_pushnumber(L, x + 0.5); + lua_pushnumber(L, y + 0.5); lua_pushnumber(L, w); lua_pushnumber(L, h); lua_pushnumber(L, r); @@ -638,12 +638,12 @@ static void nk_love_draw_triangle(int x0, int y0, int x1, int y1, lua_pushstring(L, "line"); else lua_pushstring(L, "fill"); - lua_pushnumber(L, x0); - lua_pushnumber(L, y0); - lua_pushnumber(L, x1); - lua_pushnumber(L, y1); - lua_pushnumber(L, x2); - lua_pushnumber(L, y2); + lua_pushnumber(L, x0 + 0.5); + lua_pushnumber(L, y0 + 0.5); + lua_pushnumber(L, x1 + 0.5); + lua_pushnumber(L, y1 + 0.5); + lua_pushnumber(L, x2 + 0.5); + lua_pushnumber(L, y2 + 0.5); lua_call(L, 7, 0); lua_pop(L, 1); } @@ -659,8 +659,8 @@ static void nk_love_draw_polygon(const struct nk_vec2i *pnts, int count, lua_pushstring(L, "fill"); int i; for (i = 0; (i < count) && (i < NK_LOVE_MAX_POINTS); ++i) { - lua_pushnumber(L, pnts[i].x); - lua_pushnumber(L, pnts[i].y); + lua_pushnumber(L, pnts[i].x + 0.5); + lua_pushnumber(L, pnts[i].y + 0.5); } lua_call(L, 1 + count * 2, 0); lua_pop(L, 1); @@ -673,8 +673,8 @@ static void nk_love_draw_polyline(const struct nk_vec2i *pnts, lua_getfield(L, -1, "line"); int i; for (i = 0; (i < count) && (i < NK_LOVE_MAX_POINTS); ++i) { - lua_pushnumber(L, pnts[i].x); - lua_pushnumber(L, pnts[i].y); + lua_pushnumber(L, pnts[i].x + 0.5); + lua_pushnumber(L, pnts[i].y + 0.5); } lua_call(L, count * 2, 0); lua_pop(L, 1); From 06114619e62e90cd02706ad6cc0f7a7c7f2d0a2b Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Thu, 13 Dec 2018 20:38:38 -0500 Subject: [PATCH 20/52] Remove unnecessary code from luaopen_nuklear --- src/nuklear_love.c | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 7f7ced0..b7c5da8 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -3928,23 +3928,9 @@ LUALIB_API int luaopen_nuklear(lua_State *luaState) lua_getglobal(L, "love"); nk_love_assert(lua_istable(L, -1), "LOVE-Nuklear requires LOVE environment"); - lua_getfield(L, -1, "image"); - lua_getfield(L, -1, "newImageData"); - lua_pushnumber(L, NK_LOVE_GRADIENT_RESOLUTION); - lua_pushnumber(L, NK_LOVE_GRADIENT_RESOLUTION); - lua_call(L, 2, 1); - lua_getfield(L, -3, "graphics"); - lua_getfield(L, -1, "newImage"); - lua_pushvalue(L, -3); - lua_call(L, 1, 1); - lua_setfield(L, -6, "gradient"); lua_pop(L, 1); - lua_setfield(L, -4, "gradientData"); - lua_pop(L, 2); lua_newtable(L); - lua_pushvalue(L, -1); - lua_setfield(L, -3, "methods"); NK_LOVE_REGISTER("keypressed", nk_love_keypressed); NK_LOVE_REGISTER("keyreleased", nk_love_keyreleased); From 8f7926039b960ea7580721a1ddc16c609db25320 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Fri, 14 Dec 2018 14:46:02 -0500 Subject: [PATCH 21/52] Add Quad support for images --- example/skin.lua | 28 ++++++++----- example/skin/button.png | Bin 2428 -> 4281 bytes example/skin/button_active.png | Bin 2368 -> 0 bytes example/skin/button_hover.png | Bin 2414 -> 0 bytes example/skin/checkbox.png | Bin 0 -> 2441 bytes example/skin/checkbox_false.png | Bin 1198 -> 0 bytes example/skin/checkbox_true.png | Bin 2970 -> 0 bytes src/nuklear_love.c | 69 ++++++++++++++++++++------------ 8 files changed, 61 insertions(+), 36 deletions(-) delete mode 100644 example/skin/button_active.png delete mode 100644 example/skin/button_hover.png create mode 100644 example/skin/checkbox.png delete mode 100644 example/skin/checkbox_false.png delete mode 100644 example/skin/checkbox_true.png diff --git a/example/skin.lua b/example/skin.lua index 968e0a8..77827b4 100644 --- a/example/skin.lua +++ b/example/skin.lua @@ -1,28 +1,34 @@ -- Basic skinning example. local windowHeader = love.graphics.newImage 'skin/window_header.png' -local checkboxSkin = love.graphics.newImage 'skin/checkbox_false.png' -local checkboxCheck = love.graphics.newImage 'skin/checkbox_true.png' +local windowBody = love.graphics.newImage 'skin/window.png' +local checkboxTexture = love.graphics.newImage 'skin/checkbox.png' +local checkboxOff = {checkboxTexture, love.graphics.newQuad(0, 0, 51, 55, 58, 115)} +local checkboxOn = {checkboxTexture, love.graphics.newQuad(0, 55, 58, 60, 58, 115)} +local buttonTexture = love.graphics.newImage 'skin/button.png' +local buttonNormal = {buttonTexture, love.graphics.newQuad(0, 0, 69, 52, 69, 156)} +local buttonActive = {buttonTexture, love.graphics.newQuad(0, 52, 69, 52, 69, 156)} +local buttonHover = {buttonTexture, love.graphics.newQuad(0, 104, 69, 52, 69, 156)} local style = { ['text'] = { ['color'] = '#000000' }, ['button'] = { - ['normal'] = love.graphics.newImage 'skin/button.png', - ['hover'] = love.graphics.newImage 'skin/button_hover.png', - ['active'] = love.graphics.newImage 'skin/button_active.png', + ['normal'] = buttonNormal, + ['hover'] = buttonHover, + ['active'] = buttonActive, ['text background'] = '#00000000', ['text normal'] = '#000000', ['text hover'] = '#000000', ['text active'] = '#ffffff' }, ['checkbox'] = { - ['normal'] = checkboxSkin, - ['hover'] = checkboxSkin, - ['active'] = checkboxSkin, - ['cursor normal'] = checkboxCheck, - ['cursor hover'] = checkboxCheck, + ['normal'] = checkboxOff, + ['hover'] = checkboxOff, + ['active'] = checkboxOff, + ['cursor normal'] = checkboxOn, + ['cursor hover'] = checkboxOn, ['text normal'] = '#000000', ['text hover'] = '#000000', ['text active'] = '#000000', @@ -38,7 +44,7 @@ local style = { ['label active'] = '#000000', ['label padding'] = {x = 10, y = 8} }, - ['fixed background'] = love.graphics.newImage 'skin/window.png', + ['fixed background'] = windowBody, ['background'] = '#d3ceaa' } } diff --git a/example/skin/button.png b/example/skin/button.png index 0c4783dfb72eb4546b06d6160a6b9ff093478ffc..d0381a10e428c2670342a6c5cb438dd32cfc7197 100644 GIT binary patch delta 4278 zcmV;n5J~U+61gFe7aW`j1^@s6DZFOR00006VoOIv0RI600RN!9r<0K(7=H(JNliru z;tUQG8YCb&i8ufN5JpKvK~#9!?VWpUTvdL@Kj+*#@3F^rUbabXqXLP#Z8j045JYOj zwluWLL)!{bK}uLDh%O+zg-B&f8za$OR-q*k?6zvyY!E4GDO&}yyM$d>JCMY%*=Z*Nzfot>(?yIUPSdQ@duZ?6950G)J*M;k&j4)Nw22p9cp<~h*01ZLYxVVXbhNTyeq+YpUVq~d zfB)3?X1M0c8@|N9eD`req3{(89_P+qW#PEx{^nFHsbSo2uj^|$Dor6bH$GW?_*}i>y^63g-P$r)A0`&LyX8>8hu8VEo>Oo3T5D36mZe7V= z^=!;Ic2&!QvYK){9v6ST;nZdz9R;*Z&dgB{E`VLY`VlgA{hf68&xZQZY zZd@)WQi}K@V1M5M9^d*MzQ41V8?OJYvRtSHgHzo8#}B4rOkZCgU0q!OPsqedH9g?4 zeq9&!_5Qp-q!a||{j|0$WZt|09*-*p1YrHzPM&Zq&pRm@s{>5`7r;{yi{OnXA|!SL{K>R@Zjf*HXzQ17oOAW0X>86HY` zv)kn)5b#qiRuYt^rU2DsMWJ{+E_7Y1LCJ*TrrNA16t~M+gOY94<~k(@+8UHBEA?Wq zs-PH#QGbJyZH2Cqs}{-^%~@15lyEp&b#bMn*=H(G8lk$OT)}L7*+Pj#V>$CHRlj%} z9}iYk$AyrW4y95?ix2{b!-LCRe|eVqb9uA~AFq40!O@GrINU0+=N8Z?>lzsW2n3l!lWTW{A(2u;NDV1V>yomq;!xV#+fy0+6Tce9=X2*>$&OD1@pxP~osLWPb0M^%vxJcG zvn>z^sioYaRCmZ_mSqqOPUVI&IyySu*w`58=;%m6nVgKUWXW}$|Lk*YJNGb~44Jgl zHGh_TX3EPHwrz0a7ayjMbar;e<7Olhm8VXfItL{8Q33eT{sXw(9$K0g;dXoC{+ZQG zQ!zdsnv*k;5t~9O#asPHQpc7rUk<>TGiT1pBS(%50c_Z?A@zb+e()xzPoKi+bkNeg zfO$;~cs*{U$Q-T{lOZCJ=#@Fw%EmOp1%G2hJ|LE5aQ4#+y!u*y>iDKjn*iwV?>{b9 zty;DH@ZrPV$dPNUMe(@oX{p}8-kqJyAjBT65%f-saE`MB{ zp!<<+?0u~d(~J_0Oc0AsVwq8tvQf&$whWBe6w$~;3drNV-_3|8$-10FhYr0zFfcHt z0oQ!=(MOTH@4kD*;>C+~nBHtYabk#{^&jEpRlkGR>*2GY6m4w_vCL?^Sq4hQU4QKCrx)mcG%>#n>0{mhv& z#|3Z`ux8t~ZOb2h^wIT6|H>`|yzSPN+}8EGTvcW@CwuqxXT%18x;h`X-MW&mbgiVW z&d2c+Lmc_#hZ!&+vk>t4=bwLL^XAR(0`CGe5U+XbJ$UfoV6uktnP;A<=zo&Yty{OI z>O~J8JUEzS05p@Rj@*$fi^stq`Iacpy2fwr$(C{Go>)TH|)R zT@Wwm*}Hcy{r&xnjg8H@bxA`*16^HRY}l}Yj*gDhBJH{7p4-1=%a*~km+n?d4W){U zg%DjpY6aKU*5-ccrI*%Re}Db;OOtRa#SR@h^!}DDTlxnE2GY;)yGp73kfq`fLfi!` zP5*$lwl?>}4?nzg)v8q;ixw?vxaOK`+UFXClP6COpFe+oY+ztu=-FqVJ$~xcsq9Sr zIi=J;r#NImaTBkWTv|nW>T?OViR6Tm24_uTs;+cPxlBg?^NFB@5Puzs7}HUKFouAB zrPNT)z890rC1$4O2{_Gj+)ajnL8a8Wg6T*hM3Jje_@8KmpNA$n9GoPWmcGo^5DFoBQ&18?C#QyRwZERPFKEFd zWx>w96CCHCE}WYI^6Lv)n6#|2n1Uk8V&{eP41^{#l1%9uLLo$94dESuMz*%LfuQKw zUhNO?7Yj0%G0l$hDol0@jfWSua%{9?9YhKLix&R7=LkLU-R*4^GDTP%eCmg+mva{`(Cl#HlhfICR~c;z^tQHf z=YO9dXj=>q4}a6&-=Ez3+LTj6ct@ZSzouc>Dld#WO{3Z2q*2##Cx9evO`ws-nitdJ zDBrq%pVYV`FfBSSsv*3^Ur!`Y3~@_Iv%^WfrWGu1Srcevk;6eb+b#a-!kwZT!fRa~ zqB%+x0=kA@R*rz-TKBYUr>KT-k;5?~megsL1tjT0xqqyr@-CMY)N9ovaVZH(gQinW zR$Lh-CAttbDA`b|&5HKeRb`{3Bq(|fO3g}wV%MO|G(*`nC^Mnd)TWm;XHl_GBDP(1 zaiyf$Cv4j_C^Mm0H7L2_O0QkNQ< zlxR}UkmIDT=?bjWXAQ+t5VWka$=PaBB7{H_5-H>VmSQDAN!Jjbj7IQEnRj<^(z5VK ziQ^J;Edo(gwJL;2o$d73OCc{wg=E?mp*-uaoPQd^Dcfd=zm5w=EbkIOEClrqXUS)# zoJ;|hWpi|DTJBd=L->=4Q9M$k+3Ch3H8de-Gz-C`H8-c|s9~T`91KpRjuq7q?i(B7 z^wbniAz0*a(xmBlgq#svCvA)96}m36l_F}IC08+|E^ti))3!Ko7`#3{nmV4YA(Xpz z?SDF+B$XUO0KI32h!_S6@M#+J9S*K?y7-N(=DI@DvlIf;wg|_fgkn*mrdcp}Dj;GQ zn2DA7H_x5UaGm5x*|lre@w^(s!Kn~Wo*iZ?8cm!Ln{vynj9M0te=^MLVL`;LJXcRLMSf4s~hJV4dcrq&u;mK%(cY@=r^!jlDhGh~fqvjTZ ze;v8NYvUulGWt2EjVKDmRk{PivM?+Q)3#D0=)7U@H)n@4V#y_G2v_^-xy4^!b|qW; zCq^@31At#@tgegyUcb~h84WWy72=&V7*Nv?&Q3#^48GJfguMw4sYPK;Lm0Q+z<*Hs zO1XTm?Yh1^zA3Z}N?lbnBqP zHH753kysc8uF0jX&SDXt;hc%zR$`fV@Q zEz)TTWG-)B{^b-sFOSTC{Cn5B`G4Vq3kXK9TzcxvsL2OsO@8`Om>+)-W;|@?OdT*A z4dL2l9-i9Jh~F)kjO6d!YsaSe#@?|RAiw?9Mn*#Ak>C!+Y_{$jWB-Sdj3m>nG=v*( z^zq_-^AUoAKmgV)^YYCN4H?JU=apAYzt1Uo`cInp>Wb+*K+Q@+xOSO`7k}@Y&)G4H zQp?_S4Sskm%%eYuXTo;gGmmv&_Mj9vb%9|iO5GJPEJd+f7KV*4?v_3OF~XZ4#YarG zhA{7a2v0oGiWGvVsS0G&!RHeAToR8%klgIs^InL}dp_qU-HTX#O?hS=#=|yCww_JJ zn4c>4LiIQh&e{?XAxxaA-9R zVTO5~HDy@Ncv6FsW!2_7B?sCXlq{=44O9urFter%n_;mUl*^m5s8}eWsH(cSQqt@b zjMg-SGoeIkcdX1ASE?ztl)QKw4ON&6X_rAmSTUnTfbeO^M#tq@=FjQTB0zXGM3V#I zDV$KBRc1MCC_*uXZ+|YO6Lb-W;1rZvmon>g`^BJzr%^|iGZ&{sI0U#PBF_(1_GQ!% znpS)xscdq#4uQ}_JaFm);VAKUnX}Xo9{VIlolE9j$&Q3Hq%zu=TDWyyrK=__P_XaV^c^dUY6y274s!Z*1eZgwc%IIJ zfQHX4$X3RUhwPj?iT^JhvlS|2qO93{zY3ZVvpF?l^5T2b>adbDgkRfv5qrW!DMh_U z(%hhP^+E@~*?*kbJXdIX7E+*M3N>nAj~a161!febRJ=ctcpTZhXC%XQO41O%{c(i* zcU?e@B)-tFQDyd@R(%q(xO3+P_8bnP!ZvEez#cPDVJkk8Y=w&1sGx}&F(4KPa{r#s zdHdt(YNlCf2#UF`>Z+}km{J|j4^-uDv^9I;(U6Tf3 zJKmypYWh7jVshusi|jiVP64?j4dL2l9@Z`MmR-r#i|+BsA&jOZZbp(8p8ht YBp>1-=npal+yDRo07*qoM6N<$f-DV7=>Px# literal 2428 zcmV-?34`{DP)6n1Qxa{u4I{|rJx{0x8p{AKw0^B2PpAO-<%|GfVo zMfcg+*%(Ac1Q~DvCI%t^Skd?IKe0buf?vp!ykE zV1|H#N={aSWP?C@L4ghrC2|bGn&KWkenF8zEbrgH{|Kb{S=qQ4p1=IWpr{~6vLQcy z{P+nDtRqK`T(Gyd2Zb6*7^DZJ7i0!N0KpP22p{-Q8aM#c3nudO^GS9I2!r&&3W)!g zFC7C%I}88|1%@-nmjL|?$svD$g~s2%e<9%j%{3s<+Bp}J_T=T||Ie6G!T>5|$cc7c zqc8@hhYuf;Z2(E76nzc(i4Vc>dxw!`t`Y8Kk8o zi8koxkB{h)FNTs+*ox(!&8vk=ZNz*3BzgAZJyfeKG(Im6KKAHpITL-jQfQ!@}M z@frXE2ss17YYM(u2k7!Dsf4=zL|Oqc*E#X&_3&>{99ha5k=km23C zk8lnAd^`;N{5){}#?1#A{C({is0l!20nkAVKnER!r#cXTl~9z{E2KIGR!d?O3sRDz z3jh;0DD%3OHU1D9fugQ$^^K}9h$Gb;l#3p<9~zrVjx z9W!UnoSo;-pLaJjG-Tl9`t`zW&A_E+)*t!NE>usrnB@Ffc+} z`N-h+uWt;GpF9P#f%Y?i+yTnd7Tw+56(uDl{;(_#D)?ffyr~i$8#W(c*tqEcnCQ3H0!&aG3yy(e&gALwgdUT&J_-!EUjF<9Cqptf0H zJ_Q9SJsbf7JUm>uYh^Jpp~2HU7u>p@7N=7z_1+1)`gdIZ5 z?gnVg5Y(ziZ(!qW*?;`_1*MNnE?{P616s;PaIgg~d*feC)H4W3iYWDBn z&j4}*BXqp0J|Q82Vd27sV9A!oYzAp5>a;r&Qz4^Zpc=ikF^l2pv)2rrJqy5M@$vBt z3l=N@PbDxwyATbqhBLj|tFQ(%w11Pp1ZuoO`!$VUzka=lrF%gW46+Lr5+FCgMiD`? zO%(%F&2#|W@B_WK2Z|;IIyvA0EH*#@AVB-p7t&M|#=krNbk)k4!A1|xhk;Upf(kJR z96=bp_##vv9^fDhp+6G3#!eK6nLb9iVUhF9?5EtcwEapR@v4^Q8PEBvgN;gNH z?|9GUdiS?f<~D!haJSw2u=Bg$`R@6C-*20KhytbJ{cD2s!*epOBN@@eV&&*(RTx?3 zJ6eGzW8i#nUNZD6Igm#RGH0jhMV8_iunem^QBCTzBVoPe7ejC1R%c%r;k7l#;6ZOY zS+Kv877}y5MeulL;El@-&etDdWyLR@V`)`{Ra_YoDR`!7WMl-4M&l+TZr->A^9xI~ z#T5^7u^(~uN+X;@{j z5C{ZgNShqw@jT?`^MLLIR=i`=q1}8B9zC=`P4&@TnN)D=bLz}BDRN}QSdkQgyeY&l zoK7db;BJ=*Dk}65odYO!6dzJ=QZG^!V;??CyB6##;!2S=k4!~pKtqO7xmUKJ(gkb- zD-+frp?y0lJK5P;kSU0(5}>k}NrWUs%Km~p$TY+yC6PyQ#*yOp|Jox(iDh9W;yPYwKY=Gff(GJX6Nvdy$%g-(eO{AD-tt1y8v44;a$DW zN2>|y$trS;77^455|0#}P6ufCL`;5}f`S4*VuD={h2ZNqA80kjkei#c<-5mmS#iUh zQ*BI)!xuBH)8qf&nDyW)Eea!&GCn>&Sy@?Gh8l|1N;OJ&`fLbpceK$8`TLR(u~J_C z5rl&Wa-pcGFy)p~FyMz!Cy z5^JkJ=`z4*V(zTy3G@$bAcRD>*2T+Kct7?jtU@*z4Ba@}3xt@e;o;$Cp6B;jEEXwZ z5ang1(A{N9sx>hd8qc-C#H5!tEYR$AvaS}4WJCfQG}_I*5*aK)M5ihx#N~3i@jbfk zP*kvp4tol_-JVw3ta6ymW=W*Qc{2{k(6aspIt-f`dp4V`s->l+o)=AiUauDh1_o$| z34)MvIYg~i)6~$=(7-%E^nV%-hhxxWGPz}!Qd66<*An*cnv#+dzO}WroGh_YjYgx^ z>-D-+BL$PE&*u}!((v~5^h``oPp>L2nIk;hmw1TO_aWrelf&^0*wMcu^B)YGe~e}r uBM6sP6>K!ev?)vg>Z@KdFmJ|mN&f{)#q^R}s7WUP00006jmr}+y}B51Xx%Y{sI}l{{Lh6`41=n1X1^I{s$?# z&(6riAk50bfB?)4$bgA~2mn^}Y?aM~)o1U~g{^3N?~2NDoLa$P9o0f+buKKJcG3Z~&$kOyuY1lk5@@2I&Lm4iIU_ z%g>N0Dhc;1uux#wrKSzfA)rFzF9U=P69a(>ubwiT{PLNBQC?pDe+M%M1ISh6M7#aX z%M45pA3h}80Fp{6`Wo`{|3AtNdGq@ZNgBDKMJhP?15+M@+}qC#FMt1H_yjB^q}Vu! zHt6fmp9~=V;B*4Y;-_?t8JHOf2N9?g;{q1)-+`Ie?&c*}Im6KKAHpITL-jQfQ!@}M z@frXE2ss17YYa8;%87H!Z2aN1V||kDq?^Ru?IP1o2oX$hd+p< z$IHyjz{iZJ6?c4m&)~r?$Usd1Dhr?vItWj7AONbEs8O$w>KIrpiIL{TSy>qd3Sc+} zRz433$BE~O+#j@7D9fBV zb9SCTf8O2D(2#+PnT0`?iwhW4zZt#*tEl%t1!63$4D3wgRe%3Lg&D{}SlXdK{`_Wm z`r`+f4YZ#DrL=e&fe@J1-?D+JaVf)ATV74c)jPd|B z{nxyI11v~C!Lu~TAwYLbfYnY8pbAD#POjto_wPUc(+OBwT7rsRP<`fsTphq#)e1l= zfsPqy0nq*jFb+Tf_N-tffhdYToylo13QEjGVQA4Rq89D_k#Ot>YMBsZJNNYg<0_&) zA#jt+7PgKw3bNWrFlCwAz0das=c!~fjv92~B8~(1%zJO{Ip-=8CFU+<)x!m;vKqF! zB7hbgq?QvM{u{lHHCSXsAcj*R<0e@c{G8y~DDk)F1PD?j0hIFm4cM9wyFMJXR)cJp zZ#u2x1~!aRB9WjMFIHB2GSTUBo4wc7AeoaTI*dR$A*SN@*|J|u`y@*dXqrY_izOGt zr^N+|Oa*M*@C1!-m<)dW{7BBAzu2TgYGKj30iQZ5fbc>_q(b3tGUP>YNoV}hNE%77 zqfQs8gyivvCqJ2x_y_WLfSAqZWB}RF&{eLC2$l#TaB-<7kcodC9~yAK6=G{c3HKn6MJn z#+L;s9#9+?`+$S5$a3FNlg+3X3WXy242=D|8zRHSC=`#gbQ3T2PjU`ak45Y7farVB z-{ALb<$>Ol&*uqZ@UeN}c?1d+ZwfIUP20leZyS_Ijn#lr&n&|d$ zo3s%DDD&OG_!gjj>xXF~jN*?=E5|=mHL)aU53vRu3>}r|z{W@%TrqKBfW(EFb`S?* zDghG{7iOorv50gj#;I%)(Ns*dDC!}UXraaT`+DDbT(5s(QE1DLd?9ze9(=#P_wK#l zd)@bs*wATwenV7yc*g54$S5Qh+t;kD!r0CKX$5*13;V&oESR-(V6_x57Z%~g65=FK zhFxoIBh9VSXM8X^kQ?vJjR31 z5CWo*(?E)_ka#>!P#q9FtfK)gHa&>uWXz^DmEPOy{$7LhGd;hLmqnnoD-1XeIA$TxI)Uxv?pN`n|T zxhqWS@aZW^LrF828?#=I;q^-XdOEW!ib6+HmX?;5dwYAkp`nCALZhUJ{*{8hiD7jxJ3F+?gh;s>z}TNKh?SN14H%1w#bV-&wNggO|46$e8Z$>V z$xo$ov*NHOIh|F=UGTB~GRP1laCM9-*BA=A)bp7c?g9 z1;XKQO=+|CH9kIWinQ3b~FO5!LVd+a&j^-GBPp%%fWFXk)TKIRhlVr!g}#GdDIi7Pnj~hT81Cmb`D*_x#JUljeaO(RoIRpIbY8_VGy zK~#9!V)+06KLZde{KpL#SQr=>6s|W&-~acYfkBj)iQ(@*28Qpy{xf|44HN(Zou+61 zL5l8kvN1A9@G&#MfFKtG3SeR&0)Q2L{rMleEDIw<5hEjGrLq_c1Ct0F3J#n7nt_oE z;si#J>wyXz@YwbOAb=R4L4XS=fDHkv{C}lRiebmq?+j69++eN9L7?6ER1vJ``(!zs zc5&o9Bqa!bVGjb3FTo)JB6y|PF% zbuf?vp!ykEV1|H#N?DYJWP?C@L4ghrC2|bGn&R%h{zH*LEbrgH{|Kb{8F`r*?)~`B zpd!OgvLQcy{P+nDtRqK`T(Gyd2Zb6*7^DZJ7i0!N0KpP22p{-Q8aM#c3nudO^GS9I z2!r&2a|ej@)8}Gn4-ka=6<8=Ryz7%?_yNr!zyJPc`12Pc2g@}eP_X(P!|rR}85rf| z<^OL_04Cl4 z=H-N#83qbqIOfkkhQZ?)Qj05^I))le2;vG|L<0}r^uVPSnKJkTN zF|d3BfmmP}6>Gu6FnRwchRv70!?QHVAwYLbfYnY8pbAD#POjto_wPUc(+OBwT7rsR zP<`fsTphq#)e1l=fsPqy0nmPU*aHL*dPNDs1wj0Ph5_;cY6ptF=nnV~Oi2I9X@`^2 zUI*C$1r6v9SwL<>h7t?~3t;I4ls4|ea?e#@1&Q9)1U2PhIiTE~pW?*v6Rgh_bn699C=|DFbxBb-u(gA;M8kMl&*WvFlGN|c#96~kefhX|Ay9uAYb!y zGSNK$e*FHAL89ylYMTY-Q&5o7!x12WY!8NwQUU|hcw%B0Bw*AbL&qVXe*7C84ngjW z4S3X0GO|H*o0^-MutR9s-C*Q}3{;~xuyIx#@4x*+=_8YEF$d5Lb|!*@EpUhIz4wzr zS(*(zh6Z*BsKd_BLeyvtxg*E0#x!im9^P61xm<&! zaszA>kpsxD7@%sV1L%ey=)FBqG%3)@0S{oY0RjL4+PA)trlL6fn=?0!wHewL*yh8? z9zuPILWuAw5egrBvzLLCBOmfj+V&VejG9or`OuR=7)4ltI0Nk^nj%y*Xi15d*izBW z+}8Qd?z!FF{c+_)=ltMscelIC&UerGo%8#C-%@^v3iZ!kgMC#>~=jI2RU)doct z#-{m>W}v&#&>!?mfk8C~%8>%*yga-pLYx57u=dkcbkbZ-$Bn7$=g`|V>KR%}BOLkR zv+K3A8cpolkU)%|ncJjS@7L-1%sS7g1VmVs;E`sAObR~f92*;>*4EY(B79el@{LM7 zkA5k|exR@2NY^eH3L^R!61Sd)>G8WY`5X(MBn0>&r-2kcLIQyRL3I>BUR=hu3enB% zY#>=btkbZTR0Txxj-rEut+X$1nCZM-wB=?RetNXUJ-B^EbWC|YJN~c5nqw`N zA|s@bGBq{zv8k!40UCCv~Og@)fluzH*nd!|V0#D{WTY zTrQU^(xTsj1J*2}zF}b}U4p&-{{H6f?(Q~>9Gr(jAsQYY=0hwT4i{YvF_}y}G<0-y z2oDhdpT@wzz({XzZ$NP=H?^s2EouL*v0ANqr_*_gO|g2j*=(}g?Y3eg1;KM+VIj<> zM#$&$eVCn{ji@*6jEDO&51HydjGQ)hI9CEY`ZuiqBWeB5Q4A9VVQMu~vN&cbVgg|1 g3$cc{IiKsoKQSNhy88hnl>h($07*qoM6N<$g80Q&!vFvP diff --git a/example/skin/checkbox.png b/example/skin/checkbox.png new file mode 100644 index 0000000000000000000000000000000000000000..fd04d56d47a090327de29a5002802734b4e3926d GIT binary patch literal 2441 zcmV;433m30P)92NhOL>!2=JVN<0wd0R^Ns zQd?0GKM;6nB~+;mMWqi=iwaRBh*AUs)Y8}@O+}rA35}@}H|az!NsLH5@iNO@d6*?L zo*B=bnLC~(K9c3@d;hz9|9}2-&c7TBmStIVhzNopl)EOk+Q*KKja7Zm@bEC*-Q6pe zDFTf~qeP?8f^(5bgjg(A^+!b_5pLhU-SEmedGaKC_wKa^8jVKTwrv}oDml<_I1Ip- z9{HN%FOg&yUa#WlGgURk*k68UyS%s>wQ1-YsZ^q=Eh?_oi*&8Iu;OaIteLRlYQL~q zumzxd_U!3cSg~1s#!1-W;o;T=&B7)Vaq5jhD@8?32L&(;on(9ot5a1sUrf^=v9y3` znw<>_U|AN)#1e+CcQ$AaHW^1%Q=JW(6-`Z%OeP%dw=D^pjgJ%YMGV7m9@LJRUR6~# zO_GTv)Ks!;>xM$1>Ot)|-lnLsrjl4%Kvh#LpZT%-%`$BwmSv%-DKs^OC`z>tyxU1k z)2OeHcCxI2b~I=$bE?f2yDqWqThy+5S?i-~pq&ERsPSM+8KYP%hNh)RCF5z}Hds-t z$rznKfBsHfsiiW5;3S5wV_9Zft8T?u*E9@F!vGCHKoms;QA9u}5qO)q(MA4o*;*DQ z5I__pL`keESZ`2JZ>0lj)0x3=HaIsiAY&RtNkS53BuRqu3)t;HddX^#rlp}}*|lOz zS6Tor0*yo>v?8=>7zl!ZEW459+S^6Ux411m=+5^BouImQfOTC(mff8n8D)X1S^dj_ zF5ijLu_54H1KP=B(Y6PzX2SjM8>Y2lz|auYZRLQ$3DqqM7|y=;PRoFVLZMu%b$55? zj>F+F;c&RB7L{vJgIZLqRP8Sh$n#;G7U6E$n0(h*U0D!>AKg{%Wes!&FZ0%Jg5=FDj4_0boE1Hfx}XRZp* z@DLD`fzBYE94$Ka(f{}p@E~xism7>WDp#WU%zjv?Db`>nP^H-UoY&VJy&@8{`bm1zpyo@n>u=_750l8W>$m*7`x1` zOSNVZXshO?^#|s7rUGQbwUIBs%A2KHv#H}zyXjgz-X(rA^cH?uTd~}~?tLnISk#(L zA9zOad+=)V>3J^EKjxO$M&8#$saKWAiF7K!*Gx~^69m03A| z4PN|v`gm|~kZcrGGw5a|&OmT}Re-m;XF1e=##Y$_Z=`>(U%#H--rg0<+5)|v@NszX zLc?JLE{*3Ne7~ac0eI!YRwCE#v}Pr-t-8OMzMt0~9_O*%nTCOm3`N+aRQ9izVgX+H zU~BI0U@%x6=t}F2|Gc?@KYy}?BSVpfzDE;8-#ay{;? z8Fu7>2mu@Z^5UG2=gx#P?-#39*^;1ka?`wuf+&hCs1iRoRrnvR_V>;(`tUdb*UA8o z_0BN7{-&*ddE|6I3#wFBWgC{8CI}+_K-w5h%m#UVYE!}WPY36D`JvNgVFNCWCkH2O z_sgkUzDx!CkzK`mbQ_wRCcE6Ze)6;Py)0;wZP<|^dn)_-)TUg&lwB?qpT92aUi0Y1 zALv351Qt|@k$3xxu9t${-E+fM^fNIV3h(hOVh3lj)DQlctrnglQE%^6vJMW%l>ZaB$#K z#b-Tna!1bixZEDRim&25b>*gkg|4flk_l4D1iG%3{TDp>zO|GUw(VC>Z)ajQZOtV~ z#vkZ%baYg4ebll{bWJS)%NmJn-;re(mSth+8pqCTGiyHW+S?$qN*v}9xtY8 zI-cF=Shb0wh(}2;4gTYLfS;c0u?3l5DT+UUD2nJ>y4$F!WW~%SySZtOZZ-FK6lB>2 zz_D|in7UPdO?2v(QUK}kdT}oc)=kq$=dmW@=$ckn;;!Rc&3&0b0^s}OTXL>qIpnwg z*q`U7JUN3_#!{f`cf3I@z>d%B3W9)=8DV^QQ{mV$70yD=XDUFJT_`@gBa&sn%>)H- zx!kxtX*C?Za1U=^4V8qHWC>qQZX^xd61iz!#g8baAOGz57T&%ZDuVO}LI@6K{Wb+O zUxnvYiSND@ZW?66R5urm$S%B!Qd9vxf6x)gmdZ`@czsCnooLG670ju0z0z{IX+D1% z&fgV8lA5zCZ&i}nMNyv}#*42YTSZ0lK@p z9c$VA53+Nunvbr@-C3QLCb5>A)={}>D=Ft%%GMfao67$I)tU;03wxh600000NkvXX Hu0mjfcgnO; literal 0 HcmV?d00001 diff --git a/example/skin/checkbox_false.png b/example/skin/checkbox_false.png deleted file mode 100644 index df85f65883a5b93a54b2e2a251e9da1996a37962..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1198 zcmV;f1X25mP)?q5XF_)sN>E{w zWI=W=$f+PFg8)DPft`!)GISRr1FV6L?m*;7Kye!(fUFT7Nu&Uf-@tw{v2rF#0|zHJ z!;!t4NV5qR#EhWGC)J4n0mO(eu@jvM@Ws)uUq2c4Z(ohuPuSuIZ>B-k_@7iwAn*;S z@yCzvq^F3#e|`gP{msC{#7vw`$l%|0#PZ?82b7`pjU1X%!7=}85aH=C|0_&+IA%PmQVKRa>maRl^>qRQK)DplDdn7$ zlhnRlAkq+(60zv@7Dd>ytT1C;K#+%rfqM0AWp~Xv{}=R*lqe}B-RQON3j#T7yMTlc zxOGqOlj3X~huMeDz!+MOt(#J&r0DuKIrB+7)Ve=Fxr9Uk-V4)O*)a2>QjvL&0>mH!D%Cg4 zIp?wjthEo3DxCT4{_y$5{51mBn_V!>MKI3QC;`CmU$z6P@xIOHk=$`P=LyY7-cmxa zn@KJWp|+`in&;#w06aGJv3yTb6!Q(NdlBYAiPY@;DJ{Y*DC5N4y{foZV)0DSS789P zY|*PkMFfP8`xG7tY%)XFrJNDp&BU0Qi$gXo1E`CL#1?~XuOj#pYdpjlA)=>Ch5Bi{ zq9SBt^Nio?=q=z1`4hYkIk^hlsi3NgcijYN*IIHDhM}N;Nd%|J9lB!6!P&CvA}7ct zP%Bj=;Bm0)7zaTy{-8d|N>))O&z?7LUZSb{d*d%<$Ly2SCHlSxs3Mot^$LTb$+m6P z)vNJCRFl$kZaUs-o6QIS4z*ZwRltO(CrYF=YXCL_ z&%ktDM?N;&i8+)baoxzFLWBRQM}S~9uBp?$Up%lFZEMedRdu@l?4!9&pV&+wOke%PbuJ+jF0rv0Vml9wFiy!JSq=N$G$M>%cAgnq^ zpTSoA1=w5gAcisc1%(+5SJ{EtAjL4(Hehw_|NjjD0R;9e$ruvV;vfhAw{iBv8b~ax ztPJezoG3m80mD9qFrXnXSs3tuKfiy2u`1suhWR!JA^s=T9a@;(?(5o#%#62j1 znOHevIQirIw+wu2e;F9KM8T;D2F@JY3yTE?oS6q5+uPg!pFe*-JVApnKmg%QlK21< zS-N@||1;dbf1fDlB9{RMQVgNsK*E(UK=}a~z!C;Lf8YXNzkI^bv`up>IPbtUf>I1F z%`ig;XfFv^)A3AB{{#Lq$EC20fa3& zI?gCCc*;CxSO87rs~z`3vi%JSaPq^5M^LT+0Ulm{u*oVw{~|lF_o5p11n}q2F9ui{ z!pX(WF!`J~Tmh&|VPV0{kxT~;9KaBq0W7Xynn2|ZEH}Z*8=M+Bxp+`CVFjR+Pbitm z%gg_-udn~VfB$}X4#8Q<;RB!&5Fmia&Q@gRss}*w0pouPZN(P8=)mEuBbfgCkKqeT z8G|Q0esSeYVv2EG06A~{`N!~;wTvMg=*J^09 zpn7E+`6V8b<)G5y+t<%veunZ{ubg~7HbvP3%o zIXfelJeSWNVSrk}h*FoKgHz9WfUPcmhU0#l8MXs0ZoOoH(=t%01uB4$!RO!13>GU) z7zBj)LHQ9gO<*&Km5m+fx_{t82~-<{488M-k0IfRJwe|EZ`EbE@s0~#;t2={5MPUd z%3qL1kj48R3&Kl8kk5Ene`8p9^F0?>?bAO}C`GJ+fdOuxA{TD3MlGn8=j9V*$l0&T zzzwVbPRCpV`x-qsH5ZyPFf%g)EfdAeqp&6|x~14MIH;iv16;s{$-n>H-~#nr+!Y31 zHi(?@A{~ZLznFoAEH8$`SyS&Ae}v9*vvCAbmLA{gKk5MtoxX2hPIfb|XtV=Y`j z#DxnNh|U^#s}3Ro#bqxkb`k7wP#Ylo4Ilt9RudXE6ojYQG}&s>)}{Vo1)-?)BBB;n zPkN{nLB)&ctsXrW6$(9j69w_?(c(|(MZ_(L2==gw-IIl)P^dq9P$SkPyN9iBX8W=) z$xC>OE}UfQ=9{2#ds z*Vy4r$4y-H2r``A{W(Oq9_3&35CN&I=XFXvWc`AP!}H-*c@r+P!>f%MG=W%Lg_d52 z2gja=?y1^E3YJH?VN!(4h#QRtoj*PGIClO0;1q~aN=ZedqzcbnB?TA-+agdX6llnl zO8U*)CY2#+xP81FQh;O`9Shjvfnu>Jmn&kU#pXR$8dD}lBa3sMD6W=9)SW)cl$EMO zLQI92&ssuTX67i=YF>Ne1ylqVXNMCNABQ*#zklSbtVMyqKWjK6FV zqXHChj)^lXQb*LglW&KZnCtIskkYqf0Ls-~dJsi5`XWR;c_c1zP++4N)4Pq%r#JYG z#RmoH9Broa$(gx0;dJvpl&nf4BWzE;=aQ-yVVHU9Ks?Pn)q8rviv+L#spn#Ij8xJ} zr9y4B=6k_ZpSIB z?{-0s)e&D3f15UuL*t8SzA)38=qfm^Tlvl5tK7-sOI%}lu{|B{`&YD#eLe*Ccp zbIW}&XxHKVvqO;6b2Pt^2u|>QH?d{5r3Sa#y>PvBfX%HXzPhIaOGEB7!q0BtB z9cgodrygArDS4UAQiE7Sblzx$kx(G*iba~WwaCOkMT64dKj5DLg zcGVIo#GgWtGJh&TMJS>OvXhX9qS`t~hv*;}1fGnbOOfOuJXE*DP!FZ0lvxo9sddnl z6+^>{(#FaItZ$223%6d-n{b#xvb&+P+{&WUeR9T&QJRW{2p(Z+^ z{E(^v>wpIM^xn|#Urf-witE(DJ`S?&ITxfMA^sX^j74E%naSaZkLW!wC#1h_0v(I`a)JRBW20%R8TH{!P~? zH66M|4+10yRpn2lc)!hQ8F8D196`NYIkTu|15obTzIPbyA^gM)MxCteF@nxJ?T&d{R3K`ESkT?s@@T2!t8PQGT|c zu@5md(ltK z(e-Q~OjZ7ixs{jd2MTD^>*Q(*F1^vFs1{euD~9;6 zBA$U~VS(2qF@%(9ERWe)#X>p)3sx3lwRi3-J)TVE?$vU1h_)s!h7d;3nqG*fvw0($ z_k@(}ezb!hi;%o0iCHp1^MAB5g=z_i)!S2kAcT<3ZeJ19!vS(55hA7VRi_M>Ct zwUwVVT(zZOQHAEEZ&AJ3xhSAHFEJ1Bu>u1`+y$Sgu52oZ;EDF_{7emh0Y(7-qvKbK zV=oeMQ^6>lWMUREEGk3YhbuP^r&G&F8eQ2p$z4handle = nk_handle_id(ref); - image->w = width; - image->h = height; - image->region[0] = 0; - image->region[1] = 0; - image->region[2] = width; - image->region[3] = height; - lua_pop(L, 3); + lua_pop(L, 2); } static int nk_love_is_hex(char c) @@ -847,20 +865,21 @@ static void nk_love_draw_image(int x, int y, unsigned int w, unsigned int h, lua_getfield(L, -1, "draw"); nk_love_pushregistry("image"); lua_rawgeti(L, -1, image.handle.id); + lua_rawgeti(L, -1, 1); + lua_replace(L, -3); + lua_rawgeti(L, -1, 2); lua_replace(L, -2); - lua_getfield(L, -3, "newQuad"); - lua_pushnumber(L, image.region[0]); - lua_pushnumber(L, image.region[1]); - lua_pushnumber(L, image.region[2]); - lua_pushnumber(L, image.region[3]); - lua_pushnumber(L, image.w); - lua_pushnumber(L, image.h); - lua_call(L, 6, 1); lua_pushnumber(L, x); lua_pushnumber(L, y); lua_pushnumber(L, 0); - lua_pushnumber(L, (double) w / image.region[2]); - lua_pushnumber(L, (double) h / image.region[3]); + lua_getfield(L, -4, "getViewport"); + lua_pushvalue(L, -5); + lua_call(L, 1, 4); + double viewportWidth = lua_tonumber(L, -2); + double viewportHeight = lua_tonumber(L, -1); + lua_pop(L, 4); + lua_pushnumber(L, (double) w / viewportWidth); + lua_pushnumber(L, (double) h / viewportHeight); lua_call(L, 7, 0); lua_pop(L, 1); } From 752eacec7cac0422a335cd6ed670421031fa741a Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Fri, 14 Dec 2018 17:32:56 -0500 Subject: [PATCH 22/52] Fix tooltip transparency --- src/nuklear_love.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index b1637ba..8f09e72 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -637,8 +637,8 @@ static void nk_love_draw_rect(int x, int y, unsigned int w, lua_pushstring(L, "line"); else lua_pushstring(L, "fill"); - lua_pushnumber(L, x + 0.5); - lua_pushnumber(L, y + 0.5); + lua_pushnumber(L, x); + lua_pushnumber(L, y); lua_pushnumber(L, w); lua_pushnumber(L, h); lua_pushnumber(L, r); From bb3ffeb957eaf92bec81f1d69dc315092f6e5a16 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Sat, 15 Dec 2018 13:36:43 -0500 Subject: [PATCH 23/52] Add template layout and allow manual edit focus/unfocus --- example/main.lua | 2 ++ example/template.lua | 19 ++++++++++++ src/nuklear_love.c | 69 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 example/template.lua diff --git a/example/main.lua b/example/main.lua index 6c60133..e9f021e 100644 --- a/example/main.lua +++ b/example/main.lua @@ -7,6 +7,7 @@ local draw = require 'draw' local overview = require 'overview' local style = require 'style' local skin = require 'skin' +local template = require 'template' local transform = require 'transform' local ui1, ui2 @@ -21,6 +22,7 @@ function love.update(dt) style(ui1) overview(ui1) draw(ui1) + template(ui1) skin(ui1) ui1:frameEnd() ui2:frameBegin() diff --git a/example/template.lua b/example/template.lua new file mode 100644 index 0000000..f876173 --- /dev/null +++ b/example/template.lua @@ -0,0 +1,19 @@ +-- Show off the template row layout + +return function(ui) + if ui:windowBegin('Template Layout', 200, 100, 300, 200, + 'title', 'border', 'movable', 'scalable') then + x, y, width, height = ui:windowGetContentRegion() + ui:layoutRow('dynamic', 40, 1) + ui:label('Scale me!'); + ui:layoutTemplateBegin(height - 40) + ui:layoutTemplatePush('static', 75) + ui:layoutTemplatePush('dynamic') + ui:layoutTemplatePush('variable', 75) + ui:layoutTemplateEnd() + ui:button(nil, '#ff0000') + ui:button(nil, '#00ff00') + ui:button(nil, '#0000ff') + end + ui:windowEnd() +end diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 8f09e72..98faa4b 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -1808,6 +1808,16 @@ static int nk_love_window_is_collapsed(lua_State *L) return 1; } +static int nk_love_window_is_closed(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + const char *name = luaL_checkstring(L, 2); + int is_closed = nk_window_is_closed(&context->nkctx, name); + lua_pushboolean(L, is_closed); + return 1; +} + static int nk_love_window_is_hidden(lua_State *L) { nk_love_assert_argc(lua_gettop(L) == 2); @@ -2027,6 +2037,43 @@ static int nk_love_layout_row_end(lua_State *L) return 0; } +static int nk_love_layout_template_begin(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + float height = luaL_checknumber(L, 2); + nk_layout_row_template_begin(&context->nkctx, height); + return 0; +} + +static int nk_love_layout_template_push(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) == 2 || lua_gettop(L) == 3); + nk_love_assert_context(1); + const char *mode = luaL_checkstring(L, 2); + if (lua_gettop(L) == 2) { + nk_love_assert(!strcmp(mode, "dynamic"), "%s: expecting 'dynamic' mode or width argument"); + nk_layout_row_template_push_dynamic(&context->nkctx); + } else { + float width = luaL_checknumber(L, 3); + if (!strcmp(mode, "variable")) { + nk_layout_row_template_push_variable(&context->nkctx, width); + } else if (!strcmp(mode, "static")) { + nk_layout_row_template_push_static(&context->nkctx, width); + } else { + return luaL_argerror(L, 2, "expecting 'dynamic', 'variable', or 'static' modes"); + } + } + return 0; +} + +static int nk_love_layout_template_end(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_layout_row_template_end(&context->nkctx); +} + static int nk_love_layout_space_begin(lua_State *L) { nk_love_assert_argc(lua_gettop(L) == 4); @@ -2658,6 +2705,22 @@ static int nk_love_edit(lua_State *L) return 2; } +int nk_love_edit_focus(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_edit_focus(&context->nkctx, NK_EDIT_DEFAULT); + return 0; +} + +int nk_love_edit_unfocus(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_edit_unfocus(&context->nkctx); + return 0; +} + static int nk_love_popup_begin(lua_State *L) { nk_love_assert_argc(lua_gettop(L) >= 7); @@ -3977,6 +4040,7 @@ LUALIB_API int luaopen_nuklear(lua_State *luaState) NK_LOVE_REGISTER("windowGetContentRegion", nk_love_window_get_content_region); NK_LOVE_REGISTER("windowHasFocus", nk_love_window_has_focus); NK_LOVE_REGISTER("windowIsCollapsed", nk_love_window_is_collapsed); + NK_LOVE_REGISTER("windowIsClosed", nk_love_window_is_closed); NK_LOVE_REGISTER("windowIsHidden", nk_love_window_is_hidden); NK_LOVE_REGISTER("windowIsActive", nk_love_window_is_active); NK_LOVE_REGISTER("windowIsHovered", nk_love_window_is_hovered); @@ -3996,6 +4060,9 @@ LUALIB_API int luaopen_nuklear(lua_State *luaState) NK_LOVE_REGISTER("layoutRowBegin", nk_love_layout_row_begin); NK_LOVE_REGISTER("layoutRowPush", nk_love_layout_row_push); NK_LOVE_REGISTER("layoutRowEnd", nk_love_layout_row_end); + NK_LOVE_REGISTER("layoutTemplateBegin", nk_love_layout_template_begin); + NK_LOVE_REGISTER("layoutTemplatePush", nk_love_layout_template_push); + NK_LOVE_REGISTER("layoutTemplateEnd", nk_love_layout_template_end); NK_LOVE_REGISTER("layoutSpaceBegin", nk_love_layout_space_begin); NK_LOVE_REGISTER("layoutSpacePush", nk_love_layout_space_push); NK_LOVE_REGISTER("layoutSpaceEnd", nk_love_layout_space_end); @@ -4026,6 +4093,8 @@ LUALIB_API int luaopen_nuklear(lua_State *luaState) NK_LOVE_REGISTER("colorPicker", nk_love_color_picker); NK_LOVE_REGISTER("property", nk_love_property); NK_LOVE_REGISTER("edit", nk_love_edit); + NK_LOVE_REGISTER("editFocus", nk_love_edit_focus); + NK_LOVE_REGISTER("editUnfocus", nk_love_edit_unfocus); NK_LOVE_REGISTER("popupBegin", nk_love_popup_begin); NK_LOVE_REGISTER("popupClose", nk_love_popup_close); NK_LOVE_REGISTER("popupEnd", nk_love_popup_end); From 976ff9f63b838f93669cbdd84ea2160e7efe2748 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Sun, 16 Dec 2018 17:15:17 -0500 Subject: [PATCH 24/52] Add closure alternatives to begin/end pairs --- example/closure.lua | 51 ++++++ example/main.lua | 4 +- src/nuklear_love.c | 410 ++++++++++++++++++++++++++++++++++++++------ 3 files changed, 413 insertions(+), 52 deletions(-) create mode 100644 example/closure.lua diff --git a/example/closure.lua b/example/closure.lua new file mode 100644 index 0000000..754f7e7 --- /dev/null +++ b/example/closure.lua @@ -0,0 +1,51 @@ +-- Show off the optional closure-oriented versions of basic functions + +local function menu(ui) + ui:layoutRow('dynamic', 30, 2) + ui:menu('Menu A', nil, 100, 200, function () + ui:layoutRow('dynamic', 30, 1) + if ui:menuItem('Item 1') then + print 'Closure: Item 1' + end + if ui:menuItem('Item 2') then + print 'Closure: Item 2' + end + end) + ui:menu('Menu B', nil, 100, 200, function () + ui:layoutRow('dynamic', 30, 1) + if ui:menuItem('Item 3') then + print 'Closure: Item 3' + end + if ui:menuItem('Item 4') then + print 'Closure: Item 4' + end + end) +end + +local comboText = 'Combo 1' + +function combo(ui) + ui:layoutRow('dynamic', 30, 1) + if ui:comboboxItem('Combo 1') then + print 'Closure: Combo 1' + comboText = 'Combo 1' + end + if ui:comboboxItem('Combo 2') then + print 'Closure: Combo 2' + comboText = 'Combo 2' + end + if ui:comboboxItem('Combo 3') then + print 'Closure: Combo 3' + comboText = 'Combo 3' + end +end + +local function window(ui) + ui:menubar(menu) + ui:layoutRow('dynamic', 30, 1) + ui:combobox(comboText, combo) +end + +return function (ui) + ui:window('Closure', 200, 200, 150, 120, {'title', 'movable', 'border'}, window) +end diff --git a/example/main.lua b/example/main.lua index e9f021e..049102f 100644 --- a/example/main.lua +++ b/example/main.lua @@ -3,6 +3,7 @@ local nuklear = require 'nuklear' local calculator = require 'calculator' +local closure = require 'closure' local draw = require 'draw' local overview = require 'overview' local style = require 'style' @@ -20,6 +21,7 @@ function love.update(dt) ui1:frameBegin() calculator(ui1) style(ui1) + closure(ui1) overview(ui1) draw(ui1) template(ui1) @@ -33,7 +35,7 @@ end function love.draw() ui1:draw() ui2:draw() - love.graphics.print("Current FPS: "..tostring(love.timer.getFPS( )), 10, 10) + love.graphics.print('Current FPS: '..tostring(love.timer.getFPS( )), 10, 10) end local function input(name, ...) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 98faa4b..c668bb8 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -290,12 +290,20 @@ static void nk_love_color(int r, int g, int b, int a, char *color_string) sprintf(color_string, format_string, r, g, b, a); } -static nk_flags nk_love_parse_window_flags(int flags_begin) +static nk_flags nk_love_parse_window_flags(int flags_begin, int flags_end) { - int argc = lua_gettop(L); - nk_flags flags = NK_WINDOW_NO_SCROLLBAR; int i; - for (i = flags_begin; i <= argc; ++i) { + if (flags_begin == flags_end && lua_istable(L, flags_begin)) { + size_t flagCount = lua_objlen(L, flags_begin); + nk_love_assert(lua_checkstack(L, flagCount), "%s: failed to allocate stack space"); + for (i = 1; i <= flagCount; ++i) { + lua_rawgeti(L, flags_begin, i); + } + lua_remove(L, flags_begin); + flags_end = flags_begin + flagCount - 1; + } + nk_flags flags = NK_WINDOW_NO_SCROLLBAR; + for (i = flags_begin; i <= flags_end; ++i) { const char *flag = luaL_checkstring(L, i); if (!strcmp(flag, "border")) flags |= NK_WINDOW_BORDER; @@ -1518,6 +1526,22 @@ static int nk_love_frame_end(lua_State *L) return 0; } +static int nk_love_frame(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) == 2); + if (!lua_isfunction(L, -1)) + luaL_typerror(L, lua_gettop(L), "function"); + lua_getfield(L, 1, "frameBegin"); + lua_pushvalue(L, 1); + lua_call(L, 1, 0); + lua_pushvalue(L, 1); + lua_call(L, 1, 0); + lua_getfield(L, 1, "frameEnd"); + lua_insert(L, 1); + lua_call(L, 1, 0); + return 0; +} + /* * =============================================================== * @@ -1727,7 +1751,7 @@ static int nk_love_window_begin(lua_State *L) title = luaL_checkstring(L, 3); bounds_begin = 4; } - nk_flags flags = nk_love_parse_window_flags(bounds_begin + 4); + nk_flags flags = nk_love_parse_window_flags(bounds_begin + 4, lua_gettop(L)); float x = luaL_checknumber(L, bounds_begin); float y = luaL_checknumber(L, bounds_begin + 1); float width = luaL_checknumber(L, bounds_begin + 2); @@ -1745,6 +1769,31 @@ static int nk_love_window_end(lua_State *L) return 0; } +static int nk_love_window(lua_State *L) +{ + nk_love_assert(lua_checkstack(L, 2), "%s: failed to allocate stack space"); + if (!lua_isfunction(L, -1)) + luaL_typerror(L, lua_gettop(L), "function"); + lua_insert(L, 2); + lua_pushvalue(L, 1); + lua_insert(L, 3); + lua_getfield(L, 1, "windowBegin"); + lua_insert(L, 3); + lua_call(L, lua_gettop(L) - 3, 1); + int open = lua_toboolean(L, -1); + lua_pop(L, 1); + if (open) { + lua_pushvalue(L, 1); + lua_call(L, 1, 0); + } else { + lua_pop(L, 1); + } + lua_getfield(L, -1, "windowEnd"); + lua_insert(L, 1); + lua_call(L, 1, 0); + return 0; +} + static int nk_love_window_get_bounds(lua_State *L) { nk_love_assert_argc(lua_gettop(L) == 1); @@ -1967,45 +2016,61 @@ static int nk_love_window_hide(lua_State *L) static int nk_love_layout_row(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 4 && argc <= 5); - nk_love_assert_context(1); - enum nk_layout_format format = nk_love_checkformat(2); - float height = luaL_checknumber(L, 3); - int use_ratios = 0; - if (format == NK_DYNAMIC) { - nk_love_assert_argc(argc == 4); - if (lua_isnumber(L, 4)) { - int cols = luaL_checkint(L, 4); - nk_layout_row_dynamic(&context->nkctx, height, cols); - } else { - if (!lua_istable(L, 4)) - luaL_argerror(L, 4, "should be a number or table"); - use_ratios = 1; + if (argc == 5 && lua_isfunction(L, 5)) { + nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); + lua_pushvalue(L, 1); + lua_insert(L, 2); + lua_pushvalue(L, 1); + lua_insert(L, 3); + lua_insert(L, 2); + lua_getfield(L, 1, "layoutRowBegin"); + lua_insert(L, 4); + lua_call(L, 4, 0); + lua_call(L, 1, 0); + lua_getfield(L, 1, "layoutRowEnd"); + lua_insert(L, 1); + lua_call(L, 1, 0); + } else { + nk_love_assert_argc(argc >= 4 && argc <= 5); + nk_love_assert_context(1); + enum nk_layout_format format = nk_love_checkformat(2); + float height = luaL_checknumber(L, 3); + int use_ratios = 0; + if (format == NK_DYNAMIC) { + nk_love_assert_argc(argc == 4); + if (lua_isnumber(L, 4)) { + int cols = luaL_checkint(L, 4); + nk_layout_row_dynamic(&context->nkctx, height, cols); + } else { + if (!lua_istable(L, 4)) + luaL_argerror(L, 4, "should be a number or table"); + use_ratios = 1; + } + } else if (format == NK_STATIC) { + if (argc == 5) { + int item_width = luaL_checkint(L, 4); + int cols = luaL_checkint(L, 5); + nk_layout_row_static(&context->nkctx, height, item_width, cols); + } else { + if (!lua_istable(L, 4)) + luaL_argerror(L, 4, "should be a number or table"); + use_ratios = 1; + } } - } else if (format == NK_STATIC) { - if (argc == 5) { - int item_width = luaL_checkint(L, 4); - int cols = luaL_checkint(L, 5); - nk_layout_row_static(&context->nkctx, height, item_width, cols); - } else { - if (!lua_istable(L, 4)) - luaL_argerror(L, 4, "should be a number or table"); - use_ratios = 1; + if (use_ratios) { + int cols = lua_objlen(L, -1); + int i, j; + for (i = 1, j = context->layout_ratio_count; i <= cols && j < NK_LOVE_MAX_RATIOS; ++i, ++j) { + lua_rawgeti(L, -1, i); + if (!lua_isnumber(L, -1)) + luaL_argerror(L, lua_gettop(L) - 1, "should contain numbers only"); + context->layout_ratios[j] = lua_tonumber(L, -1); + lua_pop(L, 1); + } + nk_layout_row(&context->nkctx, format, height, cols, context->layout_ratios + context->layout_ratio_count); + context->layout_ratio_count += cols; } } - if (use_ratios) { - int cols = lua_objlen(L, -1); - int i, j; - for (i = 1, j = context->layout_ratio_count; i <= cols && j < NK_LOVE_MAX_RATIOS; ++i, ++j) { - lua_rawgeti(L, -1, i); - if (!lua_isnumber(L, -1)) - luaL_argerror(L, lua_gettop(L) - 1, "should contain numbers only"); - context->layout_ratios[j] = lua_tonumber(L, -1); - lua_pop(L, 1); - } - nk_layout_row(&context->nkctx, format, height, cols, context->layout_ratios + context->layout_ratio_count); - context->layout_ratio_count += cols; - } return 0; } @@ -2051,10 +2116,11 @@ static int nk_love_layout_template_push(lua_State *L) nk_love_assert_argc(lua_gettop(L) == 2 || lua_gettop(L) == 3); nk_love_assert_context(1); const char *mode = luaL_checkstring(L, 2); - if (lua_gettop(L) == 2) { - nk_love_assert(!strcmp(mode, "dynamic"), "%s: expecting 'dynamic' mode or width argument"); + if (!strcmp(mode, "dynamic")) { + nk_love_assert_argc(lua_gettop(L) == 2); nk_layout_row_template_push_dynamic(&context->nkctx); } else { + nk_love_assert_argc(lua_gettop(L) == 3); float width = luaL_checknumber(L, 3); if (!strcmp(mode, "variable")) { nk_layout_row_template_push_variable(&context->nkctx, width); @@ -2074,6 +2140,27 @@ static int nk_love_layout_template_end(lua_State *L) nk_layout_row_template_end(&context->nkctx); } +static int nk_love_layout_template(lua_State *L) +{ + nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(lua_gettop(L) == 3); + if (!lua_isfunction(L, -1)) + luaL_typerror(L, lua_gettop(L), "function"); + lua_pushvalue(L, 1); + lua_insert(L, 2); + lua_pushvalue(L, 1); + lua_insert(L, 3); + lua_insert(L, 2); + lua_getfield(L, 1, "layoutTemplateBegin"); + lua_insert(L, 4); + lua_call(L, 2, 0); + lua_call(L, 1, 0); + lua_getfield(L, 1, "layoutTemplateEnd"); + lua_insert(L, 1); + lua_call(L, 1, 0); + return 0; +} + static int nk_love_layout_space_begin(lua_State *L) { nk_love_assert_argc(lua_gettop(L) == 4); @@ -2105,6 +2192,27 @@ static int nk_love_layout_space_end(lua_State *L) return 0; } +static int nk_love_layout_space(lua_State *L) +{ + nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(lua_gettop(L) == 5); + if (!lua_isfunction(L, -1)) + luaL_typerror(L, lua_gettop(L), "function"); + lua_pushvalue(L, 1); + lua_insert(L, 2); + lua_pushvalue(L, 1); + lua_insert(L, 3); + lua_insert(L, 2); + lua_getfield(L, 1, "layoutSpaceBegin"); + lua_insert(L, 4); + lua_call(L, 4, 0); + lua_call(L, 1, 0); + lua_getfield(L, 1, "layoutSpaceEnd"); + lua_insert(L, 1); + lua_call(L, 1, 0); + return 0; +} + static int nk_love_layout_space_bounds(lua_State *L) { nk_love_assert_argc(lua_gettop(L) == 1); @@ -2200,7 +2308,7 @@ static int nk_love_group_begin(lua_State *L) nk_love_assert_argc(lua_gettop(L) >= 2); nk_love_assert_context(1); const char *title = luaL_checkstring(L, 2); - nk_flags flags = nk_love_parse_window_flags(3); + nk_flags flags = nk_love_parse_window_flags(3, lua_gettop(L)); int open = nk_group_begin(&context->nkctx, title, flags); lua_pushboolean(L, open); return 1; @@ -2214,6 +2322,33 @@ static int nk_love_group_end(lua_State *L) return 0; } +static int nk_love_group(lua_State *L) +{ + nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(lua_gettop(L) >= 3); + if (!lua_isfunction(L, -1)) + luaL_typerror(L, lua_gettop(L), "function"); + lua_pushvalue(L, 1); + lua_insert(L, 2); + lua_pushvalue(L, 1); + lua_insert(L, 3); + lua_insert(L, 2); + lua_getfield(L, 1, "groupBegin"); + lua_insert(L, 4); + lua_call(L, lua_gettop(L) - 4, 1); + int open = lua_toboolean(L, -1); + lua_pop(L, 1); + if (open) { + lua_call(L, 1, 0); + lua_getfield(L, 1, "groupEnd"); + lua_insert(L, 1); + lua_call(L, 1, 0); + } else { + lua_pop(L, 3); + } + return 0; +} + static int nk_love_tree_push(lua_State *L) { int argc = lua_gettop(L); @@ -2251,6 +2386,31 @@ static int nk_love_tree_pop(lua_State *L) return 0; } +static int nk_love_tree(lua_State *L) +{ + nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(lua_gettop(L) >= 4); + if (!lua_isfunction(L, -1)) + luaL_typerror(L, lua_gettop(L), "function"); + lua_pushvalue(L, 1); + lua_insert(L, 2); + lua_pushvalue(L, 1); + lua_insert(L, 3); + lua_insert(L, 2); + lua_getfield(L, 1, "treePush"); + lua_insert(L, 4); + lua_call(L, lua_gettop(L) - 4, 1); + int open = lua_toboolean(L, -1); + lua_pop(L, 1); + if (open) { + lua_call(L, 1, 0); + lua_getfield(L, 1, "treePop"); + lua_insert(L, 1); + lua_call(L, 1, 0); + } + return 0; +} + static int nk_love_color_rgba(lua_State *L) { int argc = lua_gettop(L); @@ -2732,7 +2892,7 @@ static int nk_love_popup_begin(lua_State *L) bounds.y = luaL_checknumber(L, 5); bounds.w = luaL_checknumber(L, 6); bounds.h = luaL_checknumber(L, 7); - nk_flags flags = nk_love_parse_window_flags(8); + nk_flags flags = nk_love_parse_window_flags(8, lua_gettop(L)); int open = nk_popup_begin(&context->nkctx, type, title, flags, bounds); lua_pushboolean(L, open); return 1; @@ -2754,11 +2914,56 @@ static int nk_love_popup_end(lua_State *L) return 0; } +static int nk_love_popup(lua_State *L) +{ + nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(lua_gettop(L) >= 8); + if (!lua_isfunction(L, -1)) + luaL_typerror(L, lua_gettop(L), "function"); + lua_pushvalue(L, 1); + lua_insert(L, 2); + lua_pushvalue(L, 1); + lua_insert(L, 3); + lua_insert(L, 2); + lua_getfield(L, 1, "popupBegin"); + lua_insert(L, 4); + lua_call(L, lua_gettop(L) - 4, 1); + int open = lua_toboolean(L, -1); + lua_pop(L, 1); + if (open) { + lua_call(L, 1, 0); + lua_getfield(L, 1, "popupEnd"); + lua_insert(L, 1); + lua_call(L, 1, 0); + } + return 0; +} + static int nk_love_combobox(lua_State *L) { int argc = lua_gettop(L); nk_love_assert_argc(argc >= 3 && argc <= 6); nk_love_assert_context(1); + if (lua_isfunction(L, -1)) { + nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); + lua_pushvalue(L, 1); + lua_insert(L, 2); + lua_pushvalue(L, 1); + lua_insert(L, 3); + lua_insert(L, 2); + lua_getfield(L, 1, "comboboxBegin"); + lua_insert(L, 4); + lua_call(L, lua_gettop(L) - 4, 1); + int open = lua_toboolean(L, -1); + lua_pop(L, 1); + if (open) { + lua_call(L, 1, 0); + lua_getfield(L, 1, "comboboxEnd"); + lua_insert(L, 1); + lua_call(L, 1, 0); + } + return 0; + } if (!lua_istable(L, 3)) luaL_typerror(L, 3, "table"); int i; @@ -2918,7 +3123,7 @@ static int nk_love_contextual_begin(lua_State *L) trigger.y = luaL_checknumber(L, 5); trigger.w = luaL_checknumber(L, 6); trigger.h = luaL_checknumber(L, 7); - nk_flags flags = nk_love_parse_window_flags(8); + nk_flags flags = nk_love_parse_window_flags(8, lua_gettop(L)); int open = nk_contextual_begin(&context->nkctx, flags, size, trigger); lua_pushboolean(L, open); return 1; @@ -2971,12 +3176,28 @@ static int nk_love_contextual_end(lua_State *L) return 0; } -static int nk_love_tooltip(lua_State *L) +static int nk_love_contextual(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); - const char *text = luaL_checkstring(L, 2); - nk_tooltip(&context->nkctx, text); + nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(lua_gettop(L) >= 8); + if (!lua_isfunction(L, -1)) + luaL_typerror(L, lua_gettop(L), "function"); + lua_pushvalue(L, 1); + lua_insert(L, 2); + lua_pushvalue(L, 1); + lua_insert(L, 3); + lua_insert(L, 2); + lua_getfield(L, 1, "contextualBegin"); + lua_insert(L, 4); + lua_call(L, lua_gettop(L) - 4, 1); + int open = lua_toboolean(L, -1); + lua_pop(L, 1); + if (open) { + lua_call(L, 1, 0); + lua_getfield(L, 1, "contextualEnd"); + lua_insert(L, 1); + lua_call(L, 1, 0); + } return 0; } @@ -2998,6 +3219,37 @@ static int nk_love_tooltip_end(lua_State *L) return 0; } +static int nk_love_tooltip(lua_State *L) +{ + if (lua_gettop(L) == 3) { + nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); + if (!lua_isfunction(L, -1)) + luaL_typerror(L, lua_gettop(L), "function"); + lua_pushvalue(L, 1); + lua_insert(L, 2); + lua_pushvalue(L, 1); + lua_insert(L, 3); + lua_insert(L, 2); + lua_getfield(L, 1, "tooltipBegin"); + lua_insert(L, 4); + lua_call(L, 2, 1); + int open = lua_toboolean(L, -1); + lua_pop(L, 1); + if (open) { + lua_call(L, 1, 0); + lua_getfield(L, 1, "tooltipEnd"); + lua_insert(L, 1); + lua_call(L, 1, 0); + } + } else { + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + const char *text = luaL_checkstring(L, 2); + nk_tooltip(&context->nkctx, text); + } + return 0; +} + static int nk_love_menubar_begin(lua_State *L) { nk_love_assert_argc(lua_gettop(L) == 1); @@ -3014,6 +3266,27 @@ static int nk_love_menubar_end(lua_State *L) return 0; } +static int nk_love_menubar(lua_State *L) +{ + nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(lua_gettop(L) == 2); + if (!lua_isfunction(L, -1)) + luaL_typerror(L, lua_gettop(L), "function"); + lua_pushvalue(L, 1); + lua_insert(L, 2); + lua_pushvalue(L, 1); + lua_insert(L, 3); + lua_insert(L, 2); + lua_getfield(L, 1, "menubarBegin"); + lua_insert(L, 4); + lua_call(L, 1, 0); + lua_call(L, 1, 0); + lua_getfield(L, 1, "menubarEnd"); + lua_insert(L, 1); + lua_call(L, 1, 0); + return 0; +} + static int nk_love_menu_begin(lua_State *L) { int argc = lua_gettop(L); @@ -3093,6 +3366,31 @@ static int nk_love_menu_end(lua_State *L) return 0; } +static int nk_love_menu(lua_State *L) +{ + nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(lua_gettop(L) == 6 || lua_gettop(L) == 7); + if (!lua_isfunction(L, -1)) + luaL_typerror(L, lua_gettop(L), "function"); + lua_pushvalue(L, 1); + lua_insert(L, 2); + lua_pushvalue(L, 1); + lua_insert(L, 3); + lua_insert(L, 2); + lua_getfield(L, 1, "menuBegin"); + lua_insert(L, 4); + lua_call(L, lua_gettop(L) - 4, 1); + int open = lua_toboolean(L, -1); + lua_pop(L, 1); + if (open) { + lua_call(L, 1, 0); + lua_getfield(L, 1, "menuEnd"); + lua_insert(L, 1); + lua_call(L, 1, 0); + } + return 0; +} + /* * =============================================================== * @@ -4026,6 +4324,7 @@ LUALIB_API int luaopen_nuklear(lua_State *luaState) NK_LOVE_REGISTER("frameBegin", nk_love_frame_begin); NK_LOVE_REGISTER("frameEnd", nk_love_frame_end); + NK_LOVE_REGISTER("frame", nk_love_frame); NK_LOVE_REGISTER("rotate", nk_love_rotate); NK_LOVE_REGISTER("scale", nk_love_scale); @@ -4034,6 +4333,7 @@ LUALIB_API int luaopen_nuklear(lua_State *luaState) NK_LOVE_REGISTER("windowBegin", nk_love_window_begin); NK_LOVE_REGISTER("windowEnd", nk_love_window_end); + NK_LOVE_REGISTER("window", nk_love_window); NK_LOVE_REGISTER("windowGetBounds", nk_love_window_get_bounds); NK_LOVE_REGISTER("windowGetPosition", nk_love_window_get_position); NK_LOVE_REGISTER("windowGetSize", nk_love_window_get_size); @@ -4063,9 +4363,11 @@ LUALIB_API int luaopen_nuklear(lua_State *luaState) NK_LOVE_REGISTER("layoutTemplateBegin", nk_love_layout_template_begin); NK_LOVE_REGISTER("layoutTemplatePush", nk_love_layout_template_push); NK_LOVE_REGISTER("layoutTemplateEnd", nk_love_layout_template_end); + NK_LOVE_REGISTER("layoutTemplate", nk_love_layout_template); NK_LOVE_REGISTER("layoutSpaceBegin", nk_love_layout_space_begin); NK_LOVE_REGISTER("layoutSpacePush", nk_love_layout_space_push); NK_LOVE_REGISTER("layoutSpaceEnd", nk_love_layout_space_end); + NK_LOVE_REGISTER("layoutSpace", nk_love_layout_space); NK_LOVE_REGISTER("layoutSpaceBounds", nk_love_layout_space_bounds); NK_LOVE_REGISTER("layoutSpaceToScreen", nk_love_layout_space_to_screen); NK_LOVE_REGISTER("layoutSpaceToLocal", nk_love_layout_space_to_local); @@ -4075,9 +4377,11 @@ LUALIB_API int luaopen_nuklear(lua_State *luaState) NK_LOVE_REGISTER("groupBegin", nk_love_group_begin); NK_LOVE_REGISTER("groupEnd", nk_love_group_end); + NK_LOVE_REGISTER("group", nk_love_group); NK_LOVE_REGISTER("treePush", nk_love_tree_push); NK_LOVE_REGISTER("treePop", nk_love_tree_pop); + NK_LOVE_REGISTER("tree", nk_love_tree); NK_LOVE_REGISTER("label", nk_love_label); NK_LOVE_REGISTER("image", nk_love_image); @@ -4098,6 +4402,7 @@ LUALIB_API int luaopen_nuklear(lua_State *luaState) NK_LOVE_REGISTER("popupBegin", nk_love_popup_begin); NK_LOVE_REGISTER("popupClose", nk_love_popup_close); NK_LOVE_REGISTER("popupEnd", nk_love_popup_end); + NK_LOVE_REGISTER("popup", nk_love_popup); NK_LOVE_REGISTER("combobox", nk_love_combobox); NK_LOVE_REGISTER("comboboxBegin", nk_love_combobox_begin); NK_LOVE_REGISTER("comboboxItem", nk_love_combobox_item); @@ -4107,15 +4412,18 @@ LUALIB_API int luaopen_nuklear(lua_State *luaState) NK_LOVE_REGISTER("contextualItem", nk_love_contextual_item); NK_LOVE_REGISTER("contextualClose", nk_love_contextual_close); NK_LOVE_REGISTER("contextualEnd", nk_love_contextual_end); + NK_LOVE_REGISTER("contextual", nk_love_contextual); NK_LOVE_REGISTER("tooltip", nk_love_tooltip); NK_LOVE_REGISTER("tooltipBegin", nk_love_tooltip_begin); NK_LOVE_REGISTER("tooltipEnd", nk_love_tooltip_end); NK_LOVE_REGISTER("menubarBegin", nk_love_menubar_begin); NK_LOVE_REGISTER("menubarEnd", nk_love_menubar_end); + NK_LOVE_REGISTER("menubar", nk_love_menubar); NK_LOVE_REGISTER("menuBegin", nk_love_menu_begin); NK_LOVE_REGISTER("menuItem", nk_love_menu_item); NK_LOVE_REGISTER("menuClose", nk_love_menu_close); NK_LOVE_REGISTER("menuEnd", nk_love_menu_end); + NK_LOVE_REGISTER("menu", nk_love_menu); NK_LOVE_REGISTER("styleDefault", nk_love_style_default); NK_LOVE_REGISTER("styleLoadColors", nk_love_style_load_colors); From 42ca8ed3c0e479a372b0047ea0b95f182c7fabd9 Mon Sep 17 00:00:00 2001 From: Grump Date: Tue, 18 Dec 2018 15:08:30 +0100 Subject: [PATCH 25/52] added support for nk_tree_state --- src/nuklear_love.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index c668bb8..7bb2b47 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -2411,6 +2411,65 @@ static int nk_love_tree(lua_State *L) return 0; } +static int nk_love_tree_state_push(lua_State *L) +{ + int argc = lua_gettop(L); + nk_love_assert_argc(argc >= 3 && argc <= 5); + nk_love_assert_context(1); + enum nk_tree_type type = nk_love_checktree(2); + const char *title = luaL_checkstring(L, 3); + struct nk_image image; + int use_image = 0; + if (argc >= 3 && !lua_isnil(L, 4)) { + nk_love_checkImage(4, &image); + use_image = 1; + } + enum nk_collapse_states state = NK_MINIMIZED; + if (argc >= 5) + state = nk_love_checkstate(5); + + int open = 0; + if (use_image) + open = nk_tree_state_image_push(&context->nkctx, type, image, title, &state); + else + open = nk_tree_state_push(&context->nkctx, type, title, &state); + lua_pushboolean(L, open); + return 1; +} + +static int nk_love_tree_state_pop(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_tree_state_pop(&context->nkctx); + return 0; +} + +static int nk_love_tree_state(lua_State *L) +{ + nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(lua_gettop(L) >= 4); + if (!lua_isfunction(L, -1)) + luaL_typerror(L, lua_gettop(L), "function"); + lua_pushvalue(L, 1); + lua_insert(L, 2); + lua_pushvalue(L, 1); + lua_insert(L, 3); + lua_insert(L, 2); + lua_getfield(L, 1, "treeStatePush"); + lua_insert(L, 4); + lua_call(L, lua_gettop(L) - 4, 1); + int open = lua_toboolean(L, -1); + lua_pop(L, 1); + if (open) { + lua_call(L, 1, 0); + lua_getfield(L, 1, "treeStatePop"); + lua_insert(L, 1); + lua_call(L, 1, 0); + } + return 0; +} + static int nk_love_color_rgba(lua_State *L) { int argc = lua_gettop(L); @@ -4383,6 +4442,10 @@ LUALIB_API int luaopen_nuklear(lua_State *luaState) NK_LOVE_REGISTER("treePop", nk_love_tree_pop); NK_LOVE_REGISTER("tree", nk_love_tree); + NK_LOVE_REGISTER("treeStatePush", nk_love_tree_state_push); + NK_LOVE_REGISTER("treeStatePop", nk_love_tree_state_pop); + NK_LOVE_REGISTER("treeState", nk_love_tree_state); + NK_LOVE_REGISTER("label", nk_love_label); NK_LOVE_REGISTER("image", nk_love_image); NK_LOVE_REGISTER("button", nk_love_button); From a7991c524ef4d2f9918968c11d99fef5ce59d7ec Mon Sep 17 00:00:00 2001 From: Grump Date: Tue, 18 Dec 2018 11:52:17 +0100 Subject: [PATCH 26/52] added Canvas support --- src/nuklear_love.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index c668bb8..e9da170 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -184,7 +184,7 @@ static void nk_love_checkImage(int index, struct nk_image *image) { if (index < 0) index += lua_gettop(L) + 1; - if (nk_love_is_type(index, "Image")) { + if (nk_love_is_type(index, "Image") || nk_love_is_type(index, "Canvas")) { lua_getglobal(L, "love"); lua_getfield(L, -1, "graphics"); lua_getfield(L, -1, "newQuad"); @@ -206,14 +206,14 @@ static void nk_love_checkImage(int index, struct nk_image *image) lua_createtable(L, 2, 0); lua_rawgeti(L, index, 2); lua_rawgeti(L, index, 1); - if (nk_love_is_type(-1, "Image") && nk_love_is_type(-2, "Quad")) { + if ((nk_love_is_type(-1, "Image") || nk_love_is_type(-1, "Canvas")) && nk_love_is_type(-2, "Quad")) { lua_rawseti(L, -3, 1); lua_rawseti(L, -2, 2); } else { - luaL_argerror(L, index, "expecting {Image, Quad}"); + luaL_argerror(L, index, "expecting {Image, Quad} or {Canvas, Quad}"); } } else { - luaL_argerror(L, index, "expecting Image or {Image, Quad}"); + luaL_argerror(L, index, "expecting Image or Canvas or {Image, Quad} or {Canvas, Quad}"); } nk_love_pushregistry("image"); lua_pushvalue(L, -2); From 75da1af2b69d61e2b50e73d785e2302d01c3b9fa Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Tue, 18 Dec 2018 13:41:41 -0500 Subject: [PATCH 27/52] Fix `nk_love_tree_state_push` by making the image argument optional --- src/nuklear_love.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 7bb2b47..f8fca2a 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -2420,7 +2420,7 @@ static int nk_love_tree_state_push(lua_State *L) const char *title = luaL_checkstring(L, 3); struct nk_image image; int use_image = 0; - if (argc >= 3 && !lua_isnil(L, 4)) { + if (argc >= 4 && !lua_isnil(L, 4)) { nk_love_checkImage(4, &image); use_image = 1; } From 206f937e6e6f1aab373d9d8602333d1c94873c9f Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Tue, 18 Dec 2018 14:36:13 -0500 Subject: [PATCH 28/52] Add ui:style --- src/nuklear_love.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 5086641..775b99d 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -3979,6 +3979,27 @@ static int nk_love_style_pop(lua_State *L) return 0; } +static int nk_love_style(lua_State *L) +{ + nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(lua_gettop(L) == 3); + if (!lua_isfunction(L, -1)) + luaL_typerror(L, lua_gettop(L), "function"); + lua_pushvalue(L, 1); + lua_insert(L, 2); + lua_pushvalue(L, 1); + lua_insert(L, 3); + lua_insert(L, 2); + lua_getfield(L, 1, "stylePush"); + lua_insert(L, 4); + lua_call(L, 2, 0); + lua_call(L, 1, 0); + lua_getfield(L, 1, "stylePop"); + lua_insert(L, 1); + lua_call(L, 1, 0); + return 0; +} + /* * =============================================================== * @@ -4493,6 +4514,7 @@ LUALIB_API int luaopen_nuklear(lua_State *luaState) NK_LOVE_REGISTER("styleSetFont", nk_love_style_set_font); NK_LOVE_REGISTER("stylePush", nk_love_style_push); NK_LOVE_REGISTER("stylePop", nk_love_style_pop); + NK_LOVE_REGISTER("style", nk_love_style); NK_LOVE_REGISTER("widgetBounds", nk_love_widget_bounds); NK_LOVE_REGISTER("widgetPosition", nk_love_widget_position); From f652c1936fb0f5df20ae25c5abc363be9763b60a Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Wed, 19 Dec 2018 12:02:05 -0500 Subject: [PATCH 29/52] Fix build instructions for Linux --- README.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1a3648f..29a1aa0 100644 --- a/README.md +++ b/README.md @@ -91,12 +91,17 @@ Next, you need to compile the code to a native Lua module. 1. First, ensure you have the `cmake` and `luajit` packages installed. 2. Create a new folder next to `love-nuklear` called `love-nuklear-build`. 3. Open a terminal inside `love-nuklear-build`. -4. Compile with +4. Run CMake with: +```sh +$ cmake -DCMAKE_BUILD_TYPE=Release ../love-nuklear +$ ccmake . +``` +5. Press `c`, then `g` +6. Make the library with: ```sh -$ cmake ../love-nuklear $ make ``` -5. Locate `nuklear.so` in the build folder. +7. Locate `nuklear.so` in the build folder. ### Compiling with CMake and MinGW on Windows From 901c47fe2c12b9628b81bc4e543a4235a37ad5b4 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Wed, 19 Dec 2018 12:15:07 -0500 Subject: [PATCH 30/52] Simplify build instructions for Linux --- README.md | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/README.md b/README.md index 29a1aa0..b09f974 100644 --- a/README.md +++ b/README.md @@ -91,14 +91,9 @@ Next, you need to compile the code to a native Lua module. 1. First, ensure you have the `cmake` and `luajit` packages installed. 2. Create a new folder next to `love-nuklear` called `love-nuklear-build`. 3. Open a terminal inside `love-nuklear-build`. -4. Run CMake with: +4. Compile the library with: ```sh $ cmake -DCMAKE_BUILD_TYPE=Release ../love-nuklear -$ ccmake . -``` -5. Press `c`, then `g` -6. Make the library with: -```sh $ make ``` 7. Locate `nuklear.so` in the build folder. From 3254a429922a2fe5e0ca838dd87a35fb469ebe04 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Thu, 20 Dec 2018 15:16:44 -0500 Subject: [PATCH 31/52] Extremely minor README update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b09f974..b624583 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ Next, you need to compile the code to a native Lua module. 1. First, ensure you have the `cmake` and `luajit` packages installed. 2. Create a new folder next to `love-nuklear` called `love-nuklear-build`. 3. Open a terminal inside `love-nuklear-build`. -4. Compile the library with: +4. Compile the library with ```sh $ cmake -DCMAKE_BUILD_TYPE=Release ../love-nuklear $ make From 8d081e50e63bb56f024d51fb140e7f2b39d9adf4 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Wed, 26 Dec 2018 13:08:45 -0500 Subject: [PATCH 32/52] Add `presses` parameter to ui:mousepressed and ui:mousereleased --- README.md | 8 ++++---- example/main.lua | 8 ++++---- src/nuklear_love.c | 12 +++++++----- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index b624583..fe9758d 100644 --- a/README.md +++ b/README.md @@ -54,12 +54,12 @@ function love.keyreleased(key, scancode) ui:keyreleased(key, scancode) end -function love.mousepressed(x, y, button, istouch) - ui:mousepressed(x, y, button, istouch) +function love.mousepressed(x, y, button, istouch, presses) + ui:mousepressed(x, y, button, istouch, presses) end -function love.mousereleased(x, y, button, istouch) - ui:mousereleased(x, y, button, istouch) +function love.mousereleased(x, y, button, istouch, presses) + ui:mousereleased(x, y, button, istouch, presses) end function love.mousemoved(x, y, dx, dy, istouch) diff --git a/example/main.lua b/example/main.lua index 049102f..5fd2010 100644 --- a/example/main.lua +++ b/example/main.lua @@ -50,12 +50,12 @@ function love.keyreleased(key, scancode) input('keyreleased', key, scancode) end -function love.mousepressed(x, y, button, istouch) - input('mousepressed', x, y, button, istouch) +function love.mousepressed(x, y, button, istouch, presses) + input('mousepressed', x, y, button, istouch, presses) end -function love.mousereleased(x, y, button, istouch) - input('mousereleased', x, y, button, istouch) +function love.mousereleased(x, y, button, istouch, presses) + input('mousereleased', x, y, button, istouch, presses) end function love.mousemoved(x, y, dx, dy, istouch) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 775b99d..0991273 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -1035,7 +1035,7 @@ static int nk_love_keyevent(struct nk_context *ctx, const char *key, } static int nk_love_clickevent(struct nk_love_context *ctx, int x, int y, - int button, int istouch, int down) + int button, int istouch, int presses, int down) { nk_love_transform(ctx->Ti, &x, &y); struct nk_context *nkctx = &ctx->nkctx; @@ -1161,26 +1161,28 @@ static int nk_love_keyreleased(lua_State *L) static int nk_love_mousepressed(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 5); + nk_love_assert_argc(lua_gettop(L) == 5 || lua_gettop(L) == 6); struct nk_love_context *ctx = nk_love_checkcontext(1); int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); int button = luaL_checkint(L, 4); int istouch = nk_love_checkboolean(L, 5); - int consume = nk_love_clickevent(ctx, x, y, button, istouch, 1); + int presses = lua_gettop(L) == 6 ? luaL_checkint(L, 6) : 1; + int consume = nk_love_clickevent(ctx, x, y, button, istouch, presses, 1); lua_pushboolean(L, consume); return 1; } static int nk_love_mousereleased(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 5); + nk_love_assert_argc(lua_gettop(L) == 5 || lua_gettop(L) == 6); struct nk_love_context *ctx = nk_love_checkcontext(1); int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); int button = luaL_checkint(L, 4); int istouch = nk_love_checkboolean(L, 5); - int consume = nk_love_clickevent(ctx, x, y, button, istouch, 0); + int presses = lua_gettop(L) == 6 ? luaL_checkint(L, 6) : 1; + int consume = nk_love_clickevent(ctx, x, y, button, istouch, presses, 0); lua_pushboolean(L, consume); return 1; } From de05b2595c0c01c0cf628dd76b829deafba58334 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Thu, 3 Jan 2019 12:40:11 -0500 Subject: [PATCH 33/52] Allow ui:style* outside of frameBegin/frameEnd --- src/nuklear_love.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 775b99d..81d39c2 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -3910,6 +3910,9 @@ static int nk_love_style_push(lua_State *L) struct nk_love_context *ctx = nk_love_checkcontext(1); if (!lua_istable(L, 2)) luaL_typerror(L, 2, "table"); + int outOfContext = (context == NULL); + if (outOfContext) + context = ctx; lua_newtable(L); lua_insert(L, 2); NK_LOVE_STYLE_PUSH("font", font, &ctx->nkctx.style.font); @@ -3938,6 +3941,8 @@ static int nk_love_style_push(lua_State *L) size_t stack_size = lua_objlen(L, -1); lua_pushvalue(L, 2); lua_rawseti(L, -2, stack_size + 1); + if (outOfContext) + context = NULL; return 0; } @@ -3945,6 +3950,9 @@ static int nk_love_style_pop(lua_State *L) { nk_love_assert_argc(lua_gettop(L) == 1); struct nk_love_context *ctx = nk_love_checkcontext(1); + int outOfContext = (context == NULL); + if (outOfContext) + context = ctx; lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); lua_pushlightuserdata(L, ctx); lua_gettable(L, -2); @@ -3976,6 +3984,8 @@ static int nk_love_style_pop(lua_State *L) } lua_pop(L, 1); } + if (outOfContext) + context = NULL; return 0; } From 0d14e1c2cfe056e00453e6207047d0d41f9aac97 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Fri, 8 Feb 2019 12:54:19 -0500 Subject: [PATCH 34/52] Update draw example: use colors in [0,1] instead of [0,255] --- example/draw.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/draw.lua b/example/draw.lua index 3a5611b..5a93770 100644 --- a/example/draw.lua +++ b/example/draw.lua @@ -5,7 +5,7 @@ local img = love.graphics.newImage 'skin/button.png' return function (ui) if ui:windowBegin('Draw Example', 300, 300, 200, 200, 'title', 'movable', 'border') then local x, y, w, h = ui:windowGetBounds() - love.graphics.setColor(255, 0, 0) + love.graphics.setColor(1, 0, 0) ui:line(x + 10, y + 40, x + 50, y + 40, x + 50, y + 80) ui:curve(x + 50, y + 80, x + 80, y + 40, x + 100, y + 80, x + 80, y + 80) ui:polygon('line', x + 100, y + 150, x + 60, y + 140, x + 70, y + 70) @@ -13,7 +13,7 @@ return function (ui) ui:ellipse('fill', x + 30, y + 150, 20, 40) ui:arc('fill', x + 150, y + 80, 40, 3 * math.pi / 2, 2 * math.pi); ui:rectMultiColor(x + 95, y + 50, 50, 50, '#ff0000', '#00ff00', '#0000ff', '#000000') - love.graphics.setColor(255, 255, 255) + love.graphics.setColor(1, 1, 1) ui:image(img, x + 120, y + 120, 70, 50) ui:text('DRAW TEXT', x + 15, y + 75, 100, 100) end From d463002a970fd7a72cfad047a944861e3df6a4e9 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Fri, 5 Apr 2019 17:24:48 -0400 Subject: [PATCH 35/52] Fix build scripts and instructions for Linux builds --- README.md | 2 +- cmake/FindLuaJIT.cmake | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fe9758d..17b1061 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ Next, you need to compile the code to a native Lua module. ### Compiling with CMake on Linux -1. First, ensure you have the `cmake` and `luajit` packages installed. +1. First, ensure you have the `cmake` and `luajit` packages installed, as well as `libluajit-5.1-dev` if your distro has this package. 2. Create a new folder next to `love-nuklear` called `love-nuklear-build`. 3. Open a terminal inside `love-nuklear-build`. 4. Compile the library with diff --git a/cmake/FindLuaJIT.cmake b/cmake/FindLuaJIT.cmake index e8384e2..cf996e0 100644 --- a/cmake/FindLuaJIT.cmake +++ b/cmake/FindLuaJIT.cmake @@ -11,7 +11,7 @@ find_path(LUA_INCLUDE_DIR luajit.h HINTS ENV LUA_DIR - PATH_SUFFIXES include/luajit-2.0 include + PATH_SUFFIXES include/luajit-2.0 include/luajit-2.1 include PATHS ~/Library/Frameworks /Library/Frameworks From be9b57393a2d66eda49f4f2275b61d58369896b0 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Sat, 6 Apr 2019 15:35:57 -0400 Subject: [PATCH 36/52] Expand Linux build instructions to cover more distros --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 17b1061..95bbecd 100644 --- a/README.md +++ b/README.md @@ -81,14 +81,14 @@ Windows binaries are available for each [release](https://github.com/keharriso/l To build the library yourself, grab the code with: ```sh -$ git clone --recursive git@github.com:keharriso/love-nuklear.git +$ git clone --recursive https://github.com/keharriso/love-nuklear.git ``` Next, you need to compile the code to a native Lua module. ### Compiling with CMake on Linux -1. First, ensure you have the `cmake` and `luajit` packages installed, as well as `libluajit-5.1-dev` if your distro has this package. +1. First, ensure you have a C compiler and the `cmake` and `luajit` or `lua51-luajit` (for openSUSE) packages installed, as well as `libluajit-5.1-dev` (for Ubuntu/Debian), `luajit-devel` (for Fedora), or `lua51-luajit-devel` (for openSUSE) if your distro has one of these packages. 2. Create a new folder next to `love-nuklear` called `love-nuklear-build`. 3. Open a terminal inside `love-nuklear-build`. 4. Compile the library with @@ -96,7 +96,7 @@ Next, you need to compile the code to a native Lua module. $ cmake -DCMAKE_BUILD_TYPE=Release ../love-nuklear $ make ``` -7. Locate `nuklear.so` in the build folder. +5. Locate `nuklear.so` in the build folder. ### Compiling with CMake and MinGW on Windows From bf89bf9f088cfeba218bbcf494bd6d1cf0e72c29 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Sat, 6 Apr 2019 15:42:03 -0400 Subject: [PATCH 37/52] Update build script to support more distros --- cmake/FindLuaJIT.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/FindLuaJIT.cmake b/cmake/FindLuaJIT.cmake index cf996e0..762526b 100644 --- a/cmake/FindLuaJIT.cmake +++ b/cmake/FindLuaJIT.cmake @@ -11,7 +11,7 @@ find_path(LUA_INCLUDE_DIR luajit.h HINTS ENV LUA_DIR - PATH_SUFFIXES include/luajit-2.0 include/luajit-2.1 include + PATH_SUFFIXES include/luajit-2.0 include/luajit-2.1 include/luajit-5_1-2.1 include PATHS ~/Library/Frameworks /Library/Frameworks From 05dfdb5c34d534bfba415c4e1c4193dca150a9f4 Mon Sep 17 00:00:00 2001 From: jason <247739562@qq.com> Date: Thu, 17 Oct 2019 13:55:42 +0800 Subject: [PATCH 38/52] Scissor example Scissor button example --- example/overview.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/example/overview.lua b/example/overview.lua index 5909c77..7c4ae28 100644 --- a/example/overview.lua +++ b/example/overview.lua @@ -11,6 +11,7 @@ local colorPicker = {value = '#ff0000'} local property = {value = 6} local edit = {value = 'Edit text'} local comboA = {value = 1, items = {'A', 'B', 'C'}} +local scissorActive = false return function (ui) if ui:windowBegin('Overview', 100, 100, 600, 450, 'border', 'movable', 'title') then @@ -49,6 +50,9 @@ return function (ui) ui:spacing(1) ui:checkbox('Checkbox A', checkA) ui:checkbox('Checkbox B', checkB) + if ui:button('Scissor') then + scissorActive = not scissorActive + end ui:groupEnd() end if ui:groupBegin('Group 2', 'border') then @@ -104,4 +108,11 @@ return function (ui) end end ui:windowEnd() + if(scissorActive) then + love.graphics.setScissor() + love.graphics.clear() + love.graphics.setScissor(130, 130, 500, 400) + else + love.graphics.setScissor() + end end From b3af181896beddcdaa6b62c5d1066ae60d0943df Mon Sep 17 00:00:00 2001 From: jason <247739562@qq.com> Date: Thu, 17 Oct 2019 19:18:09 -0400 Subject: [PATCH 39/52] Keep parent scissor setting --- src/nuklear_love.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index aada11a..3741e8f 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -600,7 +600,8 @@ static void nk_love_getGraphics(float *line_thickness, struct nk_color *color) lua_pop(L, 6); } -static void nk_love_scissor(int x, int y, int w, int h) +static void nk_love_scissor(int x, int y, int w, int h, + int nested, int px, int py, int pw, int ph) { lua_getglobal(L, "love"); lua_getfield(L, -1, "graphics"); @@ -614,10 +615,16 @@ static void nk_love_scissor(int x, int y, int w, int h) int top = NK_MIN(NK_MIN(y1, y2), NK_MIN(y3, y4)); int right = NK_MAX(NK_MAX(x1, x2), NK_MAX(x3, x4)); int bottom = NK_MAX(NK_MAX(y1, y2), NK_MAX(y3, y4)); + if (nested) { + left = NK_MAX(left, px); + top = NK_MAX(top, py); + right = NK_MIN(right, px + pw); + bottom = NK_MIN(bottom, py + ph); + } lua_pushnumber(L, left); lua_pushnumber(L, top); - lua_pushnumber(L, right - left); - lua_pushnumber(L, bottom - top); + lua_pushnumber(L, NK_MAX(0, right - left)); + lua_pushnumber(L, NK_MAX(0, bottom - top)); lua_call(L, 4, 0); lua_pop(L, 2); } @@ -1237,6 +1244,19 @@ static int nk_love_draw(lua_State *L) lua_getfield(L, -1, "origin"); lua_call(L, 0, 0); + int nest_scissor = 0; + int px = 0, py = 0, pw = 0, ph = 0; + lua_getfield(L, -1, "getScissor"); + lua_call(L, 0, 4); + if (lua_isnumber(L, -4)) { + nest_scissor = 1; + px = lua_tonumber(L, -4); + py = lua_tonumber(L, -3); + pw = lua_tonumber(L, -2); + ph = lua_tonumber(L, -1); + } + lua_pop(L, 4); + nk_love_pushregistry("transform"); size_t transform_count = lua_objlen(L, -1); size_t i, j; @@ -1259,7 +1279,7 @@ static int nk_love_draw(lua_State *L) case NK_COMMAND_NOP: break; case NK_COMMAND_SCISSOR: { const struct nk_command_scissor *s =(const struct nk_command_scissor*)cmd; - nk_love_scissor(s->x, s->y, s->w, s->h); + nk_love_scissor(s->x, s->y, s->w, s->h, nest_scissor, px, py, pw, ph); } break; case NK_COMMAND_LINE: { const struct nk_command_line *l = (const struct nk_command_line *)cmd; From 0cca218ac72f71c39130277dac1a6d1c9aceef69 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Thu, 17 Oct 2019 19:40:43 -0400 Subject: [PATCH 40/52] Add support for *GetScroll and *SetScroll for windows, groups, and popups --- src/nuklear | 2 +- src/nuklear_love.c | 78 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/src/nuklear b/src/nuklear index 181cfd8..adc52d7 160000 --- a/src/nuklear +++ b/src/nuklear @@ -1 +1 @@ -Subproject commit 181cfd86c47ae83eceabaf4e640587b844e613b6 +Subproject commit adc52d710fe3c87194b99f540c53e82eb75c2521 diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 3741e8f..a4488da 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -39,7 +39,6 @@ #define NK_LOVE_COMBOBOX_MAX_ITEMS 1024 #define NK_LOVE_MAX_FONTS 1024 #define NK_LOVE_MAX_RATIOS 1024 -#define NK_LOVE_GRADIENT_RESOLUTION 32 static lua_State *L; static char *edit_buffer; @@ -1848,6 +1847,17 @@ static int nk_love_window_get_size(lua_State *L) return 2; } +static int nk_love_window_get_scroll(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_uint offset_x, offset_y; + nk_window_get_scroll(&context->nkctx, &offset_x, &offset_y); + lua_pushinteger(L, offset_x); + lua_pushinteger(L, offset_y); + return 2; +} + static int nk_love_window_get_content_region(lua_State *L) { nk_love_assert_argc(lua_gettop(L) == 1); @@ -1982,6 +1992,17 @@ static int nk_love_window_set_focus(lua_State *L) return 0; } +static int nk_love_window_set_scroll(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) == 3); + nk_love_assert_context(1); + nk_uint offset_x, offset_y; + offset_x = luaL_checkinteger(L, 2); + offset_y = luaL_checkinteger(L, 3); + nk_window_set_scroll(&context->nkctx, offset_x, offset_y); + return 0; +} + static int nk_love_window_close(lua_State *L) { nk_love_assert_argc(lua_gettop(L) == 2); @@ -2371,6 +2392,29 @@ static int nk_love_group(lua_State *L) return 0; } +static int nk_love_group_get_scroll(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_context(1); + const char *id = luaL_checkstring(L, 2); + nk_uint x_offset, y_offset; + nk_group_get_scroll(&context->nkctx, id, &x_offset, &y_offset); + lua_pushinteger(L, x_offset); + lua_pushinteger(L, y_offset); + return 2; +} + +static int nk_love_group_set_scroll(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) == 4); + nk_love_assert_context(1); + const char *id = luaL_checkstring(L, 2); + nk_uint x_offset = luaL_checkint(L, 3); + nk_uint y_offset = luaL_checkint(L, 4); + nk_group_set_scroll(&context->nkctx, id, x_offset, y_offset); + return 0; +} + static int nk_love_tree_push(lua_State *L) { int argc = lua_gettop(L); @@ -3020,6 +3064,28 @@ static int nk_love_popup(lua_State *L) return 0; } +static int nk_love_popup_get_scroll(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) == 1); + nk_love_assert_context(1); + nk_uint offset_x, offset_y; + nk_popup_get_scroll(&context->nkctx, &offset_x, &offset_y); + lua_pushinteger(L, offset_x); + lua_pushinteger(L, offset_y); + return 2; +} + +static int nk_love_popup_set_scroll(lua_State *L) +{ + nk_love_assert_argc(lua_gettop(L) == 3); + nk_love_assert_context(1); + nk_uint offset_x, offset_y; + offset_x = luaL_checkinteger(L, 2); + offset_y = luaL_checkinteger(L, 3); + nk_popup_set_scroll(&context->nkctx, offset_x, offset_y); + return 0; +} + static int nk_love_combobox(lua_State *L) { int argc = lua_gettop(L); @@ -3067,14 +3133,14 @@ static int nk_love_combobox(lua_State *L) if (argc >= 6 && !lua_isnil(L, 6)) size.y = luaL_checknumber(L, 6); if (lua_isnumber(L, 2)) { - int value = lua_tointeger(L, 2) - 1; + int value = luaL_checkinteger(L, 2) - 1; value = nk_combo(&context->nkctx, combobox_items, i, value, item_height, size); lua_pushnumber(L, value + 1); } else if (lua_istable(L, 2)) { lua_getfield(L, 2, "value"); if (!lua_isnumber(L, -1)) luaL_argerror(L, 2, "should have a number value"); - int value = lua_tointeger(L, -1) - 1; + int value = luaL_checkinteger(L, -1) - 1; int old = value; nk_combobox(&context->nkctx, combobox_items, i, &value, item_height, size); int changed = value != old; @@ -4449,6 +4515,7 @@ LUALIB_API int luaopen_nuklear(lua_State *luaState) NK_LOVE_REGISTER("windowGetBounds", nk_love_window_get_bounds); NK_LOVE_REGISTER("windowGetPosition", nk_love_window_get_position); NK_LOVE_REGISTER("windowGetSize", nk_love_window_get_size); + NK_LOVE_REGISTER("windowGetScroll", nk_love_window_get_scroll); NK_LOVE_REGISTER("windowGetContentRegion", nk_love_window_get_content_region); NK_LOVE_REGISTER("windowHasFocus", nk_love_window_has_focus); NK_LOVE_REGISTER("windowIsCollapsed", nk_love_window_is_collapsed); @@ -4462,6 +4529,7 @@ LUALIB_API int luaopen_nuklear(lua_State *luaState) NK_LOVE_REGISTER("windowSetPosition", nk_love_window_set_position); NK_LOVE_REGISTER("windowSetSize", nk_love_window_set_size); NK_LOVE_REGISTER("windowSetFocus", nk_love_window_set_focus); + NK_LOVE_REGISTER("windowSetScroll", nk_love_window_set_scroll); NK_LOVE_REGISTER("windowClose", nk_love_window_close); NK_LOVE_REGISTER("windowCollapse", nk_love_window_collapse); NK_LOVE_REGISTER("windowExpand", nk_love_window_expand); @@ -4490,6 +4558,8 @@ LUALIB_API int luaopen_nuklear(lua_State *luaState) NK_LOVE_REGISTER("groupBegin", nk_love_group_begin); NK_LOVE_REGISTER("groupEnd", nk_love_group_end); NK_LOVE_REGISTER("group", nk_love_group); + NK_LOVE_REGISTER("groupGetScroll", nk_love_group_get_scroll); + NK_LOVE_REGISTER("groupSetScroll", nk_love_group_set_scroll); NK_LOVE_REGISTER("treePush", nk_love_tree_push); NK_LOVE_REGISTER("treePop", nk_love_tree_pop); @@ -4519,6 +4589,8 @@ LUALIB_API int luaopen_nuklear(lua_State *luaState) NK_LOVE_REGISTER("popupClose", nk_love_popup_close); NK_LOVE_REGISTER("popupEnd", nk_love_popup_end); NK_LOVE_REGISTER("popup", nk_love_popup); + NK_LOVE_REGISTER("popupGetScroll", nk_love_popup_get_scroll); + NK_LOVE_REGISTER("popupSetScroll", nk_love_popup_set_scroll); NK_LOVE_REGISTER("combobox", nk_love_combobox); NK_LOVE_REGISTER("comboboxBegin", nk_love_combobox_begin); NK_LOVE_REGISTER("comboboxItem", nk_love_combobox_item); From f7ab5085f11659685b1653ee9d1b5a0563034093 Mon Sep 17 00:00:00 2001 From: Jethro Cao Date: Mon, 2 Dec 2019 10:34:28 +0700 Subject: [PATCH 41/52] Update Nuklear repo URL --- .gitmodules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index 26ee9e0..d87571a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "src/nuklear"] path = src/nuklear - url = https://github.com/vurtun/nuklear.git + url = https://github.com/Immediate-Mode-UI/Nuklear.git From f33c94db661aec44e7973271283cfdd30e7bd57b Mon Sep 17 00:00:00 2001 From: Jethro Cao Date: Thu, 5 Dec 2019 00:40:24 +0700 Subject: [PATCH 42/52] Add install target * this allows `$ make install` to also execute successfully * without changing the output location of nuklear.so --- CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index d87381b..ebb849a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,3 +25,9 @@ TARGET_LINK_LIBRARIES( ) SET_TARGET_PROPERTIES("${LIB_NAME}" PROPERTIES PREFIX "") + +IF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) + SET(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}" CACHE PATH "..." FORCE) +ENDIF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) + +INSTALL(TARGETS "${LIB_NAME}" DESTINATION .) From 09cdd09716e668999d70d839b9b3bff825702452 Mon Sep 17 00:00:00 2001 From: Jethro Cao Date: Fri, 20 Dec 2019 17:15:24 +0700 Subject: [PATCH 43/52] Add Guix build instruction in README --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 95bbecd..22d391e 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,13 @@ $ make ``` 5. Locate `nuklear.so` in the build folder. +#### Via GNU Guix + +LÖVE-Nuklear is also available as a [Guix](http://guix.gnu.org/) package, and can thus be directly downloaded and built via: +``` +$ guix package --install love-nuklear +``` + ### Compiling with CMake and MinGW on Windows 1. Install [CMake](https://cmake.org/download/) and [MinGW](http://mingw.org/) or [MinGW-w64](https://mingw-w64.org/doku.php). From d41e9d187539d8a9efe4cef8432d249af0a57463 Mon Sep 17 00:00:00 2001 From: Grump Date: Tue, 14 Apr 2020 09:27:36 +0200 Subject: [PATCH 44/52] fix typo: cusor -> cursor --- src/nuklear_love.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index a4488da..77af1c6 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -3865,7 +3865,7 @@ static void nk_love_style_push_progress(struct nk_style_progress *style) NK_LOVE_STYLE_PUSH("border color", color, &style->border_color); NK_LOVE_STYLE_PUSH("cursor normal", item, &style->cursor_normal); NK_LOVE_STYLE_PUSH("cursor hover", item, &style->cursor_hover); - NK_LOVE_STYLE_PUSH("cusor active", item, &style->cursor_active); + NK_LOVE_STYLE_PUSH("cursor active", item, &style->cursor_active); NK_LOVE_STYLE_PUSH("cursor border color", color, &style->cursor_border_color); NK_LOVE_STYLE_PUSH("rounding", float, &style->rounding); NK_LOVE_STYLE_PUSH("border", float, &style->border); From 7491ab58b2b96aceb00be26be48a824ff9f09154 Mon Sep 17 00:00:00 2001 From: Yannick A Date: Sat, 25 Apr 2020 23:35:58 +0200 Subject: [PATCH 45/52] Update Nuklear repo URL in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 22d391e..6ded569 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # LÖVE-Nuklear -[Nuklear](https://github.com/vurtun/nuklear) module for the [LÖVE](https://love2d.org/) game engine. +[Nuklear](https://github.com/Immediate-Mode-UI/Nuklear) module for the [LÖVE](https://love2d.org/) game engine. Provides a lightweight immediate mode GUI for LÖVE games. From 640a50532bddf4553ece701f24291b64f601a26d Mon Sep 17 00:00:00 2001 From: Miku AuahDark Date: Thu, 7 May 2020 20:44:21 +0800 Subject: [PATCH 46/52] MSVC tweaks --- CMakeLists.txt | 4 ++++ src/nuklear_love.c | 2 ++ 2 files changed, 6 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index ebb849a..9d8c1f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,10 @@ TARGET_LINK_LIBRARIES( ${LUA_LIBRARIES} ) +IF(MSVC) + TARGET_COMPILE_DEFINITIONS(${LIB_NAME} PRIVATE LUA_BUILD_AS_DLL) +endif(MSVC) + SET_TARGET_PROPERTIES("${LIB_NAME}" PROPERTIES PREFIX "") IF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 77af1c6..4f2f8ff 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -8,6 +8,8 @@ #include #include +#define LUA_LIB + #include #include From 0347818ecaa5dbddb89b8a9623a6c0df4a3a40be Mon Sep 17 00:00:00 2001 From: Miku AuahDark Date: Fri, 8 May 2020 16:57:15 +0800 Subject: [PATCH 47/52] Add MSVC build instructions. --- .gitignore | 1 + README.md | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/README.md b/README.md index 6ded569..77244e5 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,22 @@ $ mingw32-make ``` 18. Locate `nuklear.dll` inside the build folder. +### Compiling with CMake and MSVC on Windows + +1. Install [CMake](https://cmake.org/download/) and [Visual Studio](https://visualstudio.microsoft.com/). +Community or Express edition is sufficient. +2. Download the source code for [LuaJIT](http://luajit.org/download.html). +3. Open a Visual Studio Command Prompt (x86 or x64 depending on what architecture you need) +and set the current directory to the LuaJIT folder (the one that contains "README"). Also +remember this path. +4. At the VS Command Prompt, set your current directory to `src` then +execute `msvcbuild.bat`. This will create lua51.dll, lua51.lib, and luajit.exe +5. Now open new command prompt window inside the `love-nuklear` folder. +6. Type `set "LUA_DIR="` +7. Then type `cmake -Bbuild -H. -A Win32 -DLUA_INCLUDE_DIR=%LUA_DIR%\src -DLUA_LIBRARY=%LUA_DIR%\src\lua51.lib -DCMAKE_INSTALL_PREFIX=%CD%\install`. +If you previously compile LuaJIT using x64 VS command prompt, replace `Win32` with `x64` at above command. +8. Then type `cmake --build build --config Release --target install` and you'll found `nuklear.dll` inside "install" folder. + ## Documentation A complete description of all functions and style properties, alongside additional examples, is available at the [LÖVE-Nuklear wiki](https://github.com/keharriso/love-nuklear/wiki). From 018aaf60a08871397c3b31423a65e6043edf2515 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Fri, 8 May 2020 14:41:31 -0400 Subject: [PATCH 48/52] Prevent crash due to not returning value from layout_template_end --- src/nuklear_love.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 77af1c6..2c1332b 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -2181,6 +2181,7 @@ static int nk_love_layout_template_end(lua_State *L) nk_love_assert_argc(lua_gettop(L) == 1); nk_love_assert_context(1); nk_layout_row_template_end(&context->nkctx); + return 0; } static int nk_love_layout_template(lua_State *L) From 829dc151c0f7e195213f33135a9974d5f4c7c4ae Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Sun, 27 Aug 2023 11:27:51 -0400 Subject: [PATCH 49/52] Replace global lua_State with local arguments --- src/nuklear_love.c | 1333 ++++++++++++++++++++++---------------------- 1 file changed, 674 insertions(+), 659 deletions(-) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index 0ca9213..a740913 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -42,14 +42,23 @@ #define NK_LOVE_MAX_FONTS 1024 #define NK_LOVE_MAX_RATIOS 1024 -static lua_State *L; static char *edit_buffer; static const char **combobox_items; static float *points; +static struct nk_love_handle { + lua_State *L; + int ref; +}; + +static struct nk_love_font { + struct nk_user_font font; + struct nk_love_handle handle; +}; + static struct nk_love_context { struct nk_context nkctx; - struct nk_user_font *fonts; + struct nk_love_font *fonts; int font_count; float *layout_ratios; int layout_ratio_count; @@ -58,7 +67,7 @@ static struct nk_love_context { int transform_allowed; } *context; -static void nk_love_assert(int pass, const char *msg) +static void nk_love_assert(lua_State *L, int pass, const char *msg) { if (!pass) { lua_Debug ar; @@ -71,24 +80,24 @@ static void nk_love_assert(int pass, const char *msg) } } -static void nk_love_assert_argc(int pass) +static void nk_love_assert_argc(lua_State *L, int pass) { - nk_love_assert(pass, "wrong number of arguments to '%s'"); + nk_love_assert(L, pass, "wrong number of arguments to '%s'"); } -static void nk_love_assert_alloc(void *mem) +static void nk_love_assert_alloc(lua_State *L, void *mem) { - nk_love_assert(mem != NULL, "out of memory in '%s'"); + nk_love_assert(L, mem != NULL, "out of memory in '%s'"); } -static void *nk_love_malloc(size_t size) +static void *nk_love_malloc(lua_State *L, size_t size) { void *mem = malloc(size); - nk_love_assert_alloc(mem); + nk_love_assert_alloc(L, mem); return mem; } -static struct nk_love_context *nk_love_checkcontext(int index) +static struct nk_love_context *nk_love_checkcontext(lua_State *L, int index) { if (index < 0) index += lua_gettop(L) + 1; @@ -104,21 +113,21 @@ static struct nk_love_context *nk_love_checkcontext(int index) luaL_typerror(L, index, "Nuklear context"); } -static void nk_love_assert_context(int index) +static void nk_love_assert_context(lua_State *L, int index) { - struct nk_love_context *ctx = nk_love_checkcontext(index); + struct nk_love_context *ctx = nk_love_checkcontext(L, index); ctx->transform_allowed = 0; - nk_love_assert(ctx == context, "%s: UI calls must reside between ui:frameBegin and ui:frameEnd"); + nk_love_assert(L, ctx == context, "%s: UI calls must reside between ui:frameBegin and ui:frameEnd"); } -static void nk_love_assert_transform(void) +static void nk_love_assert_transform(lua_State *L) { - struct nk_love_context *ctx = nk_love_checkcontext(1); - nk_love_assert(ctx == context && ctx->transform_allowed, + struct nk_love_context *ctx = nk_love_checkcontext(L, 1); + nk_love_assert(L, ctx == context && ctx->transform_allowed, "%s: UI transformations must occur directly after ui:frameBegin"); } -static void nk_love_pushregistry(const char *name) +static void nk_love_pushregistry(lua_State *L, const char *name) { lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); lua_pushlightuserdata(L, context); @@ -128,7 +137,7 @@ static void nk_love_pushregistry(const char *name) lua_pop(L, 1); } -static int nk_love_is_type(int index, const char *type) +static int nk_love_is_type(lua_State *L, int index, const char *type) { if (index < 0) index += lua_gettop(L) + 1; @@ -151,8 +160,10 @@ static int nk_love_is_type(int index, const char *type) static float nk_love_get_text_width(nk_handle handle, float height, const char *text, int len) { - nk_love_pushregistry("font"); - lua_rawgeti(L, -1, handle.id); + struct nk_love_handle *love_handle = handle.ptr; + lua_State *L = love_handle->L; + nk_love_pushregistry(L, "font"); + lua_rawgeti(L, -1, love_handle->ref); lua_getfield(L, -1, "getWidth"); lua_replace(L, -3); lua_pushlstring(L, text, len); @@ -162,30 +173,32 @@ static float nk_love_get_text_width(nk_handle handle, float height, return width; } -static void nk_love_checkFont(int index, struct nk_user_font *font) +static void nk_love_checkFont(lua_State *L, int index, struct nk_love_font *font) { if (index < 0) index += lua_gettop(L) + 1; - if (!nk_love_is_type(index, "Font")) + if (!nk_love_is_type(L, index, "Font")) luaL_typerror(L, index, "Font"); - nk_love_pushregistry("font"); + nk_love_pushregistry(L, "font"); lua_pushvalue(L, index); int ref = luaL_ref(L, -2); lua_getfield(L, index, "getHeight"); lua_pushvalue(L, index); lua_call(L, 1, 1); float height = lua_tonumber(L, -1); - font->userdata = nk_handle_id(ref); - font->height = height; - font->width = nk_love_get_text_width; + font->handle.L = L; + font->handle.ref = ref; + font->font.userdata.ptr = &font->handle; + font->font.height = height; + font->font.width = nk_love_get_text_width; lua_pop(L, 2); } -static void nk_love_checkImage(int index, struct nk_image *image) +static void nk_love_checkImage(lua_State *L, int index, struct nk_image *image) { if (index < 0) index += lua_gettop(L) + 1; - if (nk_love_is_type(index, "Image") || nk_love_is_type(index, "Canvas")) { + if (nk_love_is_type(L, index, "Image") || nk_love_is_type(L, index, "Canvas")) { lua_getglobal(L, "love"); lua_getfield(L, -1, "graphics"); lua_getfield(L, -1, "newQuad"); @@ -207,7 +220,7 @@ static void nk_love_checkImage(int index, struct nk_image *image) lua_createtable(L, 2, 0); lua_rawgeti(L, index, 2); lua_rawgeti(L, index, 1); - if ((nk_love_is_type(-1, "Image") || nk_love_is_type(-1, "Canvas")) && nk_love_is_type(-2, "Quad")) { + if ((nk_love_is_type(L, -1, "Image") || nk_love_is_type(L, -1, "Canvas")) && nk_love_is_type(L, -2, "Quad")) { lua_rawseti(L, -3, 1); lua_rawseti(L, -2, 2); } else { @@ -216,7 +229,7 @@ static void nk_love_checkImage(int index, struct nk_image *image) } else { luaL_argerror(L, index, "expecting Image or Canvas or {Image, Quad} or {Canvas, Quad}"); } - nk_love_pushregistry("image"); + nk_love_pushregistry(L, "image"); lua_pushvalue(L, -2); int ref = luaL_ref(L, -2); image->handle = nk_handle_id(ref); @@ -230,7 +243,7 @@ static int nk_love_is_hex(char c) || (c >= 'A' && c <= 'F'); } -static int nk_love_is_color(int index) +static int nk_love_is_color(lua_State *L, int index) { if (index < 0) index += lua_gettop(L) + 1; @@ -249,11 +262,11 @@ static int nk_love_is_color(int index) return 0; } -static struct nk_color nk_love_checkcolor(int index) +static struct nk_color nk_love_checkcolor(lua_State *L, int index) { if (index < 0) index += lua_gettop(L) + 1; - if (!nk_love_is_color(index)) { + if (!nk_love_is_color(L, index)) { if (lua_isstring(L, index)){ const char *msg = lua_pushfstring(L, "bad color string '%s'", lua_tostring(L, index)); luaL_argerror(L, index, msg); @@ -272,8 +285,8 @@ static struct nk_color nk_love_checkcolor(int index) return color; } -static struct nk_colorf nk_love_checkcolorf(int index) { - return nk_color_cf(nk_love_checkcolor(index)); +static struct nk_colorf nk_love_checkcolorf(lua_State *L, int index) { + return nk_color_cf(nk_love_checkcolor(L, index)); } static void nk_love_color(int r, int g, int b, int a, char *color_string) @@ -291,12 +304,12 @@ static void nk_love_color(int r, int g, int b, int a, char *color_string) sprintf(color_string, format_string, r, g, b, a); } -static nk_flags nk_love_parse_window_flags(int flags_begin, int flags_end) +static nk_flags nk_love_parse_window_flags(lua_State *L, int flags_begin, int flags_end) { int i; if (flags_begin == flags_end && lua_istable(L, flags_begin)) { size_t flagCount = lua_objlen(L, flags_begin); - nk_love_assert(lua_checkstack(L, flagCount), "%s: failed to allocate stack space"); + nk_love_assert(L, lua_checkstack(L, flagCount), "%s: failed to allocate stack space"); for (i = 1; i <= flagCount; ++i) { lua_rawgeti(L, flags_begin, i); } @@ -332,7 +345,7 @@ static nk_flags nk_love_parse_window_flags(int flags_begin, int flags_end) return flags; } -static enum nk_symbol_type nk_love_checksymbol(int index) +static enum nk_symbol_type nk_love_checksymbol(lua_State *L, int index) { if (index < 0) index += lua_gettop(L) + 1; @@ -371,7 +384,7 @@ static enum nk_symbol_type nk_love_checksymbol(int index) } } -static nk_flags nk_love_checkalign(int index) +static nk_flags nk_love_checkalign(lua_State *L, int index) { if (index < 0) index += lua_gettop(L) + 1; @@ -400,7 +413,7 @@ static nk_flags nk_love_checkalign(int index) } } -static enum nk_buttons nk_love_checkbutton(int index) +static enum nk_buttons nk_love_checkbutton(lua_State *L, int index) { if (index < 0) index += lua_gettop(L) + 1; @@ -417,7 +430,7 @@ static enum nk_buttons nk_love_checkbutton(int index) } } -static enum nk_layout_format nk_love_checkformat(int index) +static enum nk_layout_format nk_love_checkformat(lua_State *L, int index) { if (index < 0) index += lua_gettop(L) + 1; @@ -432,7 +445,7 @@ static enum nk_layout_format nk_love_checkformat(int index) } } -static enum nk_tree_type nk_love_checktree(int index) +static enum nk_tree_type nk_love_checktree(lua_State *L, int index) { if (index < 0) index += lua_gettop(L) + 1; @@ -447,7 +460,7 @@ static enum nk_tree_type nk_love_checktree(int index) } } -static enum nk_collapse_states nk_love_checkstate(int index) +static enum nk_collapse_states nk_love_checkstate(lua_State *L, int index) { if (index < 0) index += lua_gettop(L) + 1; @@ -462,7 +475,7 @@ static enum nk_collapse_states nk_love_checkstate(int index) } } -static enum nk_button_behavior nk_love_checkbehavior(int index) +static enum nk_button_behavior nk_love_checkbehavior(lua_State *L, int index) { if (index < 0) index += lua_gettop(L) + 1; @@ -477,7 +490,7 @@ static enum nk_button_behavior nk_love_checkbehavior(int index) } } -static enum nk_color_format nk_love_checkcolorformat(int index) +static enum nk_color_format nk_love_checkcolorformat(lua_State *L, int index) { if (index < 0) index += lua_gettop(L) + 1; @@ -492,7 +505,7 @@ static enum nk_color_format nk_love_checkcolorformat(int index) } } -static nk_flags nk_love_checkedittype(int index) +static nk_flags nk_love_checkedittype(lua_State *L, int index) { if (index < 0) index += lua_gettop(L) + 1; @@ -509,7 +522,7 @@ static nk_flags nk_love_checkedittype(int index) } } -static enum nk_popup_type nk_love_checkpopup(int index) +static enum nk_popup_type nk_love_checkpopup(lua_State *L, int index) { if (index < 0) index += lua_gettop(L) + 1; @@ -526,7 +539,7 @@ static enum nk_popup_type nk_love_checkpopup(int index) enum nk_love_draw_mode {NK_LOVE_FILL, NK_LOVE_LINE}; -static enum nk_love_draw_mode nk_love_checkdraw(int index) +static enum nk_love_draw_mode nk_love_checkdraw(lua_State *L, int index) { if (index < 0) index += lua_gettop(L) + 1; @@ -566,7 +579,7 @@ static void nk_love_transform(float *T, int *x, int *y) * =============================================================== */ -static void nk_love_configureGraphics(int line_thickness, struct nk_color col) +static void nk_love_configureGraphics(lua_State *L, int line_thickness, struct nk_color col) { lua_getglobal(L, "love"); lua_getfield(L, -1, "graphics"); @@ -584,7 +597,7 @@ static void nk_love_configureGraphics(int line_thickness, struct nk_color col) lua_call(L, 4, 0); } -static void nk_love_getGraphics(float *line_thickness, struct nk_color *color) +static void nk_love_getGraphics(lua_State *L, float *line_thickness, struct nk_color *color) { lua_getglobal(L, "love"); lua_getfield(L, -1, "graphics"); @@ -601,7 +614,7 @@ static void nk_love_getGraphics(float *line_thickness, struct nk_color *color) lua_pop(L, 6); } -static void nk_love_scissor(int x, int y, int w, int h, +static void nk_love_scissor(lua_State *L, int x, int y, int w, int h, int nested, int px, int py, int pw, int ph) { lua_getglobal(L, "love"); @@ -630,10 +643,10 @@ static void nk_love_scissor(int x, int y, int w, int h, lua_pop(L, 2); } -static void nk_love_draw_line(int x0, int y0, int x1, int y1, +static void nk_love_draw_line(lua_State *L, int x0, int y0, int x1, int y1, int line_thickness, struct nk_color col) { - nk_love_configureGraphics(line_thickness, col); + nk_love_configureGraphics(L, line_thickness, col); lua_getfield(L, -1, "line"); lua_pushnumber(L, x0 + 0.5); lua_pushnumber(L, y0 + 0.5); @@ -643,11 +656,11 @@ static void nk_love_draw_line(int x0, int y0, int x1, int y1, lua_pop(L, 1); } -static void nk_love_draw_rect(int x, int y, unsigned int w, +static void nk_love_draw_rect(lua_State *L, int x, int y, unsigned int w, unsigned int h, unsigned int r, int line_thickness, struct nk_color col) { - nk_love_configureGraphics(line_thickness, col); + nk_love_configureGraphics(L, line_thickness, col); lua_getfield(L, -1, "rectangle"); if (line_thickness >= 0) lua_pushstring(L, "line"); @@ -663,10 +676,10 @@ static void nk_love_draw_rect(int x, int y, unsigned int w, lua_pop(L, 1); } -static void nk_love_draw_triangle(int x0, int y0, int x1, int y1, +static void nk_love_draw_triangle(lua_State *L, int x0, int y0, int x1, int y1, int x2, int y2, int line_thickness, struct nk_color col) { - nk_love_configureGraphics(line_thickness, col); + nk_love_configureGraphics(L, line_thickness, col); lua_getfield(L, -1, "polygon"); if (line_thickness >= 0) lua_pushstring(L, "line"); @@ -682,10 +695,10 @@ static void nk_love_draw_triangle(int x0, int y0, int x1, int y1, lua_pop(L, 1); } -static void nk_love_draw_polygon(const struct nk_vec2i *pnts, int count, +static void nk_love_draw_polygon(lua_State *L, const struct nk_vec2i *pnts, int count, int line_thickness, struct nk_color col) { - nk_love_configureGraphics(line_thickness, col); + nk_love_configureGraphics(L, line_thickness, col); lua_getfield(L, -1, "polygon"); if (line_thickness >= 0) lua_pushstring(L, "line"); @@ -700,10 +713,10 @@ static void nk_love_draw_polygon(const struct nk_vec2i *pnts, int count, lua_pop(L, 1); } -static void nk_love_draw_polyline(const struct nk_vec2i *pnts, +static void nk_love_draw_polyline(lua_State *L, const struct nk_vec2i *pnts, int count, int line_thickness, struct nk_color col) { - nk_love_configureGraphics(line_thickness, col); + nk_love_configureGraphics(L, line_thickness, col); lua_getfield(L, -1, "line"); int i; for (i = 0; (i < count) && (i < NK_LOVE_MAX_POINTS); ++i) { @@ -714,10 +727,10 @@ static void nk_love_draw_polyline(const struct nk_vec2i *pnts, lua_pop(L, 1); } -static void nk_love_draw_circle(int x, int y, unsigned int w, +static void nk_love_draw_circle(lua_State *L, int x, int y, unsigned int w, unsigned int h, int line_thickness, struct nk_color col) { - nk_love_configureGraphics(line_thickness, col); + nk_love_configureGraphics(L, line_thickness, col); lua_getfield(L, -1, "ellipse"); if (line_thickness >= 0) lua_pushstring(L, "line"); @@ -731,7 +744,7 @@ static void nk_love_draw_circle(int x, int y, unsigned int w, lua_pop(L, 1); } -static void nk_love_draw_curve(struct nk_vec2i p1, struct nk_vec2i p2, +static void nk_love_draw_curve(lua_State *L, struct nk_vec2i p1, struct nk_vec2i p2, struct nk_vec2i p3, struct nk_vec2i p4, unsigned int num_segments, int line_thickness, struct nk_color col) { @@ -742,7 +755,7 @@ static void nk_love_draw_curve(struct nk_vec2i p1, struct nk_vec2i p2, if (num_segments < 1) num_segments = 1; t_step = 1.0f/(float)num_segments; - nk_love_configureGraphics(line_thickness, col); + nk_love_configureGraphics(L, line_thickness, col); lua_getfield(L, -1, "line"); for (i_step = 1; i_step <= num_segments; ++i_step) { float t = t_step * (float)i_step; @@ -760,7 +773,7 @@ static void nk_love_draw_curve(struct nk_vec2i p1, struct nk_vec2i p2, lua_pop(L, 1); } -static void nk_love_draw_text(int fontref, struct nk_color cbg, +static void nk_love_draw_text(lua_State *L, int fontref, struct nk_color cbg, struct nk_color cfg, int x, int y, unsigned int w, unsigned int h, float height, int len, const char *text) { @@ -790,7 +803,7 @@ static void nk_love_draw_text(int fontref, struct nk_color cbg, lua_call(L, 4, 0); lua_getfield(L, -1, "setFont"); - nk_love_pushregistry("font"); + nk_love_pushregistry(L, "font"); lua_rawgeti(L, -1, fontref); lua_replace(L, -2); lua_call(L, 1, 0); @@ -818,7 +831,7 @@ static void interpolate_color(struct nk_color c1, struct nk_color c2, result->a = (nk_byte)NK_CLAMP(0, a, 255); } -static void nk_love_draw_rect_multi_color(int x, int y, unsigned int w, +static void nk_love_draw_rect_multi_color(lua_State *L, int x, int y, unsigned int w, unsigned int h, struct nk_color left, struct nk_color top, struct nk_color right, struct nk_color bottom) { @@ -874,12 +887,12 @@ static void nk_love_draw_rect_multi_color(int x, int y, unsigned int w, lua_pop(L, 2); } -static void nk_love_draw_image(int x, int y, unsigned int w, unsigned int h, +static void nk_love_draw_image(lua_State *L, int x, int y, unsigned int w, unsigned int h, struct nk_image image, struct nk_color color) { - nk_love_configureGraphics(-1, color); + nk_love_configureGraphics(L, -1, color); lua_getfield(L, -1, "draw"); - nk_love_pushregistry("image"); + nk_love_pushregistry(L, "image"); lua_rawgeti(L, -1, image.handle.id); lua_rawgeti(L, -1, 1); lua_replace(L, -3); @@ -900,10 +913,10 @@ static void nk_love_draw_image(int x, int y, unsigned int w, unsigned int h, lua_pop(L, 1); } -static void nk_love_draw_arc(int cx, int cy, unsigned int r, +static void nk_love_draw_arc(lua_State *L, int cx, int cy, unsigned int r, int line_thickness, float a1, float a2, struct nk_color color) { - nk_love_configureGraphics(line_thickness, color); + nk_love_configureGraphics(L, line_thickness, color); lua_getfield(L, -1, "arc"); if (line_thickness >= 0) lua_pushstring(L, "line"); @@ -928,7 +941,7 @@ static void nk_love_draw_arc(int cx, int cy, unsigned int r, static void nk_love_clipboard_paste(nk_handle usr, struct nk_text_edit *edit) { - (void)usr; + lua_State *L = usr.ptr; lua_getglobal(L, "love"); lua_getfield(L, -1, "system"); lua_getfield(L, -1, "getClipboardText"); @@ -940,7 +953,7 @@ static void nk_love_clipboard_paste(nk_handle usr, struct nk_text_edit *edit) static void nk_love_clipboard_copy(nk_handle usr, const char *text, int len) { - (void)usr; + lua_State *L = usr.ptr; char *str = 0; if (!len) return; str = (char*)malloc((size_t)len+1); @@ -980,7 +993,7 @@ static int nk_love_is_active(struct nk_context *ctx) return 0; } -static int nk_love_keyevent(struct nk_context *ctx, const char *key, +static int nk_love_keyevent(lua_State *L, struct nk_context *ctx, const char *key, const char *scancode, int isrepeat, int down) { lua_getglobal(L, "love"); @@ -1094,10 +1107,10 @@ static int nk_love_wheelmoved_event(struct nk_context *ctx, int x, int y) static int nk_love_new_ui(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 0); + nk_love_assert_argc(L, lua_gettop(L) == 0); lua_getfield(L, LUA_REGISTRYINDEX, "nuklear"); struct nk_love_context *ctx = lua_newuserdata(L, sizeof(struct nk_love_context)); - nk_love_assert_alloc(ctx); + nk_love_assert_alloc(L, ctx); lua_pushlightuserdata(L, ctx); lua_newtable(L); lua_newtable(L); @@ -1109,31 +1122,31 @@ static int nk_love_new_ui(lua_State *L) lua_settable(L, -4); lua_getfield(L, -2, "metatable"); lua_setmetatable(L, -2); - ctx->fonts = nk_love_malloc(sizeof(struct nk_user_font) * NK_LOVE_MAX_FONTS); + ctx->fonts = nk_love_malloc(L, sizeof(struct nk_love_font) * NK_LOVE_MAX_FONTS); lua_getglobal(L, "love"); - nk_love_assert(lua_istable(L, -1), "LOVE-Nuklear requires LOVE environment"); + nk_love_assert(L, lua_istable(L, -1), "LOVE-Nuklear requires LOVE environment"); lua_getfield(L, -1, "graphics"); lua_getfield(L, -1, "getFont"); lua_call(L, 0, 1); struct nk_love_context *current = context; context = ctx; - nk_love_checkFont(-1, &ctx->fonts[0]); + nk_love_checkFont(L, -1, &ctx->fonts[0]); context = current; - nk_init_default(&ctx->nkctx, &ctx->fonts[0]); + nk_init_default(&ctx->nkctx, &ctx->fonts[0].font); ctx->font_count = 1; ctx->nkctx.clip.copy = nk_love_clipboard_copy; ctx->nkctx.clip.paste = nk_love_clipboard_paste; - ctx->nkctx.clip.userdata = nk_handle_ptr(0); - ctx->layout_ratios = nk_love_malloc(sizeof(float) * NK_LOVE_MAX_RATIOS); + ctx->nkctx.clip.userdata = nk_handle_ptr(L); + ctx->layout_ratios = nk_love_malloc(L, sizeof(float) * NK_LOVE_MAX_RATIOS); ctx->layout_ratio_count = 0; lua_pop(L, 3); return 1; } -static int nk_love_destroy(lua_State *luaState) +static int nk_love_destroy(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - struct nk_love_context *ctx = nk_love_checkcontext(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + struct nk_love_context *ctx = nk_love_checkcontext(L, 1); nk_free(&ctx->nkctx); free(ctx->fonts); free(ctx->layout_ratios); @@ -1146,31 +1159,31 @@ static int nk_love_destroy(lua_State *luaState) static int nk_love_keypressed(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 4); - struct nk_context *ctx = &nk_love_checkcontext(1)->nkctx; + nk_love_assert_argc(L, lua_gettop(L) == 4); + struct nk_context *ctx = &nk_love_checkcontext(L, 1)->nkctx; const char *key = luaL_checkstring(L, 2); const char *scancode = luaL_checkstring(L, 3); int isrepeat = nk_love_checkboolean(L, 4); - int consume = nk_love_keyevent(ctx, key, scancode, isrepeat, 1); + int consume = nk_love_keyevent(L, ctx, key, scancode, isrepeat, 1); lua_pushboolean(L, consume); return 1; } static int nk_love_keyreleased(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 3); - struct nk_context *ctx = &nk_love_checkcontext(1)->nkctx; + nk_love_assert_argc(L, lua_gettop(L) == 3); + struct nk_context *ctx = &nk_love_checkcontext(L, 1)->nkctx; const char *key = luaL_checkstring(L, 2); const char *scancode = luaL_checkstring(L, 3); - int consume = nk_love_keyevent(ctx, key, scancode, 0, 0); + int consume = nk_love_keyevent(L, ctx, key, scancode, 0, 0); lua_pushboolean(L, consume); return 1; } static int nk_love_mousepressed(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 5 || lua_gettop(L) == 6); - struct nk_love_context *ctx = nk_love_checkcontext(1); + nk_love_assert_argc(L, lua_gettop(L) == 5 || lua_gettop(L) == 6); + struct nk_love_context *ctx = nk_love_checkcontext(L, 1); int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); int button = luaL_checkint(L, 4); @@ -1183,8 +1196,8 @@ static int nk_love_mousepressed(lua_State *L) static int nk_love_mousereleased(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 5 || lua_gettop(L) == 6); - struct nk_love_context *ctx = nk_love_checkcontext(1); + nk_love_assert_argc(L, lua_gettop(L) == 5 || lua_gettop(L) == 6); + struct nk_love_context *ctx = nk_love_checkcontext(L, 1); int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); int button = luaL_checkint(L, 4); @@ -1197,8 +1210,8 @@ static int nk_love_mousereleased(lua_State *L) static int nk_love_mousemoved(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 6); - struct nk_love_context *ctx = nk_love_checkcontext(1); + nk_love_assert_argc(L, lua_gettop(L) == 6); + struct nk_love_context *ctx = nk_love_checkcontext(L, 1); int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); int dx = luaL_checkint(L, 4); @@ -1211,8 +1224,8 @@ static int nk_love_mousemoved(lua_State *L) static int nk_love_textinput(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - struct nk_context *ctx = &nk_love_checkcontext(1)->nkctx; + nk_love_assert_argc(L, lua_gettop(L) == 2); + struct nk_context *ctx = &nk_love_checkcontext(L, 1)->nkctx; const char *text = luaL_checkstring(L, 2); int consume = nk_love_textinput_event(ctx, text); lua_pushboolean(L, consume); @@ -1221,8 +1234,8 @@ static int nk_love_textinput(lua_State *L) static int nk_love_wheelmoved(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 3); - struct nk_context *ctx = &nk_love_checkcontext(1)->nkctx; + nk_love_assert_argc(L, lua_gettop(L) == 3); + struct nk_context *ctx = &nk_love_checkcontext(L, 1)->nkctx; int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); int consume = nk_love_wheelmoved_event(ctx, x, y); @@ -1232,8 +1245,8 @@ static int nk_love_wheelmoved(lua_State *L) static int nk_love_draw(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - context = nk_love_checkcontext(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + context = nk_love_checkcontext(L, 1); lua_getglobal(L, "love"); lua_getfield(L, -1, "graphics"); @@ -1258,7 +1271,7 @@ static int nk_love_draw(lua_State *L) } lua_pop(L, 4); - nk_love_pushregistry("transform"); + nk_love_pushregistry(L, "transform"); size_t transform_count = lua_objlen(L, -1); size_t i, j; for (i = 1; i <= transform_count; ++i) { @@ -1280,78 +1293,79 @@ static int nk_love_draw(lua_State *L) case NK_COMMAND_NOP: break; case NK_COMMAND_SCISSOR: { const struct nk_command_scissor *s =(const struct nk_command_scissor*)cmd; - nk_love_scissor(s->x, s->y, s->w, s->h, nest_scissor, px, py, pw, ph); + nk_love_scissor(L, s->x, s->y, s->w, s->h, nest_scissor, px, py, pw, ph); } break; case NK_COMMAND_LINE: { const struct nk_command_line *l = (const struct nk_command_line *)cmd; - nk_love_draw_line(l->begin.x, l->begin.y, l->end.x, + nk_love_draw_line(L, l->begin.x, l->begin.y, l->end.x, l->end.y, l->line_thickness, l->color); } break; case NK_COMMAND_RECT: { const struct nk_command_rect *r = (const struct nk_command_rect *)cmd; - nk_love_draw_rect(r->x, r->y, r->w, r->h, + nk_love_draw_rect(L, r->x, r->y, r->w, r->h, (unsigned int)r->rounding, r->line_thickness, r->color); } break; case NK_COMMAND_RECT_FILLED: { const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled *)cmd; - nk_love_draw_rect(r->x, r->y, r->w, r->h, (unsigned int)r->rounding, -1, r->color); + nk_love_draw_rect(L, r->x, r->y, r->w, r->h, (unsigned int)r->rounding, -1, r->color); } break; case NK_COMMAND_CIRCLE: { const struct nk_command_circle *c = (const struct nk_command_circle *)cmd; - nk_love_draw_circle(c->x, c->y, c->w, c->h, c->line_thickness, c->color); + nk_love_draw_circle(L, c->x, c->y, c->w, c->h, c->line_thickness, c->color); } break; case NK_COMMAND_CIRCLE_FILLED: { const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd; - nk_love_draw_circle(c->x, c->y, c->w, c->h, -1, c->color); + nk_love_draw_circle(L, c->x, c->y, c->w, c->h, -1, c->color); } break; case NK_COMMAND_TRIANGLE: { const struct nk_command_triangle *t = (const struct nk_command_triangle*)cmd; - nk_love_draw_triangle(t->a.x, t->a.y, t->b.x, t->b.y, + nk_love_draw_triangle(L, t->a.x, t->a.y, t->b.x, t->b.y, t->c.x, t->c.y, t->line_thickness, t->color); } break; case NK_COMMAND_TRIANGLE_FILLED: { const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled *)cmd; - nk_love_draw_triangle(t->a.x, t->a.y, t->b.x, t->b.y, t->c.x, t->c.y, -1, t->color); + nk_love_draw_triangle(L, t->a.x, t->a.y, t->b.x, t->b.y, t->c.x, t->c.y, -1, t->color); } break; case NK_COMMAND_POLYGON: { const struct nk_command_polygon *p =(const struct nk_command_polygon*)cmd; - nk_love_draw_polygon(p->points, p->point_count, p->line_thickness, p->color); + nk_love_draw_polygon(L, p->points, p->point_count, p->line_thickness, p->color); } break; case NK_COMMAND_POLYGON_FILLED: { const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled*)cmd; - nk_love_draw_polygon(p->points, p->point_count, -1, p->color); + nk_love_draw_polygon(L, p->points, p->point_count, -1, p->color); } break; case NK_COMMAND_POLYLINE: { const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd; - nk_love_draw_polyline(p->points, p->point_count, p->line_thickness, p->color); + nk_love_draw_polyline(L, p->points, p->point_count, p->line_thickness, p->color); } break; case NK_COMMAND_TEXT: { const struct nk_command_text *t = (const struct nk_command_text*)cmd; - nk_love_draw_text(t->font->userdata.id, t->background, + struct nk_love_handle *love_handle = t->font->userdata.ptr; + nk_love_draw_text(love_handle->L, love_handle->ref, t->background, t->foreground, t->x, t->y, t->w, t->h, t->height, t->length, (const char*)t->string); } break; case NK_COMMAND_CURVE: { const struct nk_command_curve *q = (const struct nk_command_curve *)cmd; - nk_love_draw_curve(q->begin, q->ctrl[0], q->ctrl[1], + nk_love_draw_curve(L, q->begin, q->ctrl[0], q->ctrl[1], q->end, 22, q->line_thickness, q->color); } break; case NK_COMMAND_RECT_MULTI_COLOR: { const struct nk_command_rect_multi_color *r = (const struct nk_command_rect_multi_color *)cmd; - nk_love_draw_rect_multi_color(r->x, r->y, r->w, r->h, r->left, r->top, r->bottom, r->right); + nk_love_draw_rect_multi_color(L, r->x, r->y, r->w, r->h, r->left, r->top, r->bottom, r->right); } break; case NK_COMMAND_IMAGE: { const struct nk_command_image *i = (const struct nk_command_image *)cmd; - nk_love_draw_image(i->x, i->y, i->w, i->h, i->img, i->col); + nk_love_draw_image(L, i->x, i->y, i->w, i->h, i->img, i->col); } break; case NK_COMMAND_ARC: { const struct nk_command_arc *a = (const struct nk_command_arc *)cmd; - nk_love_draw_arc(a->cx, a->cy, a->r, a->line_thickness, + nk_love_draw_arc(L, a->cx, a->cy, a->r, a->line_thickness, a->a[0], a->a[1], a->color); } break; case NK_COMMAND_ARC_FILLED: { const struct nk_command_arc_filled *a = (const struct nk_command_arc_filled *)cmd; - nk_love_draw_arc(a->cx, a->cy, a->r, -1, a->a[0], a->a[1], a->color); + nk_love_draw_arc(L, a->cx, a->cy, a->r, -1, a->a[0], a->a[1], a->color); } break; default: break; } @@ -1365,140 +1379,140 @@ static int nk_love_draw(lua_State *L) return 0; } -static void nk_love_preserve(struct nk_style_item *item) +static void nk_love_preserve(lua_State *L, struct nk_style_item *item) { if (item->type == NK_STYLE_ITEM_IMAGE) { lua_rawgeti(L, -1, item->data.image.handle.id); - nk_love_checkImage(-1, &item->data.image); + nk_love_checkImage(L, -1, &item->data.image); lua_pop(L, 1); } } -static void nk_love_preserve_all(void) +static void nk_love_preserve_all(lua_State *L) { - nk_love_preserve(&context->nkctx.style.button.normal); - nk_love_preserve(&context->nkctx.style.button.hover); - nk_love_preserve(&context->nkctx.style.button.active); + nk_love_preserve(L, &context->nkctx.style.button.normal); + nk_love_preserve(L, &context->nkctx.style.button.hover); + nk_love_preserve(L, &context->nkctx.style.button.active); - nk_love_preserve(&context->nkctx.style.contextual_button.normal); - nk_love_preserve(&context->nkctx.style.contextual_button.hover); - nk_love_preserve(&context->nkctx.style.contextual_button.active); + nk_love_preserve(L, &context->nkctx.style.contextual_button.normal); + nk_love_preserve(L, &context->nkctx.style.contextual_button.hover); + nk_love_preserve(L, &context->nkctx.style.contextual_button.active); - nk_love_preserve(&context->nkctx.style.menu_button.normal); - nk_love_preserve(&context->nkctx.style.menu_button.hover); - nk_love_preserve(&context->nkctx.style.menu_button.active); + nk_love_preserve(L, &context->nkctx.style.menu_button.normal); + nk_love_preserve(L, &context->nkctx.style.menu_button.hover); + nk_love_preserve(L, &context->nkctx.style.menu_button.active); - nk_love_preserve(&context->nkctx.style.option.normal); - nk_love_preserve(&context->nkctx.style.option.hover); - nk_love_preserve(&context->nkctx.style.option.active); - nk_love_preserve(&context->nkctx.style.option.cursor_normal); - nk_love_preserve(&context->nkctx.style.option.cursor_hover); + nk_love_preserve(L, &context->nkctx.style.option.normal); + nk_love_preserve(L, &context->nkctx.style.option.hover); + nk_love_preserve(L, &context->nkctx.style.option.active); + nk_love_preserve(L, &context->nkctx.style.option.cursor_normal); + nk_love_preserve(L, &context->nkctx.style.option.cursor_hover); - nk_love_preserve(&context->nkctx.style.checkbox.normal); - nk_love_preserve(&context->nkctx.style.checkbox.hover); - nk_love_preserve(&context->nkctx.style.checkbox.active); - nk_love_preserve(&context->nkctx.style.checkbox.cursor_normal); - nk_love_preserve(&context->nkctx.style.checkbox.cursor_hover); + nk_love_preserve(L, &context->nkctx.style.checkbox.normal); + nk_love_preserve(L, &context->nkctx.style.checkbox.hover); + nk_love_preserve(L, &context->nkctx.style.checkbox.active); + nk_love_preserve(L, &context->nkctx.style.checkbox.cursor_normal); + nk_love_preserve(L, &context->nkctx.style.checkbox.cursor_hover); - nk_love_preserve(&context->nkctx.style.selectable.normal); - nk_love_preserve(&context->nkctx.style.selectable.hover); - nk_love_preserve(&context->nkctx.style.selectable.pressed); - nk_love_preserve(&context->nkctx.style.selectable.normal_active); - nk_love_preserve(&context->nkctx.style.selectable.hover_active); - nk_love_preserve(&context->nkctx.style.selectable.pressed_active); + nk_love_preserve(L, &context->nkctx.style.selectable.normal); + nk_love_preserve(L, &context->nkctx.style.selectable.hover); + nk_love_preserve(L, &context->nkctx.style.selectable.pressed); + nk_love_preserve(L, &context->nkctx.style.selectable.normal_active); + nk_love_preserve(L, &context->nkctx.style.selectable.hover_active); + nk_love_preserve(L, &context->nkctx.style.selectable.pressed_active); - nk_love_preserve(&context->nkctx.style.slider.normal); - nk_love_preserve(&context->nkctx.style.slider.hover); - nk_love_preserve(&context->nkctx.style.slider.active); - nk_love_preserve(&context->nkctx.style.slider.cursor_normal); - nk_love_preserve(&context->nkctx.style.slider.cursor_hover); - nk_love_preserve(&context->nkctx.style.slider.cursor_active); + nk_love_preserve(L, &context->nkctx.style.slider.normal); + nk_love_preserve(L, &context->nkctx.style.slider.hover); + nk_love_preserve(L, &context->nkctx.style.slider.active); + nk_love_preserve(L, &context->nkctx.style.slider.cursor_normal); + nk_love_preserve(L, &context->nkctx.style.slider.cursor_hover); + nk_love_preserve(L, &context->nkctx.style.slider.cursor_active); - nk_love_preserve(&context->nkctx.style.progress.normal); - nk_love_preserve(&context->nkctx.style.progress.hover); - nk_love_preserve(&context->nkctx.style.progress.active); - nk_love_preserve(&context->nkctx.style.progress.cursor_normal); - nk_love_preserve(&context->nkctx.style.progress.cursor_hover); - nk_love_preserve(&context->nkctx.style.progress.cursor_active); + nk_love_preserve(L, &context->nkctx.style.progress.normal); + nk_love_preserve(L, &context->nkctx.style.progress.hover); + nk_love_preserve(L, &context->nkctx.style.progress.active); + nk_love_preserve(L, &context->nkctx.style.progress.cursor_normal); + nk_love_preserve(L, &context->nkctx.style.progress.cursor_hover); + nk_love_preserve(L, &context->nkctx.style.progress.cursor_active); - nk_love_preserve(&context->nkctx.style.property.normal); - nk_love_preserve(&context->nkctx.style.property.hover); - nk_love_preserve(&context->nkctx.style.property.active); - nk_love_preserve(&context->nkctx.style.property.edit.normal); - nk_love_preserve(&context->nkctx.style.property.edit.hover); - nk_love_preserve(&context->nkctx.style.property.edit.active); - nk_love_preserve(&context->nkctx.style.property.inc_button.normal); - nk_love_preserve(&context->nkctx.style.property.inc_button.hover); - nk_love_preserve(&context->nkctx.style.property.inc_button.active); - nk_love_preserve(&context->nkctx.style.property.dec_button.normal); - nk_love_preserve(&context->nkctx.style.property.dec_button.hover); - nk_love_preserve(&context->nkctx.style.property.dec_button.active); + nk_love_preserve(L, &context->nkctx.style.property.normal); + nk_love_preserve(L, &context->nkctx.style.property.hover); + nk_love_preserve(L, &context->nkctx.style.property.active); + nk_love_preserve(L, &context->nkctx.style.property.edit.normal); + nk_love_preserve(L, &context->nkctx.style.property.edit.hover); + nk_love_preserve(L, &context->nkctx.style.property.edit.active); + nk_love_preserve(L, &context->nkctx.style.property.inc_button.normal); + nk_love_preserve(L, &context->nkctx.style.property.inc_button.hover); + nk_love_preserve(L, &context->nkctx.style.property.inc_button.active); + nk_love_preserve(L, &context->nkctx.style.property.dec_button.normal); + nk_love_preserve(L, &context->nkctx.style.property.dec_button.hover); + nk_love_preserve(L, &context->nkctx.style.property.dec_button.active); - nk_love_preserve(&context->nkctx.style.edit.normal); - nk_love_preserve(&context->nkctx.style.edit.hover); - nk_love_preserve(&context->nkctx.style.edit.active); - nk_love_preserve(&context->nkctx.style.edit.scrollbar.normal); - nk_love_preserve(&context->nkctx.style.edit.scrollbar.hover); - nk_love_preserve(&context->nkctx.style.edit.scrollbar.active); - nk_love_preserve(&context->nkctx.style.edit.scrollbar.cursor_normal); - nk_love_preserve(&context->nkctx.style.edit.scrollbar.cursor_hover); - nk_love_preserve(&context->nkctx.style.edit.scrollbar.cursor_active); + nk_love_preserve(L, &context->nkctx.style.edit.normal); + nk_love_preserve(L, &context->nkctx.style.edit.hover); + nk_love_preserve(L, &context->nkctx.style.edit.active); + nk_love_preserve(L, &context->nkctx.style.edit.scrollbar.normal); + nk_love_preserve(L, &context->nkctx.style.edit.scrollbar.hover); + nk_love_preserve(L, &context->nkctx.style.edit.scrollbar.active); + nk_love_preserve(L, &context->nkctx.style.edit.scrollbar.cursor_normal); + nk_love_preserve(L, &context->nkctx.style.edit.scrollbar.cursor_hover); + nk_love_preserve(L, &context->nkctx.style.edit.scrollbar.cursor_active); - nk_love_preserve(&context->nkctx.style.chart.background); + nk_love_preserve(L, &context->nkctx.style.chart.background); - nk_love_preserve(&context->nkctx.style.scrollh.normal); - nk_love_preserve(&context->nkctx.style.scrollh.hover); - nk_love_preserve(&context->nkctx.style.scrollh.active); - nk_love_preserve(&context->nkctx.style.scrollh.cursor_normal); - nk_love_preserve(&context->nkctx.style.scrollh.cursor_hover); - nk_love_preserve(&context->nkctx.style.scrollh.cursor_active); + nk_love_preserve(L, &context->nkctx.style.scrollh.normal); + nk_love_preserve(L, &context->nkctx.style.scrollh.hover); + nk_love_preserve(L, &context->nkctx.style.scrollh.active); + nk_love_preserve(L, &context->nkctx.style.scrollh.cursor_normal); + nk_love_preserve(L, &context->nkctx.style.scrollh.cursor_hover); + nk_love_preserve(L, &context->nkctx.style.scrollh.cursor_active); - nk_love_preserve(&context->nkctx.style.scrollv.normal); - nk_love_preserve(&context->nkctx.style.scrollv.hover); - nk_love_preserve(&context->nkctx.style.scrollv.active); - nk_love_preserve(&context->nkctx.style.scrollv.cursor_normal); - nk_love_preserve(&context->nkctx.style.scrollv.cursor_hover); - nk_love_preserve(&context->nkctx.style.scrollv.cursor_active); + nk_love_preserve(L, &context->nkctx.style.scrollv.normal); + nk_love_preserve(L, &context->nkctx.style.scrollv.hover); + nk_love_preserve(L, &context->nkctx.style.scrollv.active); + nk_love_preserve(L, &context->nkctx.style.scrollv.cursor_normal); + nk_love_preserve(L, &context->nkctx.style.scrollv.cursor_hover); + nk_love_preserve(L, &context->nkctx.style.scrollv.cursor_active); - nk_love_preserve(&context->nkctx.style.tab.background); - nk_love_preserve(&context->nkctx.style.tab.tab_maximize_button.normal); - nk_love_preserve(&context->nkctx.style.tab.tab_maximize_button.hover); - nk_love_preserve(&context->nkctx.style.tab.tab_maximize_button.active); - nk_love_preserve(&context->nkctx.style.tab.tab_minimize_button.normal); - nk_love_preserve(&context->nkctx.style.tab.tab_minimize_button.hover); - nk_love_preserve(&context->nkctx.style.tab.tab_minimize_button.active); - nk_love_preserve(&context->nkctx.style.tab.node_maximize_button.normal); - nk_love_preserve(&context->nkctx.style.tab.node_maximize_button.hover); - nk_love_preserve(&context->nkctx.style.tab.node_maximize_button.active); - nk_love_preserve(&context->nkctx.style.tab.node_minimize_button.normal); - nk_love_preserve(&context->nkctx.style.tab.node_minimize_button.hover); - nk_love_preserve(&context->nkctx.style.tab.node_minimize_button.active); + nk_love_preserve(L, &context->nkctx.style.tab.background); + nk_love_preserve(L, &context->nkctx.style.tab.tab_maximize_button.normal); + nk_love_preserve(L, &context->nkctx.style.tab.tab_maximize_button.hover); + nk_love_preserve(L, &context->nkctx.style.tab.tab_maximize_button.active); + nk_love_preserve(L, &context->nkctx.style.tab.tab_minimize_button.normal); + nk_love_preserve(L, &context->nkctx.style.tab.tab_minimize_button.hover); + nk_love_preserve(L, &context->nkctx.style.tab.tab_minimize_button.active); + nk_love_preserve(L, &context->nkctx.style.tab.node_maximize_button.normal); + nk_love_preserve(L, &context->nkctx.style.tab.node_maximize_button.hover); + nk_love_preserve(L, &context->nkctx.style.tab.node_maximize_button.active); + nk_love_preserve(L, &context->nkctx.style.tab.node_minimize_button.normal); + nk_love_preserve(L, &context->nkctx.style.tab.node_minimize_button.hover); + nk_love_preserve(L, &context->nkctx.style.tab.node_minimize_button.active); - nk_love_preserve(&context->nkctx.style.combo.normal); - nk_love_preserve(&context->nkctx.style.combo.hover); - nk_love_preserve(&context->nkctx.style.combo.active); - nk_love_preserve(&context->nkctx.style.combo.button.normal); - nk_love_preserve(&context->nkctx.style.combo.button.hover); - nk_love_preserve(&context->nkctx.style.combo.button.active); + nk_love_preserve(L, &context->nkctx.style.combo.normal); + nk_love_preserve(L, &context->nkctx.style.combo.hover); + nk_love_preserve(L, &context->nkctx.style.combo.active); + nk_love_preserve(L, &context->nkctx.style.combo.button.normal); + nk_love_preserve(L, &context->nkctx.style.combo.button.hover); + nk_love_preserve(L, &context->nkctx.style.combo.button.active); - nk_love_preserve(&context->nkctx.style.window.fixed_background); - nk_love_preserve(&context->nkctx.style.window.scaler); - nk_love_preserve(&context->nkctx.style.window.header.normal); - nk_love_preserve(&context->nkctx.style.window.header.hover); - nk_love_preserve(&context->nkctx.style.window.header.active); - nk_love_preserve(&context->nkctx.style.window.header.close_button.normal); - nk_love_preserve(&context->nkctx.style.window.header.close_button.hover); - nk_love_preserve(&context->nkctx.style.window.header.close_button.active); - nk_love_preserve(&context->nkctx.style.window.header.minimize_button.normal); - nk_love_preserve(&context->nkctx.style.window.header.minimize_button.hover); - nk_love_preserve(&context->nkctx.style.window.header.minimize_button.active); + nk_love_preserve(L, &context->nkctx.style.window.fixed_background); + nk_love_preserve(L, &context->nkctx.style.window.scaler); + nk_love_preserve(L, &context->nkctx.style.window.header.normal); + nk_love_preserve(L, &context->nkctx.style.window.header.hover); + nk_love_preserve(L, &context->nkctx.style.window.header.active); + nk_love_preserve(L, &context->nkctx.style.window.header.close_button.normal); + nk_love_preserve(L, &context->nkctx.style.window.header.close_button.hover); + nk_love_preserve(L, &context->nkctx.style.window.header.close_button.active); + nk_love_preserve(L, &context->nkctx.style.window.header.minimize_button.normal); + nk_love_preserve(L, &context->nkctx.style.window.header.minimize_button.hover); + nk_love_preserve(L, &context->nkctx.style.window.header.minimize_button.active); } static int nk_love_frame_begin(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert(context == NULL, "%s: missing ui:frameEnd for previous frame"); - context = nk_love_checkcontext(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert(L, context == NULL, "%s: missing ui:frameEnd for previous frame"); + context = nk_love_checkcontext(L, 1); nk_input_end(&context->nkctx); lua_getglobal(L, "love"); lua_getfield(L, -1, "timer"); @@ -1512,21 +1526,23 @@ static int nk_love_frame_begin(lua_State *L) lua_getfield(L, -1, "image"); lua_newtable(L); lua_setfield(L, -3, "image"); - nk_love_preserve_all(); + nk_love_preserve_all(L); lua_pop(L, 1); lua_getfield(L, -1, "font"); lua_newtable(L); lua_setfield(L, -3, "font"); context->font_count = 0; - lua_rawgeti(L, -1, context->nkctx.style.font->userdata.id); - nk_love_checkFont(-1, &context->fonts[context->font_count]); + struct nk_love_handle *love_handle = context->nkctx.style.font->userdata.ptr; + lua_rawgeti(L, -1, love_handle->ref); + nk_love_checkFont(L, -1, &context->fonts[context->font_count]); lua_pop(L, 1); context->nkctx.style.font = &context->fonts[context->font_count++]; int i; for (i = 0; i < context->nkctx.stacks.fonts.head; ++i) { struct nk_config_stack_user_font_element *element = &context->nkctx.stacks.fonts.elements[i]; - lua_rawgeti(L, -1, element->old_value->userdata.id); - nk_love_checkFont(-1, &context->fonts[context->font_count]); + love_handle = element->old_value->userdata.ptr; + lua_rawgeti(L, -1, love_handle->ref); + nk_love_checkFont(L, -1, &context->fonts[context->font_count]); lua_pop(L, 1); context->nkctx.stacks.fonts.elements[i].old_value = &context->fonts[context->font_count++]; } @@ -1542,8 +1558,8 @@ static int nk_love_frame_begin(lua_State *L) static int nk_love_frame_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_input_begin(&context->nkctx); context = NULL; return 0; @@ -1551,7 +1567,7 @@ static int nk_love_frame_end(lua_State *L) static int nk_love_frame(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_argc(L, lua_gettop(L) == 2); if (!lua_isfunction(L, -1)) luaL_typerror(L, lua_gettop(L), "function"); lua_getfield(L, 1, "frameBegin"); @@ -1580,10 +1596,10 @@ sin cos 0 | -sin cos 0 */ static int nk_love_rotate(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_transform(); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_transform(L); float angle = luaL_checknumber(L, 2); - nk_love_pushregistry("transform"); + nk_love_pushregistry(L, "transform"); size_t len = lua_objlen(L, -1); lua_newtable(L); lua_pushstring(L, "rotate"); @@ -1628,11 +1644,11 @@ sx 0 0 | 1/sx 0 0 */ static int nk_love_scale(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) >= 2 && lua_gettop(L) <= 3); - nk_love_assert_transform(); + nk_love_assert_argc(L, lua_gettop(L) >= 2 && lua_gettop(L) <= 3); + nk_love_assert_transform(L); float sx = luaL_checknumber(L, 2); float sy = luaL_optnumber(L, 3, sx); - nk_love_pushregistry("transform"); + nk_love_pushregistry(L, "transform"); size_t len = lua_objlen(L, -1); lua_newtable(L); lua_pushstring(L, "scale"); @@ -1665,11 +1681,11 @@ ky 1 0 | ky/(kx*ky-1) 1/(1-kx*ky) 0 */ static int nk_love_shear(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 3); - nk_love_assert_transform(); + nk_love_assert_argc(L, lua_gettop(L) == 3); + nk_love_assert_transform(L); float kx = luaL_checknumber(L, 2); float ky = luaL_checknumber(L, 3); - nk_love_pushregistry("transform"); + nk_love_pushregistry(L, "transform"); size_t len = lua_objlen(L, -1); lua_newtable(L); lua_pushstring(L, "shear"); @@ -1716,11 +1732,11 @@ static int nk_love_shear(lua_State *L) */ static int nk_love_translate(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 3); - nk_love_assert_transform(); + nk_love_assert_argc(L, lua_gettop(L) == 3); + nk_love_assert_transform(L); float dx = luaL_checknumber(L, 2); float dy = luaL_checknumber(L, 3); - nk_love_pushregistry("transform"); + nk_love_pushregistry(L, "transform"); size_t len = lua_objlen(L, -1); lua_newtable(L); lua_pushstring(L, "translate"); @@ -1760,21 +1776,21 @@ static int nk_love_translate(lua_State *L) static int nk_love_window_begin(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) >= 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) >= 1); + nk_love_assert_context(L, 1); const char *name, *title; int bounds_begin; if (lua_isnumber(L, 3)) { - nk_love_assert_argc(lua_gettop(L) >= 6); + nk_love_assert_argc(L, lua_gettop(L) >= 6); name = title = luaL_checkstring(L, 2); bounds_begin = 3; } else { - nk_love_assert_argc(lua_gettop(L) >= 7); + nk_love_assert_argc(L, lua_gettop(L) >= 7); name = luaL_checkstring(L, 2); title = luaL_checkstring(L, 3); bounds_begin = 4; } - nk_flags flags = nk_love_parse_window_flags(bounds_begin + 4, lua_gettop(L)); + nk_flags flags = nk_love_parse_window_flags(L, bounds_begin + 4, lua_gettop(L)); float x = luaL_checknumber(L, bounds_begin); float y = luaL_checknumber(L, bounds_begin + 1); float width = luaL_checknumber(L, bounds_begin + 2); @@ -1786,15 +1802,15 @@ static int nk_love_window_begin(lua_State *L) static int nk_love_window_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_end(&context->nkctx); return 0; } static int nk_love_window(lua_State *L) { - nk_love_assert(lua_checkstack(L, 2), "%s: failed to allocate stack space"); + nk_love_assert(L, lua_checkstack(L, 2), "%s: failed to allocate stack space"); if (!lua_isfunction(L, -1)) luaL_typerror(L, lua_gettop(L), "function"); lua_insert(L, 2); @@ -1819,8 +1835,8 @@ static int nk_love_window(lua_State *L) static int nk_love_window_get_bounds(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); struct nk_rect rect = nk_window_get_bounds(&context->nkctx); lua_pushnumber(L, rect.x); lua_pushnumber(L, rect.y); @@ -1831,8 +1847,8 @@ static int nk_love_window_get_bounds(lua_State *L) static int nk_love_window_get_position(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); struct nk_vec2 pos = nk_window_get_position(&context->nkctx); lua_pushnumber(L, pos.x); lua_pushnumber(L, pos.y); @@ -1841,8 +1857,8 @@ static int nk_love_window_get_position(lua_State *L) static int nk_love_window_get_size(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); struct nk_vec2 size = nk_window_get_size(&context->nkctx); lua_pushnumber(L, size.x); lua_pushnumber(L, size.y); @@ -1851,8 +1867,8 @@ static int nk_love_window_get_size(lua_State *L) static int nk_love_window_get_scroll(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_uint offset_x, offset_y; nk_window_get_scroll(&context->nkctx, &offset_x, &offset_y); lua_pushinteger(L, offset_x); @@ -1862,8 +1878,8 @@ static int nk_love_window_get_scroll(lua_State *L) static int nk_love_window_get_content_region(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); struct nk_rect rect = nk_window_get_content_region(&context->nkctx); lua_pushnumber(L, rect.x); lua_pushnumber(L, rect.y); @@ -1874,8 +1890,8 @@ static int nk_love_window_get_content_region(lua_State *L) static int nk_love_window_has_focus(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); int has_focus = nk_window_has_focus(&context->nkctx); lua_pushboolean(L, has_focus); return 1; @@ -1883,8 +1899,8 @@ static int nk_love_window_has_focus(lua_State *L) static int nk_love_window_is_collapsed(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_context(L, 1); const char *name = luaL_checkstring(L, 2); int is_collapsed = nk_window_is_collapsed(&context->nkctx, name); lua_pushboolean(L, is_collapsed); @@ -1893,8 +1909,8 @@ static int nk_love_window_is_collapsed(lua_State *L) static int nk_love_window_is_closed(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_context(L, 1); const char *name = luaL_checkstring(L, 2); int is_closed = nk_window_is_closed(&context->nkctx, name); lua_pushboolean(L, is_closed); @@ -1903,8 +1919,8 @@ static int nk_love_window_is_closed(lua_State *L) static int nk_love_window_is_hidden(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_context(L, 1); const char *name = luaL_checkstring(L, 2); int is_hidden = nk_window_is_hidden(&context->nkctx, name); lua_pushboolean(L, is_hidden); @@ -1913,8 +1929,8 @@ static int nk_love_window_is_hidden(lua_State *L) static int nk_love_window_is_active(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_context(L, 1); const char *name = luaL_checkstring(L, 2); int is_active = nk_window_is_active(&context->nkctx, name); lua_pushboolean(L, is_active); @@ -1923,8 +1939,8 @@ static int nk_love_window_is_active(lua_State *L) static int nk_love_window_is_hovered(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); int is_hovered = nk_window_is_hovered(&context->nkctx); lua_pushboolean(L, is_hovered); return 1; @@ -1932,8 +1948,8 @@ static int nk_love_window_is_hovered(lua_State *L) static int nk_love_window_is_any_hovered(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); int is_any_hovered = nk_window_is_any_hovered(&context->nkctx); lua_pushboolean(L, is_any_hovered); return 1; @@ -1941,16 +1957,16 @@ static int nk_love_window_is_any_hovered(lua_State *L) static int nk_love_item_is_any_active(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); lua_pushboolean(L, nk_love_is_active(&context->nkctx)); return 1; } static int nk_love_window_set_bounds(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 6); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 6); + nk_love_assert_context(L, 1); const char *name = luaL_checkstring(L, 2); struct nk_rect bounds; bounds.x = luaL_checknumber(L, 3); @@ -1963,8 +1979,8 @@ static int nk_love_window_set_bounds(lua_State *L) static int nk_love_window_set_position(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 4); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 4); + nk_love_assert_context(L, 1); const char *name = luaL_checkstring(L, 2); struct nk_vec2 pos; pos.x = luaL_checknumber(L, 3); @@ -1975,8 +1991,8 @@ static int nk_love_window_set_position(lua_State *L) static int nk_love_window_set_size(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 4); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 4); + nk_love_assert_context(L, 1); const char *name = luaL_checkstring(L, 2); struct nk_vec2 size; size.x = luaL_checknumber(L, 3); @@ -1987,8 +2003,8 @@ static int nk_love_window_set_size(lua_State *L) static int nk_love_window_set_focus(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_context(L, 1); const char *name = luaL_checkstring(L, 2); nk_window_set_focus(&context->nkctx, name); return 0; @@ -1996,8 +2012,8 @@ static int nk_love_window_set_focus(lua_State *L) static int nk_love_window_set_scroll(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 3); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 3); + nk_love_assert_context(L, 1); nk_uint offset_x, offset_y; offset_x = luaL_checkinteger(L, 2); offset_y = luaL_checkinteger(L, 3); @@ -2007,8 +2023,8 @@ static int nk_love_window_set_scroll(lua_State *L) static int nk_love_window_close(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_context(L, 1); const char *name = luaL_checkstring(L, 2); nk_window_close(&context->nkctx, name); return 0; @@ -2016,8 +2032,8 @@ static int nk_love_window_close(lua_State *L) static int nk_love_window_collapse(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_context(L, 1); const char *name = luaL_checkstring(L, 2); nk_window_collapse(&context->nkctx, name, NK_MINIMIZED); return 0; @@ -2025,8 +2041,8 @@ static int nk_love_window_collapse(lua_State *L) static int nk_love_window_expand(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_context(L, 1); const char *name = luaL_checkstring(L, 2); nk_window_collapse(&context->nkctx, name, NK_MAXIMIZED); return 0; @@ -2034,8 +2050,8 @@ static int nk_love_window_expand(lua_State *L) static int nk_love_window_show(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_context(L, 1); const char *name = luaL_checkstring(L, 2); nk_window_show(&context->nkctx, name, NK_SHOWN); return 0; @@ -2043,8 +2059,8 @@ static int nk_love_window_show(lua_State *L) static int nk_love_window_hide(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_context(L, 1); const char *name = luaL_checkstring(L, 2); nk_window_show(&context->nkctx, name, NK_HIDDEN); return 0; @@ -2062,7 +2078,7 @@ static int nk_love_layout_row(lua_State *L) { int argc = lua_gettop(L); if (argc == 5 && lua_isfunction(L, 5)) { - nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert(L, lua_checkstack(L, 3), "%s: failed to allocate stack space"); lua_pushvalue(L, 1); lua_insert(L, 2); lua_pushvalue(L, 1); @@ -2076,13 +2092,13 @@ static int nk_love_layout_row(lua_State *L) lua_insert(L, 1); lua_call(L, 1, 0); } else { - nk_love_assert_argc(argc >= 4 && argc <= 5); - nk_love_assert_context(1); - enum nk_layout_format format = nk_love_checkformat(2); + nk_love_assert_argc(L, argc >= 4 && argc <= 5); + nk_love_assert_context(L, 1); + enum nk_layout_format format = nk_love_checkformat(L, 2); float height = luaL_checknumber(L, 3); int use_ratios = 0; if (format == NK_DYNAMIC) { - nk_love_assert_argc(argc == 4); + nk_love_assert_argc(L, argc == 4); if (lua_isnumber(L, 4)) { int cols = luaL_checkint(L, 4); nk_layout_row_dynamic(&context->nkctx, height, cols); @@ -2121,9 +2137,9 @@ static int nk_love_layout_row(lua_State *L) static int nk_love_layout_row_begin(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 4); - nk_love_assert_context(1); - enum nk_layout_format format = nk_love_checkformat(2); + nk_love_assert_argc(L, lua_gettop(L) == 4); + nk_love_assert_context(L, 1); + enum nk_layout_format format = nk_love_checkformat(L, 2); float height = luaL_checknumber(L, 3); int cols = luaL_checkint(L, 4); nk_layout_row_begin(&context->nkctx, format, height, cols); @@ -2132,8 +2148,8 @@ static int nk_love_layout_row_begin(lua_State *L) static int nk_love_layout_row_push(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_context(L, 1); float value = luaL_checknumber(L, 2); nk_layout_row_push(&context->nkctx, value); return 0; @@ -2141,16 +2157,16 @@ static int nk_love_layout_row_push(lua_State *L) static int nk_love_layout_row_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_layout_row_end(&context->nkctx); return 0; } static int nk_love_layout_template_begin(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_context(L, 1); float height = luaL_checknumber(L, 2); nk_layout_row_template_begin(&context->nkctx, height); return 0; @@ -2158,14 +2174,14 @@ static int nk_love_layout_template_begin(lua_State *L) static int nk_love_layout_template_push(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2 || lua_gettop(L) == 3); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 2 || lua_gettop(L) == 3); + nk_love_assert_context(L, 1); const char *mode = luaL_checkstring(L, 2); if (!strcmp(mode, "dynamic")) { - nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert_argc(L, lua_gettop(L) == 2); nk_layout_row_template_push_dynamic(&context->nkctx); } else { - nk_love_assert_argc(lua_gettop(L) == 3); + nk_love_assert_argc(L, lua_gettop(L) == 3); float width = luaL_checknumber(L, 3); if (!strcmp(mode, "variable")) { nk_layout_row_template_push_variable(&context->nkctx, width); @@ -2180,16 +2196,16 @@ static int nk_love_layout_template_push(lua_State *L) static int nk_love_layout_template_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_layout_row_template_end(&context->nkctx); return 0; } static int nk_love_layout_template(lua_State *L) { - nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); - nk_love_assert_argc(lua_gettop(L) == 3); + nk_love_assert(L, lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(L, lua_gettop(L) == 3); if (!lua_isfunction(L, -1)) luaL_typerror(L, lua_gettop(L), "function"); lua_pushvalue(L, 1); @@ -2209,9 +2225,9 @@ static int nk_love_layout_template(lua_State *L) static int nk_love_layout_space_begin(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 4); - nk_love_assert_context(1); - enum nk_layout_format format = nk_love_checkformat(2); + nk_love_assert_argc(L, lua_gettop(L) == 4); + nk_love_assert_context(L, 1); + enum nk_layout_format format = nk_love_checkformat(L, 2); float height = luaL_checknumber(L, 3); int widget_count = luaL_checkint(L, 4); nk_layout_space_begin(&context->nkctx, format, height, widget_count); @@ -2220,8 +2236,8 @@ static int nk_love_layout_space_begin(lua_State *L) static int nk_love_layout_space_push(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 5); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 5); + nk_love_assert_context(L, 1); float x = luaL_checknumber(L, 2); float y = luaL_checknumber(L, 3); float width = luaL_checknumber(L, 4); @@ -2232,16 +2248,16 @@ static int nk_love_layout_space_push(lua_State *L) static int nk_love_layout_space_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_layout_space_end(&context->nkctx); return 0; } static int nk_love_layout_space(lua_State *L) { - nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); - nk_love_assert_argc(lua_gettop(L) == 5); + nk_love_assert(L, lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(L, lua_gettop(L) == 5); if (!lua_isfunction(L, -1)) luaL_typerror(L, lua_gettop(L), "function"); lua_pushvalue(L, 1); @@ -2261,8 +2277,8 @@ static int nk_love_layout_space(lua_State *L) static int nk_love_layout_space_bounds(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); struct nk_rect bounds = nk_layout_space_bounds(&context->nkctx); lua_pushnumber(L, bounds.x); lua_pushnumber(L, bounds.y); @@ -2273,8 +2289,8 @@ static int nk_love_layout_space_bounds(lua_State *L) static int nk_love_layout_space_to_screen(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 3); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 3); + nk_love_assert_context(L, 1); struct nk_vec2 local; local.x = luaL_checknumber(L, 2); local.y = luaL_checknumber(L, 3); @@ -2286,8 +2302,8 @@ static int nk_love_layout_space_to_screen(lua_State *L) static int nk_love_layout_space_to_local(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 3); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 3); + nk_love_assert_context(L, 1); struct nk_vec2 screen; screen.x = luaL_checknumber(L, 2); screen.y = luaL_checknumber(L, 3); @@ -2299,8 +2315,8 @@ static int nk_love_layout_space_to_local(lua_State *L) static int nk_love_layout_space_rect_to_screen(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 5); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 5); + nk_love_assert_context(L, 1); struct nk_rect local; local.x = luaL_checknumber(L, 2); local.y = luaL_checknumber(L, 3); @@ -2316,8 +2332,8 @@ static int nk_love_layout_space_rect_to_screen(lua_State *L) static int nk_love_layout_space_rect_to_local(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 5); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 5); + nk_love_assert_context(L, 1); struct nk_rect screen; screen.x = luaL_checknumber(L, 2); screen.y = luaL_checknumber(L, 3); @@ -2333,8 +2349,8 @@ static int nk_love_layout_space_rect_to_local(lua_State *L) static int nk_love_layout_ratio_from_pixel(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_context(L, 1); float pixel_width = luaL_checknumber(L, 2); float ratio = nk_layout_ratio_from_pixel(&context->nkctx, pixel_width); lua_pushnumber(L, ratio); @@ -2351,10 +2367,10 @@ static int nk_love_layout_ratio_from_pixel(lua_State *L) static int nk_love_group_begin(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) >= 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) >= 2); + nk_love_assert_context(L, 1); const char *title = luaL_checkstring(L, 2); - nk_flags flags = nk_love_parse_window_flags(3, lua_gettop(L)); + nk_flags flags = nk_love_parse_window_flags(L, 3, lua_gettop(L)); int open = nk_group_begin(&context->nkctx, title, flags); lua_pushboolean(L, open); return 1; @@ -2362,16 +2378,16 @@ static int nk_love_group_begin(lua_State *L) static int nk_love_group_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_group_end(&context->nkctx); return 0; } static int nk_love_group(lua_State *L) { - nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); - nk_love_assert_argc(lua_gettop(L) >= 3); + nk_love_assert(L, lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(L, lua_gettop(L) >= 3); if (!lua_isfunction(L, -1)) luaL_typerror(L, lua_gettop(L), "function"); lua_pushvalue(L, 1); @@ -2397,8 +2413,8 @@ static int nk_love_group(lua_State *L) static int nk_love_group_get_scroll(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_context(L, 1); const char *id = luaL_checkstring(L, 2); nk_uint x_offset, y_offset; nk_group_get_scroll(&context->nkctx, id, &x_offset, &y_offset); @@ -2409,8 +2425,8 @@ static int nk_love_group_get_scroll(lua_State *L) static int nk_love_group_set_scroll(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 4); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 4); + nk_love_assert_context(L, 1); const char *id = luaL_checkstring(L, 2); nk_uint x_offset = luaL_checkint(L, 3); nk_uint y_offset = luaL_checkint(L, 4); @@ -2421,19 +2437,19 @@ static int nk_love_group_set_scroll(lua_State *L) static int nk_love_tree_push(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 3 && argc <= 5); - nk_love_assert_context(1); - enum nk_tree_type type = nk_love_checktree(2); + nk_love_assert_argc(L, argc >= 3 && argc <= 5); + nk_love_assert_context(L, 1); + enum nk_tree_type type = nk_love_checktree(L, 2); const char *title = luaL_checkstring(L, 3); struct nk_image image; int use_image = 0; if (argc >= 4 && !lua_isnil(L, 4)) { - nk_love_checkImage(4, &image); + nk_love_checkImage(L, 4, &image); use_image = 1; } enum nk_collapse_states state = NK_MINIMIZED; if (argc >= 5 && !lua_isnil(L, 5)) - state = nk_love_checkstate(5); + state = nk_love_checkstate(L, 5); lua_Debug ar; lua_getstack(L, 1, &ar); lua_getinfo(L, "l", &ar); @@ -2449,16 +2465,16 @@ static int nk_love_tree_push(lua_State *L) static int nk_love_tree_pop(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_tree_pop(&context->nkctx); return 0; } static int nk_love_tree(lua_State *L) { - nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); - nk_love_assert_argc(lua_gettop(L) >= 4); + nk_love_assert(L, lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(L, lua_gettop(L) >= 4); if (!lua_isfunction(L, -1)) luaL_typerror(L, lua_gettop(L), "function"); lua_pushvalue(L, 1); @@ -2483,19 +2499,19 @@ static int nk_love_tree(lua_State *L) static int nk_love_tree_state_push(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 3 && argc <= 5); - nk_love_assert_context(1); - enum nk_tree_type type = nk_love_checktree(2); + nk_love_assert_argc(L, argc >= 3 && argc <= 5); + nk_love_assert_context(L, 1); + enum nk_tree_type type = nk_love_checktree(L, 2); const char *title = luaL_checkstring(L, 3); struct nk_image image; int use_image = 0; if (argc >= 4 && !lua_isnil(L, 4)) { - nk_love_checkImage(4, &image); + nk_love_checkImage(L, 4, &image); use_image = 1; } enum nk_collapse_states state = NK_MINIMIZED; if (argc >= 5) - state = nk_love_checkstate(5); + state = nk_love_checkstate(L, 5); int open = 0; if (use_image) @@ -2508,16 +2524,16 @@ static int nk_love_tree_state_push(lua_State *L) static int nk_love_tree_state_pop(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_tree_state_pop(&context->nkctx); return 0; } static int nk_love_tree_state(lua_State *L) { - nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); - nk_love_assert_argc(lua_gettop(L) >= 4); + nk_love_assert(L, lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(L, lua_gettop(L) >= 4); if (!lua_isfunction(L, -1)) luaL_typerror(L, lua_gettop(L), "function"); lua_pushvalue(L, 1); @@ -2542,7 +2558,7 @@ static int nk_love_tree_state(lua_State *L) static int nk_love_color_rgba(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc == 3 || argc == 4); + nk_love_assert_argc(L, argc == 3 || argc == 4); int r = luaL_checkint(L, 1); int g = luaL_checkint(L, 2); int b = luaL_checkint(L, 3); @@ -2558,7 +2574,7 @@ static int nk_love_color_rgba(lua_State *L) static int nk_love_color_hsva(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc == 3 || argc == 4); + nk_love_assert_argc(L, argc == 3 || argc == 4); int h = NK_CLAMP(0, luaL_checkint(L, 1), 255); int s = NK_CLAMP(0, luaL_checkint(L, 2), 255); int v = NK_CLAMP(0, luaL_checkint(L, 3), 255); @@ -2574,8 +2590,8 @@ static int nk_love_color_hsva(lua_State *L) static int nk_love_color_parse_rgba(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - struct nk_color rgba = nk_love_checkcolor(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + struct nk_color rgba = nk_love_checkcolor(L, 1); lua_pushnumber(L, rgba.r); lua_pushnumber(L, rgba.g); lua_pushnumber(L, rgba.b); @@ -2585,8 +2601,8 @@ static int nk_love_color_parse_rgba(lua_State *L) static int nk_love_color_parse_hsva(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - struct nk_color rgba = nk_love_checkcolor(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + struct nk_color rgba = nk_love_checkcolor(L, 1); int h, s, v, a2; nk_color_hsva_i(&h, &s, &v, &a2, rgba); lua_pushnumber(L, h); @@ -2599,8 +2615,8 @@ static int nk_love_color_parse_hsva(lua_State *L) static int nk_love_label(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 2 && argc <= 4); - nk_love_assert_context(1); + nk_love_assert_argc(L, argc >= 2 && argc <= 4); + nk_love_assert_context(L, 1); const char *text = luaL_checkstring(L, 2); nk_flags align = NK_TEXT_LEFT; int wrap = 0; @@ -2611,9 +2627,9 @@ static int nk_love_label(lua_State *L) if (!strcmp(align_string, "wrap")) wrap = 1; else - align = nk_love_checkalign(3); + align = nk_love_checkalign(L, 3); if (argc >= 4) { - color = nk_love_checkcolor(4); + color = nk_love_checkcolor(L, 4); use_color = 1; } } @@ -2634,10 +2650,10 @@ static int nk_love_label(lua_State *L) static int nk_love_image(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc == 2 || argc == 6); - nk_love_assert_context(1); + nk_love_assert_argc(L, argc == 2 || argc == 6); + nk_love_assert_context(L, 1); struct nk_image image; - nk_love_checkImage(2, &image); + nk_love_checkImage(L, 2, &image); if (argc == 2) { nk_image(&context->nkctx, image); } else { @@ -2647,7 +2663,7 @@ static int nk_love_image(lua_State *L) float h = luaL_checknumber(L, 6); float line_thickness; struct nk_color color; - nk_love_getGraphics(&line_thickness, &color); + nk_love_getGraphics(L, &line_thickness, &color); nk_draw_image(&context->nkctx.current->buffer, nk_rect(x, y, w, h), &image, color); } return 0; @@ -2656,8 +2672,8 @@ static int nk_love_image(lua_State *L) static int nk_love_button(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 2 && argc <= 3); - nk_love_assert_context(1); + nk_love_assert_argc(L, argc >= 2 && argc <= 3); + nk_love_assert_context(L, 1); const char *title = NULL; if (!lua_isnil(L, 2)) title = luaL_checkstring(L, 2); @@ -2667,14 +2683,14 @@ static int nk_love_button(lua_State *L) struct nk_image image; if (argc >= 3 && !lua_isnil(L, 3)) { if (lua_isstring(L, 3)) { - if (nk_love_is_color(3)) { - color = nk_love_checkcolor(3); + if (nk_love_is_color(L, 3)) { + color = nk_love_checkcolor(L, 3); use_color = 1; } else { - symbol = nk_love_checksymbol(3); + symbol = nk_love_checksymbol(L, 3); } } else { - nk_love_checkImage(3, &image); + nk_love_checkImage(L, 3, &image); use_image = 1; } } @@ -2682,7 +2698,7 @@ static int nk_love_button(lua_State *L) int activated = 0; if (title != NULL) { if (use_color) - nk_love_assert(0, "%s: color buttons can't have titles"); + nk_love_assert(L, 0, "%s: color buttons can't have titles"); else if (symbol != NK_SYMBOL_NONE) activated = nk_button_symbol_label(&context->nkctx, symbol, title, align); else if (use_image) @@ -2697,7 +2713,7 @@ static int nk_love_button(lua_State *L) else if (use_image) activated = nk_button_image(&context->nkctx, image); else - nk_love_assert(0, "%s: must specify a title, color, symbol, and/or image"); + nk_love_assert(L, 0, "%s: must specify a title, color, symbol, and/or image"); } lua_pushboolean(L, activated); return 1; @@ -2705,34 +2721,34 @@ static int nk_love_button(lua_State *L) static int nk_love_button_set_behavior(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); - enum nk_button_behavior behavior = nk_love_checkbehavior(2); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_context(L, 1); + enum nk_button_behavior behavior = nk_love_checkbehavior(L, 2); nk_button_set_behavior(&context->nkctx, behavior); return 0; } static int nk_love_button_push_behavior(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); - enum nk_button_behavior behavior = nk_love_checkbehavior(2); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_context(L, 1); + enum nk_button_behavior behavior = nk_love_checkbehavior(L, 2); nk_button_push_behavior(&context->nkctx, behavior); return 0; } static int nk_love_button_pop_behavior(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_button_pop_behavior(&context->nkctx); return 0; } static int nk_love_checkbox(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 3); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 3); + nk_love_assert_context(L, 1); const char *text = luaL_checkstring(L, 2); if (lua_isboolean(L, 3)) { int value = lua_toboolean(L, 3); @@ -2758,8 +2774,8 @@ static int nk_love_checkbox(lua_State *L) static int nk_love_radio(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc == 3 || argc == 4); - nk_love_assert_context(1); + nk_love_assert_argc(L, argc == 3 || argc == 4); + nk_love_assert_context(L, 1); const char *name = luaL_checkstring(L, 2); const char *text = name; if (argc == 4) @@ -2793,18 +2809,18 @@ static int nk_love_radio(lua_State *L) static int nk_love_selectable(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 3 && argc <= 5); - nk_love_assert_context(1); + nk_love_assert_argc(L, argc >= 3 && argc <= 5); + nk_love_assert_context(L, 1); const char *text = luaL_checkstring(L, 2); struct nk_image image; int use_image = 0; if (argc >= 4 && !lua_isnil(L, 3)) { - nk_love_checkImage(3, &image); + nk_love_checkImage(L, 3, &image); use_image = 1; } nk_flags align = NK_TEXT_LEFT; if (argc >= 5) - align = nk_love_checkalign(4); + align = nk_love_checkalign(L, 4); if (lua_isboolean(L, -1)) { int value = lua_toboolean(L, -1); if (use_image) @@ -2835,8 +2851,8 @@ static int nk_love_selectable(lua_State *L) static int nk_love_slider(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 5); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 5); + nk_love_assert_context(L, 1); float min = luaL_checknumber(L, 2); float max = luaL_checknumber(L, 4); float step = luaL_checknumber(L, 5); @@ -2864,8 +2880,8 @@ static int nk_love_slider(lua_State *L) static int nk_love_progress(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 3 || argc <= 4); - nk_love_assert_context(1); + nk_love_assert_argc(L, argc >= 3 || argc <= 4); + nk_love_assert_context(L, 1); nk_size max = luaL_checklong(L, 3); int modifiable = 0; if (argc >= 4 && !lua_isnil(L, 4)) @@ -2894,13 +2910,13 @@ static int nk_love_progress(lua_State *L) static int nk_love_color_picker(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 2 && argc <= 3); - nk_love_assert_context(1); + nk_love_assert_argc(L, argc >= 2 && argc <= 3); + nk_love_assert_context(L, 1); enum nk_color_format format = NK_RGB; if (argc >= 3) - format = nk_love_checkcolorformat(3); + format = nk_love_checkcolorformat(L, 3); if (lua_isstring(L, 2)) { - struct nk_colorf color = nk_love_checkcolorf(2); + struct nk_colorf color = nk_love_checkcolorf(L, 2); color = nk_color_picker(&context->nkctx, color, format); char new_color_string[10]; nk_love_color((int) (color.r * 255), (int) (color.g * 255), @@ -2908,9 +2924,9 @@ static int nk_love_color_picker(lua_State *L) lua_pushstring(L, new_color_string); } else if (lua_istable(L, 2)) { lua_getfield(L, 2, "value"); - if (!nk_love_is_color(-1)) + if (!nk_love_is_color(L, -1)) luaL_argerror(L, 2, "should have a color string value"); - struct nk_colorf color = nk_love_checkcolorf(-1); + struct nk_colorf color = nk_love_checkcolorf(L, -1); int changed = nk_color_pick(&context->nkctx, &color, format); if (changed) { char new_color_string[10]; @@ -2928,8 +2944,8 @@ static int nk_love_color_picker(lua_State *L) static int nk_love_property(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 7); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 7); + nk_love_assert_context(L, 1); const char *name = luaL_checkstring(L, 2); double min = luaL_checknumber(L, 3); double max = luaL_checknumber(L, 5); @@ -2960,9 +2976,9 @@ static int nk_love_property(lua_State *L) static int nk_love_edit(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 3); - nk_love_assert_context(1); - nk_flags flags = nk_love_checkedittype(2); + nk_love_assert_argc(L, lua_gettop(L) == 3); + nk_love_assert_context(L, 1); + nk_flags flags = nk_love_checkedittype(L, 2); if (!lua_istable(L, 3)) luaL_typerror(L, 3, "table"); lua_getfield(L, 3, "value"); @@ -2995,32 +3011,32 @@ static int nk_love_edit(lua_State *L) int nk_love_edit_focus(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_edit_focus(&context->nkctx, NK_EDIT_DEFAULT); return 0; } int nk_love_edit_unfocus(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_edit_unfocus(&context->nkctx); return 0; } static int nk_love_popup_begin(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) >= 7); - nk_love_assert_context(1); - enum nk_popup_type type = nk_love_checkpopup(2); + nk_love_assert_argc(L, lua_gettop(L) >= 7); + nk_love_assert_context(L, 1); + enum nk_popup_type type = nk_love_checkpopup(L, 2); const char *title = luaL_checkstring(L, 3); struct nk_rect bounds; bounds.x = luaL_checknumber(L, 4); bounds.y = luaL_checknumber(L, 5); bounds.w = luaL_checknumber(L, 6); bounds.h = luaL_checknumber(L, 7); - nk_flags flags = nk_love_parse_window_flags(8, lua_gettop(L)); + nk_flags flags = nk_love_parse_window_flags(L, 8, lua_gettop(L)); int open = nk_popup_begin(&context->nkctx, type, title, flags, bounds); lua_pushboolean(L, open); return 1; @@ -3028,24 +3044,24 @@ static int nk_love_popup_begin(lua_State *L) static int nk_love_popup_close(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_popup_close(&context->nkctx); return 0; } static int nk_love_popup_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_popup_end(&context->nkctx); return 0; } static int nk_love_popup(lua_State *L) { - nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); - nk_love_assert_argc(lua_gettop(L) >= 8); + nk_love_assert(L, lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(L, lua_gettop(L) >= 8); if (!lua_isfunction(L, -1)) luaL_typerror(L, lua_gettop(L), "function"); lua_pushvalue(L, 1); @@ -3069,8 +3085,8 @@ static int nk_love_popup(lua_State *L) static int nk_love_popup_get_scroll(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_uint offset_x, offset_y; nk_popup_get_scroll(&context->nkctx, &offset_x, &offset_y); lua_pushinteger(L, offset_x); @@ -3080,8 +3096,8 @@ static int nk_love_popup_get_scroll(lua_State *L) static int nk_love_popup_set_scroll(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 3); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 3); + nk_love_assert_context(L, 1); nk_uint offset_x, offset_y; offset_x = luaL_checkinteger(L, 2); offset_y = luaL_checkinteger(L, 3); @@ -3092,10 +3108,10 @@ static int nk_love_popup_set_scroll(lua_State *L) static int nk_love_combobox(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 3 && argc <= 6); - nk_love_assert_context(1); + nk_love_assert_argc(L, argc >= 3 && argc <= 6); + nk_love_assert_context(L, 1); if (lua_isfunction(L, -1)) { - nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert(L, lua_checkstack(L, 3), "%s: failed to allocate stack space"); lua_pushvalue(L, 1); lua_insert(L, 2); lua_pushvalue(L, 1); @@ -3161,8 +3177,8 @@ static int nk_love_combobox(lua_State *L) static int nk_love_combobox_begin(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 2 && argc <= 5); - nk_love_assert_context(1); + nk_love_assert_argc(L, argc >= 2 && argc <= 5); + nk_love_assert_context(L, 1); const char *text = NULL; if (!lua_isnil(L, 2)) text = luaL_checkstring(L, 2); @@ -3173,14 +3189,14 @@ static int nk_love_combobox_begin(lua_State *L) int use_image = 0; if (argc >= 3 && !lua_isnil(L, 3)) { if (lua_isstring(L, 3)) { - if (nk_love_is_color(3)) { - color = nk_love_checkcolor(3); + if (nk_love_is_color(L, 3)) { + color = nk_love_checkcolor(L, 3); use_color = 1; } else { - symbol = nk_love_checksymbol(3); + symbol = nk_love_checksymbol(L, 3); } } else { - nk_love_checkImage(3, &image); + nk_love_checkImage(L, 3, &image); use_image = 1; } } @@ -3193,7 +3209,7 @@ static int nk_love_combobox_begin(lua_State *L) int open = 0; if (text != NULL) { if (use_color) - nk_love_assert(0, "%s: color comboboxes can't have titles"); + nk_love_assert(L, 0, "%s: color comboboxes can't have titles"); else if (symbol != NK_SYMBOL_NONE) open = nk_combo_begin_symbol_label(&context->nkctx, text, symbol, size); else if (use_image) @@ -3208,7 +3224,7 @@ static int nk_love_combobox_begin(lua_State *L) else if (use_image) open = nk_combo_begin_image(&context->nkctx, image, size); else - nk_love_assert(0, "%s: must specify color, symbol, image, and/or title"); + nk_love_assert(L, 0, "%s: must specify color, symbol, image, and/or title"); } lua_pushboolean(L, open); return 1; @@ -3217,23 +3233,23 @@ static int nk_love_combobox_begin(lua_State *L) static int nk_love_combobox_item(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 2 && argc <= 4); - nk_love_assert_context(1); + nk_love_assert_argc(L, argc >= 2 && argc <= 4); + nk_love_assert_context(L, 1); const char *text = luaL_checkstring(L, 2); enum nk_symbol_type symbol = NK_SYMBOL_NONE; struct nk_image image; int use_image = 0; if (argc >= 3 && !lua_isnil(L, 3)) { if (lua_isstring(L, 3)) { - symbol = nk_love_checksymbol(3); + symbol = nk_love_checksymbol(L, 3); } else { - nk_love_checkImage(3, &image); + nk_love_checkImage(L, 3, &image); use_image = 1; } } nk_flags align = NK_TEXT_LEFT; if (argc >= 4 && !lua_isnil(L, 4)) - align = nk_love_checkalign(4); + align = nk_love_checkalign(L, 4); int activated = 0; if (symbol != NK_SYMBOL_NONE) activated = nk_combo_item_symbol_label(&context->nkctx, symbol, text, align); @@ -3247,24 +3263,24 @@ static int nk_love_combobox_item(lua_State *L) static int nk_love_combobox_close(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_combo_close(&context->nkctx); return 0; } static int nk_love_combobox_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_combo_end(&context->nkctx); return 0; } static int nk_love_contextual_begin(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) >= 7); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) >= 7); + nk_love_assert_context(L, 1); struct nk_vec2 size; size.x = luaL_checknumber(L, 2); size.y = luaL_checknumber(L, 3); @@ -3273,7 +3289,7 @@ static int nk_love_contextual_begin(lua_State *L) trigger.y = luaL_checknumber(L, 5); trigger.w = luaL_checknumber(L, 6); trigger.h = luaL_checknumber(L, 7); - nk_flags flags = nk_love_parse_window_flags(8, lua_gettop(L)); + nk_flags flags = nk_love_parse_window_flags(L, 8, lua_gettop(L)); int open = nk_contextual_begin(&context->nkctx, flags, size, trigger); lua_pushboolean(L, open); return 1; @@ -3282,23 +3298,23 @@ static int nk_love_contextual_begin(lua_State *L) static int nk_love_contextual_item(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 2 && argc <= 4); - nk_love_assert_context(1); + nk_love_assert_argc(L, argc >= 2 && argc <= 4); + nk_love_assert_context(L, 1); const char *text = luaL_checkstring(L, 2); enum nk_symbol_type symbol = NK_SYMBOL_NONE; struct nk_image image; int use_image = 0; if (argc >= 3 && !lua_isnil(L, 3)) { if (lua_isstring(L, 3)) { - symbol = nk_love_checksymbol(3); + symbol = nk_love_checksymbol(L, 3); } else { - nk_love_checkImage(3, &image); + nk_love_checkImage(L, 3, &image); use_image = 1; } } nk_flags align = NK_TEXT_LEFT; if (argc >= 4 && !lua_isnil(L, 4)) - align = nk_love_checkalign(4); + align = nk_love_checkalign(L, 4); int activated; if (symbol != NK_SYMBOL_NONE) activated = nk_contextual_item_symbol_label(&context->nkctx, symbol, text, align); @@ -3312,24 +3328,24 @@ static int nk_love_contextual_item(lua_State *L) static int nk_love_contextual_close(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_contextual_close(&context->nkctx); return 0; } static int nk_love_contextual_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_contextual_end(&context->nkctx); return 0; } static int nk_love_contextual(lua_State *L) { - nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); - nk_love_assert_argc(lua_gettop(L) >= 8); + nk_love_assert(L, lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(L, lua_gettop(L) >= 8); if (!lua_isfunction(L, -1)) luaL_typerror(L, lua_gettop(L), "function"); lua_pushvalue(L, 1); @@ -3353,8 +3369,8 @@ static int nk_love_contextual(lua_State *L) static int nk_love_tooltip_begin(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_context(L, 1); float width = luaL_checknumber(L, 2); int open = nk_tooltip_begin(&context->nkctx, width); lua_pushnumber(L, open); @@ -3363,8 +3379,8 @@ static int nk_love_tooltip_begin(lua_State *L) static int nk_love_tooltip_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_tooltip_end(&context->nkctx); return 0; } @@ -3372,7 +3388,7 @@ static int nk_love_tooltip_end(lua_State *L) static int nk_love_tooltip(lua_State *L) { if (lua_gettop(L) == 3) { - nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert(L, lua_checkstack(L, 3), "%s: failed to allocate stack space"); if (!lua_isfunction(L, -1)) luaL_typerror(L, lua_gettop(L), "function"); lua_pushvalue(L, 1); @@ -3392,8 +3408,8 @@ static int nk_love_tooltip(lua_State *L) lua_call(L, 1, 0); } } else { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_context(L, 1); const char *text = luaL_checkstring(L, 2); nk_tooltip(&context->nkctx, text); } @@ -3402,24 +3418,24 @@ static int nk_love_tooltip(lua_State *L) static int nk_love_menubar_begin(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_menubar_begin(&context->nkctx); return 0; } static int nk_love_menubar_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_menubar_end(&context->nkctx); return 0; } static int nk_love_menubar(lua_State *L) { - nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); - nk_love_assert_argc(lua_gettop(L) == 2); + nk_love_assert(L, lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(L, lua_gettop(L) == 2); if (!lua_isfunction(L, -1)) luaL_typerror(L, lua_gettop(L), "function"); lua_pushvalue(L, 1); @@ -3440,16 +3456,16 @@ static int nk_love_menubar(lua_State *L) static int nk_love_menu_begin(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 5 && argc <= 6); - nk_love_assert_context(1); + nk_love_assert_argc(L, argc >= 5 && argc <= 6); + nk_love_assert_context(L, 1); const char *text = luaL_checkstring(L, 2); enum nk_symbol_type symbol = NK_SYMBOL_NONE; struct nk_image image; int use_image = 0; if (lua_isstring(L, 3)) { - symbol = nk_love_checksymbol(3); + symbol = nk_love_checksymbol(L, 3); } else if (!lua_isnil(L, 3)) { - nk_love_checkImage(3, &image); + nk_love_checkImage(L, 3, &image); use_image = 1; } struct nk_vec2 size; @@ -3457,7 +3473,7 @@ static int nk_love_menu_begin(lua_State *L) size.y = luaL_checknumber(L, 5); nk_flags align = NK_TEXT_LEFT; if (argc >= 6 && !lua_isnil(L, 6)) - align = nk_love_checkalign(6); + align = nk_love_checkalign(L, 6); int open; if (symbol != NK_SYMBOL_NONE) open = nk_menu_begin_symbol_label(&context->nkctx, text, align, symbol, size); @@ -3472,23 +3488,23 @@ static int nk_love_menu_begin(lua_State *L) static int nk_love_menu_item(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 2 && argc <= 4); - nk_love_assert_context(1); + nk_love_assert_argc(L, argc >= 2 && argc <= 4); + nk_love_assert_context(L, 1); const char *text = luaL_checkstring(L, 2); enum nk_symbol_type symbol = NK_SYMBOL_NONE; struct nk_image image; int use_image = 0; if (argc >= 3 && !lua_isnil(L, 3)) { if (lua_isstring(L, 3)) { - symbol = nk_love_checksymbol(3); + symbol = nk_love_checksymbol(L, 3); } else { - nk_love_checkImage(3, &image); + nk_love_checkImage(L, 3, &image); use_image = 1; } } nk_flags align = NK_TEXT_LEFT; if (argc >= 4 && !lua_isnil(L, 4)) - align = nk_love_checkalign(4); + align = nk_love_checkalign(L, 4); int activated; if (symbol != NK_SYMBOL_NONE) activated = nk_menu_item_symbol_label(&context->nkctx, symbol, text, align); @@ -3502,24 +3518,24 @@ static int nk_love_menu_item(lua_State *L) static int nk_love_menu_close(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_menu_close(&context->nkctx); return 0; } static int nk_love_menu_end(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); nk_menu_end(&context->nkctx); return 0; } static int nk_love_menu(lua_State *L) { - nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); - nk_love_assert_argc(lua_gettop(L) == 6 || lua_gettop(L) == 7); + nk_love_assert(L, lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(L, lua_gettop(L) == 6 || lua_gettop(L) == 7); if (!lua_isfunction(L, -1)) luaL_typerror(L, lua_gettop(L), "function"); lua_pushvalue(L, 1); @@ -3551,25 +3567,25 @@ static int nk_love_menu(lua_State *L) static int nk_love_style_default(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - struct nk_love_context *ctx = nk_love_checkcontext(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + struct nk_love_context *ctx = nk_love_checkcontext(L, 1); nk_style_default(&ctx->nkctx); return 0; } #define NK_LOVE_LOAD_COLOR(type) \ lua_getfield(L, -1, (type)); \ - if (!nk_love_is_color(-1)) { \ + if (!nk_love_is_color(L, -1)) { \ const char *msg = lua_pushfstring(L, "%%s: table missing color value for '%s'", type); \ - nk_love_assert(0, msg); \ + nk_love_assert(L, 0, msg); \ } \ - colors[index++] = nk_love_checkcolor(-1); \ + colors[index++] = nk_love_checkcolor(L, -1); \ lua_pop(L, 1) static int nk_love_style_load_colors(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - struct nk_love_context *ctx = nk_love_checkcontext(1); + nk_love_assert_argc(L, lua_gettop(L) == 2); + struct nk_love_context *ctx = nk_love_checkcontext(L, 1); if (!lua_istable(L, 2)) luaL_typerror(L, 2, "table"); struct nk_color colors[NK_COLOR_COUNT]; @@ -3608,21 +3624,21 @@ static int nk_love_style_load_colors(lua_State *L) static int nk_love_style_set_font(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - struct nk_love_context *ctx = nk_love_checkcontext(1); - nk_love_checkFont(2, &ctx->fonts[ctx->font_count]); + nk_love_assert_argc(L, lua_gettop(L) == 2); + struct nk_love_context *ctx = nk_love_checkcontext(L, 1); + nk_love_checkFont(L, 2, &ctx->fonts[ctx->font_count]); nk_style_set_font(&ctx->nkctx, &ctx->fonts[ctx->font_count++]); return 0; } -static int nk_love_style_push_color(struct nk_color *field) +static int nk_love_style_push_color(lua_State *L, struct nk_color *field) { - struct nk_love_context *ctx = nk_love_checkcontext(1); - if (!nk_love_is_color(-1)) { + struct nk_love_context *ctx = nk_love_checkcontext(L, 1); + if (!nk_love_is_color(L, -1)) { const char *msg = lua_pushfstring(L, "%%s: bad color string '%s'", lua_tostring(L, -1)); - nk_love_assert(0, msg); + nk_love_assert(L, 0, msg); } - struct nk_color color = nk_love_checkcolor(-1); + struct nk_color color = nk_love_checkcolor(L, -1); int success = nk_style_push_color(&ctx->nkctx, field, color); if (success) { lua_pushstring(L, "color"); @@ -3632,15 +3648,15 @@ static int nk_love_style_push_color(struct nk_color *field) return success; } -static int nk_love_style_push_vec2(struct nk_vec2 *field) +static int nk_love_style_push_vec2(lua_State *L, struct nk_vec2 *field) { - struct nk_love_context *ctx = nk_love_checkcontext(1); + struct nk_love_context *ctx = nk_love_checkcontext(L, 1); static const char *msg = "%s: vec2 fields must have x and y components"; - nk_love_assert(lua_istable(L, -1), msg); + nk_love_assert(L, lua_istable(L, -1), msg); lua_getfield(L, -1, "x"); - nk_love_assert(lua_isnumber(L, -1), msg); + nk_love_assert(L, lua_isnumber(L, -1), msg); lua_getfield(L, -2, "y"); - nk_love_assert(lua_isnumber(L, -1), msg); + nk_love_assert(L, lua_isnumber(L, -1), msg); struct nk_vec2 vec2; vec2.x = lua_tonumber(L, -2); vec2.y = lua_tonumber(L, -1); @@ -3654,20 +3670,20 @@ static int nk_love_style_push_vec2(struct nk_vec2 *field) return success; } -static int nk_love_style_push_item(struct nk_style_item *field) +static int nk_love_style_push_item(lua_State *L, struct nk_style_item *field) { - struct nk_love_context *ctx = nk_love_checkcontext(1); + struct nk_love_context *ctx = nk_love_checkcontext(L, 1); struct nk_style_item item; if (lua_isstring(L, -1)) { - if (!nk_love_is_color(-1)) { + if (!nk_love_is_color(L, -1)) { const char *msg = lua_pushfstring(L, "%%s: bad color string '%s'", lua_tostring(L, -1)); - nk_love_assert(0, msg); + nk_love_assert(L, 0, msg); } item.type = NK_STYLE_ITEM_COLOR; - item.data.color = nk_love_checkcolor(-1); + item.data.color = nk_love_checkcolor(L, -1); } else { item.type = NK_STYLE_ITEM_IMAGE; - nk_love_checkImage(-1, &item.data.image); + nk_love_checkImage(L, -1, &item.data.image); } int success = nk_style_push_style_item(&ctx->nkctx, field, item); if (success) { @@ -3678,10 +3694,10 @@ static int nk_love_style_push_item(struct nk_style_item *field) return success; } -static int nk_love_style_push_align(nk_flags *field) +static int nk_love_style_push_align(lua_State *L, nk_flags *field) { - struct nk_love_context *ctx = nk_love_checkcontext(1); - nk_flags align = nk_love_checkalign(-1); + struct nk_love_context *ctx = nk_love_checkcontext(L, 1); + nk_flags align = nk_love_checkalign(L, -1); int success = nk_style_push_flags(&ctx->nkctx, field, align); if (success) { lua_pushstring(L, "flags"); @@ -3691,9 +3707,9 @@ static int nk_love_style_push_align(nk_flags *field) return success; } -static int nk_love_style_push_float(float *field) +static int nk_love_style_push_float(lua_State *L, float *field) { - struct nk_love_context *ctx = nk_love_checkcontext(1); + struct nk_love_context *ctx = nk_love_checkcontext(L, 1); float f = luaL_checknumber(L, -1); int success = nk_style_push_float(&ctx->nkctx, field, f); if (success) { @@ -3704,10 +3720,10 @@ static int nk_love_style_push_float(float *field) return success; } -static int nk_love_style_push_font(const struct nk_user_font **field) +static int nk_love_style_push_font(lua_State *L, const struct nk_user_font **field) { - struct nk_love_context *ctx = nk_love_checkcontext(1); - nk_love_checkFont(-1, &context->fonts[context->font_count]); + struct nk_love_context *ctx = nk_love_checkcontext(L, 1); + nk_love_checkFont(L, -1, &context->fonts[context->font_count]); int success = nk_style_push_font(&ctx->nkctx, &context->fonts[context->font_count++]); if (success) { lua_pushstring(L, "font"); @@ -3718,22 +3734,22 @@ static int nk_love_style_push_font(const struct nk_user_font **field) } #define NK_LOVE_STYLE_PUSH(name, type, field) \ - nk_love_assert(lua_istable(L, -1), "%s: " name " field must be a table"); \ + nk_love_assert(L, lua_istable(L, -1), "%s: " name " field must be a table"); \ lua_getfield(L, -1, name); \ if (!lua_isnil(L, -1)) \ - nk_love_style_push_##type(field); \ + nk_love_style_push_##type(L, field); \ lua_pop(L, 1); -static void nk_love_style_push_text(struct nk_style_text *style) +static void nk_love_style_push_text(lua_State *L, struct nk_style_text *style) { - nk_love_assert(lua_istable(L, -1), "%s: text style must be a table"); + nk_love_assert(L, lua_istable(L, -1), "%s: text style must be a table"); NK_LOVE_STYLE_PUSH("color", color, &style->color); NK_LOVE_STYLE_PUSH("padding", vec2, &style->padding); } -static void nk_love_style_push_button(struct nk_style_button *style) +static void nk_love_style_push_button(lua_State *L, struct nk_style_button *style) { - nk_love_assert(lua_istable(L, -1), "%s: button style must be a table"); + nk_love_assert(L, lua_istable(L, -1), "%s: button style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("active", item, &style->active); @@ -3750,9 +3766,9 @@ static void nk_love_style_push_button(struct nk_style_button *style) NK_LOVE_STYLE_PUSH("touch padding", vec2, &style->touch_padding); } -static void nk_love_style_push_scrollbar(struct nk_style_scrollbar *style) +static void nk_love_style_push_scrollbar(lua_State *L, struct nk_style_scrollbar *style) { - nk_love_assert(lua_istable(L, -1), "%s: scrollbar style must be a table"); + nk_love_assert(L, lua_istable(L, -1), "%s: scrollbar style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("active", item, &style->active); @@ -3768,9 +3784,9 @@ static void nk_love_style_push_scrollbar(struct nk_style_scrollbar *style) NK_LOVE_STYLE_PUSH("padding", vec2, &style->padding); } -static void nk_love_style_push_edit(struct nk_style_edit *style) +static void nk_love_style_push_edit(lua_State *L, struct nk_style_edit *style) { - nk_love_assert(lua_istable(L, -1), "%s: edit style must be a table"); + nk_love_assert(L, lua_istable(L, -1), "%s: edit style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("active", item, &style->active); @@ -3795,9 +3811,9 @@ static void nk_love_style_push_edit(struct nk_style_edit *style) NK_LOVE_STYLE_PUSH("row padding", float, &style->row_padding); } -static void nk_love_style_push_toggle(struct nk_style_toggle *style) +static void nk_love_style_push_toggle(lua_State *L, struct nk_style_toggle *style) { - nk_love_assert(lua_istable(L, -1), "%s: toggle style must be a table"); + nk_love_assert(L, lua_istable(L, -1), "%s: toggle style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("active", item, &style->active); @@ -3815,9 +3831,9 @@ static void nk_love_style_push_toggle(struct nk_style_toggle *style) NK_LOVE_STYLE_PUSH("border", float, &style->border); } -static void nk_love_style_push_selectable(struct nk_style_selectable *style) +static void nk_love_style_push_selectable(lua_State *L, struct nk_style_selectable *style) { - nk_love_assert(lua_istable(L, -1), "%s: selectable style must be a table"); + nk_love_assert(L, lua_istable(L, -1), "%s: selectable style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("pressed", item, &style->pressed); @@ -3838,9 +3854,9 @@ static void nk_love_style_push_selectable(struct nk_style_selectable *style) NK_LOVE_STYLE_PUSH("image padding", vec2, &style->image_padding); } -static void nk_love_style_push_slider(struct nk_style_slider *style) +static void nk_love_style_push_slider(lua_State *L, struct nk_style_slider *style) { - nk_love_assert(lua_istable(L, -1), "%s: slider style must be a table"); + nk_love_assert(L, lua_istable(L, -1), "%s: slider style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("active", item, &style->active); @@ -3859,9 +3875,9 @@ static void nk_love_style_push_slider(struct nk_style_slider *style) NK_LOVE_STYLE_PUSH("cursor size", vec2, &style->cursor_size); } -static void nk_love_style_push_progress(struct nk_style_progress *style) +static void nk_love_style_push_progress(lua_State *L, struct nk_style_progress *style) { - nk_love_assert(lua_istable(L, -1), "%s: progress style must be a table"); + nk_love_assert(L, lua_istable(L, -1), "%s: progress style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("active", item, &style->active); @@ -3877,9 +3893,9 @@ static void nk_love_style_push_progress(struct nk_style_progress *style) NK_LOVE_STYLE_PUSH("padding", vec2, &style->padding); } -static void nk_love_style_push_property(struct nk_style_property *style) +static void nk_love_style_push_property(lua_State *L, struct nk_style_property *style) { - nk_love_assert(lua_istable(L, -1), "%s: property style must be a table"); + nk_love_assert(L, lua_istable(L, -1), "%s: property style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("active", item, &style->active); @@ -3895,9 +3911,9 @@ static void nk_love_style_push_property(struct nk_style_property *style) NK_LOVE_STYLE_PUSH("dec button", button, &style->dec_button); } -static void nk_love_style_push_chart(struct nk_style_chart *style) +static void nk_love_style_push_chart(lua_State *L, struct nk_style_chart *style) { - nk_love_assert(lua_istable(L, -1), "%s: chart style must be a table"); + nk_love_assert(L, lua_istable(L, -1), "%s: chart style must be a table"); NK_LOVE_STYLE_PUSH("background", item, &style->background); NK_LOVE_STYLE_PUSH("border color", color, &style->border_color); NK_LOVE_STYLE_PUSH("selected color", color, &style->selected_color); @@ -3907,9 +3923,9 @@ static void nk_love_style_push_chart(struct nk_style_chart *style) NK_LOVE_STYLE_PUSH("padding", vec2, &style->padding); } -static void nk_love_style_push_tab(struct nk_style_tab *style) +static void nk_love_style_push_tab(lua_State *L, struct nk_style_tab *style) { - nk_love_assert(lua_istable(L, -1), "%s: tab style must be a table"); + nk_love_assert(L, lua_istable(L, -1), "%s: tab style must be a table"); NK_LOVE_STYLE_PUSH("background", item, &style->background); NK_LOVE_STYLE_PUSH("border color", color, &style->border_color); NK_LOVE_STYLE_PUSH("text", color, &style->text); @@ -3924,9 +3940,9 @@ static void nk_love_style_push_tab(struct nk_style_tab *style) NK_LOVE_STYLE_PUSH("spacing", vec2, &style->spacing); } -static void nk_love_style_push_combo(struct nk_style_combo *style) +static void nk_love_style_push_combo(lua_State *L, struct nk_style_combo *style) { - nk_love_assert(lua_istable(L, -1), "%s: combo style must be a table"); + nk_love_assert(L, lua_istable(L, -1), "%s: combo style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("active", item, &style->active); @@ -3945,9 +3961,9 @@ static void nk_love_style_push_combo(struct nk_style_combo *style) NK_LOVE_STYLE_PUSH("spacing", vec2, &style->spacing); } -static void nk_love_style_push_window_header(struct nk_style_window_header *style) +static void nk_love_style_push_window_header(lua_State *L, struct nk_style_window_header *style) { - nk_love_assert(lua_istable(L, -1), "%s: window header style must be a table"); + nk_love_assert(L, lua_istable(L, -1), "%s: window header style must be a table"); NK_LOVE_STYLE_PUSH("normal", item, &style->normal); NK_LOVE_STYLE_PUSH("hover", item, &style->hover); NK_LOVE_STYLE_PUSH("active", item, &style->active); @@ -3961,9 +3977,9 @@ static void nk_love_style_push_window_header(struct nk_style_window_header *styl NK_LOVE_STYLE_PUSH("spacing", vec2, &style->spacing); } -static void nk_love_style_push_window(struct nk_style_window *style) +static void nk_love_style_push_window(lua_State *L, struct nk_style_window *style) { - nk_love_assert(lua_istable(L, -1), "%s: window style must be a table"); + nk_love_assert(L, lua_istable(L, -1), "%s: window style must be a table"); NK_LOVE_STYLE_PUSH("header", window_header, &style->header); NK_LOVE_STYLE_PUSH("fixed background", item, &style->fixed_background); NK_LOVE_STYLE_PUSH("background", color, &style->background); @@ -3997,8 +4013,8 @@ static void nk_love_style_push_window(struct nk_style_window *style) static int nk_love_style_push(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - struct nk_love_context *ctx = nk_love_checkcontext(1); + nk_love_assert_argc(L, lua_gettop(L) == 2); + struct nk_love_context *ctx = nk_love_checkcontext(L, 1); if (!lua_istable(L, 2)) luaL_typerror(L, 2, "table"); int outOfContext = (context == NULL); @@ -4039,8 +4055,8 @@ static int nk_love_style_push(lua_State *L) static int nk_love_style_pop(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - struct nk_love_context *ctx = nk_love_checkcontext(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + struct nk_love_context *ctx = nk_love_checkcontext(L, 1); int outOfContext = (context == NULL); if (outOfContext) context = ctx; @@ -4071,7 +4087,7 @@ static int nk_love_style_pop(lua_State *L) nk_style_pop_font(&ctx->nkctx); } else { const char *msg = lua_pushfstring(L, "%%s: bad style item type '%s'", lua_tostring(L, -1)); - nk_love_assert(0, msg); + nk_love_assert(L, 0, msg); } lua_pop(L, 1); } @@ -4082,8 +4098,8 @@ static int nk_love_style_pop(lua_State *L) static int nk_love_style(lua_State *L) { - nk_love_assert(lua_checkstack(L, 3), "%s: failed to allocate stack space"); - nk_love_assert_argc(lua_gettop(L) == 3); + nk_love_assert(L, lua_checkstack(L, 3), "%s: failed to allocate stack space"); + nk_love_assert_argc(L, lua_gettop(L) == 3); if (!lua_isfunction(L, -1)) luaL_typerror(L, lua_gettop(L), "function"); lua_pushvalue(L, 1); @@ -4111,8 +4127,8 @@ static int nk_love_style(lua_State *L) static int nk_love_widget_bounds(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); struct nk_rect bounds = nk_widget_bounds(&context->nkctx); lua_pushnumber(L, bounds.x); lua_pushnumber(L, bounds.y); @@ -4123,8 +4139,8 @@ static int nk_love_widget_bounds(lua_State *L) static int nk_love_widget_position(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); struct nk_vec2 pos = nk_widget_position(&context->nkctx); lua_pushnumber(L, pos.x); lua_pushnumber(L, pos.y); @@ -4133,8 +4149,8 @@ static int nk_love_widget_position(lua_State *L) static int nk_love_widget_size(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); struct nk_vec2 pos = nk_widget_size(&context->nkctx); lua_pushnumber(L, pos.x); lua_pushnumber(L, pos.y); @@ -4143,8 +4159,8 @@ static int nk_love_widget_size(lua_State *L) static int nk_love_widget_width(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); float width = nk_widget_width(&context->nkctx); lua_pushnumber(L, width); return 1; @@ -4152,8 +4168,8 @@ static int nk_love_widget_width(lua_State *L) static int nk_love_widget_height(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); float height = nk_widget_height(&context->nkctx); lua_pushnumber(L, height); return 1; @@ -4161,8 +4177,8 @@ static int nk_love_widget_height(lua_State *L) static int nk_love_widget_is_hovered(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 1); + nk_love_assert_context(L, 1); int hovered = nk_widget_is_hovered(&context->nkctx); lua_pushboolean(L, hovered); return 1; @@ -4171,11 +4187,11 @@ static int nk_love_widget_is_hovered(lua_State *L) static int nk_love_widget_has_mouse(lua_State *L, int down) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 1 && argc <= 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, argc >= 1 && argc <= 2); + nk_love_assert_context(L, 1); enum nk_buttons button = NK_BUTTON_LEFT; if (argc >= 2 && !lua_isnil(L, 2)) - button = nk_love_checkbutton(2); + button = nk_love_checkbutton(L, 2); int ret = nk_widget_has_mouse_click_down(&context->nkctx, button, down); lua_pushboolean(L, ret); return 1; @@ -4194,11 +4210,11 @@ static int nk_love_widget_has_mouse_released(lua_State *L) static int nk_love_widget_is_mouse(lua_State *L, int down) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 1 && argc <= 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, argc >= 1 && argc <= 2); + nk_love_assert_context(L, 1); enum nk_buttons button = NK_BUTTON_LEFT; if (argc >= 2 && !lua_isnil(L, 2)) - button = nk_love_checkbutton(2); + button = nk_love_checkbutton(L, 2); struct nk_rect bounds = nk_widget_bounds(&context->nkctx); int ret = nk_input_is_mouse_click_down_in_rect(&context->nkctx.input, button, bounds, down); lua_pushboolean(L, ret); @@ -4217,8 +4233,8 @@ static int nk_love_widget_is_mouse_released(lua_State *L) static int nk_love_spacing(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 2); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 2); + nk_love_assert_context(L, 1); int cols = luaL_checkint(L, 2); nk_spacing(&context->nkctx, cols); return 0; @@ -4227,24 +4243,24 @@ static int nk_love_spacing(lua_State *L) static int nk_love_line(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 5 && argc % 2 == 1); - nk_love_assert_context(1); + nk_love_assert_argc(L, argc >= 5 && argc % 2 == 1); + nk_love_assert_context(L, 1); int i; for (i = 0; i < argc - 1; ++i) { - nk_love_assert(lua_isnumber(L, i + 2), "%s: point coordinates should be numbers"); + nk_love_assert(L, lua_isnumber(L, i + 2), "%s: point coordinates should be numbers"); points[i] = lua_tonumber(L, i + 2); } float line_thickness; struct nk_color color; - nk_love_getGraphics(&line_thickness, &color); + nk_love_getGraphics(L, &line_thickness, &color); nk_stroke_polyline(&context->nkctx.current->buffer, points, (argc - 1) / 2, line_thickness, color); return 0; } static int nk_love_curve(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 9); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 9); + nk_love_assert_context(L, 1); int i; float ax = luaL_checknumber(L, 2); float ay = luaL_checknumber(L, 3); @@ -4256,7 +4272,7 @@ static int nk_love_curve(lua_State *L) float by = luaL_checknumber(L, 9); float line_thickness; struct nk_color color; - nk_love_getGraphics(&line_thickness, &color); + nk_love_getGraphics(L, &line_thickness, &color); nk_stroke_curve(&context->nkctx.current->buffer, ax, ay, ctrl0x, ctrl0y, ctrl1x, ctrl1y, bx, by, line_thickness, color); return 0; } @@ -4264,17 +4280,17 @@ static int nk_love_curve(lua_State *L) static int nk_love_polygon(lua_State *L) { int argc = lua_gettop(L); - nk_love_assert_argc(argc >= 8 && argc % 2 == 0); - nk_love_assert_context(1); - enum nk_love_draw_mode mode = nk_love_checkdraw(2); + nk_love_assert_argc(L, argc >= 8 && argc % 2 == 0); + nk_love_assert_context(L, 1); + enum nk_love_draw_mode mode = nk_love_checkdraw(L, 2); int i; for (i = 0; i < argc - 2; ++i) { - nk_love_assert(lua_isnumber(L, i + 3), "%s: point coordinates should be numbers"); + nk_love_assert(L, lua_isnumber(L, i + 3), "%s: point coordinates should be numbers"); points[i] = lua_tonumber(L, i + 3); } float line_thickness; struct nk_color color; - nk_love_getGraphics(&line_thickness, &color); + nk_love_getGraphics(L, &line_thickness, &color); if (mode == NK_LOVE_FILL) nk_fill_polygon(&context->nkctx.current->buffer, points, (argc - 2) / 2, color); else if (mode == NK_LOVE_LINE) @@ -4284,15 +4300,15 @@ static int nk_love_polygon(lua_State *L) static int nk_love_circle(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 5); - nk_love_assert_context(1); - enum nk_love_draw_mode mode = nk_love_checkdraw(2); + nk_love_assert_argc(L, lua_gettop(L) == 5); + nk_love_assert_context(L, 1); + enum nk_love_draw_mode mode = nk_love_checkdraw(L, 2); float x = luaL_checknumber(L, 3); float y = luaL_checknumber(L, 4); float r = luaL_checknumber(L, 5); float line_thickness; struct nk_color color; - nk_love_getGraphics(&line_thickness, &color); + nk_love_getGraphics(L, &line_thickness, &color); if (mode == NK_LOVE_FILL) nk_fill_circle(&context->nkctx.current->buffer, nk_rect(x - r, y - r, r * 2, r * 2), color); else if (mode == NK_LOVE_LINE) @@ -4302,16 +4318,16 @@ static int nk_love_circle(lua_State *L) static int nk_love_ellipse(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 6); - nk_love_assert_context(1); - enum nk_love_draw_mode mode = nk_love_checkdraw(2); + nk_love_assert_argc(L, lua_gettop(L) == 6); + nk_love_assert_context(L, 1); + enum nk_love_draw_mode mode = nk_love_checkdraw(L, 2); float x = luaL_checknumber(L, 3); float y = luaL_checknumber(L, 4); float rx = luaL_checknumber(L, 5); float ry = luaL_checknumber(L, 6); float line_thickness; struct nk_color color; - nk_love_getGraphics(&line_thickness, &color); + nk_love_getGraphics(L, &line_thickness, &color); if (mode == NK_LOVE_FILL) nk_fill_circle(&context->nkctx.current->buffer, nk_rect(x - rx, y - ry, rx * 2, ry * 2), color); else if (mode == NK_LOVE_LINE) @@ -4321,9 +4337,9 @@ static int nk_love_ellipse(lua_State *L) static int nk_love_arc(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 7); - nk_love_assert_context(1); - enum nk_love_draw_mode mode = nk_love_checkdraw(2); + nk_love_assert_argc(L, lua_gettop(L) == 7); + nk_love_assert_context(L, 1); + enum nk_love_draw_mode mode = nk_love_checkdraw(L, 2); float cx = luaL_checknumber(L, 3); float cy = luaL_checknumber(L, 4); float r = luaL_checknumber(L, 5); @@ -4331,7 +4347,7 @@ static int nk_love_arc(lua_State *L) float a1 = luaL_checknumber(L, 7); float line_thickness; struct nk_color color; - nk_love_getGraphics(&line_thickness, &color); + nk_love_getGraphics(L, &line_thickness, &color); if (mode == NK_LOVE_FILL) nk_fill_arc(&context->nkctx.current->buffer, cx, cy, r, a0, a1, color); else if (mode == NK_LOVE_LINE) @@ -4341,24 +4357,24 @@ static int nk_love_arc(lua_State *L) static int nk_love_rect_multi_color(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 9); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 9); + nk_love_assert_context(L, 1); float x = luaL_checknumber(L, 2); float y = luaL_checknumber(L, 3); float w = luaL_checknumber(L, 4); float h = luaL_checknumber(L, 5); - struct nk_color topLeft = nk_love_checkcolor(6); - struct nk_color topRight = nk_love_checkcolor(7); - struct nk_color bottomLeft = nk_love_checkcolor(8); - struct nk_color bottomRight = nk_love_checkcolor(9); + struct nk_color topLeft = nk_love_checkcolor(L, 6); + struct nk_color topRight = nk_love_checkcolor(L, 7); + struct nk_color bottomLeft = nk_love_checkcolor(L, 8); + struct nk_color bottomRight = nk_love_checkcolor(L, 9); nk_fill_rect_multi_color(&context->nkctx.current->buffer, nk_rect(x, y, w, h), topLeft, topRight, bottomLeft, bottomRight); return 0; } static int nk_love_push_scissor(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 5); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 5); + nk_love_assert_context(L, 1); float x = luaL_checknumber(L, 2); float y = luaL_checknumber(L, 3); float w = luaL_checknumber(L, 4); @@ -4369,8 +4385,8 @@ static int nk_love_push_scissor(lua_State *L) static int nk_love_text(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 6); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 6); + nk_love_assert_context(L, 1); const char *text = luaL_checkstring(L, 2); float x = luaL_checknumber(L, 3); float y = luaL_checknumber(L, 4); @@ -4380,20 +4396,20 @@ static int nk_love_text(lua_State *L) lua_getfield(L, -1, "graphics"); lua_getfield(L, -1, "getFont"); lua_call(L, 0, 1); - nk_love_checkFont(-1, &context->fonts[context->font_count]); + nk_love_checkFont(L, -1, &context->fonts[context->font_count]); float line_thickness; struct nk_color color; - nk_love_getGraphics(&line_thickness, &color); + nk_love_getGraphics(L, &line_thickness, &color); nk_draw_text(&context->nkctx.current->buffer, nk_rect(x, y, w, h), text, strlen(text), &context->fonts[context->font_count++], nk_rgba(0, 0, 0, 0), color); return 0; } -static int nk_love_input_has_mouse(int down) +static int nk_love_input_has_mouse(lua_State *L, int down) { int argc = lua_gettop(L); - nk_love_assert_argc(argc == 6); - nk_love_assert_context(1); - enum nk_buttons button = nk_love_checkbutton(2); + nk_love_assert_argc(L, argc == 6); + nk_love_assert_context(L, 1); + enum nk_buttons button = nk_love_checkbutton(L, 2); float x = luaL_checknumber(L, 3); float y = luaL_checknumber(L, 4); float w = luaL_checknumber(L, 5); @@ -4405,20 +4421,20 @@ static int nk_love_input_has_mouse(int down) static int nk_love_input_has_mouse_pressed(lua_State *L) { - return nk_love_input_has_mouse(nk_true); + return nk_love_input_has_mouse(L, nk_true); } static int nk_love_input_has_mouse_released(lua_State *L) { - return nk_love_input_has_mouse(nk_false); + return nk_love_input_has_mouse(L, nk_false); } -static int nk_love_input_is_mouse(int down) +static int nk_love_input_is_mouse(lua_State *L, int down) { int argc = lua_gettop(L); - nk_love_assert_argc(argc == 6); - nk_love_assert_context(1); - enum nk_buttons button = nk_love_checkbutton(2); + nk_love_assert_argc(L, argc == 6); + nk_love_assert_context(L, 1); + enum nk_buttons button = nk_love_checkbutton(L, 2); float x = luaL_checknumber(L, 3); float y = luaL_checknumber(L, 4); float w = luaL_checknumber(L, 5); @@ -4430,18 +4446,18 @@ static int nk_love_input_is_mouse(int down) static int nk_love_input_is_mouse_pressed(lua_State *L) { - return nk_love_input_is_mouse(nk_true); + return nk_love_input_is_mouse(L, nk_true); } static int nk_love_input_is_mouse_released(lua_State *L) { - return nk_love_input_is_mouse(nk_false); + return nk_love_input_is_mouse(L, nk_false); } static int nk_love_input_was_hovered(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 5); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 5); + nk_love_assert_context(L, 1); float x = luaL_checknumber(L, 2); float y = luaL_checknumber(L, 3); float w = luaL_checknumber(L, 4); @@ -4453,8 +4469,8 @@ static int nk_love_input_was_hovered(lua_State *L) static int nk_love_input_is_hovered(lua_State *L) { - nk_love_assert_argc(lua_gettop(L) == 5); - nk_love_assert_context(1); + nk_love_assert_argc(L, lua_gettop(L) == 5); + nk_love_assert_context(L, 1); float x = luaL_checknumber(L, 2); float y = luaL_checknumber(L, 3); float w = luaL_checknumber(L, 4); @@ -4476,19 +4492,18 @@ static int nk_love_input_is_hovered(lua_State *L) lua_pushcfunction(L, func); \ lua_setfield(L, -2, name) -LUALIB_API int luaopen_nuklear(lua_State *luaState) +LUALIB_API int luaopen_nuklear(lua_State *L) { - L = luaState; - edit_buffer = nk_love_malloc(NK_LOVE_EDIT_BUFFER_LEN); - combobox_items = nk_love_malloc(sizeof(char*) * NK_LOVE_COMBOBOX_MAX_ITEMS); - points = nk_love_malloc(sizeof(float) * NK_LOVE_MAX_POINTS * 2); + edit_buffer = nk_love_malloc(L, NK_LOVE_EDIT_BUFFER_LEN); + combobox_items = nk_love_malloc(L, sizeof(char*) * NK_LOVE_COMBOBOX_MAX_ITEMS); + points = nk_love_malloc(L, sizeof(float) * NK_LOVE_MAX_POINTS * 2); lua_newtable(L); lua_pushvalue(L, -1); lua_setfield(L, LUA_REGISTRYINDEX, "nuklear"); lua_getglobal(L, "love"); - nk_love_assert(lua_istable(L, -1), "LOVE-Nuklear requires LOVE environment"); + nk_love_assert(L, lua_istable(L, -1), "LOVE-Nuklear requires LOVE environment"); lua_pop(L, 1); lua_newtable(L); From 1b8058071ee7a2a39ca82c629f82f383f27d5cdb Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Sun, 27 Aug 2023 11:35:54 -0400 Subject: [PATCH 50/52] Add key repeat to example --- example/main.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/example/main.lua b/example/main.lua index 5fd2010..e335370 100644 --- a/example/main.lua +++ b/example/main.lua @@ -14,6 +14,7 @@ local transform = require 'transform' local ui1, ui2 function love.load() + love.keyboard.setKeyRepeat(true) ui1, ui2 = nuklear.newUI(), nuklear.newUI() end From 8d092eefa43a2ac58c69bf3638a236d95eb2db89 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Sun, 27 Aug 2023 11:37:31 -0400 Subject: [PATCH 51/52] Update Nuklear version to latest --- src/nuklear | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nuklear b/src/nuklear index adc52d7..614abce 160000 --- a/src/nuklear +++ b/src/nuklear @@ -1 +1 @@ -Subproject commit adc52d710fe3c87194b99f540c53e82eb75c2521 +Subproject commit 614abce05b9455849bbf1519b7f86e53c78b04ab From 5336818494a0b80fd2ae3ce2d04c2e1bb9f9d9d3 Mon Sep 17 00:00:00 2001 From: Kevin Harrison Date: Mon, 10 Feb 2025 19:32:28 -0500 Subject: [PATCH 52/52] Fix #62 --- src/nuklear_love.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/nuklear_love.c b/src/nuklear_love.c index a740913..67d8380 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -46,12 +46,12 @@ static char *edit_buffer; static const char **combobox_items; static float *points; -static struct nk_love_handle { +struct nk_love_handle { lua_State *L; int ref; }; -static struct nk_love_font { +struct nk_love_font { struct nk_user_font font; struct nk_love_handle handle; }; @@ -1536,7 +1536,7 @@ static int nk_love_frame_begin(lua_State *L) lua_rawgeti(L, -1, love_handle->ref); nk_love_checkFont(L, -1, &context->fonts[context->font_count]); lua_pop(L, 1); - context->nkctx.style.font = &context->fonts[context->font_count++]; + context->nkctx.style.font = &context->fonts[context->font_count++].font; int i; for (i = 0; i < context->nkctx.stacks.fonts.head; ++i) { struct nk_config_stack_user_font_element *element = &context->nkctx.stacks.fonts.elements[i]; @@ -1544,7 +1544,7 @@ static int nk_love_frame_begin(lua_State *L) lua_rawgeti(L, -1, love_handle->ref); nk_love_checkFont(L, -1, &context->fonts[context->font_count]); lua_pop(L, 1); - context->nkctx.stacks.fonts.elements[i].old_value = &context->fonts[context->font_count++]; + context->nkctx.stacks.fonts.elements[i].old_value = &context->fonts[context->font_count++].font; } lua_pop(L, 1); context->layout_ratio_count = 0; @@ -3627,7 +3627,7 @@ static int nk_love_style_set_font(lua_State *L) nk_love_assert_argc(L, lua_gettop(L) == 2); struct nk_love_context *ctx = nk_love_checkcontext(L, 1); nk_love_checkFont(L, 2, &ctx->fonts[ctx->font_count]); - nk_style_set_font(&ctx->nkctx, &ctx->fonts[ctx->font_count++]); + nk_style_set_font(&ctx->nkctx, &ctx->fonts[ctx->font_count++].font); return 0; } @@ -3724,7 +3724,7 @@ static int nk_love_style_push_font(lua_State *L, const struct nk_user_font **fie { struct nk_love_context *ctx = nk_love_checkcontext(L, 1); nk_love_checkFont(L, -1, &context->fonts[context->font_count]); - int success = nk_style_push_font(&ctx->nkctx, &context->fonts[context->font_count++]); + int success = nk_style_push_font(&ctx->nkctx, &context->fonts[context->font_count++].font); if (success) { lua_pushstring(L, "font"); size_t stack_size = lua_objlen(L, 2); @@ -4400,7 +4400,7 @@ static int nk_love_text(lua_State *L) float line_thickness; struct nk_color color; nk_love_getGraphics(L, &line_thickness, &color); - nk_draw_text(&context->nkctx.current->buffer, nk_rect(x, y, w, h), text, strlen(text), &context->fonts[context->font_count++], nk_rgba(0, 0, 0, 0), color); + nk_draw_text(&context->nkctx.current->buffer, nk_rect(x, y, w, h), text, strlen(text), &context->fonts[context->font_count++].font, nk_rgba(0, 0, 0, 0), color); return 0; }