Skip to content
Open
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export const initAppState = (opt: {
capsOn: false,
mousePos: new Vec2(0),
mouseDeltaPos: new Vec2(0),
scrollDelta: new Vec2(0),
keyState: new ButtonState<Key>(
"keyPress",
"keyPressRepeat",
Expand Down Expand Up @@ -465,6 +466,10 @@ export const initApp = (
return state.mouseDeltaPos.clone();
}

function scrollDelta(): Vec2 {
return state.scrollDelta.clone();
}

function isMousePressed(m: MouseButton = "left"): boolean {
return state.mouseState.pressed.has(m);
}
Expand Down Expand Up @@ -818,6 +823,7 @@ export const initApp = (
state.charInputted = [];
state.isMouseMoved = false;
state.mouseDeltaPos = new Vec2(0);
state.scrollDelta = new Vec2(0);

state.gamepadStates.forEach((s) => {
s.buttonState.update();
Expand Down Expand Up @@ -1233,8 +1239,10 @@ export const initApp = (
// TODO: option to not prevent default?
canvasEvents.wheel = (e) => {
e.preventDefault();
state.scrollDelta.set(e.deltaX, e.deltaY);

state.events.onOnce("input", () => {
state.events.trigger("scroll", new Vec2(e.deltaX, e.deltaY));
state.events.trigger("scroll", state.scrollDelta);
});
};

Expand Down Expand Up @@ -1333,6 +1341,7 @@ export const initApp = (
isTouchscreen,
mousePos,
mouseDeltaPos,
scrollDelta,
isKeyDown,
isKeyPressed,
isKeyPressedRepeat,
Expand Down
1 change: 1 addition & 0 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ export const createContext = (
onButtonRelease: defaultScope.onButtonRelease,
mousePos: app.mousePos,
mouseDeltaPos: app.mouseDeltaPos,
scrollDelta: app.scrollDelta,
isKeyDown: app.isKeyDown,
isKeyPressed: app.isKeyPressed,
isKeyPressedRepeat: app.isKeyPressedRepeat,
Expand Down
9 changes: 9 additions & 0 deletions src/core/contextType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4651,6 +4651,15 @@ export interface KAPLAYCtx {
* @subgroup Mouse
*/
mouseDeltaPos(): Vec2;
/**
* How much the mouse wheel scrolled since last frame.
*
* @returns The delta scroll.
* @since v4000.0
* @group Input
* @subgroup Mouse
*/
scrollDelta(): Vec2;
/**
* If any or certain key(s) are currently down.
*
Expand Down
11 changes: 6 additions & 5 deletions tests/playtests/largeTexture.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ onUpdate(() => {
);
setCamPos(cameraPosition);
}

const scroll = scrollDelta();
if (scroll.y !== 0) {
cameraScale *= 1 - 0.1 * Math.sign(scroll.y);
setCamScale(cameraScale);
}
});

let cameraScale = 1;

onScroll((delta) => {
cameraScale = cameraScale * (1 - 0.1 * Math.sign(delta.y));
setCamScale(cameraScale);
});