Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Is this the best way to make a swipe left / right class? - Page 2 — Gideros Forum

Is this the best way to make a swipe left / right class?

2»

Comments

  • MellsMells Guru
    edited October 2013
    @ar2rsawseen I understand that you don't have enough time for it, I believe that would largely benefit new users to have access to that swipe class (for a start, with a lot of others :) ).

    I didn't understand how to implement @adispose 's code.
    self.onFlingY(self.parent, self.lastX - event.touch.x, self.startY - event.touch.y)
    Where is self.onFlingY defined?
    It is given as a parameter to the class constructor but where is it defined?
    (I am not asking you to answer)

    Also in the following :
    function SwypeDetector:doScroll(event)
    	if onScroll ~= nil then
    (...)
    Shouldn't it be
    if self.onScroll
    ?
    To my understanding, onScroll is not in the scope of function SwypeDetector:doScroll(event).

    Also
    function SwypeDetector:doScroll(event)
    	if *onScroll* ~= nil then
    		self.*onFlingY*(self.parent, self.lastX - event.touch.x, self.startY - event.touch.y)
    	end
    end
    shouldn't be :
    function SwypeDetector:doScroll(event)
    	if *self.onScroll* ~= nil then
    		self.*onScroll*(self.parent, self.lastX - event.touch.x, self.startY - event.touch.y)
    	end
    end
    ?

    I didn't ask you about this because I know you don't have time to support someone else's code, and I thought it would take me even more time to understand if there are mistakes or I am misunderstanding.
    Nobody's been reporting mistakes, so maybe it's me.

    I will try @Tom2012 's one.
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • MellsMells Guru
    edited October 2013
    Here is a version that I have built on top of different sources shared on the forum, and I have added several tricks that make it easy for me to customize my apps.

    I have tried to comment the code, for what it's worth.

    How to use :
     
    --[[
    -- ------------------------------------------------
    -- CREATE AN INSTANCE OF THE GESTURES CLASS
    -- ------------------------------------------------
    	options (optional):		
    		- minDistance (int : default -> 100). The minimum distance to take swipes into consideration.
    		- maxDuration (float : default -> 0.5). The maximum duration to take swipes into consideration.
    ]]--
    local options = {minDistance = 100, maxDuration = 0.5}
    local gestures = Gestures.new(stage, options)
     
     
     
    --[[
    -- ------------------------------------------------
    -- ADD SWIPE CALLBACKS
    -- ------------------------------------------------
    Accepted : 	onSwipeUp, onSwipeDown, onSwipeLeft, onSwipeRight, onSwipeUpLeft, onSwipeUpRight, onSwipeDownLeft, onSwipeRight	
    Note : 		Uncomment below to overwrite the default callbacks
    -----------------------------
    ]]-- 
    gestures:addCallback("onSwipeUp", function(swipe, event, options) print ("Custom", "["..swipe.."]") end)
    --gestures:addCallback("onSwipeDown", function(swipe, event, options) print ("Custom", "["..swipe.."]") end)
    --gestures:addCallback("onSwipeLeft", function(swipe, event, options) print ("Custom", "["..swipe.."]") end)
    --gestures:addCallback("onSwipeRight", function(swipe, event, options) print ("Custom", "["..swipe.."]") end)
     
     
    --[[
    -- ------------------------------------------------
    -- ADD TOUCHES CALLBACKS
    -- ------------------------------------------------
    Accepted : 	touchesBegin, touchesMove, touchesEnd
    Note : 		Uncomment below to overwrite the default callbacks
    -----------------------------
    ]]--
    --gestures:addCallback("touchesBegin", function(type, event, options) print ("On Touches Move >", event:getType(), "["..event.touch.x..":"..event.touch.y.."]") end)
    --gestures:addCallback("touchesMove", function(type, event, options) print ("On Touches Move >", event:getType(), "["..event.touch.x..":"..event.touch.y.."]") end)
    gestures:addCallback("touchesEnd", function(type, event, options) print ("", "On Touches End >", event:getType(), "["..event.touch.x..":"..event.touch.y.."]") end)
     
    --[[
     
    -- ------------------------------------------------
    -- MORE
    -- ------------------------------------------------
    -- Set gestures to inactive
    gestures:setActive(false)
     
    -- Check if gestures are active
    gestures:isActive()
     
    -- Remove all Callbacks
    gestures:removeCallbacks()
     
     
    -- Get all Callbacks
    gestures:getCallbacks()
     
     
    -- Remove callbacks
    gestures:removeCallback("touchesBegin")
     
    -- Free gestures
    gestures:free()
     
    ]]--
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • MellsMells Guru
    edited October 2013
    And here is the class.
    --[[
     
    	### GESTURES ###
     
    	File :			Mells_Gestures.lua
    	Date :			2013.10.03
    	Author : 		Mells
    	Description : 	Simple Gestures Class to recognize Swipes on a Sprite, and easily add callbacks.
    					Inspired by different classes shared by the Gideros community.
    ]]--
     
     
    Gestures = Core.class(EventDispatcher)
     
    -- ----------------------
    -- Output true > print Swipe types in the console
    -- ----------------------
    local output = false
     
    function Gestures:init(parent, options)
     
    	self.parent = parent
     
    	-- -----------------------
    	-- Setup Variables
    	-- -----------------------
    	local conf = {
    		maxDuration = 0.5,
    		minDistance = 70		
    	}
    	if options then
    		for i, option in pairs(options) do
    			conf[i] = option
    		end	
    	end
     
    	self.options = conf
     
     
    	-- -----------------------
    	-- Add Event Listeners
    	-- -----------------------
    	parent:addEventListener(Event.TOUCHES_BEGIN, self.onTouchBegin, self)
    	parent:addEventListener(Event.TOUCHES_MOVE, self.onTouchMove, self)
    	parent:addEventListener(Event.TOUCHES_END, self.onTouchEnd, self)
     
    	-- -----------------------
    	-- Set Active
    	-- -----------------------
    	self:setActive(true)
     
     
    	-- Add Native callbacks :
    	-- Accepted : onSwipeUp, onSwipeDown, onSwipeLeft, onSwipeRight, onSwipeUpLeft, onSwipeUpRight, onSwipeDownLeft, onSwipeRight	
    	local swipeCallbacks = {"onSwipeUp", "onSwipeDown", "onSwipeLeft", "onSwipeRight", "onSwipeUpLeft", "onSwipeUpRight", "onSwipeDownLeft", "onSwipeDownRight"}
    	local swipeCallback
     
    	print ("------------------")
    	print ("Add Callbacks")
    	print ("------------------")
    	for i=1, #swipeCallbacks, 1 do
    		swipeCallback = swipeCallbacks[i]
    		self[swipeCallback] = function(callback) print ("Default ["..callback.."]") end		
    		self:addCallback(swipeCallback,  self[swipeCallback])
    	end
     
    end
     
    -- ------------------------------------
     
    function Gestures:setActive(bool)
    	self.active = bool
    end
     
    -- ------------------------------------
     
    function Gestures:isActive()
    	if self.active == true then return true end
    	return false
    end
     
    -- ------------------------------------
     
    function Gestures:onTouchBegin(event)
     
    	-- Is Active?
    	if not(self:isActive()) then return end
    	if not self.parent:hitTestPoint(event.touch.x, event.touch.y) then return end
     
    	if self.timer then
    		self.timer:stop()
    	end
    	self.swipeDirection = nil
     
     
    	self.startX = event.touch.x
    	self.startY = event.touch.y
    	self.lastX = event.touch.x
    	self.lastY = event.touch.y
    	self.activeTouchId = event.touch.id
     
    	-- --------------------
    	-- Duration & Distance
    	-- --------------------
    	self.swipeDuration = 0
     
    	-- -------------
    	-- Add timer
    	-- -------------
    	local timer = Timer.new(100, 10)
    	self.timer = timer
    	timer:addEventListener(Event.TIMER, self.onSwipeTimer, self)
    	timer:addEventListener(Event.TIMER_COMPLETE, function() self.timer:stop() self.canSwipe = false end)
    	timer:start()	
     
    	-- -----------
    	-- CALLBACK
    	-- -----------
    	self:runCallback(event:getType(), event)
     
    	-- -----------
    	-- Stop Propagation	
    	-- -----------
    	event:stopPropagation()	
     
    end
     
    -- ----------------------------------------
     
    function Gestures:onTouchMove(event)
     
    	self.lastX = event.touch.x
    	self.lastY = event.touch.y
     
    	-- -----------
    	-- CALLBACK
    	-- -----------
    	self:runCallback(event:getType(), event)
     
    	-- -----------
    	-- Stop Propagation
    	-- -----------
    	event:stopPropagation()
     
    end
     
     
    -- ----------------------------------------
     
    function Gestures:onTouchEnd(event)
     
    	local options = self.options
    	local up, down, left, right
     
    	-- ------------------------
    	-- Check 
    	-- ------------------------
    	if self.activeTouchId == event.touch.id then
     
    		self.timer:stop()
    		self.timer = nil		
     
    		-- ------------------------
    		-- Check Swipe Duration
    		-- ------------------------
    		if self.swipeDuration < options.maxDuration*0.99 then
     
    				-- ----------------------
    				-- GET SWIPE DIRECTION
    				-- ----------------------				
    				-- LEFT
    				if(event.touch.x < self.startX - options.minDistance) then					
    					left = true
    				-- RIGHT	
    				elseif(event.touch.x > self.startX + options.minDistance) then
    					right = true								
    				end
     
    				-- UP
    				if(event.touch.y < self.startY - options.minDistance) then					
    					up = true
    				-- DOWN
    				elseif(event.touch.y > self.startY + options.minDistance) then
    					down = true									
    				end
     
    				-- ----------------------
    				-- SET SWIPE DIRECTION
    				-- ----------------------				
    				if up then				
    					if left then self.swipeDirection = "onSwipeUpLeft"
    					elseif right then self.swipeDirection = "onSwipeUpRight"
    					else self.swipeDirection = "onSwipeUp"						
    					end
     
    				elseif down then				
    					if left then self.swipeDirection = "onSwipeDownLeft"
    					elseif right then self.swipeDirection = "onSwipeDownRight"
    					else self.swipeDirection = "onSwipeDown"
    					end
    				elseif left then self.swipeDirection = "onSwipeLeft"
    				elseif right then self.swipeDirection = "onSwipeRight"
    				end
     
    				-- -----------
    				-- CALLBACK
    				-- -----------
    				self:runCallback(self.swipeDirection, event, {duration = self.swipeDuration, start = {self.startX, self.startY}})				
    		end
     
    		-- ----------------------
    		-- Reset
    		-- ----------------------
    		self.swipeDuration = 0
    		self.startX = nil
    		self.startY = nil
    		self.lastX = nil
    		self.lastY = nil
    		self.activeTouchId = nil		
     
    		-- -----------
    		-- CALLBACK
    		-- -----------
    		self:runCallback(event:getType(), event)
     
    		-- -----------
    		-- Stop Propagation
    		-- -----------
    		event:stopPropagation()
     
    	end
     
    end
     
     
    -- ----------------------------------------
     
    function Gestures:onSwipeTimer(event)
    	self.swipeDuration = self.swipeDuration + .1
    end
     
     
    -- -------------------------------------------
    -- Accepted : "touchesBegin", "touchesMove", "touchesEnd"
    function Gestures:addCallback(id, callback)
    	if output then print ("Add Callback : ", id) end
    	self.callbacks = self.callbacks or {}
    	self.callbacks[id] = callback		
    end
     
    -- -------------------------------------------
     
    function Gestures:getCallbacks()
    	return self.callbacks
    end
     
    -- -------------------------------------------
     
    function Gestures:removeCallbacks(id)
    	print ("------------------")
    	print ("Remove callbacks")
    	print ("------------------")
    	local callbacks = self:getCallbacks()
    	for i, callback in pairs(callbacks) do
    		self:removeCallback(i)
    	end	
    	callback = nil
    end
     
    -- -------------------------------------------
     
    function Gestures:removeCallback(id)
    	local callbacks = self:getCallbacks()
    	local flag
    	for i, callback in pairs(callbacks) do		
    		if id == i then flag = i end
    	end
     
    	if flag then
    		if output then print ("Remove Callback : ", flag) end
    		self[flag] = nil
    		callbacks[flag] = nil
    	end
    end
     
     
    -- ----------------------------------------
     
    function Gestures:runCallback(type, event, options)	
    	local callbacks = self:getCallbacks()
     
    	-- -----------
    	-- CALLBACK
    	-- -----------
    	if self.callbacks and self.callbacks[type] then self.callbacks[type](type, event, options) end
    end
     
    -- -------------------------------------------
     
    function Gestures:free()	
    	local timer = self.timer	
    	if timer then
    		timer:removeEventListener(Event.TIMER, self.onSwipeTimer, self)
    		timer:removeEventListener(Event.TIMER_COMPLETE, function() self.timer:stop() self.canSwipe = false end)
    		timer:stop()
    		timer = nil
    	end	
     
    	self:removeCallbacks()	
    end
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • ar2rsawseenar2rsawseen Maintainer
    edited October 2013
    Wow great, also most probably we need a lib for basic gesture events like long tap and double clicks etc
    which I will need to try my hand on :)
Sign In or Register to comment.