hnnggg icons
This commit is contained in:
84
icons.go
84
icons.go
@@ -1,56 +1,66 @@
|
||||
package aluminum
|
||||
|
||||
import "fmt"
|
||||
import "io/fs"
|
||||
import "io"
|
||||
import "image"
|
||||
import _ "image/png"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/data"
|
||||
import "git.tebibyte.media/tomo/tomo/theme"
|
||||
import "git.tebibyte.media/tomo/tomo/canvas"
|
||||
|
||||
type iconSet struct {
|
||||
fs fs.FS
|
||||
icons map[theme.IconSize] map[string] canvas.Texture
|
||||
type iconIndex interface {
|
||||
~int | ~string
|
||||
}
|
||||
|
||||
func (this *iconSet) get (size theme.IconSize, name string) canvas.Texture {
|
||||
texture := this.icons[size][name]
|
||||
if texture != nil {
|
||||
return texture
|
||||
} else {
|
||||
texture = nil
|
||||
defer func () { this.icons[size][name] = texture } ()
|
||||
type iconEntry[T iconIndex] struct {
|
||||
position image.Point
|
||||
index T
|
||||
}
|
||||
|
||||
file, err := this.fs.Open(fmt.Sprint(size, "/", name, ".png"))
|
||||
if err != nil { return texture }
|
||||
defer file.Close()
|
||||
image, _, err := image.Decode(file)
|
||||
if err != nil { return texture }
|
||||
|
||||
texture = tomo.NewTexture(image)
|
||||
return texture
|
||||
func i[T iconIndex] (x, y int, index T) iconEntry[T] {
|
||||
return iconEntry[T] {
|
||||
position: image.Pt(x, y),
|
||||
index: index,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *iconSet) Icon (id theme.Icon, size theme.IconSize) canvas.Texture {
|
||||
return this.get(size, fmt.Sprint("actions/", id))
|
||||
type iconSet[T iconIndex] struct {
|
||||
icons map[theme.IconSize] map[T] canvas.Texture
|
||||
}
|
||||
|
||||
func (this *iconSet) MimeIcon (mime data.Mime, size theme.IconSize) canvas.Texture {
|
||||
texture := this.get(size, fmt.Sprint("mime/", mime))
|
||||
if texture != nil {
|
||||
return texture
|
||||
} else {
|
||||
return this.get(size, fmt.Sprint("mime-generic/", mime.Type))
|
||||
}
|
||||
func (this *iconSet[T]) Get (index T,size theme.IconSize) canvas.Texture {
|
||||
return this.icons[size][index]
|
||||
}
|
||||
|
||||
func (this *iconSet) ApplicationIcon (id theme.ApplicationIcon, size theme.IconSize) canvas.Texture {
|
||||
texture := this.get(size, fmt.Sprint("app/", id.Name))
|
||||
if texture != nil {
|
||||
return texture
|
||||
} else {
|
||||
return this.get(size, fmt.Sprint("app-generic/", id.Role))
|
||||
func (this *iconSet[T]) Init (
|
||||
small, medium, large io.Reader,
|
||||
rows, columns int,
|
||||
icons ...iconEntry[T],
|
||||
) {
|
||||
this.icons = make(map[theme.IconSize] map[T] canvas.Texture)
|
||||
this.initFor(theme.IconSizeSmall, small, rows, columns, icons...)
|
||||
this.initFor(theme.IconSizeMedium, medium, rows, columns, icons...)
|
||||
this.initFor(theme.IconSizeLarge, large, rows, columns, icons...)
|
||||
}
|
||||
|
||||
func (this *iconSet[T]) initFor (
|
||||
size theme.IconSize,
|
||||
file io.Reader,
|
||||
rows, columns int,
|
||||
icons ...iconEntry[T],
|
||||
) {
|
||||
if file == nil { return }
|
||||
|
||||
atlasImage, _, err := image.Decode(file)
|
||||
if err != nil { panic(err) }
|
||||
atlas := tomo.NewTexture(atlasImage)
|
||||
cellW := atlasImage.Bounds().Dx() / columns
|
||||
cellH := atlasImage.Bounds().Dy() / rows
|
||||
cellSize := image.Rect(0, 0, cellW, cellH)
|
||||
|
||||
this.icons[size] = make(map[T] canvas.Texture)
|
||||
for _, icon := range icons {
|
||||
offset := image.Pt (
|
||||
icon.position.X * cellW,
|
||||
icon.position.Y * cellH)
|
||||
this.icons[size][icon.index] = atlas.Clip(cellSize.Add(offset))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user