Refactor mouse position control to use postrender event

This commit is contained in:
Tom Payne
2013-01-15 20:47:25 +01:00
parent 5d43aa22d4
commit 11ec1de89b
3 changed files with 51 additions and 86 deletions

View File

@@ -43,7 +43,7 @@ domMap.getControls().push(new ol.control.MousePosition({
coordinateFormat: ol.Coordinate.toStringHDMS,
projection: ol.Projection.getFromCode('EPSG:4326'),
target: document.getElementById('domMousePosition'),
undefinedHtml: ' '
undefinedHTML: ' '
}));
var webglMap = new ol.Map({
@@ -59,7 +59,7 @@ webglMap.getControls().push(new ol.control.MousePosition({
coordinateFormat: ol.Coordinate.toStringHDMS,
projection: ol.Projection.getFromCode('EPSG:4326'),
target: document.getElementById('webglMousePosition'),
undefinedHtml: ' '
undefinedHTML: ' '
}));
var keyboardInteraction = new ol.interaction.Keyboard();

View File

@@ -7,7 +7,7 @@
@exportObjectLiteralProperty ol.control.MousePositionOptions.map ol.Map|undefined
@exportObjectLiteralProperty ol.control.MousePositionOptions.projection ol.Projection|undefined
@exportObjectLiteralProperty ol.control.MousePositionOptions.target Element|undefined
@exportObjectLiteralProperty ol.control.MousePositionOptions.undefinedHtml string|undefined
@exportObjectLiteralProperty ol.control.MousePositionOptions.undefinedHTML string|undefined
@exportObjectLiteral ol.control.ZoomOptions
@exportObjectLiteralProperty ol.control.ZoomOptions.delta number|undefined

View File

@@ -3,6 +3,7 @@
goog.provide('ol.control.MousePosition');
goog.require('goog.dom');
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('goog.style');
@@ -48,52 +49,51 @@ ol.control.MousePosition = function(mousePositionOptions) {
* @private
* @type {string}
*/
this.undefinedHtml_ = goog.isDef(mousePositionOptions.undefinedHtml) ?
mousePositionOptions.undefinedHtml : '';
this.undefinedHTML_ = goog.isDef(mousePositionOptions.undefinedHTML) ?
mousePositionOptions.undefinedHTML : '';
/**
* @private
* @type {ol.TransformFunction}
* @type {ol.TransformFunction|undefined}
*/
this.transform_ = ol.Projection.identityTransform;
this.transform_ = undefined;
/**
* @private
* @type {Array.<number>}
* @type {ol.Projection}
*/
this.mapListenerKeys_ = null;
this.transformProjection_ = null;
/**
* @private
* @type {Array.<number>}
* @type {Array.<?number>}
*/
this.viewListenerKeys_ = null;
this.listenerKeys_ = null;
this.handleViewProjectionChanged_();
};
goog.inherits(ol.control.MousePosition, ol.control.Control);
/**
* @private
* @param {ol.MapEvent} mapEvent Map event.
* @protected
*/
ol.control.MousePosition.prototype.handleMapViewChanged_ = function() {
var map = this.getMap();
goog.asserts.assert(!goog.isNull(map));
if (!goog.isNull(this.viewListenerKeys_)) {
goog.array.forEach(this.viewListenerKeys_, goog.events.unlistenByKey);
this.viewListenerKeys_ = null;
ol.control.MousePosition.prototype.handleMapPostrender = function(mapEvent) {
var frameState = mapEvent.frameState;
if (goog.isNull(frameState)) {
this.transform_ = undefined;
this.transformProjection_ = null;
} else {
var projection = frameState.view2DState.projection;
if (projection != this.transformProjection_) {
if (goog.isDef(this.projection_)) {
this.transform_ = ol.Projection.getTransform(
projection, this.projection_);
} else {
this.transform_ = ol.Projection.identityTransform;
}
this.transformProjection_ = projection;
}
var view = map.getView();
if (goog.isDefAndNotNull(view)) {
// FIXME works for View2D only
goog.asserts.assert(view instanceof ol.View2D);
this.viewListenerKeys_ = [
goog.events.listen(
view, ol.Object.getChangedEventType(ol.View2DProperty.ROTATION),
this.handleViewProjectionChanged_, false, this)
];
this.handleViewProjectionChanged_();
}
};
@@ -103,12 +103,13 @@ ol.control.MousePosition.prototype.handleMapViewChanged_ = function() {
* @protected
*/
ol.control.MousePosition.prototype.handleMouseMove = function(browserEvent) {
var html = this.undefinedHTML_;
if (goog.isDef(this.transform_)) {
var map = this.getMap();
var eventPosition = goog.style.getRelativePosition(
browserEvent, map.getViewport());
var pixel = new ol.Pixel(eventPosition.x, eventPosition.y);
var coordinate = map.getCoordinateFromPixel(pixel);
var html;
if (!goog.isNull(coordinate)) {
coordinate = this.transform_(coordinate);
if (goog.isDef(this.coordinateFormat_)) {
@@ -116,8 +117,7 @@ ol.control.MousePosition.prototype.handleMouseMove = function(browserEvent) {
} else {
html = coordinate.toString();
}
} else {
html = this.undefinedHtml_;
}
}
this.element.innerHTML = html;
};
@@ -128,18 +128,7 @@ ol.control.MousePosition.prototype.handleMouseMove = function(browserEvent) {
* @protected
*/
ol.control.MousePosition.prototype.handleMouseOut = function(browserEvent) {
this.element.innerHTML = this.undefinedHtml_;
};
/**
* @private
*/
ol.control.MousePosition.prototype.handleViewProjectionChanged_ = function() {
this.updateTransform_();
// FIXME should we instead re-calculate using the last known
// mouse position?
this.element.innerHTML = this.undefinedHtml_;
this.element.innerHTML = this.undefinedHTML_;
};
@@ -147,44 +136,20 @@ ol.control.MousePosition.prototype.handleViewProjectionChanged_ = function() {
* @inheritDoc
*/
ol.control.MousePosition.prototype.setMap = function(map) {
if (!goog.isNull(this.mapListenerKeys_)) {
goog.array.forEach(this.mapListenerKeys_, goog.events.unlistenByKey);
this.mapListenerKeys_ = null;
if (!goog.isNull(this.listenerKeys_)) {
goog.array.forEach(this.listenerKeys_, goog.events.unlistenByKey);
this.listenerKeys_ = null;
}
goog.base(this, 'setMap', map);
if (!goog.isNull(map)) {
var viewport = map.getViewport();
this.listenerKeys = [
this.listenerKeys_ = [
goog.events.listen(viewport, goog.events.EventType.MOUSEMOVE,
this.handleMouseMove, false, this),
goog.events.listen(viewport, goog.events.EventType.MOUSEOUT,
this.handleMouseOut, false, this),
goog.events.listen(map,
ol.Object.getChangedEventType(ol.MapProperty.VIEW),
this.handleMapViewChanged_, false, this)
goog.events.listen(map, ol.MapEventType.POSTRENDER,
this.handleMapPostrender, false, this)
];
this.updateTransform_();
}
};
/**
* @private
*/
ol.control.MousePosition.prototype.updateTransform_ = function() {
var map, view;
if (!goog.isNull(map = this.getMap()) &&
!goog.isNull(view = map.getView())) {
// FIXME works for View2D only
goog.asserts.assert(view instanceof ol.View2D);
var viewProjection = view.getProjection();
if (!goog.isDef(viewProjection) || !goog.isDef(this.projection_)) {
this.transform_ = ol.Projection.identityTransform;
} else {
this.transform_ =
ol.Projection.getTransform(viewProjection, this.projection_);
}
} else {
this.transform_ = ol.Projection.identityTransform;
}
};