Skip to content

Commit

Permalink
✨Implemented DragProcessor
Browse files Browse the repository at this point in the history
now we can drag things around, great!
  • Loading branch information
carefree0910 committed Nov 3, 2024
1 parent 2f1645d commit 456e5e8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ This will start the demo at `http://localhost:1245`, and you can play with the `
Currently this demo only supports:

1. Display two squares on the screen.
2. Respond to mouse clicks - it will log out the nodes being clicked in the console.
2. Respond to pointer events - you can drag the squares around!

> You can also visit [here](https://cfb-web.deno.dev/), which is hosted by `deno deploy`,
> to play around with the demo.
Expand Down
1 change: 1 addition & 0 deletions cfb-core/src/event/impl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./pointer/base.ts";
export * from "./pointer/drag.ts";

import type { IEventHandler, IEventSystem } from "./types.ts";
import type { IWorld } from "../world.ts";
Expand Down
48 changes: 48 additions & 0 deletions cfb-core/src/event/pointer/drag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type { IPointerData, PointerEventTypes } from "./base.ts";
import type { Point } from "../../toolkit.ts";
import type { ISingleNodeR } from "../../nodes.ts";
import type { IWorld } from "../../world.ts";

import { PointerProcessorBase, registerPointerProcessor } from "./base.ts";
import { DirtyStatus } from "../../board.ts";

class DragProcessor extends PointerProcessorBase<PointerEventTypes, IWorld> {
private pointer: Point | null = null;
private pointed: ISingleNodeR | null = null;

exec(data: IPointerData<PointerEventTypes, IWorld>): Promise<void> {
if (data.type === "onPointerDown") {
const allPointed = this.getPointed(data);
if (allPointed.length === 0) {
this.reset();
} else {
this.pointer = this.getPointer(data.e);
this.pointed = allPointed.sort((a, b) => a.node.z - b.node.z)[0].node;
}
} else if (data.type === "onPointerMove") {
if (this.pointer && this.pointed) {
const newPointer = this.getPointer(data.e);
this.pointed.position = this.pointed.lt.add(newPointer.subtract(this.pointer));
this.pointer = newPointer;
data.world.renderer.board.get(this.pointed.alias).setDirtyStatus(
DirtyStatus.TRANSFORM_DIRTY,
);
}
} else if (data.type === "onPointerUp") {
this.reset();
}
return Promise.resolve();
}

private reset(): void {
this.pointed = null;
this.pointer = null;
}
}

const dragProcessor: DragProcessor = new DragProcessor();
export function registerDragPRocessor(): void {
registerPointerProcessor("onPointerDown", dragProcessor);
registerPointerProcessor("onPointerMove", dragProcessor);
registerPointerProcessor("onPointerUp", dragProcessor);
}
18 changes: 2 additions & 16 deletions cfb-web/src/event/pointer.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
import type { IPointerData, IPointerProcessor } from "@carefree0910/cfb-core";
import type { WebWorld } from "../world.ts";
import { registerDragPRocessor } from "@carefree0910/cfb-core";

import { Point, registerPointerProcessor } from "@carefree0910/cfb-core";

export class PointerDownProcessor
implements IPointerProcessor<"onPointerDown", WebWorld> {
exec({ e, world }: IPointerData<"onPointerDown", WebWorld>): Promise<void> {
const graph = world.renderer.board.graph;
const point = new Point(e.clientX, e.clientY);
const pointed = graph.allSingleNodes.filter((gnode) => point.in(gnode.node.bbox));
console.log(pointed);
return Promise.resolve();
}
}

registerPointerProcessor("onPointerDown", new PointerDownProcessor());
registerDragPRocessor();

0 comments on commit 456e5e8

Please sign in to comment.