renaming stuff

This commit is contained in:
Zutatensuppe 2020-11-08 23:42:36 +01:00
parent 883e2a8d7a
commit f67215c892
2 changed files with 28 additions and 23 deletions

View file

@ -56,17 +56,27 @@ export default class Camera {
return false return false
} }
translateMouse(mouse) { /**
* Translate a coordinate in the viewport to a
* coordinate in the world
* @param {x, y} coord
*/
viewportToWorld(coord) {
return { return {
x: (mouse.x / this.zoom) - this.x, x: (coord.x / this.zoom) - this.x,
y: (mouse.y / this.zoom) - this.y, y: (coord.y / this.zoom) - this.y,
} }
} }
translateMouseBack(mouse) { /**
* Translate a coordinate in the world to a
* coordinate in the viewport
* @param {x, y} coord
*/
worldToViewport(coord) {
return { return {
x: (mouse.x + this.x) * this.zoom, x: (coord.x + this.x) * this.zoom,
y: (mouse.y + this.y) * this.zoom, y: (coord.y + this.y) * this.zoom,
} }
} }
} }

View file

@ -519,7 +519,7 @@ async function main () {
// initialize some view data // initialize some view data
// this global data will change according to input events // this global data will change according to input events
const cam = new Camera(canvas) const viewport = new Camera(canvas)
conn.onSocket('message', ({data}) => { conn.onSocket('message', ({data}) => {
const d = JSON.parse(data) const d = JSON.parse(data)
@ -528,12 +528,12 @@ async function main () {
switch (change.type) { switch (change.type) {
case 'change_player': { case 'change_player': {
if (players[change.player.id]) { if (players[change.player.id]) {
rectPlayer.add(cam.translateMouseBack(players[change.player.id]), cursorGrab.width) rectPlayer.add(viewport.worldToViewport(players[change.player.id]), cursorGrab.width)
} }
players[change.player.id] = change.player players[change.player.id] = change.player
rectPlayer.add(cam.translateMouseBack(players[change.player.id]), cursorGrab.width) rectPlayer.add(viewport.worldToViewport(players[change.player.id]), cursorGrab.width)
} break; } break;
case 'change_tile': { case 'change_tile': {
@ -710,8 +710,8 @@ async function main () {
last_y = _last_mouse_down.y last_y = _last_mouse_down.y
} }
for (let mouse of evts.consumeAll()) { for (let mouse of evts.consumeAll()) {
const tp = viewport.viewportToWorld(mouse)
if (mouse.type === 'move') { if (mouse.type === 'move') {
const tp = cam.translateMouse(mouse)
changePlayer({ x: tp.x, y: tp.y }) changePlayer({ x: tp.x, y: tp.y })
if (_last_mouse) { if (_last_mouse) {
rectPlayer.add(_last_mouse, cursorGrab.width) rectPlayer.add(_last_mouse, cursorGrab.width)
@ -727,8 +727,7 @@ async function main () {
} }
if (grabbingTileIdx >= 0) { if (grabbingTileIdx >= 0) {
let tp = cam.translateMouse(mouse) let tp_last = viewport.viewportToWorld({ x: last_x, y: last_y })
let tp_last = cam.translateMouse({ x: last_x, y: last_y })
const diffX = tp.x - tp_last.x const diffX = tp.x - tp_last.x
const diffY = tp.y - tp_last.y const diffY = tp.y - tp_last.y
@ -742,7 +741,7 @@ async function main () {
// move the cam // move the cam
const diffX = Math.round(mouse.x - last_x) const diffX = Math.round(mouse.x - last_x)
const diffY = Math.round(mouse.y - last_y) const diffY = Math.round(mouse.y - last_y)
cam.move(diffX, diffY) viewport.move(diffX, diffY)
rerender = true rerender = true
} }
} }
@ -756,7 +755,6 @@ async function main () {
last_y = mouse.y last_y = mouse.y
} }
let tp = cam.translateMouse(mouse)
grabbingTileIdx = unfinishedTileByPos(puzzle, tp) grabbingTileIdx = unfinishedTileByPos(puzzle, tp)
console.log(grabbingTileIdx) console.log(grabbingTileIdx)
if (grabbingTileIdx >= 0) { if (grabbingTileIdx >= 0) {
@ -794,8 +792,6 @@ async function main () {
y: srcRect.y0 + boardPos.y, y: srcRect.y0 + boardPos.y,
}) })
finishGroupedTiles(tile) finishGroupedTiles(tile)
let tp = cam.translateMouse(mouse)
rectTable.add(tp, puzzle.info.tileDrawSize) rectTable.add(tp, puzzle.info.tileDrawSize)
} else { } else {
// Snap to other tiles // Snap to other tiles
@ -832,14 +828,13 @@ async function main () {
} }
} }
grabbingTileIdx = -1 grabbingTileIdx = -1
console.log('up', cam.translateMouse(mouse)) console.log('up', tp)
} else if (mouse.type === 'wheel') { } else if (mouse.type === 'wheel') {
if ( if (
mouse.deltaY < 0 && cam.zoomIn() mouse.deltaY < 0 && viewport.zoomIn()
|| mouse.deltaY > 0 && cam.zoomOut() || mouse.deltaY > 0 && viewport.zoomOut()
) { ) {
rerender = true rerender = true
const tp = cam.translateMouse(mouse)
changePlayer({ x: tp.x, y: tp.y }) changePlayer({ x: tp.x, y: tp.y })
if (_last_mouse) { if (_last_mouse) {
rectPlayer.add(_last_mouse, cursorGrab.width) rectPlayer.add(_last_mouse, cursorGrab.width)
@ -949,7 +944,7 @@ async function main () {
// atm it is pretty slow (~40-50ms) // atm it is pretty slow (~40-50ms)
mapBitmapToAdapter( mapBitmapToAdapter(
puzzleTable, puzzleTable,
cam.rect(), viewport.rect(),
adapter, adapter,
adapter.getBoundingRect() adapter.getBoundingRect()
) )
@ -959,7 +954,7 @@ async function main () {
checkpoint('afterclear_2') checkpoint('afterclear_2')
mapBitmapToAdapterCapped( mapBitmapToAdapterCapped(
puzzleTable, puzzleTable,
cam.rect(), viewport.rect(),
adapter, adapter,
adapter.getBoundingRect(), adapter.getBoundingRect(),
rectPlayer.get() rectPlayer.get()
@ -971,7 +966,7 @@ async function main () {
for (let id of Object.keys(players)) { for (let id of Object.keys(players)) {
let p = players[id] let p = players[id]
let cursor = p.down ? cursorGrab : cursorHand let cursor = p.down ? cursorGrab : cursorHand
let back = cam.translateMouseBack(p) let back = viewport.worldToViewport(p)
mapBitmapToAdapter( mapBitmapToAdapter(
cursor, cursor,
cursor.getBoundingRect(), cursor.getBoundingRect(),