diff options
-rw-r--r-- | JGUI/JGUI.lua | 89 | ||||
-rw-r--r-- | JGUI/a.png | bin | 0 -> 4794 bytes | |||
-rw-r--r-- | JGUI/b.png | bin | 0 -> 6050 bytes | |||
-rw-r--r-- | JGUI/config.lua | 4 | ||||
-rw-r--r-- | JGUI/main.lua | 34 | ||||
-rw-r--r-- | shader/main.lua | 49 |
6 files changed, 112 insertions, 64 deletions
diff --git a/JGUI/JGUI.lua b/JGUI/JGUI.lua index 6f82896..998a810 100644 --- a/JGUI/JGUI.lua +++ b/JGUI/JGUI.lua @@ -1,36 +1,103 @@ - -- panel local JGUI = {} +local contain = function(x, y, box) + return x >= box.x and x <= box.x + box.width + and y >= box.y and y <= box.y + box.height +end + local Widget = { -- common properties name, -- name - x, y, width, height, -- boundary + x = 0, y = 0, width = 0, height = 0, -- boundary swallow, -- swallow event or not by default -- event callbacks onClick, -- clicked callback onHover, -- hover callback - -- common functions + -- onEvent, } -function Widget.onEvent(clicked, ) - +Widget.__index = Widget +function Widget.onEvent(w, e) + if e.type == "MouseButtonDown" then + if e.button == "Left" then + if contain(e.x, e.y, {x = w.x, y = w.y, width = w.width, height = w.height}) then + w.onClick() + end + end + elseif e.type == "MouseButtonUp" then + if e.button == "Left" then + if contain(e.x, e.y, {x = w.x, y = w.y, width = w.width, height = w.height}) then + w.onRelease() + end + end + end +end +function Widget.setSize(widget, w, h) + widget.width = w + widget.height = h +end +function Widget.setPosition(widget, x, y) + widget.x = x + widget.y = y end local Panel = { widgets = {}, -} + x = 0, + y = 0, + width = 0, + height = 0, + goon = true, +} +Panel.__index = Panel +function Panel.onEvent(p, e) + for _, w in pairs(p.widgets) do + if goon then + w:onEvent(e) + else + break + end + end + goon = true +end +function Panel.onUpdate(p, dt) + +end +function Panel.setSize(p, w, h) + p.width = w + p.height = h +end +function Panel.add(p, w) + table.insert(p.widgets, w) +end + +local Button = {} +Button.__index = Button +setmetatable(Button, Widget) ------------------------------------ -- ------------------------------------ -JGUI.newButton = function() - +JGUI.newButton = function(name, ...) + local b = {} + b.name = name + setmetatable(b, Button) + if b.init then + b.init(...) + end + return b end -JGUI.newPanel = function() - +JGUI.newPanel = function(name, ...) + local p = {} + p.name = name + setmetatable(p, Panel) + if p.init then + p.init(...) + end + return p end - +return JGUI
\ No newline at end of file diff --git a/JGUI/a.png b/JGUI/a.png Binary files differnew file mode 100644 index 0000000..e31d924 --- /dev/null +++ b/JGUI/a.png diff --git a/JGUI/b.png b/JGUI/b.png Binary files differnew file mode 100644 index 0000000..8fba9dc --- /dev/null +++ b/JGUI/b.png diff --git a/JGUI/config.lua b/JGUI/config.lua new file mode 100644 index 0000000..9e930e1 --- /dev/null +++ b/JGUI/config.lua @@ -0,0 +1,4 @@ +return +{ + resizable = true, +}
\ No newline at end of file diff --git a/JGUI/main.lua b/JGUI/main.lua index 93fea92..1cb7b9c 100644 --- a/JGUI/main.lua +++ b/JGUI/main.lua @@ -1,19 +1,45 @@ +io.stdout:setvbuf("no") local JGUI = require("JGUI") +local panel = JGUI.newPanel("Panel") +panel:setSize(300, 300) +local button = JGUI.newButton("test") +button:setSize(200, 50) +button:setPosition(0, 0) +panel:add(button) +local b1 = jin.graphics.newBitmap("a.png") +local b2 = jin.graphics.newBitmap("b.png") +local bt1 = jin.graphics.newTexture(b1) +local bt2 = jin.graphics.newTexture(b2) +b1 = nil +b2 = nil +local bt = bt1 +button.onClick = function() + print("click") + bt = bt2 +end +button.onRelease = function() + bt = bt1 +end function jin.core.onLoad() end function jin.core.onEvent(e) - if e.type == "quit" then + if e.type == "Quit" then + jin.core.stop() + end + if e.type == "KeyDown" and e.key == "Escape" then jin.core.stop() end + panel:onEvent(e) end -function jin.core.onUpdate() - +function jin.core.onUpdate(dt) + panel:onUpdate(dt) end function jin.core.onDraw() - + -- jin.graphics.unbindCanvas() + jin.graphics.draw(bt, 0, button.y) end diff --git a/shader/main.lua b/shader/main.lua index 39679b8..8b13789 100644 --- a/shader/main.lua +++ b/shader/main.lua @@ -1,50 +1 @@ -io.stdout:setvbuf("no") -local shader -local img -local img2 -local canvas -local sw, sh = jin.graphics.getSize() -function jin.core.onLoad() - local str = jin.filesystem.read("metaball.shader") - shader = jin.graphics.newShader(str) - local bitmap = jin.graphics.newBitmap("img.png") - local blackImg = jin.graphics.newBitmap(20, 20) - -- local bitmap2 = jin.graphics.newBitmap("img2.bmp") - img = jin.graphics.newTexture(blackImg) - -- img2 = jin.graphics.newTexture(bitmap2) - canvas = jin.graphics.newCanvas(200, 200) -end --- extern vec3 iResolution; --- extern number iGlobalTime; --- extern vec4 iMouse; -local mx, my = 0, 0 -function jin.core.onEvent(e) - if e.type == "Quit" then - jin.core.stop() - end - if e.type == "KeyDown" then - if e.key == "Escape" then - jin.core.stop() - end - end - if e.type == "MouseMotion" then - -- if e.button == "left" then - mx = e.x - my = e.y - -- end - end -end -local dt = 0 -function jin.core.onDraw() - dt = dt + 0.1 - jin.graphics.bindCanvas(canvas) - jin.graphics.useShader(shader) - shader:sendNumber("iGlobalTime", dt ) - shader:sendVec3("iResolution", sw, sh, 1) - shader:sendVec4("iMouse", mx, my, mx, my) - jin.graphics.draw(img, 0, 0, 1, 1) - jin.graphics.unuseShader() - jin.graphics.unbindCanvas() - jin.graphics.draw(canvas, 0, 0, 5, 5) -end
\ No newline at end of file |