|
| 1 | +/**************************************************************************** |
| 2 | + * Copyright 2021 EPAM Systems |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + ***************************************************************************/ |
| 16 | + |
| 17 | +import { |
| 18 | + Vec2, |
| 19 | + fromArrowAddition, |
| 20 | + fromArrowDeletion, |
| 21 | + fromArrowResizing, |
| 22 | + fromMultipleMove |
| 23 | +} from 'ketcher-core' |
| 24 | + |
| 25 | +function ReactionArrowTool(editor, mode) { |
| 26 | + if (!(this instanceof ReactionArrowTool)) |
| 27 | + return new ReactionArrowTool(editor, mode) |
| 28 | + |
| 29 | + this.mode = mode |
| 30 | + this.editor = editor |
| 31 | + this.editor.selection(null) |
| 32 | +} |
| 33 | + |
| 34 | +ReactionArrowTool.prototype.mousedown = function (event) { |
| 35 | + var rnd = this.editor.render |
| 36 | + const p0 = rnd.page2obj(event) |
| 37 | + this.dragCtx = { p0 } |
| 38 | + |
| 39 | + var ci = this.editor.findItem(event, ['rxnArrows']) |
| 40 | + if (ci && ci.map === 'rxnArrows') { |
| 41 | + this.editor.hover(null) |
| 42 | + this.editor.selection({ rxnArrows: [ci.id] }) |
| 43 | + this.dragCtx.ci = ci |
| 44 | + } else { |
| 45 | + this.dragCtx.isNew = true |
| 46 | + this.editor.selection(null) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +ReactionArrowTool.prototype.mousemove = function (event) { |
| 51 | + var rnd = this.editor.render |
| 52 | + if (this.dragCtx) { |
| 53 | + const current = rnd.page2obj(event) |
| 54 | + const diff = current.sub(this.dragCtx.p0) |
| 55 | + this.dragCtx.previous = current |
| 56 | + if (this.dragCtx.ci) { |
| 57 | + if (this.dragCtx.action) this.dragCtx.action.perform(rnd.ctab) |
| 58 | + if (!this.dragCtx.ci.ref) { |
| 59 | + this.dragCtx.action = fromMultipleMove( |
| 60 | + rnd.ctab, |
| 61 | + this.editor.selection() || {}, |
| 62 | + diff |
| 63 | + ) |
| 64 | + } else { |
| 65 | + this.dragCtx.action = fromArrowResizing( |
| 66 | + rnd.ctab, |
| 67 | + this.dragCtx.ci.id, |
| 68 | + diff, |
| 69 | + current, |
| 70 | + this.dragCtx.ci.ref |
| 71 | + ) |
| 72 | + } |
| 73 | + this.editor.update(this.dragCtx.action, true) |
| 74 | + } else { |
| 75 | + if (!this.dragCtx.action) { |
| 76 | + const action = fromArrowAddition( |
| 77 | + rnd.ctab, |
| 78 | + [this.dragCtx.p0, this.dragCtx.p0], |
| 79 | + this.mode |
| 80 | + ) |
| 81 | + //TODO: need to rework actions/operations logic |
| 82 | + const addOperation = action.operations[0] |
| 83 | + const itemId = addOperation.data.id |
| 84 | + this.dragCtx.itemId = itemId |
| 85 | + this.dragCtx.action = action |
| 86 | + this.editor.update(this.dragCtx.action, true) |
| 87 | + } else { |
| 88 | + this.dragCtx.action.perform(rnd.ctab) |
| 89 | + } |
| 90 | + |
| 91 | + this.dragCtx.action = fromArrowResizing( |
| 92 | + rnd.ctab, |
| 93 | + this.dragCtx.itemId, |
| 94 | + diff, |
| 95 | + current, |
| 96 | + null |
| 97 | + ) |
| 98 | + this.editor.update(this.dragCtx.action, true) |
| 99 | + } |
| 100 | + } else { |
| 101 | + const items = this.editor.findItem(event, ['rxnArrows']) |
| 102 | + this.editor.hover(items) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +ReactionArrowTool.prototype.mouseup = function (event) { |
| 107 | + if (!this.dragCtx) return true |
| 108 | + const rnd = this.editor.render |
| 109 | + |
| 110 | + const p0 = this.dragCtx.p0 |
| 111 | + const p1 = getDefaultLengthPos(p0, this.dragCtx.previous) |
| 112 | + |
| 113 | + if (this.dragCtx.action) { |
| 114 | + if (this.dragCtx.isNew) { |
| 115 | + this.editor.update(fromArrowDeletion(rnd.ctab, this.dragCtx.itemId), true) |
| 116 | + this.dragCtx.action = fromArrowAddition(rnd.ctab, [p0, p1], this.mode) |
| 117 | + } |
| 118 | + this.editor.update(this.dragCtx.action) |
| 119 | + } |
| 120 | + delete this.dragCtx |
| 121 | + return true |
| 122 | +} |
| 123 | + |
| 124 | +ReactionArrowTool.prototype.click = function (event) { |
| 125 | + const rnd = this.editor.render |
| 126 | + const ci = this.editor.findItem(event, ['rxnArrows']) |
| 127 | + const p0 = rnd.page2obj(event) |
| 128 | + if (!ci) { |
| 129 | + this.editor.update( |
| 130 | + fromArrowAddition(rnd.ctab, [p0, getDefaultLengthPos(p0)], this.mode) |
| 131 | + ) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +function getArrowParams(x1, y1, x2, y2) { |
| 136 | + const length = Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) |
| 137 | + const angle = calcAngle(x2, y2, x1, y1) |
| 138 | + return { length, angle } |
| 139 | +} |
| 140 | + |
| 141 | +function getDefaultLengthPos(pos1, pos2) { |
| 142 | + const minLength = 1.5 |
| 143 | + const defaultLength = 2 |
| 144 | + if (!pos2) { |
| 145 | + return new Vec2(pos1.x + defaultLength, pos1.y) |
| 146 | + } |
| 147 | + const arrowParams = getArrowParams(pos1.x, pos1.y, pos2.x, pos2.y) |
| 148 | + if (arrowParams.length <= minLength) { |
| 149 | + const newPos = new Vec2() |
| 150 | + newPos.x = |
| 151 | + pos1.x + defaultLength * Math.cos((Math.PI * arrowParams.angle) / 180) |
| 152 | + newPos.y = |
| 153 | + pos1.y + defaultLength * Math.sin((Math.PI * arrowParams.angle) / 180) |
| 154 | + return newPos |
| 155 | + } |
| 156 | + return pos2 |
| 157 | +} |
| 158 | + |
| 159 | +function calcAngle(x1, y1, x2, y2) { |
| 160 | + const x = x1 - x2, |
| 161 | + y = y1 - y2 |
| 162 | + if (!x && !y) { |
| 163 | + return 0 |
| 164 | + } |
| 165 | + return (180 + (Math.atan2(-y, -x) * 180) / Math.PI + 360) % 360 |
| 166 | +} |
| 167 | + |
| 168 | +export default ReactionArrowTool |
0 commit comments