Skip to content

Commit f5ebdac

Browse files
committed
[devtools] [refactor] Move screen inspector to separate package
1 parent 01371f7 commit f5ebdac

File tree

4 files changed

+85
-61
lines changed

4 files changed

+85
-61
lines changed

devtools/devtools.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package devtools
55

66
import (
77
"github.com/elgopher/pi"
8+
"github.com/elgopher/pi/devtools/internal/inspector"
89
"github.com/elgopher/pi/vm"
910
)
1011

@@ -23,6 +24,8 @@ func MustRun(runBackend func() error) {
2324
update := pi.Update
2425
draw := pi.Draw
2526

27+
inspector.BgColor, inspector.FgColor = BgColor, FgColor
28+
2629
pi.Update = func() {
2730
updateDevTools()
2831

devtools/draw.go

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,8 @@
33

44
package devtools
55

6-
import (
7-
"fmt"
8-
9-
"github.com/elgopher/pi"
10-
"github.com/elgopher/pi/devtools/internal/icons"
11-
"github.com/elgopher/pi/devtools/internal/snapshot"
12-
"github.com/elgopher/pi/vm"
13-
)
6+
import "github.com/elgopher/pi/devtools/internal/inspector"
147

158
func drawDevTools() {
16-
snapshot.Draw()
17-
18-
// I check the input in Draw function because only during Draw operation
19-
// I have access to screen restored from snapshot
20-
if pi.MouseBtnp(pi.MouseLeft) {
21-
x, y := pi.MousePos()
22-
fmt.Printf("Screen pixel (%d, %d) with color %d selected\n", x, y, pi.Pget(x, y))
23-
}
24-
25-
moveBarIfNeeded()
26-
drawBar()
27-
drawPointer()
28-
}
29-
30-
var isBarOnTop bool
31-
32-
func moveBarIfNeeded() {
33-
_, mouseY := pi.MousePos()
34-
switch {
35-
case isBarOnTop && mouseY <= 12:
36-
isBarOnTop = false
37-
case !isBarOnTop && mouseY >= vm.ScreenHeight-12:
38-
isBarOnTop = true
39-
}
40-
}
41-
42-
func drawBar() {
43-
mouseX, mouseY := pi.MousePos()
44-
var barY int
45-
if !isBarOnTop {
46-
barY = vm.ScreenHeight - 7
47-
}
48-
49-
pi.RectFill(0, barY, vm.ScreenWidth, barY+6, BgColor)
50-
51-
mostX := printCoords(mouseX, mouseY, 1, barY+1)
52-
color := pi.Pget(mouseX, mouseY)
53-
printPixelColor(color, mostX+4, barY+1)
54-
}
55-
56-
func printCoords(mouseX int, mouseY int, x, y int) int {
57-
coords := fmt.Sprintf("%d %d", mouseX, mouseY)
58-
return pi.Print(coords, x, y, FgColor)
59-
}
60-
61-
func printPixelColor(color byte, x int, y int) int {
62-
c := fmt.Sprintf("%d", color)
63-
return pi.Print(c, x, y, color)
64-
}
65-
66-
func drawPointer() {
67-
x, y := pi.MousePos()
68-
icons.Draw(icons.Pointer, x, y, FgColor)
9+
inspector.Draw()
6910
}

devtools/internal/inspector/draw.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// (c) 2022 Jacek Olszak
2+
// This code is licensed under MIT license (see LICENSE for details)
3+
4+
package inspector
5+
6+
import (
7+
"fmt"
8+
9+
"github.com/elgopher/pi"
10+
"github.com/elgopher/pi/devtools/internal/icons"
11+
"github.com/elgopher/pi/devtools/internal/snapshot"
12+
"github.com/elgopher/pi/vm"
13+
)
14+
15+
var BgColor, FgColor byte
16+
17+
func Draw() {
18+
snapshot.Draw()
19+
20+
// I check the input in Draw function because only during Draw operation
21+
// I have access to screen restored from snapshot
22+
if pi.MouseBtnp(pi.MouseLeft) {
23+
x, y := pi.MousePos()
24+
fmt.Printf("Screen pixel (%d, %d) with color %d selected\n", x, y, pi.Pget(x, y))
25+
}
26+
27+
moveBarIfNeeded()
28+
drawBar()
29+
drawPointer()
30+
}
31+
32+
func drawBar() {
33+
mouseX, mouseY := pi.MousePos()
34+
var barY int
35+
if !isBarOnTop {
36+
barY = vm.ScreenHeight - 7
37+
}
38+
39+
pi.RectFill(0, barY, vm.ScreenWidth, barY+6, BgColor)
40+
41+
mostX := printCoords(mouseX, mouseY, 1, barY+1)
42+
color := pi.Pget(mouseX, mouseY)
43+
printPixelColor(color, mostX+4, barY+1)
44+
}
45+
46+
func printCoords(mouseX int, mouseY int, x, y int) int {
47+
coords := fmt.Sprintf("%d %d", mouseX, mouseY)
48+
return pi.Print(coords, x, y, FgColor)
49+
}
50+
51+
func printPixelColor(color byte, x int, y int) int {
52+
c := fmt.Sprintf("%d", color)
53+
return pi.Print(c, x, y, color)
54+
}
55+
56+
func drawPointer() {
57+
x, y := pi.MousePos()
58+
icons.Draw(icons.Pointer, x, y, FgColor)
59+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// (c) 2022 Jacek Olszak
2+
// This code is licensed under MIT license (see LICENSE for details)
3+
4+
package inspector
5+
6+
import (
7+
"github.com/elgopher/pi"
8+
"github.com/elgopher/pi/vm"
9+
)
10+
11+
var isBarOnTop bool
12+
13+
func moveBarIfNeeded() {
14+
_, mouseY := pi.MousePos()
15+
switch {
16+
case isBarOnTop && mouseY <= 12:
17+
isBarOnTop = false
18+
case !isBarOnTop && mouseY >= vm.ScreenHeight-12:
19+
isBarOnTop = true
20+
}
21+
}

0 commit comments

Comments
 (0)