Skip to content

Input system design

GuoLei edited this page Oct 1, 2021 · 6 revisions

Whether to unify single-point Touch and Mouse events needs to be discussed. From the point of view of ease of use for developers, I think we need to unify! Before we only consider mouse, but now we should consider mobile device(touch). So the call back named onMouseXX() is not good, I think use onPointerXX() instead onMouseXX() or onTouchXX() is better for modern design.

Schematic diagram

image-20211001164136273

API Design

UML: image

class Script
{
    .......
   
    /**
     * Called when the pointer is down while over the Collider.
     */
    onPointerDown(): void {}
  
    /**
     * Called when the pointer is up while over the Collider.
     */
    onPointerUp(): void {}

    /**
     * Called when the pointer is down and up with the same collider.
     */
    onPointerClick(): void {}
  
    /**
     * Called when the pointer is enters the Collider.
     */
    onPointerEnter(): void {}
  
    /**
     * Called when the pointer is no longer over the Collider.
     */
    onPointerExit(): void {}

    /**
     * Called when the pointer is down while over the Collider and is still holding down.
     * @remarks onMouseDrag is called every frame while the pointer is down.
     */
    onPointerDrag(): void {}
   
   ......
}

/**
 * Input Manager.
 */
export class InputManager {
  /** Whether to handle multi-touch. */
  multiTouchEnabled: boolean;

  /**
   * The pointer list of the current frame.
   */
  get pointers(): Readonly<Pointer[]> {
    return null;
  }
}

/**
 * Description of pointer information.
 */
export class Pointer {
  /** The unique identifier for the pointer. */
  id: number;
  /** The position of the pointer in screen space pixel coordinates. */
  position: Vector2 = new Vector2();
}