Skip to content

Commit

Permalink
client: Handle state more nicely with for macro commands with tapping…
Browse files Browse the repository at this point in the history
… a button.
  • Loading branch information
Justin D. Harris committed Aug 10, 2020
1 parent be89585 commit 09fa05d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 5 deletions.
43 changes: 43 additions & 0 deletions website-client/src/components/Controller/ControllerState.ts
@@ -1,5 +1,10 @@
class ButtonState {
public isPressed = false
constructor(other?: ButtonState) {
if (other !== undefined) {
this.isPressed = other.isPressed
}
}
}

class StickState extends ButtonState {
Expand All @@ -8,6 +13,14 @@ class StickState extends ButtonState {

/** -1 is up, +1 is down */
public verticalValue = 0

constructor(other?: StickState) {
super(other)
if (other !== undefined) {
this.horizontalValue = other.horizontalValue
this.verticalValue = other.verticalValue
}
}
}

// Member names in here match actions.
Expand Down Expand Up @@ -37,6 +50,36 @@ class ControllerState {

leftStick = new StickState()
rightStick = new StickState()

constructor(other?: ControllerState) {
if (other !== undefined) {
// Deep copy
this.arrowLeft = new ButtonState(other.arrowLeft)
this.arrowRight = new ButtonState(other.arrowRight)
this.arrowUp = new ButtonState(other.arrowUp)
this.arrowDown = new ButtonState(other.arrowDown)

this.a = new ButtonState(other.a)
this.b = new ButtonState(other.b)
this.x = new ButtonState(other.x)
this.y = new ButtonState(other.y)

this.l = new ButtonState(other.l)
this.zl = new ButtonState(other.zl)

this.r = new ButtonState(other.r)
this.zr = new ButtonState(other.zr)

this.minus = new ButtonState(other.minus)
this.plus = new ButtonState(other.plus)

this.capture = new ButtonState(other.capture)
this.home = new ButtonState(other.home)

this.leftStick = new StickState(other.leftStick)
this.rightStick = new StickState(other.rightStick)
}
}
}

export { ControllerState, ButtonState }
Expand Up @@ -49,6 +49,29 @@ describe('parse-command', () => {
expect(parseCommand(`left`)).toStrictEqual([c1, c2])
})

it('tap with other command', () => {
const s1 = new ControllerState()
s1.a.isPressed = true
s1.b.isPressed = true
const s2 = new ControllerState()
s2.b.isPressed = true
expect(parseCommand(`a&b d`)).toStrictEqual([s1, s2])
})

it('tap with other commands and stick', () => {
const s1 = new ControllerState()
s1.a.isPressed = true
s1.y.isPressed = true
s1.b.isPressed = true
s1.x.isPressed = true
s1.leftStick.horizontalValue = 0.6
const s2 = new ControllerState()
s2.b.isPressed = true
s2.x.isPressed = true
s2.leftStick.horizontalValue = 0.6
expect(parseCommand(`a&y&b d&x d&s l h 0.6`)).toStrictEqual([s1, s2])
})

it('push button', () => {
let c = new ControllerState()
for (const buttonName of [
Expand Down
14 changes: 9 additions & 5 deletions website-client/src/components/Controller/parse-command.ts
Expand Up @@ -121,21 +121,25 @@ function parseCommand(command: string): ControllerState[] {
const result = []

const controllerState = new ControllerState()
let hasTap = false
const tappedButtons = []
for (let singleCommand of command.split('&')) {
singleCommand = singleCommand.trim()
if (buttonNames.has(singleCommand)) {
const member: string = buttonNameToStateMember[singleCommand] || singleCommand;
(controllerState as any)[member].isPressed = true
hasTap = true
tappedButtons.push(singleCommand)
} else {
updateState(singleCommand, controllerState, command)
}
}
result.push(controllerState)
if (hasTap) {
// TODO It would be better just to undo the button that was tapped.
result.push(new ControllerState())
if (tappedButtons.length > 0) {
// Undo the tapped buttons.
const nextState = new ControllerState(controllerState)
for (const tappedButton of tappedButtons) {
updateState(`${tappedButton} u`, nextState)
}
result.push(nextState)
}

return result
Expand Down

0 comments on commit 09fa05d

Please sign in to comment.