-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
119 lines (94 loc) · 3 KB
/
main.lua
File metadata and controls
119 lines (94 loc) · 3 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
require "gyroscope"
local W = application:getContentWidth()
local H = application:getContentHeight()
local tolerance = 0.02
local kRad2Deg = 180.0 / math.pi
local gyroButton = Button.new(Bitmap.new(Texture.new("CompassOff.png")), Bitmap.new(Texture.new("CompassOver.png")), Bitmap.new(Texture.new("CompassOn.png")))
local circleTex = Texture.new("Circle.png")
local rotx = 0
local roty = 0
local rotz = 0
local spriteRotX
local spriteRotY
local spriteRotZ
local function onEnterFrame(event)
local x, y, z = gyroscope:getRotationRate()
if (math.abs(x) > tolerance) then
rotx = rotx + x * event.deltaTime
spriteRotX:setRotation(rotx * kRad2Deg)
end
if (math.abs(y) > tolerance) then
roty = roty + y * event.deltaTime
spriteRotY:setRotation(roty * kRad2Deg)
end
if (math.abs(z) > tolerance) then
rotz = rotz + z * event.deltaTime
spriteRotZ:setRotation(rotz * kRad2Deg)
end
end
gyroButton:setScale(0.5)
gyroButton:setPosition((W - gyroButton:getWidth()) / 2.0, H - (gyroButton:getHeight() + 10))
gyroButton:setToggle(false)
gyroButton:addEventListener("click",
function()
if (gyroButton.toggle) then
if (gyroscope:isAvailable()) then
gyroscope:start()
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
print("Gyroscope started")
else
print("Start: No gyroscope available!")
end
else
rotx = 0
roty = 0
rotz = 0
spriteRotX:setRotation(0)
spriteRotY:setRotation(0)
spriteRotZ:setRotation(0)
if (gyroscope:isAvailable()) then
gyroscope:stop()
stage:removeEventListener(Event.ENTER_FRAME, onEnterFrame)
print("Gyroscope stopped")
else
print("Stop: No gyroscope available!")
end
end
end)
stage:addChild(gyroButton)
function makeCircleSprite()
local circleBitmap = Bitmap.new(circleTex)
local circleSprite = Sprite.new()
circleBitmap:setScale(0.5)
circleBitmap:setPosition(-(circleBitmap:getWidth() / 2.0), -(circleBitmap:getHeight() / 2.0))
circleSprite:addChild(circleBitmap)
return circleSprite
end
function addRefLinesFor(aSprite, aColor)
local vertLine = Shape.new()
local horLine = Shape.new()
local posX = aSprite:getX()
local posY = aSprite:getY()
vertLine:setLineStyle(3, aColor)
vertLine:moveTo(posX, posY - (aSprite:getHeight() / 2.0 + 10.0))
vertLine:lineTo(posX, posY + aSprite:getHeight() / 2.0 + 10.0)
vertLine:endPath()
horLine:setLineStyle(3, aColor)
horLine:moveTo(posX - (aSprite:getWidth() / 2.0 + 10.0), posY)
horLine:lineTo(posX + aSprite:getWidth() / 2.0 + 10.0, posY)
horLine:endPath()
stage:addChild(vertLine)
stage:addChild(horLine)
end
spriteRotX = makeCircleSprite()
spriteRotX:setPosition(W * 0.25, H / 4.0)
addRefLinesFor(spriteRotX, 0xFF7777)
stage:addChild(spriteRotX)
spriteRotY = makeCircleSprite()
spriteRotY:setPosition(W * 0.75, H / 4.0)
addRefLinesFor(spriteRotY, 0x77FF77)
stage:addChild(spriteRotY)
spriteRotZ = makeCircleSprite()
spriteRotZ:setPosition(W / 2.0, (H + spriteRotZ:getHeight()) / 2.0)
addRefLinesFor(spriteRotZ, 0x7777FF)
stage:addChild(spriteRotZ)