mirror of
https://github.com/AR2000AR/openComputers_codes.git
synced 2025-09-08 14:41:14 +02:00
add touch capability
This commit is contained in:
36
libGUI/libGUI/Screen.lua
Normal file
36
libGUI/libGUI/Screen.lua
Normal file
@@ -0,0 +1,36 @@
|
||||
local libClass = require("libClass")
|
||||
local Widget = require("libGUI/widget/Widget")
|
||||
local Rectangle = require("libGUI/widget/Rectangle")
|
||||
|
||||
local Screen = libClass.newClass("Screen")
|
||||
Screen.childs = {}
|
||||
Screen.addChild = function(self,child)
|
||||
if(not child.class) then
|
||||
error("arg #2 is not a class",2)
|
||||
elseif(not libClass.instanceOf(child,Widget)) then
|
||||
error("arg #2 is not a Widget",2)
|
||||
else
|
||||
table.insert(self.childs,child)
|
||||
end
|
||||
end
|
||||
Screen.trigger = function(self,...) self.private.clickHandler(self,...) end
|
||||
Screen.private = {}
|
||||
Screen.private.clickHandler = function(self,eventName,uuid,x,y)
|
||||
print(eventName,x,y)
|
||||
if(eventName == "touch") then --filter only "touch" events
|
||||
print("ok touch")
|
||||
for _,widget in ipairs(self.childs) do
|
||||
print("collide ",widget:collide(x,y))
|
||||
if(widget:collide(x,y)) then --test colision
|
||||
widget:trigger(eventName,uuid,x,y)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Screen.draw = function(self)
|
||||
for _,widget in ipairs(self.childs) do
|
||||
widget:draw()
|
||||
end
|
||||
end
|
||||
|
||||
return Screen
|
@@ -13,6 +13,12 @@ Rectangle.getHeight = function(self) return self.private.height end
|
||||
Rectangle.getSize = function(self) return self:getWidth(), self:getHeight() end
|
||||
Rectangle.getColor = function(self) return self.private.color end
|
||||
Rectangle.constructor = function(self,x,y,width,height,color) self:setWidth(width) self:setHeight(height) self:setColor(color) end
|
||||
Rectangle.collide = function(self,x,y)
|
||||
local wx1,wy1 = self:getPos()
|
||||
local wx2 = self:getX()+self:getWidth()-1
|
||||
local wy2 = self:getY()+self:getHeight()-1
|
||||
return ((x-wx1)*(wx2-x) >= 0 and (y-wy1)*(wy2-y) >= 0)
|
||||
end
|
||||
Rectangle.draw = function(self)
|
||||
local bk = gpu.setBackground(self:getColor())
|
||||
gpu.fill(self:getX(),self:getY(),self:getWidth(),self:getHeight()," ")
|
||||
|
@@ -2,7 +2,7 @@ local function emptyCallback(self) end
|
||||
|
||||
Widget = require("libClass").newClass("Widget")
|
||||
Widget.type = "Widget"
|
||||
Widget.private = {id = "", x = 1, y = 1, callback = emptyCallback}
|
||||
Widget.private = {x = 1, y = 1, callback = emptyCallback}
|
||||
Widget.setPos = function(self,x,y) self:setX(x) self:setY(y) end
|
||||
Widget.setX = function(self,x) self.private.x = x or self:getX() end
|
||||
Widget.setY = function(self,y) self.private.y = y or self:getY() end
|
||||
@@ -11,8 +11,11 @@ Widget.getX = function(self) return self.private.x end
|
||||
Widget.getY = function(self) return self.private.y end
|
||||
Widget.getPos = function(self) return self:getX(), self:getY() end
|
||||
Widget.getId = function(self) return self.private.id end
|
||||
Widget.trigger = function(self,widgetId) --call the callback function
|
||||
pcall(function()if(widgetId == self:getId()) then self.private:callback() end end)
|
||||
Widget.trigger = function(self,...) --call the callback function
|
||||
self.private.callback(self,...)
|
||||
end
|
||||
Widget.collide = function(self,x,y)
|
||||
return (x == self:getX() and y == self:getY())
|
||||
end
|
||||
Widget.constructor = function(self,x,y) self:setPos(x,y) self.private.id = require("uuid").next() end
|
||||
|
||||
|
@@ -7,6 +7,7 @@ local libGUI = {widget={}}
|
||||
libGUI.widget.Widget = require("libGUI/widget/Widget")
|
||||
libGUI.widget.Rectangle = require("libGUI/widget/Rectangle")
|
||||
libGUI.widget.Image = require("libGUI/widget/Image")
|
||||
libGUI.Screen = require("libGUI/Screen")
|
||||
libGUI.Image = require("libGUI/Image")
|
||||
|
||||
return libGUI
|
||||
|
Reference in New Issue
Block a user