Skip to content

Commit

Permalink
Add next [n] - go to next frame from devtools
Browse files Browse the repository at this point in the history
User can go to next frame when the game is paused. To do so he can:

* issue next command in REPL terminal (or 'n')
* press n in game window
  • Loading branch information
elgopher committed Aug 13, 2023
1 parent 74af10d commit 2886e84
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 11 deletions.
16 changes: 14 additions & 2 deletions devtools/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
)

var (
gamePaused bool
timeWhenPaused float64
gamePaused bool
pauseOnNextFrame bool
timeWhenPaused float64
)

var helpShown bool
Expand All @@ -23,6 +24,8 @@ func pauseGame() {
helpShown = true
fmt.Println("\nPress right mouse button in the game window to show the toolbar.")
fmt.Println("Press P in the game window to take screenshot.")
fmt.Println("Press N in the game window to go to next frame.")
fmt.Println("Press F12 in the game window to resume the game and exit devtools inspector.")
}
gamePaused = true
timeWhenPaused = pi.Time
Expand All @@ -35,3 +38,12 @@ func resumeGame() {
snapshot.Draw()
fmt.Println("Game resumed")
}

func resumeUntilNextFrame() {
if gamePaused {
resumeGame()
pauseOnNextFrame = true
} else {
fmt.Println("Game not paused")
}
}
2 changes: 1 addition & 1 deletion devtools/internal/help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func PrintHelp(topic string) error {
"\n\n" +
"Type help topic for more information. For example: help pi or help pi.Spr" +
"\n\n" +
"Available commands: help [h], pause [p], resume [r], undo [u]",
"Available commands: help [h], pause [p], resume [r], undo [u], next [n]",
)
return nil
default:
Expand Down
17 changes: 10 additions & 7 deletions devtools/internal/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func ensureNameDoesNotClashWithImportedPackages(name string) error {
return nil
}

var allCommandNames = []string{"h", "help", "p", "pause", "r", "resume", "u", "undo"}
var allCommandNames = []string{"h", "help", "p", "pause", "r", "resume", "u", "undo", "n", "next"}

func ensureNameDoesNotClashWithCommandNames(name string) error {
for _, cmd := range allCommandNames {
Expand Down Expand Up @@ -185,12 +185,13 @@ func ExportType[T any](i Instance) error {
type EvalResult int

const (
HelpPrinted EvalResult = 0
GoCodeExecuted EvalResult = 1
Resumed EvalResult = 2
Paused EvalResult = 3
Undoed EvalResult = 4
Continued EvalResult = 5
HelpPrinted EvalResult = 0
GoCodeExecuted EvalResult = 1
Resumed EvalResult = 2
Paused EvalResult = 3
Undoed EvalResult = 4
Continued EvalResult = 5
NextFrameRequested EvalResult = 6
)

func (i Instance) Eval(cmd string) (EvalResult, error) {
Expand All @@ -199,6 +200,8 @@ func (i Instance) Eval(cmd string) (EvalResult, error) {
if isHelpCommand(trimmedCmd) {
topic := strings.Trim(strings.TrimLeft(trimmedCmd, "help"), " ")
return HelpPrinted, i.printHelp(topic)
} else if trimmedCmd == "next" || trimmedCmd == "n" {
return NextFrameRequested, nil
} else if trimmedCmd == "resume" || trimmedCmd == "r" {
return Resumed, nil
} else if trimmedCmd == "pause" || trimmedCmd == "p" {
Expand Down
18 changes: 17 additions & 1 deletion devtools/internal/interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ func TestEval(t *testing.T) {
code: "resume ",
expectedResult: interpreter.Resumed,
},
"next": {
code: "next",
expectedResult: interpreter.NextFrameRequested,
},
"next with space in the beginning": {
code: " next",
expectedResult: interpreter.NextFrameRequested,
},
"next with space in the end": {
code: "next ",
expectedResult: interpreter.NextFrameRequested,
},
"n": {
code: "n",
expectedResult: interpreter.NextFrameRequested,
},
}
for name, testCase := range tests {
t.Run(name, func(t *testing.T) {
Expand Down Expand Up @@ -273,7 +289,7 @@ func TestExport(t *testing.T) {
})

t.Run("should return error when name clashes with command name", func(t *testing.T) {
var allCommandNames = []string{"h", "help", "p", "pause", "r", "resume", "u", "undo"}
var allCommandNames = []string{"h", "help", "p", "pause", "r", "resume", "u", "undo", "n", "next"}

for _, cmd := range allCommandNames {
t.Run(cmd, func(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions devtools/scripting.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func evaluateNextCommandFromTerminal() {
case interpreter.Undoed:
snapshot.Undo()
fmt.Println("Undoed last draw operation")
case interpreter.NextFrameRequested:
resumeUntilNextFrame()
}

if result == interpreter.Continued {
Expand Down
9 changes: 9 additions & 0 deletions devtools/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ func updateDevTools() {
}
}

if pauseOnNextFrame {
pauseGame()
pauseOnNextFrame = false
}

if gamePaused {
if key.Btnp(key.N) {
resumeUntilNextFrame()
}

inspector.Update()
}

Expand Down

0 comments on commit 2886e84

Please sign in to comment.