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 0c4783d..d0381a1 100644 Binary files a/example/skin/button.png and b/example/skin/button.png differ diff --git a/example/skin/button_active.png b/example/skin/button_active.png deleted file mode 100644 index ba73495..0000000 Binary files a/example/skin/button_active.png and /dev/null differ diff --git a/example/skin/button_hover.png b/example/skin/button_hover.png deleted file mode 100644 index dc3b728..0000000 Binary files a/example/skin/button_hover.png and /dev/null differ diff --git a/example/skin/checkbox.png b/example/skin/checkbox.png new file mode 100644 index 0000000..fd04d56 Binary files /dev/null and b/example/skin/checkbox.png differ diff --git a/example/skin/checkbox_false.png b/example/skin/checkbox_false.png deleted file mode 100644 index df85f65..0000000 Binary files a/example/skin/checkbox_false.png and /dev/null differ diff --git a/example/skin/checkbox_true.png b/example/skin/checkbox_true.png deleted file mode 100644 index 8efc225..0000000 Binary files a/example/skin/checkbox_true.png and /dev/null differ diff --git a/src/nuklear_love.c b/src/nuklear_love.c index b7c5da8..b1637ba 100644 --- a/src/nuklear_love.c +++ b/src/nuklear_love.c @@ -184,24 +184,42 @@ 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"); + if (nk_love_is_type(index, "Image")) { + lua_getglobal(L, "love"); + lua_getfield(L, -1, "graphics"); + lua_getfield(L, -1, "newQuad"); + lua_pushnumber(L, 0); + lua_pushnumber(L, 0); + lua_getfield(L, index, "getDimensions"); + lua_pushvalue(L, index); + lua_call(L, 1, 2); + lua_pushvalue(L, -2); + lua_pushvalue(L, -2); + lua_call(L, 6, 1); + lua_newtable(L); + lua_pushvalue(L, index); + lua_rawseti(L, -2, 1); + lua_replace(L, -3); + lua_rawseti(L, -2, 2); + lua_replace(L, -2); + } else if (lua_istable(L, index)) { + 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")) { + lua_rawseti(L, -3, 1); + lua_rawseti(L, -2, 2); + } else { + luaL_argerror(L, index, "expecting {Image, Quad}"); + } + } else { + luaL_argerror(L, index, "expecting Image or {Image, Quad}"); + } nk_love_pushregistry("image"); - lua_pushvalue(L, index); + lua_pushvalue(L, -2); 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, 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); }