New input subscription model, nested elements work better with love translation operations

This commit is contained in:
qfx 2020-02-17 02:38:07 +02:00
parent 4e021c80b8
commit 8eab7d3d35
2 changed files with 39 additions and 10 deletions

View File

@ -63,6 +63,23 @@ end
local element = {} local element = {}
element.__index = element element.__index = element
function element.newProxy(base)
local base = base or {}
local fakeBase = {}
local activeContext = activeContext
return setmetatable({},{
__index = function(t, index)
return fakeBase[index] or base[index]
end,
__newindex = function(t, index, val)
if fakeBase[index] ~= val then
fakeBase[index] = val
activeContext:bubbleUpdate()
end
end
})
end
local type,pcall = type,pcall local type,pcall = type,pcall
setmetatable(element,{ setmetatable(element,{
__call = function(cls, ...) __call = function(cls, ...)
@ -127,6 +144,9 @@ function element:new(w, h, param)
self.baseView[index] = val self.baseView[index] = val
self.context:bubbleUpdate() self.context:bubbleUpdate()
self:updateInputCtx() self:updateInputCtx()
if self.view.onChange then
self.view.onChange()
end
end end
end end
}) })
@ -147,6 +167,8 @@ function element:new(w, h, param)
self.settings.restrictView = true self.settings.restrictView = true
return self.view return self.view
end end
self:setup()
end end
function element:updateInputCtx() function element:updateInputCtx()
@ -232,7 +254,10 @@ function element:classlessRender()
self.inputContext:set() self.inputContext:set()
if type(self.renderer)=='function' then if type(self.renderer)=='function' then
love.graphics.push()
love.graphics.origin()
local status, err = pcall(self.renderer) local status, err = pcall(self.renderer)
love.graphics.pop()
if not status then if not status then
setColor(1,0,0) setColor(1,0,0)
@ -308,9 +333,6 @@ function element:draw(x, y)
if y then self.view.y = y end if y then self.view.y = y end
end end
if not self.settings.isSetup then
self:setup()
end
if activeContext then if activeContext then
self:externalRender() self:externalRender()

View File

@ -136,7 +136,6 @@ function subscription.create(x, y, w, h, eventType, callback, doff)
active = doff or true, active = doff or true,
callback = callback callback = callback
},subscription) },subscription)
activeContext.subs[#activeContext.subs+1] = sub activeContext.subs[#activeContext.subs+1] = sub
else else
sub = setmetatable({ sub = setmetatable({
@ -154,7 +153,7 @@ function subscription.create(x, y, w, h, eventType, callback, doff)
input.subscriptions[eventType] = {} input.subscriptions[eventType] = {}
end end
table.insert(input.subscriptions[eventType],sub) input.subscriptions[eventType][#input.subscriptions[eventType]+1] = sub
return sub return sub
end end
@ -211,16 +210,24 @@ function subscription:checkOutside(x, y)
end end
input.subscribe = subscription.create input.subscribe = subscription.create
---@param eventType string
--Since the introduction of the relative subscriptions, there is more utility in ommiting coordinates by default ---@param callback function
setmetatable(input, {__call = function(eventType, callback, cbOff, x, y, w, h) ---@param cbOff boolean
---@param x number
---@param y number
---@param w number
---@param h number
input.__call = function(self, eventType, callback, cbOff, x, y, w, h)
x = x or 0 x = x or 0
y = y or 0 y = y or 0
w = w or 1 w = w or 1
h = h or 1 h = h or 1
return subscription.create(x,y,w,h,eventType,callback,cbOff)
end
subscription.create(x,y,w,h,eventType,callback,cbOff) --Since the introduction of the relative subscriptions, there is more utility in ommiting coordinates by default
end}) setmetatable(input, input)
function input.eventHandlers.mousereleased(x, y, btn) function input.eventHandlers.mousereleased(x, y, btn)
local captured = false local captured = false