Better structure

This commit is contained in:
Ben Cragg 2019-04-10 11:34:20 +01:00
parent e7da74e974
commit 65f91d5cea
1 changed files with 21 additions and 31 deletions

View File

@ -8,11 +8,7 @@ import (
"github.com/faiface/pixel/pixelgl" "github.com/faiface/pixel/pixelgl"
) )
const ( // These hold the state of whether we're placing the first or second point of the line.
modeR = iota
modeL
)
const ( const (
clickLineA = iota clickLineA = iota
clickLineB clickLineB
@ -24,7 +20,6 @@ var (
r = pixel.R(10, 10, 70, 50) r = pixel.R(10, 10, 70, 50)
l = pixel.L(pixel.V(20, 20), pixel.V(100, 30)) l = pixel.L(pixel.V(20, 20), pixel.V(100, 30))
mode = modeR
clickLine = clickLineA clickLine = clickLineA
) )
@ -43,47 +38,42 @@ func run() {
imd := imdraw.New(nil) imd := imdraw.New(nil)
for !win.Closed() { for !win.Closed() {
if mode == modeR { win.Clear(color.RGBA{R: 23, G: 39, B: 58, A: 125})
win.Clear(color.RGBA{R: 23, G: 39, B: 58, A: 125})
} else {
win.Clear(color.RGBA{R: 21, G: 55, B: 18, A: 125})
}
imd.Clear() imd.Clear()
if win.JustPressed(pixelgl.KeyR) { // When mouse left-click, move the rectangle so its' center is at the mouse position
mode = modeR
}
if win.JustPressed(pixelgl.KeyL) {
mode = modeL
clickLine = clickLineA
}
if win.JustPressed(pixelgl.MouseButtonLeft) { if win.JustPressed(pixelgl.MouseButtonLeft) {
if mode == modeR { rectToMouse := r.Center().To(win.MousePosition())
rectToMouse := r.Center().To(win.MousePosition()) r = r.Moved(rectToMouse)
r = r.Moved(rectToMouse) }
}
if mode == modeL { // When mouse right-click, set either the beginning or end of the line.
if clickLine == clickLineA { if win.JustPressed(pixelgl.MouseButtonRight) {
l = pixel.L(win.MousePosition(), win.MousePosition().Add(pixel.V(1, 1))) if clickLine == clickLineA {
clickLine = clickLineB // Set the beginning of the line to the mouse position.
} else { // To make it clearer to the user, set the end position 1 pixel (in each direction) away from the first
l = pixel.L(l.A, win.MousePosition()) // point.
clickLine = clickLineA l = pixel.L(win.MousePosition(), win.MousePosition().Add(pixel.V(1, 1)))
} clickLine = clickLineB
} else {
// Set the end point of the line.
l = pixel.L(l.A, win.MousePosition())
clickLine = clickLineA
} }
} }
// Draw the rectangle.
imd.Color = color.Black imd.Color = color.Black
imd.Push(r.Min, r.Max) imd.Push(r.Min, r.Max)
imd.Rectangle(3) imd.Rectangle(3)
// Draw the line.
imd.Color = color.RGBA{R: 10, G: 10, B: 250, A: 255} imd.Color = color.RGBA{R: 10, G: 10, B: 250, A: 255}
imd.Push(l.A, l.B) imd.Push(l.A, l.B)
imd.Line(3) imd.Line(3)
imd.Color = color.RGBA{R: 250, G: 10, B: 10, A: 255} imd.Color = color.RGBA{R: 250, G: 10, B: 10, A: 255}
// Draw any intersection points.
for _, i := range r.IntersectionPoints(l) { for _, i := range r.IntersectionPoints(l) {
imd.Push(i) imd.Push(i)
imd.Circle(4, 0) imd.Circle(4, 0)