forked from pickerel/owl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathowl.lua
More file actions
302 lines (255 loc) · 7.59 KB
/
owl.lua
File metadata and controls
302 lines (255 loc) · 7.59 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
-----------------------------------------------------------------------------------------
--
-- owl.lua (Objects with Lua)
--
-- Version: 1.0.1
--
-- Copyright (C) Jonathan Beebe (http://jonbeebe.net)
--
-----------------------------------------------------------------------------------------
local class = {}
local registered = {} -- stores all class definitions
-- for non-Corona SDK compatibility
local system = system
if not system then system = {}; end
--
--
-- PRIVATE FUNCTIONS
--
--
local function get_display_object( params )
local params = params or {}
local image = params.image
local width = params.width
local height = params.height
local base_dir = params.baseDir or system.ResourceDirectory
local create_object = params.custom -- should be a function that returns a display object
local obj
if create_object and type(create_object) == "function" then
obj = create_object()
else
if image and width and height then
obj = display.newImageRect( image, base_dir, width, height )
elseif image then
obj = display.newImage( image, base_dir )
end
end
return obj
end
local function extend_class( name, class )
local class_mt = { __index = class }
local private_mt = { __index = class.private }
local c =
{
class_name=name,
super_class=class,
private=setmetatable( {}, private_mt )
}
if class.display_obj then
local display_obj_mt = { __index = class.display_obj }
c.display_obj = setmetatable( {}, display_obj_mt )
end
return setmetatable( c, class_mt )
end
--
--
-- CLASS METHODS
--
--
-- checks to see if object "is a" class (name or class object can be passed) -- super_classes are also checked
local function is_a( self, class_name )
if not class_name then return false; end
if type(class_name) == "table" then class_name = class_name.class_name; end
local found_match
while found_match == nil do
if self.class_name == class_name then
found_match = true
elseif not self.super_class then
found_match = false
else
found_match = is_a( self.super_class, class_name )
end
end
return found_match
end
-- checks to see if object is an instance of specific class (super_classes are not checked)
local function instance_of( self, class_name )
if not class_name then return false; end
if type(class_name) == "table" then class_name = class_name.class_name; end
return self.class_name == class_name
end
local function add_property_callback( self, property_name, callback )
if not callback then return; end
self.callback_properties[property_name] = callback
end
local function remove_property_callback( self, property_name )
if not property_name then return; end
self.callback_properties[property_name] = nil
end
--
--
-- PUBLIC FUNCTIONS
--
--
function class.class( params )
-- required params: name, createFunction or image or (image and width and height)
local params = params or {}
-- valid parameters
local name = params.name
local image = params.image
local width = params.width
local height = params.height
local base_dir = params.baseDir or system.ResourceDirectory
local create_object = params.createFunction -- should be a function or closure that returns a display object
local parent_class = params.from -- class object or string that corresponds to existing registered class
local private = params.private or {} -- for private class data; can be anything (not accessible by instances)
if type(parent_class) == "string" then
parent_class = registered[parent_class]
end
-- throw warning and return if no class name is specified, or class name is already taken
if not name then
print( "WARNING: You must specify a name when creating a new class." )
elseif registered[name] then
print( "WARNING: The class name, " .. name .. ", is already in use. Class names must be unique." )
end
-- create brand new class or extend from base class (if specified)
local c
if parent_class then
c = extend_class( name, parent_class )
-- instances may optionally have different display object properties
if image or (createFunction and type(createFunction) == "function") then
c.display_obj =
{
image = image,
width = width,
height = height,
base_dir = base_dir,
createFunction = create_object
}
end
else
local proxy = {}
c = {}
c.class_name = name
local c_mt = {
__index = function(t,k)
if k ~= "display_obj" then
return proxy[k]
end
end,
__newindex = function(t,k,v)
proxy[k] = v
end,
}
if image or create_object then
-- holds data for associated display object
c.display_obj =
{
image = image,
width = width,
height = height,
base_dir = base_dir,
createFunction = create_object
}
end
-- optional private class data (accessible from sub-classes, but not instances)
c.private = private
setmetatable( c, c_mt )
end
-- assign methods
c.is_a = is_a
c.kind_of = is_a
c.instance_of = instance_of
c.add_property_callback = add_property_callback
c.remove_property_callback = remove_property_callback
-- register the class and return it
registered[name] = c
return c
end
-- will de-register/remove a class (it's user's responsibility to make sure no objects are using it!)
function class.remove( name )
registered[name] = nil
end
-- creates a new instance of a specific class
function class.instance( params )
local params = params or {}
-- extract function params
local id = params.id -- an id can be optionally assigned to this object
local base_class = params.from -- the class this instance is derived from
local constructor = params.init -- optionally specify constructor function for this instance (or class constructor is used)
local init_params = params.params -- user-provided params passed to constructor (init) function
-- throw a warning and return if base class is not specified or does not exist
local no_base_warning = "WARNING: You must specify a base class when creating a new class instance."
if not base_class then
print( no_base_warning )
return
else
if type(base_class) == "string" then
base_class = registered[base_class]
end
end
if not base_class then
print( no_base_warning )
return
end
-- create object instance from class' definition and copy class properties to instance
local obj
if base_class.display_obj then
obj = get_display_object( base_class.display_obj )
else
obj = {}
end
-- set up object properties
obj.id = id
obj.callback_properties = {} -- contains properties and corresponding callbacks (called on update)
obj.private = {}
-- proxy and metatable setup
local _t = obj
local t = {}
t.raw = obj
local mt =
{
__index = function(tb,k)
if k == "display_obj" then return;
elseif k == "private" then
return rawget( _t, "private" )
elseif k == "raw" then
return rawget( t, "raw" )
end
if _t[k] then
if type(_t[k]) == 'function' then
return function(...) arg[1] = _t; _t[k](unpack(arg)) end
else
return _t[k]
end
else
return base_class[k]
end
end,
__newindex = function(tb,k,v)
if k == "super_class" or k == "class_name" or k == "callback_properties" then return; end
if _t.callback_properties[k] and type(_t.callback_properties[k]) == "function" then
local event =
{
name="propertyUpdate",
target=tb,
key=k,
value=v
}
local callback = _t.callback_properties[k]
callback( _t, event )
else
_t[k] = v
end
end
}
setmetatable(t, mt)
-- call constructor function (if exists in class)
if constructor and type(constructor) == "function" then
constructor( t, init_params )
elseif base_class.init and type(base_class.init) == "function" then
base_class.init( t, init_params )
end
return t
end
return class