beholder.lua/beholder.lua

92 lines
2.9 KiB
Lua
Raw Normal View History

-- beholder.lua - v1.0 (2011-11)
-- requires middleclass 2.0
-- Copyright (c) 2011 Enrique García Cota
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-- Based on YaciCode, from Julien Patte and LuaObject, from Sebastien Rocca-Serra
2011-10-25 22:18:17 +00:00
local beholder = {}
local function copy(t)
local c={}
for i=1,#t do c[i]=t[i] end
return c
end
2011-10-26 22:22:52 +00:00
2011-10-29 01:07:19 +00:00
local function extractEventAndActionFromParams(params)
local action = table.remove(params, #params)
return params, action
2011-10-26 22:22:52 +00:00
end
local function findNodeById(self, id)
return self._nodesById[id]
end
local function findOrCreateNode(self, event)
local current = self._root
local key
for i=1, #event do
key = event[i]
current.children[key] = current.children[key] or {actions={},children={}}
current = current.children[key]
end
return current
2011-10-26 22:22:52 +00:00
end
local function executeNodeActions(self, event)
local current = self._root
local counter = 0
local params = copy(event)
local key
for i=1,#event do
key = event[i]
current = current.children[key]
if not current then break end
table.remove(params, 1)
for _,action in pairs(current.actions) do
action(unpack(params))
counter = counter + 1
end
end
return counter > 0 and counter
2011-10-26 22:22:52 +00:00
end
local function addActionToNode(self, node, action)
2011-10-26 22:22:52 +00:00
local id = {}
2011-10-26 22:27:21 +00:00
node.actions[id] = action
self._nodesById[id] = node
2011-10-26 22:22:52 +00:00
return id
end
local function removeActionFromNode(node, id)
if not node then return false end
node.actions[id] = nil
return true
end
function beholder:reset()
self._root = { actions={}, children={} }
self._nodesById = setmetatable({}, {__mode="k"})
end
function beholder:observe(...)
2011-10-29 01:07:19 +00:00
local event, action = extractEventAndActionFromParams({...})
return addActionToNode(self, findOrCreateNode(self, event), action)
end
function beholder:stopObserving(id)
return removeActionFromNode(findNodeById(self, id), id)
end
function beholder:trigger(...)
2011-10-29 01:07:19 +00:00
return executeNodeActions(self, {...})
end
2011-10-25 22:18:17 +00:00
beholder:reset()
return beholder