How do I make mobile gestures? (SOLVED)

How do I make and use mobile gestures for function on_input and even with game.input_binding? Even how do I use touch at all.

Thanks in advance.

have a look here


or here

BTW you simply say

if action_id == hash("touch") then
--do something
end

For handling normal clicks. :point_up_2:

1 Like

sheesh so much code :thinking:

@britzl Im wondering why not put this in the engine’s inputs? its really good

+1 for this :smile:

What do you mean, so much code?

STEP 1
Open game.project and add a dependency to https://github.com/britzl/defold-input/archive/1.3.0.zip

STEP 2
In whichever script that you want to detect gestures do this:

local gesture = require "in.gesture"

function init(self)
	msg.post(".", "acquire_input_focus")
end

function on_input(self, action_id, action)
	local g = gesture.on_input(self, action_id, action)
	if g then
		if g.swipe_right then
			print(g.swipe.from, g.swipe.to)
		elseif g.tap then
			print(g.tap.position)
		elseif g.double_tap then
			print(g.double_tap.position)
		elseif g.long_press then
			print(g.long_press.position, g.long_press.time)
		elseif g.two_finger.tap then
			print("Two finger tap!")
		elseif g.two_finger.pinch then
			print(g.two_finger.pinch.ratio, g.two_finger.center)
		end
	end
end

Alternative STEP2
Add gesture.script to a game object from which you wish to detect gestures. In another script on the same game object do this:

function on_message(self, message_id, message, sender)
	if message_id == hash("on_gesture") then
		if message.swipe_right then
			print(message.swipe.from, message.swipe.to)
		elseif message.tap then
			print(message.tap.position)
		elseif message.double_tap then
			print(message.double_tap.position)
		elseif message.long_press then
			print(g.long_press.position, message.long_press.time)
		end
	end
end
3 Likes

In general we want to provide the basic building blocks in the engine. In this case basic input capabilities to detect keys, mouse, text and touch. We do not wish to increase the engine size by adding all sorts of higher level concepts that might not be needed by everyone.

With the basic building blocks any user can create more complex and higher level functionality. In this case gesture detection based on the basic building block for detecting touch.

1 Like

Already solved it but lot of code anyway

What do you mean by “lot of code anyway”? I’m not letting you get away that easy :slight_smile:

Yes, there is a bit of code involved in detecting a swipe or a pinch gesture, but since the code is put into a library project that you can depend upon and use there really is very little code that you need to write yourself. It wouldn’t be much different if we had added support for gestures in the engine.

1 Like

Didn’t expect that much of a code for gesture input :no_mouth: and yeah I barely had to write 2 lines of code

1 Like

Detecting gestures requires a fair amount of logic. Here is the documentation for apple’s gesture library. There’s a lot of stuff in there so the library code itself is most likely pretty large: https://developer.apple.com/documentation/uikit/uigesturerecognizer

3 Likes

@britzl Thank you so much for Defold-Input library!

3 Likes