Abstract out key conditions for interactions, fixes #51, see also #54

This commit is contained in:
Tom Payne
2012-10-08 12:55:20 +02:00
parent a14a15159f
commit 8df0cfb9d9
6 changed files with 119 additions and 25 deletions

View File

@@ -0,0 +1,56 @@
goog.provide('ol.interaction.ConditionType');
goog.provide('ol.interaction.condition');
/**
* @typedef {function(goog.events.BrowserEvent): boolean}
*/
ol.interaction.ConditionType;
/**
* @param {goog.events.BrowserEvent} browserEvent Browser event.
* @return {boolean} True if only the alt key is pressed.
*/
ol.interaction.condition.altKeyOnly = function(browserEvent) {
return (
browserEvent.altKey &&
!browserEvent.platformModifierKey &&
!browserEvent.shiftKey);
};
/**
* @param {goog.events.BrowserEvent} browserEvent Browser event.
* @return {boolean} True if only the no modifier keys are pressed.
*/
ol.interaction.condition.noModifierKeys = function(browserEvent) {
return (
!browserEvent.altKey &&
!browserEvent.platformModifierKey &&
!browserEvent.shiftKey);
};
/**
* @param {goog.events.BrowserEvent} browserEvent Browser event.
* @return {boolean} True if only the platform modifier key is pressed.
*/
ol.interaction.condition.platformModifierKeyOnly = function(browserEvent) {
return (
!browserEvent.altKey &&
browserEvent.platformModifierKey &&
!browserEvent.shiftKey);
};
/**
* @param {goog.events.BrowserEvent} browserEvent Browser event.
* @return {boolean} True if only the shift key is pressed.
*/
ol.interaction.condition.shiftKeyOnly = function(browserEvent) {
return (
!browserEvent.altKey &&
!browserEvent.platformModifierKey &&
browserEvent.shiftKey);
};

View File

@@ -2,6 +2,7 @@ goog.provide('ol.interaction.DragPan');
goog.require('ol.Coordinate');
goog.require('ol.MapBrowserEvent');
goog.require('ol.interaction.ConditionType');
goog.require('ol.interaction.Drag');
@@ -9,9 +10,18 @@ goog.require('ol.interaction.Drag');
/**
* @constructor
* @extends {ol.interaction.Drag}
* @param {ol.interaction.ConditionType} condition Condition.
*/
ol.interaction.DragPan = function() {
ol.interaction.DragPan = function(condition) {
goog.base(this);
/**
* @private
* @type {ol.interaction.ConditionType}
*/
this.condition_ = condition;
};
goog.inherits(ol.interaction.DragPan, ol.interaction.Drag);
@@ -39,7 +49,7 @@ ol.interaction.DragPan.prototype.handleDrag = function(mapBrowserEvent) {
*/
ol.interaction.DragPan.prototype.handleDragStart = function(mapBrowserEvent) {
var browserEvent = mapBrowserEvent.browserEvent;
if (!browserEvent.shiftKey) {
if (this.condition_(browserEvent)) {
return true;
} else {
return false;

View File

@@ -1,6 +1,7 @@
goog.provide('ol.interaction.AltDragRotate');
goog.provide('ol.interaction.DragRotate');
goog.require('ol.MapBrowserEvent');
goog.require('ol.interaction.ConditionType');
goog.require('ol.interaction.Drag');
@@ -8,11 +9,18 @@ goog.require('ol.interaction.Drag');
/**
* @constructor
* @extends {ol.interaction.Drag}
* @param {ol.interaction.ConditionType} condition Condition.
*/
ol.interaction.AltDragRotate = function() {
ol.interaction.DragRotate = function(condition) {
goog.base(this);
/**
* @private
* @type {ol.interaction.ConditionType}
*/
this.condition_ = condition;
/**
* @private
* @type {number}
@@ -20,13 +28,13 @@ ol.interaction.AltDragRotate = function() {
this.startRotation_ = 0;
};
goog.inherits(ol.interaction.AltDragRotate, ol.interaction.Drag);
goog.inherits(ol.interaction.DragRotate, ol.interaction.Drag);
/**
* @inheritDoc
*/
ol.interaction.AltDragRotate.prototype.handleDrag = function(mapBrowserEvent) {
ol.interaction.DragRotate.prototype.handleDrag = function(mapBrowserEvent) {
var browserEvent = mapBrowserEvent.browserEvent;
var map = mapBrowserEvent.map;
var size = map.getSize();
@@ -41,11 +49,11 @@ ol.interaction.AltDragRotate.prototype.handleDrag = function(mapBrowserEvent) {
/**
* @inheritDoc
*/
ol.interaction.AltDragRotate.prototype.handleDragStart =
ol.interaction.DragRotate.prototype.handleDragStart =
function(mapBrowserEvent) {
var browserEvent = mapBrowserEvent.browserEvent;
var map = mapBrowserEvent.map;
if (browserEvent.isMouseActionButton() && browserEvent.altKey &&
if (browserEvent.isMouseActionButton() && this.condition_(browserEvent) &&
map.canRotate()) {
var size = map.getSize();
var offset = mapBrowserEvent.getPixel();

View File

@@ -1,7 +1,8 @@
goog.provide('ol.interaction.ShiftDragRotateAndZoom');
goog.provide('ol.interaction.DragRotateAndZoom');
goog.require('goog.math.Vec2');
goog.require('ol.MapBrowserEvent');
goog.require('ol.interaction.ConditionType');
goog.require('ol.interaction.Drag');
@@ -9,11 +10,18 @@ goog.require('ol.interaction.Drag');
/**
* @constructor
* @extends {ol.interaction.Drag}
* @param {ol.interaction.ConditionType} condition Condition.
*/
ol.interaction.ShiftDragRotateAndZoom = function() {
ol.interaction.DragRotateAndZoom = function(condition) {
goog.base(this);
/**
* @private
* @type {ol.interaction.ConditionType}
*/
this.condition_ = condition;
/**
* @private
* @type {number}
@@ -27,13 +35,13 @@ ol.interaction.ShiftDragRotateAndZoom = function() {
this.startRotation_ = 0;
};
goog.inherits(ol.interaction.ShiftDragRotateAndZoom, ol.interaction.Drag);
goog.inherits(ol.interaction.DragRotateAndZoom, ol.interaction.Drag);
/**
* @inheritDoc
*/
ol.interaction.ShiftDragRotateAndZoom.prototype.handleDrag =
ol.interaction.DragRotateAndZoom.prototype.handleDrag =
function(mapBrowserEvent) {
var browserEvent = mapBrowserEvent.browserEvent;
var map = mapBrowserEvent.map;
@@ -52,11 +60,11 @@ ol.interaction.ShiftDragRotateAndZoom.prototype.handleDrag =
/**
* @inheritDoc
*/
ol.interaction.ShiftDragRotateAndZoom.prototype.handleDragStart =
ol.interaction.DragRotateAndZoom.prototype.handleDragStart =
function(mapBrowserEvent) {
var browserEvent = mapBrowserEvent.browserEvent;
var map = mapBrowserEvent.map;
if (map.canRotate() && browserEvent.shiftKey) {
if (map.canRotate() && this.condition_(browserEvent)) {
var resolution = map.getResolution();
var size = map.getSize();
var delta = new goog.math.Vec2(

View File

@@ -1,10 +1,11 @@
// FIXME draw drag box
goog.provide('ol.interaction.ShiftDragZoom');
goog.provide('ol.interaction.DragZoom');
goog.require('ol.Extent');
goog.require('ol.MapBrowserEvent');
goog.require('ol.control.DragBox');
goog.require('ol.interaction.ConditionType');
goog.require('ol.interaction.Drag');
@@ -26,11 +27,18 @@ ol.SHIFT_DRAG_ZOOM_HYSTERESIS_PIXELS_SQUARED =
/**
* @constructor
* @extends {ol.interaction.Drag}
* @param {ol.interaction.ConditionType} condition Condition.
*/
ol.interaction.ShiftDragZoom = function() {
ol.interaction.DragZoom = function(condition) {
goog.base(this);
/**
* @private
* @type {ol.interaction.ConditionType}
*/
this.condition_ = condition;
/**
* @type {ol.control.DragBox}
* @private
@@ -39,13 +47,13 @@ ol.interaction.ShiftDragZoom = function() {
};
goog.inherits(ol.interaction.ShiftDragZoom, ol.interaction.Drag);
goog.inherits(ol.interaction.DragZoom, ol.interaction.Drag);
/**
* @inheritDoc
*/
ol.interaction.ShiftDragZoom.prototype.handleDragEnd =
ol.interaction.DragZoom.prototype.handleDragEnd =
function(mapBrowserEvent) {
this.dragBox_.setMap(null);
this.dragBox_ = null;
@@ -63,10 +71,10 @@ ol.interaction.ShiftDragZoom.prototype.handleDragEnd =
/**
* @inheritDoc
*/
ol.interaction.ShiftDragZoom.prototype.handleDragStart =
ol.interaction.DragZoom.prototype.handleDragStart =
function(mapBrowserEvent) {
var browserEvent = mapBrowserEvent.browserEvent;
if (browserEvent.isMouseActionButton() && browserEvent.shiftKey) {
if (browserEvent.isMouseActionButton() && this.condition_(browserEvent)) {
this.dragBox_ = new ol.control.DragBox({
map: mapBrowserEvent.map,
startCoordinate: this.startCoordinate

View File

@@ -41,14 +41,15 @@ goog.require('ol.Size');
goog.require('ol.TransformFunction');
goog.require('ol.control.Attribution');
goog.require('ol.control.Zoom');
goog.require('ol.interaction.AltDragRotate');
goog.require('ol.interaction.DblClickZoom');
goog.require('ol.interaction.DragPan');
goog.require('ol.interaction.DragRotate');
goog.require('ol.interaction.DragZoom');
goog.require('ol.interaction.Interaction');
goog.require('ol.interaction.KeyboardPan');
goog.require('ol.interaction.KeyboardZoom');
goog.require('ol.interaction.MouseWheelZoom');
goog.require('ol.interaction.ShiftDragZoom');
goog.require('ol.interaction.condition');
goog.require('ol.renderer.Layer');
goog.require('ol.renderer.Map');
goog.require('ol.renderer.dom');
@@ -1129,7 +1130,8 @@ ol.Map.createInteractions_ = function(mapOptions) {
var rotate = goog.isDef(mapOptions.rotate) ?
mapOptions.rotate : true;
if (rotate) {
interactions.push(new ol.interaction.AltDragRotate());
interactions.push(
new ol.interaction.DragRotate(ol.interaction.condition.altKeyOnly));
}
var doubleClickZoom = goog.isDef(mapOptions.doubleClickZoom) ?
@@ -1143,7 +1145,8 @@ ol.Map.createInteractions_ = function(mapOptions) {
var dragPan = goog.isDef(mapOptions.dragPan) ?
mapOptions.dragPan : true;
if (dragPan) {
interactions.push(new ol.interaction.DragPan());
interactions.push(
new ol.interaction.DragPan(ol.interaction.condition.noModifierKeys));
}
var keyboard = goog.isDef(mapOptions.keyboard) ?
@@ -1167,7 +1170,8 @@ ol.Map.createInteractions_ = function(mapOptions) {
var shiftDragZoom = goog.isDef(mapOptions.shiftDragZoom) ?
mapOptions.shiftDragZoom : true;
if (shiftDragZoom) {
interactions.push(new ol.interaction.ShiftDragZoom());
interactions.push(
new ol.interaction.DragZoom(ol.interaction.condition.shiftKeyOnly));
}
return interactions;