Skip to content

Commit 5e9bae5

Browse files
committed
[devtools] Add Spr tool
Allow user drawing sprites on the paused game. Let user select the sprite with new devtool and draw the sprite on the screen. This tool support moving the entire sprite-sheet. To do so, please use the middle mouse button.
1 parent 5548902 commit 5e9bae5

File tree

6 files changed

+118
-3
lines changed

6 files changed

+118
-3
lines changed

devtools/internal/icons/icons.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const (
2020
RectFillTool = 5
2121
CircTool = 6
2222
CircFillTool = 7
23+
SprTool = 8
2324
)
2425

2526
//go:embed icons.png

devtools/internal/icons/icons.png

4 Bytes
Loading

devtools/internal/inspector/spr.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package inspector
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/elgopher/pi"
7+
"github.com/elgopher/pi/devtools/internal/icons"
8+
"github.com/elgopher/pi/devtools/internal/snapshot"
9+
)
10+
11+
type Spr struct {
12+
icon byte
13+
mode sprMode
14+
}
15+
16+
type sprMode interface {
17+
update(*Spr)
18+
draw()
19+
}
20+
21+
func (l *Spr) Update() {
22+
l.mode.update(l)
23+
}
24+
25+
func (l *Spr) Draw() {
26+
l.mode.draw()
27+
}
28+
29+
func (l *Spr) Icon() byte {
30+
return l.icon
31+
}
32+
33+
type sprSelectionMode struct {
34+
lastMousePos pi.Position
35+
camera pi.Position
36+
}
37+
38+
func (m *sprSelectionMode) update(spr *Spr) {
39+
if pi.MouseBtn(pi.MouseMiddle) {
40+
zero := pi.Position{}
41+
if m.lastMousePos == zero {
42+
m.lastMousePos = pi.MousePos
43+
}
44+
dx := m.lastMousePos.X - pi.MousePos.X
45+
dy := m.lastMousePos.Y - pi.MousePos.Y
46+
47+
m.camera.X -= dx
48+
minCameraX := -(pi.SprSheet().Width() - pi.Scr().Width())
49+
m.camera.X = pi.MidInt(minCameraX, m.camera.X, 0)
50+
51+
m.camera.Y -= dy
52+
minCameraY := -(pi.SprSheet().Height() - pi.Scr().Height())
53+
m.camera.Y = pi.MidInt(minCameraY, m.camera.Y, 0)
54+
55+
m.lastMousePos = pi.MousePos
56+
}
57+
58+
if !pi.MouseBtn(pi.MouseMiddle) {
59+
m.lastMousePos.Reset()
60+
}
61+
62+
if pi.MouseBtnp(pi.MouseLeft) {
63+
spr.mode = &sprDrawingMode{selectedSprite: m.spriteNo(pi.MousePos)}
64+
}
65+
}
66+
67+
func (m *sprSelectionMode) draw() {
68+
pi.SprSheet().Copy(0, 0, pi.SprSheet().Width(), pi.SprSheet().Height(), pi.Scr(), m.camera.X, m.camera.Y)
69+
70+
mouseX, mouseY := pi.MousePos.X, pi.MousePos.Y
71+
72+
icons.Draw(mouseX, mouseY, FgColor, icons.Pointer)
73+
74+
screen := pi.Scr()
75+
var barY int
76+
if !isBarOnTop {
77+
barY = screen.Height() - 7
78+
}
79+
80+
pi.RectFill(0, barY, screen.Width(), barY+6, BgColor)
81+
82+
spriteNo := fmt.Sprintf("SRITE %d", m.spriteNo(pi.MousePos))
83+
pi.Print(spriteNo, 1, barY+1, FgColor)
84+
}
85+
86+
func (m *sprSelectionMode) spriteNo(mousePos pi.Position) int {
87+
x := mousePos.X - m.camera.X
88+
y := mousePos.Y - m.camera.Y
89+
cellX := x / 8
90+
cellY := y / 8
91+
return cellY*(pi.SprSheet().Width()/8) + cellX
92+
}
93+
94+
type sprDrawingMode struct {
95+
selectedSprite int
96+
}
97+
98+
func (m *sprDrawingMode) update(*Spr) {
99+
if pi.MouseBtnp(pi.MouseLeft) {
100+
fmt.Printf("pi.Spr(%d, %d, %d)\n", m.selectedSprite, pi.MousePos.X, pi.MousePos.Y)
101+
snapshot.Take()
102+
}
103+
}
104+
105+
func (m *sprDrawingMode) draw() {
106+
mouseX, mouseY := pi.MousePos.X, pi.MousePos.Y
107+
pi.Spr(m.selectedSprite, mouseX, mouseY)
108+
}

devtools/internal/inspector/tool.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ func selectTool(icon byte) {
5050
draw: drawCirc("CircFill", pi.CircFill),
5151
icon: icons.CircFillTool,
5252
}
53+
case icons.SprTool:
54+
tool = &Spr{
55+
icon: icons.SprTool,
56+
mode: &sprSelectionMode{},
57+
}
5358
}
5459
}
5560

devtools/internal/inspector/toolbar.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/elgopher/pi/key"
1010
)
1111

12-
const toolbarWidth = 28
12+
const toolbarWidth = 32
1313
const toolbarHeight = 4
1414

1515
var toolbar Toolbar
@@ -73,7 +73,7 @@ func (t *Toolbar) draw() {
7373
y := t.pos.Y
7474
pi.RectFill(x, y, x+toolbarWidth, y+toolbarHeight, BgColor)
7575
icons.Draw(x+1, y+1, FgColor,
76-
icons.MeasureTool, icons.SetTool, icons.LineTool, icons.RectTool, icons.RectFillTool, icons.CircTool, icons.CircFillTool)
76+
icons.MeasureTool, icons.SetTool, icons.LineTool, icons.RectTool, icons.RectFillTool, icons.CircTool, icons.CircFillTool, icons.SprTool)
7777

7878
if t.toolHighlighted > 0 {
7979
toolX := x + int((t.toolHighlighted-1)*4)

docs/ROADMAP.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
* [ ] zoom-in, zoom-out
4242
* [ ] drawing on the screen using Pi functions
4343
* [x] Set, shapes
44-
* [ ] Spr, Print
44+
* [x] Spr
45+
* [ ] Print
4546
* [x] **scripting/REPL** - write Go code **live** when the game is running
4647
* [ ] palette inspector
4748
* [ ] display, draw palette

0 commit comments

Comments
 (0)