Skip to content

Commit

Permalink
💥✨Introduced PointerEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
carefree0910 committed Nov 5, 2024
1 parent 25eb61e commit e10fb3b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
16 changes: 16 additions & 0 deletions cfb-core/src/event/pointer/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ export type IPointerEvent = {
type: "onPointerUp";
button: PointerButton;
};
/**
* List of pointer environments.
*
* It should be safe to always use `mouse`, but specifying this can be helpful for some
* plugins / event handlers.
*
* - `mouse` - Mouse pointer, often used in desktop environments.
* - `touch` - Touch pointer, often used in mobile environments.
*/
export type PointerEnv = "mouse" | "touch";
/**
* The pointer data. This will be used to pass data to the pointer processors.
*
Expand All @@ -60,6 +70,12 @@ export interface IPointerData<W extends IWorld> {
* The pointer event data.
*/
e: IPointerEvent;
/**
* The environment of the pointer.
*
* See {@link PointerEnv} for detailed explanation.
*/
env: PointerEnv;
/**
* The `world` instance.
*/
Expand Down
11 changes: 7 additions & 4 deletions cfb-web/src/event/pointer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IPointerEvent } from "@carefree0910/cfb-core";
import type { IPointerEvent, PointerEnv } from "@carefree0910/cfb-core";
import type { WebWorld } from "../world.ts";

import { PointerButton, PointerHandlerBase } from "@carefree0910/cfb-core";
Expand All @@ -7,20 +7,23 @@ export class WebPointerHandler extends PointerHandlerBase<WebWorld> {
get mobileEnv(): boolean {
return /(iphone|ipad|android|mobile)/gi.test(navigator.userAgent);
}
get pointerEnv(): PointerEnv {
return this.mobileEnv ? "touch" : "mouse";
}

setup(world: WebWorld): void {
const container = world.renderer.container;
container.addEventListener(
!this.mobileEnv ? `pointerdown` : "touchstart",
(e) => this.queue.push({ e: this.parse(e), world }),
(e) => this.queue.push({ e: this.parse(e), env: this.pointerEnv, world }),
);
document.addEventListener(
!this.mobileEnv ? `pointermove` : "touchmove",
(e) => this.queue.push({ e: this.parse(e), world }),
(e) => this.queue.push({ e: this.parse(e), env: this.pointerEnv, world }),
);
container.addEventListener(
!this.mobileEnv ? `pointerup` : "touchend",
(e) => this.queue.push({ e: this.parse(e), world }),
(e) => this.queue.push({ e: this.parse(e), env: this.pointerEnv, world }),
);
container.addEventListener("contextmenu", (e) => {
// right clicks should be handled by `onPointerDown`
Expand Down

0 comments on commit e10fb3b

Please sign in to comment.