-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdropdown.lua
More file actions
162 lines (130 loc) · 3.88 KB
/
dropdown.lua
File metadata and controls
162 lines (130 loc) · 3.88 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
local screen = require('screen')
local widget = require('widget')
local Dropdown = {}
local function onRowRender( event )
local row = event.row
local rowHeight = row.contentHeight
local rowWidth = row.contentWidth
local title = row.params.title
local leftIcon = row.params.leftIcon or nil
local rightIcon = row.params.rightIcon or nil
local titleColor = row.params.titleColor or {0,0,0}
local options = {
parent = row,
text = title,
x = 20,
y = rowHeight * .5,
width = rowWidth,
font = native.systemFont,
fontSize = 14,
align = 'left'
}
local title = display.newText( options )
title.anchorX = 0
title:setFillColor( titleColor )
if leftIcon then
leftIcon.anchorX = 0
leftIcon.x, leftIcon.y = leftIcon.width * .5 - 14, rowHeight * .5
title.x = leftIcon.width + 10
row:insert( leftIcon )
end
if rightIcon then
rightIcon.anchorX = 0
rightIcon.x, rightIcon.y = rowWidth - rightIcon.width * .5 - 14, rowHeight * .5
row:insert( rightIcon )
end
end
local function onRowTouch(event)
local row = event.target
local phase = event.phase
if phase == 'release' then
row.params._group:hide(0.001)
row.params.action()
end
return true
end
local function createTable( options, width, height, group )
local tableView = widget.newTableView{
x = 0,
y = 0,
width = width,
height = height,
hideBackground = false,
rowTouchDelay = 0,
onRowRender = onRowRender,
onRowTouch = onRowTouch
}
for i=1, #options do
local option = options[i]
option._group = group
tableView:insertRow{
rowHeight = 44,
isCategory = false,
params = option
}
end
return tableView
end
function Dropdown.new( o )
o = o or {}
local height = o.height or nil
local width = o.width or 144
local x = o.x or 100
local y = o.y or 100
local isOverlay = o.isOverlay or false
local marginTop = o.marginTop or 10
local padding = o.padding or 10
local options = o.options or {}
local button = o.toggleButton or error('Invalid toggle button')
local group = display.newGroup()
local overlay = display.newRect( group, 0, 0, screen.totalWidth, screen.totalHeight )
overlay:setFillColor( 0, 0, 0, .3 )
overlay.x, overlay.y = screen.centerX, screen.centerY
overlay.touch = function( self, e )
local phase = e.phase
if phase == 'ended' then
group:hide()
end
return true
end
overlay:addEventListener( 'touch', overlay )
overlay.isVisible = false
button.x, button.y = x, y
group:insert( button )
if not height then
height = 44 * #options + padding
end
local container = display.newContainer( width, height )
container.x, container.y = x + 10, y + 10 + marginTop
container.anchorX, container.anchorY = 1, 0
group:insert( container )
local groupMove = display.newGroup( )
container:insert( groupMove )
group.move = groupMove
groupMove.y = height * -1
local background = display.newRect( 0, 0, width, height )
groupMove:insert( background )
local table = createTable( options, width - padding, height - padding, group )
groupMove:insert( table )
function group:show()
local move = self.move
transition.to( move, { y = 0, time = time or 100, transition = easing.outQuad } )
self.isShow = true
overlay.isVisible = true
end
function group:hide( time )
local move = self.move
transition.to( move, { y = height * -1, time = time or 100, transition = easing.outQuad } )
self.isShow = false
overlay.isVisible = false
end
function group:toggle( time )
if self.isShow then
self:hide( time )
else
self:show( time )
end
end
return group
end
return Dropdown