From 7e308af41c3aa16b8e845acc99f9098c11f83b36 Mon Sep 17 00:00:00 2001 From: "Colin J. Brigato" Date: Thu, 3 Oct 2024 13:25:08 +0200 Subject: [PATCH] linter p1 --- examples/asyncimage/asyncimage.go | 27 ++++++++++++--- .../loadimageAdvanced/loadimageAdvanced.go | 3 ++ examples/paint/canvas.go | 33 +++++++++++++++++++ examples/paint/draw.go | 6 ++++ examples/paint/paint.go | 1 + examples/paint/toolbar.go | 25 ++++++++++++-- 6 files changed, 89 insertions(+), 6 deletions(-) diff --git a/examples/asyncimage/asyncimage.go b/examples/asyncimage/asyncimage.go index 04fde02c..4c1bad06 100644 --- a/examples/asyncimage/asyncimage.go +++ b/examples/asyncimage/asyncimage.go @@ -1,3 +1,4 @@ +// Package main shows how to handle image async with state and events package main import ( @@ -10,6 +11,7 @@ import ( "time" "github.com/AllenDang/cimgui-go/imgui" + g "github.com/AllenDang/giu" ) @@ -52,6 +54,7 @@ func HeaderWidget() g.Widget { if dynamicImage.GetState() == g.SurfaceStateLoading { return TextHeader("Image Is Currently loading...", color.RGBA{0x80, 0x80, 0xFF, 255}) } + return CanLoadHeader() } @@ -63,19 +66,22 @@ func ShouldReturnImage() g.Widget { if dynamicImage.GetState() != g.SurfaceStateSuccess { return fallback.ToImageWidget().Size(-1, -1) } + return g.Custom(func() { dynamicImage.ToImageWidget().Scale(imageScaleX, imageScaleY).Build() }) } func ShouldReturnPanel() g.Widget { - return g.Custom(func() { imgui.SeparatorText("Image Scale") + if imgui.Button("Reset##Scaling") { imageScaleX = 1.0 imageScaleY = 1.0 } + imgui.SameLine() imgui.Checkbox("Linked##Scaling", &linkedScale) + if linkedScale { imgui.SliderFloat("scale XY##Scaling", &imageScaleX, 0.1, 4.0) imageScaleY = imageScaleX @@ -83,7 +89,9 @@ func ShouldReturnPanel() g.Widget { imgui.SliderFloat("scale X##Scaling", &imageScaleX, 0.1, 4.0) imgui.SliderFloat("scale Y##Scaling", &imageScaleY, 0.1, 4.0) } + imgui.SeparatorText("FileSystem URLS") + if dynamicImage.GetState() == g.SurfaceStateLoading { imgui.Text("Unavailable while loading image...") } else { @@ -91,30 +99,36 @@ func ShouldReturnPanel() g.Widget { imgui.Text("By default, root is executable working dir") imgui.Text("-> Try loading this in the url bar:") imgui.Text("file:///files/sonic.png ->") + if imgui.Button("or CLICK HERE") { inputRootfs = "." dynamicImage.SetFSRoot(inputRootfs) + dynamicImageUrl = "file:///files/sonic.png" err := dynamicImage.ResetState() + if err == nil { dynamicImage.SetSurfaceFromURL(dynamicImageUrl, time.Second*5, false) } + linkedScale = true imageScaleX = 0.356 imageScaleY = 0.356 } + imgui.Separator() imgui.Text("Set rootFS to / for full filesystem access") + rootfs := dynamicImage.GetFSRoot() imgui.Text(fmt.Sprintf("Current ROOTFS: %s", rootfs)) g.InputText(&inputRootfs).Build() imgui.SameLine() + if imgui.Button("SET rootfs") { dynamicImage.SetFSRoot(inputRootfs) } } }) - } func loop() { @@ -145,6 +159,7 @@ func noOSDecoratedWindowsConfig() g.MasterWindowFlags { io.SetConfigViewportsNoAutoMerge(true) io.SetConfigViewportsNoDefaultParent(true) io.SetConfigWindowsMoveFromTitleBarOnly(true) + return g.MasterWindowFlagsHidden | g.MasterWindowFlagsTransparent | g.MasterWindowFlagsFrameless } @@ -165,8 +180,9 @@ func initDynamicImage() error { log.Printf("DynamicImage failed loading with error: %+v\n", e) } }).OnSuccess(func() { - footerLabel = "DynamicImage has sucessfully loaded new surface !" - log.Println("DynamicImage has sucessfully loaded new surface !") + footerLabel = "DynamicImage has successfully loaded new surface !" + + log.Println("DynamicImage has successfully loaded new surface !") }) return nil @@ -177,6 +193,7 @@ func main() { // Flags are to be used with NewMasterWindow. // Should NOT use SingleLayoutWindow ! mwFlags := noOSDecoratedWindowsConfig() + if err := initDynamicImage(); err != nil { log.Fatalf("Error in DynamicImage initialization: %v", err) } @@ -185,3 +202,5 @@ func main() { wnd.SetTargetFPS(60) wnd.Run(loop) } + +/**/ diff --git a/examples/loadimageAdvanced/loadimageAdvanced.go b/examples/loadimageAdvanced/loadimageAdvanced.go index 4f464412..0c184dc9 100644 --- a/examples/loadimageAdvanced/loadimageAdvanced.go +++ b/examples/loadimageAdvanced/loadimageAdvanced.go @@ -23,6 +23,7 @@ var ( func loop() { var start_pos image.Point + g.SingleWindow().Layout( g.Custom(func() { start_pos = g.GetCursorScreenPos() @@ -69,10 +70,12 @@ func loop() { func main() { var err error + rgba, err = g.LoadImage("./fallback.png") if err != nil { log.Fatalf("Cannot loadIamge fallback.png") } + fromfile.SetSurfaceFromFile("gopher.png", false) fromrgba.SetSurfaceFromRGBA(rgba, false) fromurl.SetSurfaceFromURL("https://static.wikia.nocookie.net/smashbros/images/0/0e/Art_Sonic_TSR.png/revision/latest?cb=20200210122913&path-prefix=fr", time.Second*5, false) diff --git a/examples/paint/canvas.go b/examples/paint/canvas.go index 11d98c08..0d0d31aa 100644 --- a/examples/paint/canvas.go +++ b/examples/paint/canvas.go @@ -25,6 +25,7 @@ var ( func FlushDrawCommands(c *Canvas) { bcopy := append(buffer) go c.AppendDrawCommands(&bcopy) + buffer = nil buffer = []DrawCommand{} } @@ -55,19 +56,25 @@ func (c *Canvas) Compute() { if !c.inited { Floodfill(c.Image, color.RGBA{255, 255, 254, 255}, 1, 1) Floodfill(c.Image, color.RGBA{255, 255, 255, 255}, 2, 2) + err := c.PushImageToBackend(false) if err != nil { return } + c.inited = true + return } + if len(c.DrawCommands) < 1 { return } + if len(c.DrawCommands) <= c.LastComputedLen { return } + draws := c.GetDrawCommands(c.LastComputedLen) for _, r := range draws { switch r.Tool { @@ -80,6 +87,7 @@ func (c *Canvas) Compute() { default: } } + _ = c.PushImageToBackend(false) c.LastComputedLen = len(c.DrawCommands) } @@ -99,19 +107,24 @@ func UndoCanvas() { func ClearCanvas() error { var err error + canvas.Backend.ForceRelease() canvas, err = NewCanvas(canvasDetectedHeight) + return err } func NewCanvas(height float32) (*Canvas, error) { backend := &g.ReflectiveBoundTexture{} image := defaultSurface(height) + err := backend.SetSurfaceFromRGBA(image, false) if err != nil { return nil, err } + c := &Canvas{Image: image, Backend: backend} + return c, nil } @@ -123,6 +136,7 @@ func FittingCanvasSize16By9(height float32) image.Point { func defaultSurface(height float32) *image.RGBA { p := FittingCanvasSize16By9(height) surface, _ := g.UniformLoader(p.X, p.Y, color.RGBA{255, 255, 255, 255}).ServeRGBA() + return surface } @@ -161,35 +175,49 @@ func computeCanvasBounds() { func CanvasWidget() g.Widget { canvas.Compute() + return g.Custom(func() { if was_drawing && !g.IsMouseDown(g.MouseButtonLeft) { was_drawing = false + FlushDrawCommands(canvas) + lastTo = image.Point{0, 0} } + scr := g.GetCursorScreenPos() + canvas.Backend.ToImageWidget().Build() + if g.IsItemHovered() { mousepos := g.GetMousePos() if mousepos.X >= scr.X && mousepos.X <= scr.X+int(canvasComputedWidth) && mousepos.Y >= scr.Y && mousepos.Y <= scr.Y+int(canvasDetectedHeight) { inpos := image.Point{mousepos.X - scr.X, mousepos.Y - scr.Y} + if imgui.IsMouseClickedBool(imgui.MouseButtonLeft) { was_drawing = true + canvas.UndoIndexes = append(canvas.UndoIndexes, len(canvas.DrawCommands)) lastTo = image.Point{0, 0} + buffer = append(buffer, DrawCommand{Tool: current_tool, Color: current_color, BrushSize: brush_size, From: inpos, To: inpos}) lastTo = inpos + FlushDrawCommands(canvas) } + if g.IsMouseDown(g.MouseButtonLeft) && was_drawing { delta := imgui.CurrentIO().MouseDelta() dx := int(delta.X) dy := int(delta.Y) + if dx == 0 || dy == 0 { FlushDrawCommands(canvas) } + buffer = append(buffer, DrawCommand{Tool: current_tool, Color: current_color, BrushSize: brush_size, From: lastTo, To: inpos}) lastTo = inpos + if len(buffer) >= 8 { FlushDrawCommands(canvas) } @@ -203,14 +231,19 @@ func CanvasRow() g.Widget { return g.Custom(func() { if !canvasInited { computeCanvasBounds() + var err error + canvas, err = NewCanvas(canvasDetectedHeight) if err != nil { return } + canvasInited = true + return } + g.Row( g.Dummy(canvasMarginComputedWidth, canvasDetectedHeight), CanvasWidget(), diff --git a/examples/paint/draw.go b/examples/paint/draw.go index 4aa96a0e..96f4598f 100644 --- a/examples/paint/draw.go +++ b/examples/paint/draw.go @@ -11,14 +11,17 @@ func DrawLine(x1, y1, x2, y2 int, c color.Color, linewidth float32, img *image.R // Use Bresenham's line algorithm to get all points along the line dx := math.Abs(float64(x2 - x1)) dy := math.Abs(float64(y2 - y1)) + sx := -1 if x1 < x2 { sx = 1 } + sy := -1 if y1 < y2 { sy = 1 } + err := dx - dy for { @@ -28,11 +31,13 @@ func DrawLine(x1, y1, x2, y2 int, c color.Color, linewidth float32, img *image.R if x1 == x2 && y1 == y2 { break } + e2 := 2 * err if e2 > -dy { err -= dy x1 += sx } + if e2 < dx { err += dx y1 += sy @@ -100,5 +105,6 @@ func Floodfill(input *image.RGBA, c color.Color, x int, y int) { func colorsEqual(c1, c2 color.Color) bool { r1, g1, b1, a1 := c1.RGBA() r2, g2, b2, a2 := c2.RGBA() + return r1 == r2 && g1 == g2 && b1 == b2 && a1 == a2 } diff --git a/examples/paint/paint.go b/examples/paint/paint.go index f0b41dca..5336fc31 100644 --- a/examples/paint/paint.go +++ b/examples/paint/paint.go @@ -43,6 +43,7 @@ func noOSDecoratedWindowsConfig() g.MasterWindowFlags { io.SetConfigViewportsNoAutoMerge(true) io.SetConfigViewportsNoDefaultParent(true) io.SetConfigWindowsMoveFromTitleBarOnly(true) + return g.MasterWindowFlagsHidden | g.MasterWindowFlagsTransparent | g.MasterWindowFlagsFrameless } diff --git a/examples/paint/toolbar.go b/examples/paint/toolbar.go index 99b6fb67..86716531 100644 --- a/examples/paint/toolbar.go +++ b/examples/paint/toolbar.go @@ -36,6 +36,7 @@ func Assets() (fs.FS, error) { func LoadAsset(path string, backend *g.ReflectiveBoundTexture) error { assets, _ := Assets() + file, err := assets.Open(path) if err != nil { return fmt.Errorf("LoadAsset: error opening image file %s: %w", file, err) @@ -46,6 +47,7 @@ func LoadAsset(path string, backend *g.ReflectiveBoundTexture) error { panic(fmt.Sprintf("error closing image file: %s", file)) } }() + return backend.SetSurfaceFromFsFile(file, false) } @@ -65,7 +67,9 @@ func initToolbar() error { return err } } + toolbarInited = true + return nil } @@ -73,13 +77,13 @@ func ShowToolbar() g.Widget { if !toolbarInited { initToolbar() } + return g.Child().Size(-1, TOOLBAR_H).Layout( ButtonColorMaker(), ) } func colorPopup(ce *color.RGBA, fe g.ColorEditFlags) { - p := g.ToVec4Color(pickerRefColor) pcol := []float32{p.X, p.Y, p.Z, p.W} @@ -106,14 +110,15 @@ func colorPopup(ce *color.RGBA, fe g.ColorEditFlags) { W: col[3], }) } + imgui.EndPopup() } - } func ButtonColorMaker() *g.RowWidget { start_ul := imgui.CursorPos() sz := imgui.Vec2{} + return g.Row(g.Custom(func() { for i := range defaultColors { if i%2 == 0 { @@ -121,50 +126,66 @@ func ButtonColorMaker() *g.RowWidget { if imgui.ColorButtonV(fmt.Sprintf("%d##cur_color%d", i, i), col, 0, imgui.Vec2{X: 0, Y: 0}) { current_color = defaultColors[i] } + sz = imgui.ItemRectSize() imgui.SameLineV(0, 0) } } + col := g.ToVec4Color(current_color) if imgui.ColorButtonV(fmt.Sprintf("##CHOSENcur_color%d", current_color), col, 0, sz.Mul(2.0)) { pickerRefColor = current_color + imgui.OpenPopupStr("Custom Color") } + colorPopup(¤t_color, g.ColorEditFlagsNoAlpha) imgui.SameLine() + if imgui.ImageButton("##pen_tool", penButtonImg.Texture().ID(), sz.Mul(1.7)) { current_tool = 0 } + imgui.SameLine() + if imgui.ImageButton("##fill_tool", fillButtonImg.Texture().ID(), sz.Mul(1.7)) { current_tool = 1 } + imgui.SameLine() + if imgui.ImageButton("##undo_tool", undoButtonImg.Texture().ID(), sz.Mul(1.7)) { UndoCanvas() } + imgui.SameLine() + if imgui.ImageButton("##clear_tool", clearButtonImg.Texture().ID(), sz.Mul(1.7)) { ClearCanvas() } + imgui.SameLine() imgui.ImageButton("##open_tool", openButtonImg.Texture().ID(), sz.Mul(1.7)) imgui.SameLine() imgui.ImageButton("##save_tool", saveButtonImg.Texture().ID(), sz.Mul(1.7)) + if imgui.ImageButton("##brush_tool", brushButtonImg.Texture().ID(), sz.Mul(0.9)) { brush_size = 12.0 } + imgui.SameLine() imgui.PushItemWidth(225.0) imgui.SliderFloat("##Brush Size", &brush_size, float32(0.1), float32(72.0)) imgui.PopItemWidth() imgui.SetCursorPos(start_ul) + for i := range defaultColors { if i%2 != 0 { col := g.ToVec4Color(defaultColors[i]) if imgui.ColorButtonV(fmt.Sprintf("%d##cur_color%d", i, i), col, 0, imgui.Vec2{X: 0, Y: 0}) { current_color = defaultColors[i] } + imgui.SameLineV(0, 0) } }