-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlayout.lua
More file actions
262 lines (237 loc) · 11.9 KB
/
layout.lua
File metadata and controls
262 lines (237 loc) · 11.9 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
-- ============================================================================================== --
--- ## layout.lua
-- A simple but powerful device and display-size independent layout manager for the Corona SDK.
-- **OVERVIEW**
-- The layout manager makes it easy to create *regions*, rectangular areas
-- defined relative to the screen, the stage (which may or may not be the full
-- screen), and to the previously created user-defined regions. The final
-- regions are created with content coordinates. No actual display objects are
-- created, the programmer is free to utilize the defined regions in any manner.
-- There are default 'screen' and 'stage' regions, but the real power of the
-- layout manager is its ability to easily define new regions using simple but
-- powerful positioning and sizing options relative to any region that already
-- exists.
-- Each **region** is stored as a table within the layout manager object. Each region contains the
-- following fields that define the region:
-- - `width`: (**_number_**) region width in content units
-- - `height`: (**_number_**) region height in content units
-- - `top`: (**_number_**) content coordinate for top region edge
-- - `right`: (**_number_**) content coordinte for right region edge
-- - `bottom`: (**_number_**) content coordinate for bottom region edge
-- - `left`: (**_number_**) content coordinate for left region edge
-- - `xCenter`: (**_number_**) content coordiate for horizontal center
-- - `yCenter`: (**_number_**) content coordinate for vertical center
-- - `xPct`: (**_number_**) content units for 1% of the region width
-- - `yPct`: (**_number_**) content units for 1% of the region height
-- - `aspect`: (**_number_**) ratio of region width to height (i.e. width / height)
-- - `isPortrait`: (**_bool_**) true if aspect <= 1
-- **EXAMPLES**
-- To quickly observe and understand the power and flexibility of the Layout manager, run the
-- examples in the Corona simulator and switch around to different devices and orientations.
-- The "examples" subfolder contains several examples of how to use the Layout manager. To run
-- an example, copy the "example-##.lua" (where ## is a two digit number) file to main.lua, and
-- then run the main.lua with the Corona simulator. The build.settings and config.lua files do
-- not need to be changed (unless you want to experiment with different settings), these two
-- files work with all the examples. Simply copy the example you want to try to main.lua and run.
--
-- @classmod Layout
-- @release 1.0.0-2016.06.22
-- @author [Ron Pacheco](mailto:ron@wolfden.pub)
-- @license [MIT](https://opensource.org/licenses/MIT)
-- @copyright 2016 [Wolfden Publishing](http://www.wolfden.pub/), [Ron Pacheco](mailto:ron@wolfden.pub)
-- @usage local LayoutManager = require( "layout" )
local Layout = {}
-- ============================================================================================== --
--- Constructor - build and return a new Layout object.
--
-- The new Layout object will contain the following entries:
--
-- - `pixel`: (**number**) content units for one screen pixel (assumes device to have square pixels;
-- if not, this value will represent the content units for one pixel in the largest screen dimension)
-- - `screen`: (**region**) content region for the full screen
-- - `stage`: (**region**) content region for the stage (generally the same as the screen minus the status bar)
-- - `pixels`: (**region**) special region for positioning and sizing by pixels
--
-- @return new Layout object
-- @usage local Layout = LayoutManager:new()
function Layout:new()
local layout = {}
-- screen --
layout.screen = {}
local screen = layout.screen
screen.user = false
screen.width = display.currentStage.width
screen.height = display.currentStage.height
screen.top = 0
screen.right = screen.width
screen.bottom = screen.height
screen.left = 0
screen.xCenter = 0.5 * screen.width
screen.yCenter = 0.5 * screen.height
screen.xPct = 0.01 * screen.width
screen.yPct = 0.01 * screen.height
screen.aspect = screen.width / screen.height
screen.isPortrait = screen.aspect <= 1
local statusBarHeight = display.topStatusBarContentHeight or 0
-- stage (screen minus status bar if present ) --
layout.stage = {}
local stage = layout.stage
stage.user = false
stage.width = display.currentStage.width
stage.height = display.currentStage.height - statusBarHeight
stage.top = statusBarHeight
stage.right = stage.width
stage.bottom = stage.height + statusBarHeight
stage.left = 0
stage.xCenter = 0.5 * stage.width
stage.yCenter = 0.5 * stage.height + statusBarHeight
stage.xPct = 0.01 * stage.width
stage.yPct = 0.01 * stage.height
stage.aspect = stage.width / stage.height
stage.isPortrait = stage.aspect <= 1
-- pixel size --
layout.pixel = math.max( screen.width, screen.height ) / math.max( display.pixelHeight, display.pixelWidth )
layout.pixels = {}
local pixels = layout.pixels
pixels.user = false
if ( screen.isPortrait ) then
pixels.width = math.min( display.pixelWidth, display.pixelHeight )
pixels.height = math.max( display.pixelWidth, display.pixelHeight )
else
pixels.width = math.max( display.pixelWidth, display.pixelHeight )
pixels.height = math.min( display.pixelWidth, display.pixelHeight )
end
pixels.top = 0
pixels.right = pixels.width
pixels.bottom = pixels.height
pixels.left = 0
pixels.xCenter = 0.5 * pixels.width
pixels.yCenter = 0.5 * pixels.height
pixels.xPct = layout.pixel
pixels.yPct = layout.pixel
pixels.aspect = pixels.width / pixels.height
pixels.isPortrait = pixels.aspect <= 1
setmetatable( layout, self )
self.__index = self
return layout
end
-- ============================================================================================== --
--- addRegion - add a new region to the layout manager.
--
-- @param dimens a table specifying the options for the new region:
--
-- - `id`: (**_string_**) **required** unique identifier for the new region
-- - `sizeTo`: (**_string_**) id of region to size against (default: "stage")
-- - `width`: (**_number_**) new region width as a percentage of the `sizeTo` region (default: 100)
-- - `height`: (**_number_**) new region height as a percentage of the 'sizeTo' region (default: 100)
-- - `positionTo`: (**_string_**) id of region to position relative to (defaults to `sizeTo` region)
-- - `horizontal`: (**_string_**) one of: "before", "left", "center", "right", "after" (default: "center")
-- - `vertical`: (**_string_**) one of: "above", "top", "center", "bottom", "below" (default: "center")
-- - `padTo`: (**_string_**) id of region to pad relative to (defaults to `sizeTo` region)
-- - `padding`: (**_table_**) array of: `{top=#, right=#, bottom=#, left=#}` (all #'s default to 0)
function Layout:addRegion( dimens )
assert( dimens.id, "Region id not defined" )
assert( not self[dimens.id], "Region already exists" )
self[ dimens.id ] = {}
local region = self[ dimens.id ]
region.user = true
dimens.sizeTo = dimens.sizeTo or "stage"
dimens.width = dimens.width or 100
dimens.height = dimens.height or 100
local sizTo = self[ dimens.sizeTo ]
region.width = dimens.width * sizTo.xPct
region.height = dimens.height * sizTo.yPct
region.aspect = region.width / region.height
region.isPortrait = region.aspect <= 1
region.xPct = 0.01 * region.width
region.yPct = 0.01 * region.height
dimens.positionTo = dimens.positionTo or dimens.sizeTo
dimens.padTo = dimens.padTo or dimens.sizeTo
dimens.padding = dimens.padding or { top = 0, right = 0, bottom = 0, left = 0 }
dimens.horizontal = dimens.horizontal or "center"
dimens.vertical = dimens.vertical or "center"
local posTo = self[ dimens.positionTo ]
local padTo = self[ dimens.padTo ]
if ( dimens.horizontal == "before" ) then
local padding = dimens.padding.right or 0
region.xCenter = posTo.left - padding * padTo.xPct - 0.5 * region.width
elseif ( dimens.horizontal == "left" ) then
local padding = dimens.padding.left or 0
region.xCenter = posTo.left + padding * padTo.xPct + 0.5 * region.width
elseif ( dimens.horizontal == "center" ) then
region.xCenter = posTo.xCenter
elseif ( dimens.horizontal == "right" ) then
local padding = dimens.padding.right or 0
region.xCenter = posTo.right - padding * padTo.xPct - 0.5 * region.width
elseif ( dimens.horizontal == "after" ) then
local padding = dimens.padding.left or 0
region.xCenter = posTo.right + padding * padTo.xPct + 0.5 * region.width
end
if ( dimens.vertical == "above" ) then
local padding = dimens.padding.bottom or 0
region.yCenter = posTo.top - padding * padTo.yPct - 0.5 * region.height
elseif ( dimens.vertical == "top" ) then
local padding = dimens.padding.top or 0
region.yCenter = posTo.top + padding * padTo.yPct + 0.5 * region.height
elseif ( dimens.vertical == "center" ) then
region.yCenter = posTo.yCenter
elseif ( dimens.vertical == "bottom" ) then
local padding = dimens.padding.bottom or 0
region.yCenter = posTo.bottom - padding * padTo.yPct - 0.5 * region.height
elseif ( dimens.vertical == "below" ) then
local padding = dimens.padding.top or 0
region.yCenter = posTo.bottom + padding * padTo.yPct + 0.5 * region.height
end
region.top = region.yCenter - 0.5 * region.height
region.right = region.xCenter + 0.5 * region.width
region.bottom = region.yCenter + 0.5 * region.height
region.left = region.xCenter - 0.5 * region.width
end
-- ============================================================================================== --
--- removeRegion - remove a region from the layout.
--
-- @string id id of the region to be removed
-- @usage Layout:removeRegion( "toolbar" )
function Layout:removeRegion( id )
assert( self[id], "Specified region does not exist" )
self[id] = nil
end
-- ============================================================================================== --
--- adjustRegion - change a region's size and/or position.
--
-- @param dimens a table specifying the adjustments for the region:
--
-- - `id`: (**_string_**) **required** unique identifier for the new region
-- - `width`: (**_number_**) new region width in *content* units (default: no change)
-- - `height`: (**_number_**) new region height in *content* units (default: no change)
-- - `xCenter`: (**_number_**) new region horizontal center in *content* coordinates (default: no change)
-- - `yCenter`: (**_number_**) new region vertical center in *content* coordinates (default: no change)
function Layout:adjustRegion( dimens )
assert( dimens.id, "Region id not defined" )
assert( self[dimens.id], "Region does not exist" )
assert( self[dimens.id].user, "Can't adjust non-user region" )
local region = self[ dimens.id ]
region.width = dimens.width or region.width
region.height = dimens.height or region.height
region.xCenter = dimens.xCenter or region.xCenter
region.yCenter = dimens.yCenter or region.yCenter
region.aspect = region.width / region.height
region.isPortrait = region.aspect <= 1
region.xPct = 0.01 * region.width
region.yPct = 0.01 * region.height
region.top = region.yCenter - 0.5 * region.height
region.right = region.xCenter + 0.5 * region.width
region.bottom = region.yCenter + 0.5 * region.height
region.left = region.xCenter - 0.5 * region.width
end
-- ============================================================================================== --
--- regionRect - return a display rect matching a specified region.
--
-- @string id id of the region for which to return a display rect
-- @return ShapeObject (as returned by [display.newRect()](https://docs.coronalabs.com/api/library/display/newRect.html))
-- @usage local toolbarRect = Layout:regionRect( "toolbar" )
function Layout:regionRect( id )
assert( self[id], "Specified region does not exist" )
return display.newRect( self[id].xCenter, self[id].yCenter, self[id].width, self[id].height )
end
return Layout