Skip to content

Commit

Permalink
Improve keyboard shortcut support
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyuchia committed Dec 11, 2018
1 parent 7c1ad42 commit 692bc00
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 109 deletions.
213 changes: 107 additions & 106 deletions core/lib/monogatari.js
@@ -1,5 +1,6 @@
import { $_, Space, SpaceAdapter, Platform, Preload, Util, FileSystem, Text } from '@aegis-framework/artemis';
import moment from 'moment';
import mousetrap from 'mousetrap';
import { FancyError } from './FancyError';


Expand Down Expand Up @@ -1215,6 +1216,15 @@ class Monogatari {
}
}

static keyboardShortcut (shortcut, callback) {
mousetrap.bind (shortcut, (event) => {
if (event.target.tagName.toLowerCase () != 'input') {
event.preventDefault ();
callback.call (null, event);
}
});
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//

Expand Down Expand Up @@ -1597,6 +1607,51 @@ class Monogatari {
}
}

static runAction (action, element) {
switch (action) {

// The open-menu action does exactly what it says, it takes the
// data-menu property of the object it's in and then opens that
// menu, meaning it hides everything else and shows that one.
case 'open-menu':
$_(`${Monogatari.selector} section`).hide();

if (element.data('open') == 'save') {
$_(`${Monogatari.selector} [data-menu="save"] [data-input="slotName"]`).value (moment ().format ('MMMM Do YYYY, h:mm:ss a'));
}
$_(`${Monogatari.selector} [data-menu="${element.data('open')}"]`).show();
break;

// The start action starts the game so it shows the game screen
// and the game starts
case 'start':
Monogatari.stopAmbient();
Monogatari.global ('playing', true);

Monogatari.onStart ().then (() => {
$_(`${Monogatari.selector} section`).hide();
$_('[data-ui="quick-menu"]').show ();
$_(`${Monogatari.selector} #game`).show();
Monogatari.run (Monogatari.label ()[Monogatari.state ('step')]);
});
break;

// The close action removes the active class from the element it
// points to.
case 'close':
$_(`${Monogatari.selector} [data-ui="${element.data('close')}"]`).removeClass('active');
break;

case 'dismiss-notice':
$_(`${Monogatari.selector} [data-notice]`).removeClass('modal--active');
break;

case 'distraction-free':
Monogatari.distractionFree ();
break;
}
}

/**
* Every event listener should be binded in this function.
*/
Expand Down Expand Up @@ -1635,49 +1690,7 @@ class Monogatari {
// Add listeners for the data-action properties
$_(`${selector} [data-action], [data-action] *`).click (function (event) {
event.stopPropagation ();

switch ($_(this).data('action')) {

// The open-menu action does exactly what it says, it takes the
// data-menu property of the object it's in and then opens that
// menu, meaning it hides everything else and shows that one.
case 'open-menu':
$_(`${selector} section`).hide();

if ($_(this).data('open') == 'save') {
$_(`${selector} [data-menu="save"] [data-input="slotName"]`).value (moment ().format ('MMMM Do YYYY, h:mm:ss a'));
}
$_(`${selector} [data-menu="${$_(this).data('open')}"]`).show();
break;

// The start action starts the game so it shows the game screen
// and the game starts
case 'start':
Monogatari.stopAmbient();
Monogatari.global ('playing', true);

Monogatari.onStart ().then (() => {
$_(`${selector} section`).hide();
$_('[data-ui="quick-menu"]').show ();
$_(`${selector} #game`).show();
Monogatari.run (Monogatari.label ()[Monogatari.state ('step')]);
});
break;

// The close action removes the active class from the element it
// points to.
case 'close':
$_(`${selector} [data-ui="${$_(this).data('close')}"]`).removeClass('active');
break;

case 'dismiss-notice':
$_(`${selector} [data-notice]`).removeClass('modal--active');
break;

case 'distraction-free':
Monogatari.distractionFree ();
break;
}
Monogatari.runAction ($_(this).data ('action'), $_(this));
return false;
});

Expand All @@ -1687,81 +1700,69 @@ class Monogatari {
Monogatari.autoPlay (Monogatari.global ('_AutoPlayTimer') === null);
});

// Listen for KeyDown events
$_(document).keydown (function (e) {
Monogatari.keyboardShortcut (['right', 'space'], () => {
Monogatari.canProceed ().then (() => {
Monogatari.next ();
}).catch (() => {
// An action waiting for user interaction or something else
// is blocking the game.
});
});

// If the player is typing into the input box, we shouldn't do
// any weird things with their keys so we check for that first.
if (e.target.tagName.toLowerCase() !== 'input') {
switch (e.which) {
Monogatari.keyboardShortcut ('esc', () => {
if ($_(`${selector} #game`).isVisible () && Monogatari.global ('playing')) {
$_(`${selector} #game`).hide ();
$_(`${selector} [data-menu="settings"]`).show();
} else if ($_(`${selector} [data-menu="settings"]`).isVisible () && Monogatari.global ('playing')) {
$_(`${selector} [data-menu="settings"]`).hide ();
$_(`${selector} #game`).show ();
}
});

// The S key activates skipping mode
case 83:
if (Monogatari.global ('skip') !== null) {
Monogatari.skip (false);
} else {
Monogatari.skip (true);
}
break;
Monogatari.keyboardShortcut ('left', () => {
Monogatari.revert ().catch (() => {
// The game could not be reverted, either because an
// action prevented it or because there are no statements
// left to revert to.
});
});

default:
return;
Monogatari.keyboardShortcut ('h', () => {
if (Monogatari.global ('playing')) {
Monogatari.distractionFree ();
}
});

Monogatari.keyboardShortcut ('s', () => {
if (Monogatari.global ('playing')) {
if (Monogatari.global ('skip') !== null) {
Monogatari.skip (false);
} else {
Monogatari.skip (true);
}
}
});

Monogatari.keyboardShortcut ('shift+s', () => {
if (Monogatari.global ('playing')) {
$_(`${Monogatari.selector} section`).hide();

$_(document).keyup ((event) => {
if (event.target.tagName.toLowerCase () != 'input') {
switch (event.which) {

// Escape Key
case 27:
if ($_(`${selector} #game`).isVisible () && Monogatari.global ('playing')) {
$_(`${selector} #game`).hide ();
$_(`${selector} [data-menu="settings"]`).show();
} else if ($_(`${selector} [data-menu="settings"]`).isVisible () && Monogatari.global ('playing')) {
$_(`${selector} [data-menu="settings"]`).hide ();
$_(`${selector} #game`).show ();
}
break;

// Spacebar and Right Arrow
case 32:
case 39:
Monogatari.canProceed ().then (() => {
Monogatari.next ();
}).catch (() => {
// An action waiting for user interaction or something else
// is blocking the game.
});
break;

// Left Arrow
case 37:
Monogatari.revert ().catch (() => {
// The game could not be reverted, either because an
// action prevented it or because there are no statements
// left to revert to.
});
break;

// H Key
case 72:
event.stopPropagation();
if (Monogatari.global ('playing')) {
Monogatari.distractionFree ();
}
break;
$_(`${Monogatari.selector} [data-menu="save"] [data-input="slotName"]`).value (moment ().format ('MMMM Do YYYY, h:mm:ss a'));
$_(`${Monogatari.selector} [data-menu="save"]`).show();
}
});

// Exit this handler for other keys to run normally
default:
return;
}
Monogatari.keyboardShortcut ('shift+l', () => {
if (Monogatari.global ('playing')) {
$_(`${Monogatari.selector} section`).hide();
$_(`${Monogatari.selector} [data-menu="load"]`).show();
}
});

event.preventDefault();
Monogatari.keyboardShortcut ('shift+q', () => {
if (Monogatari.global ('playing')) {
$_(`${Monogatari.selector} [data-notice="exit"]`).addClass ('modal--active');
}
});

const promises = [];
Expand Down
7 changes: 5 additions & 2 deletions dist/engine/monogatari.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/engine/monogatari.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -43,6 +43,7 @@
"animate.css": "^3.6.1",
"electron": "^3.0.10",
"moment": "^2.22.2",
"mousetrap": "^1.6.2",
"particles.js": "^2.0.0",
"typed.js": "^2.0.8"
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -4538,6 +4538,11 @@ moment@^2.22.2:
resolved "https://registry.yarnpkg.com/moment/-/moment-2.22.2.tgz#3c257f9839fc0e93ff53149632239eb90783ff66"
integrity sha1-PCV/mDn8DpP/UxSWMiOeuQeD/2Y=

mousetrap@^1.6.2:
version "1.6.2"
resolved "https://registry.yarnpkg.com/mousetrap/-/mousetrap-1.6.2.tgz#caadd9cf886db0986fb2fee59a82f6bd37527587"
integrity sha512-jDjhi7wlHwdO6q6DS7YRmSHcuI+RVxadBkLt3KHrhd3C2b+w5pKefg3oj5beTcHZyVFA9Aksf+yEE1y5jxUjVA==

ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
Expand Down

0 comments on commit 692bc00

Please sign in to comment.