Skip to content

Commit dc62726

Browse files
author
James Petts
committed
fix(FreehandMouseTool): Small fixes to the freehand tool in the current multipart-tool framework.
Scrolling whilst drawing finished the previous annotation and closes it. Edit no longer broken. Cannot draw 2 points polygon.
1 parent 59f7054 commit dc62726

2 files changed

Lines changed: 57 additions & 22 deletions

File tree

examples-old/freehand/index.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,6 @@ <h1>Freehand Sculpter Mouse Tool API</h1>
301301
const mouseButtonMask = evt.buttons
302302
? evt.buttons
303303
: convertMouseEventWhichToButtons(evt.which)
304-
// TODO: Let's make this happen automagically for mask/touch conflicts?
305-
setAllToolsPassive();
306304
const toolType = evt.target.getAttribute('data-tool-type')
307305
setButtonActive(toolName, mouseButtonMask, toolType);
308306
cornerstoneTools.setToolActive(toolName, { mouseButtonMask });

src/tools/annotation/FreehandMouseTool.js

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,6 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
514514
addNewMeasurement(evt) {
515515
const eventData = evt.detail;
516516

517-
this._drawing = true;
518-
519517
this._startDrawing(eventData);
520518
this._addPoint(eventData);
521519

@@ -571,7 +569,6 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
571569
this._activateModify(element);
572570

573571
// Interupt eventDispatchers
574-
state.isMultiPartToolActive = true;
575572

576573
preventPropagation(evt);
577574
}
@@ -658,10 +655,6 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
658655
_drawingMouseUpCallback(evt) {
659656
const eventData = evt.detail;
660657

661-
if (!this.options.mouseButtonMask.includes(eventData.buttons)) {
662-
return;
663-
}
664-
665658
if (!this._dragging) {
666659
return;
667660
}
@@ -795,10 +788,6 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
795788
_editMouseUpCallback(evt) {
796789
const eventData = evt.detail;
797790

798-
if (!this.options.mouseButtonMask.includes(eventData.buttons)) {
799-
return;
800-
}
801-
802791
const element = eventData.element;
803792
const toolState = getToolState(eventData.element, this.name);
804793

@@ -807,8 +796,6 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
807796
this._dropHandle(eventData, toolState);
808797
this._endDrawing(element);
809798

810-
state.isMultiPartToolActive = false;
811-
812799
external.cornerstone.updateImage(eventData.element);
813800
}
814801

@@ -865,7 +852,6 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
865852
const config = this.configuration;
866853

867854
// Block event distpatcher whilst drawing
868-
state.isMultiPartToolActive = true;
869855

870856
this._referencedElement = element;
871857

@@ -877,6 +863,8 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
877863
const toolState = getToolState(eventData.element, this.name);
878864

879865
config.currentTool = toolState.data.length - 1;
866+
867+
config.activeDrawingToolReference = toolState.data[config.currentTool];
880868
}
881869

882870
/**
@@ -976,8 +964,6 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
976964
data.canComplete = false;
977965

978966
if (this._drawing) {
979-
this._drawing = false;
980-
state.isMultiPartToolActive = false;
981967
this._deactivateDraw(element);
982968
}
983969

@@ -1118,7 +1104,11 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
11181104
mousePoint
11191105
);
11201106

1121-
if (mouseAtOriginHandle && !freehandIntersect.end(points)) {
1107+
if (
1108+
mouseAtOriginHandle &&
1109+
!freehandIntersect.end(points) &&
1110+
points.length > 2
1111+
) {
11221112
data.canComplete = true;
11231113
invalidHandlePlacement = false;
11241114
} else {
@@ -1276,6 +1266,9 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
12761266
* @returns {undefined}
12771267
*/
12781268
_activateDraw(element) {
1269+
this._drawing = true;
1270+
state.isMultiPartToolActive = true;
1271+
12791272
// Polygonal Mode
12801273
element.addEventListener(EVENTS.MOUSE_DOWN, this._drawingMouseDownCallback);
12811274
element.addEventListener(EVENTS.MOUSE_MOVE, this._drawingMouseMoveCallback);
@@ -1296,6 +1289,10 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
12961289
* @returns {undefined}
12971290
*/
12981291
_deactivateDraw(element) {
1292+
this._drawing = false;
1293+
state.isMultiPartToolActive = false;
1294+
this.configuration.activeDrawingToolReference = null;
1295+
12991296
element.removeEventListener(
13001297
EVENTS.MOUSE_DOWN,
13011298
this._drawingMouseDownCallback
@@ -1322,6 +1319,8 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
13221319
* @returns {undefined}
13231320
*/
13241321
_activateModify(element) {
1322+
state.isToolLocked = true;
1323+
13251324
element.addEventListener(EVENTS.MOUSE_UP, this._editMouseUpCallback);
13261325
element.addEventListener(EVENTS.MOUSE_DRAG, this._editMouseDragCallback);
13271326
element.addEventListener(EVENTS.MOUSE_CLICK, this._editMouseUpCallback);
@@ -1338,6 +1337,8 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
13381337
* @returns {undefined}
13391338
*/
13401339
_deactivateModify(element) {
1340+
state.isToolLocked = false;
1341+
13411342
element.removeEventListener(EVENTS.MOUSE_UP, this._editMouseUpCallback);
13421343
element.removeEventListener(EVENTS.MOUSE_DRAG, this._editMouseDragCallback);
13431344
element.removeEventListener(EVENTS.MOUSE_CLICK, this._editMouseUpCallback);
@@ -1453,7 +1454,7 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
14531454
*
14541455
* @public
14551456
* @param {Object} element - The element on which the roi is being drawn.
1456-
* @returns {undefined}
1457+
* @returns {null}
14571458
*/
14581459
cancelDrawing(element) {
14591460
if (!this._drawing) {
@@ -1476,8 +1477,43 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
14761477

14771478
removeToolState(element, this.name, data);
14781479

1479-
this._drawing = false;
1480-
state.isMultiPartToolActive = false;
1480+
this._deactivateDraw(element);
1481+
1482+
external.cornerstone.updateImage(element);
1483+
}
1484+
1485+
/**
1486+
* newImageCallback - new image event handler.
1487+
*
1488+
* @public
1489+
* @param {object} evt The event.
1490+
* @returns {null}
1491+
*/
1492+
newImageCallback(evt) {
1493+
const config = this.configuration;
1494+
1495+
if (!(this._drawing && config.activeDrawingToolReference)) {
1496+
return;
1497+
}
1498+
1499+
// Actively drawing but scrolled to different image.
1500+
1501+
const element = evt.detail.element;
1502+
const data = config.activeDrawingToolReference;
1503+
1504+
data.active = false;
1505+
data.highlight = false;
1506+
data.handles.invalidHandlePlacement = false;
1507+
1508+
// Connect the end handle to the origin handle
1509+
const points = data.handles.points;
1510+
points[config.currentHandle - 1].lines.push(points[0]);
1511+
1512+
// Reset the current handle
1513+
config.currentHandle = 0;
1514+
config.currentTool = -1;
1515+
data.canComplete = false;
1516+
14811517
this._deactivateDraw(element);
14821518

14831519
external.cornerstone.updateImage(element);
@@ -1501,6 +1537,7 @@ function defaultFreehandConfiguration() {
15011537
invalidColor: 'crimson',
15021538
currentHandle: 0,
15031539
currentTool: -1,
1540+
activeDrawingToolReference: null,
15041541
};
15051542
}
15061543

0 commit comments

Comments
 (0)