-
Notifications
You must be signed in to change notification settings - Fork 456
/
setToolMode.js
699 lines (621 loc) · 19.1 KB
/
setToolMode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
import EVENTS from './../events.js';
import triggerEvent from './../util/triggerEvent.js';
import getToolForElement from './getToolForElement.js';
import {
setToolCursor,
resetToolCursor,
hideToolCursor,
} from './setToolCursor.js';
import { getLogger } from '../util/logger.js';
import store, { getModule } from './index.js';
const globalConfiguration = getModule('globalConfiguration');
const logger = getLogger('store:setToolMode');
/**
* Sets a tool's state, with the provided toolName and element, to 'active'. Active tools are rendered,
* respond to user input, and can create new data.
*
* @public
* @function setToolActiveForElement
* @memberof CornerstoneTools
*
* @example <caption>Setting a tool 'active' for a specific interaction type.</caption>
* // Sets length tool to Active
* setToolActiveForElement(element, 'Length', {
* mouseButtonMask: 1
* }, ['Mouse'])
* @example <caption>Setting a tool 'active' for all interaction types.</caption>
* // Sets length tool to Active
* setToolActiveForElement(element, 'Length', {
* mouseButtonMask: 1
* })
* @param {HTMLElement} element
* @param {string} toolName
* @param {(Object|string[]|number)} options
* @param {(string[])} interactionTypes
* @returns {undefined}
*/
const setToolActiveForElement = function(
element,
toolName,
options,
interactionTypes
) {
// If interactionTypes was passed in via options
if (interactionTypes === undefined && Array.isArray(options)) {
interactionTypes = options;
options = null;
}
const tool = getToolForElement(element, toolName);
if (tool) {
_resolveInputConflicts(element, tool, options, interactionTypes);
// Iterate over specific interaction types and set active
// This is used as a secondary check on active tools to find the active "parts" of the tool
tool.supportedInteractionTypes.forEach(interactionType => {
if (
interactionTypes === undefined ||
interactionTypes.includes(interactionType)
) {
options[`is${interactionType}Active`] = true;
} else {
options[`is${interactionType}Active`] = false;
}
});
if (
globalConfiguration.configuration.showSVGCursors &&
tool.supportedInteractionTypes.includes('Mouse')
) {
_setToolCursorIfPrimary(element, options, tool);
}
}
// Resume normal behavior
setToolModeForElement('active', null, element, toolName, options);
};
function _setToolCursorIfPrimary(element, options, tool) {
let mouseButtonMask;
if (typeof options === 'number') {
mouseButtonMask = [options];
} else {
mouseButtonMask = options.mouseButtonMask;
}
if (mouseButtonMask.includes(1)) {
if (tool.svgCursor) {
setToolCursor(tool.element, tool.svgCursor);
} else if (tool.hideDefaultCursor) {
hideToolCursor(element);
} else {
resetToolCursor(element);
}
}
}
/**
* Sets all tool's state, with the provided toolName, to 'active'. Active tools are rendered,
* respond to user input, and can create new data.
* @public
* @function setToolActive
* @memberof CornerstoneTools
*
* @param {string} toolName
* @param {(Object|string[]|number)} options
* @param {(string[])} interactionTypes
* @returns {undefined}
*/
const setToolActive = function(toolName, options, interactionTypes) {
_trackGlobalToolModeChange('active', toolName, options, interactionTypes);
store.state.enabledElements.forEach(element => {
setToolActiveForElement(element, toolName, options, interactionTypes);
});
};
/**
* Sets a tool's state, with the provided toolName and element, to 'disabled'. Disabled tools are not rendered,
* and do not respond to user input
* @public
* @function setToolDisabledForElement
* @memberof CornerstoneTools
*
* @param {HTMLElement} element
* @param {string} toolName
* @param {(Object|number)} options
* @returns {undefined}
*/
const setToolDisabledForElement = setToolModeForElement.bind(
null,
'disabled',
null
);
/**
* Sets all tool's state, with the provided toolName, to 'disabled'. Disabled tools are not rendered,
* and do not respond to user input
* @public
* @function setToolDisabled
* @memberof CornerstoneTools
*
* @param {string} toolName
* @param {(Object|number)} options
* @returns {undefined}
*/
const setToolDisabled = setToolMode.bind(null, 'disabled', null);
/**
* Sets a tool's state, with the provided toolName and element, to 'enabled'. Enabled tools are rendered,
* but do not respond to user input
* @public
* @function setToolEnabledForElement
* @memberof CornerstoneTools
*
* @param {HTMLElement} element
* @param {string} toolName
* @param {(Object|number)} options
* @returns {undefined}
*/
const setToolEnabledForElement = setToolModeForElement.bind(
null,
'enabled',
null
);
/**
* Sets all tool's state, with the provided toolName, to 'enabled'. Enabled tools are rendered,
* but do not respond to user input
* @public
* @function setToolEnabled
* @memberof CornerstoneTools
*
* @param {string} toolName
* @param {(Object|number)} options
* @returns {undefined}
*/
const setToolEnabled = setToolMode.bind(null, 'enabled', null);
/**
* Sets a tool's state, with the provided toolName and element, to 'passive'. Passive tools are rendered and respond to user input,
* but do not create new measurements or annotations.
* @public
* @function setToolPassiveForElement
* @memberof CornerstoneTools
*
* @param {HTMLElement} element
* @param {string} toolName
* @param {(Object|number)} options
* @returns {undefined}
*/
const setToolPassiveForElement = setToolModeForElement.bind(
null,
'passive',
EVENTS.TOOL_DEACTIVATED
);
/**
* Sets all tool's state, with the provided toolName, to 'passive'. Passive tools are rendered and respond to user input,
* but do not create new measurements or annotations.
* @public
* @function setToolPassive
* @memberof CornerstoneTools
*
* @param {string} toolName
* @param {(Object|number)} options
* @returns {undefined}
*/
const setToolPassive = setToolMode.bind(
null,
'passive',
EVENTS.TOOL_DEACTIVATED
);
/**
* An internal method that helps make sure we change tool mode in a consistent
* way
* @private
* @function setToolModeForElement
*
* @param {string} mode
* @param {string} changeEvent
* @param {HTMLElement} element
* @param {string} toolName
* @param {(Object|number[]|number)} options
* @returns {undefined}
*/
function setToolModeForElement(mode, changeEvent, element, toolName, options) {
const tool = getToolForElement(element, toolName);
if (!tool) {
logger.warn('Unable to find tool "%s" for enabledElement', toolName);
return;
}
options = _getNormalizedOptions(options);
// Keep the same if not an array (undefined)
// Reset if empty array
// Merge if array contains any bindings
if (
Array.isArray(options.mouseButtonMask) &&
options.mouseButtonMask.length !== 0 &&
Array.isArray(tool.options.mouseButtonMask)
) {
options.mouseButtonMask = _mergeMouseButtonMask(
options.mouseButtonMask,
tool.options.mouseButtonMask
);
}
// Set mode & options
tool.mode = mode;
tool.mergeOptions(options);
// Call tool's hook for this event, if one exists
if (tool[`${mode}Callback`]) {
tool[`${mode}Callback`](element, options);
}
// Emit event indicating tool state change
if (changeEvent) {
const statusChangeEventData = {
options,
toolName,
toolType: toolName, // Deprecation notice: toolType will be replaced by toolName
type: changeEvent,
};
triggerEvent(element, changeEvent, statusChangeEventData);
}
// Trigger Update
// Todo: don't error out if image hasn't been loaded...
// Cornerstone.updateImage(element);
}
/**
* A helper/quick way to set a tool's mode for all canvases
*
* @private
* @function setToolMode
*
* @param {string} mode
* @param {string} changeEvent
* @param {string} toolName
* @param {(Object|number)} options
* @returns {undefined}
*/
function setToolMode(mode, changeEvent, toolName, options) {
_trackGlobalToolModeChange(mode, toolName, options);
store.state.enabledElements.forEach(element => {
setToolModeForElement(mode, changeEvent, element, toolName, options);
});
}
/**
* Find tool's that conflict with the incoming tool's mouse/touch bindings and
* resolve those conflicts.
*
* @private
* @function _resolveInputConflicts
*
* @param {HTMLElement} element
* @param {Object} tool
* @param {(Object|number)} options
* @param {(Array)} interactionTypes
* @returns {undefined}
*/
function _resolveInputConflicts(element, tool, options, interactionTypes) {
// Iterate over the interaction types our tool supports.
// For each one we intend to activate, check for potential conflicts
// And resolve them
tool.supportedInteractionTypes.forEach(interactionType => {
if (
interactionTypes === undefined ||
interactionTypes.includes(interactionType)
) {
const inputResolver = _inputResolvers[interactionType];
if (inputResolver) {
inputResolver(tool, element, options);
} else {
logger.warn(
'Unable to resolve input conflicts for type %s',
interactionType
);
}
}
});
const activeToolsForElement = store.state.tools.filter(
t =>
t.element === element &&
t.mode === 'active' &&
t.supportedInteractionTypes.length > 0
);
activeToolsForElement.forEach(t => {
let toolHasAnyActiveInteractionType = false;
t.supportedInteractionTypes.forEach(interactionType => {
if (t.options[`is${interactionType}Active`]) {
toolHasAnyActiveInteractionType = true;
}
});
if (!toolHasAnyActiveInteractionType) {
logger.log("Setting tool %s's to PASSIVE", t.name);
setToolPassiveForElement(element, t.name);
}
});
}
/**
* Resolves conflicting active tools when activating a tool for mouse interaction
* @private
* @function _resolveMouseInputConflicts
*
* @param {Object} tool
* @param {HTMLElement} element
* @param {(Object|number)} options
* @returns {undefined}
*/
function _resolveMouseInputConflicts(tool, element, options) {
const mouseButtonMask = _getNormalizedOptions(options).mouseButtonMask;
const hasMouseButtonMask =
Array.isArray(mouseButtonMask) && mouseButtonMask.length > 0;
if (!hasMouseButtonMask) {
return;
}
const activeToolWithMatchingMouseButtonMask = store.state.tools.find(
t =>
t.element === element &&
t.mode === 'active' &&
t.options.isMouseActive === true &&
Array.isArray(t.options.mouseButtonMask) &&
t.options.mouseButtonMask.some(v => mouseButtonMask.includes(v))
);
if (activeToolWithMatchingMouseButtonMask) {
// Remove collissions
activeToolWithMatchingMouseButtonMask.options.mouseButtonMask = activeToolWithMatchingMouseButtonMask.options.mouseButtonMask.filter(
mask => !mouseButtonMask.includes(mask)
);
// If no remaining bindings, set inactive
if (
activeToolWithMatchingMouseButtonMask.options.mouseButtonMask.length === 0
) {
activeToolWithMatchingMouseButtonMask.options.isMouseActive = false;
}
}
}
/**
* Resolves conflicting active tools when activating a tool for touch interaction
* @private
* @function _resolveTouchInputConflicts
*
* @param {Object} tool
* @param {HTMLElement} element
* @returns {undefined}
*/
function _resolveTouchInputConflicts(tool, element) {
const activeTouchTool = store.state.tools.find(
t =>
t.element === element &&
t.mode === 'active' &&
t.options.isTouchActive === true
);
const activeMultiTouchToolWithOneTouchPointer = store.state.tools.find(
t =>
t.element === element &&
t.mode === 'active' &&
t.options.isMultiTouchActive === true &&
t.configuration.touchPointers === 1
);
if (activeTouchTool) {
logger.log(
"Setting tool %s's isTouchActive to false",
activeTouchTool.name
);
activeTouchTool.options.isTouchActive = false;
}
if (activeMultiTouchToolWithOneTouchPointer) {
logger.log(
"Setting tool %s's isTouchActive to false",
activeMultiTouchToolWithOneTouchPointer.name
);
activeMultiTouchToolWithOneTouchPointer.options.isMultiTouchActive = false;
}
}
/**
* Resolves conflicting active tools when activating a tool for MultiTouch interaction
* @private
* @function _resolveMultiTouchInputConflicts
*
* @param {Object} tool
* @param {HTMLElement} element
* @returns {undefined}
*/
function _resolveMultiTouchInputConflicts(tool, element) {
const activeMultiTouchTool = store.state.tools.find(
t =>
t.element === element &&
t.mode === 'active' &&
t.options.isMultiTouchActive === true &&
t.configuration.touchPointers === tool.configuration.touchPointers
);
let activeTouchTool;
if (tool.configuration.touchPointers === 1) {
activeTouchTool = store.state.tools.find(
t =>
t.element === element &&
t.mode === 'active' &&
t.options.isTouchActive === true
);
}
if (activeMultiTouchTool) {
logger.log(
"Setting tool %s's isMultiTouchActive to false",
activeMultiTouchTool.name
);
activeMultiTouchTool.options.isMultiTouchActive = false;
}
if (activeTouchTool) {
logger.log(
"Setting tool %s's isTouchActive to false",
activeTouchTool.name
);
activeTouchTool.options.isTouchActive = false;
}
}
/**
* If the incoming tool isTouchActive, find any conflicting tools
* and set their isTouchActive to false to avoid conflicts.
*
* @private
* @function _resolveGenericInputConflicts
*
* @param {string} interactionType
* @param {Object} tool
* @param {HTMLElement} element
* @param {(Object|number)} options
* @returns {undefined}
*/
function _resolveGenericInputConflicts(interactionType, tool, element) {
const interactionTypeFlag = `is${interactionType}Active`;
const activeToolWithActiveInteractionType = store.state.tools.find(
t =>
t.element === element &&
t.mode === 'active' &&
t.options[interactionTypeFlag] === true
);
if (activeToolWithActiveInteractionType) {
logger.log(
"Setting tool %s's %s to false",
activeToolWithActiveInteractionType.name,
interactionTypeFlag
);
activeToolWithActiveInteractionType.options[interactionTypeFlag] = false;
}
}
function _trackGlobalToolModeChange(mode, toolName, options, interactionTypes) {
if (!globalConfiguration.configuration.globalToolSyncEnabled) {
return;
}
// Update Tool History
const historyEvent = {
mode,
args: [toolName, options],
};
if (interactionTypes) {
historyEvent.push(interactionTypes);
}
store.state.globalToolChangeHistory.push(historyEvent);
const arbitraryChangeHistoryLimit = 50;
if (
store.state.globalToolChangeHistory.length > arbitraryChangeHistoryLimit
) {
store.state.globalToolChangeHistory.shift();
}
// Update ActiveBindings Array
const globalTool = store.state.globalTools[toolName];
if (!globalTool) {
logger.warn(
`setToolMode call for tool not available globally: ${toolName}`
);
return;
}
if (mode === 'active') {
let stringBindings = _determineStringBindings(
toolName,
options,
interactionTypes
);
// Remove the incoming bindings from all global tools
Object.keys(store.state.globalTools).forEach(key => {
const tool = store.state.globalTools[key];
tool.activeBindings = tool.activeBindings.filter(
binding => !stringBindings.includes(binding)
);
});
// @HACK: Clear mouse bindings
if (stringBindings.some(binding => binding.includes('Mouse-DELETE'))) {
globalTool.activeBindings = globalTool.activeBindings.filter(
binding => !binding.includes('Mouse')
);
stringBindings = stringBindings.filter(
binding => !binding.includes('Mouse')
);
}
globalTool.activeBindings = globalTool.activeBindings.concat(
stringBindings
);
} else {
globalTool.activeBindings = [];
}
}
function _determineStringBindings(toolName, options, interactionTypes) {
if (interactionTypes === undefined && Array.isArray(options)) {
interactionTypes = options;
options = null;
}
const stringBindings = [];
const globalTool = store.state.globalTools[toolName];
if (globalTool) {
// eslint-disable-next-line new-cap
const tool = new globalTool.tool(globalTool.props);
tool.supportedInteractionTypes.forEach(interactionType => {
if (
interactionTypes === undefined ||
interactionTypes.includes(interactionType)
) {
if (interactionType === 'Mouse') {
const mouseButtonMasks = _getNormalizedOptions(options)
.mouseButtonMask;
// Add or delete
if (Array.isArray(mouseButtonMasks) && mouseButtonMasks.length > 0) {
mouseButtonMasks.forEach(mask =>
stringBindings.push(`${interactionType}-${mask}`)
);
} else if (
Array.isArray(mouseButtonMasks) &&
mouseButtonMasks.length === 0
) {
stringBindings.push(`${interactionType}-DELETE`);
}
} else if (interactionType === 'MultiTouch') {
stringBindings.push(
`${interactionType}-${tool.configuration.touchPointers}`
);
} else {
stringBindings.push(interactionType);
}
}
});
}
return stringBindings;
}
const _inputResolvers = {
Mouse: _resolveMouseInputConflicts,
MouseWheel: _resolveGenericInputConflicts.bind(this, 'MouseWheel'),
Touch: _resolveTouchInputConflicts,
TouchPinch: _resolveGenericInputConflicts.bind(this, 'TouchPinch'),
TouchRotate: _resolveGenericInputConflicts.bind(this, 'TouchRotate'),
DoubleTap: _resolveGenericInputConflicts.bind(this, 'DoubleTap'),
MultiTouch: _resolveMultiTouchInputConflicts,
};
function _getNormalizedOptions(options) {
if (Array.isArray(options)) {
// If options is an array assume the array is the mouseButtonMask array
options = { mouseButtonMask: options };
} else if (options !== Object(options)) {
// And if it's something other than an object, assume options is
// a single mouseButtonMask
options = { mouseButtonMask: [options] };
}
// If there is still no 'mouseButtonMask' default it to an empty array
if (!options.hasOwnProperty('mouseButtonMask')) {
options.mouseButtonMask = [];
}
if (!Array.isArray(options.mouseButtonMask)) {
options.mouseButtonMask = [options.mouseButtonMask];
}
// Now filter out anything that is not an number or is the number 0
options.mouseButtonMask = options.mouseButtonMask.filter(
o => typeof o === 'number' && o !== 0
);
return options;
}
function _mergeMouseButtonMask(newMask, oldMask) {
// Merges and removes duplicates
return newMask.concat(oldMask).reduce((acc, m) => {
if (acc.indexOf(m) === -1) {
acc.push(m);
}
return acc;
}, []);
}
export {
setToolActive,
setToolActiveForElement,
setToolDisabled,
setToolDisabledForElement,
setToolEnabled,
setToolEnabledForElement,
setToolPassive,
setToolPassiveForElement,
// Exported for testing
setToolMode,
setToolModeForElement,
_getNormalizedOptions,
_mergeMouseButtonMask,
};