/
bkg.go
161 lines (143 loc) · 4.16 KB
/
bkg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package element
import (
"encoding/xml"
"github.com/bhollier/ui/pkg/ui/util"
"github.com/faiface/pixel"
"github.com/srwiley/oksvg"
"image/color"
"path/filepath"
"strings"
)
// Implementation of the image
// interface for a background
type Background struct {
// The background field
// from xml
Field string `uixml:"http://github.com/bhollier/ui/api/schema background,optional"`
// The background's scale option
Scale util.ScaleOption `uixml:"http://github.com/bhollier/ui/api/schema bkg-scale,optional"`
// The background's colour
Color color.RGBA `uixml:"http://github.com/bhollier/ui/api/schema bkg-color,optional"`
// The background's svg (if
// applicable)
svg *oksvg.SvgIcon
// The element's background
// sprite
sprite *pixel.Sprite
}
// Function to get the background's
// XML field
func (b *Background) GetField() string { return b.Field }
// Function to get the background's
// scale option
func (b *Background) GetScale() util.ScaleOption { return b.Scale }
// Function to get the background's
// colour
func (b *Background) GetColor() color.RGBA { return b.Color }
// Function to determine whether
// the background looks like an SVG
func (b *Background) IsSVG() bool {
// If the image field isn't empty
return b.GetField() != "" &&
// And the extension is ".svg"
strings.ToLower(filepath.Ext(b.GetField())) == ".svg"
}
// Function to get the background's
// SVG
func (b *Background) GetSVG() *oksvg.SvgIcon { return b.svg }
// Function to set the background's
// SVG
func (b *Background) SetSVG(s *oksvg.SvgIcon) { b.svg = s }
// Function to get the background's
// sprite
func (b *Background) GetSprite() *pixel.Sprite { return b.sprite }
// Function to set the background's
// sprite
func (b *Background) SetSprite(s *pixel.Sprite) { b.sprite = s }
// Function to unmarshal an XML element into
// an element. SetAttrs should've been called
// before this function
func (b *Background) UnmarshalXML(*xml.Decoder, xml.StartElement) (err error) {
// If the scale wasn't given
if b.Scale == util.ZeroScaleOption {
// Default for a background is scale to fill
b.Scale = util.ScaleToFill
}
return nil
}
// Function to reset the background
func (b *Background) Reset() {
// If the image is an SVG
if b.IsSVG() {
// Reset the sprite
b.SetSprite(nil)
}
}
// Function to determine whether the
// background has been initialised, by
// whether its background sprite has been
// set (assuming it's meant to be set).
// This function doesn't call
// element.IsInitialised
func (b *Background) IsInitialised() bool {
// If the element doesn't have a background
return b.GetField() == "" ||
// Or the background has been initialised
b.GetSprite() != nil
}
// Function to initialise an element's
// background. Doesn't call element.Init
func InitBkg(e Element, i Image) error {
// If the image looks like an SVG
if i.IsSVG() {
// If the SVG hasn't been loaded yet
if i.GetSVG() == nil {
// Load the svg
svg, err := util.LoadSVG(
e.GetFS(), i.GetField(), i.GetColor())
if err != nil {
return err
}
// Set the SVG
i.SetSVG(svg)
}
// If the svg has been loaded
if i.GetSVG() != nil {
// If the image hasn't been created
// and the width and height are known
if i.GetSprite() == nil &&
e.GetActualWidth() != nil &&
e.GetActualHeight() != nil {
// Create a picture from the SVG
pic := util.CreatePictureFromSVG(i.GetSVG(), i.GetScale(),
*e.GetActualWidth(), *e.GetActualHeight())
// Create a sprite with the picture and set it
i.SetSprite(pixel.NewSprite(pic, pic.Bounds()))
}
}
return nil
}
// If the image hasn't been made yet
if i.GetSprite() == nil {
// Load the image
picture, err := util.CreatePictureFromField(e.GetFS(), i.GetField())
if err != nil {
return err
}
if picture != nil {
i.SetSprite(pixel.NewSprite(picture, picture.Bounds()))
}
}
return nil
}
// Function to draw the background
// of an element. This function
// should be called first
func DrawBkg(e Element, b Image) {
// Clear the canvas (with a transparent background)
// (this is why it's important the background is
// drawn first)
e.GetCanvas().Clear(color.Transparent)
// Draw the background
DrawImage(e, b)
}