Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Manually fire a touch event?
Started by GrahamRanson Dec 08 2017 02:16 AM

- - - - -
5 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

GrahamRanson

[GLOBAL: userInfoPane.html]
GrahamRanson
  • Enthusiast

  • 60 posts
  • Corona SDK

We require the ability to manually fire a non-runtime touch event that all required display objects get told about.

 

I've tried a couple of things so far, such as firing a Runtime touch event and then checking for all objects that are in that position, and also manually firing non-runtime touch events specifically on each of those intersecting objects but due to events not propagating properly, and the lack of touch IDs etc, these solutions aren't working for us.

 

What I want to be able to do is basically what Corona must be doing with the mouse in both the simulator and desktop builds, where on press it fires required touch events that all work as if they really are touch events.

 

Is it possible to hook into this? If not then I'll make a feature request.



[TOPIC: post.html]
#2

Alan PlantPot

[GLOBAL: userInfoPane.html]
Alan PlantPot
  • Contributor

  • 848 posts
  • Corona SDK

If there is no "official" way to do it, the only way I can think of is to do this is a bit clunky:

 

  • Loop through all of the objects to find out which ones are in the relevant xy position. 
  • Manually call your touch function(s) passing in each of those objects as the target
local function getObjectsInClickPosition(x, y)
	--check objects and return table of all objects that are in the fake touch location
	--I'll assume you have some code for checking positions already
	return objects
end

local fakeX, fakeY = 300, 160

local touchedObjects = getObjectsInClickPosition(fakeX, fakeY)

for i = 1, #touchedObjects do
	myTouchFunction({target = touchedObjects[i], x = fakeX, y = fakeY, id = someFakeIDNumber, phase = "whateverPhaseYouNeed"})
end

Edit: You can also manually access the touch event associated with an object too, so you can call of the objects own functions rather than having one touch function with lots of if/elseif for different cases:

for i = 1, #touchedObjects do
	if touchedObjects[i]._functionListeners then
		if touchedObjects[i]._functionListeners.touch and touchedObjects[i]._functionListeners.touch[1] then
			touchedObjects[i]._functionListeners.touch[1]({target = touchedObjects[i], x = fakeX, y = fakeY, id = someFakeIDNumber, phase = "whateverPhaseYouNeed"})
		elseif touchedObjects[i]._functionListeners.tap and touchedObjects[i]._functionListeners.tap[1] then
			touchedObjects[i]._functionListeners.tap[1]({target = touchedObjects[i], x = fakeX, y = fakeY, id = someFakeIDNumber, phase = "whateverPhaseYouNeed"})
		end
	end
end


[TOPIC: post.html]
#3

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Contributor

  • 943 posts
  • Enterprise

tricky parts would be making sure you get the z-order correct for propagation (particularly in nested groups), handling propagation cancel, respecting isVisible and isHitTestable (particularly if isVisible was set false at group level) and masking... that's when you start wishing for a backdoor "simulateTouch(event)"



[TOPIC: post.html]
#4

GrahamRanson

[GLOBAL: userInfoPane.html]
GrahamRanson
  • Enthusiast

  • 60 posts
  • Corona SDK

@Alan - Yea that's basically what I started with and it was working quite well until I hit the problem points that Dave mentioned. I'm sure with enough time and energy they could all be worked around, however, a simulateTouch function would be perfect.

 

I've just created a feature request for it so if it's something you think you might find useful please consider throwing some votes towards it - http://feedback.coronalabs.com/forums/188732-corona-feature-requests-feedback/suggestions/32583772-a-manual-simulatetouch-event



[TOPIC: post.html]
#5

Sphere Game Studios

[GLOBAL: userInfoPane.html]
Sphere Game Studios
  • Corona Geek

  • 1,358 posts
  • Corona SDK

I've asked for ray-casting to be added which would do exactly what you want but got told it wasn't going to happen anytime soon. 

 

Unity has this and it returns a collection of all objects "hit" in the order they were hit.



[TOPIC: post.html]
#6

GrahamRanson

[GLOBAL: userInfoPane.html]
GrahamRanson
  • Enthusiast

  • 60 posts
  • Corona SDK

Ray casting would certainly work for this, and probably a lot more use cases then I need and thus help a lot more people, but what I need would presumably be simpler. I don't need a list of hit objects or anything like that, I just want to do what the mouse must already do.




[topic_controls]
[/topic_controls]