Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AGS 4: Touch Input API (2) #2692

Draft
wants to merge 3 commits into
base: ags4
Choose a base branch
from

Conversation

ivan-mogilko
Copy link
Contributor

Resolves #1538
This is a recreation of #2440.

Added following API to the script:

managed struct TouchPointer {
  /// Gets this touch pointer's identifier.
  import readonly attribute int ID;
  /// Gets whether this touch pointer is currently down (in touching state).
  import readonly attribute bool IsDown;
  /// Gets this touch pointer's X screen coordinate.
  import readonly attribute int X;
  /// Gets this touch pointer's Y screen coordinate.
  import readonly attribute int Y;
};

builtin struct Touch {
  /// Number of touch pointers registered by the game engine.
  readonly import static attribute int TouchPointerCount;  // $AUTOCOMPLETESTATICONLY$
  /// Accesses a touch pointer state by its identifier.
  readonly import static attribute TouchPointer* TouchPointers[];  // $AUTOCOMPLETESTATICONLY$
};

The Touch.TouchPointers[] indexed property has an underlying list that only grows during a game session, but never shrinks or resets. It cannot be preallocated, because the backend does not always have ability to tell how many pointers / fingers are supported by the connected touch devices. That is why it starts at zero size, and keep growing as more simultaneous touches are detected.

TouchPointer struct describes a single "touch move" by a Nth pointing device, whatever that might be (a finger, or a mouse in case of mouse to touch emulation, etc). It's ID matches the index of TouchPointers[]. IsDown tells whether this pointer is currently down (touches the sensor device), and where at. More properties could be added later if necessary.

Normally user should iterate over TouchPointers array, looking for the states of all recorded TouchPointers, and maybe remember their states and compare with the last saved state to see how they are changed.

Because multiple touch events may occur within a single game frame, there are 3 new script callbacks supported, that let to react to them with more precision:

function on_pointer_down(int pointer, int x, int y);
function on_pointer_up(int pointer, int x, int y);
function on_pointer_move(int pointer, int x, int y);

where "int pointer" argument is the TouchPointer index, and x,y is the new/last position of a pointer.

Other additions: added eInputTouch to InputType enum for consistency.


NOTES on implementation.

I had to create duplicate "touch" events recorded as SDL_UserEvent in our input queue. Primary reason is that input queue is processed with a delay, and our internal "pointer id" cannot be reliably calculated anymore after the finger is unpressed. So it has to be recorded along with the event in queue. This user event stores AGS_TouchPointerEventData with pointer id, and a position already in absolute window coordinates (SDL finger events have it in [0,1] relative coordinates).

At the time touch events do not affect anything in game state, rather than calling script callbacks. I.e. they do not skip display boxes, cutscenes etc. But this may be implemented later.
(At least display boxes skip must be implemented, I suppose, as no script is run when they are shown, so user cannot script their removal with a touch.)


TODO

Besides few things mentioned above, I think we may benefit from a config setting that enables mouse-to-touch emulation, so that one could test at least 1 finger touch without having a touch device.

@ivan-mogilko ivan-mogilko added type: enhancement a suggestion or necessity to have something improved ags 4 related to the ags4 development context: script api context: input labels Feb 24, 2025
@ivan-mogilko ivan-mogilko changed the base branch from master to ags4 February 24, 2025 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ags 4 related to the ags4 development context: input context: script api type: enhancement a suggestion or necessity to have something improved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enhancement: Proper touch input support
1 participant