TSS now supports PNG image textures
This commit is contained in:
parent
24357bf19f
commit
77e335a238
@ -1,32 +1,36 @@
|
||||
package tss
|
||||
|
||||
import "os"
|
||||
import "io"
|
||||
import "fmt"
|
||||
import "image"
|
||||
import "errors"
|
||||
import "image/color"
|
||||
import _ "image/png"
|
||||
import "path/filepath"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/event"
|
||||
import "git.tebibyte.media/tomo/tomo/canvas"
|
||||
import "git.tebibyte.media/tomo/backend/style"
|
||||
|
||||
type styleBuilder struct {
|
||||
sheet Sheet
|
||||
}
|
||||
|
||||
type faceKey struct {
|
||||
name string
|
||||
size int
|
||||
sheet Sheet
|
||||
workingDir string
|
||||
textures map[string] canvas.TextureCloser
|
||||
}
|
||||
|
||||
// BuildStyle builds a Tomo style from the specified sheet. Resources associated
|
||||
// with it (such as textures) can be freed by closing the returned cookie.
|
||||
func BuildStyle (sheet Sheet) (*style.Style, event.Cookie, error) {
|
||||
builder := &styleBuilder {
|
||||
sheet: sheet,
|
||||
sheet: sheet,
|
||||
workingDir: filepath.Dir(sheet.Path),
|
||||
}
|
||||
return builder.build()
|
||||
}
|
||||
|
||||
func (this *styleBuilder) build () (*style.Style, event.Cookie, error) {
|
||||
// ensure the sheet is flattened (all vars are resolved)
|
||||
err := this.sheet.Flatten()
|
||||
if err != nil { return nil, nil, err }
|
||||
|
||||
@ -41,6 +45,7 @@ func (this *styleBuilder) build () (*style.Style, event.Cookie, error) {
|
||||
return color.RGBA { R: 255, B: 255, A: 255 }
|
||||
}
|
||||
|
||||
// construct style and get colors
|
||||
cookies := []event.Cookie { }
|
||||
sty := &style.Style {
|
||||
Rules: make([]style.Rule, len(this.sheet.Rules)),
|
||||
@ -53,6 +58,7 @@ func (this *styleBuilder) build () (*style.Style, event.Cookie, error) {
|
||||
},
|
||||
}
|
||||
|
||||
// build style rules from the sheet's rule slice
|
||||
for index, rule := range this.sheet.Rules {
|
||||
styleRule := style.Rule {
|
||||
Role: tomo.Role {
|
||||
@ -73,8 +79,10 @@ func (this *styleBuilder) build () (*style.Style, event.Cookie, error) {
|
||||
sty.Rules[index] = styleRule
|
||||
}
|
||||
|
||||
// TODO include all the faces in this.faces (ONCE EACH) in the
|
||||
// multicookie
|
||||
// add each texture to the cookies list
|
||||
for _, texture := range this.textures {
|
||||
cookies = append(cookies, closerCookie { Closer: texture })
|
||||
}
|
||||
|
||||
return sty, event.MultiCookie(cookies...), nil
|
||||
}
|
||||
@ -166,7 +174,14 @@ func (this *styleBuilder) buildAttr (name string, attr []ValueList) (tomo.Attr,
|
||||
return nil, nil, errWrongType()
|
||||
|
||||
case "Texture":
|
||||
// TODO load image from file
|
||||
err := expectSingleSingle()
|
||||
if err != nil { return nil, nil, err }
|
||||
if name, ok := attr[0][0].(ValueString); ok {
|
||||
texture, err := this.texture(string(name))
|
||||
if err != nil { return nil, nil, err }
|
||||
return tomo.ATexture(texture), nil, nil
|
||||
}
|
||||
return nil, nil, errWrongType()
|
||||
|
||||
case "TextureMode":
|
||||
err := expectSingleSingle()
|
||||
@ -306,6 +321,19 @@ func (this *styleBuilder) buildAttr (name string, attr []ValueList) (tomo.Attr,
|
||||
return nil, nil, errors.New(fmt.Sprintf("unimplemented attribute name %s", name))
|
||||
}
|
||||
|
||||
func (this *styleBuilder) texture (path string) (canvas.TextureCloser, error) {
|
||||
path = filepath.Join(this.workingDir, path)
|
||||
if texture, ok := this.textures[path]; ok {
|
||||
return texture, nil
|
||||
}
|
||||
file, err := os.Open(path)
|
||||
if err != nil { return nil, err }
|
||||
defer file.Close()
|
||||
rawImage, _, err := image.Decode(file)
|
||||
if err != nil { return nil, err }
|
||||
return tomo.NewTexture(rawImage), nil
|
||||
}
|
||||
|
||||
func buildAttrBorder (attr []ValueList) (tomo.Attr, error) {
|
||||
borders := make([]tomo.Border, len(attr))
|
||||
for index, list := range attr {
|
||||
@ -371,3 +399,9 @@ func copyBorderValue[T any, U ~[]T] (destination, source U) bool {
|
||||
}
|
||||
}
|
||||
|
||||
type closerCookie struct {
|
||||
io.Closer
|
||||
}
|
||||
func (cookie closerCookie) Close () {
|
||||
cookie.Closer.Close()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user