Files
pointvec/desktop/sources/scripts/cursor.js

86 lines
2.7 KiB
JavaScript
Raw Normal View History

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