From 00c0fb45dedc056a8922a809f0e5c39dafc0b507 Mon Sep 17 00:00:00 2001 From: tga Date: Tue, 10 Oct 2023 15:16:31 +0800 Subject: [PATCH] fix mousepos in fullscreen --- CHANGELOG.md | 5 +++++ src/app.ts | 22 ++++++++++++++++++++++ src/kaboom.ts | 46 +++++++++++++--------------------------------- src/math.ts | 2 +- src/types.ts | 12 +++++++++--- 5 files changed, 50 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cadb128c..dea05fa91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +### v3000.1.11 +- added option `kaboom({ focus: false })` to disable focus on start +- fixed `rand()` typing for numbers +- fixed mouse position in fullscreen + ### v3000.1.10 - fix test code accidentally getting shipped (where a screenshot will be downloaded every time you press space) diff --git a/src/app.ts b/src/app.ts index 2f2de5da9..1df70c850 100644 --- a/src/app.ts +++ b/src/app.ts @@ -13,6 +13,7 @@ import type { import { Vec2, + map, } from "./math" import { @@ -608,9 +609,30 @@ export default (opt: { const docEvents: EventList = {} const winEvents: EventList = {} + const pd = opt.pixelDensity || window.devicePixelRatio || 1 + canvasEvents.mousemove = (e) => { const mousePos = new Vec2(e.offsetX, e.offsetY) const mouseDeltaPos = new Vec2(e.movementX, e.movementY) + if (isFullscreen()) { + const cw = state.canvas.width / pd + const ch = state.canvas.height / pd + const ww = window.innerWidth + const wh = window.innerHeight + const rw = ww / wh + const rc = cw / ch + if (rw > rc) { + const ratio = wh / ch + const offset = (ww - (cw * ratio)) / 2 + mousePos.x = map(e.offsetX - offset, 0, cw * ratio, 0, cw) + mousePos.y = map(e.offsetY, 0, ch * ratio, 0, ch) + } else { + const ratio = ww / cw + const offset = (wh - (ch * ratio)) / 2 + mousePos.x = map(e.offsetX , 0, cw * ratio, 0, cw) + mousePos.y = map(e.offsetY - offset, 0, ch * ratio, 0, ch) + } + } state.events.onOnce("input", () => { state.isMouseMoved = true state.mousePos = mousePos diff --git a/src/kaboom.ts b/src/kaboom.ts index 45e8384b3..62a6f1b27 100644 --- a/src/kaboom.ts +++ b/src/kaboom.ts @@ -697,7 +697,6 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => { ) } - // gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true) gl.enable(gl.BLEND) gl.blendFuncSeparate( gl.SRC_ALPHA, @@ -861,10 +860,10 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => { const audio = (() => { - // TODO: handle when audio context is unavailable const ctx = new ( window.AudioContext || (window as any).webkitAudioContext )() as AudioContext + const masterNode = ctx.createGain() masterNode.connect(ctx.destination) @@ -3072,7 +3071,6 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => { let onCurCompCleanup = null let paused = false - // TODO // @ts-ignore const obj: GameObj = { @@ -3323,20 +3321,26 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => { ? this.isAncestorOf(obj) : obj.parent === this } + const events = [] // TODO: handle when object add / remove tags // TODO: clean up when obj destroyed - onAdd((obj) => { + events.push(onAdd((obj) => { if (isChild(obj) && obj.is(t)) { list.push(obj) } - }) - onDestroy((obj) => { + })) + events.push(onDestroy((obj) => { if (isChild(obj) && obj.is(t)) { const idx = list.findIndex((o) => o.id === obj.id) if (idx !== -1) { list.splice(idx, 1) } } + })) + this.onDestroy(() => { + for (const ev of events) { + ev.cancel() + } }) } return list @@ -4973,7 +4977,6 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => { } } - // TODO: all children should be fixed function fixed(): FixedComp { return { id: "fixed", @@ -6754,31 +6757,6 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => { const cw = gl.drawingBufferWidth / pd const ch = gl.drawingBufferHeight / pd - if (app.isFullscreen()) { - const ww = window.innerWidth - const wh = window.innerHeight - const rw = ww / wh - const rc = cw / ch - if (rw > rc) { - const sw = window.innerHeight * rc - gfx.viewport = { - x: (ww - sw) / 2, - y: 0, - width: sw, - height: wh, - } - } else { - const sh = window.innerWidth / rc - gfx.viewport = { - x: 0, - y: (wh - sh) / 2, - width: ww, - height: sh, - } - } - return - } - if (gopt.letterbox) { if (!gopt.width || !gopt.height) { @@ -7139,7 +7117,9 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => { } } - app.canvas().focus() + if (gopt.focus !== false) { + app.canvas().focus() + } return ctx diff --git a/src/math.ts b/src/math.ts index ca07886e2..606856924 100644 --- a/src/math.ts +++ b/src/math.ts @@ -627,7 +627,7 @@ export class RNG { this.genNumber(a.b, b.b), ) } - genAny(...args: T[]): T { + genAny(...args: T[]): T { if (args.length === 0) { return this.gen() as T } else if (args.length === 1) { diff --git a/src/types.ts b/src/types.ts index a4818ce45..43833112b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1659,7 +1659,7 @@ export interface KaboomCtx { * rand(rgb(255, 255, 255)) * ``` */ - rand(n: T): T, + rand(n: T): T, /** * Get a random value between the given bound. * @@ -1674,7 +1674,7 @@ export interface KaboomCtx { * ]) * ``` */ - rand(a: T, b: T): T, + rand(a: T, b: T): T, /** * rand() but floored to integer. * @@ -2537,6 +2537,12 @@ export interface KaboomOpt = any> { * @since v3000.0 */ maxFPS?: number, + /** + * If focus on the canvas on start (default true). + * + * @since v3000.2 + */ + focus?: boolean, /** * If import all kaboom functions to global (default true). */ @@ -3909,7 +3915,7 @@ export declare class RNG { genNumber(a: number, b: number): number genVec2(a: Vec2, b?: Vec2): Vec2 genColor(a: Color, b: Color): Color - genAny(...args: T[]): T + genAny(...args: T[]): T } export interface Comp {