Page 1 of 1

Buttons on android?

Posted: Fri Nov 13, 2020 6:15 am
by ë-talian
Hi,
I'm planning on making a top down shooter game on android for android but I can't seem to get the buttons right.

I used GUI libraries like "SUIT" but it's doesn't support hold or I can't configure how. If you know how to make button (doesn't need to be complex just supports hold movement) please help me out. If you want to join me on my "On Android For Android" just tell me.

Thanks in advance. ^^

Re: Buttons on android?

Posted: Tue Nov 17, 2020 8:51 am
by norubal
I can't name library for android GUI, but this is code what I use:
(I tried to comment in English, my bad if you can't understand well.)

Code: Select all

-- love.touchpressed / love.touchmoved / love.touchreleased
-- Those are essential callback function for android touch event handling
-- But those callback functions won't call if you click mouse at PC, so you have to call it manually if you want to call it.

-- Code below will store touch inputs to touches{} table.
-- touches[id] = {x = x coords, y = y coords, time = how much time it got pressed}
-- You can add another informations if you want.

local touches = {}

function love.load()	
    count = 0
end

function love.draw()
	local k, v
	for k, v in pairs(touches) do
		love.graphics.print(tostring(k) .. ":" .. math.floor(v.x) .. ":" .. math.floor(v.y) .. ":".. math.floor(v.time), v.x, v.y)
	end
	love.graphics.print(count, 0, 0)
end

function love.touchpressed(id, x, y)
	touches[id] = {x = x, y = y, time = 0}
	count = count + 1
end

function love.touchmoved(id, x, y)
	if touches[id] ~= nil then
		touches[id].x = x
		touches[id].y = y
	end
end

function love.touchreleased(id, x, y)
	touches[id] = nil
	count = count - 1
end

function love.update(dt)
	-- store time to each touch gestures
	local k, v
	for k, v in pairs(touches) do
		v.time = v.time + dt
	end
end

----- callback function for mouse - for PC -----

function love.mousepressed(x, y, button, istouch)
	if istouch == false then 
		love.touchpressed("mouse", x, y)
	end
end

function love.mousemoved(x, y, dx, dy, istouch)
	if istouch == false then
		if love.mouse.isDown(1) then
			love.touchmoved("mouse", x, y)
		end
	end
end

function love.mousereleased(x, y, button, istouch)
	if istouch == false then
		love.touchreleased("mouse", x, y)
	end
end
--------------------------------------------------
So if you want to separate between long press and short-tap, you should set threshold and check touch[id].time like:

Code: Select all

function love.touchreleased(id, x, y)
	touches[id] = nil
	count = count - 1
	
	-- psudocode warning!
	if button.clicked(button.x, button.y, button.width, button.height, x, y) == true then
		if touches[id].time >= 1 then -- let's assume threshold is 1 sec
			-- long-press
			print("user long-pressed button")
		else
			-- short-tap
			print("user short-tapped button")
		end
	end
end
Not sure if it will solve your problem, but I hope it helped.

Re: Buttons on android?

Posted: Tue Nov 17, 2020 9:21 am
by ë-talian
Hey, norubal,

Thank you for taking the time to reply and help me,
But as I am a beginner, I don't understand well.

If you have a discord account please join this server for future projects too.
We can work together and if you want you can join my project "From Android to Android".

Discord link: https://discord.gg/zmPNMTu4vg
Please join and let's help each other.

Thanks. :megagrin:

Re: Buttons on android?

Posted: Tue Nov 17, 2020 11:56 am
by zorg
or you can just join the löve discord instead...

Re: Buttons on android?

Posted: Sun Dec 13, 2020 6:03 pm
by Gunroar:Cannon()
You could use gooi and gooi.newButton():onPress(),blah , blah, if I'm correct. Learn more: https://love2d.org/forums/viewtopic.php?t=79751
or you could use double joysticks because goois joystick isn't the best: https://love2d.org/forums/viewtopic.php?t=79732