1
1
package buttons
2
2
3
3
import (
4
+ "github.com/disintegration/gift"
4
5
"image"
5
6
"image/draw"
6
7
"os"
@@ -20,9 +21,10 @@ type ImageFileButton struct {
20
21
21
22
// GetImageForButton is the interface implemention to get the button's image as an image.Image
22
23
func (btn * ImageFileButton ) GetImageForButton (btnSize int ) image.Image {
23
- // TODO base the 96 on the image bounds
24
- newimg := image .NewRGBA (image .Rect (0 , 0 , btnSize , btnSize ))
25
- draw .Draw (newimg , newimg .Bounds (), btn .img , image.Point {0 , 0 }, draw .Src )
24
+ // Resize the image to what the button wants
25
+ g := gift .New (gift .Resize (btnSize , btnSize , gift .LanczosResampling ))
26
+ newimg := image .NewRGBA (image .Rect (0 , 0 , btn .img .Bounds ().Max .X , btn .img .Bounds ().Max .Y ))
27
+ g .Draw (newimg , btn .img )
26
28
return newimg
27
29
}
28
30
@@ -37,17 +39,17 @@ func (btn *ImageFileButton) GetButtonIndex() int {
37
39
}
38
40
39
41
// SetFilePath allows the image file to be changed on the fly
40
- func (btn * ImageFileButton ) SetFilePath (filePath string , btnSize int ) error {
42
+ func (btn * ImageFileButton ) SetFilePath (filePath string ) error {
41
43
btn .filePath = filePath
42
- err := btn .loadImage (btnSize )
44
+ err := btn .loadImage ()
43
45
if err != nil {
44
46
return err
45
47
}
46
48
btn .updateHandler (btn )
47
49
return nil
48
50
}
49
51
50
- func (btn * ImageFileButton ) loadImage (btnSize int ) error {
52
+ func (btn * ImageFileButton ) loadImage () error {
51
53
f , err := os .Open (btn .filePath )
52
54
if err != nil {
53
55
return err
@@ -58,8 +60,7 @@ func (btn *ImageFileButton) loadImage(btnSize int) error {
58
60
var newimg * image.RGBA
59
61
newimg , ok := img .(* image.RGBA )
60
62
if ! ok {
61
- // TODO base the 96 on the button size
62
- newimg = image .NewRGBA (image .Rect (0 , 0 , btnSize , btnSize ))
63
+ newimg = image .NewRGBA (image .Rect (0 , 0 , img .Bounds ().Max .X , img .Bounds ().Max .Y ))
63
64
draw .Draw (newimg , newimg .Bounds (), img , image.Point {0 , 0 }, draw .Src )
64
65
}
65
66
@@ -91,9 +92,9 @@ func (btn *ImageFileButton) Pressed() {
91
92
}
92
93
93
94
// NewImageFileButton creates a new ImageFileButton with the specified image on it
94
- func NewImageFileButton (filePath string , btnSize int ) (* ImageFileButton , error ) {
95
+ func NewImageFileButton (filePath string ) (* ImageFileButton , error ) {
95
96
btn := & ImageFileButton {filePath : filePath }
96
- err := btn .loadImage (btnSize )
97
+ err := btn .loadImage ()
97
98
if err != nil {
98
99
return nil , err
99
100
}
0 commit comments