-
Notifications
You must be signed in to change notification settings - Fork 1
/
texturelovepad.lua
160 lines (153 loc) · 4.96 KB
/
texturelovepad.lua
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
local texturelovepad = {_VERSION = "v0.1.0", _TYPE= "module", _NAME = "texturelovepad", buttons = {}}
local mt = {x = 0, y = 0, width=64, height=64, r=0, sx=1, sy=1, ox=0, oy=0, kx=0, ky=0,
id = "button",isDown = false, _lastIsDown = false, texturePressed=false, textureNormal=false}
function texturelovepad:new(o)
o = o or {}
setmetatable(o, {__index = mt})
self.buttons[o.id] = o
end
function texturelovepad:draw()
for _, button in pairs(self.buttons) do
if button.isDown then
if button.texturePressed then
love.graphics.draw(button.texturePressed,
button.x,
button.y,
button.r,
button.sx,
button.sy,
button.ox,
button.oy,
button.kx,
button.ky)
end
else
if button.textureNormal then
love.graphics.draw(button.textureNormal,
button.x,
button.y,
button.r,
button.sx,
button.sy,
button.ox,
button.oy,
button.kx,
button.ky)
end
end
end
end
function texturelovepad:update()
local touches = love.touch.getTouches()
for _, button in pairs(self.buttons) do
button._lastIsDown = button.isDown
button.isDown = false
for _, touch in ipairs(touches) do
local xt, yt = love.touch.getPosition(touch)
if xt > button.x and xt < button.x + button.width and
yt > button.y and yt < button.y + button.height then
button.isDown = true
end
end
end
end
function texturelovepad:isDown(id)
return self.buttons[id].isDown
end
function texturelovepad:isPressed(id)
return self.buttons[id].isDown and not self.buttons[id]._lastIsDown
end
function texturelovepad:isReleased(id)
return not self.buttons[id].isDown and self.buttons[id]._lastIsDown
end
function texturelovepad:remove(id)
table.remove(self.buttons, id)
end
-- function texturelovepad:setGamePad(radius, dir, ab, xy, font)
-- local width = love.graphics.getWidth()
-- local height = love.graphics.getHeight()
-- radius = radius or width/24
-- dir = dir or true
-- ab = ab or true
-- xy = xy or false
-- font = font or love.graphics.getFont()
-- if dir then
-- self:new{
-- text = 'Down',
-- radius = radius,
-- x = radius * 3,
-- y = height - radius * 1.25,
-- normalColor = {0.8,0.8,0.8,0.5},
-- pressedColor = {0.4,0.4,0.4,0.5},
-- font = font
-- }
-- self:new{
-- text = 'Up',
-- radius = radius,
-- x = radius * 3,
-- y = height - radius * 4.5,
-- normalColor = {0.8,0.8,0.8,0.5},
-- pressedColor = {0.4,0.4,0.4,0.5},
-- font = font
-- }
-- self:new{
-- text = 'Left',
-- radius = radius,
-- x = radius * 1.25,
-- y = height - radius * 2.75,
-- normalColor = {0.8,0.8,0.8,0.5},
-- pressedColor = {0.4,0.4,0.4,0.5},
-- font = font
-- }
-- self:new{
-- text = 'Right',
-- radius = radius,
-- x = radius * 4.75,
-- y = height - radius * 2.75,
-- normalColor = {0.8,0.8,0.8,0.5},
-- pressedColor = {0.4,0.4,0.4,0.5},
-- font = font
-- }
-- end
-- if ab then
-- self:new{
-- text = 'A',
-- radius = radius,
-- x = width - radius * 1.25,
-- y = height - radius * 2.75,
-- normalColor = {0.9,0.1,0.1,0.5},
-- pressedColor = {0.4,0,0,0.5},
-- font = font
-- }
-- self:new{
-- text = 'B',
-- radius = radius,
-- x = width - radius * 3,
-- y = height - radius * 1.25,
-- normalColor = {0,0.9,0,0.5},
-- pressedColor = {0,0.4,0,0.5},
-- font = font
-- }
-- end
-- if xy then
-- self:new{
-- text = 'X',
-- radius = radius,
-- x = width - radius * 3,
-- y = height - radius * 4.5,
-- normalColor = {0.9,0.9,0,0.5},
-- pressedColor = {0.4,0.4,0,0.5},
-- font = font
-- }
-- self:new{
-- text = 'Y',
-- radius = radius,
-- x = width - radius * 4.75,
-- y = height - radius * 2.75,
-- normalColor = {0,0,0.9,0.5},
-- pressedColor = {0,0,0.4,0.5},
-- font = font
-- }
-- end
-- end
return texturelovepad