Skip to content

Commit e19797a

Browse files
committed
[devtools] Implement distance measurement
1 parent 25caa59 commit e19797a

File tree

3 files changed

+84
-13
lines changed

3 files changed

+84
-13
lines changed

devtools/internal/inspector/draw.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@ import (
1515

1616
var BgColor, FgColor byte
1717

18+
var pixelColorAtMouseCoords byte
19+
1820
func Draw() {
1921
snapshot.Draw()
20-
21-
// I check the input in Draw function because only during Draw operation
22-
// I have access to screen restored from snapshot
23-
if pi.MouseBtnp(pi.MouseLeft) {
24-
x, y := pi.MousePos()
25-
fmt.Printf("Screen pixel (%d, %d) with color %d selected\n", x, y, pi.Pget(x, y))
26-
}
27-
22+
pixelColorAtMouseCoords = pi.Pget(pi.MousePos())
2823
moveBarIfNeeded()
2924
drawBar()
25+
26+
drawDistanceLine()
27+
3028
drawPointer()
3129
}
3230

@@ -39,9 +37,15 @@ func drawBar() {
3937

4038
pi.RectFill(0, barY, vm.ScreenWidth, barY+6, BgColor)
4139

42-
mostX := printCoords(mouseX, mouseY, 1, barY+1)
43-
color := pi.Pget(mouseX, mouseY)
44-
printPixelColor(color, mostX+4, barY+1)
40+
textX := 1
41+
textY := barY + 1
42+
43+
if distance.measuring {
44+
printDistance(textX, textY)
45+
} else {
46+
mostX := printCoords(mouseX, mouseY, textX, textY)
47+
printPixelColor(pixelColorAtMouseCoords, mostX+4, textY)
48+
}
4549
}
4650

4751
func printCoords(mouseX int, mouseY int, x, y int) int {
@@ -60,10 +64,27 @@ func drawPointer() {
6064
}
6165

6266
func choosePointerColor(x, y int) byte {
63-
c := pi.Pget(x, y)
67+
c := pixelColorAtMouseCoords
6468
if rgb.BrightnessDelta(vm.Palette[FgColor], vm.Palette[c]) >= rgb.BrightnessDelta(vm.Palette[BgColor], vm.Palette[c]) {
6569
return FgColor
6670
}
6771

6872
return BgColor
6973
}
74+
75+
func drawDistanceLine() {
76+
if distance.measuring {
77+
x, y := pi.MousePos()
78+
pi.Line(distance.startX, distance.startY, x, y, BgColor)
79+
}
80+
}
81+
82+
func printDistance(x, y int) int {
83+
if distance.measuring {
84+
dist, width, height := calcDistance()
85+
text := fmt.Sprintf("D: %.1f W: %d H: %d", dist, width, height)
86+
return pi.Print(text, x, y, FgColor)
87+
}
88+
89+
return x
90+
}

devtools/internal/inspector/update.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package inspector
2+
3+
import (
4+
"fmt"
5+
"math"
6+
7+
"github.com/elgopher/pi"
8+
)
9+
10+
var distance struct {
11+
measuring bool
12+
startX, startY int
13+
}
14+
15+
func calcDistance() (dist float64, width, height int) {
16+
x, y := pi.MousePos()
17+
18+
width = x - distance.startX
19+
if width < 0 {
20+
width *= -1
21+
}
22+
23+
height = y - distance.startY
24+
if height < 0 {
25+
height *= -1
26+
}
27+
dist = math.Sqrt(float64(width*width + height*height))
28+
return
29+
}
30+
31+
func Update() {
32+
x, y := pi.MousePos()
33+
34+
if pi.MouseBtn(pi.MouseLeft) && !distance.measuring {
35+
distance.measuring = true
36+
distance.startX, distance.startY = x, y
37+
fmt.Printf("Measuring started at (%d, %d)\n", x, y)
38+
} else if !pi.MouseBtn(pi.MouseLeft) && distance.measuring {
39+
distance.measuring = false
40+
dist, width, height := calcDistance()
41+
fmt.Printf("Measuring stopped at (%d, %d). Distance is: %f, width: %d, height %d\n", x, y, dist, width, height)
42+
}
43+
}

devtools/update.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
package devtools
55

6-
import "github.com/elgopher/pi/key"
6+
import (
7+
"github.com/elgopher/pi/devtools/internal/inspector"
8+
"github.com/elgopher/pi/key"
9+
)
710

811
func updateDevTools() {
912
if key.Btnp(key.F12) {
@@ -13,4 +16,8 @@ func updateDevTools() {
1316
resumeGame()
1417
}
1518
}
19+
20+
if gamePaused {
21+
inspector.Update()
22+
}
1623
}

0 commit comments

Comments
 (0)