objects/icon.go

66 lines
1.5 KiB
Go
Raw Normal View History

package objects
import "git.tebibyte.media/tomo/tomo"
2024-08-11 20:36:10 -06:00
import "git.tebibyte.media/tomo/tomo/canvas"
2024-08-24 17:50:20 -06:00
var _ tomo.Object = new(Icon)
// Icon displays a single icon.
//
// Tags:
// - [large] The icon is large sized.
// - [medium] The icon is medium sized.
// - [small] The icon is small sized.
type Icon struct {
2024-08-24 17:50:20 -06:00
box tomo.Box
2024-08-11 20:36:10 -06:00
icon tomo.Icon
size tomo.IconSize
}
2024-07-21 09:48:28 -06:00
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.
2024-08-10 19:44:03 -06:00
func NewIcon (icon tomo.Icon, size tomo.IconSize) *Icon {
this := &Icon {
2024-08-24 17:50:20 -06:00
box: tomo.NewBox(),
}
2024-08-24 17:50:20 -06:00
this.box.SetRole(tomo.R("objects", "Icon"))
2024-08-10 19:44:03 -06:00
this.SetIcon(icon, size)
2024-08-24 17:50:20 -06:00
this.box.OnIconSetChange(this.handleIconSetChange)
return this
}
2024-08-24 17:50:20 -06:00
// GetBox returns the underlying box.
func (this *Icon) GetBox () tomo.Box {
return this.box
}
2024-08-10 19:44:03 -06:00
// SetIcon sets the icon.
func (this *Icon) SetIcon (icon tomo.Icon, size tomo.IconSize) {
2024-08-11 20:36:10 -06:00
if this.icon == icon { return }
this.icon = icon
2024-08-12 20:06:06 -06:00
this.size = size
2024-08-11 20:36:10 -06:00
this.setTexture(icon.Texture(size))
}
func (this *Icon) handleIconSetChange () {
this.setTexture(this.icon.Texture(this.size))
}
func (this *Icon) setTexture (texture canvas.Texture) {
2024-08-24 17:50:20 -06:00
this.box.SetAttr(tomo.ATexture(texture))
this.box.SetAttr(tomo.ATextureMode(tomo.TextureModeCenter))
2024-08-11 20:36:10 -06:00
if texture == nil {
2024-08-24 17:50:20 -06:00
this.box.SetAttr(tomo.AMinimumSize(0, 0))
2024-08-11 20:36:10 -06:00
} else {
bounds := texture.Bounds()
2024-08-24 17:50:20 -06:00
this.box.SetAttr(tomo.AttrMinimumSize(bounds.Max.Sub(bounds.Min)))
2024-08-11 20:36:10 -06:00
}
}