-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
101 lines (88 loc) · 2.38 KB
/
image.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
package builtin
import (
"encoding/xml"
"errors"
"github.com/bhollier/ui/pkg/ui/element"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"net/http"
)
// Element type for an image
type Image struct {
// An image element is an
// element
element.Impl
// It also has an image
element.ImageImpl
}
// Function to create a new image
func NewImage(fs http.FileSystem, name xml.Name, parent element.Layout) element.Element {
return &Image{Impl: element.NewElement(fs, name, parent)}
}
// The XML name of the element
var ImageTypeName = xml.Name{Space: "http://github.com/bhollier/ui/api/schema", Local: "Image"}
// Function to unmarshal an XML element into
// an element. This function is usually only
// called by xml.Unmarshal
func (e *Image) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
// Unmarshal the element
err = e.Impl.UnmarshalXML(d, start)
if err != nil {
return err
}
// Set the element's attributes
err = element.SetAttrs(e, start.Attr)
if err != nil {
return err
}
return d.Skip()
}
// Function to reset the element
func (e *Image) Reset() {
e.Impl.Reset()
e.ImageImpl.Reset()
}
// Function to determine whether
// the element is initialised
func (e *Image) IsInitialised() bool {
// If the element is initialised
return e.Impl.IsInitialised() &&
// And the image has been initialised
e.ImageImpl.IsInitialised()
}
// Function to initialise the element
// (load textures, create sprites, set
// sprite locations, etc.)
func (e *Image) Init(window *pixelgl.Window, bounds *pixel.Rect) error {
// Initialise the element
err := e.Impl.Init(window, bounds)
if err != nil {
return err
}
// Initialise the image
err = element.InitImage(e, &e.ImageImpl)
if err != nil {
return err
}
// If no image was given
if e.ImageImpl.GetField() == "" {
// If it wants to match the content
if e.GetRelWidth().MatchContent {
return errors.New("invalid width attribute value 'match_content' on XML element '" +
element.FullName(e, ".", false) +
"': no content to match")
} else if e.GetRelHeight().MatchContent {
return errors.New("invalid height attribute value 'match_content' on XML element '" +
element.FullName(e, ".", false) +
"': no content to match")
}
}
return nil
}
// Function to draw the element
func (e *Image) Draw() {
// Draw the element
e.Impl.Draw()
// Draw the image
element.DrawImage(e, &e.ImageImpl)
}