tomo/attribute.go

279 lines
8.4 KiB
Go

package tomo
import "image"
import "image/color"
import "golang.org/x/image/font"
import "git.tebibyte.media/tomo/tomo/canvas"
// AttrSet is a set of attributes wherein only one/zero of each attribute type
// can exist. Its zero value can be used safely, and you can copy it if you
// want, but it will point to the same set of attributes.
type AttrSet struct {
set map[int] Attr
}
// AS builds an AttrSet out of a vararg list of Attr values.
func AS (attrs ...Attr) AttrSet {
set := AttrSet { }
set.Add(attrs...)
return set
}
// Add adds attributes to the set.
func (this *AttrSet) Add (attrs ...Attr) {
this.ensure()
for _, attr := range attrs {
this.set[attr.attr()] = attr
}
}
// MergeUnder takes attributes from another set and adds them if they don't
// already exist in this one.
func (this *AttrSet) MergeUnder (other AttrSet) {
this.ensure()
if other.set == nil { return }
for _, attr := range other.set {
if _, exists := this.set[attr.attr()]; !exists {
this.Add(attr)
}
}
}
// MergeOver takes attributes from another set and adds them, overriding this
// one.
func (this *AttrSet) MergeOver (other AttrSet) {
this.ensure()
if other.set == nil { return }
for _, attr := range other.set {
this.Add(attr)
}
}
func (this *AttrSet) ensure () {
if this.set == nil { this.set = make(map[int] Attr) }
}
// Attr modifies one thing about a box's style.
type Attr interface {
// Equals returns true if both attributes can reasonably be declared
// equal.
Equals (Attr) bool
attr () int
}
// AttrColor sets the background color of a box.
type AttrColor struct { color.Color }
// AttrTexture sets the texture of a box to a named texture.
type AttrTexture struct { canvas.Texture }
// AttrTextureMode sets the rendering mode of a box's texture.
type AttrTextureMode TextureMode
// AttrBorder sets the border of a box.
type AttrBorder []Border
// AttrMinimumSize sets the minimum size of a box.
type AttrMinimumSize image.Point
// AttrPadding sets the inner padding of a box.
type AttrPadding Inset
// AttrGap sets the gap between child boxes, if the box is a ContainerBox.
type AttrGap image.Point
// AttrTextColor sets the text color, if the box is a TextBox.
type AttrTextColor struct { color.Color }
// AttrDotColor sets the text selection color, if the box is a TextBox.
type AttrDotColor struct { color.Color }
// AttrFace sets the font face, if the box is a TextBox.
type AttrFace struct { font.Face }
// AttrAlign sets the alignment, if the box is a ContentBox.
type AttrAlign struct { X, Y Align }
// AttrOverflow sets the overflow, if the box is a ContentBox.
type AttrOverflow struct { X, Y bool }
// AttrLayout sets the layout, if the box is a ContentBox.
type AttrLayout struct { Layout }
// AColor is a convenience constructor for the color attribute.
func AColor (col color.Color) AttrColor {
return AttrColor { Color: col }
}
// ATexture is a convenience constructor for the texture attribute.
func ATexture (texture canvas.Texture) AttrTexture {
return AttrTexture { Texture: texture }
}
// ATextureMode is a convenience constructor for the texture mode attribute.
func ATextureMode (mode TextureMode) AttrTextureMode {
return AttrTextureMode(mode)
}
// ABorder is a convenience constructor for the border attribute.
func ABorder (borders ...Border) AttrBorder {
return AttrBorder(borders)
}
// AMinimumSize is a convenience constructor for the minimum size attribute.
func AMinimumSize (x, y int) AttrMinimumSize {
return AttrMinimumSize(image.Pt(x, y))
}
// APadding is a convenience constructor for the padding attribute.
func APadding (sides ...int) AttrPadding {
return AttrPadding(I(sides...))
}
// AGap is a convenience constructor for the gap attribute.
func AGap (x, y int) AttrGap {
return AttrGap(image.Pt(x, y))
}
// ATextColor is a convenience constructor for the text color attribute.
func ATextColor (col color.Color) AttrTextColor {
return AttrTextColor { Color: col }
}
// ADotColor is a convenience constructor for the dot color attribute.
func ADotColor (col color.Color) AttrDotColor {
return AttrDotColor { Color: col }
}
// AFace is a convenience constructor for the face attribute.
func AFace (face font.Face) AttrFace {
return AttrFace { Face: face }
}
// AAlign is a convenience constructor for the align attribute.
func AAlign (x, y Align) AttrAlign {
return AttrAlign { X: x, Y: y }
}
// AOverflow is a convenience constructor for the overflow attribute.
func AOverflow (x, y bool) AttrOverflow {
return AttrOverflow { X: x, Y: y }
}
// ALayout is a convenience constructor for the overflow attribute.
func ALayout (layout Layout) AttrLayout {
return AttrLayout { Layout: layout }
}
// Equals returns true if both attributes can reasonably be declared equal.
func (this AttrColor) Equals (other Attr) bool {
if other, ok := other.(AttrColor); ok {
return this == other
} else {
return false
}
}
// Equals returns true if both attributes can reasonably be declared equal.
func (this AttrTexture) Equals (other Attr) bool {
if other, ok := other.(AttrTexture); ok {
return this == other
} else {
return false
}
}
// Equals returns true if both attributes can reasonably be declared equal.
func (this AttrTextureMode) Equals (other Attr) bool {
if other, ok := other.(AttrTextureMode); ok {
return this == other
} else {
return false
}
}
// Equals returns true if both attributes can reasonably be declared equal.
func (this AttrBorder) Equals (other Attr) bool {
if other, ok := other.(AttrBorder); ok {
if len(this) != len(other) { return false }
for index := range this {
thisBorder := this[index]
otherBorder := other[index]
if thisBorder != otherBorder { return false }
}
return true
} else {
return false
}
}
// Equals returns true if both attributes can reasonably be declared equal.
func (this AttrMinimumSize) Equals (other Attr) bool {
if other, ok := other.(AttrMinimumSize); ok {
return this == other
} else {
return false
}
}
// Equals returns true if both attributes can reasonably be declared equal.
func (this AttrPadding) Equals (other Attr) bool {
if other, ok := other.(AttrPadding); ok {
return this == other
} else {
return false
}
}
// Equals returns true if both attributes can reasonably be declared equal.
func (this AttrGap) Equals (other Attr) bool {
if other, ok := other.(AttrGap); ok {
return this == other
} else {
return false
}
}
// Equals returns true if both attributes can reasonably be declared equal.
func (this AttrTextColor) Equals (other Attr) bool {
if other, ok := other.(AttrTextColor); ok {
return this == other
} else {
return false
}
}
// Equals returns true if both attributes can reasonably be declared equal.
func (this AttrDotColor) Equals (other Attr) bool {
if other, ok := other.(AttrDotColor); ok {
return this == other
} else {
return false
}
}
// Equals returns true if both attributes can reasonably be declared equal.
func (this AttrFace) Equals (other Attr) bool {
if other, ok := other.(AttrFace); ok {
return this == other
} else {
return false
}
}
// Equals returns true if both attributes can reasonably be declared equal.
func (this AttrAlign) Equals (other Attr) bool {
if other, ok := other.(AttrAlign); ok {
return this == other
} else {
return false
}
}
// Equals returns true if both attributes can reasonably be declared equal.
func (this AttrOverflow) Equals (other Attr) bool {
if other, ok := other.(AttrOverflow); ok {
return this == other
} else {
return false
}
}
// Equals returns true if both attributes can reasonably be declared equal.
func (this AttrLayout) Equals (other Attr) bool {
if other, ok := other.(AttrLayout); ok {
return this == other
} else {
return false
}
}
// AttrNumber returns the number of an attribute. Each attribute type has a
// unique number. The exact values of these numbers are not part of the API and
// may change.
func AttrNumber (attr Attr) int {
return attr.attr()
}
func (AttrColor) attr () int { return 0 }
func (AttrTexture) attr () int { return 1 }
func (AttrTextureMode) attr () int { return 2 }
func (AttrBorder) attr () int { return 3 }
func (AttrMinimumSize) attr () int { return 4 }
func (AttrPadding) attr () int { return 5 }
func (AttrGap) attr () int { return 6 }
func (AttrTextColor) attr () int { return 7 }
func (AttrDotColor) attr () int { return 8 }
func (AttrFace) attr () int { return 9 }
func (AttrAlign) attr () int { return 10 }
func (AttrOverflow) attr () int { return 11 }
func (AttrLayout) attr () int { return 12 }