From 36cc25edb8f736734795363947cbbbb3471747ca Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Thu, 22 Nov 2018 11:14:57 +1200 Subject: [PATCH] Added multi/drag/copy --- desktop/sources/scripts/cursor.js | 8 +++--- desktop/sources/scripts/tool.js | 46 ++++++++++++++++++++++++++----- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/desktop/sources/scripts/cursor.js b/desktop/sources/scripts/cursor.js index 4ecae4b..3204e46 100644 --- a/desktop/sources/scripts/cursor.js +++ b/desktop/sources/scripts/cursor.js @@ -5,8 +5,8 @@ DOTGRID.Cursor = function () { this.translation = null this.operation = null - this.translate = function (from = null, to = null, multi = false) { - if ((from || to) && this.translation == null) { this.translation = { multi: multi }; console.log('Begin translation') } + 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) } if (from) { this.translation.from = from } if (to) { this.translation.to = to } @@ -21,7 +21,7 @@ DOTGRID.Cursor = function () { // Translation if (DOTGRID.tool.vertex_at(this.pos)) { - this.translate(this.pos, this.pos, e.shiftKey) + this.translate(this.pos, this.pos, e.shiftKey, e.ctrlKey || e.metaKey) } DOTGRID.guide.update() @@ -55,7 +55,7 @@ DOTGRID.Cursor = function () { if (e.altKey) { DOTGRID.tool.remove_segments_at(this.pos); this.translate(); return } if (this.translation && !is_equal(this.translation.from, this.translation.to)) { - if (this.translation.multi) { DOTGRID.tool.translate_multi(this.translation.from, this.translation.to) } else { DOTGRID.tool.translate(this.translation.from, this.translation.to) } + 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) } } else if (e.target.id == 'guide') { DOTGRID.tool.add_vertex({ x: this.pos.x, y: this.pos.y }) DOTGRID.picker.stop() diff --git a/desktop/sources/scripts/tool.js b/desktop/sources/scripts/tool.js index 74fb2d3..7300f93 100644 --- a/desktop/sources/scripts/tool.js +++ b/desktop/sources/scripts/tool.js @@ -109,6 +109,20 @@ DOTGRID.Tool = function () { DOTGRID.interface.update(true) } + this.select_segment_at = function (pos, source = this.layer()) { + let target_segment = null + for (const segment_id in source) { + let segment = source[segment_id] + for (const vertex_id in segment.vertices) { + let vertex = segment.vertices[vertex_id] + if (vertex.x == Math.abs(pos.x) && vertex.y == Math.abs(pos.y)) { + return segment + } + } + } + return null + } + this.add_vertex = function (pos) { pos = { x: Math.abs(pos.x), y: Math.abs(pos.y) } this.vertices.push(pos) @@ -245,15 +259,33 @@ DOTGRID.Tool = function () { } this.translate_multi = function (a, b) { - let offset = { x: a.x - b.x, y: a.y - b.y } + const offset = { x: a.x - b.x, y: a.y - b.y } + const segment = this.select_segment_at(a) - for (const segment_id in this.layer()) { - let segment = this.layer()[segment_id] - for (const vertex_id in segment.vertices) { - let vertex = segment.vertices[vertex_id] - segment.vertices[vertex_id] = { x: vertex.x - offset.x, y: vertex.y - offset.y } - } + if (!segment) { return } + + for (const vertex_id in segment.vertices) { + let vertex = segment.vertices[vertex_id] + segment.vertices[vertex_id] = { x: vertex.x - offset.x, y: vertex.y - offset.y } } + + DOTGRID.history.push(this.layers) + this.clear() + DOTGRID.guide.update() + } + + this.translate_copy = function (a, b) { + const offset = { x: a.x - b.x, y: a.y - b.y } + const segment = this.select_segment_at(a, copy(this.layer())) + + if (!segment) { return } + + for (const vertex_id in segment.vertices) { + let vertex = segment.vertices[vertex_id] + segment.vertices[vertex_id] = { x: vertex.x - offset.x, y: vertex.y - offset.y } + } + this.layer().push(segment) + DOTGRID.history.push(this.layers) this.clear() DOTGRID.guide.update()