Skip to content

Commit

Permalink
Bug fixes, hook up pause.
Browse files Browse the repository at this point in the history
  • Loading branch information
DirtyHairy committed Nov 29, 2018
1 parent 8432065 commit 43dfde5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
14 changes: 10 additions & 4 deletions src/web/driver/Gamepad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,12 @@ export default class GamepadDriver {
this._mappings.delete(id);
}

private static _onBeforeSwitchRead(swtch: SwitchInterface, self: GamepadDriver) {
self._readGamepads();
poll() {
this._readGamepads();
}

self._shadows.get(swtch).sync(swtch);
private static _onBeforeSwitchRead(swtch: SwitchInterface, self: GamepadDriver) {
self.poll();
}

private probeGamepads(): void {
Expand Down Expand Up @@ -268,7 +270,11 @@ export default class GamepadDriver {
continue;
}

swtch.toggle(states.get(target));
const shadow = this._shadows.get(swtch);

shadow.toggle(states.get(target));

shadow.sync(swtch);
}

joystickIndex++;
Expand Down
2 changes: 1 addition & 1 deletion src/web/driver/gamepad/defaultMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const defaultMapping: Array<Mapping> = [
button(9, Target.start),

...[0, 1, 2, 3, 10, 11].map(i => button(i, Target.fire)),
...[4, 5, 6, 7, 16].map(i => button(i, Target.pause)),
...[4, 5, 6, 7].map(i => button(i, Target.pause)),

axis(0, Sign.negative, Target.left),
axis(0, Sign.positive, Target.right),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ import {
updateFrequency,
updateGamepadCount,
StartAction,
SetEnforceRateLimitAction
SetEnforceRateLimitAction,
resume,
userPause,
UpdateGamepadCountAction
} from '../../actions/emulation';
import { types as settingsActions } from '../../actions/settings';

Expand All @@ -48,7 +51,9 @@ import Settings from '../../model/Settings';
import * as VideoProcessorConfig from '../../../../../video/processing/config';
import CpuFactory from '../../../../../machine/cpu/Factory';
import { Target } from '../../../../driver/gamepad/Mapping';
import Switch from '../../../../../machine/io/Switch';

const POLL_GAMEPAD_INTERVALL = 100;
const isSafari = bowser.safari || bowser.ios;

class EmulationProvider implements EmulationProviderInterface {
Expand Down Expand Up @@ -111,7 +116,8 @@ class EmulationProvider implements EmulationProviderInterface {
this._driverManager.addDriver(gamepadDriver, (context: EmulationContextInterface, driver: GamepadDriver) =>
driver.bind([context.getJoystick(0), context.getJoystick(1)], {
[Target.start]: context.getControlPanel().getResetButton(),
[Target.select]: context.getControlPanel().getSelectSwitch()
[Target.select]: context.getControlPanel().getSelectSwitch(),
[Target.pause]: this._pauseSwitch
})
);

Expand All @@ -126,6 +132,22 @@ class EmulationProvider implements EmulationProviderInterface {
this._gamepadDriver.gamepadCountChanged.addHandler(gamepadCount =>
this._store.dispatch(updateGamepadCount(gamepadCount))
);

this._pauseSwitch.stateChanged.addHandler(state => {
if (!state) {
return;
}

switch (this._service.getState()) {
case EmulationServiceInterface.State.paused:
this._store.dispatch(resume());
break;

case EmulationServiceInterface.State.running:
this._store.dispatch(userPause());
break;
}
});
}

this._store.dispatch(updateGamepadCount(this._gamepadDriver.getGamepadCount()));
Expand Down Expand Up @@ -210,6 +232,23 @@ class EmulationProvider implements EmulationProviderInterface {
this._audioDriver.setMasterVolume(state.settings.volume * cartridge.volume);
}

private _onUpdateGamepadCount(a: UpdateGamepadCountAction) {
if (!this._gamepadDriver) {
return;
}

if (a.value > 0) {
if (this._pollGamepadIntervalHandle === null) {
this._pollGamepadIntervalHandle = setInterval(() => this._gamepadDriver.poll(), POLL_GAMEPAD_INTERVALL);
}
} else {
if (this._pollGamepadIntervalHandle !== null) {
clearInterval(this._pollGamepadIntervalHandle);
this._pollGamepadIntervalHandle = null;
}
}
}

private _middleware = ((api: MiddlewareAPI) => (next: (a: Action) => any) => async (a: Action): Promise<any> => {
if (!a || !this._service) {
return next(a);
Expand Down Expand Up @@ -252,6 +291,10 @@ class EmulationProvider implements EmulationProviderInterface {
this._updateVolume();
return result;

case emulationActions.updateGamepadCount:
this._onUpdateGamepadCount(a as UpdateGamepadCountAction);
return next(a);

default:
return next(a);
}
Expand All @@ -263,6 +306,9 @@ class EmulationProvider implements EmulationProviderInterface {
private _service: EmulationServiceInterface;
private _audioDriver: WebAudioDriver;
private _gamepadDriver: GamepadDriver;
private _pauseSwitch = new Switch();

private _pollGamepadIntervalHandle: any = null;
}

export { EmulationProvider as default };

0 comments on commit 43dfde5

Please sign in to comment.