-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexample-05.lua.html
More file actions
175 lines (150 loc) · 9.34 KB
/
example-05.lua.html
File metadata and controls
175 lines (150 loc) · 9.34 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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<head>
<title>Layout Reference</title>
<link rel="stylesheet" href="../ldoc.css" type="text/css" />
</head>
<body>
<div id="container">
<div id="product">
<div id="product_logo"></div>
<div id="product_name"><big><b></b></big></div>
<div id="product_description"></div>
</div> <!-- id="product" -->
<div id="main">
<!-- Menu -->
<div id="navigation">
<br/>
<h1>Layout</h1>
<h2>Examples</h2>
<ul class="nowrap">
<li><a href="../examples/config.lua.html">config.lua</a></li>
<li><a href="../examples/example-01.lua.html">example-01.lua</a></li>
<li><a href="../examples/example-02.lua.html">example-02.lua</a></li>
<li><a href="../examples/example-03.lua.html">example-03.lua</a></li>
<li><a href="../examples/example-04.lua.html">example-04.lua</a></li>
<li><strong>example-05.lua</strong></li>
</ul>
<h2>Modules</h2>
<ul class="nowrap">
<li><a href="../index.html">layout</a></li>
</ul>
</div>
<div id="content">
<h2>example-05.lua</h2>
<pre>
<span class="comment">-- ========================================================================== --
</span><span class="comment">-- example-04.lua
</span><span class="comment">--
</span><span class="comment">-- Expands on example-04 by slightly shrinking all of the "cell" regions after
</span><span class="comment">-- they are created by a fixed number of pixels per dimension, demonstrating the
</span><span class="comment">-- use of adjustRegion and showing how the Layout.pixel element might be used.
</span>
<span class="comment">-- layout.lua is in parent folder
</span><span class="global">package</span>.path = <span class="string">"../?.lua;"</span> .. <span class="global">package</span>.path
<span class="keyword">local</span> LayoutManager = <span class="global">require</span>( <span class="string">"layout"</span> )
<span class="comment">-- layout manager object
</span><span class="keyword">local</span> Layout
<span class="comment">-- Group to hold rects that display the regions
</span><span class="keyword">local</span> Regions
<span class="comment">-- Content grid size; change these to change the size of the generated grid
</span><span class="keyword">local</span> ContentGridSize = <span class="number">4</span>
<span class="keyword">local</span> GridPixelPadding = <span class="number">8</span>
<span class="comment">-- layout creation
</span><span class="keyword">local</span> <span class="keyword">function</span> createLayout()
<span class="comment">-- init the layout manager
</span> Layout = LayoutManager:new()
<span class="comment">-- device is in portrait orientation
</span> <span class="keyword">if</span> ( Layout.stage.isPortrait ) <span class="keyword">then</span>
<span class="comment">-- create a header occupying 10% of the stage
</span> Layout:addRegion( { id = <span class="string">"header"</span>, vertical = <span class="string">"top"</span>, height = <span class="number">10</span> } )
<span class="comment">-- create a content area below the header with some small padding
</span> Layout:addRegion( { id = <span class="string">"content"</span>, positionTo = <span class="string">"header"</span>, vertical = <span class="string">"below"</span>, height = <span class="number">83</span>, padding = { top = <span class="number">1</span> } } )
<span class="comment">-- create a footer area at the bottom of the stage
</span> Layout:addRegion( { id = <span class="string">"footer"</span>, vertical = <span class="string">"bottom"</span>, height = <span class="number">5</span> } )
<span class="comment">-- device is in landscape orientation
</span> <span class="keyword">else</span>
<span class="comment">-- create a header occupying 10% of the stage
</span> Layout:addRegion( { id = <span class="string">"header"</span>, horizontal = <span class="string">"left"</span>, width = <span class="number">10</span> } )
<span class="comment">-- create a content area below the header with some small padding
</span> Layout:addRegion( { id = <span class="string">"content"</span>, positionTo = <span class="string">"header"</span>, horizontal = <span class="string">"after"</span>, width = <span class="number">83</span>, padding = { left = <span class="number">1</span> } } )
<span class="comment">-- create a footer area at the bottom of the stage
</span> Layout:addRegion( { id = <span class="string">"footer"</span>, horizontal = <span class="string">"right"</span>, width = <span class="number">5</span> } )
<span class="keyword">end</span>
<span class="comment">-- Create the content grid regions. This works independent of device
</span> <span class="comment">-- orientation since we are working relative to the "content" region
</span> <span class="comment">-- already created above.
</span> <span class="keyword">local</span> cellWidth = <span class="number">100</span> / ContentGridSize
<span class="keyword">local</span> cellHeight = <span class="number">100</span> / ContentGridSize
<span class="keyword">for</span> row = <span class="number">1</span>, ContentGridSize <span class="keyword">do</span>
<span class="keyword">for</span> col = <span class="number">1</span>, ContentGridSize <span class="keyword">do</span>
<span class="keyword">local</span> id = <span class="string">"("</span>..row..<span class="string">","</span>..col..<span class="string">")"</span>
<span class="keyword">local</span> dimens = {
id = id, sizeTo = <span class="string">"content"</span>,
width = cellWidth, height = cellHeight,
horizontal = <span class="string">"left"</span>, vertical = <span class="string">"top"</span>,
padding = { left = cellWidth * ( col - <span class="number">1</span> ), top = cellHeight * ( row - <span class="number">1</span> ) }
}
Layout:addRegion( dimens )
dimens = {
id = id,
width = Layout[id].width - GridPixelPadding * Layout.pixel,
height = Layout[id].height - GridPixelPadding * Layout.pixel
}
Layout:adjustRegion( dimens )
<span class="keyword">end</span>
<span class="keyword">end</span>
<span class="keyword">end</span>
<span class="comment">-- ========================================================================== --
</span>
<span class="comment">-- helper function to display a region as a rect
</span><span class="keyword">local</span> <span class="keyword">function</span> showRegion( id, fill )
<span class="keyword">local</span> rect = Layout:regionRect( id )
Regions:insert( rect )
rect.fill = fill <span class="keyword">or</span> { <span class="number">0</span>, <span class="number">0</span> }
rect.stroke = { <span class="number">1</span>, <span class="number">1</span> }
rect.strokeWidth = <span class="number">2</span> * Layout.pixel
<span class="keyword">local</span> region = Layout[id]
<span class="keyword">local</span> text = display.newText( id, region.xCenter, region.yCenter, native.systemFont, <span class="number">20</span> * Layout.pixel )
Regions:insert( text )
text.fill = { <span class="number">1</span>, <span class="number">1</span> }
<span class="keyword">end</span>
<span class="comment">-- for demo purposes just display the layout regions as rects on the screen
</span><span class="keyword">local</span> <span class="keyword">function</span> showLayout()
Regions = display.newGroup()
showRegion( <span class="string">"header"</span>, {<span class="number">0.5</span>,<span class="number">0</span>,<span class="number">0</span>} )
showRegion( <span class="string">"content"</span>, {<span class="number">1</span>,<span class="number">0.5</span>} )
showRegion( <span class="string">"footer"</span>, {<span class="number">0</span>,<span class="number">0</span>,<span class="number">0.5</span>} )
<span class="keyword">for</span> row = <span class="number">1</span>, ContentGridSize <span class="keyword">do</span>
<span class="keyword">for</span> col = <span class="number">1</span>, ContentGridSize <span class="keyword">do</span>
showRegion( <span class="string">"("</span>..row..<span class="string">","</span>..col..<span class="string">")"</span> )
<span class="keyword">end</span>
<span class="keyword">end</span>
<span class="keyword">end</span>
<span class="comment">-- ========================================================================== --
</span>
<span class="comment">-- orientation change listener
</span><span class="keyword">local</span> <span class="keyword">function</span> onOrientationChange( event )
Regions:removeSelf()
Regions = <span class="keyword">nil</span>
createLayout()
showLayout()
<span class="keyword">end</span>
<span class="comment">-- add listener for orientation change
</span>Runtime:addEventListener( <span class="string">"orientation"</span>, onOrientationChange )
<span class="comment">-- ========================================================================== --
</span>
<span class="comment">-- initial layout and display
</span>createLayout()
showLayout()</pre>
</div> <!-- id="content" -->
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.3</a></i>
<i style="float:right;">Last updated 2016-06-22 17:03:47 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
</html>