Skip to content

Commit

Permalink
fix drag with ctrl after mousedown
Browse files Browse the repository at this point in the history
  • Loading branch information
k2d222 committed Oct 27, 2024
1 parent 3865838 commit eae0be4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
20 changes: 15 additions & 5 deletions client/src/gl/viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class Viewport {

clickTimeout: number // millis between press and release to be considered click

drag: boolean
dragTimestamp: number
touchDistance: number

Expand All @@ -39,6 +40,7 @@ export class Viewport {

this.clickTimeout = 100

this.drag = false
this.dragTimestamp = 0
this.touchDistance = 0

Expand Down Expand Up @@ -105,7 +107,9 @@ export class Viewport {
private onmousedown(e: MouseEvent) {
const [canvasX, canvasY] = this.pixelToCanvas(e.clientX, e.clientY)

this.onDragStart(canvasX, canvasY)
// wheel button or ctrl + left click
if (e.buttons === 4 || (e.ctrlKey && e.buttons === 1))
this.onDragStart(canvasX, canvasY)
}

private onmousemove(e: MouseEvent) {
Expand All @@ -114,12 +118,17 @@ export class Viewport {
this.mousePos.x = worldX
this.mousePos.y = worldY

if (e.buttons === 4 || (e.ctrlKey && e.buttons === 1))
// wheel button or ctrl + left click
this.onDrag(canvasX, canvasY)
if (this.drag) {
if (e.buttons === 4 || (e.ctrlKey && e.buttons === 1))
this.onDrag(canvasX, canvasY)
else
this.drag = false
}
}

private onmouseup(_e: MouseEvent) {}
private onmouseup(_e: MouseEvent) {
this.drag = false
}

private onwheel(e: WheelEvent) {
const direction = e.deltaY < 0 ? 1 : -1
Expand Down Expand Up @@ -188,6 +197,7 @@ export class Viewport {
this.posDragLast.x = this.posDragStart.x
this.posDragLast.y = this.posDragStart.y
this.dragTimestamp = Date.now()
this.drag = true
}

private onDrag(x: number, y: number) {
Expand Down
15 changes: 10 additions & 5 deletions client/src/twmap/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,30 @@ export class Image {
height: number
data: ImageSource | null
external: boolean
private loadingImg: HTMLImageElement | null

constructor() {
this.name = ''
this.width = 0
this.height = 0
this.data = null
this.external = false
this.loadingImg = null
}

loadExternal(url: string) {
this.data = document.createElement('img')
this.data.onerror = () => console.warn('failed to load image:', url)
this.data.onload = () => {
if (this.data) {
const img = document.createElement('img')
this.loadingImg = img
img.onerror = () => console.warn('failed to load image:', url)
img.onload = () => {
if (this.loadingImg === img) {
this.loadingImg = null
this.data = img
this.width = this.data.width
this.height = this.data.height
}
}
this.data.src = url
img.src = url
this.external = true
}

Expand Down
2 changes: 1 addition & 1 deletion client/src/ui/lib/mapEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
updateMouseRange()
if (e.buttons === 1 && !e.ctrlKey) {
// left clickft
// left click
if (brushState === BrushState.Empty) {
// start a selection
mouseRange.start = mouseRange.end
Expand Down

0 comments on commit eae0be4

Please sign in to comment.