Skip to content

Commit

Permalink
linter p1
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbrigato committed Oct 3, 2024
1 parent 05156df commit 7e308af
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 6 deletions.
27 changes: 23 additions & 4 deletions examples/asyncimage/asyncimage.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package main shows how to handle image async with state and events
package main

import (
Expand All @@ -10,6 +11,7 @@ import (
"time"

"github.com/AllenDang/cimgui-go/imgui"

g "github.com/AllenDang/giu"
)

Expand Down Expand Up @@ -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()
}

Expand All @@ -63,58 +66,69 @@ 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
} else {
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 {
imgui.Text("Loading URLS Works with file:/// scheme too.")
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)

Check failure on line 111 in examples/asyncimage/asyncimage.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `dynamicImage.SetSurfaceFromURL` is not checked (errcheck)
}

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() {
Expand Down Expand Up @@ -145,6 +159,7 @@ func noOSDecoratedWindowsConfig() g.MasterWindowFlags {
io.SetConfigViewportsNoAutoMerge(true)
io.SetConfigViewportsNoDefaultParent(true)
io.SetConfigWindowsMoveFromTitleBarOnly(true)

return g.MasterWindowFlagsHidden | g.MasterWindowFlagsTransparent | g.MasterWindowFlagsFrameless
}

Expand All @@ -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
Expand All @@ -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)
}
Expand All @@ -185,3 +202,5 @@ func main() {
wnd.SetTargetFPS(60)
wnd.Run(loop)
}

/**/
3 changes: 3 additions & 0 deletions examples/loadimageAdvanced/loadimageAdvanced.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (

func loop() {
var start_pos image.Point

g.SingleWindow().Layout(
g.Custom(func() {
start_pos = g.GetCursorScreenPos()
Expand Down Expand Up @@ -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)

Check failure on line 79 in examples/loadimageAdvanced/loadimageAdvanced.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `fromfile.SetSurfaceFromFile` is not checked (errcheck)
fromrgba.SetSurfaceFromRGBA(rgba, false)

Check failure on line 80 in examples/loadimageAdvanced/loadimageAdvanced.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `fromrgba.SetSurfaceFromRGBA` is not checked (errcheck)
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)

Check failure on line 81 in examples/loadimageAdvanced/loadimageAdvanced.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `fromurl.SetSurfaceFromURL` is not checked (errcheck)
Expand Down
33 changes: 33 additions & 0 deletions examples/paint/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
func FlushDrawCommands(c *Canvas) {
bcopy := append(buffer)

Check failure on line 26 in examples/paint/canvas.go

View workflow job for this annotation

GitHub Actions / Lint

appendAssign: append result not assigned to the same slice (gocritic)
go c.AppendDrawCommands(&bcopy)

buffer = nil
buffer = []DrawCommand{}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -80,6 +87,7 @@ func (c *Canvas) Compute() {
default:
}
}

_ = c.PushImageToBackend(false)
c.LastComputedLen = len(c.DrawCommands)
}
Expand All @@ -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)

Check failure on line 119 in examples/paint/canvas.go

View workflow job for this annotation

GitHub Actions / Lint

importShadow: shadow of imported package 'image' (gocritic)

err := backend.SetSurfaceFromRGBA(image, false)
if err != nil {
return nil, err
}

c := &Canvas{Image: image, Backend: backend}

return c, nil
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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(),
Expand Down
6 changes: 6 additions & 0 deletions examples/paint/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions examples/paint/paint.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func noOSDecoratedWindowsConfig() g.MasterWindowFlags {
io.SetConfigViewportsNoAutoMerge(true)
io.SetConfigViewportsNoDefaultParent(true)
io.SetConfigWindowsMoveFromTitleBarOnly(true)

return g.MasterWindowFlagsHidden | g.MasterWindowFlagsTransparent | g.MasterWindowFlagsFrameless
}

Expand Down
Loading

0 comments on commit 7e308af

Please sign in to comment.