pointvec/desktop/sources/scripts/cursor.js

86 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-10-03 19:27:40 -04:00
'use strict'
2018-08-28 00:34:17 -04:00
2019-11-03 13:36:58 -05:00
function Cursor (dotgrid) {
2018-10-03 19:27:40 -04:00
this.pos = { x: 0, y: 0 }
2019-11-03 20:44:31 -05:00
this.lastPos = { x: 0, y: 0 }
2018-10-03 19:27:40 -04:00
this.translation = null
this.operation = null
2018-08-02 18:57:35 -04:00
2018-12-16 18:01:53 -05:00
this.translate = function (from = null, to = null, multi = false, copy = false, layer = false) {
2019-04-21 19:36:12 -04:00
if ((from || to) && this.translation === null) { this.translation = { multi: multi, copy: copy, layer: layer } }
2018-10-03 19:27:40 -04:00
if (from) { this.translation.from = from }
if (to) { this.translation.to = to }
if (!from && !to) {
this.translation = null
2018-08-02 18:57:35 -04:00
}
}
2018-10-03 19:27:40 -04:00
this.down = function (e) {
2019-01-09 15:41:41 -05:00
this.pos = this.atEvent(e)
2019-04-21 19:49:47 -04:00
if (dotgrid.tool.vertexAt(this.pos)) {
2018-12-16 18:01:53 -05:00
this.translate(this.pos, this.pos, e.shiftKey, e.ctrlKey || e.metaKey, e.altKey)
2018-08-02 18:57:35 -04:00
}
2019-04-21 19:49:47 -04:00
dotgrid.renderer.update()
dotgrid.interface.update()
2018-10-03 19:27:40 -04:00
e.preventDefault()
2018-08-02 18:57:35 -04:00
}
2018-10-03 19:27:40 -04:00
this.move = function (e) {
2019-01-09 15:41:41 -05:00
this.pos = this.atEvent(e)
2018-10-03 19:27:40 -04:00
if (this.translation) {
this.translate(null, this.pos)
2018-08-02 18:57:35 -04:00
}
2019-11-03 20:44:31 -05:00
if (this.lastPos.x !== this.pos.x || this.lastPos.y !== this.pos.y) {
2019-04-21 19:49:47 -04:00
dotgrid.renderer.update()
2018-08-03 22:36:45 -04:00
}
2019-04-21 19:49:47 -04:00
dotgrid.interface.update()
2019-11-03 20:44:31 -05:00
this.lastPos = this.pos
2018-10-03 19:27:40 -04:00
e.preventDefault()
2018-08-02 18:57:35 -04:00
}
2018-10-03 19:27:40 -04:00
this.up = function (e) {
2019-01-09 15:41:41 -05:00
this.pos = this.atEvent(e)
2019-02-06 23:06:55 -05:00
if (this.translation && !isEqual(this.translation.from, this.translation.to)) {
2019-04-21 19:49:47 -04:00
if (this.translation.layer === true) { dotgrid.tool.translateLayer(this.translation.from, this.translation.to) } else if (this.translation.copy) { dotgrid.tool.translateCopy(this.translation.from, this.translation.to) } else if (this.translation.multi) { dotgrid.tool.translateMulti(this.translation.from, this.translation.to) } else { dotgrid.tool.translate(this.translation.from, this.translation.to) }
2019-04-21 19:36:12 -04:00
} else if (e.target.id === 'guide') {
2019-04-21 19:49:47 -04:00
dotgrid.tool.addVertex({ x: this.pos.x, y: this.pos.y })
dotgrid.picker.stop()
2018-08-02 18:57:35 -04:00
}
2018-10-03 19:27:40 -04:00
this.translate()
2019-04-21 19:49:47 -04:00
dotgrid.interface.update()
dotgrid.renderer.update()
2018-10-03 19:27:40 -04:00
e.preventDefault()
2018-08-02 18:57:35 -04:00
}
2018-10-03 19:27:40 -04:00
this.alt = function (e) {
2019-01-09 15:41:41 -05:00
this.pos = this.atEvent(e)
2019-04-21 19:49:47 -04:00
dotgrid.tool.removeSegmentsAt(this.pos)
2018-10-03 19:27:40 -04:00
e.preventDefault()
2019-11-03 20:44:31 -05:00
setTimeout(() => {
dotgrid.tool.clear()
}, 150)
2018-08-02 18:57:35 -04:00
}
2019-01-09 15:41:41 -05:00
this.atEvent = function (e) {
return this.snapPos(this.relativePos({ x: e.clientX, y: e.clientY }))
2018-08-02 18:57:35 -04:00
}
2019-01-09 15:41:41 -05:00
this.relativePos = function (pos) {
2018-08-02 18:57:35 -04:00
return {
2019-04-21 19:49:47 -04:00
x: pos.x - dotgrid.renderer.el.offsetLeft,
y: pos.y - dotgrid.renderer.el.offsetTop
2018-10-03 19:27:40 -04:00
}
2018-08-02 18:57:35 -04:00
}
2019-01-09 15:41:41 -05:00
this.snapPos = function (pos) {
2018-08-02 18:57:35 -04:00
return {
2019-04-21 21:25:31 -04:00
x: clamp(step(pos.x, 15), 15, dotgrid.tool.settings.size.width - 15),
y: clamp(step(pos.y, 15), 15, dotgrid.tool.settings.size.height - 15)
2018-10-03 19:27:40 -04:00
}
2018-08-02 18:57:35 -04:00
}
2018-08-04 15:56:13 -04:00
2019-04-21 19:36:12 -04:00
function isEqual (a, b) { return a.x === b.x && a.y === b.y }
2019-11-03 13:36:58 -05:00
function clamp (v, min, max) { return v < min ? min : v > max ? max : v }
function step (v, s) { return Math.round(v / s) * s }
2018-10-03 19:27:40 -04:00
}