-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.go
61 lines (53 loc) · 1.53 KB
/
files.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
package main
import (
"github.com/dresswithpockets/go-vgui"
"github.com/faiface/pixel"
"image"
"os"
)
type PictureProvider interface {
getPicture(path string) (pixel.Picture, error)
}
type VguiProvider interface {
getObject(path string) (*vgui.Object, error)
}
type SourcePictureProvider struct {
pictures map[string]pixel.Picture
fileSourceProvider vgui.FileSourceProvider
}
type SourceVguiProvider struct {
roots map[string]*vgui.Object
fileSourceProvider vgui.FileSourceProvider
}
// getPicture returns a pixel.Picture from the path, and caches it in pictures.
// If the pixel.Picture is not already loaded & mapped to the path, it will load it according to fileSourceProvider
func (p *SourcePictureProvider) getPicture(path string) (pixel.Picture, error) {
abs, err := p.fileSourceProvider.GetAbsolute(path)
if err != nil {
return nil, err
}
if pic, ok := p.pictures[abs]; ok {
return pic, nil
}
pic, err := loadPicture(abs)
if err != nil {
return nil, err
}
p.pictures[abs] = pic
return pic, nil
}
func (p * SourceVguiProvider) getObject(path string) (*vgui.Object, error) {
return vgui.FromFileSourceProvider(path, p.fileSourceProvider)
}
func loadPicture(path string) (pixel.Picture, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
return pixel.PictureDataFromImage(img), nil
}