Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions raylib.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class RaylibJs {
this.currentPressedKeyState = new Set();
this.currentMouseWheelMoveState = 0;
this.currentMousePosition = {x: 0, y: 0};
this.currentGestureState = 0;
this.quit = false;
}

Expand All @@ -58,24 +59,32 @@ class RaylibJs {
this.wasm = await WebAssembly.instantiateStreaming(fetch(wasmPath), {
env: make_environment(this)
});

const keyDown = (e) => {
this.currentPressedKeyState.add(glfwKeyMapping[e.code]);
};
const keyUp = (e) => {
this.currentPressedKeyState.delete(glfwKeyMapping[e.code]);
};
const wheelMove = (e) => {
this.currentMouseWheelMoveState = Math.sign(-e.deltaY);
this.currentMouseWheelMoveState = Math.sign(-e.deltaY);
};
const mouseMove = (e) => {
this.currentMousePosition = {x: e.clientX, y: e.clientY};
};
const gestureTap = (e) => {
this.currentGestureState = gestureMapping.GESTURE_TAP;
Copy link

@RobLoach RobLoach Feb 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will register a GESTURE_TAP if the middle-mouse button is pressed, and never actually release it. I'm not sure this is the intended behavior.

};
window.addEventListener("keydown", keyDown);
window.addEventListener("keyup", keyUp);
window.addEventListener("wheel", wheelMove);
window.addEventListener("mousemove", mouseMove);

canvas.addEventListener("mousedown", gestureTap);
canvas.oncontextmenu = function(e) {
e.preventDefault();
};

this.wasm.instance.exports.main();
const next = (timestamp) => {
if (this.quit) {
Expand Down Expand Up @@ -131,6 +140,7 @@ class RaylibJs {
this.prevPressedKeyState.clear();
this.prevPressedKeyState = new Set(this.currentPressedKeyState);
this.currentMouseWheelMoveState = 0.0;
this.currentGestureState = 0;
}

DrawCircleV(center_ptr, radius, color_ptr) {
Expand Down Expand Up @@ -178,8 +188,8 @@ class RaylibJs {
GetMouseWheelMove() {
return this.currentMouseWheelMoveState;
}
IsGestureDetected() {
return false;
IsGestureDetected(gesture) {
return this.currentGestureState === gesture;
}

TextFormat(... args){
Expand Down Expand Up @@ -364,6 +374,20 @@ const glfwKeyMapping = {
// GLFW_KEY_LAST GLFW_KEY_MENU
}

const gestureMapping = {
GESTURE_NONE: 0,
GESTURE_TAP: 1,
GESTURE_DOUBLETAP: 2,
GESTURE_HOLD: 4,
GESTURE_DRAG: 8,
GESTURE_SWIPE_RIGHT: 16,
GESTURE_SWIPE_LEFT: 32,
GESTURE_SWIPE_UP: 64,
GESTURE_SWIPE_DOWN: 128,
GESTURE_PINCH_IN: 256,
GESTURE_PINCH_OUT: 512
}

function cstrlen(mem, ptr) {
let len = 0;
while (mem[ptr] != 0) {
Expand Down