pointvec/desktop/sources/scripts/cursor.js

102 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-10-03 23:27:40 +00:00
'use strict'
2018-08-28 04:34:17 +00:00
2019-11-03 18:36:58 +00:00
function Cursor (dotgrid) {
2018-10-03 23:27:40 +00:00
this.pos = { x: 0, y: 0 }
this.translation = null
this.operation = null
2018-08-02 22:57:35 +00:00
2018-12-16 23:01:53 +00:00
this.translate = function (from = null, to = null, multi = false, copy = false, layer = false) {
2019-04-21 23:36:12 +00:00
if ((from || to) && this.translation === null) { this.translation = { multi: multi, copy: copy, layer: layer } }
2018-08-02 22:57:35 +00:00
2018-10-03 23:27:40 +00:00
if (from) { this.translation.from = from }
if (to) { this.translation.to = to }
2018-08-02 22:57:35 +00:00
2018-10-03 23:27:40 +00:00
if (!from && !to) {
this.translation = null
2018-08-02 22:57:35 +00:00
}
}
2018-10-03 23:27:40 +00:00
this.down = function (e) {
2019-01-09 20:41:41 +00:00
this.pos = this.atEvent(e)
2018-08-02 22:57:35 +00:00
// Translation
2019-04-21 23:49:47 +00:00
if (dotgrid.tool.vertexAt(this.pos)) {
2018-12-16 23:01:53 +00:00
this.translate(this.pos, this.pos, e.shiftKey, e.ctrlKey || e.metaKey, e.altKey)
2018-08-02 22:57:35 +00:00
}
2019-04-21 23:49:47 +00:00
dotgrid.renderer.update()
dotgrid.interface.update()
2018-10-03 23:27:40 +00:00
e.preventDefault()
2018-08-02 22:57:35 +00:00
}
2018-10-03 23:27:40 +00:00
this.last_pos = { x: 0, y: 0 }
2018-08-04 02:36:45 +00:00
2018-10-03 23:27:40 +00:00
this.move = function (e) {
2019-01-09 20:41:41 +00:00
this.pos = this.atEvent(e)
2018-08-02 22:57:35 +00:00
// Translation
2018-10-03 23:27:40 +00:00
if (this.translation) {
this.translate(null, this.pos)
2018-08-02 22:57:35 +00:00
}
2019-04-21 23:36:12 +00:00
if (this.last_pos.x !== this.pos.x || this.last_pos.y !== this.pos.y) {
2019-04-21 23:49:47 +00:00
dotgrid.renderer.update()
2018-08-04 02:36:45 +00:00
}
2019-04-21 23:49:47 +00:00
dotgrid.interface.update()
2018-10-03 23:27:40 +00:00
e.preventDefault()
2018-08-04 02:36:45 +00:00
2018-10-03 23:27:40 +00:00
this.last_pos = this.pos
2018-08-02 22:57:35 +00:00
}
2018-10-03 23:27:40 +00:00
this.up = function (e) {
2019-01-09 20:41:41 +00:00
this.pos = this.atEvent(e)
2018-08-02 22:57:35 +00:00
2019-02-07 04:06:55 +00:00
if (this.translation && !isEqual(this.translation.from, this.translation.to)) {
2019-04-21 23:49:47 +00: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 23:36:12 +00:00
} else if (e.target.id === 'guide') {
2019-04-21 23:49:47 +00:00
dotgrid.tool.addVertex({ x: this.pos.x, y: this.pos.y })
dotgrid.picker.stop()
2018-08-02 22:57:35 +00:00
}
2018-08-04 19:56:13 +00:00
2018-10-03 23:27:40 +00:00
this.translate()
2018-08-02 22:57:35 +00:00
2019-04-21 23:49:47 +00:00
dotgrid.interface.update()
dotgrid.renderer.update()
2018-10-03 23:27:40 +00:00
e.preventDefault()
2018-08-02 22:57:35 +00:00
}
2018-10-03 23:27:40 +00:00
this.alt = function (e) {
2019-01-09 20:41:41 +00:00
this.pos = this.atEvent(e)
2018-08-02 22:57:35 +00:00
2019-04-21 23:49:47 +00:00
dotgrid.tool.removeSegmentsAt(this.pos)
2018-10-03 23:27:40 +00:00
e.preventDefault()
2018-08-02 22:57:35 +00:00
2019-04-21 23:49:47 +00:00
setTimeout(() => { dotgrid.tool.clear() }, 150)
2018-08-02 22:57:35 +00:00
}
// Position Mods
2019-01-09 20:41:41 +00:00
this.atEvent = function (e) {
return this.snapPos(this.relativePos({ x: e.clientX, y: e.clientY }))
2018-08-02 22:57:35 +00:00
}
2019-01-09 20:41:41 +00:00
this.relativePos = function (pos) {
2018-08-02 22:57:35 +00:00
return {
2019-04-21 23:49:47 +00:00
x: pos.x - dotgrid.renderer.el.offsetLeft,
y: pos.y - dotgrid.renderer.el.offsetTop
2018-10-03 23:27:40 +00:00
}
2018-08-02 22:57:35 +00:00
}
2019-01-09 20:41:41 +00:00
this.snapPos = function (pos) {
2018-08-02 22:57:35 +00:00
return {
2019-04-22 01:25:31 +00: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 23:27:40 +00:00
}
2018-08-02 22:57:35 +00:00
}
2018-08-04 19:56:13 +00:00
2019-04-21 23:36:12 +00:00
function isEqual (a, b) { return a.x === b.x && a.y === b.y }
2019-11-03 18:36:58 +00: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 23:27:40 +00:00
}