Skip to content

Commit

Permalink
TODO Remove MousePos, rename MousePosition to MousePos
Browse files Browse the repository at this point in the history
  • Loading branch information
elgopher committed Aug 13, 2023
1 parent 9e9bf37 commit 5a014ca
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 70 deletions.
4 changes: 2 additions & 2 deletions devtools/internal/inspector/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var pixelColorAtMouseCoords byte

func Draw() {
snapshot.Draw()
pixelColorAtMouseCoords = pi.Pget(pi.MousePos())
pixelColorAtMouseCoords = pi.Pget(pi.MousePos.X, pi.MousePos.Y)
handleScreenshot()

moveBarIfNeeded()
Expand All @@ -35,7 +35,7 @@ func Draw() {
}

func cursorOutOfWindow() bool {
x, y := pi.MousePos()
x, y := pi.MousePos.X, pi.MousePos.Y
screen := pi.Scr()
return x < 0 || x >= screen.Width() || y < 0 || y >= screen.Height()
}
Expand Down
5 changes: 2 additions & 3 deletions devtools/internal/inspector/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (
var isBarOnTop bool

func moveBarIfNeeded() {
_, mouseY := pi.MousePos()
switch {
case isBarOnTop && mouseY <= 12:
case isBarOnTop && pi.MousePos.Y <= 12:
isBarOnTop = false
case !isBarOnTop && mouseY >= pi.Scr().Height()-12:
case !isBarOnTop && pi.MousePos.Y >= pi.Scr().Height()-12:
isBarOnTop = true
}
}
10 changes: 4 additions & 6 deletions devtools/internal/inspector/measure.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func (m *Measure) Draw() {

func (m *Measure) drawBar() {
screen := pi.Scr()
mouseX, mouseY := pi.MousePos()
var barY int
if !isBarOnTop {
barY = screen.Height() - 7
Expand All @@ -40,7 +39,7 @@ func (m *Measure) drawBar() {
if distance.measuring {
m.printDistance(textX, textY)
} else {
mostX := m.printCoords(mouseX, mouseY, textX, textY)
mostX := m.printCoords(pi.MousePos.X, pi.MousePos.Y, textX, textY)
m.printPixelColor(pixelColorAtMouseCoords, mostX+4, textY)
}
}
Expand All @@ -66,8 +65,7 @@ func (m *Measure) choosePointerColor(x, y int) byte {

func (m *Measure) drawDistanceLine() {
if distance.measuring {
x, y := pi.MousePos()
pi.Line(distance.startX, distance.startY, x, y, BgColor)
pi.Line(distance.startX, distance.startY, pi.MousePos.X, pi.MousePos.Y, BgColor)
}
}

Expand All @@ -82,14 +80,14 @@ func (m *Measure) printDistance(x, y int) int {
}

func (m *Measure) drawPointer() {
x, y := pi.MousePos()
x, y := pi.MousePos.X, pi.MousePos.Y
color := m.choosePointerColor(x, y)
icons.Draw(x, y, color, icons.Pointer)
icons.Draw(x+2, y+2, color, tool.Icon())
}

func (m *Measure) Update() {
x, y := pi.MousePos()
x, y := pi.MousePos.X, pi.MousePos.Y
switch {
case pi.MouseBtnp(pi.MouseLeft) && !distance.measuring:
distance.measuring = true
Expand Down
5 changes: 2 additions & 3 deletions devtools/internal/inspector/pset.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Pset struct {
}

func (p *Pset) Update() {
x, y := pi.MousePos()
x, y := pi.MousePos.X, pi.MousePos.Y
leftp := pi.MouseBtnp(pi.MouseLeft) && !p.running
left := pi.MouseBtn(pi.MouseLeft) && p.running
if (leftp || left) && pixelColorAtMouseCoords != FgColor {
Expand All @@ -29,8 +29,7 @@ func (p *Pset) Update() {
}

func (p *Pset) Draw() {
x, y := pi.MousePos()
pi.Pset(x, y, FgColor)
pi.Pset(pi.MousePos.X, pi.MousePos.Y, FgColor)
}

func (p *Pset) Icon() byte {
Expand Down
7 changes: 3 additions & 4 deletions devtools/internal/inspector/shape.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,19 @@ type Shape struct {
func (l *Shape) Update() {
switch {
case pi.MouseBtnp(pi.MouseLeft) && !l.started:
l.start = pi.MousePosition
l.start = pi.MousePos
l.started = true
case !pi.MouseBtn(pi.MouseLeft) && l.started:
x, y := pi.MousePos()
l.started = false
snapshot.Draw()
command := l.draw(l.start.X, l.start.Y, x, y, FgColor)
command := l.draw(l.start.X, l.start.Y, pi.MousePos.X, pi.MousePos.Y, FgColor)
fmt.Println(command)
snapshot.Take()
}
}

func (l *Shape) Draw() {
x, y := pi.MousePos()
x, y := pi.MousePos.X, pi.MousePos.Y
icons.Draw(x, y, FgColor, icons.Pointer)
icons.Draw(x+2, y+2, FgColor, l.icon)
if pi.MouseBtn(pi.MouseLeft) && l.started {
Expand Down
10 changes: 4 additions & 6 deletions devtools/internal/inspector/toolbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Toolbar struct {

func (t *Toolbar) toggle() {
t.visible = !t.visible
t.pos = pi.MousePosition
t.pos = pi.MousePos
t.pos.X -= toolbarWidth/2 - 2
if t.pos.X < 0 {
t.pos.X = 0
Expand All @@ -33,7 +33,7 @@ func (t *Toolbar) toggle() {
}
t.pos.Y -= toolbarHeight + 2
if t.pos.Y < 0 {
t.pos.Y = pi.MousePosition.Y + 6
t.pos.Y = pi.MousePos.Y + 6
}
}

Expand All @@ -45,8 +45,7 @@ func (t *Toolbar) hide() {

func (t *Toolbar) update() {
if t.visible {
x, y := pi.MousePos()
sx, sy := x-t.pos.X, y-t.pos.Y
sx, sy := pi.MousePos.X-t.pos.X, pi.MousePos.Y-t.pos.Y
mouseOverToolbar := sx >= 0 && sy >= 0 && sx <= toolbarWidth && sy <= toolbarHeight
if mouseOverToolbar {
t.toolHighlighted = byte((sx-1)/4) + 1
Expand Down Expand Up @@ -88,7 +87,6 @@ func (t *Toolbar) draw() {

func (t *Toolbar) drawPointer() {
if t.toolHighlighted == 0 {
x, y := pi.MousePos()
icons.Draw(x, y, FgColor, icons.Pointer)
icons.Draw(pi.MousePos.X, pi.MousePos.Y, FgColor, icons.Pointer)
}
}
6 changes: 2 additions & 4 deletions devtools/internal/inspector/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ var distance struct {
}

func calcDistance() (dist float64, width, height int) {
x, y := pi.MousePos()

width = x - distance.startX
width = pi.MousePos.X - distance.startX
if width < 0 {
width *= -1
}

height = y - distance.startY
height = pi.MousePos.Y - distance.startY
if height < 0 {
height *= -1
}
Expand Down
3 changes: 1 addition & 2 deletions devtools/internal/lib/github_com-elgopher-pi.go

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

4 changes: 2 additions & 2 deletions ebitengine/mouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ func updateMouse() {
}

x, y := ebiten.CursorPosition()
pi.MousePosition.X = x
pi.MousePosition.Y = y
pi.MousePos.X = x
pi.MousePos.Y = y
}
49 changes: 24 additions & 25 deletions examples/shapes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,42 @@ var (
//go:embed sprite-sheet.png
resources embed.FS

drawShape func(x0, y0, x1, y1 int)
drawShape func(start, stop pi.Position)

drawFunctions = []func(x0, y0, x1, y1 int){
func(x0, y0, x1, y1 int) {
pi.Rect(x0, y0, x1, y1, shapeColor)
command := fmt.Sprintf("Rect(%d,%d,%d,%d,%d)", x0, y0, x1, y1, shapeColor)
drawFunctions = []func(start, stop pi.Position){
func(start, stop pi.Position) {
pi.Rect(start.X, start.Y, stop.X, stop.Y, shapeColor)
command := fmt.Sprintf("Rect(%d,%d,%d,%d,%d)", start.X, start.Y, stop.X, stop.Y, shapeColor)
printCmd(command)
},
func(x0, y0, x1, y1 int) {
pi.RectFill(x0, y0, x1, y1, shapeColor)
command := fmt.Sprintf("RectFill(%d,%d,%d,%d,%d)", x0, y0, x1, y1, shapeColor)
func(start, stop pi.Position) {
pi.RectFill(start.X, start.Y, stop.X, stop.Y, shapeColor)
command := fmt.Sprintf("RectFill(%d,%d,%d,%d,%d)", start.X, start.Y, stop.X, stop.Y, shapeColor)
printCmd(command)
},
func(x0, y0, x1, y1 int) {
pi.Line(x0, y0, x1, y1, shapeColor)
command := fmt.Sprintf("Line(%d,%d,%d,%d,%d)", x0, y0, x1, y1, shapeColor)
func(start, stop pi.Position) {
pi.Line(start.X, start.Y, stop.X, stop.Y, shapeColor)
command := fmt.Sprintf("Line(%d,%d,%d,%d,%d)", start.X, start.Y, stop.X, stop.Y, shapeColor)
printCmd(command)
},
func(x0, y0, x1, y1 int) {
r := radius(x0, y0, x1, y1)
pi.Circ(x0, y0, r, shapeColor)
func(start, stop pi.Position) {
r := radius(start.X, start.Y, stop.X, stop.Y)
pi.Circ(start.X, start.Y, r, shapeColor)

command := fmt.Sprintf("Circ(%d,%d,%d,%d)", x0, y0, r, shapeColor)
command := fmt.Sprintf("Circ(%d,%d,%d,%d)", start.X, start.Y, r, shapeColor)
printCmd(command)
},
func(x0, y0, x1, y1 int) {
r := radius(x0, y0, x1, y1)
pi.CircFill(x0, y0, r, shapeColor)
command := fmt.Sprintf("CircFill(%d,%d,%d,%d)", x0, y0, r, shapeColor)
func(start, stop pi.Position) {
r := radius(start.X, start.Y, stop.X, stop.Y)
pi.CircFill(start.X, start.Y, r, shapeColor)
command := fmt.Sprintf("CircFill(%d,%d,%d,%d)", start.X, start.Y, r, shapeColor)
printCmd(command)
},
}

current = 0

x0, y0 int
start pi.Position
)

func main() {
Expand All @@ -72,15 +72,15 @@ func main() {

// set initial coordinates on start dragging
if pi.MouseBtn(pi.MouseLeft) && drawShape == nil {
x0, y0 = pi.MousePos()
start = pi.MousePos
drawShape = drawFunctions[current]

}

// set coordinates during dragging
if drawShape != nil {
x1, y1 := pi.MousePos()
drawShape(x0, y0, x1, y1)
stop := pi.MousePos
drawShape(start, stop)
}

if !pi.MouseBtn(pi.MouseLeft) {
Expand All @@ -94,8 +94,7 @@ func main() {
}

func drawMousePointer() {
x, y := pi.MousePos()
pi.Spr(current, x, y)
pi.Spr(current, pi.MousePos.X, pi.MousePos.Y)
}

func radius(x0, y0, x1, y1 int) int {
Expand Down
9 changes: 2 additions & 7 deletions mouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ var (
// Index of array is equal to mouse button constant.
MouseBtnDuration [3]uint

// MousePosition is the position of mouse in screen coordinates.
MousePosition Position
// MousePos is the position of mouse in screen coordinates.
MousePos Position
)

// MouseBtn returns true if the mouse button is being pressed at this moment.
Expand All @@ -41,8 +41,3 @@ func MouseBtnp(b MouseButton) bool {

return input.IsPressedRepeatably(MouseBtnDuration[b])
}

// MousePos returns the position of mouse in screen coordinates.
func MousePos() (x, y int) {
return MousePosition.X, MousePosition.Y
}
6 changes: 0 additions & 6 deletions mouse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,3 @@ func testMouseBtn(t *testing.T, mouseBtn func(button pi.MouseButton) bool) {
func TestMouseBtnp(t *testing.T) {
testMouseBtn(t, pi.MouseBtnp)
}

func TestMousePos(t *testing.T) {
t.Run("should not panic", func(t *testing.T) {
pi.MousePos()
})
}

0 comments on commit 5a014ca

Please sign in to comment.