Skip to content

Commit 3b35903

Browse files
committed
Now using mostly pointerevents; context menu on long touch
1 parent c830398 commit 3b35903

20 files changed

+467
-456
lines changed

simulator/src/ComponentFactory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,9 +413,9 @@ export class ComponentFactory {
413413

414414
const customComp = maker.make(editor)
415415
customComp.setSpawned()
416-
customComp.setPosition(editor.mouseX + customComp.unrotatedWidth / 2 - 5, editor.mouseY, true)
416+
customComp.setPosition(editor.pointerX + customComp.unrotatedWidth / 2 - 5, editor.pointerY, true)
417417
editor.eventMgr.currentSelection = undefined
418-
editor.eventMgr.setCurrentMouseOverComp(customComp)
418+
editor.eventMgr.setCurrentPointerOverComp(customComp)
419419

420420
for (const comp of componentsToInclude) {
421421
editor.components.tryDelete(comp)

simulator/src/LogicEditor.ts

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { Tests } from "./Tests"
3030
import { TestsPalette } from "./TestsPalette"
3131
import { Timeline } from "./Timeline"
3232
import { TopBar } from "./TopBar"
33-
import { EditorSelection, MouseDragEvent, TouchDragEvent, UIEventManager } from "./UIEventManager"
33+
import { EditorSelection, PointerDragEvent, UIEventManager } from "./UIEventManager"
3434
import { UndoManager } from './UndoManager'
3535
import { Component, ComponentBase } from "./components/Component"
3636
import { CustomComponent } from "./components/CustomComponent"
@@ -115,7 +115,7 @@ const DEFAULT_EDITOR_OPTIONS = {
115115

116116
export type EditorOptions = typeof DEFAULT_EDITOR_OPTIONS
117117

118-
export const MouseActions = {
118+
export const PointerActions = {
119119
edit: {
120120
cursor: null,
121121
paramTypes: [],
@@ -137,9 +137,9 @@ export const MouseActions = {
137137
paramTypes: any[]
138138
}>
139139

140-
export type MouseActionParams<M extends keyof typeof MouseActions> = typeof MouseActions[M]["paramTypes"]
140+
export type PoionterActionParams<M extends keyof typeof PointerActions> = typeof PointerActions[M]["paramTypes"]
141141

142-
export type MouseAction = keyof typeof MouseActions
142+
export type PointerAction = keyof typeof PointerActions
143143

144144
type InitialData = { _type: "url", url: string } | { _type: "json", json: string } | { _type: "compressed", str: string }
145145

@@ -284,8 +284,8 @@ export class LogicEditor extends HTMLElement implements DrawableParent {
284284

285285
private _baseDrawingScale = 1
286286
private _actualZoomFactor = 1
287-
public mouseX = -1000 // offscreen at start
288-
public mouseY = -1000
287+
public pointerX = -1000 // offscreen at start
288+
public pointerY = -1000
289289

290290
public constructor() {
291291
super()
@@ -739,7 +739,7 @@ export class LogicEditor extends HTMLElement implements DrawableParent {
739739
console.log("ERROR: Could not find group button")
740740
}
741741
} else {
742-
groupButton.addEventListener("mousedown", this.wrapHandler(e => {
742+
groupButton.addEventListener("pointerdown", this.wrapHandler(e => {
743743
const success = this.makeGroupWithSelection()
744744
if (success) {
745745
e.preventDefault()
@@ -834,7 +834,7 @@ export class LogicEditor extends HTMLElement implements DrawableParent {
834834
textArea.addEventListener("focus", selectAllListener)
835835
}
836836

837-
this.setCurrentMouseAction("edit", true)
837+
this.setCurrentPointerAction("edit", true)
838838
this.timeline.reset()
839839

840840
// Options
@@ -987,14 +987,14 @@ export class LogicEditor extends HTMLElement implements DrawableParent {
987987
window.gallery = gallery
988988

989989
window.addEventListener("pointermove", e => {
990-
// console.log(`pointermove with x=${e.clientX}, y=${e.clientY}`)
990+
// console.log(`pointermove (type '${e.pointerType}') with x=${e.clientX}, y=${e.clientY}`)
991991
for (const editor of LogicEditor._allConnectedEditors) {
992992
const canvasContainer = editor.html.canvasContainer
993993
if (canvasContainer !== undefined) {
994994
const canvasPos = canvasContainer.getBoundingClientRect()
995995
// console.log(canvasContainer.getBoundingClientRect(), { x: e.clientX - canvasPos.left, y: e.clientY - canvasPos.top })
996-
editor.mouseX = e.clientX - canvasPos.left
997-
editor.mouseY = e.clientY - canvasPos.top
996+
editor.pointerX = e.clientX - canvasPos.left
997+
editor.pointerY = e.clientY - canvasPos.top
998998
}
999999
}
10001000
// console.log("--")
@@ -1088,7 +1088,7 @@ export class LogicEditor extends HTMLElement implements DrawableParent {
10881088
})
10891089

10901090
if (mode < Mode.CONNECT) {
1091-
this.setCurrentMouseAction("edit")
1091+
this.setCurrentPointerAction("edit")
10921092
}
10931093

10941094
const showComponentsAndEditControls: UIDisplay =
@@ -1180,9 +1180,9 @@ export class LogicEditor extends HTMLElement implements DrawableParent {
11801180
sessionStorage.setItem(key, saveStr)
11811181
if (this._autosave) {
11821182
localStorage.setItem(key, saveStr)
1183-
console.log(`Saved circuit to session and local storage with key '${key}'`)
1183+
// console.log(`Saved circuit to session and local storage with key '${key}'`)
11841184
} else {
1185-
console.log(`Saved circuit to session (not local) storage with key '${key}'`)
1185+
// console.log(`Saved circuit to session (not local) storage with key '${key}'`)
11861186
}
11871187
} catch (e) {
11881188
console.error("Failed to save circuit to browser storage", e)
@@ -1524,31 +1524,31 @@ export class LogicEditor extends HTMLElement implements DrawableParent {
15241524
this._highlightedItems = undefined
15251525
this.eventMgr.currentSelection = undefined
15261526
this.eventMgr.clearPopperIfNecessary()
1527-
this.eventMgr.updateMouseOver([this.mouseX, this.mouseY], false, false)
1527+
this.eventMgr.updatePointerOver([this.pointerX, this.pointerY], false, false)
15281528
this.editTools.testsPalette.update()
15291529
this.editTools.moveMgr.clear()
15301530
this.editTools.redrawMgr.requestRedraw({ why: "editor root changed", invalidateMask: true, invalidateTests: true })
15311531

15321532
this.focus()
15331533
}
15341534

1535-
public setCurrentMouseAction<M extends MouseAction>(action: M, forceUpdate: boolean = false, ...params: MouseActionParams<M>): boolean {
1535+
public setCurrentPointerAction<M extends PointerAction>(action: M, forceUpdate: boolean = false, ...params: PoionterActionParams<M>): boolean {
15361536
const changed = this.eventMgr.setHandlersFor(action, ...params)
15371537
if (forceUpdate || changed) {
1538-
this.setToolCursor(MouseActions[action].cursor)
1538+
this.setToolCursor(PointerActions[action].cursor)
15391539
this._topBar?.updateActiveTool(action)
15401540
this.editTools.redrawMgr.requestRedraw({ why: "mouse action changed" })
15411541
this.editor.focus()
15421542
}
15431543
return changed
15441544
}
15451545

1546-
public updateCursor(e?: MouseEvent | TouchEvent) {
1546+
public updateCursor(e?: PointerEvent) {
15471547
const cursor =
15481548
this.editTools.moveMgr.areDrawablesMoving()
15491549
? "grabbing"
15501550
: this._toolCursor
1551-
?? this.eventMgr.currentMouseOverComp?.cursorWhenMouseover(e)
1551+
?? this.eventMgr.currentPointerOverComp?.cursorWhenMouseover(e)
15521552
?? "default"
15531553
this.html.canvasContainer.style.cursor = cursor
15541554
}
@@ -1557,7 +1557,7 @@ export class LogicEditor extends HTMLElement implements DrawableParent {
15571557
return this._messageBar?.showMessage(msg, duration, withCloseButton) ?? (() => undefined)
15581558
}
15591559

1560-
public offsetXYForContextMenu(e: MouseEvent | TouchEvent | MouseDragEvent | TouchDragEvent, snapToGrid = false): [number, number] {
1560+
public offsetXYForContextMenu(e: MouseEvent | PointerDragEvent, snapToGrid = false): [number, number] {
15611561
const mainCanvas = this.html.mainCanvas
15621562
let x, y
15631563

@@ -1578,7 +1578,7 @@ export class LogicEditor extends HTMLElement implements DrawableParent {
15781578
return [x, y]
15791579
}
15801580

1581-
public offsetXY(e: MouseEvent | TouchEvent, skipScaling: boolean = false): [number, number] {
1581+
public offsetXY(e: MouseEvent, skipScaling: boolean = false): [number, number] {
15821582
const [unscaledX, unscaledY] = (() => {
15831583
const mainCanvas = this.html.mainCanvas
15841584
let target = e.target
@@ -1612,28 +1612,30 @@ export class LogicEditor extends HTMLElement implements DrawableParent {
16121612
]
16131613
}
16141614
} else {
1615-
const elemRect = (target as HTMLElement).getBoundingClientRect()
1616-
const bodyRect = document.body.getBoundingClientRect()
1617-
const touch = e.changedTouches[0]
1618-
const offsetX = touch.pageX - (elemRect.left - bodyRect.left)
1619-
const offsetY = touch.pageY - (elemRect.top - bodyRect.top)
1620-
1621-
if (target === mainCanvas) {
1622-
return [offsetX, offsetY]
1623-
} else {
1624-
const canvasRect = mainCanvas.getBoundingClientRect()
1625-
return [
1626-
Math.max(GRID_STEP * 2, offsetX + elemRect.x - canvasRect.x),
1627-
Math.max(GRID_STEP * 2, offsetY + elemRect.y - canvasRect.y),
1628-
]
1629-
}
1615+
console.error("calling offsetXY with TouchEvent")
1616+
throw new Error("not implemented")
1617+
// const elemRect = (target as HTMLElement).getBoundingClientRect()
1618+
// const bodyRect = document.body.getBoundingClientRect()
1619+
// const touch = e.changedTouches[0]
1620+
// const offsetX = touch.pageX - (elemRect.left - bodyRect.left)
1621+
// const offsetY = touch.pageY - (elemRect.top - bodyRect.top)
1622+
1623+
// if (target === mainCanvas) {
1624+
// return [offsetX, offsetY]
1625+
// } else {
1626+
// const canvasRect = mainCanvas.getBoundingClientRect()
1627+
// return [
1628+
// Math.max(GRID_STEP * 2, offsetX + elemRect.x - canvasRect.x),
1629+
// Math.max(GRID_STEP * 2, offsetY + elemRect.y - canvasRect.y),
1630+
// ]
1631+
// }
16301632
}
16311633
})()
16321634
const currentScale = skipScaling ? 1 : this._actualZoomFactor
16331635
return [unscaledX / currentScale, unscaledY / currentScale]
16341636
}
16351637

1636-
public offsetXYForComponent(e: MouseEvent | TouchEvent, comp: Component): [number, number] {
1638+
public offsetXYForComponent(e: PointerEvent, comp: Component): [number, number] {
16371639
const offset = this.offsetXY(e)
16381640
if (comp.orient === Orientation.default) {
16391641
return offset
@@ -2390,7 +2392,7 @@ export class LogicEditor extends HTMLElement implements DrawableParent {
23902392
const drawTime = this.timeline.logicalTime()
23912393
const drawTimeAnimationFraction = !this._options.animateWires ? undefined : (drawTime / 1000) % 1
23922394
g.strokeStyle = COLOR_COMPONENT_BORDER
2393-
const currentMouseOverComp = this.eventMgr.currentMouseOverComp
2395+
const currentMouseOverComp = this.eventMgr.currentPointerOverComp
23942396
const drawParams: DrawParams = {
23952397
drawTime,
23962398
drawTimeAnimationFraction,
@@ -2529,7 +2531,7 @@ export class LogicEditor extends HTMLElement implements DrawableParent {
25292531
// ... but then, beware of duplicated custom components if pasting into the same circuit,
25302532
// or find some compatibility criterion for component defs (e.g., number of in/out nodes
25312533
// and names) that would seem enough to determine they are the same (beyond their id/name)
2532-
const reprs = Serialization.buildComponentsAndWireObject(componentsToInclude, [], [this.mouseX, this.mouseY])
2534+
const reprs = Serialization.buildComponentsAndWireObject(componentsToInclude, [], [this.pointerX, this.pointerY])
25332535
if (reprs.components === undefined && reprs.wires === undefined) {
25342536
return false
25352537
}

simulator/src/MoveManager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ export class MoveManager {
3737
return undefined
3838
}
3939

40-
public setDrawableMoving(comp: DrawableWithPosition, e?: MouseEvent | TouchEvent) {
40+
public setDrawableMoving(comp: DrawableWithPosition, e?: PointerEvent) {
4141
this.changeMovingDrawables(() => {
4242
this._movingDrawables.add(comp)
4343
}, e)
4444
}
4545

46-
public setDrawableStoppedMoving(comp: DrawableWithPosition, e?: MouseEvent | TouchEvent) {
46+
public setDrawableStoppedMoving(comp: DrawableWithPosition, e?: PointerEvent) {
4747
this.changeMovingDrawables(() => {
4848
this._movingDrawables.delete(comp)
4949
}, e)
5050
}
5151

52-
private changeMovingDrawables(change: () => void, e?: MouseEvent | TouchEvent) {
52+
private changeMovingDrawables(change: () => void, e?: PointerEvent) {
5353
const emptyBefore = this._movingDrawables.size === 0
5454
change()
5555
const emptyAfter = this._movingDrawables.size === 0

simulator/src/Serialization.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class _Serialization {
118118
if (isArray(parsed.pos)) {
119119
const editor = parent.editor
120120
const ceil10 = (x: number) => Math.ceil(x / 10) * 10
121-
offsetPos = [ceil10(editor.mouseX - parsed.pos[0]), ceil10(editor.mouseY - parsed.pos[1])]
121+
offsetPos = [ceil10(editor.pointerX - parsed.pos[0]), ceil10(editor.pointerY - parsed.pos[1])]
122122
// console.log(`offsetPos = ${offsetPos}`)
123123
}
124124

simulator/src/TopBar.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LogicEditor, MouseAction } from "./LogicEditor"
1+
import { LogicEditor, PointerAction } from "./LogicEditor"
22
import { Serialization } from "./Serialization"
33
import { TimelineState } from "./Timeline"
44
import { UndoState } from "./UndoManager"
@@ -107,17 +107,17 @@ export class TopBar {
107107
this.timeLabel.style.fontSize = "8pt"
108108

109109
this.designButton = this.makeButtonWithLabel("mouse", s.Design,
110-
() => this.editor.setCurrentMouseAction("edit"))
110+
() => this.editor.setCurrentPointerAction("edit"))
111111

112112
this.deleteButton = this.makeButtonWithLabel("trash", s.Delete,
113-
() => this.editor.setCurrentMouseAction("delete"))
113+
() => this.editor.setCurrentPointerAction("delete"))
114114

115115
this.testsButton = this.makeButtonWithLabel("check", s.Tests,
116116
() => this.editor.setTestsPaletteVisible(!this.testsButton.classList.contains("active")))
117117

118118
this.flexibleSep = div(style("flex: auto")).render()
119119
this.moveButton = this.makeButton("move", s.Move[1],
120-
() => this.editor.setCurrentMouseAction("move"))
120+
() => this.editor.setCurrentPointerAction("move"))
121121
this.zoomLevelInput = input(type("number"),
122122
style("margin: 0 2px 0 0; width: 4em; background-color: inherit;"),
123123
attr("min", "0"), attr("step", "10"),
@@ -393,7 +393,7 @@ export class TopBar {
393393
(ms < 100 ? (ms < 10 ? "00" : "0") : "") + ms
394394
}
395395

396-
public updateActiveTool(tool: MouseAction) {
396+
public updateActiveTool(tool: PointerAction) {
397397
setActive(this.designButton, tool === "edit")
398398
setActive(this.deleteButton, tool === "delete")
399399
setActive(this.moveButton, tool === "move")

0 commit comments

Comments
 (0)