Skip to content

Commit

Permalink
slightly improved android controls
Browse files Browse the repository at this point in the history
  • Loading branch information
ericoporto committed Feb 8, 2018
1 parent e6b4d83 commit 2ea5a9c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 52 deletions.
1 change: 0 additions & 1 deletion bin/build/README.md

This file was deleted.

135 changes: 84 additions & 51 deletions project/src/entities/ScreenButton.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
-- OnScreenButtons by Érico Vieira Porto 2018

local Gamestate = requireLibrary("hump.gamestate")
local Class = requireLibrary("hump.class")

Expand All @@ -6,16 +8,18 @@ ScreenButton = Class{
init = function(self)
self.osString = love.system.getOS( )
self.joy = {
r = 24,
r = 32,
x = 48,
y = 132,
deadzone = 6,
ini_x = 48,
ini_y = 132,
deadzone = 7,
color = {255,255,255,255},
}

self.buttonA = {
r = 18,
x = 262,
r = 28,
x = 266,
y = 140,
color = {255,255,255,255},
}
Expand All @@ -27,6 +31,8 @@ ScreenButton = Class{
self.scale = nil
self.pressed = {}
self.previouPressed = {}
self.isTouching = false
self.wasTouching = false
end,

update = function(self, dt)
Expand All @@ -47,68 +53,95 @@ ScreenButton = Class{

for i, id in ipairs(touches) do
x, y = love.touch.getPosition(id)
sx = x / self.scale
sy = y / self.scale
end

-- used for edge/border detection (state as square wave)
self.wasTouching = self.isTouching
if x~=nil and y~=nil then
sx = x / self.scale
sy = y / self.scale
self.isTouching = true
else
sx = -1
sy = -1
self.isTouching = false
end

if (self.joy.x-sx)^2 + (self.joy.y-sy)^2 < 4*(self.joy.r^2) then

self.joy.color = {255,128,255,255}
-- checks if touchdown or touchup
if self.wasTouching == false and self.isTouching == true then
-- finger starts touching
self.joy.x = self.joy.ini_x
self.joy.y = self.joy.ini_y

elseif self.wasTouching == true and self.isTouching == false then
-- finger leaves screen
self.joy.x = self.joy.ini_x
self.joy.y = self.joy.ini_y

if(sx < self.joy.x-self.joy.deadzone ) then
self.pressed.left = true
self.pressed.right = nil
elseif (sx > self.joy.x+self.joy.deadzone ) then
self.pressed.right = true
self.pressed.left = nil
else
self.pressed.right = nil
self.pressed.left = nil
end


if(sy > self.joy.y+self.joy.deadzone ) then
self.pressed.down = true
self.pressed.up = nil
elseif (sy < self.joy.y-self.joy.deadzone ) then
self.pressed.up = true
self.pressed.down = nil
else
self.pressed.up = nil
self.pressed.down = nil
end
end

-- resets everything first
self.touchpos.x = self.joy.x
self.touchpos.y = self.joy.y
self.joy.color = {128,128,128,64}
self.pressed.up = nil
self.pressed.down = nil
self.pressed.right = nil
self.pressed.left = nil
self.pressed.buttona = nil
self.buttonA.color = {128,128,128,64}

self.touchpos.x = sx
self.touchpos.y = sy

else
for i, id in ipairs(touches) do
x, y = love.touch.getPosition(id)
sx = x / self.scale
sy = y / self.scale

self.touchpos.x = self.joy.x
self.touchpos.y = self.joy.y
self.joy.color = {128,128,128,64}
self.pressed.up = nil
self.pressed.down = nil
self.pressed.right = nil
self.pressed.left = nil
if (self.joy.x-sx)^2 + (self.joy.y-sy)^2 < 2*(self.joy.r^2) then

self.joy.color = {255,128,255,255}


if(sx < self.joy.x-self.joy.deadzone ) then
self.pressed.left = true
self.pressed.right = nil
elseif (sx > self.joy.x+self.joy.deadzone ) then
self.pressed.right = true
self.pressed.left = nil
else
self.pressed.right = nil
self.pressed.left = nil
end


if(sy > self.joy.y+self.joy.deadzone ) then
self.pressed.down = true
self.pressed.up = nil
elseif (sy < self.joy.y-self.joy.deadzone ) then
self.pressed.up = true
self.pressed.down = nil
else
self.pressed.up = nil
self.pressed.down = nil
end

self.touchpos.x = sx
self.touchpos.y = sy

if (self.joy.x-sx)^2 + (self.joy.y-sy)^2 > 1.6*(self.joy.r^2) then

self.joy.x = sx
self.joy.y = sy
end
end

end
if (self.buttonA.x-sx)^2 + (self.buttonA.y-sy)^2 < 2*(self.buttonA.r^2) then
self.pressed.buttona = true
self.buttonA.color = {255,128,255,255}
end

if (self.buttonA.x-sx)^2 + (self.buttonA.y-sy)^2 < 4*(self.buttonA.r^2) then
self.pressed.buttona = true
self.buttonA.color = {255,128,255,255}
else
self.pressed.buttona = nil
self.buttonA.color = {128,128,128,64}
end



end,

Expand All @@ -126,8 +159,8 @@ ScreenButton = Class{
love.graphics.setColor(self.buttonA.color)
love.graphics.circle('fill',self.buttonA.x,self.buttonA.y,self.buttonA.r)

love.graphics.setColor(244,244,255,40)
love.graphics.circle('fill',self.touchpos.x,self.touchpos.y,self.joy.r/2)
love.graphics.setColor(244,244,255,64)
love.graphics.circle('fill',self.touchpos.x,self.touchpos.y,self.joy.r/1.5)
end
}

Expand Down

0 comments on commit 2ea5a9c

Please sign in to comment.