pointvec/desktop/sources/scripts/cursor.js

100 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-01-08 22:51:57 -05:00
function Cursor () {
2018-10-03 19:27:40 -04:00
this.pos = { x: 0, y: 0 }
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-08-02 18:57:35 -04:00
2018-10-03 19:27:40 -04:00
if (from) { this.translation.from = from }
if (to) { this.translation.to = to }
2018-08-02 18:57:35 -04:00
2018-10-03 19:27:40 -04:00
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)
2018-08-02 18:57:35 -04:00
// Translation
2019-01-08 22:49:34 -05: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-01-08 22:12:18 -05:00
DOTGRID.renderer.update()
2018-10-04 19:52:17 -04:00
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.last_pos = { x: 0, y: 0 }
2018-08-03 22:36:45 -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-08-02 18:57:35 -04:00
// Translation
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-04-21 19:36:12 -04:00
if (this.last_pos.x !== this.pos.x || this.last_pos.y !== this.pos.y) {
2019-01-08 22:12:18 -05:00
DOTGRID.renderer.update()
2018-08-03 22:36:45 -04:00
}
2018-10-04 19:52:17 -04:00
DOTGRID.interface.update()
2018-10-03 19:27:40 -04:00
e.preventDefault()
2018-08-03 22:36:45 -04:00
2018-10-03 19:27:40 -04:00
this.last_pos = this.pos
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)
2018-08-02 18:57:35 -04:00
2019-02-06 23:06:55 -05:00
if (this.translation && !isEqual(this.translation.from, this.translation.to)) {
2019-01-08 22:49:34 -05: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-01-08 22:49:34 -05:00
DOTGRID.tool.addVertex({ x: this.pos.x, y: this.pos.y })
2018-10-04 19:52:17 -04:00
DOTGRID.picker.stop()
2018-08-02 18:57:35 -04:00
}
2018-08-04 15:56:13 -04:00
2018-10-03 19:27:40 -04:00
this.translate()
2018-08-02 18:57:35 -04:00
2018-10-04 19:52:17 -04:00
DOTGRID.interface.update()
2019-01-08 22:12:18 -05:00
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)
2018-08-02 18:57:35 -04:00
2019-01-08 22:49:34 -05:00
DOTGRID.tool.removeSegmentsAt(this.pos)
2018-10-03 19:27:40 -04:00
e.preventDefault()
2018-08-02 18:57:35 -04:00
2018-10-04 19:52:17 -04:00
setTimeout(() => { DOTGRID.tool.clear() }, 150)
2018-08-02 18:57:35 -04:00
}
// Position Mods
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-01-08 22:12:18 -05: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-01-09 15:35:10 -05:00
x: clamp(step(pos.x, 15), 15, DOTGRID.tool.settings.size.width),
2019-01-09 15:45:50 -05:00
y: clamp(step(pos.y, 15), 15, DOTGRID.tool.settings.size.height)
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 }
2018-10-03 19:27:40 -04:00
}