https://kotlinlang.org logo
#compose
Title
# compose
n

nglauber

12/04/2019, 11:34 PM
I’m playing with custom components in Compose… Is there a way to capture the on touch (and get the X/Y position)? 🤔
s

Shep Shapard

12/05/2019, 5:57 PM
Hi there. I work on pointer input / gestures for Compose. The underlying pointer input system is different from how Android traditionally works and right now, none of the GestureDetectors produce the actual position during movement... but rather that change in movement.
What is it that you are actually trying to achieve? 🙂
z

Zach Klippenstein (he/him) [MOD]

12/05/2019, 6:00 PM
I’m curious how do you do hit detection for buttons and stuff then? How would you write a drawing app?
👍 1
n

nglauber

12/05/2019, 6:02 PM
@Shep Shapard It’s just a sample. I want to create a custom view for a TicTacToe game. So I would like to detect which square was touch on.
z

Zach Klippenstein (he/him) [MOD]

12/05/2019, 6:31 PM
Can you use
Clickable
for that?
n

nglauber

12/05/2019, 6:31 PM
Sure I can.
I can put 9
Clickable
and I’m good to go. Ignore my example. I just want to know how to get X/Y of a touch event.
1
z

Zach Klippenstein (he/him) [MOD]

12/05/2019, 6:34 PM
I’m curious now too!
s

Shep Shapard

12/05/2019, 6:59 PM
So, given that things are still in development and in flux, there is no documentation that explains how the lower level pointer input system works.... but, you can start digging through the source of the GestureDetectors (anywhere you see the us
PointerInputWrapper
)(I'd start with PressReleasedGestureDetector) to see the lower level system in action.
Essentially GestureDetectors use PointerInputWrappers to observe and react to the stream of events. The job of GestureDetectors is to interpret the lower level stream, and eventually "filter out" the events that it wants to react to by "consuming" each event change.
Right now there simply aren't any GestureDetectors that tell you the position of the of the pointer overtime. I'm still working through what GestureDetectors should exist (and how GestureDetectors may be composed together themselves to allow simple gesture detectors to be composed together to form more complex ones).
(For example, RawDragGestureDetector currently cares about drags and flings... it doesn't know about conditions underwhich it should start. It can be composed together with say, TouchSlopExceededGestureDetector or LongPressGestureDetector, to form a DragGestureDetector that waits for the finger to move beyond a touch slop, or for a long press to occur before dragging starts.)
(woah woah woah, backing up there, I think I may have jumped the gun and gone into way more detail than was necessary to address what you were looking for): Still there is no GestureDetector that just says "a finger clicked at (x, y) because the most common use cases are simply "was this area clicked". But, looking at PressReleasedGestureDetector (and maybe PressIndicatorGestureDetector), you should be able to design your own GestureDetector that does what you want!
n

nglauber

12/05/2019, 7:24 PM
thanks for the explanation @Shep Shapard. Can I expect something easy like we had in the current UI Toolkit (just override
onTouch
)?
s

Shep Shapard

12/05/2019, 7:30 PM
@nglauber Hmm, the equivalent of overriding onTouch is something like implementing a GestureDetector and using it. I will try to make that easy. 🙂
👏 1
😊 1
z

Zach Klippenstein (he/him) [MOD]

12/05/2019, 7:38 PM
@Shep Shapard That’s great context, thanks!
f

Foso

12/05/2019, 8:18 PM
@nglauber you can use the PressGestureDetector to get X/Y position https://github.com/Foso/Jetpack-Compose-Playground/wiki/PressGestureDetector
n

nglauber

12/05/2019, 8:20 PM
awesome! thanks @Foso! 😉
Didn’t work for me @Foso 😕
s

Shep Shapard

12/05/2019, 9:01 PM
PressGestureDetector (despite its name) is actually misleadingly the wrong thing to use here. It is intended to be used for something that will display press indication. (it has other behavior related to gesture disambiguation that will likely eventually backfire). (This is something else I have to improve)
Also, @Foso, in your example, you are actually only ever getting the "press" location. The "release" location isn't provided by the gesture detector.
Also, @nglauber I assume you want to get the location where a pointer was "released" and not where it was "pressed", (you want the Up event, not the Down event, correct?).
n

nglauber

12/05/2019, 9:16 PM
For my sample, I think the down-press will work, but it would be nice to have both.
s

Shep Shapard

12/05/2019, 9:22 PM
Well, if you use
PressGestureDetector
, it will give you coordinates for the first finger that touches on an area, but it won't give you any other coordinates again until all fingers have been released and then a finger touches again (thus calling onStart with the position of that finger.
I still suggest digging into the lower level... the lower level is where you get all of the raw events, and then you can get any pointer position at any time. 😁