From 8eab7d3d351a07be46647029cca428f594ff8ed2 Mon Sep 17 00:00:00 2001 From: qfx Date: Mon, 17 Feb 2020 02:38:07 +0200 Subject: [PATCH] New input subscription model, nested elements work better with love translation operations --- core/element.lua | 28 +++++++++++++++++++++++++--- core/input.lua | 21 ++++++++++++++------- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/core/element.lua b/core/element.lua index 0cb61d8..23e1daf 100755 --- a/core/element.lua +++ b/core/element.lua @@ -63,6 +63,23 @@ end local 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 setmetatable(element,{ __call = function(cls, ...) @@ -127,6 +144,9 @@ function element:new(w, h, param) self.baseView[index] = val self.context:bubbleUpdate() self:updateInputCtx() + if self.view.onChange then + self.view.onChange() + end end end }) @@ -147,6 +167,8 @@ function element:new(w, h, param) self.settings.restrictView = true return self.view end + + self:setup() end function element:updateInputCtx() @@ -232,7 +254,10 @@ function element:classlessRender() self.inputContext:set() if type(self.renderer)=='function' then + love.graphics.push() + love.graphics.origin() local status, err = pcall(self.renderer) + love.graphics.pop() if not status then setColor(1,0,0) @@ -308,9 +333,6 @@ function element:draw(x, y) if y then self.view.y = y end end - if not self.settings.isSetup then - self:setup() - end if activeContext then self:externalRender() diff --git a/core/input.lua b/core/input.lua index c363874..4ddb78e 100755 --- a/core/input.lua +++ b/core/input.lua @@ -136,7 +136,6 @@ function subscription.create(x, y, w, h, eventType, callback, doff) active = doff or true, callback = callback },subscription) - activeContext.subs[#activeContext.subs+1] = sub else sub = setmetatable({ @@ -154,7 +153,7 @@ function subscription.create(x, y, w, h, eventType, callback, doff) input.subscriptions[eventType] = {} end - table.insert(input.subscriptions[eventType],sub) + input.subscriptions[eventType][#input.subscriptions[eventType]+1] = sub return sub end @@ -211,16 +210,24 @@ function subscription:checkOutside(x, y) end input.subscribe = subscription.create - ---Since the introduction of the relative subscriptions, there is more utility in ommiting coordinates by default -setmetatable(input, {__call = function(eventType, callback, cbOff, x, y, w, h) +---@param eventType string +---@param callback function +---@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 y = y or 0 w = w 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) -end}) +--Since the introduction of the relative subscriptions, there is more utility in ommiting coordinates by default +setmetatable(input, input) function input.eventHandlers.mousereleased(x, y, btn) local captured = false