Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
GestureEventsEasy Question — Gideros Forum

GestureEventsEasy Question

NinjadoodleNinjadoodle Member
edited February 2016 in General questions
Hi guys

I'm trying to figure out how to use @ar2rsawseen 's GestureEventsEasy class, but can't seem to find any samples.

Would anybody be able to show me, how to for example make a 'swipe right' on a sprite, using this class?

Thank you heaps in advance!

Comments

  • you just listen on the event and thats it :)

    Available events are:

    Event.SINGLE_TAP
    Event.DOUBLE_TAP
    Event.TRIPLE_TAP
    Event.LONG_TAP
    Event.SWIPE_DOWN
    Event.SWIPE_UP
    Event.SWIPE_LEFT
    Event.SWIPE_RIGHT
    Event.DRAG_START
    Event.DRAG_MOVE
    Event.DRAG_END

    to enable dragging you need to call sprite:setDragging(true)
  • NinjadoodleNinjadoodle Member
    edited February 2016
    Hi @ar2rsawseen

    Thank you for the reply :)

    I've tried doing, that but nothing happens. Here is a sample of my code ...
    function test()
    	print("right")
    end
     
    self.dial1:addEventListener(Event.SWIPE_RIGHT, test, self.dial1)
    As far as I can see it should work.

    Thanks again!
  • NinjadoodleNinjadoodle Member
    edited February 2016
    I tried playing with the code again, and it seems to work when I add the listener to the stage. However, if I try to add it to a graphic or movie-clip, strange stuff happens ...

    When added to a movie clip I seem to get a swipe response when the swipe is started outside of the movie clip, and is released over the movie clip.

    Is the class only mean to be used with the stage itself?

    Thank you in advance!
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    I see, yes it generally listens to events on stage, so if your sprite is blocking input, it could happen. Usually as far as I remember, they did not block the input.
  • NinjadoodleNinjadoodle Member
    edited February 2016
    Hi @ar2rsawseen

    Thanks for the reply!

    I worked out a way of doing it, by using a mouseDown event as well as the swipe event.

    This way I add the swipe listener to the stage and check whether a sprite has been touched.

    It's working well :)

    Thank you!
  • Apollo14Apollo14 Member
    edited February 2018
    Hi guys!
    How to detect swipe left/right? Something is wrong in my test code:
    I've added "GestureEventEasy.lua" to my project.
    In my "main.lua I've added:
    function onSwipeRightFunc()
    	print("swiped right!")
    end
    stage:addEventListener(Event.SWIPE_RIGHT, onSwipeRightFunc)
    It doesn't work as it is :(

    This code also doesn't work:
    function onSwipeRightFunc()
    	print("swiped right!")
    end
    stage:addEventListener("swipeRight", onSwipeRightFunc)
    I'm using it together with SceneManager, to change scenes with swipe.
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
  • Apollo14Apollo14 Member
    edited February 2018
    This old stuff also doesn't work:
    function onSwipeRightFunc()
    	print("swiped right!")
    end
     
    function onSwipeLeftFunc()
    	print("swiped left!")
    end
     
    function onScroll()
    	print("scrolled!")
    end
    --Detecting swipe via Detector:
    detector = SwypeDetector.new(stage, onSwipeLeftFunc, onSwipeRightFunc, onScroll)
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
  • Apollo14Apollo14 Member
    edited February 2018
    Guys, so how to detect them freaking swipes? :)
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
  • Apollo14Apollo14 Member
    edited February 2018
    aha! that stuff seems to be working :) So problem is 99% solved :)
    Guys, what methods/libs do you usually use to detect swipes?
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
  • Apollo14Apollo14 Member
    edited February 2018
    not solved :s
    Guys, why swipes are detected only in the initial scene of SceneManager?
    When app starts, on first swipe scene changes to next/current.
    But after scene changed, swipe detection does not work (app is working no errors shown, but swipe detection just doesn't work)

    First I've added class 'Mells_Gestures.lua'
    Then I've added this code to my 'main.lua':
    local sceneManager = SceneManager.new({
     	["menu"] = menu,
    	["day1"] = day1,
    	["day2"] = day2,
    	["day3"] = day3,
    	["day4"] = day4,
    	["day5"] = day5,
    	["day6"] = day6,
    	["day7"] = day7
    })
    stage:addChild(sceneManager)
    currentScene = os.date("*t").wday --get current day number
    sceneManager:changeScene("day"..currentScene) --show today's relevant scene on app start
     
    function gotoNextScene()
    	local nextScene=(currentScene%7)+1
    	sceneManager:changeScene("day"..nextScene, 1, SceneManager.moveFromRight, easing.outBack)
    	currentScene = nextScene
    end
     
    function gotoPrevScene()
    	if currentScene ==1 then currentScene = 7
    	else currentScene -=1 end
    	sceneManager:changeScene("day"..currentScene, 1, SceneManager.moveFromLeft, easing.outBack)
    end
     
    --MELL's Gestures:
    options = {minDistance = 100, maxDuration = 1.2}
    gestures = Gestures.new(stage, options)
     
    gestures:addCallback("onSwipeLeft", function(swipe, event, options)
    	print ("swiped left!")
    	gotoNextScene()
    end)
     
    gestures:addCallback("onSwipeRight", function(swipe, event, options)
    	print ("swiped right!")
    	gotoPrevScene()
    end)
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
  • When each scene is transitioned in.. have you called the sprite:setDragging(true) function as mentioned by @ar2rsawseen?
  • antix said:

    When each scene is transitioned in.. have you called the sprite:setDragging(true) function as mentioned by @ar2rsawseen?

    I've tried all variants but 'Gestures.lua' :D :D
    Now my swipe detection actually works :D
    Thx @antix ! :)
    Inside that old topic there was like 3-4 different varians or so :)

    Likes: antix

    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
    +1 -1 (+1 / -0 )Share on Facebook
  • SinisterSoftSinisterSoft Maintainer
    edited February 2018
    Gestures.lua ?
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • Apollo14Apollo14 Member
    edited February 2018
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
  • SinisterSoftSinisterSoft Maintainer
    edited February 2018
    I have written a gesture stacking system - no events - you check the stack for gestures and use them in turn. Good for games because the player can plan ahead as the game just goes through the list.
    -- settings
    local tapTime=0.3
    local tapLongTime=0.3
    local tapDisperse=7
    local swipeDistance=15
    local swipeDisperse=50
    local tapPriority=true -- true if taps take priority over swipes
     
    local insert,remove=table.insert,table.remove
    local gestureStack={}
    local gestureNames={"swipeRight","swipeLeft","swipeDown","swipeUp","tap","longTap"}
     
    local touches={}
    stage:addEventListener(Event.TOUCHES_BEGIN, function(e)
    	local t=e.touch
    	if not touches[t.id] then		
    		local touch={}
    		touch.x=t.rx
    		touch.y=t.ry
    		touch.time=os.timer()
    		touches[t.id]=touch
    	end
    end)
     
    stage:addEventListener(Event.TOUCHES_MOVE,function(e)
     
    end)
     
    stage:addEventListener(Event.TOUCHES_END,function(e)
    	local t=e.touch
    	local touch=touches[t.id]
    	if touch then --if touch is defined
    		local x,y=t.rx,t.ry
    		local dx=t.rx-touch.x -- get swipe distance
    		local dy=t.ry-touch.y
    		local adx=-dx<>dx -- get abs swipe distance
    		local ady=-dy<>dy
    		if adx<=tapDisperse and
    			ady<=tapDisperse then
    			--can be a tap
    			if os.timer()-touch.time>=tapLongTime then
    				if tapPriority then
    					insert(gestureStack,1,{6,x,y}) -- long tap
    				else
    					gestureStack[#gestureStack+1]={6,x,y} -- long tap
    				end
    			else
    				if tapPriority then
    					insert(gestureStack,1,{5,x,y}) -- tap
    				else
    					gestureStack[#gestureStack+1]={5,x,y} -- tap
    				end
    			end
    		else
    			--can be a swipe
     
    			if adx<=swipeDisperse and ady>swipeDistance then -- vertical
    				if dy<0 then -- up
    					gestureStack[#gestureStack+1]={4,dy}
    				else -- down
    					gestureStack[#gestureStack+1]={3,dy}
    				end
    			elseif adx>swipeDistance and
    				ady<=swipeDisperse then -- horizontal
    				if dx<0 then -- left
    					gestureStack[#gestureStack+1]={2,dx}
    				else -- right
    					gestureStack[#gestureStack+1]={1,dx}
    				end
    			end
    		end
    		touches[t.id]=nil
    	end
    end)
     
    function getGesture()
    	if #gestureStack>0 then
    		local id=gestureStack[1]
    		return id,gestureNames[id[1]]
    	end
    end
     
    function removeGesture()
    	if #gestureStack>0 then
    		remove(gestureStack,1)
    		return getGesture()
    	end
    end
     
    function emptyGestures()
    	gestureStack={}
    end
    In your game loop have something like this to read the gestures:
    		local gesture,gestureName=getGesture()
    		if gesture then
    			print(gesture[1],gestureName)
    			removeGesture()
    		end

    Likes: Apollo14

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 )Share on Facebook
  • I just updated the routine to also return the distance swiped.

    Likes: Apollo14

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 )Share on Facebook
  • @SinisterSoft cool thanks,
    @hgy29 please notice that "special chars" < > are being translated by the forum to html > < (see snippet post above) it's not the first time that it happens, is it some setting in the updated forum? Thanks :)

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • SinisterSoftSinisterSoft Maintainer
    edited February 2018
    Yes, the <> should actually be <> and not & lt;& gt; That bit will find the abs.
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • It forgot to mention it's also fully multi-touch - so you can tap 3 fingers to get 3 taps added, swipe and tap at the same time, etc...
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • SinisterSoftSinisterSoft Maintainer
    edited February 2018
    Made it so it's now multiplayer - if set to two players then left and right hand sides of the screen are player 1 and player 2 touch events.
    -- settings
    -- settings
    local tapTime=0.3
    local tapLongTime=0.3
    local tapDisperse=7
    local swipeDistance=15
    local swipeDisperse=50
    local tapPriority=false
    local players=1
    local width2=application:getLogicalHeight()/2
     
    local insert,remove=table.insert,table.remove
    local gestureStack={{},{}}
    local gestureNames={"swipeRight","swipeLeft","swipeDown","swipeUp","tap","longTap"}
     
    local touches={}
    stage:addEventListener(Event.TOUCHES_BEGIN, function(e)
    	local t=e.touch
    	if not touches[t.id] then		
    		local touch={}
    		local x=t.rx
    		if players==1 or x< width2 then
    			touch.player=1
    		else
    			touch.player=2
    		end
    		touch.x=x
    		touch.y=t.ry
    		touch.time=os.timer()
    		touches[t.id]=touch
    	end
    end)
     
    stage:addEventListener(Event.TOUCHES_MOVE,function(e)
     
    end)
     
    stage:addEventListener(Event.TOUCHES_END,function(e)
    	local t=e.touch
    	local touch=touches[t.id]
    	if touch then --if touch is defined
    		local stack=gestureStack[touch.player]
    		local x,y=t.rx,t.ry
    		local dx=t.rx-touch.x -- get swipe distance
    		local dy=t.ry-touch.y
    		local adx=-dx<>dx -- get abs swipe distance
    		local ady=-dy<>dy
    		if adx<=tapDisperse and
    			ady<=tapDisperse then
    			--can be a tap
    			if os.timer()-touch.time>=tapLongTime then
    				if tapPriority then
    					insert(stack,1,{6,x,y}) -- long tap
    				else
    					stack[#stack+1]={6,x,y} -- long tap
    				end
    			else
    				if tapPriority then
    					insert(stack,1,{5,x,y}) -- tap
    				else
    					stack[#stack+1]={5,x,y} -- tap
    				end
    			end
    		else
    			--can be a swipe
     
    			if adx<=swipeDisperse and ady>swipeDistance then -- vertical
    				if dy<0 then -- up
    					stack[#stack+1]={4,dy}
    				else -- down
    					stack[#stack+1]={3,dy}
    				end
    			elseif adx>swipeDistance and
    				ady<=swipeDisperse then -- horizontal
    				if dx<0 then -- left
    					stack[#stack+1]={2,dx}
    				else -- right
    					stack[#stack+1]={1,dx}
    				end
    			end
    		end
    		touches[t.id]=nil
    	end
    end)
     
    function getGesture(player)
    	local stack=gestureStack[player or 1]
    	if #stack>0 then
    		local id=stack[1]
    		return id,gestureNames[id[1]]
    	end
    end
     
    function removeGesture(player)
    	local stack=gestureStack[player or 1]
    	if #stack>0 then
    		remove(stack,1)
    		return getGesture(player)
    	end
    end
     
    function emptyGestures()
    	gestureStack={{},{}}
    end
     
    function gesturePlayers(no)
    	players=no
    	emptyGestures()
    	return
    end
     
    function gestureWidth(w)
    	width2=w/2
    end
    So if the screen size changes do this:
    gestureWidth(width)
    If the number of players changes do this:
    gesturePlayers(1)
    And for normal usage do this:
    for loop=1,#players do
    	local gesture,gestureName=getGesture(loop)
    	if gesture then
    		print("player "..loop,gesture[1],gestureName)
    		removeGesture(loop)
    	end
    end

    Likes: Apollo14, pie

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+2 / -0 )Share on Facebook
  • I just noticed a huge chunk is missing from the code above - I can see it if I edit the post. Hopefully the forum code block can be fixed soon.
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • SinisterSoftSinisterSoft Maintainer
    edited February 2018
    <width2 was causing an issue, I added a space, code looks ok now.

    Likes: Apollo14

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 )Share on Facebook
  • @SinisterSoft super cool, thank you! I'd like to make use of this asap :smiley:

    p.s. speaking about polishing gideros'website:
    I believe that the code in this post - as like as many others around - should be moved to the code snippet section to be easily found later :smile:

    Likes: Apollo14

    +1 -1 (+1 / -0 )Share on Facebook
  • @pie, I've never visited the snippets section, I shall look this afternoon :D
Sign In or Register to comment.