Created
July 1, 2022 16:27
-
-
Save mgcaret/ae8daa3fc15f3e0a3c10919ef726fe72 to your computer and use it in GitHub Desktop.
gfxtest
This file contains hidden or 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 ( | |
"fmt" | |
"image" | |
"image/color" | |
"image/draw" | |
"github.com/fogleman/gg" | |
"golang.org/x/exp/shiny/driver" | |
"golang.org/x/exp/shiny/screen" | |
"golang.org/x/mobile/event/key" | |
"golang.org/x/mobile/event/lifecycle" | |
"golang.org/x/mobile/event/mouse" | |
"golang.org/x/mobile/event/paint" | |
"golang.org/x/mobile/event/size" | |
) | |
var theSize = image.Point{ | |
X: 640, | |
Y: 480, | |
} | |
func main() { | |
driver.Main(func(s screen.Screen) { | |
isDrawing := false | |
drawPt := image.Point{} | |
w, err := s.NewWindow(&screen.NewWindowOptions{ | |
Width: theSize.X, | |
Height: theSize.Y, | |
Title: "Test", | |
}) | |
if err != nil { | |
panic(err) | |
return | |
} | |
defer w.Release() | |
//w.Fill(image.Rect(0, 0, 639, 479), color.Black, draw.Src) | |
buf, err := s.NewBuffer(theSize) | |
if err != nil { | |
panic(err) | |
} | |
defer buf.Release() | |
dc := gg.NewContextForRGBA(buf.RGBA()) | |
dc.SetRGB(0, 0.5, 0) | |
dc.Clear() | |
dc.DrawCircle(float64(theSize.X/2), float64(theSize.Y/2), float64(theSize.Y/3)) | |
dc.SetRGB(0.75, 0.75, 0) | |
dc.Fill() | |
dc.SetRGB(0, 0, 0) | |
fmt.Println(buf.RGBA().Bounds()) | |
for { | |
switch e := w.NextEvent().(type) { | |
case size.Event: | |
theSize = e.Size() | |
w.Fill(image.Rect(0, 0, theSize.X-1, theSize.Y-1), color.Black, draw.Src) | |
w.Send(paint.Event{}) | |
case paint.Event: | |
w.Upload(image.Point{X: 0, Y: 0}, buf, buf.Bounds()) | |
w.Publish() | |
case key.Event: | |
fmt.Println(e) | |
switch e.Code { | |
case key.CodeEscape: | |
return | |
case key.CodeLeftShift: | |
if e.Direction == key.DirPress { | |
dc.SetRGB(1, 1, 1) | |
} else { | |
dc.SetRGB(0, 0, 0) | |
} | |
} | |
case mouse.Event: | |
switch e.Button { | |
case mouse.ButtonLeft: | |
isDrawing = !isDrawing | |
if isDrawing { | |
dc.SetLineWidth(1) | |
drawPt.X = int(e.X) | |
drawPt.Y = int(e.Y) | |
} | |
case mouse.ButtonRight: | |
dc.SetColor(buf.RGBA().At(int(e.X), int(e.Y))) | |
} | |
if isDrawing { | |
dc.DrawLine(float64(drawPt.X), float64(drawPt.Y), float64(e.X), float64(e.Y)) | |
drawPt.X = int(e.X) | |
drawPt.Y = int(e.Y) | |
dc.Stroke() | |
w.Send(paint.Event{}) | |
} | |
case lifecycle.Event: | |
if e.To == lifecycle.StageDead { | |
return | |
} | |
// etc | |
default: | |
fmt.Printf("%T\n", e) | |
} | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment