Page 1 of 1

Touch happens on release instead of on press (Android)

Posted: Mon Jun 25, 2018 6:01 pm
by Ats
Hi. I'm handling the touch input on Android like a mouse click using the KeyPress componant with { for the left click.
On the computer, it works perfectly on press. But on Android, the magic only occurs on touch release.

For pushing a button, it's weird but it's not a big problem. But for constant pressing, like dragging an object around, it's totally broken.
Is there a way to fix that?

By the way, the same thing is happening with the Android back button (KeyPress 253).

Thanks !

Re: Touch happens on release instead of on press (Android)

Posted: Mon Jun 25, 2018 6:13 pm
by Kjell
Hi Ats,

On Android you should use the touchGetCount, touchGetX, touchGetY and touchGetID functions to properly deal with touch events.

K

Re: Touch happens on release instead of on press (Android)

Posted: Mon Jun 25, 2018 6:49 pm
by Ats
Thanks. I didn't saw that in the scripting reference :oops:
So I have another question because it seems to be working differently:
App.MousePosition.X is moving between -1 and 1. But what about touchGetX?

Re: Touch happens on release instead of on press (Android)

Posted: Mon Jun 25, 2018 7:21 pm
by Ats
Oh ok... I've just verified and everything's perfect:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2" AndroidPackageName="com.mydomain.touchscreen">
  <OnLoaded>
    <SetAppState State="AppState"/>
  </OnLoaded>
  <States>
    <AppState Name="AppState">
      <OnUpdate>
        <ZExpression>
          <Expression>
<![CDATA[//if (touchGetCount>0){
touchX = touchGetX(0);
touchY = touchGetY(0);
//}else{
mouseX = App.MousePosition.X;
mouseY = App.MousePosition.Y;
//}]]>
          </Expression>
        </ZExpression>
      </OnUpdate>
      <OnRender>
        <UseMaterial Material="FontMaterial"/>
        <RenderText TextFloatRef="mouseX" X="-0.4" Y="0.4" Scale="0.4" FloatMultiply="100"/>
        <RenderText TextFloatRef="mouseY" X="0.4" Y="0.4" Scale="0.4" FloatMultiply="100"/>
        <RenderText TextFloatRef="touchX" X="-0.4" Y="-0.4" Scale="0.4" FloatMultiply="100"/>
        <RenderText TextFloatRef="touchY" X="0.4" Y="-0.4" Scale="0.4" FloatMultiply="100"/>
      </OnRender>
    </AppState>
  </States>
  <Content>
    <Variable Name="touchX"/>
    <Variable Name="touchY"/>
    <Variable Name="mouseX"/>
    <Variable Name="mouseY"/>
    <Bitmap Name="FontBitmap" Width="256" Height="32">
      <Producers>
        <BitmapFromFile Transparency="1" DataWidth="256" DataHeight="32">
          <BitmapFile>
<![CDATA[78DAED9BC19284300844E7FF7F7AF7B45596A3041A8831FBDE51CB4CC49634C4F97C0000000000000000000076E6E78FCB83478815A07F80EDF56F1C07D84FFCBC020000FFD3F99C523DFE1FD03FFA070000003879A726AF7579A1FF2AC3BCD95ECE73D5F0E78C012FA3710AE670F2978FC038E59F463E1A43CF3C3C6E8731A42859ABEBD41A7DA73C8F3BA97FCF2D0BD118EABF291A2565A3B0D99ACC724DD5AB9CFF3369CD8864F4B1CA031626A8687BE138DA5DA0AAD27526C93B63924C9B210FD09DD2F3FA37EE42D6BF16B1AAB4369C6DD4937C2BFC38A0712AB3F226FD6AF4A5084DC3693EE7B89A92E5A37CC05737A593B624B3FCE54F9578636D356CCDFF99D94E36F91D5583B6D4CEAC50A205F5CAE1D58ADC6EFFAFBD5C8227E9EBFF8466D2A77FFB54B48720F77FB4E655F27975EB7F3F6F0000008B94B1EBCCDC5885E7377B93CB6EA67915328A8600E4268FBF7E8956D9E8DF1FCFF2F25F1827F3D6084ACE6C06556DDE7D223BBCD1DD61D6A9E86B3BB3035CF55B9E2A2FB4E5A4B5A1B4E2DDAFE1D686EA7EF95FFB7BDA7BF53FECE408B76C6F2F66BC56288D8716AFA7FC8FF32304F4BF4D5E9A1CA8EF9D5C63264FE9BFF50BA2FC039AE9FF9F2A6AEEEC4A55D57037EC225B630FE699772D01ADFD9F05EBFA4CD72854FF6A1B85D16FD21E11FF2F2CB9AB28]]>
          </BitmapFile>
        </BitmapFromFile>
      </Producers>
    </Bitmap>
    <Font Name="Font" Bitmap="FontBitmap" FirstChar="33" CharPixelWidth="8" CharPixelHeight="8" BorderPixels="1"/>
    <Material Name="FontMaterial" Blend="1" Font="Font">
      <Textures>
        <MaterialTexture Texture="FontBitmap" TexCoords="1"/>
      </Textures>
    </Material>
  </Content>
</ZApplication>
So I must have a poop in my code :lol:

Re: Touch happens on release instead of on press (Android)

Posted: Mon Jun 25, 2018 7:29 pm
by Kjell
Hi Ats,
Ats wrote:App.MousePosition.X is moving between -1 and 1. But what about touchGetX?
Should be the same, i double-checked the source ... touchGetX/Y ( ZExpressions.pas ) calls Platform_TouchGetPos ( ZPlatform_Android.inc ) to retrieve the pixel-coordinates of a touch-event and then uses NormalizeToScreen ( ZApplication.pas ) to convert the pixel coordinates into homogeneous coordinates.

+ Ville, what i don't understand however is why the AndroidTouch record uses Integers for the X / Y coordinates .. is sub-pixel accuracy something that got introduced in a more recent Android SDK or something?

K

Re: Touch happens on release instead of on press (Android)

Posted: Mon Jun 25, 2018 10:45 pm
by Ats
So here's my setup to automatically handle the mouse or touchscreen:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnUpdate>
    <ZExpression Comment="Reset Controls">
      <Expression>
<![CDATA[Mouse.X = 0;
Mouse.Y = 0;
MouseClick = 0;]]>
      </Expression>
    </ZExpression>
    <Condition Comment="ANDROID ?" Expression="return ANDROID;">
      <OnTrue>
        <ZExpression Comment="Touchscreen">
          <Expression>
<![CDATA[if (touchGetCount()>0){
  Mouse.X = touchGetX(0);
  Mouse.Y = touchGetY(0);
  MouseClick = 1;
}]]>
          </Expression>
        </ZExpression>
      </OnTrue>
      <OnFalse>
        <KeyPress Comment="Mouse click" Keys="{">
          <OnPressed>
            <ZExpression Comment="Touchscreen">
              <Expression>
<![CDATA[Mouse.X = App.MousePosition.X;
Mouse.Y = App.MousePosition.Y;
MouseClick = 1;]]>
              </Expression>
            </ZExpression>
          </OnPressed>
        </KeyPress>
      </OnFalse>
    </Condition>
  </OnUpdate>
  <Content>
    <Variable Name="Mouse" Type="6"/>
    <Variable Name="MouseClick" Type="4"/>
  </Content>
</ZApplication>
Then I just check the MouseClick wherever I need it in the States or Models:

Code: Select all

if (MouseClick) {
 // do some stuff with Mouse.X and Mouse.Y
}

Re: Touch happens on release instead of on press (Android)

Posted: Wed Jun 27, 2018 6:14 am
by Ats
By the way, can I handle all the controls test (keyboard, mouse, gamepad) in App - ZApplication / OnUpdate instead of making the same tests over and over again in different AppStates? I won't have bad surprises with reactivity or something else?

Re: Touch happens on release instead of on press (Android)

Posted: Wed Jun 27, 2018 12:57 pm
by Kjell
Hi Ats,
Ats wrote:By the way, can I handle all the controls test (keyboard, mouse, gamepad) in App - ZApplication / OnUpdate instead of making the same tests over and over again in different AppStates? I won't have bad surprises with reactivity or something else?
There's no difference in performance between putting them in App.OnUpdate and AppState.OnUpdate .. so if you need something throughout multiple AppStates it's generally a good idea to put it in App.OnUpdate ( opposed to copying it to each AppState ).

Alternatively you could wrap the components in a Group in App.Content and use CallComponent from each AppState that requires the tests, but for input handling that's usually not worth the effort.

K