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

103 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-10-04 11:27:40 +12:00
'use strict'
2018-08-28 16:34:17 +12:00
2018-10-11 08:09:00 +12:00
DOTGRID.Cursor = function () {
2018-10-04 11:27:40 +12:00
this.pos = { x: 0, y: 0 }
this.translation = null
this.operation = null
2018-08-03 10:57:35 +12:00
2018-11-22 11:14:57 +12:00
this.translate = function (from = null, to = null, multi = false, copy = false) {
if ((from || to) && this.translation == null) { this.translation = { multi: multi, copy: copy }; console.log('Begin translation', multi, copy) }
2018-08-03 10:57:35 +12:00
2018-10-04 11:27:40 +12:00
if (from) { this.translation.from = from }
if (to) { this.translation.to = to }
2018-08-03 10:57:35 +12:00
2018-10-04 11:27:40 +12:00
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) {
this.pos = this.pos_from_event(e)
2018-08-03 10:57:35 +12:00
// Translation
2018-10-05 11:52:17 +12:00
if (DOTGRID.tool.vertex_at(this.pos)) {
2018-11-22 11:14:57 +12:00
this.translate(this.pos, this.pos, e.shiftKey, e.ctrlKey || e.metaKey)
2018-08-03 10:57:35 +12:00
}
2018-10-05 11:52:17 +12:00
DOTGRID.guide.update()
DOTGRID.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.last_pos = { x: 0, y: 0 }
2018-08-04 14:36:45 +12:00
2018-10-04 11:27:40 +12:00
this.move = function (e) {
2018-08-03 10:57:35 +12:00
this.pos = this.pos_from_event(e)
// Translation
2018-10-04 11:27:40 +12:00
if (this.translation) {
this.translate(null, this.pos)
2018-08-03 10:57:35 +12:00
}
2018-10-04 11:27:40 +12:00
if (this.last_pos.x != this.pos.x || this.last_pos.y != this.pos.y) {
2018-10-05 11:52:17 +12:00
DOTGRID.guide.update()
2018-08-04 14:36:45 +12:00
}
2018-10-05 11:52:17 +12:00
DOTGRID.interface.update()
2018-10-04 11:27:40 +12:00
e.preventDefault()
2018-08-04 14:36:45 +12:00
2018-10-04 11:27:40 +12:00
this.last_pos = this.pos
2018-08-03 10:57:35 +12:00
}
2018-10-04 11:27:40 +12:00
this.up = function (e) {
2018-08-03 10:57:35 +12:00
this.pos = this.pos_from_event(e)
2018-10-05 11:52:17 +12:00
if (e.altKey) { DOTGRID.tool.remove_segments_at(this.pos); this.translate(); return }
2018-08-03 10:57:35 +12:00
2018-10-04 11:27:40 +12:00
if (this.translation && !is_equal(this.translation.from, this.translation.to)) {
2018-11-22 11:14:57 +12:00
if (this.translation.copy) { DOTGRID.tool.translate_copy(this.translation.from, this.translation.to) } else if (this.translation.multi) { DOTGRID.tool.translate_multi(this.translation.from, this.translation.to) } else { DOTGRID.tool.translate(this.translation.from, this.translation.to) }
2018-10-04 11:27:40 +12:00
} else if (e.target.id == 'guide') {
2018-10-05 11:52:17 +12:00
DOTGRID.tool.add_vertex({ x: this.pos.x, y: this.pos.y })
DOTGRID.picker.stop()
2018-08-03 10:57:35 +12:00
}
2018-08-05 07:56:13 +12:00
2018-10-04 11:27:40 +12:00
this.translate()
2018-08-03 10:57:35 +12:00
2018-10-05 11:52:17 +12:00
DOTGRID.interface.update()
DOTGRID.guide.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) {
2018-08-03 10:57:35 +12:00
this.pos = this.pos_from_event(e)
2018-10-05 11:52:17 +12:00
DOTGRID.tool.remove_segments_at(this.pos)
2018-10-04 11:27:40 +12:00
e.preventDefault()
2018-08-03 10:57:35 +12:00
2018-10-05 11:52:17 +12:00
setTimeout(() => { DOTGRID.tool.clear() }, 150)
2018-08-03 10:57:35 +12:00
}
// Position Mods
2018-10-04 11:27:40 +12:00
this.pos_from_event = function (e) {
return this.pos_snap(this.pos_relative({ x: e.clientX, y: e.clientY }))
2018-08-03 10:57:35 +12:00
}
2018-10-04 11:27:40 +12:00
this.pos_relative = function (pos) {
2018-08-03 10:57:35 +12:00
return {
2018-10-05 11:52:17 +12:00
x: pos.x - DOTGRID.guide.el.offsetLeft,
y: pos.y - DOTGRID.guide.el.offsetTop
2018-10-04 11:27:40 +12:00
}
2018-08-03 10:57:35 +12:00
}
2018-10-04 11:27:40 +12:00
this.pos_snap = function (pos) {
2018-10-05 11:52:17 +12:00
const grid = DOTGRID.tool.settings.size.width / DOTGRID.grid_x
2018-08-03 10:57:35 +12:00
return {
2018-10-05 11:52:17 +12:00
x: clamp(step(pos.x, grid), grid, DOTGRID.tool.settings.size.width),
y: clamp(step(pos.y, grid), grid, DOTGRID.tool.settings.size.height + grid)
2018-10-04 11:27:40 +12:00
}
2018-08-03 10:57:35 +12:00
}
2018-08-05 07:56:13 +12:00
2018-10-04 11:27:40 +12:00
function is_equal (a, b) { return a.x == b.x && a.y == b.y }
}