2023-09-04 00:32:04 -06:00
|
|
|
package objects
|
|
|
|
|
|
|
|
import "git.tebibyte.media/tomo/tomo"
|
2024-08-11 20:36:10 -06:00
|
|
|
import "git.tebibyte.media/tomo/tomo/canvas"
|
2023-09-04 00:32:04 -06:00
|
|
|
|
2024-08-24 17:50:20 -06:00
|
|
|
var _ tomo.Object = new(Icon)
|
|
|
|
|
2023-09-04 00:32:04 -06:00
|
|
|
// Icon displays a single icon.
|
2024-08-25 00:36:05 -06:00
|
|
|
//
|
|
|
|
// Tags:
|
|
|
|
// - [large] The icon is large sized.
|
|
|
|
// - [medium] The icon is medium sized.
|
|
|
|
// - [small] The icon is small sized.
|
2023-09-04 00:32:04 -06:00
|
|
|
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
|
2023-09-04 00:32:04 -06:00
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-04 00:32:04 -06:00
|
|
|
// 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 {
|
2023-09-04 00:32:04 -06:00
|
|
|
this := &Icon {
|
2024-08-24 17:50:20 -06:00
|
|
|
box: tomo.NewBox(),
|
2023-09-04 00:32:04 -06:00
|
|
|
}
|
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)
|
2023-09-04 00:32:04 -06:00
|
|
|
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
|
|
|
}
|
2023-09-04 00:32:04 -06:00
|
|
|
}
|