Add Quad support for images

This commit is contained in:
Kevin Harrison 2018-12-14 14:46:02 -05:00
parent 06114619e6
commit 8f7926039b
8 changed files with 61 additions and 36 deletions

View file

@ -1,28 +1,34 @@
-- Basic skinning example. -- Basic skinning example.
local windowHeader = love.graphics.newImage 'skin/window_header.png' local windowHeader = love.graphics.newImage 'skin/window_header.png'
local checkboxSkin = love.graphics.newImage 'skin/checkbox_false.png' local windowBody = love.graphics.newImage 'skin/window.png'
local checkboxCheck = love.graphics.newImage 'skin/checkbox_true.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 = { local style = {
['text'] = { ['text'] = {
['color'] = '#000000' ['color'] = '#000000'
}, },
['button'] = { ['button'] = {
['normal'] = love.graphics.newImage 'skin/button.png', ['normal'] = buttonNormal,
['hover'] = love.graphics.newImage 'skin/button_hover.png', ['hover'] = buttonHover,
['active'] = love.graphics.newImage 'skin/button_active.png', ['active'] = buttonActive,
['text background'] = '#00000000', ['text background'] = '#00000000',
['text normal'] = '#000000', ['text normal'] = '#000000',
['text hover'] = '#000000', ['text hover'] = '#000000',
['text active'] = '#ffffff' ['text active'] = '#ffffff'
}, },
['checkbox'] = { ['checkbox'] = {
['normal'] = checkboxSkin, ['normal'] = checkboxOff,
['hover'] = checkboxSkin, ['hover'] = checkboxOff,
['active'] = checkboxSkin, ['active'] = checkboxOff,
['cursor normal'] = checkboxCheck, ['cursor normal'] = checkboxOn,
['cursor hover'] = checkboxCheck, ['cursor hover'] = checkboxOn,
['text normal'] = '#000000', ['text normal'] = '#000000',
['text hover'] = '#000000', ['text hover'] = '#000000',
['text active'] = '#000000', ['text active'] = '#000000',
@ -38,7 +44,7 @@ local style = {
['label active'] = '#000000', ['label active'] = '#000000',
['label padding'] = {x = 10, y = 8} ['label padding'] = {x = 10, y = 8}
}, },
['fixed background'] = love.graphics.newImage 'skin/window.png', ['fixed background'] = windowBody,
['background'] = '#d3ceaa' ['background'] = '#d3ceaa'
} }
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

BIN
example/skin/checkbox.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

View file

@ -184,24 +184,42 @@ static void nk_love_checkImage(int index, struct nk_image *image)
{ {
if (index < 0) if (index < 0)
index += lua_gettop(L) + 1; index += lua_gettop(L) + 1;
if (!nk_love_is_type(index, "Image")) if (nk_love_is_type(index, "Image")) {
luaL_typerror(L, 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"); nk_love_pushregistry("image");
lua_pushvalue(L, index); lua_pushvalue(L, -2);
int ref = luaL_ref(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->handle = nk_handle_id(ref);
image->w = width; lua_pop(L, 2);
image->h = height;
image->region[0] = 0;
image->region[1] = 0;
image->region[2] = width;
image->region[3] = height;
lua_pop(L, 3);
} }
static int nk_love_is_hex(char c) 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"); lua_getfield(L, -1, "draw");
nk_love_pushregistry("image"); nk_love_pushregistry("image");
lua_rawgeti(L, -1, image.handle.id); 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_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, x);
lua_pushnumber(L, y); lua_pushnumber(L, y);
lua_pushnumber(L, 0); lua_pushnumber(L, 0);
lua_pushnumber(L, (double) w / image.region[2]); lua_getfield(L, -4, "getViewport");
lua_pushnumber(L, (double) h / image.region[3]); 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_call(L, 7, 0);
lua_pop(L, 1); lua_pop(L, 1);
} }