/
stateful.lua
192 lines (153 loc) · 6.27 KB
/
stateful.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
-- stateful.lua - v1.0.3 (2014-10)
-- 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
local Stateful = { static = {} }
local _callbacks = {
enteredState = 1,
exitedState = 1,
pushedState = 1,
poppedState = 1,
pausedState = 1,
continuedState = 1
}
local _BaseState = {}
local function _addStatesToClass(klass, superStates)
klass.static.states = {}
for stateName, state in pairs(superStates or {}) do
klass:addState(stateName,state)
end
end
local function _getStatefulMethod(instance, name)
if not _callbacks[name] then
local stack = rawget(instance, '__stateStack')
if not stack then return end
for i = #stack, 1, -1 do
if stack[i][name] then return stack[i][name] end
end
end
end
local function _getNewInstanceIndex(prevIndex)
if type(prevIndex) == 'function' then
return function(instance, name) return _getStatefulMethod(instance, name) or prevIndex(instance, name) end
end
return function(instance, name) return _getStatefulMethod(instance, name) or prevIndex[name] end
end
local function _getNewAllocateMethod(oldAllocateMethod)
return function(klass, ...)
local instance = oldAllocateMethod(klass, ...)
instance.__stateStack = {}
return instance
end
end
local function _modifyInstanceIndex(klass)
klass.__instanceDict.__index = _getNewInstanceIndex(klass.__instanceDict.__index)
end
local function _getNewSubclassMethod(prevSubclass)
return function(klass, name)
local subclass = prevSubclass(klass, name)
_addStatesToClass(subclass, klass.states)
_modifyInstanceIndex(subclass)
return subclass
end
end
local function _modifySubclassMethod(klass)
klass.static.subclass = _getNewSubclassMethod(klass.static.subclass)
end
local function _modifyAllocateMethod(klass)
klass.static.allocate = _getNewAllocateMethod(klass.static.allocate)
end
local function _assertType(val, name, expected_type, type_to_s)
assert(type(val) == expected_type, "Expected " .. name .. " to be of type " .. (type_to_s or expected_type) .. ". Was " .. tostring(val) .. "(" .. type(val) .. ")")
end
local function _assertInexistingState(klass, stateName)
assert(klass.states[stateName] == nil, "State " .. tostring(stateName) .. " already exists on " .. tostring(klass) )
end
local function _assertExistingState(self, state, stateName)
assert(state, "The state " .. stateName .. " was not found in " .. tostring(self.class) )
end
local function _invokeCallback(self, state, callbackName, ...)
if state and state[callbackName] then state[callbackName](self, ...) end
end
local function _getCurrentState(self)
return self.__stateStack[#self.__stateStack]
end
local function _getStateFromClassByName(self, stateName)
local state = self.class.static.states[stateName]
_assertExistingState(self, state, stateName)
return state
end
local function _getStateIndexFromStackByName(self, stateName)
if stateName == nil then return #self.__stateStack end
local target = _getStateFromClassByName(self, stateName)
for i = #self.__stateStack, 1, -1 do
if self.__stateStack[i] == target then return i end
end
end
local function _getStateName(self, target)
for name,state in pairs(self.class.static.states) do
if state == target then return name end
end
end
function Stateful:included(klass)
_addStatesToClass(klass)
_modifyInstanceIndex(klass)
_modifySubclassMethod(klass)
_modifyAllocateMethod(klass)
end
function Stateful.static:addState(stateName, superState)
superState = superState or _BaseState
_assertType(stateName, 'stateName', 'string')
_assertInexistingState(self, stateName)
self.static.states[stateName] = setmetatable({}, { __index = superState })
return self.static.states[stateName]
end
function Stateful:gotoState(stateName, ...)
self:popAllStates()
if stateName == nil then
self.__stateStack = { }
else
_assertType(stateName, 'stateName', 'string', 'string or nil')
local newState = _getStateFromClassByName(self, stateName)
self.__stateStack = { newState }
_invokeCallback(self, newState, 'enteredState', ...)
end
end
function Stateful:pushState(stateName)
local oldState = _getCurrentState(self)
_invokeCallback(self, oldState, 'pausedState')
local newState = _getStateFromClassByName(self, stateName)
table.insert(self.__stateStack, newState)
_invokeCallback(self, newState, 'pushedState')
_invokeCallback(self, newState, 'enteredState')
end
function Stateful:popState(stateName)
local oldStateIndex = _getStateIndexFromStackByName(self, stateName)
local oldState
if oldStateIndex then
oldState = self.__stateStack[oldStateIndex]
_invokeCallback(self, oldState, 'poppedState')
_invokeCallback(self, oldState, 'exitedState')
table.remove(self.__stateStack, oldStateIndex)
end
local newState = _getCurrentState(self)
if oldState ~= newState then
_invokeCallback(self, newState, 'continuedState')
end
end
function Stateful:popAllStates()
local size = #self.__stateStack
for i=1,size do self:popState() end
end
function Stateful:getStateStackDebugInfo()
local info, state = {}, nil
for i=#self.__stateStack,1,-1 do
state = self.__stateStack[i]
table.insert(info, _getStateName(self, state))
end
return info
end
return Stateful