deduplicated some code
This commit is contained in:
parent
37767fa86f
commit
df650c6113
2 changed files with 18 additions and 21 deletions
|
@ -299,13 +299,8 @@ func parseStyle(value ...interface{}) drawStyle {
|
||||||
case *RadialGradient:
|
case *RadialGradient:
|
||||||
style.radialGradient = v
|
style.radialGradient = v
|
||||||
return style
|
return style
|
||||||
case *Image:
|
case *Image, string:
|
||||||
style.image = v
|
style.image = getImage(v)
|
||||||
return style
|
|
||||||
case string:
|
|
||||||
if img, ok := images[v]; ok {
|
|
||||||
style.image = img
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c, ok := parseColor(value...)
|
c, ok := parseColor(value...)
|
||||||
|
|
30
images.go
30
images.go
|
@ -72,6 +72,21 @@ func LoadImage(src interface{}, name string) (*Image, error) {
|
||||||
return img, nil
|
return img, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getImage(image interface{}) *Image {
|
||||||
|
switch v := image.(type) {
|
||||||
|
case *Image:
|
||||||
|
return v
|
||||||
|
case string:
|
||||||
|
if img, ok := images[v]; ok {
|
||||||
|
return img
|
||||||
|
}
|
||||||
|
if img, err := LoadImage(v, v); err == nil {
|
||||||
|
return img
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func loadImageRGBA(src *image.RGBA) (*Image, error) {
|
func loadImageRGBA(src *image.RGBA) (*Image, error) {
|
||||||
img := &Image{w: src.Bounds().Dx(), h: src.Bounds().Dy()}
|
img := &Image{w: src.Bounds().Dx(), h: src.Bounds().Dy()}
|
||||||
gli.GenTextures(1, &img.tex)
|
gli.GenTextures(1, &img.tex)
|
||||||
|
@ -197,20 +212,7 @@ func (img *Image) Delete() {
|
||||||
// Where dx/dy/dw/dh are the destination coordinates and sx/sy/sw/sh are the
|
// Where dx/dy/dw/dh are the destination coordinates and sx/sy/sw/sh are the
|
||||||
// source coordinates
|
// source coordinates
|
||||||
func (cv *Canvas) DrawImage(image interface{}, coords ...float64) {
|
func (cv *Canvas) DrawImage(image interface{}, coords ...float64) {
|
||||||
var img *Image
|
img := getImage(image)
|
||||||
switch v := image.(type) {
|
|
||||||
case *Image:
|
|
||||||
img = v
|
|
||||||
case string:
|
|
||||||
if i, ok := images[v]; ok {
|
|
||||||
img = i
|
|
||||||
} else {
|
|
||||||
i, err := LoadImage(v, v)
|
|
||||||
if err == nil {
|
|
||||||
img = i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if img == nil {
|
if img == nil {
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue