backend/internal/system/system.go

67 lines
1.7 KiB
Go
Raw Normal View History

2024-06-01 14:39:14 -06:00
package system
2024-06-02 11:34:11 -06:00
import "io"
2024-06-01 14:39:14 -06:00
import "image"
2024-06-11 16:12:47 -06:00
import "git.tebibyte.media/tomo/tomo"
2024-06-01 14:39:14 -06:00
import "git.tebibyte.media/tomo/tomo/canvas"
2024-06-11 16:12:47 -06:00
import "git.tebibyte.media/tomo/backend/internal/util"
2024-06-01 14:39:14 -06:00
2024-06-02 11:23:03 -06:00
// System is coupled to a tomo.Backend implementation, and manages Hierarchies
// and Boxes.
2024-06-01 14:39:14 -06:00
type System struct {
2024-06-11 22:39:00 -06:00
link BackendLink
2024-07-25 11:01:15 -06:00
style *tomo.Style
2024-06-11 22:39:00 -06:00
styleNonce int
iconsNonce int
2024-06-11 16:12:47 -06:00
hierarchies util.Set[*Hierarchy]
2024-06-01 14:39:14 -06:00
}
2024-06-02 11:33:59 -06:00
// BackendLink allows the System to call up into the tomo.Backend implementation
2024-06-02 11:23:03 -06:00
// which contains it in order to do things such as create new textures.
2024-06-01 14:39:14 -06:00
type BackendLink interface {
2024-06-02 11:33:59 -06:00
// NewTexture creates a new texture from an image.
2024-06-01 14:39:14 -06:00
NewTexture (image.Image) canvas.TextureCloser
2024-06-02 11:33:59 -06:00
// NewSurface creates a new surface with the specified bounds.
NewSurface (image.Rectangle) (SurfaceLink, error)
2024-06-01 14:39:14 -06:00
}
2024-06-02 11:23:03 -06:00
// SurfaceLink wraps a Surface created by the backend implementation, allowing
// the System a higher level of control over it.
2024-06-01 14:39:14 -06:00
type SurfaceLink interface {
2024-06-02 11:34:11 -06:00
io.Closer
2024-06-01 14:39:14 -06:00
GetSurface () any
SetSize (image.Rectangle)
}
2024-06-02 11:23:03 -06:00
// New creates a new System.
2024-06-01 14:39:14 -06:00
func New (link BackendLink) *System {
return &System {
2024-06-11 16:12:47 -06:00
link: link,
hierarchies: make(util.Set[*Hierarchy]),
2024-06-01 14:39:14 -06:00
}
}
2024-06-11 16:12:47 -06:00
// SetStyle sets the tomo.Style that is applied to objects, and notifies them
// that the style has changed.
2024-07-25 11:01:15 -06:00
func (this *System) SetStyle (style *tomo.Style) {
2024-06-11 22:39:00 -06:00
this.style = style
this.styleNonce ++
2024-06-11 16:12:47 -06:00
for hierarchy := range this.hierarchies {
2024-06-11 22:39:00 -06:00
hierarchy.setStyle()
2024-06-11 16:12:47 -06:00
}
}
2024-07-25 11:01:15 -06:00
// SetIconSet notifies objects that the icons have changed.
func (this *System) SetIconSet (iconSet tomo.IconSet) {
2024-06-11 22:39:00 -06:00
this.iconsNonce ++
2024-06-11 16:12:47 -06:00
for hierarchy := range this.hierarchies {
2024-07-25 11:01:15 -06:00
hierarchy.setIconSet()
2024-06-11 16:12:47 -06:00
}
}
func (this *System) removeHierarchy (hierarchy *Hierarchy) {
delete(this.hierarchies, hierarchy)
}