-
Notifications
You must be signed in to change notification settings - Fork 165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#2088 Selection Tool: use rounded rectangles for selection of bonds and atom labels #2602
Changes from 4 commits
6ec58d3
d41af8e
1e19987
f62468f
596488c
7558370
0086eac
dd137d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,18 +57,167 @@ class ReBond extends ReObject { | |
return true | ||
} | ||
|
||
static bondRecalc(bond: ReBond, restruct: ReStruct, options: any): void { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is just converted from member to static function, b/c we need to recalculate bond positions after they moved (it is used later in dropAndMerge.ts) |
||
const render = restruct.render | ||
const atom1 = restruct.atoms.get(bond.b.begin) | ||
const atom2 = restruct.atoms.get(bond.b.end) | ||
|
||
if ( | ||
!atom1 || | ||
!atom2 || | ||
bond.b.hb1 === undefined || | ||
bond.b.hb2 === undefined | ||
) { | ||
return | ||
} | ||
|
||
const p1 = Scale.obj2scaled(atom1.a.pp, render.options) | ||
const p2 = Scale.obj2scaled(atom2.a.pp, render.options) | ||
const hb1 = restruct.molecule.halfBonds.get(bond.b.hb1) | ||
const hb2 = restruct.molecule.halfBonds.get(bond.b.hb2) | ||
|
||
if (!hb1?.dir || !hb2?.dir) return | ||
|
||
hb1.p = shiftBondEnd(atom1, p1, hb1.dir, 2 * options.lineWidth) | ||
hb2.p = shiftBondEnd(atom2, p2, hb2.dir, 2 * options.lineWidth) | ||
bond.b.center = Vec2.lc2(atom1.a.pp, 0.5, atom2.a.pp, 0.5) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be you can add a comment for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Nitvex I actually just moved the bondRecalc function so it is a static method now, its a bit of a mystery for myself what lc2 does 😄 I'll try to figure it out and a dd a comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @KonstantinEpam23 @Nitvex I thought lc was the abbreviation of Linear Combination. The difference between |
||
bond.b.len = Vec2.dist(p1, p2) | ||
bond.b.sb = options.lineWidth * 5 | ||
/* eslint-disable no-mixed-operators */ | ||
bond.b.sa = Math.max(bond.b.sb, bond.b.len / 2 - options.lineWidth * 2) | ||
/* eslint-enable no-mixed-operators */ | ||
bond.b.angle = (Math.atan2(hb1.dir.y, hb1.dir.x) * 180) / Math.PI | ||
} | ||
|
||
drawHover(render: Render) { | ||
const ret = this.makeHoverPlate(render) | ||
render.ctab.addReObjectPath(LayerMap.hovering, this.visel, ret) | ||
return ret | ||
} | ||
|
||
getSelectionPoints(render: Render) { | ||
const bond: Bond = this.b | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
const { ctab: restruct, options } = render | ||
const { bondThickness, doubleBondWidth, stereoBondWidth } = options | ||
const regularSelectionThikness = doubleBondWidth + bondThickness | ||
|
||
// half-bonds | ||
const halfBondStart = restruct.molecule.halfBonds.get(bond.hb1!)!.p | ||
const halfBondEnd = restruct.molecule.halfBonds.get(bond.hb2!)!.p | ||
|
||
const isStereoBond = | ||
bond.stereo !== Bond.PATTERN.STEREO.NONE && | ||
bond.stereo !== Bond.PATTERN.STEREO.CIS_TRANS | ||
|
||
const addStereoPadding = isStereoBond ? stereoBondWidth / 2 : 0 | ||
const contourStart = Vec2.getLinePoint( | ||
halfBondEnd, | ||
halfBondStart, | ||
-bondThickness * 2.5 - addStereoPadding | ||
) | ||
const contourEnd = Vec2.getLinePoint( | ||
halfBondStart, | ||
halfBondEnd, | ||
-bondThickness * 2.5 | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
const addStart = isStereoBond | ||
? stereoBondWidth * 0.25 | ||
: regularSelectionThikness | ||
const addEnd = isStereoBond | ||
? stereoBondWidth + (regularSelectionThikness * 4) / stereoBondWidth | ||
: regularSelectionThikness | ||
|
||
const contourPaddedStart = Vec2.getLinePoint( | ||
contourStart, | ||
contourEnd, | ||
addEnd | ||
) | ||
const contourPaddedEnd = Vec2.getLinePoint( | ||
contourEnd, | ||
contourStart, | ||
addStart | ||
) | ||
|
||
const startPoint = contourStart.add(new Vec2(addEnd, 0)) | ||
const endPoint = contourEnd.add(new Vec2(addStart, 0)) | ||
const padStartPoint = contourPaddedStart.add(new Vec2(addEnd, 0)) | ||
const padEndPoint = contourPaddedEnd.add(new Vec2(addStart, 0)) | ||
|
||
const { angle } = bond | ||
|
||
const startTop = startPoint.rotateAroundOrigin( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
angle + 90, | ||
new Vec2(contourStart.x, contourStart.y) | ||
) | ||
const startBottom = startPoint.rotateAroundOrigin( | ||
angle - 90, | ||
new Vec2(contourStart.x, contourStart.y) | ||
) | ||
const startPadTop = padStartPoint.rotateAroundOrigin( | ||
angle + 90, | ||
contourPaddedStart | ||
) | ||
const startPadBottom = padStartPoint.rotateAroundOrigin( | ||
angle - 90, | ||
contourPaddedStart | ||
) | ||
const endTop = endPoint.rotateAroundOrigin(angle + 90, contourEnd) | ||
const endBottom = endPoint.rotateAroundOrigin(angle - 90, contourEnd) | ||
const endPadTop = padEndPoint.rotateAroundOrigin( | ||
angle + 90, | ||
contourPaddedEnd | ||
) | ||
const endPadBottom = padEndPoint.rotateAroundOrigin( | ||
angle - 90, | ||
contourPaddedEnd | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return [ | ||
startPadTop, | ||
startTop, | ||
endTop, | ||
endPadTop, | ||
endPadBottom, | ||
endBottom, | ||
startPadBottom, | ||
startBottom | ||
] | ||
} | ||
|
||
getSelectionContour(render: Render) { | ||
const { paper } = render | ||
const [ | ||
startPadTop, | ||
startTop, | ||
endTop, | ||
endPadTop, | ||
endPadBottom, | ||
endBottom, | ||
startPadBottom, | ||
startBottom | ||
] = this.getSelectionPoints(render) | ||
|
||
// for a visual representation of the points | ||
// please refer to: ketcher-core/docs/data/hover_selection_exp.png | ||
const pathString = ` | ||
M ${startTop.x} ${startTop.y} | ||
L ${endTop.x} ${endTop.y} | ||
C ${endPadTop.x} ${endPadTop.y}, ${endPadBottom.x} ${endPadBottom.y}, ${endBottom.x} ${endBottom.y} | ||
L ${startBottom.x} ${startBottom.y} | ||
C ${startPadBottom.x} ${startPadBottom.y}, ${startPadTop.x} ${startPadTop.y}, ${startTop.x} ${startTop.y} | ||
` | ||
|
||
return paper.path(pathString) | ||
} | ||
|
||
makeHoverPlate(render: Render) { | ||
const restruct = render.ctab | ||
const options = render.options | ||
bondRecalc(this, render.ctab, options) | ||
ReBond.bondRecalc(this, restruct, options) | ||
const bond = this.b | ||
const sgroups = render.ctab.sgroups | ||
const functionalGroups = render.ctab.molecule.functionalGroups | ||
const sgroups = restruct.sgroups | ||
const functionalGroups = restruct.molecule.functionalGroups | ||
if ( | ||
FunctionalGroup.isBondInContractedFunctionalGroup( | ||
bond, | ||
|
@@ -79,14 +228,13 @@ class ReBond extends ReObject { | |
return null | ||
} | ||
|
||
const c = Scale.obj2scaled(this.b.center, options) | ||
return render.paper | ||
.circle(c.x, c.y, 0.8 * options.atomSelectionPlateRadius) | ||
.attr(options.hoverStyle) | ||
const rect = this.getSelectionContour(render) | ||
|
||
return rect.attr({ ...options.hoverStyle }) | ||
} | ||
|
||
makeSelectionPlate(restruct: ReStruct, paper: any, options: any) { | ||
bondRecalc(this, restruct, options) | ||
makeSelectionPlate(restruct: ReStruct, _: any, options: any) { | ||
ReBond.bondRecalc(this, restruct, options) | ||
const bond = this.b | ||
const sgroups = restruct.render.ctab.sgroups | ||
const functionalGroups = restruct.render.ctab.molecule.functionalGroups | ||
|
@@ -100,10 +248,9 @@ class ReBond extends ReObject { | |
return null | ||
} | ||
|
||
const c = Scale.obj2scaled(this.b.center, options) | ||
return paper | ||
.circle(c.x, c.y, 0.8 * options.atomSelectionPlateRadius) | ||
.attr(options.selectionStyle) | ||
const rect = this.getSelectionContour(restruct.render) | ||
|
||
return rect.attr(options.selectionStyle) | ||
} | ||
|
||
show(restruct: ReStruct, bid: number, options: any): void { | ||
|
@@ -131,7 +278,7 @@ class ReBond extends ReObject { | |
this.b.hb2 !== undefined ? struct.halfBonds.get(this.b.hb2) : null | ||
|
||
checkStereoBold(bid, this, restruct) | ||
bondRecalc(this, restruct, options) | ||
ReBond.bondRecalc(this, restruct, options) | ||
setDoubleBondShift(this, struct) | ||
if (!hb1 || !hb2) return | ||
this.path = getBondPath(restruct, this, hb1, hb2) | ||
|
@@ -261,8 +408,6 @@ class ReBond extends ReObject { | |
visel: this.visel | ||
}) | ||
} | ||
|
||
this.path.toBack() | ||
} | ||
} | ||
|
||
|
@@ -1000,38 +1145,6 @@ function setDoubleBondShift(bond: ReBond, struct: Struct): void { | |
} | ||
} | ||
|
||
function bondRecalc(bond: ReBond, restruct: ReStruct, options: any): void { | ||
const render = restruct.render | ||
const atom1 = restruct.atoms.get(bond.b.begin) | ||
const atom2 = restruct.atoms.get(bond.b.end) | ||
|
||
if ( | ||
!atom1 || | ||
!atom2 || | ||
bond.b.hb1 === undefined || | ||
bond.b.hb2 === undefined | ||
) { | ||
return | ||
} | ||
|
||
const p1 = Scale.obj2scaled(atom1.a.pp, render.options) | ||
const p2 = Scale.obj2scaled(atom2.a.pp, render.options) | ||
const hb1 = restruct.molecule.halfBonds.get(bond.b.hb1) | ||
const hb2 = restruct.molecule.halfBonds.get(bond.b.hb2) | ||
|
||
if (!hb1?.dir || !hb2?.dir) return | ||
|
||
hb1.p = shiftBondEnd(atom1, p1, hb1.dir, 2 * options.lineWidth) | ||
hb2.p = shiftBondEnd(atom2, p2, hb2.dir, 2 * options.lineWidth) | ||
bond.b.center = Vec2.lc2(atom1.a.pp, 0.5, atom2.a.pp, 0.5) | ||
bond.b.len = Vec2.dist(p1, p2) | ||
bond.b.sb = options.lineWidth * 5 | ||
/* eslint-disable no-mixed-operators */ | ||
bond.b.sa = Math.max(bond.b.sb, bond.b.len / 2 - options.lineWidth * 2) | ||
/* eslint-enable no-mixed-operators */ | ||
bond.b.angle = (Math.atan2(hb1.dir.y, hb1.dir.x) * 180) / Math.PI | ||
} | ||
|
||
function shiftBondEnd( | ||
atom: ReAtom, | ||
pos0: Vec2, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In options.js we just change the values to suit the new UX