element

package
v0.0.0-...-e7ac65d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 1, 2021 License: MIT Imports: 21 Imported by: 5

Documentation

Index

Constants

View Source
const ButtonDefaultState = "default"

The default button state

View Source
const ButtonHoveredState = "hovered"

The button state when the mouse is over it

View Source
const ButtonPressedState = "pressed"

The button state for when the mouse is pressing the button

Variables

This section is empty.

Functions

func ButtonNewEvent

func ButtonNewEvent(e Button, window *pixelgl.Window)

Function to handle a button's new event

func CalculateHeight

func CalculateHeight(parent Element, window *pixelgl.Window,
	bounds *pixel.Rect, relHeight util.RelativeSize) (height *float64)

Function to calculate an element's height

func CalculateMin

func CalculateMin(e Element, bounds *pixel.Rect, size pixel.Vec) (min *pixel.Vec)

Function to calculate an element's minimum point

func CalculateWidth

func CalculateWidth(parent Element, window *pixelgl.Window,
	bounds *pixel.Rect, relWidth util.RelativeSize) (width *float64)

Function to calculate an element's width

func Call

func Call(name string, e Element) error

Function to call a callback

func ChildrenAreInitialised

func ChildrenAreInitialised(e Layout) bool

Function to determine whether a layout's children have been initialised. This function doesn't call element.IsInitialised

func DrawBkg

func DrawBkg(e Element, b Image)

Function to draw the background of an element. This function should be called first

func DrawCanvasOntoParent

func DrawCanvasOntoParent(child *pixelgl.Canvas, parent *pixelgl.Canvas)

Function to draw a canvas (usually an element's) onto a parent canvas. This function is generally used by layouts, or just anything that has child element(s)

func DrawImage

func DrawImage(e Element, i Image)

Function to draw an element's image

func DrawLayout

func DrawLayout(e Layout)

Function to draw a layout

func DrawText

func DrawText(e Element, t Text)

Function to draw an element's text

func DrawUI

func DrawUI(e Element, window *pixelgl.Window)

Function to draw the entire UI element tree, by traversing up the given element's parents

func FullName

func FullName(e Element, sep string, includeNamespace bool) (name string)

Function to get an element's full name, which includes its parent's and grandparent's etc. element names. The function calls any unknown elements "unknown" in the path

func InitBkg

func InitBkg(e Element, i Image) error

Function to initialise an element's background. Doesn't call element.Init

func InitImage

func InitImage(e Element, i Image) error

Function to initialise an element's image. Doesn't call element.Init

func InitText

func InitText(e Element, t Text) error

Function to initialise an element's text. Doesn't call element.Init

func InitUI

func InitUI(e Element, window *pixelgl.Window, bounds *pixel.Rect) error

Function to reset and initialise the entire UI element tree, by traversing up the given element's parents

func Name

func Name(e Element, includeNamespace bool) (name string)

Function to get an element's name. The function returns an empty string if the element isn't registered

func ParseAttr

func ParseAttr(t reflect.Type, v string) (reflect.Value, error)

Function to parse an attribute string into the given type

func Register

func Register(name xml.Name, reflectType reflect.Type, factory Factory)

Function to register a UI element type

func RegisterAttrType

func RegisterAttrType(t reflect.Type, p AttrParser)

Function to register an attribute type

func RegisterCallback

func RegisterCallback(name string, c Callback)

Function to register a new callback

func SetAttrs

func SetAttrs(e Element, attrs []xml.Attr) error

Function to parse the given xml attributes and set the fields of the given element using uixml tags. This function searches for tags recursively. It does not support arrays or maps

func XMLNameMatch

func XMLNameMatch(n1 xml.Name, n2 xml.Name) bool

Function to determine if two XML names match. Returns true if n1 and n2 match exactly or if n1.Space == "" and n1.Local and n2.Local match

func XMLNameToString

func XMLNameToString(name xml.Name) string

Function to convert an xml name to a string

Types

type AttrParser

type AttrParser func(attr string) (reflect.Value, error)

Type for an attribute parser

type Background

type Background struct {
	// The background field
	// from xml
	Field string `uixml:"http://github.com/bhollier/ui/api/schema background,optional"`
	// The background's scale option
	Scale util.ScaleOption `uixml:"http://github.com/bhollier/ui/api/schema bkg-scale,optional"`
	// The background's colour
	Color color.RGBA `uixml:"http://github.com/bhollier/ui/api/schema bkg-color,optional"`
	// contains filtered or unexported fields
}

Implementation of the image interface for a background

func (*Background) GetColor

func (b *Background) GetColor() color.RGBA

Function to get the background's colour

func (*Background) GetField

func (b *Background) GetField() string

Function to get the background's XML field

func (*Background) GetSVG

func (b *Background) GetSVG() *oksvg.SvgIcon

Function to get the background's SVG

func (*Background) GetScale

func (b *Background) GetScale() util.ScaleOption

Function to get the background's scale option

func (*Background) GetSprite

func (b *Background) GetSprite() *pixel.Sprite

Function to get the background's sprite

func (*Background) IsInitialised

func (b *Background) IsInitialised() bool

Function to determine whether the background has been initialised, by whether its background sprite has been set (assuming it's meant to be set). This function doesn't call element.IsInitialised

func (*Background) IsSVG

func (b *Background) IsSVG() bool

Function to determine whether the background looks like an SVG

func (*Background) Reset

func (b *Background) Reset()

Function to reset the background

func (*Background) SetSVG

func (b *Background) SetSVG(s *oksvg.SvgIcon)

Function to set the background's SVG

func (*Background) SetSprite

func (b *Background) SetSprite(s *pixel.Sprite)

Function to set the background's sprite

func (*Background) UnmarshalXML

func (b *Background) UnmarshalXML(*xml.Decoder, xml.StartElement) (err error)

Function to unmarshal an XML element into an element. SetAttrs should've been called before this function

type Button

type Button interface {
	// A button is an element
	Element

	// Function to get the button's
	// current state
	GetButtonState() ButtonState
	// Function to set the button's
	// current state
	SetButtonState(s ButtonState)

	// Function to get the button's
	// background field from XML for
	// the given state
	GetButtonBkgField(s ButtonState) string

	// Function to get the button's
	// background sprite for the given
	// state
	GetButtonBkg(s ButtonState) *pixel.Sprite
	// Function to set the button's
	// background sprite for the given
	// state
	SetButtonBkg(state ButtonState, sprite *pixel.Sprite)

	// Function to call the press callback
	CallPressCallback() error
}

Interface for a button element

type ButtonImpl

type ButtonImpl struct {
	// The button is an element
	Impl

	// The button's background when
	// being hovered over from XML
	HoveredBackground string `uixml:"http://github.com/bhollier/ui/api/schema bkg-hovered,optional"`
	// The button's background when
	// being pressed from XML
	PressedBackground string `uixml:"http://github.com/bhollier/ui/api/schema bkg-pressed,optional"`

	// The element's press callback
	PressCallback string `uixml:"http://github.com/bhollier/ui/api/schema press-callback,optional"`
	// contains filtered or unexported fields
}

Type for a button. Note, structs must either include ButtonImpl or Impl, not both

func NewButton

func NewButton(fs http.FileSystem, name xml.Name, parent Layout) ButtonImpl

Function to create a button element

func (*ButtonImpl) CallPressCallback

func (e *ButtonImpl) CallPressCallback() error

Function to call the press callback

func (*ButtonImpl) GetButtonBkg

func (e *ButtonImpl) GetButtonBkg(s ButtonState) *pixel.Sprite

Function to get the button's background sprite for the given state

func (*ButtonImpl) GetButtonBkgField

func (e *ButtonImpl) GetButtonBkgField(s ButtonState) string

Function to get the button's background field from XML for the given state

func (*ButtonImpl) GetButtonState

func (e *ButtonImpl) GetButtonState() ButtonState

Function to get the button's current state

func (*ButtonImpl) Init

func (e *ButtonImpl) Init(window *pixelgl.Window, bounds *pixel.Rect) error

Function to initialise the element

func (*ButtonImpl) IsInitialised

func (e *ButtonImpl) IsInitialised() bool

Function to determine whether the element is initialised

func (*ButtonImpl) SetButtonBkg

func (e *ButtonImpl) SetButtonBkg(state ButtonState, sprite *pixel.Sprite)

Function to set the button's background sprite for the given state

func (*ButtonImpl) SetButtonState

func (e *ButtonImpl) SetButtonState(s ButtonState)

Function to set the button's current state

type ButtonState

type ButtonState string

A button's state

type Callback

type Callback func(Element) error

Type for a callback

type Element

type Element interface {
	// Function to get the element's parent
	GetParent() Layout

	// Function to get the filesystem to use
	// for opening files
	GetFS() http.FileSystem

	// Function to get the element's XML name
	GetName() xml.Name
	// Function to get the element's XML
	// namespaces. This should include
	// GetName().Space
	GetNamespaces() []string
	// Function to add a namespace to the
	// element
	AddNamespace(string)

	// Function to get the element's
	// ID (or nil, if it doesn't have
	// one)
	GetID() *string

	// Function to get the element's
	// relative width
	GetRelWidth() util.RelativeSize
	// Function to get the element's
	// relative height
	GetRelHeight() util.RelativeSize

	// Function to get the element's
	// actual width. If nil, it isn't
	// known yet
	GetActualWidth() *float64
	// Function to set the element's
	// actual width
	SetActualWidth(*float64)

	// Function to get the element's
	// actual height. If nil, it isn't
	// known yet
	GetActualHeight() *float64
	// Function to set the element's
	// actual height
	SetActualHeight(*float64)

	// Function to get the element's
	// minimum point. If nil, it isn't
	// known yet
	GetMin() *pixel.Vec
	// Function to set the element's
	// minimum point
	SetMin(*pixel.Vec)

	// Function to get the element's
	// maximum point. If nil, it isn't
	// known yet
	GetMax() *pixel.Vec
	// Function to set the element's
	// maximum point
	SetMax(*pixel.Vec)

	// Function to get the element's
	// bounds. If nil, either the min
	// or max isn't known
	GetBounds() *pixel.Rect

	// Function to get the element's
	// padding
	GetPadding() util.AbsoluteQuantity

	// Function to get the element's
	// gravity
	GetGravity() util.Gravity

	// Function to get the element's
	// background
	GetBkg() Image

	// Function to get the element's
	// canvas
	GetCanvas() *pixelgl.Canvas

	// Function to unmarshal an XML element
	// into a UI element. This function is
	// usually only called by xml.Unmarshal
	UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

	// Function to reset the element's
	// position
	ResetPosition()

	// Function to reset the element
	// completely
	Reset()

	// Function to determine whether
	// the element is initialised
	IsInitialised() bool

	// Function to initialise the element
	// (load textures, create sprites, set
	// sprite locations, etc.)
	Init(window *pixelgl.Window, bounds *pixel.Rect) error

	// Function that is called when there
	// is a new event
	NewEvent(*pixelgl.Window)

	// Function to draw the element
	// to its canvas
	Draw()
}

Interface type for something that is an element

func ChildrenUnmarshalXML

func ChildrenUnmarshalXML(fs http.FileSystem, parent Layout, d *xml.Decoder,
	start xml.StartElement) ([]Element, error)

Function to unmarshal an XML element into a number of child elements. This function is usually only called by LayoutImpl.UnmarshalXML

func New

func New(fs http.FileSystem, name xml.Name, parent Layout) Element

Function to create an element with the given XML name. If none is found and name.Space is empty, the function will search for an element type with a matching name.Local and with a matching name.Space in parent.GetNamespaces(), then if still not found will search for an element type with a matching name.Local

type Factory

type Factory func(fs http.FileSystem, name xml.Name, parent Layout) Element

Type for an element factory

type Image

type Image interface {
	// Function to get the image's
	// XML field
	GetField() string
	// Function to get the image's
	// scale options
	GetScale() util.ScaleOption
	// Function to get the image's
	// colour
	GetColor() color.RGBA

	// Function to determine whether
	// the image looks like an SVG
	IsSVG() bool
	// Function to get the image's
	// SVG. Always returns nil if
	// IsSVG returns false
	GetSVG() *oksvg.SvgIcon
	// Function to set the image's
	// SVG
	SetSVG(*oksvg.SvgIcon)

	// Function to get the image's
	// sprite
	GetSprite() *pixel.Sprite
	// Function to set the image's
	// sprite
	SetSprite(*pixel.Sprite)
}

Interface for an image

type ImageImpl

type ImageImpl struct {
	// The image field from
	// xml
	Field string `uixml:"http://github.com/bhollier/ui/api/schema source,optional"`
	// The image's scale
	// option
	Scale util.ScaleOption `uixml:"http://github.com/bhollier/ui/api/schema scale,optional"`
	// The image's colour
	Color color.RGBA `uixml:"http://github.com/bhollier/ui/api/schema color,optional"`
	// contains filtered or unexported fields
}

Implementation of the image interface

func (*ImageImpl) GetColor

func (i *ImageImpl) GetColor() color.RGBA

Function to get the image's colour

func (*ImageImpl) GetField

func (i *ImageImpl) GetField() string

Function to get the image's XML field

func (*ImageImpl) GetSVG

func (i *ImageImpl) GetSVG() *oksvg.SvgIcon

Function to get the image's SVG

func (*ImageImpl) GetScale

func (i *ImageImpl) GetScale() util.ScaleOption

Function to get the image's image scale options

func (*ImageImpl) GetSprite

func (i *ImageImpl) GetSprite() *pixel.Sprite

Function to get the image's sprite

func (*ImageImpl) IsInitialised

func (i *ImageImpl) IsInitialised() bool

Function to determine whether the image has been initialised, by whether its image sprite has been set (assuming it's meant to be set). This function doesn't call element.IsInitialised

func (*ImageImpl) IsSVG

func (i *ImageImpl) IsSVG() bool

Function to determine whether the image looks like an SVG

func (*ImageImpl) Reset

func (i *ImageImpl) Reset()

Function to reset the image

func (*ImageImpl) SetSVG

func (i *ImageImpl) SetSVG(s *oksvg.SvgIcon)

Function to set the image's SVG

func (*ImageImpl) SetSprite

func (i *ImageImpl) SetSprite(s *pixel.Sprite)

Function to set the image's sprite

type Impl

type Impl struct {

	// The element's ID
	ID string `uixml:"http://github.com/bhollier/ui/api/schema id,optional"`

	// The element's relative width
	RelativeWidth util.RelativeSize `uixml:"http://github.com/bhollier/ui/api/schema width"`
	// The element's relative height
	RelativeHeight util.RelativeSize `uixml:"http://github.com/bhollier/ui/api/schema height"`

	// The element's padding
	Padding util.AbsoluteQuantity `uixml:"http://github.com/bhollier/ui/api/schema padding,optional"`

	// The element's background
	Bkg Background

	// The element's gravity
	Gravity util.Gravity `uixml:"http://github.com/bhollier/ui/api/schema gravity,optional"`
	// contains filtered or unexported fields
}

Type for the implementation of a UI element. Most elements should include this at some point (but only once)

func NewElement

func NewElement(fs http.FileSystem, name xml.Name, parent Layout) Impl

Function to create an element

func (*Impl) AddNamespace

func (e *Impl) AddNamespace(namespace string)

Function to add a namespace to the element

func (*Impl) Draw

func (e *Impl) Draw()

Function to draw the element. This function should be called first

func (*Impl) GetActualHeight

func (e *Impl) GetActualHeight() *float64

Function to get the element's actual height. If nil, it isn't known yet

func (*Impl) GetActualWidth

func (e *Impl) GetActualWidth() *float64

Function to get the element's actual width. If nil, it isn't known yet

func (*Impl) GetBkg

func (e *Impl) GetBkg() Image

Function to get the element's background

func (*Impl) GetBounds

func (e *Impl) GetBounds() *pixel.Rect

Function to get the element's bounds. If nil, either the min or max isn't known

func (*Impl) GetCanvas

func (e *Impl) GetCanvas() *pixelgl.Canvas

Function to get the element's canvas

func (*Impl) GetFS

func (e *Impl) GetFS() http.FileSystem

Function to get the filesystem to use for opening files

func (*Impl) GetGravity

func (e *Impl) GetGravity() util.Gravity

Function to get the element's gravity

func (*Impl) GetID

func (e *Impl) GetID() *string

Function to get the element's ID (or nil, if it doesn't have one)

func (*Impl) GetMax

func (e *Impl) GetMax() *pixel.Vec

Function to get the element's maximum point. If nil, it isn't known yet

func (*Impl) GetMin

func (e *Impl) GetMin() *pixel.Vec

Function to get the element's minimum point. If nil, it isn't known yet

func (*Impl) GetName

func (e *Impl) GetName() xml.Name

Function to get the element's name

func (*Impl) GetNamespaces

func (e *Impl) GetNamespaces() []string

Function to get the element's namespaces

func (*Impl) GetPadding

func (e *Impl) GetPadding() util.AbsoluteQuantity

Function to get the element's padding

func (*Impl) GetParent

func (e *Impl) GetParent() Layout

Function to get the element's parent

func (*Impl) GetRelHeight

func (e *Impl) GetRelHeight() util.RelativeSize

Function to get the element's relative height

func (*Impl) GetRelWidth

func (e *Impl) GetRelWidth() util.RelativeSize

Function to get the element's relative width

func (*Impl) Init

func (e *Impl) Init(window *pixelgl.Window, bounds *pixel.Rect) error

Function to initialise an element's position, width and height. Because it doesn't know the element's actual size, it won't set the width or height if the relative width or height is "match_content"

func (*Impl) IsInitialised

func (e *Impl) IsInitialised() bool

Function to determine whether the element has been initialised by whether its width, height, position and canvas are set (not nil) and if it's background is initialised

func (*Impl) NewEvent

func (e *Impl) NewEvent(*pixelgl.Window)

Function that is called when there is a new event. This function does nothing

func (*Impl) Reset

func (e *Impl) Reset()

Function to reset the element. This function doesn't reset the width and height of the element

func (*Impl) ResetPosition

func (e *Impl) ResetPosition()

Function to reset the element's position

func (*Impl) SetActualHeight

func (e *Impl) SetActualHeight(height *float64)

Function to set the element's actual height

func (*Impl) SetActualWidth

func (e *Impl) SetActualWidth(width *float64)

Function to set the element's actual width

func (*Impl) SetMax

func (e *Impl) SetMax(max *pixel.Vec)

Function to set the element's maximum point

func (*Impl) SetMin

func (e *Impl) SetMin(min *pixel.Vec)

Function to set the element's minimum point

func (*Impl) UnmarshalXML

func (e *Impl) UnmarshalXML(d *xml.Decoder, s xml.StartElement) (err error)

Function to unmarshal an XML element into an element. SetAttrs should've been called before this function

type Layout

type Layout interface {
	// A layout is also an element
	Element

	// Function to get one of a layout's child
	// elements
	GetChild(n int) Element
	// Function to get one of a layout's child
	// elements by its ID
	GetChildByID(id string) Element
	// Function to get the number of child
	// elements this layout has
	NumChildren() int
}

Interface type for a layout element

type LayoutImpl

type LayoutImpl struct {
	// The layout's child elements
	// (in order)
	Children []Element
}

Type for a layout

func (*LayoutImpl) GetChild

func (e *LayoutImpl) GetChild(n int) Element

Function to get one of a layout's child elements

func (*LayoutImpl) GetChildByID

func (e *LayoutImpl) GetChildByID(id string) Element

Function to get one of a layout's child elements by its ID. Returns nil if no child could be found

func (*LayoutImpl) NewEvent

func (e *LayoutImpl) NewEvent(window *pixelgl.Window)

Function that is called when there is a new event. This function only calls NewEvent on the child elements

func (*LayoutImpl) NumChildren

func (e *LayoutImpl) NumChildren() int

Function to get the number element elements a layout has

func (*LayoutImpl) Reset

func (e *LayoutImpl) Reset()

Function to reset the child elements

func (*LayoutImpl) ResetPosition

func (e *LayoutImpl) ResetPosition()

Function to reset the child element's positions

type NoElemError

type NoElemError struct {
	Element      Element
	ReferencedID string
	AttrName     string
}

Type for an error when an attribute's element ID doesn't match an actual ID

func NewNoElemError

func NewNoElemError(elem Element, refElem string, attrName string) NoElemError

Function to create a NoElemError. elem is the element that references an ID, refElem is the ID of the element being referenced (that doesn't exist), and attrName is the name of the attribute asking for the ID

func (NoElemError) Error

func (e NoElemError) Error() string

Function to return the error string

type Root

type Root struct {

	// The root element itself
	Element
	// contains filtered or unexported fields
}

Type for the root of a UI XML document

func NewRoot

func NewRoot(fs http.FileSystem, parent Layout, path string) (e *Root, err error)

Function to create a new design from an XML string

func (*Root) UnmarshalXML

func (e *Root) UnmarshalXML(decoder *xml.Decoder, start xml.StartElement) error

Function to unmarshal an XML element into a root element. This function is usually only called by xml.Unmarshal

type Text

type Text interface {
	// Function to get the text
	// XML field
	GetField() string

	// Function to get the text
	// font
	GetFont() string

	// Function to get the text
	// currently being displayed
	GetText() string
	// Function to set the text
	// currently being displayed.
	// This function doesn't alter
	// the text's width and height,
	// for this element.SetText
	// should be used instead
	SetText(string) error

	// Function to get the text size
	GetTextSize() float64

	// Function to get the text
	// "sprite"
	GetSprite() *text.Text
	// Function to set the text
	// "sprite"
	SetSprite(*text.Text)
}

Interface for some text

type TextImpl

type TextImpl struct {
	// The element's text from xml
	Text string `uixml:"http://github.com/bhollier/ui/api/schema text,optional"`
	// The element's font
	Font string `uixml:"http://github.com/bhollier/ui/api/schema font,optional"`
	// The element's text size
	Size float64 `uixml:"http://github.com/bhollier/ui/api/schema text-size,optional"`
	// contains filtered or unexported fields
}

Implementation of the text interface

func (*TextImpl) GetField

func (t *TextImpl) GetField() string

Function to get the text XML field

func (*TextImpl) GetFont

func (t *TextImpl) GetFont() string

Function to get the text font

func (*TextImpl) GetSprite

func (t *TextImpl) GetSprite() *text.Text

Function to get the text sprite

func (*TextImpl) GetText

func (t *TextImpl) GetText() string

Function to get the text currently being displayed

func (*TextImpl) GetTextSize

func (t *TextImpl) GetTextSize() float64

Function to get the text size

func (*TextImpl) IsInitialised

func (t *TextImpl) IsInitialised() bool

Function to determine whether the text has been initialised, by whether its text sprite has been set (assuming it's meant to be set). This function doesn't call element.IsInitialised

func (*TextImpl) Reset

func (t *TextImpl) Reset()

Function to reset the text

func (*TextImpl) SetSprite

func (t *TextImpl) SetSprite(s *text.Text)

Function to set the text sprite

func (*TextImpl) SetText

func (t *TextImpl) SetText(s string) error

Function to set the text currently being displayed. This function doesn't alter the text's width and height, for this element.SetText should be used instead

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL