-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
client: Support recording + running macros (#21)
Resolves #10
- Loading branch information
Showing
5 changed files
with
152 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { ControllerState } from "../Controller/ControllerState" | ||
|
||
export default class MacroRecorder { | ||
isRecording = false | ||
lastCommandTime = Date.now() | ||
currentRecording: { command: string, controllerState: any }[] = [] | ||
|
||
start(): void { | ||
this.currentRecording = [] | ||
this.lastCommandTime = Date.now() | ||
this.isRecording = true | ||
} | ||
|
||
stop(): void { | ||
this.isRecording = false | ||
console.debug("Recorded macro:") | ||
console.debug(this.currentRecording) | ||
} | ||
|
||
add(command: string, controllerState: ControllerState): void { | ||
if (this.isRecording) { | ||
const commandTime = Date.now() | ||
this.currentRecording.push({ | ||
command: `wait ${commandTime - this.lastCommandTime}`, | ||
controllerState | ||
}) | ||
this.currentRecording.push({ command, controllerState }) | ||
this.lastCommandTime = commandTime | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { createStyles, withStyles } from '@material-ui/core' | ||
import Button from '@material-ui/core/Button' | ||
import Grid from '@material-ui/core/Grid' | ||
import Tooltip from '@material-ui/core/Tooltip' | ||
import Typography from '@material-ui/core/Typography' | ||
import React from 'react' | ||
import { SendCommand } from '../../key-binding/KeyBinding' | ||
import MacroRecorder from './MacroRecorder' | ||
|
||
const styles = () => createStyles({ | ||
}) | ||
|
||
class Macros extends React.Component<{ | ||
macroRecorder: MacroRecorder, | ||
sendCommand: SendCommand, | ||
}, any> { | ||
constructor(props: any) { | ||
super(props) | ||
|
||
this.state = { | ||
isRecording: false, | ||
macroExists: false, | ||
} | ||
|
||
this.playLastRecordedMacro = this.playLastRecordedMacro.bind(this) | ||
this.startRecording = this.startRecording.bind(this) | ||
this.stopRecording = this.stopRecording.bind(this) | ||
} | ||
|
||
startRecording(): void { | ||
this.setState({ isRecording: true }) | ||
this.props.macroRecorder.start() | ||
} | ||
|
||
stopRecording(): void { | ||
this.props.macroRecorder.stop() | ||
this.setState({ isRecording: false, macroExists: true, }) | ||
} | ||
|
||
async sleep(sleepMillis: number) { | ||
return new Promise(resolve => setTimeout(resolve, sleepMillis)) | ||
} | ||
|
||
async playLastRecordedMacro(): Promise<void> { | ||
for (const c of this.props.macroRecorder.currentRecording) { | ||
const { command, controllerState } = c | ||
const m = /wait (\d+)/.exec(command) | ||
if (m) { | ||
const sleepMillis = parseInt(m[1]) | ||
if (sleepMillis > 5) { | ||
await this.sleep(sleepMillis) | ||
} | ||
} else { | ||
this.props.sendCommand(command, controllerState) | ||
} | ||
} | ||
} | ||
|
||
render(): React.ReactNode { | ||
return <div> | ||
<Typography variant="h3">Macros</Typography> | ||
<Grid container> | ||
<Grid item hidden={this.state.isRecording}> | ||
<Tooltip title="Start recording a macro" placement="top"> | ||
<Button onClick={this.startRecording}> | ||
<span role='img' aria-label="Start recording a macro">🔴</span> | ||
</Button> | ||
</Tooltip> | ||
</Grid> | ||
<Grid item hidden={!this.state.isRecording}> | ||
<Tooltip title="Stop recording the macro" placement="top" > | ||
<Button onClick={this.stopRecording}> | ||
<span role='img' aria-label="Stop recording the macro">⬛</span> | ||
</Button> | ||
</Tooltip> | ||
</Grid> | ||
<Grid item hidden={!this.state.macroExists}> | ||
<Tooltip title="Play the last macro recorded" placement="top" > | ||
<Button onClick={this.playLastRecordedMacro}> | ||
<span role='img' aria-label="Play the last macro recorded">▶</span> | ||
</Button> | ||
</Tooltip> | ||
</Grid> | ||
</Grid> | ||
</div> | ||
} | ||
} | ||
|
||
export default withStyles(styles)(Macros) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters