Skip to content

Commit

Permalink
feat: Add controls to combine and change annotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
manthey committed May 23, 2022
1 parent f3bf949 commit 2f69f7b
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features

- Polygon operations ([#1198](../../pull/1198))
- Use meta keys to modify annotations ([#1209](../../pull/1209))

## Version 1.8.8

Expand Down
6 changes: 6 additions & 0 deletions src/annotation/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ var editHandleFeatureLevel = 3;
* @property {boolean|string[]} [showLabel=true] `true` to show the annotation
* label on annotations in done or edit states. Alternately, a list of
* states in which to show the label. Falsy to not show the label.
* @property {boolean} [allowBooleanOperations] This defaults to `true` for
* annotations that have area and `false` for those without area (e.g.,
* false for lines and points). If it is truthy, then, when the annotation
* is being created, it checks the metakeys on the first click that defines
* a coordinate to determine what boolean polygon operation should be
* performaned on the completion of the annotation.
*/

/**
Expand Down
3 changes: 2 additions & 1 deletion src/annotation/polygonAnnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ var polygonAnnotation = function (args) {
},
stroke: false,
strokeColor: {r: 0, g: 0, b: 1}
}
},
allowBooleanOperations: true
}, args || {});
args.vertices = args.vertices || args.coordinates || [];
delete args.coordinates;
Expand Down
3 changes: 2 additions & 1 deletion src/annotation/rectangleAnnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ var rectangleAnnotation = function (args, annotationName) {
fillColor: {r: 0.3, g: 0.3, b: 0.3},
fillOpacity: 0.25,
strokeColor: {r: 0, g: 0, b: 1}
}
},
allowBooleanOperations: true
}, args || {});
args.corners = args.corners || args.coordinates || [];
delete args.coordinates;
Expand Down
43 changes: 43 additions & 0 deletions src/annotationLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,27 @@ var annotationLayer = function (arg) {
m_this._updateFromEvent(update);
};

/**
* Check if the map is currently in a mode where we are adding an annotation
* with a boolean operation. If so, remove the current annotation from the
* layer, then apply it via the boolean operation.
*/
this._handleBooleanOperation = function () {
const mapNode = m_this.map().node();
let op;
Object.values(m_this._booleanClasses).forEach((c) => {
if (mapNode.hasClass(c)) {
op = c.split('-')[1];
}
});
if (!op || !m_this.currentAnnotation || !m_this.currentAnnotation.toPolygonList) {
return;
}
const newAnnot = m_this.currentAnnotation;
m_this.removeAnnotation(m_this.currentAnnotation, false);
util.polyops[op](m_this, newAnnot.toPolygonList(), {correspond: {}, keepAnnotations: 'exact', style: m_this});
};

/**
* Handle updating the current annotation based on an update state.
*
Expand All @@ -196,6 +217,7 @@ var annotationLayer = function (arg) {
m_this.mode(null);
break;
case 'done':
m_this._handleBooleanOperation();
m_this.mode(null);
break;
}
Expand All @@ -213,6 +235,15 @@ var annotationLayer = function (arg) {
*/
this._handleMouseMove = function (evt) {
if (m_this.mode() && m_this.currentAnnotation) {
if (m_this.mode() !== m_this.modes.edit && m_this.currentAnnotation.options('allowBooleanOperations') && m_this.currentAnnotation._coordinates().length < 2) {
if (evt.modifiers) {
const mod = (evt.modifiers.shift ? 's' : '') + (evt.modifiers.ctrl ? 'c' : '') + (evt.modifiers.meta || evt.modifiers.alt ? 'a' : '');
const mapNode = m_this.map().node();
Object.values(m_this._booleanClasses).forEach((c) => {
mapNode.toggleClass(c, m_this._booleanClasses[mod] === c);
});
}
}
var update = m_this.currentAnnotation.mouseMove(evt);
if (update) {
m_this.modified();
Expand Down Expand Up @@ -518,6 +549,15 @@ var annotationLayer = function (arg) {
edit: 'edit'
};

/* Keys are short-hand for preferred event modifiers. Values are classes to
* apply to the map node. */
this._booleanClasses = {
s: 'annotation-union',
sc: 'annotation-intersect',
c: 'annotation-difference',
sa: 'annotation-xor'
};

/**
* Get or set the current mode.
*
Expand All @@ -542,6 +582,9 @@ var annotationLayer = function (arg) {
mapNode = m_this.map().node(), oldMode = m_mode;
m_mode = arg;
mapNode.toggleClass('annotation-input', !!(m_mode && m_mode !== m_this.modes.edit));
if (!m_mode || m_mode === m_this.modes.edit) {
Object.values(m_this._booleanClasses).forEach((c) => mapNode.toggleClass(c, false));
}
if (!m_keyHandler) {
m_keyHandler = Mousetrap(mapNode[0]);
}
Expand Down
4 changes: 4 additions & 0 deletions src/css/cursor-crosshair-difference.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/css/cursor-crosshair-intersect.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/css/cursor-crosshair-union.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/css/cursor-crosshair-xor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions src/main.styl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@

&.annotation-input
cursor crosshair
&.annotation-intersect
cursor embedurl("./css/cursor-crosshair-intersect.svg") 12 12,crosshair
&.annotation-difference
cursor embedurl("./css/cursor-crosshair-difference.svg") 12 12,crosshair
&.annotation-union
cursor embedurl("./css/cursor-crosshair-union.svg") 12 12,crosshair
&.annotation-xor
cursor embedurl("./css/cursor-crosshair-xor.svg") 12 12,crosshair

&.highlight-focus
&:after
Expand Down

0 comments on commit 2f69f7b

Please sign in to comment.