Skip to content

Commit

Permalink
Add scripting
Browse files Browse the repository at this point in the history
  • Loading branch information
elgopher committed Aug 8, 2023
1 parent 0f0e765 commit 20c73a7
Show file tree
Hide file tree
Showing 38 changed files with 1,713 additions and 15 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
go: [ 1.18 ]
go: [ "1.20" ]
env:
DISPLAY: ':99.0'
steps:
Expand All @@ -25,12 +25,12 @@ jobs:
sudo apt-get update
sudo apt-get install libasound2-dev libgl1-mesa-dev libxcursor-dev libxi-dev libxinerama-dev libxrandr-dev libxxf86vm-dev
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}

Expand Down
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"console": "externalTerminal"
}
]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Pi is under development. Only limited functionality is provided. API is not stab
## How to get started?

1. Install dependencies
* [Go 1.18+](https://go.dev/dl/)
* [Go 1.20+](https://go.dev/dl/)
* If not on Windows, please install additional dependencies for [Linux](docs/install-linux.md) or [macOS](docs/install-macos.md).
2. Try examples from [examples](examples) directory.
3. Create a new game using provided [Github template](https://github.com/elgopher/pi-template).
Expand Down
11 changes: 11 additions & 0 deletions devtools/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package devtools

import (
"fmt"

"github.com/elgopher/pi"
"github.com/elgopher/pi/devtools/internal/snapshot"
)
Expand All @@ -13,7 +15,15 @@ var (
timeWhenPaused float64
)

var helpShown bool

func pauseGame() {
fmt.Println("Game paused")
if !helpShown {
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.")
}
gamePaused = true
timeWhenPaused = pi.TimeSeconds
snapshot.Take()
Expand All @@ -23,4 +33,5 @@ func resumeGame() {
gamePaused = false
pi.TimeSeconds = timeWhenPaused
snapshot.Draw()
fmt.Println("Game resumed")
}
7 changes: 6 additions & 1 deletion devtools/devtools.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/elgopher/pi"
"github.com/elgopher/pi/devtools/internal/inspector"
"github.com/elgopher/pi/devtools/internal/terminal"
)

var (
Expand All @@ -33,7 +34,8 @@ func MustRun(runBackend func() error) {
}

inspector.BgColor, inspector.FgColor = BgColor, FgColor
fmt.Println("Press F12 to pause the game and show devtools.")
fmt.Println("Press F12 in the game window to pause the game and activate devtools with terminal.")
fmt.Println("Terminal activated. Type help for help.")

pi.Update = func() {
updateDevTools()
Expand All @@ -53,6 +55,9 @@ func MustRun(runBackend func() error) {
}
}

terminal.StartReadingCommands()
defer terminal.StopReadingCommandsFromStdin()

if err := runBackend(); err != nil {
panic(fmt.Sprintf("Something terrible happened! Pi cannot be run: %v\n", err))
}
Expand Down
2 changes: 1 addition & 1 deletion devtools/internal/inspector/measure.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (m *Measure) Update() {
case pi.MouseBtnp(pi.MouseLeft) && !distance.measuring:
distance.measuring = true
distance.startX, distance.startY = x, y
fmt.Printf("Measuring started at (%d, %d)\n", x, y)
fmt.Printf("\nMeasuring started at (%d, %d)\n", x, y)
case !pi.MouseBtn(pi.MouseLeft) && distance.measuring:
distance.measuring = false
dist, width, height := calcDistance()
Expand Down
8 changes: 0 additions & 8 deletions devtools/internal/inspector/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package inspector

import (
"fmt"
"math"

"github.com/elgopher/pi"
Expand Down Expand Up @@ -33,14 +32,7 @@ func calcDistance() (dist float64, width, height int) {
return
}

var helpShown bool

func Update() {
if !helpShown {
helpShown = true
fmt.Println("Press right mouse button to show toolbar.")
fmt.Println("Press P to take screenshot.")
}

if !toolbar.visible {
tool.Update()
Expand Down
114 changes: 114 additions & 0 deletions devtools/internal/interpreter/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package interpreter

import (
"bufio"
"errors"
"fmt"
"os"
"os/exec"
"sort"
"strings"

"github.com/elgopher/pi/devtools/internal/interpreter/internal/lib"
)

func printHelp(topic string) {
switch topic {
case "":
println("This is interactive terminal. " +
"You can write Go code here, which will run immediately in the paused game. " +
"You can use all Pi packages: pi, key, state, snap, font, image and " +
"selection of standard packages: " + strings.Join(stdPackages(), ", ") + ". " +
"\n\n" +
"Type help topic for more information. For example: help pi or help pi.Spr")
default:
goDoc(topic)
}
}

func stdPackages() []string {
var packages []string
for _, p := range lib.AllPackages() {
if p.IsStdPackage() {
packages = append(packages, p.Alias)
}
}
sort.Strings(packages)
return packages
}

func goDoc(symbol string) {
fmt.Println("###############################################################################")

symbol = completeSymbol(symbol)
if symbolNotSupported(symbol) {
fmt.Println("no help found")
return
}

var args []string
args = append(args, "doc")
if shouldShowDetailedDescriptionForSymbol(symbol) {
args = append(args, "-all")
}
args = append(args, symbol)
command := exec.Command("go", args...)
command.Stdout = bufio.NewWriter(os.Stdout)
if err := command.Run(); err != nil {
var exitErr *exec.ExitError
if isExitErr := errors.As(err, &exitErr); isExitErr && exitErr.ExitCode() == 1 {
fmt.Println("no help found")
return
}
fmt.Println("problem getting help:", err)
}
return
}

func completeSymbol(symbol string) string {
packages := lib.AllPackages()

for _, p := range packages {
if p.Alias == symbol {
return p.Name
}
}

for _, p := range packages {
prefix := p.Alias + "."
if strings.HasPrefix(symbol, prefix) {
return p.Name + "." + symbol[len(prefix):]
}
}

return symbol
}

func symbolNotSupported(symbol string) bool {
packages := lib.AllPackages()

for _, p := range packages {
prefix := p.Name + "."
if strings.HasPrefix(symbol, prefix) || symbol == p.Name {
return false
}
}

return true
}

var symbolsWithDetailedDescription = []string{
"github.com/elgopher/pi.Button",
"github.com/elgopher/pi.MouseButton",
"github.com/elgopher/pi/key.Button",
}

func shouldShowDetailedDescriptionForSymbol(symbol string) bool {
for _, s := range symbolsWithDetailedDescription {
if symbol == s {
return true
}
}

return false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 20c73a7

Please sign in to comment.