Last active
May 12, 2019 18:08
-
-
Save peterhellberg/df6b03dd6c3fec57635ec47de00a9048 to your computer and use it in GitHub Desktop.
GUI experiment with two routines drawing parts of the window (foo draws the upper half, bar the bottom half)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"image" | |
"image/draw" | |
"github.com/peterhellberg/gfx" | |
"github.com/peterhellberg/gui" | |
) | |
func main() { | |
gui.Run(func() { | |
win, err := gui.Open(gui.Size(512, 512)) | |
if err != nil { | |
return | |
} | |
mux, env := gui.NewMux(win) | |
go foo(mux.Env(), gfx.IR(0, 0, 512, 256)) | |
go bar(mux.Env(), gfx.IR(0, 256, 512, 512)) | |
for event := range env.Events() { | |
switch event := event.(type) { | |
case gui.EventClose: | |
env.Close() | |
case gui.EventKeyboardDown: | |
if event.Key == "escape" { | |
env.Close() | |
} | |
} | |
} | |
}) | |
} | |
func foo(env gui.Env, bounds image.Rectangle) { | |
drawCircle := func(pt image.Point) func(draw.Image) image.Rectangle { | |
return func(dst draw.Image) image.Rectangle { | |
if pt.In(bounds) { | |
c := gfx.PaletteArne16.Random() | |
gfx.DrawPointCircle(dst, pt, 30, 15, c) | |
} | |
return bounds | |
} | |
} | |
for event := range env.Events() { | |
switch event := event.(type) { | |
case gui.EventMouseMove: | |
env.Draw(drawCircle(event.Point)) | |
case gui.EventResize: | |
env.Draw(func(dst draw.Image) image.Rectangle { | |
gfx.EachPixel(bounds, func(x, y int) { | |
v := uint8(x ^ y) | |
c := gfx.ColorNRGBA(v%192, v, v, 255) | |
dst.Set(x, y, c) | |
}) | |
return bounds | |
}) | |
} | |
} | |
} | |
func bar(env gui.Env, bounds image.Rectangle) { | |
drawSquare := func(pt image.Point) func(draw.Image) image.Rectangle { | |
return func(dst draw.Image) image.Rectangle { | |
if pt.In(bounds) { | |
c := gfx.PaletteEDG16.Random() | |
n := 30 | |
r := gfx.IR(pt.X-n, pt.Y-n, pt.X+n, pt.Y+n) | |
gfx.DrawColor(dst, r, c) | |
} | |
return bounds | |
} | |
} | |
for event := range env.Events() { | |
switch event := event.(type) { | |
case gui.EventMouseMove: | |
env.Draw(drawSquare(event.Point)) | |
case gui.EventResize: | |
env.Draw(func(dst draw.Image) image.Rectangle { | |
gfx.EachPixel(bounds, func(x, y int) { | |
v := uint8(x ^ y) | |
c := gfx.ColorNRGBA(v, v%192, v, 255) | |
dst.Set(x, y, c) | |
}) | |
return bounds | |
}) | |
} | |
} | |
} |
Author
peterhellberg
commented
May 12, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment