0 votes

I have created a touch input script for custom button class. Which is basically just extended Godot.Button class. I'm running this script in Android. I have disabled "Emulate mouse from touch" due to some mechanic problem where touch and mouse events were being called together. Because of that, I'm not receiving any "button_down/up/pressed" signal for touches. To work around that I had to write this script.

using Godot;


public class TouchButton : Button {

private protected Timer tapTimer = new Timer();
private protected const float TAP_TIME_THRESHOLD = 0.2f;
private protected InputEventScreenTouch onlyTouch = null;

public override void _Ready() {
    tapTimer.OneShot = true;
    AddChild(tapTimer);
}

public virtual void OnButtonPressed(){}

public virtual void OnButtonDown(){}
public virtual void OnButtonUp(){}

public override void _GuiInput(InputEvent @event) {
    if (@event is InputEventScreenTouch touch) {
        if (touch.Pressed) {
            if (touch.Index == 0) {
                GD.Print("Button down");
                OnButtonDown();
                onlyTouch = touch;
                if (tapTimer.IsStopped()) {
                    tapTimer.Start(TAP_TIME_THRESHOLD);
                }
            } else {
                onlyTouch = null;
            }
        } else {
            if (!tapTimer.IsStopped()) {
                tapTimer.Stop();
                GD.Print("Button pressed");
                OnButtonPressed();
            }
            GD.Print("Button up");
            OnButtonUp();
        }
    }
}

}

In above script, I'm only receiving "Button down" output in terminal. No "pressed" or "up". Is it default behavior for touch inputs or am I doing something wrong?

Thanks!

[Edit]

Here's Button that I'm working with:

using Godot;
using System;

public class Exit : TouchButton {
public override void _Ready() {
    base._Ready();
    if (OS.GetName() == "iOS") {
        this.Hide();
        return;
    }
    this.Connect("pressed", this, "OnExitPressed");
}

private protected void OnExitPressed() {
    GetTree().Quit();
}

public override void OnButtonPressed() {
    OnExitPressed();
}
}
Godot version 3.3.3.stable.mono
in Engine by (120 points)
edited by

1 Answer

0 votes
Best answer

It is currently a bug in Godot. https://github.com/godotengine/godot/issues/29525

by (120 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.