Skip to content

Commit

Permalink
Merge pull request #237 from gucio321/image-button-rgba
Browse files Browse the repository at this point in the history
widgets: add ImageButtonWithRgba
  • Loading branch information
AllenDang authored Jul 27, 2021
2 parents ca5ced4 + 2399f9b commit 5e3ec87
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions Widgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,78 @@ func ImageButton(texture *Texture) *ImageButtonWidget {
}
}

type ImageButtonWithRgbaWidget struct {
*ImageButtonWidget
rgba *image.RGBA
id string
}

func ImageButtonWithRgba(rgba *image.RGBA) *ImageButtonWithRgbaWidget {
// Generate a unique id from first 100 pix from rgba
var pix []uint8
if len(rgba.Pix) >= 100 {
pix = rgba.Pix[:100]
} else {
pix = rgba.Pix
}

return &ImageButtonWithRgbaWidget{
ImageButtonWidget: ImageButton(nil),
rgba: rgba,
id: fmt.Sprintf("ImageButtonWithRgba_%v", pix),
}
}

func (b *ImageButtonWithRgbaWidget) Size(width, height float32) *ImageButtonWithRgbaWidget {
b.ImageButtonWidget.Size(width, height)
return b
}

func (b *ImageButtonWithRgbaWidget) OnClick(onClick func()) *ImageButtonWithRgbaWidget {
b.ImageButtonWidget.OnClick(onClick)
return b
}

func (b *ImageButtonWithRgbaWidget) UV(uv0, uv1 image.Point) *ImageButtonWithRgbaWidget {
b.ImageButtonWidget.UV(uv0, uv1)
return b
}

func (b *ImageButtonWithRgbaWidget) BgColor(bgColor color.RGBA) *ImageButtonWithRgbaWidget {
b.ImageButtonWidget.BgColor(bgColor)
return b
}

func (b *ImageButtonWithRgbaWidget) TintColor(tintColor color.RGBA) *ImageButtonWithRgbaWidget {
b.ImageButtonWidget.TintColor(tintColor)
return b
}

func (b *ImageButtonWithRgbaWidget) FramePadding(padding int) *ImageButtonWithRgbaWidget {
b.ImageButtonWidget.FramePadding(padding)
return b
}

func (i *ImageButtonWithRgbaWidget) Build() {
state := Context.GetState(i.id)

if state == nil {
Context.SetState(i.id, &ImageState{})

go func() {
texture, err := NewTextureFromRgba(i.rgba)
if err == nil {
Context.SetState(i.id, &ImageState{texture: texture})
}
}()
} else {
imgState := state.(*ImageState)
i.ImageButtonWidget.texture = imgState.texture
}

i.ImageButtonWidget.Build()
}

type CheckboxWidget struct {
text string
selected *bool
Expand Down

0 comments on commit 5e3ec87

Please sign in to comment.