objects/icon.go

61 lines
1.4 KiB
Go

package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/canvas"
var _ tomo.Object = new(Icon)
// Icon displays a single icon.
type Icon struct {
box tomo.Box
icon tomo.Icon
size tomo.IconSize
}
func iconSizeString (size tomo.IconSize) string {
switch size {
case tomo.IconSizeLarge: return "large"
case tomo.IconSizeMedium: return "medium"
default: return "small"
}
}
// NewIcon creates a new icon from an icon ID.
func NewIcon (icon tomo.Icon, size tomo.IconSize) *Icon {
this := &Icon {
box: tomo.NewBox(),
}
this.box.SetRole(tomo.R("objects", "Icon"))
this.SetIcon(icon, size)
this.box.OnIconSetChange(this.handleIconSetChange)
return this
}
// GetBox returns the underlying box.
func (this *Icon) GetBox () tomo.Box {
return this.box
}
// SetIcon sets the icon.
func (this *Icon) SetIcon (icon tomo.Icon, size tomo.IconSize) {
if this.icon == icon { return }
this.icon = icon
this.size = size
this.setTexture(icon.Texture(size))
}
func (this *Icon) handleIconSetChange () {
this.setTexture(this.icon.Texture(this.size))
}
func (this *Icon) setTexture (texture canvas.Texture) {
this.box.SetAttr(tomo.ATexture(texture))
this.box.SetAttr(tomo.ATextureMode(tomo.TextureModeCenter))
if texture == nil {
this.box.SetAttr(tomo.AMinimumSize(0, 0))
} else {
bounds := texture.Bounds()
this.box.SetAttr(tomo.AttrMinimumSize(bounds.Max.Sub(bounds.Min)))
}
}