Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add advanced image usage tools with examples #870

Merged
merged 5 commits into from
Oct 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
lint asyncimage
cjbrigato committed Oct 4, 2024
commit 44d5ad274625f2d6ee979a78494822a1b35a4a53
40 changes: 19 additions & 21 deletions examples/asyncimage/asyncimage.go
Original file line number Diff line number Diff line change
@@ -21,56 +21,56 @@ var (
imageScaleX = float32(1.0)
imageScaleY = float32(1.0)
linkedScale = true
dynamicImageUrl = "https://www.pngfind.com/pngs/b/465-4652097_gopher-png.png"
dynamicImageURL = "https://www.pngfind.com/pngs/b/465-4652097_gopher-png.png"
showWindow = true
wnd *g.MasterWindow
footerLabel string
inputRootfs = "."
)

func TextHeader(str string, col color.RGBA) *g.StyleSetter {
func textHeader(str string, col color.RGBA) *g.StyleSetter {
return g.Style().SetColor(g.StyleColorText, col).To(
g.Align(g.AlignCenter).To(
g.Label(str),
),
)
}

func CanLoadHeader() *g.AlignmentSetter {
func canLoadHeader() *g.AlignmentSetter {
return g.Align(g.AlignCenter).To(

g.Row(
g.InputText(&dynamicImageUrl),
g.InputText(&dynamicImageURL),
g.Button("Load Image from URL").OnClick(func() {
err := dynamicImage.ResetState()
if err == nil {
dynamicImage.SetSurfaceFromURL(dynamicImageUrl, time.Second*5, false)
_ = dynamicImage.SetSurfaceFromURL(dynamicImageURL, time.Second*5, false)
}
}),
))
}

func HeaderWidget() g.Widget {
func headerWidget() g.Widget {
if dynamicImage.GetState() == g.SurfaceStateLoading {
return TextHeader("Image Is Currently loading...", color.RGBA{0x80, 0x80, 0xFF, 255})
return textHeader("Image Is Currently loading...", color.RGBA{0x80, 0x80, 0xFF, 255})
}

return CanLoadHeader()
return canLoadHeader()
}

func FooterWidget(label string) g.Widget {
return g.Row(TextHeader(label, color.RGBA{0xFF, 0xFF, 0xFF, 255}))
func footerWidget(label string) g.Widget {
return g.Row(textHeader(label, color.RGBA{0xFF, 0xFF, 0xFF, 255}))
}

func ShouldReturnImage() g.Widget {
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 {
func shouldReturnPanel() g.Widget {
return g.Custom(func() {
imgui.SeparatorText("Image Scale")

@@ -104,11 +104,11 @@ func ShouldReturnPanel() g.Widget {
inputRootfs = "."
dynamicImage.SetFSRoot(inputRootfs)

dynamicImageUrl = "file:///files/sonic.png"
dynamicImageURL = "file:///files/sonic.png"
err := dynamicImage.ResetState()

if err == nil {
dynamicImage.SetSurfaceFromURL(dynamicImageUrl, time.Second*5, false)
_ = dynamicImage.SetSurfaceFromURL(dynamicImageURL, time.Second*5, false)
}

linkedScale = true
@@ -138,17 +138,17 @@ func loop() {

g.PushColorWindowBg(color.RGBA{30, 30, 30, 255})
g.Window("Async Images").IsOpen(&showWindow).Pos(10, 30).Size(1280, 720).Flags(g.WindowFlagsNoResize).Layout(
HeaderWidget(),
headerWidget(),
g.Separator(),
g.Row(
g.Child().Size(400, 625).Layout(
ShouldReturnPanel(),
shouldReturnPanel(),
),
g.Child().Flags(g.WindowFlagsHorizontalScrollbar).Size(-1, 625).Layout(
ShouldReturnImage(),
shouldReturnImage(),
)),
g.Separator(),
FooterWidget(footerLabel),
footerWidget(footerLabel),
)
g.PopStyleColor()
}
@@ -167,7 +167,7 @@ func initDynamicImage() error {
// SurfaceURL works from files:// too !
// Note : the "root" of the scheme is willingly the Executable / working directory
if err := fallback.SetSurfaceFromURL("file:///files/fallback.png", time.Second*5, false); err != nil {
return err
return fmt.Errorf("error at initDynamicImage: %w", err)
}

dynamicImage.OnReset(func() {
@@ -202,5 +202,3 @@ func main() {
wnd.SetTargetFPS(60)
wnd.Run(loop)
}

/**/