Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

WIP: Feat/keyboard navigation #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions src/constants/keyboard-mapping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const keyboardMappings = {
ArrowLeft: 'navigateLeft',
ArrowRight: 'navigateRight',
ArrowUp: 'navigateUp',
ArrowDown: 'navigateDown',
BracketRight: 'zoomIn',
Slash: 'zoomOut',
KeyP: 'preview',
Enter: 'print', // Because Macbooks dont have a Print Key omglol
PrintScreen: 'print'
};

export default keyboardMappings;
73 changes: 73 additions & 0 deletions src/lib/custom-controls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import keyboardMappings from '../constants/keyboard-mapping.js';

export default class Controller {
constructor(map) {
this.map = map;
this.step = 100;

const joystickIsConnected = false;

if (joystickIsConnected) {
this.initGamepadControls();
} else {
this.initKeyboardControls();
}
}

initGamepadControls() {
console.log('init gamepad controls');
}

initKeyboardControls() {
const label = document.querySelector('#label');

window.addEventListener('keyup', event => {
if (keyboardMappings[event.code]) {
if (document.activeElement !== label) {
const action = keyboardMappings[event.code];
this[action]();
}
}
});
}

navigateLeft() {
this.map.panBy([-this.step, 0]);
}

navigateRight() {
this.map.panBy([this.step, 0]);
}

navigateUp() {
this.map.panBy([0, -this.step]);
}

navigateDown() {
this.map.panBy([0, this.step]);
}

zoomIn() {
this.map.zoomIn();
}

zoomOut() {
this.map.zoomOut();
}

preview() {
const button = document.querySelector(
'#action-buttons > button.preview-button'
);

button.click();
}

print() {
const button = document.querySelector(
'#action-buttons > button.print-button'
);

button.click();
}
}
5 changes: 4 additions & 1 deletion src/preview.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import plotMap from './lib/plot-map';
import CustomControls from './lib/custom-controls';

mapboxgl.accessToken =
'pk.eyJ1IjoidWJpbGFicyIsImEiOiJ4Tm02bDJrIn0.aA51umnsZbzugtBiFLZPoQ';
Expand All @@ -10,6 +11,8 @@ const map = new mapboxgl.Map({
zoom: 11
});

new CustomControls(map);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create a new HTML + JS file for this.


map.dragRotate.disable();

map.addControl(
Expand All @@ -19,7 +22,7 @@ map.addControl(
);

const label = document.getElementById('label');
let labelText = "HAMBURG";
let labelText = 'HAMBURG';

label.addEventListener('input', event => {
labelText = label.innerText.toUpperCase();
Expand Down