Native Orientation

Features demonstrated:

Orientation using build settings.

Code:

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
-- Load external button/label library (ui.lua should be in the same folder as main.lua)
 
local ui = require("ui");
 
local function buttonHandler(event)
        myAlert = native.showAlert(
                "Alert!", 
                "This is either a MacOS or device native alert dialog.",
                { "Cancel", "OK" } 
        );
end
 
local blueButton = ui.newButton {
    default = "buttonBlue.png",
    over = "buttonBlueOver.png",
    onRelease = buttonHandler,
    text = "Press for Alert",
    font = native.systemFontBold,
    emboss = true
}
 
blueButton.x = 160
blueButton.y = 50
 
local myLabel = ui.newLabel{
        bounds = { 12, 90, 300, 40 },
        text = "Position (0,0) is the top left in all orientations.",
        font = native.systemFont,
        textColor = { 102, 255, 102, 255 },
        size = 13,
        align = "center"
}
 
-- Display current orientation using "system.orientation"
-- (default orientation is determined in build.settings file)
 
local orientationLabel = ui.newLabel{
        bounds = { 12, 115, 300, 40 },
        text = "Orientation: (default)",
        font = native.systemFontBold,
        textColor = { 240, 240, 90, 255 },
        size = 15,
        align = "center"
}
 
local function onOrientationChange( event )
        orientationLabel:setText("Orientation: " .. system.orientation )
end
 
Runtime:addEventListener( "orientation", onOrientationChange )