TSS now supports PNG image textures

This commit is contained in:
Sasha Koshka 2024-08-12 20:16:29 -04:00
parent 24357bf19f
commit 77e335a238

View File

@ -1,32 +1,36 @@
package tss package tss
import "os"
import "io"
import "fmt" import "fmt"
import "image" import "image"
import "errors" import "errors"
import "image/color" import "image/color"
import _ "image/png"
import "path/filepath"
import "git.tebibyte.media/tomo/tomo" import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/event" import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/tomo/canvas"
import "git.tebibyte.media/tomo/backend/style" import "git.tebibyte.media/tomo/backend/style"
type styleBuilder struct { type styleBuilder struct {
sheet Sheet sheet Sheet
} workingDir string
textures map[string] canvas.TextureCloser
type faceKey struct {
name string
size int
} }
// BuildStyle builds a Tomo style from the specified sheet. Resources associated // BuildStyle builds a Tomo style from the specified sheet. Resources associated
// with it (such as textures) can be freed by closing the returned cookie. // with it (such as textures) can be freed by closing the returned cookie.
func BuildStyle (sheet Sheet) (*style.Style, event.Cookie, error) { func BuildStyle (sheet Sheet) (*style.Style, event.Cookie, error) {
builder := &styleBuilder { builder := &styleBuilder {
sheet: sheet, sheet: sheet,
workingDir: filepath.Dir(sheet.Path),
} }
return builder.build() return builder.build()
} }
func (this *styleBuilder) build () (*style.Style, event.Cookie, error) { func (this *styleBuilder) build () (*style.Style, event.Cookie, error) {
// ensure the sheet is flattened (all vars are resolved)
err := this.sheet.Flatten() err := this.sheet.Flatten()
if err != nil { return nil, nil, err } 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 } return color.RGBA { R: 255, B: 255, A: 255 }
} }
// construct style and get colors
cookies := []event.Cookie { } cookies := []event.Cookie { }
sty := &style.Style { sty := &style.Style {
Rules: make([]style.Rule, len(this.sheet.Rules)), 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 { for index, rule := range this.sheet.Rules {
styleRule := style.Rule { styleRule := style.Rule {
Role: tomo.Role { Role: tomo.Role {
@ -73,8 +79,10 @@ func (this *styleBuilder) build () (*style.Style, event.Cookie, error) {
sty.Rules[index] = styleRule sty.Rules[index] = styleRule
} }
// TODO include all the faces in this.faces (ONCE EACH) in the // add each texture to the cookies list
// multicookie for _, texture := range this.textures {
cookies = append(cookies, closerCookie { Closer: texture })
}
return sty, event.MultiCookie(cookies...), nil return sty, event.MultiCookie(cookies...), nil
} }
@ -166,7 +174,14 @@ func (this *styleBuilder) buildAttr (name string, attr []ValueList) (tomo.Attr,
return nil, nil, errWrongType() return nil, nil, errWrongType()
case "Texture": 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": case "TextureMode":
err := expectSingleSingle() 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)) 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) { func buildAttrBorder (attr []ValueList) (tomo.Attr, error) {
borders := make([]tomo.Border, len(attr)) borders := make([]tomo.Border, len(attr))
for index, list := range 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()
}