Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GUI experiment with two routines drawing parts of the window (foo draws the upper half, bar the bottom half)
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
})
}
}
}
@peterhellberg

This comment has been minimized.

Copy link
Owner Author

commented May 12, 2019

gui-experiment

gui-experiment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.