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();
}
}