SHARE
TWEET

Untitled

a guest May 22nd, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "image"
  5.     "image/draw"
  6.  
  7.     "github.com/peterhellberg/gfx"
  8.     "github.com/peterhellberg/gui"
  9. )
  10.  
  11. func main() {
  12.     gui.Run(func() {
  13.         win, err := gui.Open(gui.Size(512, 512))
  14.         if err != nil {
  15.             return
  16.         }
  17.  
  18.         mux, env := gui.NewMux(win)
  19.         go foo(mux.Env(), gfx.IR(0, 0, 512, 256))
  20.         go bar(mux.Env(), gfx.IR(0, 256, 512, 512))
  21.  
  22.         for event := range env.Events() {
  23.             switch event := event.(type) {
  24.             case gui.EventClose:
  25.                 env.Close()
  26.             case gui.EventKeyboardDown:
  27.                 if event.Key == "escape" {
  28.                     env.Close()
  29.                 }
  30.             }
  31.         }
  32.     })
  33. }
  34.  
  35. func foo(env gui.Env, bounds image.Rectangle) {
  36.     drawCircle := func(pt image.Point) func(draw.Image) image.Rectangle {
  37.         return func(dst draw.Image) image.Rectangle {
  38.             if pt.In(bounds) {
  39.                 c := gfx.PaletteArne16.Random()
  40.                 gfx.DrawPointCircle(dst, pt, 30, 15, c)
  41.             }
  42.  
  43.             return bounds
  44.         }
  45.     }
  46.  
  47.     for event := range env.Events() {
  48.         switch event := event.(type) {
  49.         case gui.EventMouseMove:
  50.             env.Draw(drawCircle(event.Point))
  51.         case gui.EventResize:
  52.             env.Draw(func(dst draw.Image) image.Rectangle {
  53.                 gfx.EachPixel(bounds, func(x, y int) {
  54.                     v := uint8(x ^ y)
  55.                     c := gfx.ColorNRGBA(v%192, v, v, 255)
  56.  
  57.                     dst.Set(x, y, c)
  58.                 })
  59.  
  60.                 return bounds
  61.             })
  62.         }
  63.     }
  64. }
  65.  
  66. func bar(env gui.Env, bounds image.Rectangle) {
  67.     drawSquare := func(pt image.Point) func(draw.Image) image.Rectangle {
  68.         return func(dst draw.Image) image.Rectangle {
  69.             if pt.In(bounds) {
  70.                 c := gfx.PaletteEDG16.Random()
  71.                 n := 30
  72.                 r := gfx.IR(pt.X-n, pt.Y-n, pt.X+n, pt.Y+n)
  73.  
  74.                 gfx.DrawColor(dst, r, c)
  75.             }
  76.  
  77.             return bounds
  78.         }
  79.     }
  80.  
  81.     for event := range env.Events() {
  82.         switch event := event.(type) {
  83.         case gui.EventMouseMove:
  84.             env.Draw(drawSquare(event.Point))
  85.         case gui.EventResize:
  86.             env.Draw(func(dst draw.Image) image.Rectangle {
  87.                 gfx.EachPixel(bounds, func(x, y int) {
  88.                     v := uint8(x ^ y)
  89.                     c := gfx.ColorNRGBA(v, v%192, v, 255)
  90.  
  91.                     dst.Set(x, y, c)
  92.                 })
  93.  
  94.                 return bounds
  95.             })
  96.         }
  97.     }
  98. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top