deduplicated some code

This commit is contained in:
Thomas Friedel 2018-03-27 16:03:02 +02:00
parent 37767fa86f
commit df650c6113
2 changed files with 18 additions and 21 deletions

View file

@ -299,13 +299,8 @@ func parseStyle(value ...interface{}) drawStyle {
case *RadialGradient:
style.radialGradient = v
return style
case *Image:
style.image = v
return style
case string:
if img, ok := images[v]; ok {
style.image = img
}
case *Image, string:
style.image = getImage(v)
}
}
c, ok := parseColor(value...)

View file

@ -72,6 +72,21 @@ func LoadImage(src interface{}, name string) (*Image, error) {
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) {
img := &Image{w: src.Bounds().Dx(), h: src.Bounds().Dy()}
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
// source coordinates
func (cv *Canvas) DrawImage(image interface{}, coords ...float64) {
var img *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
}
}
}
img := getImage(image)
if img == nil {
return