Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add touch support for NavigationControl's compass button + add pitch indication to icon #8226

Merged
merged 7 commits into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/ui/control/navigation_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class NavigationControl {
}

_rotateCompassArrow() {
const rotate = `rotate(${this._map.transform.angle * (180 / Math.PI)}deg)`;
const rotate = `rotateX(${this._map.transform.pitch}deg) rotateZ(${this._map.transform.angle * (180 / Math.PI)}deg)`;
this._compassArrow.style.transform = rotate;
}

Expand All @@ -80,9 +80,11 @@ class NavigationControl {
}
if (this.options.showCompass) {
this._map.on('rotate', this._rotateCompassArrow);
this._map.on('pitch', this._rotateCompassArrow);
this._rotateCompassArrow();
this._handler = new DragRotateHandler(map, {button: 'left', element: this._compass});
DOM.addEventListener(this._compass, 'mousedown', this._handler.onMouseDown);
DOM.addEventListener(this._compass, 'touchstart', this._handler.onMouseDown);
this._handler.enable();
}
return this._container;
Expand All @@ -95,7 +97,9 @@ class NavigationControl {
}
if (this.options.showCompass) {
this._map.off('rotate', this._rotateCompassArrow);
this._map.off('pitch', this._rotateCompassArrow);
DOM.removeEventListener(this._compass, 'mousedown', this._handler.onMouseDown);
DOM.removeEventListener(this._compass, 'touchstart', this._handler.onMouseDown);
this._handler.disable();
delete this._handler;
}
Expand Down
42 changes: 32 additions & 10 deletions src/ui/handler/drag_rotate.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,16 @@ class DragRotateHandler {
onMouseDown(e: MouseEvent) {
if (this._state !== 'enabled') return;

if (this._button === 'right') {
this._eventButton = DOM.mouseButton(e);
if (this._eventButton !== (e.ctrlKey ? 0 : 2)) return;
} else {
if (e.ctrlKey || DOM.mouseButton(e) !== 0) return;
this._eventButton = 0;
const touchEvent = e.type === 'touchstart';

if (!touchEvent) {
if (this._button === 'right') {
this._eventButton = DOM.mouseButton(e);
if (this._eventButton !== (e.ctrlKey ? 0 : 2)) return;
} else {
if (e.ctrlKey || DOM.mouseButton(e) !== 0) return;
this._eventButton = 0;
}
}

DOM.disableDrag();
Expand All @@ -143,23 +147,32 @@ class DragRotateHandler {
// window-level event listeners give us the best shot at capturing events that
// fall outside the map canvas element. Use `{capture: true}` for the move event
// to prevent map move events from being fired during a drag.
window.document.addEventListener('mousemove', this._onMouseMove, {capture: true});
window.document.addEventListener('mouseup', this._onMouseUp);
const moveEvent = touchEvent ? 'touchmove' : 'mousemove';
const upEvent = touchEvent ? 'touchend' : 'mouseup';

window.document.addEventListener(moveEvent, this._onMouseMove, { capture: true });
window.document.addEventListener(upEvent, this._onMouseUp);

// Deactivate when the window loses focus. Otherwise if a mouseup occurs when the window
// isn't in focus, dragging will continue even though the mouse is no longer pressed.
window.addEventListener('blur', this._onBlur);

this._state = 'pending';
this._inertia = [[browser.now(), this._map.getBearing()]];
this._startPos = this._lastPos = DOM.mousePos(this._el, e);
this._startPos = this._lastPos = touchEvent ?
DOM.touchPos(this._el, e)[0] :
DOM.mousePos(this._el, e);
this._center = this._map.transform.centerPoint; // Center of rotation

e.preventDefault();
}

_onMouseMove(e: MouseEvent) {
const pos = DOM.mousePos(this._el, e);
const touchEvent = e.type === 'touchmove';
const pos = touchEvent ?
DOM.touchPos(this._el, e)[0] :
DOM.mousePos(this._el, e);

if (this._lastPos.equals(pos)) {
return;
}
Expand Down Expand Up @@ -214,6 +227,13 @@ class DragRotateHandler {
}

_onMouseUp(e: MouseEvent) {
const touchEvent = e.type === 'touchend';

if (touchEvent) {
if (this._lastPos === this._startPos) {
this._el.click();
}
}
if (DOM.mouseButton(e) !== this._eventButton) return;
switch (this._state) {
case 'active':
Expand Down Expand Up @@ -258,6 +278,8 @@ class DragRotateHandler {
_unbind() {
window.document.removeEventListener('mousemove', this._onMouseMove, {capture: true});
window.document.removeEventListener('mouseup', this._onMouseUp);
window.document.removeEventListener('touchmove', this._onMouseMove, { capture: true });
pakastin marked this conversation as resolved.
Show resolved Hide resolved
window.document.removeEventListener('touchend', this._onMouseUp);
window.removeEventListener('blur', this._onBlur);
DOM.enableDrag();
}
Expand Down