aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorfaiface <faiface2202@gmail.com>2019-05-09 14:28:25 +0200
committerfaiface <faiface2202@gmail.com>2019-05-09 14:28:25 +0200
commitba4fc4a57f9a38252f708f785f37aa24138dfe09 (patch)
tree79bd9c19859933afe9cd792c0282dc77d292391f /examples
parentde5d2f5c78da7f8a578ecc25f8e8b1fa90f20264 (diff)
downloadgui-ba4fc4a57f9a38252f708f785f37aa24138dfe09.zip
change event strings to event types
Diffstat (limited to 'examples')
-rw-r--r--examples/imageviewer/browser.go22
-rw-r--r--examples/imageviewer/button.go17
-rw-r--r--examples/imageviewer/main.go4
-rw-r--r--examples/imageviewer/splits.go55
-rw-r--r--examples/imageviewer/viewer.go7
-rw-r--r--examples/paint/main.go30
-rw-r--r--examples/pexeso/main.go11
7 files changed, 65 insertions, 81 deletions
diff --git a/examples/imageviewer/browser.go b/examples/imageviewer/browser.go
index 5a79c3b..cdac56f 100644
--- a/examples/imageviewer/browser.go
+++ b/examples/imageviewer/browser.go
@@ -8,6 +8,7 @@ import (
"path/filepath"
"github.com/faiface/gui"
+ "github.com/faiface/gui/win"
"golang.org/x/image/math/fixed"
)
@@ -111,21 +112,16 @@ func Browser(env gui.Env, theme *Theme, dir string, cd <-chan string, view chan<
return
}
- var (
- x0, y0, x1, y1 int
- x, y int
- )
-
- switch {
- case e.Matches("resize/%d/%d/%d/%d", &x0, &y0, &x1, &y1):
- r = image.Rect(x0, y0, x1, y1)
+ switch e := e.(type) {
+ case gui.Resize:
+ r = e.Rectangle
env.Draw() <- redraw(r, selected, position, lineHeight, namesImage)
- case e.Matches("mo/down/%d/%d", &x, &y):
- if !image.Pt(x, y).In(r) {
+ case win.MoDown:
+ if !e.Point.In(r) {
continue
}
- click := image.Pt(x, y).Sub(r.Min).Add(position)
+ click := e.Point.Sub(r.Min).Add(position)
i := click.Y / lineHeight
if i < 0 || i >= len(names) {
continue
@@ -157,8 +153,8 @@ func Browser(env gui.Env, theme *Theme, dir string, cd <-chan string, view chan<
env.Draw() <- redraw(r, selected, position, lineHeight, namesImage)
}
- case e.Matches("mo/scroll/%d/%d", &x, &y):
- newP := position.Sub(image.Pt(int(x*16), int(y*16)))
+ case win.MoScroll:
+ newP := position.Sub(e.Point.Mul(16))
if newP.X > namesImage.Bounds().Max.X-r.Dx() {
newP.X = namesImage.Bounds().Max.X - r.Dx()
}
diff --git a/examples/imageviewer/button.go b/examples/imageviewer/button.go
index f2a619b..0693e06 100644
--- a/examples/imageviewer/button.go
+++ b/examples/imageviewer/button.go
@@ -6,6 +6,7 @@ import (
"image/draw"
"github.com/faiface/gui"
+ "github.com/faiface/gui/win"
)
func Button(env gui.Env, theme *Theme, text string, action func()) {
@@ -34,23 +35,21 @@ func Button(env gui.Env, theme *Theme, text string, action func()) {
)
for e := range env.Events() {
- var x, y, x0, y0, x1, y1 int
-
- switch {
- case e.Matches("resize/%d/%d/%d/%d", &x0, &y0, &x1, &y1):
- r = image.Rect(x0, y0, x1, y1)
+ switch e := e.(type) {
+ case gui.Resize:
+ r = e.Rectangle
env.Draw() <- redraw(r, over, pressed)
- case e.Matches("mo/down/%d/%d/left", &x, &y):
- newPressed := image.Pt(x, y).In(r)
+ case win.MoDown:
+ newPressed := e.Point.In(r)
if newPressed != pressed {
pressed = newPressed
env.Draw() <- redraw(r, over, pressed)
}
- case e.Matches("mo/up/%d/%d/left", &x, &y):
+ case win.MoUp:
if pressed {
- if image.Pt(x, y).In(r) {
+ if e.Point.In(r) {
action()
}
pressed = false
diff --git a/examples/imageviewer/main.go b/examples/imageviewer/main.go
index eed3bb0..67f97fb 100644
--- a/examples/imageviewer/main.go
+++ b/examples/imageviewer/main.go
@@ -54,8 +54,8 @@ func run() {
})
for e := range env.Events() {
- switch {
- case e.Matches("wi/close"):
+ switch e.(type) {
+ case win.WiClose:
close(env.Draw())
}
}
diff --git a/examples/imageviewer/splits.go b/examples/imageviewer/splits.go
index 871a3a5..f9daf2e 100644
--- a/examples/imageviewer/splits.go
+++ b/examples/imageviewer/splits.go
@@ -15,16 +15,15 @@ type envPair struct {
func (ep *envPair) Events() <-chan gui.Event { return ep.events }
func (ep *envPair) Draw() chan<- func(draw.Image) image.Rectangle { return ep.draw }
-func FixedLeft(env gui.Env, x1 int) gui.Env {
+func FixedLeft(env gui.Env, maxX int) gui.Env {
out, in := gui.MakeEventsChan()
go func() {
for e := range env.Events() {
- var x0, y0, dummy, y1 int
- switch {
- case e.Matches("resize/%d/%d/%d/%d", &x0, &y0, &dummy, &y1):
- in <- gui.Eventf("resize/%d/%d/%d/%d", x0, y0, x1, y1)
- default:
+ if resize, ok := e.(gui.Resize); ok {
+ resize.Max.X = maxX
+ in <- resize
+ } else {
in <- e
}
}
@@ -34,16 +33,15 @@ func FixedLeft(env gui.Env, x1 int) gui.Env {
return &envPair{out, env.Draw()}
}
-func FixedRight(env gui.Env, x0 int) gui.Env {
+func FixedRight(env gui.Env, minX int) gui.Env {
out, in := gui.MakeEventsChan()
go func() {
for e := range env.Events() {
- var dummy, y0, x1, y1 int
- switch {
- case e.Matches("resize/%d/%d/%d/%d", &dummy, &y0, &x1, &y1):
- in <- gui.Eventf("resize/%d/%d/%d/%d", x0, y0, x1, y1)
- default:
+ if resize, ok := e.(gui.Resize); ok {
+ resize.Min.X = minX
+ in <- resize
+ } else {
in <- e
}
}
@@ -53,16 +51,15 @@ func FixedRight(env gui.Env, x0 int) gui.Env {
return &envPair{out, env.Draw()}
}
-func FixedTop(env gui.Env, y1 int) gui.Env {
+func FixedTop(env gui.Env, maxY int) gui.Env {
out, in := gui.MakeEventsChan()
go func() {
for e := range env.Events() {
- var x0, y0, x1, dummy int
- switch {
- case e.Matches("resize/%d/%d/%d/%d", &x0, &y0, &x1, &dummy):
- in <- gui.Eventf("resize/%d/%d/%d/%d", x0, y0, x1, y1)
- default:
+ if resize, ok := e.(gui.Resize); ok {
+ resize.Max.Y = maxY
+ in <- resize
+ } else {
in <- e
}
}
@@ -72,16 +69,15 @@ func FixedTop(env gui.Env, y1 int) gui.Env {
return &envPair{out, env.Draw()}
}
-func FixedBottom(env gui.Env, y0 int) gui.Env {
+func FixedBottom(env gui.Env, minY int) gui.Env {
out, in := gui.MakeEventsChan()
go func() {
for e := range env.Events() {
- var x0, dummy, x1, y1 int
- switch {
- case e.Matches("resize/%d/%d/%d/%d", &x0, &dummy, &x1, &y1):
- in <- gui.Eventf("resize/%d/%d/%d/%d", x0, y0, x1, y1)
- default:
+ if resize, ok := e.(gui.Resize); ok {
+ resize.Min.Y = minY
+ in <- resize
+ } else {
in <- e
}
}
@@ -96,12 +92,11 @@ func EvenHorizontal(env gui.Env, minI, maxI, n int) gui.Env {
go func() {
for e := range env.Events() {
- var x0, y0, x1, y1 int
- switch {
- case e.Matches("resize/%d/%d/%d/%d", &x0, &y0, &x1, &y1):
- x0, x1 := x0+(x1-x0)*minI/n, x0+(x1-x0)*maxI/n
- in <- gui.Eventf("resize/%d/%d/%d/%d", x0, y0, x1, y1)
- default:
+ if resize, ok := e.(gui.Resize); ok {
+ x0, x1 := resize.Min.X, resize.Max.X
+ resize.Min.X, resize.Max.X = x0+(x1-x0)*minI/n, x0+(x1-x0)*maxI/n
+ in <- resize
+ } else {
in <- e
}
}
diff --git a/examples/imageviewer/viewer.go b/examples/imageviewer/viewer.go
index b70594e..6f68e15 100644
--- a/examples/imageviewer/viewer.go
+++ b/examples/imageviewer/viewer.go
@@ -53,11 +53,8 @@ func Viewer(env gui.Env, theme *Theme, view <-chan string) {
close(env.Draw())
return
}
-
- var x0, y0, x1, y1 int
- switch {
- case e.Matches("resize/%d/%d/%d/%d", &x0, &y0, &x1, &y1):
- r = image.Rect(x0, y0, x1, y1)
+ if resize, ok := e.(gui.Resize); ok {
+ r = resize.Rectangle
env.Draw() <- redraw(r, img)
}
}
diff --git a/examples/paint/main.go b/examples/paint/main.go
index cd11882..fec06c3 100644
--- a/examples/paint/main.go
+++ b/examples/paint/main.go
@@ -18,10 +18,9 @@ func ColorPicker(env gui.Env, pick chan<- color.Color, r image.Rectangle, clr co
}
for event := range env.Events() {
- var x, y int
- switch {
- case event.Matches("mo/down/%d/%d", &x, &y):
- if image.Pt(x, y).In(r) {
+ switch event := event.(type) {
+ case win.MoDown:
+ if event.Point.In(r) {
pick <- clr
}
}
@@ -43,7 +42,7 @@ func Canvas(env gui.Env, pick <-chan color.Color, r image.Rectangle) {
var (
clr = color.Color(color.Black)
pressed = false
- px, py = 0, 0
+ prev image.Point
)
for {
@@ -56,21 +55,20 @@ func Canvas(env gui.Env, pick <-chan color.Color, r image.Rectangle) {
return
}
- var x, y int
- switch {
- case event.Matches("mo/down/%d/%d", &x, &y):
- if image.Pt(x, y).In(r) {
+ switch event := event.(type) {
+ case win.MoDown:
+ if event.Point.In(r) {
pressed = true
- px, py = x, y
+ prev = event.Point
}
- case event.Matches("mo/up/%d/%d", &x, &y):
+ case win.MoUp:
pressed = false
- case event.Matches("mo/move/%d/%d", &x, &y):
+ case win.MoMove:
if pressed {
- x0, y0, x1, y1 := px, py, x, y
- px, py = x, y
+ x0, y0, x1, y1 := prev.X, prev.Y, event.X, event.Y
+ prev = event.Point
env.Draw() <- func(drw draw.Image) image.Rectangle {
dc.SetColor(clr)
@@ -118,8 +116,8 @@ func run() {
go Canvas(mux.MakeEnv(), pick, image.Rect(0, 0, 750, 600))
for event := range env.Events() {
- switch {
- case event.Matches("wi/close"):
+ switch event.(type) {
+ case win.WiClose:
close(env.Draw())
}
}
diff --git a/examples/pexeso/main.go b/examples/pexeso/main.go
index 73b18fb..56bd6e6 100644
--- a/examples/pexeso/main.go
+++ b/examples/pexeso/main.go
@@ -68,10 +68,9 @@ func Tile(env gui.Env, pair chan PairMsg, r image.Rectangle, clr color.Color) {
env.Draw() <- redraw(1.0)
for event := range env.Events() {
- var x, y int
- switch {
- case event.Matches("mo/down/%d/%d", &x, &y):
- if image.Pt(x, y).In(r) {
+ switch event := event.(type) {
+ case win.MoDown:
+ if event.Point.In(r) {
for c := 32; c >= 0; c-- {
env.Draw() <- redraw(float64(c) / 32)
time.Sleep(time.Second / 32 / 4)
@@ -143,8 +142,8 @@ func run() {
}
for event := range env.Events() {
- switch {
- case event.Matches("wi/close"):
+ switch event.(type) {
+ case win.WiClose:
close(env.Draw())
}
}