Skip to content

Commit

Permalink
adding Android and iOS touchscreen support
Browse files Browse the repository at this point in the history
  • Loading branch information
ericoporto committed Feb 6, 2018
1 parent 8241364 commit 14ccba9
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 7 deletions.
55 changes: 48 additions & 7 deletions project/main.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--
--
-- Created by Tilmann Hars
-- Copyright (c) 2014 Headchant. All rights reserved.
-- Code Created by Érico
-- Copyright (c) 2018 Vacaroxa. All rights reserved.
--

-- Set Library Folders
Expand All @@ -19,7 +19,7 @@ local Terebi = requireLibrary("terebi")
local Gamestate = requireLibrary("hump/gamestate")

-- Game Version
GAME_VERSION = '1.0.1'
GAME_VERSION = '1.0.3'

-- a variable for debug flags
debug_mode = false
Expand Down Expand Up @@ -212,8 +212,16 @@ local function extractFileName(str)
return string.match(str, "(.-)([^\\/]-%.?([^%.\\/]*))$")
end



local ScreenButton = require 'src.entities.ScreenButton'
scrBtn = ScreenButton()


-- Initialization
function love.load(arg)
local onload_width, onload_height = love.graphics.getDimensions()

for k, v in ipairs(arg) do
-- mute music
if (v == '-nomusic') then
Expand All @@ -236,14 +244,16 @@ function love.load(arg)
-- Set nearest-neighbour scaling. Calling this is optional.
Terebi.initializeLoveDefaults()

local scaleH = onload_height / GAME_HEIGHT
local scaleW = onload_width / GAME_WIDTH
local aScale = math.floor(math.min(scaleW,scaleH))

-- Parameters: game width, game height, starting scale factor
screen = Terebi.newScreen(GAME_WIDTH,GAME_HEIGHT, 1)
screen = Terebi.newScreen(GAME_WIDTH,GAME_HEIGHT, aScale)
-- This color will used for fullscreen letterboxing when content doesn't fit exactly. (Optional)
:setBackgroundColor(64, 64, 64)

if GAME_WIDTH == 320 then
screen:increaseScale()
end


-- add all font as objects
font_Verdana2 = love.graphics.newFont("fonts/Verdana2.ttf", 16)
Expand Down Expand Up @@ -278,6 +288,37 @@ end

-- Logic
function love.update( dt )
scrBtn:update(dt)
if scrBtn.previouPressed.left ~= true and scrBtn.pressed.left == true then
keys_pressed['left'] = true
elseif scrBtn.previouPressed.left == true and scrBtn.pressed.left ~= true then
keys_pressed['left'] = nil
end

if scrBtn.previouPressed.right ~= true and scrBtn.pressed.right == true then
keys_pressed['right'] = true
elseif scrBtn.previouPressed.right == true and scrBtn.pressed.right ~= true then
keys_pressed['right'] = nil
end

if scrBtn.previouPressed.up ~= true and scrBtn.pressed.up == true then
keys_pressed['up'] = true
elseif scrBtn.previouPressed.up == true and scrBtn.pressed.up ~= true then
keys_pressed['up'] = nil
end

if scrBtn.previouPressed.down ~= true and scrBtn.pressed.down == true then
keys_pressed['down'] = true
elseif scrBtn.previouPressed.down == true and scrBtn.pressed.down ~= true then
keys_pressed['down'] = nil
end

if scrBtn.previouPressed.buttona ~= true and scrBtn.pressed.buttona == true then
keys_pressed['buttona'] = true
elseif scrBtn.previouPressed.buttona == true and scrBtn.pressed.buttona ~= true then
keys_pressed['buttona'] = nil
end

-- things for joystick
if p1joystick ~= nil then
-- getGamepadAxis returns a value between -1 and 1.
Expand Down
134 changes: 134 additions & 0 deletions project/src/entities/ScreenButton.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
local Gamestate = requireLibrary("hump.gamestate")
local Class = requireLibrary("hump.class")

ScreenButton = Class{
-- initializes the inventory
init = function(self)
self.osString = love.system.getOS( )
self.joy = {
r = 24,
x = 48,
y = 132,
deadzone = 6,
color = {255,255,255,255},
}

self.buttonA = {
r = 18,
x = 262,
y = 140,
color = {255,255,255,255},
}

self.touchpos = {
x=-1,
y=-1,
}
self.scale = nil
self.pressed = {}
self.previouPressed = {}
end,

update = function(self, dt)
if self.osString ~= "Android" and self.osString ~= "iOS" then
return
end


self.previouPressed.left = self.pressed.left
self.previouPressed.right = self.pressed.right
self.previouPressed.up = self.pressed.up
self.previouPressed.down = self.pressed.down
self.previouPressed.buttona = self.pressed.buttona

local touches = love.touch.getTouches()
local x, y, sx, sy
self.scale = screen:getScale()

for i, id in ipairs(touches) do
x, y = love.touch.getPosition(id)
end

if x~=nil and y~=nil then
sx = x / self.scale
sy = y / self.scale
else
sx = -1
sy = -1
end

if (self.joy.x-sx)^2 + (self.joy.y-sy)^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 = x
self.touchpos.y = y

else

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

end

if (self.buttonA.x-sx)^2 + (self.buttonA.y-sy)^2 < (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,

draw = function(self)
if self.osString ~= "Android" and self.osString ~= "iOS" then
return
end


love.graphics.setColor(self.joy.color)

love.graphics.circle('fill',self.joy.x,self.joy.y,self.joy.r)


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/4)
end
}

return ScreenButton
1 change: 1 addition & 0 deletions project/src/states/CreditsState.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ local function drawFn2()
shader_screen:send("abberationVector", {strength*math.sin(love.timer.getTime()*7)/200, strength*math.cos(love.timer.getTime()*7)/200})


scrBtn:draw()
end

function CreditsState:draw()
Expand Down
1 change: 1 addition & 0 deletions project/src/states/Game.lua
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,7 @@ local function drawFn()
love.graphics.print("player x="..player.pos.x..", y="..player.pos.y,32,8)
end

scrBtn:draw()
end)

-- now we enable the shader, this will translate pixel position and colors once
Expand Down
1 change: 1 addition & 0 deletions project/src/states/StartScreen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ local function drawFn2()

love.graphics.setColor(12,24,48,128)
love.graphics.print('v' .. GAME_VERSION,282,4)
scrBtn:draw()
end
end)

Expand Down

0 comments on commit 14ccba9

Please sign in to comment.