local path = string.sub(..., 1, string.len(...) - string.len(".core.input")) local stack = require(path .. ".core.stack") local input = { eventHandlers = {}, subscriptions = {}, activeEvents = {} } input.__index = input local function sortFunc(t1, t2) if t1.stack.temporalZ.z == t2.stack.temporalZ.z then print('same Z ???',t1.stack.temporalZ.z, t2.stack.temporalZ.z) end if t1 == t2 then print(tostring(t1), tostring(t2)) return false end return t1.stack.temporalZ.z > t2.stack.temporalZ.z end function input.sortZ() for i, subs in pairs(input.subscriptions) do table.sort(subs, sortFunc) print(#subs) end end local dummyfunc = function() end ---@class subscription local subscription = {} subscription.__index = subscription function input.unload() input.subscriptions = {} input.activeEvents = {} end --[[Event types ###SIMPLE EVENTS### mousepressed,--press started mousereleased,--press released after an event inside started mousepressed_outside --mousepressed outside of the subscription mousereleased_outside --mousereleased outside of the subscription keypressed,--key pressed keyreleased,--key released ###COMPLEX EVENTS### dragged, clicked, hover, ]] ---@param x number ---@param y number ---@param w number ---@param h number ---@param eventType string ---@param callback function ---@param doff boolean ---@return subscription function subscription.create(x, y, w, h, eventType, callback, doff) local stack = stack.getContext() local xn, yn = stack:normalizePos(x, y) local wn, hn = stack:normalizeSize(w, h) local sub = setmetatable({ x = xn, y = yn, w = wn, h = hn, origX = x, origY = y, origW = w, origH = h, eventType = eventType, active = true, stack = stack, callback = callback },subscription) sub.onSizeChange = stack:onSizeChange(function() sub.w, sub.h = sub.stack:normalizeSize(sub.origW, sub.origH) end) sub.onPosChange = stack:onPosChange(function() sub.x, sub.y = sub.stack:normalizePos(sub.origX, sub.origY) print(sub.y, sub.stack.absY) end) if doff == false then sub:off() end if not input.subscriptions[eventType] then input.subscriptions[eventType] = {} end table.insert(input.subscriptions[eventType],1,sub) input.sortZ() return sub end function subscription:off() self.active = false end function subscription:on() self.active = true end function subscription:remove() --input.subscriptions[self.eventType][self] = nil input.sortZ() end function subscription:emit(...) return self.callback(...) end function subscription:checkInside(x, y) return x>self.x and xself.y and yself.x and xself.y and y