forked from goosefx/mexico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisplayObject.lua
127 lines (116 loc) · 2.74 KB
/
DisplayObject.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
local mexico = require "mexico"
--
-- Base Clas for a Corona Display Object
--
-- Properties:
--
--
-- Styles:
-- + referencePoint [string] : Center, TopLeft, TopCenter, TopRight, CenterRight
-- BottomRight, BottomCenter, BottomLeft, CenterLeft
--
local DisplayObject = mexico.class(mexico.Object)
--
-- Style Setter
--
DisplayObject.setter = {
color = function(obj, value)
obj:setColor(unpack(value))
end,
strokeColor = function(obj, value)
obj:setStrokeColor(unpack(value))
end,
fillColor = function(obj, value)
local t = type(value)
if t == "table" then
local n = #value
if n > 0 then
if type(value[1]) == "table" then
-- value is gradient
obj:setFillColor(value)
else
-- value is color rgb(a)
obj:setFillColor(unpack(value))
end
end
else
obj:setFillColor(value)
end
end,
textColor = function(obj, value)
local t = type(value)
if t == "table" then
local n = #value
if n > 0 then
if type(value[1]) == "table" then
-- value is gradient
obj:setTextColor(value)
else
-- value is color rgb(a)
obj:setTextColor(unpack(value))
end
end
else
obj:setTextColor(value)
end
end,
referencePoint = function(obj, value)
obj:setReferencePoint(value)
end,
}
--
--
--
function DisplayObject:init(styles)
mexico.Object.init(self)
self:setStyles(styles)
end
function DisplayObject:setReferencePoint(value)
self:original_setReferencePoint(display[value .. "ReferencePoint"])
end
--
-- Overridden Corona insert method. Will return the added object after insert.
--
function DisplayObject:insert(obj)
self:original_insert(obj)
return obj
end
--
--
--
function DisplayObject:setStyles(styles)
mexico.Object.assert(self)
if type(styles) ~= "table" then return end
-- first change reference point if requested
if styles.referencePoint then
DisplayObject.setter.referencePoint(self, styles.referencePoint)
styles.referencePoint = nil
end
for k,v in pairs(styles) do
-- does a setter exist
local setter = DisplayObject.setter[k]
if setter then
setter(self, v)
else
self[k] = v
end
end
end
--
--
--
function DisplayObject:dispose(freeMem)
mexico.Object.assert(self)
self:removeSelf()
-- If we should free some memory after the display object
-- is removed
if freeMem then
-- GarbageCollector is a singleton
local GC = require "mexico.GarbageCollector"
-- will call collectgarbage("collect") either directly or
-- with a 1ms delay (default) - see GarbageCollector.lua for
-- more information
GC:collect()
end
end
return DisplayObject