add table library; fix lineends and tabs; some fixes in widget(table.remove instead of table[n] = nil); a test "every-frame rect render" for gui

This commit is contained in:
triplefox 2012-12-03 22:53:10 -08:00
parent 79fe7d2ab6
commit b26085efb4
6 changed files with 365 additions and 345 deletions

View File

@ -23,6 +23,3 @@ Basic goals:
Checkbox
Frame + clipping
Color picker
**************************************Make sure this is a unix-format textfile**************************

4
lua.c
View File

@ -163,11 +163,13 @@ void icelua_loadbasefuncs(lua_State *L)
lua_pushcfunction(L, luaopen_base);
lua_call(L, 0, 0);
// here's the other two
// here's the other three
lua_pushcfunction(L, luaopen_string);
lua_call(L, 0, 0);
lua_pushcfunction(L, luaopen_math);
lua_call(L, 0, 0);
lua_pushcfunction(L, luaopen_table);
lua_call(L, 0, 0);
// overwrite dofile/loadfile.
lua_pushcfunction(L, icelua_fn_base_loadfile);

View File

@ -601,6 +601,7 @@ function client.hook_render()
if players and players[players.current] then
players[players.current].show_hud()
end
gui_rect_frame_test()
end
client.hook_tick = h_tick_init

View File

@ -1,4 +1,9 @@
-- look into actual drawing functionality!~
-- the client drawing code requires manual memory management:
-- we have to allocate a buffer and draw pixels to it
-- this code can't deal with that problem...
-- lib_gui will have to provide a layer that takes the abstract APIs here and adds drawing functions
-- on top. the abstract API can assist by adding a "dirty" flag so that cache management is straightforward.
-- sketch listener and collision system:
-- rect and layers detection (derive layers from hierarchy)
@ -11,6 +16,7 @@
-- 3. iterate through the children, moving them to the positions and sizes desired as specified by the packing mode.
-- also something to note - when we draw we have to pass a clip rectangle upwards so that scrolling is possible.
-- this isn't strictly necessary, but if the possibility is there, use it!
local P = {}
@ -65,7 +71,7 @@ function P.widget(options)
function this.l() return this.relx() end
function this.t() return this.rely() end
function this.r() return this.relx() + this.width() end
function this.b() return this.rely() + this.bottom() end
function this.b() return this.rely() + this.height() end
function this.cx() return this.relx() + this.width() * 0.5 end
function this.cy() return this.rely() + this.height() * 0.5 end
@ -78,7 +84,23 @@ function P.widget(options)
width=r-l, height=b-t, cx=l+(r-l)*0.5, cy=t+(b-t)*0.5}
end
function this.detach() if this.parent then this.parent.children[this] = nil; this.parent = nil end end
function this.aabb(x, y, w, h)
return not (this.l()>x or this.r()<x+w or this.t()>y or this.b()<y+h)
end
function this.collide(x, y, w, h)
-- very simple aabb collision for mousing. returns the "first and deepest child".
w = w or 1
h = h or 1
local hit = this.aabb(x, y, w, h)
local result = this
for k, v in pairs(this.children) do
result = v.collide(x, y, w, h) or this
end
return result
end
function this.detach() if this.parent then table.remove(this.parent.children, this) this.parent = nil end end
function this.add_child(child) child.detach(); child.parent = this; this.children[child] = child end
function this.remove_child(child) child.detach() end
function this.remove_all_children() for k,child in pairs(this.children) do this.remove_child(child) end end
@ -92,32 +114,17 @@ function P.widget(options)
return this
end
function P.widget_frame(x, y, width, height)
local w = widget{x=x, y=y, width=width, height=height}
-- presumably this is a drawing widget.
-- the key distinction between this API and the Flash monstrosity I made is that I can readily mix
-- drawing widgets and non-drawing widgets.
-- so then, if a packer is just another child, that means that it can inherit the size of the parent's inner.
-- but what the children "see" reported is what the packer wants to make available.
-- bottom-up traversal. Now I'm remembering this part.
-- most custom widgets use their reported width and height, and that's that, but packers don't.
return w
end
if _REQUIREDNAME == nil then
widgets = P
else
_G[_REQUIREDNAME] = P
end
local test = P.widget{x=100, y=100}
local test = P.widget{x=100, y=100, width=100, height=100}
print(test)
local test2 = P.widget{x=100, y=100}
print(test2)
local test2 = P.widget{x=100, y=100, width=100, height=100}
test2.set_parent(test)
print(test2)
print(test.collide(150,150))
return P

View File

@ -105,4 +105,17 @@ function gui_string_edit(str, maxlen, key, modif)
return str
end
function gui_rect_frame_test()
-- someday this will grow up to be a real rectangle renderer
local img = common.img_new(32, 32)
for x = 0, 31, 1 do
for y = 0, 31, 1 do
common.img_pixel_set(img, x, y, 0xFFFF0000)
end
end
client.img_blit(img, 0, 0)
common.img_free(img)
end
end