Skip to content

Commit 0238f77

Browse files
authored
Merge pull request #57 from SpringRoll/feature/control-plugin-split
Feature/control plugin split
2 parents 6eebb5e + 49fbd39 commit 0238f77

20 files changed

+356
-307
lines changed

README.md

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,24 +284,44 @@ radio buttons so only the valid ones are displayed to users.
284284
See [the SpringRoll Application Class docs](https://github.com/SpringRoll/SpringRoll/tree/develop/src#responding-to-the-container) for more information on
285285
the request format and how game developers provide those values.
286286

287-
### ControlsPlugin
287+
### Controls
288+
There are two plugins associated with in-game controls: `ControlSensitivityPlugin` and `KeyboardMapPlugin`.
289+
The Control Sensitivity Plugin allows the user to determine the sensitivity of custom pointers in game. This plugin expects an HTML Input
290+
The Keyboard Map Plugin allows users to re-map the keys/controls used in-game to something they are more comfortable with.
291+
288292
```javascript
289-
import { ControlsPlugin, Container } from 'springroll-container';
293+
import { ControlSensitivityPlugin, KeyboardMapPlugin, Container } from 'springroll-container';
290294

291295
const container = new springroll.Container('#game', {
292296
plugins: [
293-
//ControlsPlugin also accepts an [optional] initial value for its control sensitivity
294-
new ControlsPlugin({
295-
//Expects an HTML Input Element of type="range"
296-
sensitivitySliders: '#sensitivity-slider-selector',
297+
//ControlSensitivityPlugin expects an input of type=range for it's input.
298+
new ControlSensitivityPlugin('#sensitivity-slider-selector', {
297299
defaultSensitivity: 0.5, //control sensitivity goes from 0.0 to 1.0. Default = 0.5
298-
keyContainers: '#key-container', // container element that will contain the mappable key buttons.
299300
}),
301+
//The KeyboardMapPlugin expects a div or similar container element as it's selector. It will automatically build out the
302+
//buttons to use for re-mapping controls based on what the application returns. See note below for HTML structure.
303+
new KeyboardMapPlugin('#key-container', {
304+
//you can provide a custom classname that will be attached to each key button that is generated
305+
customClassName: 'custom-button-class' //default='springrollContainerKeyBinding__button'.
306+
})
300307
]
301308
});
302309
container.openPath('game.html');
303310
```
304-
*The Key Binding functionality of the ControlsPlugin works similarly to the HUDPlugin in that it requests information from the Springroll Application. See [the SpringRoll Application Class docs](https://github.com/SpringRoll/SpringRoll/tree/v2/src#handling-state-change) for more information on the request format.
311+
*The Key Binding functionality of the `KeyboardMapPlugin` works similarly to the HUDPlugin in that it requests information from the Springroll Application. See [the SpringRoll Application Class docs](https://github.com/SpringRoll/SpringRoll/tree/v2/src#handling-state-change) for more information on the request format.
312+
313+
The HTML output within the key container will be look like the following:
314+
The className shown is the default, but can be overridden through the `customClassName` option. The IDs are generated based on the action name.
315+
316+
```html
317+
<div id="keyContainer">
318+
<label for="keyBoardMapPlugin-Up">Up</label>
319+
<button class="springrollContainerKeyBinding__button" value="Up" id="keyBoardMapPlugin-Up">w</button>
320+
<label for="keyBoardMapPlugin-Down">Down</label>
321+
<button class="springrollContainerKeyBinding__button" value="Down"id="keyBoardMapPlugin-Down">s</button>
322+
</div>
323+
```
324+
305325

306326
Keybindings are tracked visually by setting the textContent of the buttons themselves.
307327

dist/SpringRoll-Container-umd.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/SpringRoll-Container-umd.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { SliderPlugin } from '../base-plugins';
2+
3+
/**
4+
* @export
5+
* @class ControlSensitivityPlugin
6+
* @extends {SliderPlugin}
7+
*/
8+
export class ControlSensitivityPlugin extends SliderPlugin {
9+
/**
10+
*Creates an instance of ControlSensitivityPlugin.
11+
* @param {string | HTMLElement} sensitivitySliders
12+
* @param {object} options
13+
* @param {number} [options.defaultSensitivity=0.5]
14+
* @memberof ControlSensitivityPlugin
15+
*/
16+
constructor(sensitivitySliders, { defaultSensitivity = 0.5 } = {}) {
17+
super(sensitivitySliders, 'Control-Sensitivity-Plugin', {defaultValue: defaultSensitivity, featureName: ControlSensitivityPlugin.controlSensitivityKey});
18+
19+
this.sendAllProperties = this.sendAllProperties.bind(this);
20+
21+
for (let i = 0; i < this.slidersLength; i++) {
22+
this.sliders[i].enableSliderEvents(this.onControlSensitivityChange.bind(this));
23+
}
24+
}
25+
26+
/**
27+
* @memberof ControlSensitivityPlugin
28+
* @param {Event} e
29+
*/
30+
onControlSensitivityChange(e) {
31+
this.currentValue = e.target.value;
32+
this.sendProperty(ControlSensitivityPlugin.controlSensitivityKey, this.currentValue);
33+
}
34+
35+
/**
36+
* @readonly
37+
* @static
38+
* @memberof ControlSensitivityPlugin
39+
*/
40+
static get controlSensitivityKey() {
41+
return 'controlSensitivity';
42+
}
43+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { Container, ControlSensitivityPlugin } from '../index';
2+
import { Bellhop } from 'bellhop-iframe';
3+
import { makeSlider } from '../../TestingUtils';
4+
5+
const initEvent = eventName => {
6+
const event = document.createEvent('Event');
7+
event.initEvent(eventName, false, true);
8+
return event;
9+
};
10+
11+
describe('ControlSensitivityPlugin', () => {
12+
let csp;
13+
14+
before(() => {
15+
document.body.innerHTML = '';
16+
const sliderOne = makeSlider('ssOne');
17+
const sliderTwo = makeSlider('ssTwo');
18+
19+
document.body.append(sliderOne, sliderTwo);
20+
21+
csp = new ControlSensitivityPlugin('#ssOne, #ssTwo');
22+
csp.preload({ client: new Bellhop() });
23+
});
24+
25+
it('construct', () => {
26+
const iframe = document.createElement('iframe');
27+
iframe.id = 'controls-plugin-iframe';
28+
document.body.appendChild(iframe);
29+
new Container('#controls-plugin-iframe', { plugins: [csp] });
30+
csp.init();
31+
csp.client.trigger('features', {
32+
controlSensitivity: true,
33+
});
34+
35+
expect(csp.sliders[0].slider).to.be.instanceof(HTMLInputElement);
36+
expect(csp.sliders[1].slider).to.be.instanceof(HTMLInputElement);
37+
});
38+
39+
it('.onControlSensitivityChange()', () => {
40+
expect(csp.sliders[0].value).to.equal('0.5');
41+
expect(csp.sliders[1].value).to.equal('0.5');
42+
43+
csp.sliders[0].value = '1';
44+
csp.sliders[0].dispatchEvent(initEvent('change'));
45+
46+
expect(csp.sliders[0].value).to.equal('1');
47+
expect(csp.sliders[1].value).to.equal('1');
48+
expect(csp.currentValue).to.equal(1);
49+
50+
csp.sliders[0].value = 0;
51+
csp.sliders[0].dispatchEvent(initEvent('change'));
52+
53+
expect(csp.sliders[0].value).to.equal('0');
54+
expect(csp.sliders[1].value).to.equal('0');
55+
expect(csp.currentValue).to.equal(0);
56+
57+
csp.sliders[1].value = 1.1;
58+
csp.sliders[1].dispatchEvent(initEvent('change'));
59+
60+
61+
expect(csp.sliders[0].value).to.equal('1');
62+
expect(csp.sliders[1].value).to.equal('1');
63+
expect(csp.currentValue).to.equal(1);
64+
65+
csp.sliders[1].value = -1;
66+
csp.sliders[1].dispatchEvent(initEvent('change'));
67+
68+
expect(csp.sliders[0].value).to.equal('0');
69+
expect(csp.sliders[1].value).to.equal('0');
70+
expect(csp.currentValue).to.equal(0);
71+
});
72+
73+
it('should work without any controls', () => {
74+
//set up empty plugin
75+
csp = new ControlSensitivityPlugin();
76+
csp.preload({ client: new Bellhop() });
77+
csp.init();
78+
csp.client.trigger('features', {});
79+
});
80+
81+
it('should work with an HTMLElement as parameters', () => {
82+
//Plugin re-setup
83+
document.body.innerHTML = '';
84+
const sliderOne = makeSlider('ssOne');
85+
86+
document.body.appendChild(sliderOne);
87+
88+
csp = new ControlSensitivityPlugin(sliderOne);
89+
csp.preload({ client: new Bellhop() });
90+
91+
const iframe = document.createElement('iframe');
92+
iframe.id = 'controls-plugin-iframe';
93+
document.body.appendChild(iframe);
94+
new Container('#controls-plugin-iframe', {plugins: [csp] });
95+
csp.init();
96+
csp.client.trigger('features', {
97+
controlSensitivity: true,
98+
});
99+
100+
csp.sliders[0].value = 1;
101+
csp.sliders[0].dispatchEvent(initEvent('change'));
102+
103+
expect(csp.sliders[0].value).to.equal('1');
104+
expect(csp.currentValue).to.equal(1);
105+
});
106+
});

src/plugins/ControlsPlugin.spec.js

Lines changed: 0 additions & 162 deletions
This file was deleted.

0 commit comments

Comments
 (0)