| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208 |
- // Copyright 2016 The G3N Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- package gui
- import (
- "fmt"
- "io/ioutil"
- "os"
- "path/filepath"
- "sort"
- "strconv"
- "strings"
- "github.com/g3n/engine/gui/assets/icon"
- "github.com/g3n/engine/math32"
- "github.com/g3n/engine/window"
- "gopkg.in/yaml.v2"
- )
- // Builder builds GUI objects from a declarative description in YAML format
- type Builder struct {
- desc map[string]*descPanel // parsed descriptions
- imgpath string // base path for image panels files
- objpath strStack // stack of object names being built (used for error messages)
- }
- // descLayout describes all layout types
- type descLayout struct {
- Type string // HBox, VBox, Dock
- Spacing *float32 // spacing in pixels
- Align string // alignment type
- }
- // descLayoutParam describes all layout parameters types
- type descLayoutParams struct {
- Expand *float32 // HBox, VBox expand factor
- Align string // HBox, VBox align
- Row int // Grid layout row
- Col int // Grid layout col
- ColSpan int // Grid layout colspan
- AlignH string // horizontal alignment
- AlignV string // vertical alignment
- }
- // descPanel describes all panel types
- type descPanel struct {
- Type string // Gui object type: Panel, Label, Edit, etc ...
- Name string // Optional name for identification
- Position string // Optional position as: x y | x,y
- Width float32 // Optional width (default = 0)
- Height float32 // Optional height (default = 0)
- AspectWidth *float32 // Optional aspectwidth (default = nil)
- AspectHeight *float32 // Optional aspectwidth (default = nil)
- Margins string // Optional margins as 1 or 4 float values
- Borders string // Optional borders as 1 or 4 float values
- BorderColor string // Optional border color as name or 3 or 4 float values
- Paddings string // Optional paddings as 1 or 4 float values
- Color string // Optional color as 1 or 4 float values
- Enabled *bool
- Visible *bool
- Renderable *bool
- Imagefile string // For Panel, Button
- Layout *descLayout // Optional pointer to layout
- LayoutParams *descLayoutParams // Optional layout parameters
- Text string // Label, Button
- Icons string // Label
- BgColor string // Label
- FontColor string // Label
- FontSize *float32 // Label
- FontDPI *float32 // Label
- LineSpacing *float32 // Label
- PlaceHolder string // Edit
- MaxLength *uint // Edit
- Icon string // Button
- Group string // RadioButton
- ImageLabel *descPanel // DropDown
- Items []*descPanel // Menu, MenuBar
- Shortcut string // Menu
- Value *float32 // Slider
- ScaleFactor *float32 // Slider
- parent *descPanel // used internally
- }
- const (
- descTypePanel = "panel"
- descTypeImagePanel = "imagepanel"
- descTypeLabel = "label"
- descTypeImageLabel = "imagelabel"
- descTypeButton = "button"
- descTypeCheckBox = "checkbox"
- descTypeRadioButton = "radiobutton"
- descTypeEdit = "edit"
- descTypeVList = "vlist"
- descTypeHList = "hlist"
- descTypeDropDown = "dropdown"
- descTypeHSlider = "hslider"
- descTypeVSlider = "vslider"
- descTypeHSplitter = "hsplitter"
- descTypeVSplitter = "vsplitter"
- descTypeTree = "tree"
- descTypeTreeNode = "node"
- descTypeMenuBar = "menubar"
- descTypeMenu = "menu"
- descTypeHBoxLayout = "hbox"
- descTypeVBoxLayout = "vbox"
- descTypeGridLayout = "grid"
- fieldMargins = "margins"
- fieldBorders = "borders"
- fieldBorderColor = "bordercolor"
- fieldPaddings = "paddings"
- fieldColor = "color"
- fieldBgColor = "bgcolor"
- )
- const (
- aPOS = 1 << iota // attribute position
- aSIZE = 1 << iota // attribute size
- aNAME = 1 << iota // attribute name
- aMARGINS = 1 << iota // attribute margins widths
- aBORDERS = 1 << iota // attribute borders widths
- aBORDERCOLOR = 1 << iota // attribute border color
- aPADDINGS = 1 << iota // attribute paddings widths
- aCOLOR = 1 << iota // attribute panel bgcolor
- aENABLED = 1 << iota // attribute enabled for events
- aRENDER = 1 << iota // attribute renderable
- aVISIBLE = 1 << iota // attribute visible
- asPANEL = 0xFF // attribute set for panels
- asWIDGET = aPOS | aNAME | aENABLED | aVISIBLE // attribute set for widgets
- )
- // maps align name with align parameter
- var mapAlignName = map[string]Align{
- "none": AlignNone,
- "left": AlignLeft,
- "right": AlignRight,
- "width": AlignWidth,
- "top": AlignTop,
- "bottom": AlignBottom,
- "height": AlignHeight,
- "center": AlignCenter,
- }
- // NewBuilder creates and returns a pointer to a new gui Builder object
- func NewBuilder() *Builder {
- return new(Builder)
- }
- // ParseString parses a string with gui objects descriptions in YAML format
- // It there was a previously parsed description, it is cleared.
- func (b *Builder) ParseString(desc string) error {
- // Try assuming the description contains a single root panel
- var dp descPanel
- err := yaml.Unmarshal([]byte(desc), &dp)
- if err != nil {
- return err
- }
- if dp.Type != "" {
- b.desc = make(map[string]*descPanel)
- b.desc[""] = &dp
- b.setupDescTree(&dp)
- return nil
- }
- // Try assuming the description is a map of panels
- var dpm map[string]*descPanel
- err = yaml.Unmarshal([]byte(desc), &dpm)
- if err != nil {
- return err
- }
- b.desc = dpm
- for _, v := range dpm {
- b.setupDescTree(v)
- }
- return nil
- }
- // ParseFile parses a file with gui objects descriptions in YAML format
- // It there was a previously parsed description, it is cleared.
- func (b *Builder) ParseFile(filepath string) error {
- // Reads all file data
- f, err := os.Open(filepath)
- if err != nil {
- return err
- }
- data, err := ioutil.ReadAll(f)
- if err != nil {
- return err
- }
- err = f.Close()
- if err != nil {
- return err
- }
- // Parses file data
- return b.ParseString(string(data))
- }
- // Names returns a sorted list of names of top level previously parsed objects.
- // Only objects with defined types are returned.
- // If there is only a single object with no name, its name is returned
- // as an empty string
- func (b *Builder) Names() []string {
- var objs []string
- for name, pd := range b.desc {
- if pd.Type != "" {
- objs = append(objs, name)
- }
- }
- sort.Strings(objs)
- return objs
- }
- // Build builds a gui object and all its children recursively.
- // The specified name should be a top level name from a
- // from a previously parsed description
- // If the descriptions contains a single object with no name,
- // It should be specified the empty string to build this object.
- func (b *Builder) Build(name string) (IPanel, error) {
- pd, ok := b.desc[name]
- if !ok {
- return nil, fmt.Errorf("Object name:%s not found", name)
- }
- b.objpath.clear()
- b.objpath.push(pd.Name)
- return b.build(pd, nil)
- }
- // Sets the path for image panels relative image files
- func (b *Builder) SetImagepath(path string) {
- b.imgpath = path
- }
- // build builds the gui object from the specified description.
- // All its children are also built recursively
- // Returns the built object or an error
- func (b *Builder) build(pd *descPanel, iparent IPanel) (IPanel, error) {
- var err error
- var pan IPanel
- switch pd.Type {
- case descTypePanel:
- pan, err = b.buildPanel(pd)
- case descTypeImagePanel:
- pan, err = b.buildImagePanel(pd)
- case descTypeLabel:
- pan, err = b.buildLabel(pd)
- case descTypeImageLabel:
- pan, err = b.buildImageLabel(pd)
- case descTypeButton:
- pan, err = b.buildButton(pd)
- case descTypeCheckBox:
- pan, err = b.buildCheckBox(pd)
- case descTypeRadioButton:
- pan, err = b.buildRadioButton(pd)
- case descTypeEdit:
- pan, err = b.buildEdit(pd)
- case descTypeVList:
- pan, err = b.buildVList(pd)
- case descTypeHList:
- pan, err = b.buildHList(pd)
- case descTypeDropDown:
- pan, err = b.buildDropDown(pd)
- case descTypeHSlider:
- pan, err = b.buildSlider(pd, true)
- case descTypeVSlider:
- pan, err = b.buildSlider(pd, false)
- case descTypeHSplitter:
- pan, err = b.buildSplitter(pd, true)
- case descTypeVSplitter:
- pan, err = b.buildSplitter(pd, false)
- case descTypeTree:
- pan, err = b.buildTree(pd)
- case descTypeMenuBar:
- pan, err = b.buildMenu(pd, false, true)
- case descTypeMenu:
- pan, err = b.buildMenu(pd, false, false)
- default:
- err = fmt.Errorf("Invalid panel type:%s", pd.Type)
- }
- if err != nil {
- return nil, err
- }
- if iparent != nil {
- iparent.GetPanel().Add(pan)
- }
- return pan, nil
- }
- // buildPanel builds a gui object of type: "Panel"
- func (b *Builder) buildPanel(dp *descPanel) (IPanel, error) {
- // Builds panel and set common attributes
- pan := NewPanel(dp.Width, dp.Height)
- err := b.setCommon(dp, pan, asPANEL)
- if err != nil {
- return nil, err
- }
- // Builds panel children recursively
- for i := 0; i < len(dp.Items); i++ {
- item := dp.Items[i]
- b.objpath.push(item.Name)
- child, err := b.build(item, pan)
- b.objpath.pop()
- if err != nil {
- return nil, err
- }
- pan.Add(child)
- }
- return pan, nil
- }
- // buildImagePanel builds a gui object of type: "ImagePanel"
- func (b *Builder) buildImagePanel(pd *descPanel) (IPanel, error) {
- // Imagefile must be supplied
- if pd.Imagefile == "" {
- return nil, b.err("Imagefile", "Imagefile must be supplied")
- }
- // If path is not absolute join with user supplied image base path
- path := pd.Imagefile
- if !filepath.IsAbs(path) {
- path = filepath.Join(b.imgpath, path)
- }
- // Builds panel and set common attributes
- panel, err := NewImage(path)
- if err != nil {
- return nil, err
- }
- err = b.setCommon(pd, panel, asPANEL)
- if err != nil {
- return nil, err
- }
- // AspectWidth and AspectHeight attributes
- if pd.AspectWidth != nil {
- panel.SetContentAspectWidth(*pd.AspectWidth)
- }
- if pd.AspectHeight != nil {
- panel.SetContentAspectHeight(*pd.AspectHeight)
- }
- // Builds panel children recursively
- for i := 0; i < len(pd.Items); i++ {
- item := pd.Items[i]
- b.objpath.push(item.Name)
- child, err := b.build(item, panel)
- b.objpath.pop()
- if err != nil {
- return nil, err
- }
- panel.Add(child)
- }
- return panel, nil
- }
- // buildLabel builds a gui object of type: "Label"
- func (b *Builder) buildLabel(pd *descPanel) (IPanel, error) {
- // Builds label with icon or text font
- var label *Label
- icons, err := b.parseIconNames("icons", pd.Icons)
- if err != nil {
- return nil, err
- }
- if icons != "" {
- label = NewLabel(icons, true)
- } else {
- label = NewLabel(pd.Text)
- }
- // Sets common attributes
- err = b.setCommon(pd, label, asPANEL)
- if err != nil {
- return nil, err
- }
- // Set optional background color
- c, err := b.parseColor(fieldBgColor, pd.BgColor)
- if err != nil {
- return nil, err
- }
- if c != nil {
- label.SetBgColor4(c)
- }
- // Set optional font color
- c, err = b.parseColor("fontcolor", pd.FontColor)
- if err != nil {
- return nil, err
- }
- if c != nil {
- label.SetColor4(c)
- }
- // Sets optional font size
- if pd.FontSize != nil {
- label.SetFontSize(float64(*pd.FontSize))
- }
- // Sets optional font dpi
- if pd.FontDPI != nil {
- label.SetFontDPI(float64(*pd.FontDPI))
- }
- // Sets optional line spacing
- if pd.LineSpacing != nil {
- label.SetLineSpacing(float64(*pd.LineSpacing))
- }
- return label, nil
- }
- // buildImageLabel builds a gui object of type: ImageLabel
- func (b *Builder) buildImageLabel(pd *descPanel) (IPanel, error) {
- // Builds image label and set common attributes
- imglabel := NewImageLabel(pd.Text)
- err := b.setCommon(pd, imglabel, asPANEL)
- if err != nil {
- return nil, err
- }
- // Sets optional icon(s)
- icons, err := b.parseIconNames("icons", pd.Icons)
- if err != nil {
- return nil, err
- }
- if icons != "" {
- imglabel.SetIcon(icons)
- }
- return imglabel, nil
- }
- // buildButton builds a gui object of type: Button
- func (b *Builder) buildButton(pd *descPanel) (IPanel, error) {
- // Builds button and set commont attributes
- button := NewButton(pd.Text)
- err := b.setCommon(pd, button, asWIDGET)
- if err != nil {
- return nil, err
- }
- // Sets optional icon
- if pd.Icon != "" {
- cp, err := b.parseIconName("icon", pd.Icon)
- if err != nil {
- return nil, err
- }
- button.SetIcon(cp)
- }
- // Sets optional image from file
- // If path is not absolute join with user supplied image base path
- if pd.Imagefile != "" {
- path := pd.Imagefile
- if !filepath.IsAbs(path) {
- path = filepath.Join(b.imgpath, path)
- }
- err := button.SetImage(path)
- if err != nil {
- return nil, err
- }
- }
- return button, nil
- }
- // buildCheckBox builds a gui object of type: CheckBox
- func (b *Builder) buildCheckBox(pd *descPanel) (IPanel, error) {
- // Builds check box and set commont attributes
- cb := NewCheckBox(pd.Text)
- err := b.setCommon(pd, cb, asWIDGET)
- if err != nil {
- return nil, err
- }
- return cb, nil
- }
- // buildRadioButton builds a gui object of type: RadioButton
- func (b *Builder) buildRadioButton(pd *descPanel) (IPanel, error) {
- // Builds check box and set commont attributes
- rb := NewRadioButton(pd.Text)
- err := b.setCommon(pd, rb, asWIDGET)
- if err != nil {
- return nil, err
- }
- // Sets optional radio button group
- if pd.Group != "" {
- rb.SetGroup(pd.Group)
- }
- return rb, nil
- }
- // buildEdit builds a gui object of type: "Edit"
- func (b *Builder) buildEdit(pd *descPanel) (IPanel, error) {
- // Builds button and set commont attributes
- edit := NewEdit(int(pd.Width), pd.PlaceHolder)
- err := b.setCommon(pd, edit, asWIDGET)
- if err != nil {
- return nil, err
- }
- edit.SetText(pd.Text)
- return edit, nil
- }
- // buildVList builds a gui object of type: VList
- func (b *Builder) buildVList(pd *descPanel) (IPanel, error) {
- // Builds list and set commont attributes
- list := NewVList(pd.Width, pd.Height)
- err := b.setCommon(pd, list, asWIDGET)
- if err != nil {
- return nil, err
- }
- // Builds list children
- for i := 0; i < len(pd.Items); i++ {
- item := pd.Items[i]
- b.objpath.push(item.Name)
- child, err := b.build(item, list)
- b.objpath.pop()
- if err != nil {
- return nil, err
- }
- list.Add(child)
- }
- return list, nil
- }
- // buildHList builds a gui object of type: VList
- func (b *Builder) buildHList(pd *descPanel) (IPanel, error) {
- // Builds list and set commont attributes
- list := NewHList(pd.Width, pd.Height)
- err := b.setCommon(pd, list, asWIDGET)
- if err != nil {
- return nil, err
- }
- // Builds list children
- for i := 0; i < len(pd.Items); i++ {
- item := pd.Items[i]
- b.objpath.push(item.Name)
- child, err := b.build(item, list)
- b.objpath.pop()
- if err != nil {
- return nil, err
- }
- list.Add(child)
- }
- return list, nil
- }
- // buildDropDown builds a gui object of type: DropDown
- func (b *Builder) buildDropDown(pd *descPanel) (IPanel, error) {
- var imglabel *ImageLabel
- if pd.ImageLabel != nil {
- pd.ImageLabel.Type = descTypeImageLabel
- ipan, err := b.build(pd.ImageLabel, nil)
- if err != nil {
- return nil, err
- }
- imglabel = ipan.(*ImageLabel)
- } else {
- imglabel = NewImageLabel("")
- }
- // Builds drop down and set common attributes
- dd := NewDropDown(pd.Width, imglabel)
- err := b.setCommon(pd, dd, asWIDGET)
- if err != nil {
- return nil, err
- }
- // Builds drop down children
- for i := 0; i < len(pd.Items); i++ {
- item := pd.Items[i]
- item.Type = descTypeImageLabel
- b.objpath.push(item.Name)
- child, err := b.build(item, dd)
- b.objpath.pop()
- if err != nil {
- return nil, err
- }
- dd.Add(child.(*ImageLabel))
- }
- return dd, nil
- }
- // buildSlider builds a gui object of type: HSlider or VSlider
- func (b *Builder) buildSlider(pd *descPanel, horiz bool) (IPanel, error) {
- // Builds slider and sets its position
- var slider *Slider
- if horiz {
- slider = NewHSlider(pd.Width, pd.Height)
- } else {
- slider = NewVSlider(pd.Width, pd.Height)
- }
- err := b.setCommon(pd, slider, asWIDGET)
- if err != nil {
- return nil, err
- }
- // Sets optional text
- if pd.Text != "" {
- slider.SetText(pd.Text)
- }
- // Sets optional scale factor
- if pd.ScaleFactor != nil {
- slider.SetScaleFactor(*pd.ScaleFactor)
- }
- // Sets optional value
- if pd.Value != nil {
- slider.SetValue(*pd.Value)
- }
- return slider, nil
- }
- // buildSplitter builds a gui object of type: HSplitterr or VSplitter
- func (b *Builder) buildSplitter(pd *descPanel, horiz bool) (IPanel, error) {
- // Builds splitter and sets its common attributes
- var splitter *Splitter
- if horiz {
- splitter = NewHSplitter(pd.Width, pd.Height)
- } else {
- splitter = NewVSplitter(pd.Width, pd.Height)
- }
- err := b.setCommon(pd, splitter, asWIDGET)
- if err != nil {
- return nil, err
- }
- return splitter, nil
- }
- // buildTree builds a gui object of type: Tree
- func (b *Builder) buildTree(dp *descPanel) (IPanel, error) {
- // Builds tree and sets its common attributes
- tree := NewTree(dp.Width, dp.Height)
- err := b.setCommon(dp, tree, asWIDGET)
- if err != nil {
- return nil, err
- }
- // Internal function to build tree nodes recursively
- var buildItems func(dp *descPanel, pnode *TreeNode) error
- buildItems = func(dp *descPanel, pnode *TreeNode) error {
- for i := 0; i < len(dp.Items); i++ {
- item := dp.Items[i]
- // Item is a tree node
- if item.Type == "" || item.Type == descTypeTreeNode {
- var node *TreeNode
- if pnode == nil {
- node = tree.AddNode(item.Text)
- } else {
- node = pnode.AddNode(item.Text)
- }
- err := buildItems(item, node)
- if err != nil {
- return err
- }
- continue
- }
- // Other controls
- ipan, err := b.build(item, nil)
- if err != nil {
- return err
- }
- if pnode == nil {
- tree.Add(ipan)
- } else {
- pnode.Add(ipan)
- }
- }
- return nil
- }
- // Build nodes
- err = buildItems(dp, nil)
- if err != nil {
- return nil, err
- }
- return tree, nil
- }
- // buildMenu builds a gui object of type: Menu or MenuBar from the
- // specified panel descriptor.
- func (b *Builder) buildMenu(pd *descPanel, child, bar bool) (IPanel, error) {
- // Builds menu bar or menu
- var menu *Menu
- if bar {
- menu = NewMenuBar()
- } else {
- menu = NewMenu()
- }
- // Only sets attribs for top level menus
- if !child {
- err := b.setCommon(pd, menu, asWIDGET)
- if err != nil {
- return nil, err
- }
- }
- // Builds and adds menu items
- for i := 0; i < len(pd.Items); i++ {
- item := pd.Items[i]
- // Item is another menu
- if item.Type == descTypeMenu {
- subm, err := b.buildMenu(item, true, false)
- if err != nil {
- return nil, err
- }
- menu.AddMenu(item.Text, subm.(*Menu))
- continue
- }
- // Item is a separator
- if item.Type == "Separator" {
- menu.AddSeparator()
- continue
- }
- // Item must be a menu option
- mi := menu.AddOption(item.Text)
- // Set item optional icon(s)
- icons, err := b.parseIconNames("icon", item.Icon)
- if err != nil {
- return nil, err
- }
- if icons != "" {
- mi.SetIcon(string(icons))
- }
- // Sets optional menu item shortcut
- err = b.setMenuShortcut(mi, "shortcut", item.Shortcut)
- if err != nil {
- return nil, err
- }
- }
- return menu, nil
- }
- // setCommon sets the common attributes in the description to the specified panel
- func (b *Builder) setCommon(pd *descPanel, ipan IPanel, attr uint) error {
- panel := ipan.GetPanel()
- // Set optional position
- if attr&aPOS != 0 && pd.Position != "" {
- va, err := b.parseFloats("position", pd.Position, 2, 2)
- if va == nil || err != nil {
- return err
- }
- panel.SetPosition(va[0], va[1])
- }
- // Set optional margin sizes
- if attr&aMARGINS != 0 {
- bs, err := b.parseBorderSizes(fieldMargins, pd.Margins)
- if err != nil {
- return err
- }
- if bs != nil {
- panel.SetMarginsFrom(bs)
- }
- }
- // Set optional border sizes
- if attr&aBORDERS != 0 {
- bs, err := b.parseBorderSizes(fieldBorders, pd.Borders)
- if err != nil {
- return err
- }
- if bs != nil {
- panel.SetBordersFrom(bs)
- }
- }
- // Set optional border color
- if attr&aBORDERCOLOR != 0 {
- c, err := b.parseColor(fieldBorderColor, pd.BorderColor)
- if err != nil {
- return err
- }
- if c != nil {
- panel.SetBordersColor4(c)
- }
- }
- // Set optional paddings sizes
- if attr&aPADDINGS != 0 {
- bs, err := b.parseBorderSizes(fieldPaddings, pd.Paddings)
- if err != nil {
- return err
- }
- if bs != nil {
- panel.SetPaddingsFrom(bs)
- }
- }
- // Set optional color
- if attr&aCOLOR != 0 {
- c, err := b.parseColor(fieldColor, pd.Color)
- if err != nil {
- return err
- }
- if c != nil {
- panel.SetColor4(c)
- }
- }
- if attr&aNAME != 0 && pd.Name != "" {
- panel.SetName(pd.Name)
- }
- if attr&aVISIBLE != 0 && pd.Visible != nil {
- panel.SetVisible(*pd.Visible)
- }
- if attr&aENABLED != 0 && pd.Enabled != nil {
- panel.SetEnabled(*pd.Enabled)
- }
- if attr&aRENDER != 0 && pd.Renderable != nil {
- panel.SetRenderable(*pd.Renderable)
- }
- err := b.setLayoutParams(pd, ipan)
- if err != nil {
- return err
- }
- return b.setLayout(pd, ipan)
- }
- // setLayoutParams sets the optional layout params attribute for specified the panel
- func (b *Builder) setLayoutParams(dp *descPanel, ipan IPanel) error {
- // If layout params not declared, nothing to do
- if dp.LayoutParams == nil {
- return nil
- }
- // Get the parent layout
- if dp.parent == nil {
- return b.err("layoutparams", "No parent defined")
- }
- playout := dp.parent.Layout
- if playout == nil {
- return b.err("layoutparams", "Parent does not have layout")
- }
- panel := ipan.GetPanel()
- dlp := dp.LayoutParams
- // HBoxLayout parameters
- if playout.Type == descTypeHBoxLayout {
- // Creates layout parameter
- params := HBoxLayoutParams{Expand: 0, AlignV: AlignTop}
- // Sets optional expand parameter
- if dlp.Expand != nil {
- params.Expand = *dlp.Expand
- }
- // Sets optional align parameter
- if dlp.Align != "" {
- align, ok := mapAlignName[dlp.Align]
- if !ok {
- return b.err("align", "Invalid align name:"+dlp.Align)
- }
- params.AlignV = align
- }
- panel.SetLayoutParams(¶ms)
- return nil
- }
- // VBoxLayout parameters
- if playout.Type == descTypeVBoxLayout {
- // Creates layout parameter
- params := VBoxLayoutParams{Expand: 0, AlignH: AlignLeft}
- // Sets optional expand parameter
- if dlp.Expand != nil {
- params.Expand = *dlp.Expand
- }
- // Sets optional align parameter
- if dlp.Align != "" {
- align, ok := mapAlignName[dlp.Align]
- if !ok {
- return b.err("align", "Invalid align name:"+dlp.Align)
- }
- params.AlignH = align
- }
- panel.SetLayoutParams(¶ms)
- return nil
- }
- // GridLayout parameters
- if playout.Type == descTypeGridLayout {
- // Creates layout parameter
- params := GridLayoutParams{
- Row: 0,
- Col: 0,
- ColSpan: 0,
- AlignH: AlignCenter,
- AlignV: AlignCenter,
- }
- // Sets row parameter
- params.Row = dlp.Row
- params.Col = dlp.Col
- params.ColSpan = dlp.ColSpan
- // Sets optional alignh parameter
- if dlp.AlignH != "" {
- align, ok := mapAlignName[dlp.AlignH]
- if !ok {
- return b.err("alignh", "Invalid align name:"+dlp.AlignH)
- }
- params.AlignH = align
- }
- // Sets optional alignv parameter
- if dlp.AlignV != "" {
- align, ok := mapAlignName[dlp.AlignV]
- if !ok {
- return b.err("alignv", "Invalid align name:"+dlp.AlignV)
- }
- params.AlignV = align
- }
- panel.SetLayoutParams(¶ms)
- log.Error("set grid parameters:%v", params)
- return nil
- }
- return b.err("layoutparams", "Invalid parent layout:"+playout.Type)
- }
- // setLayout sets the optional panel layout and layout parameters
- func (b *Builder) setLayout(dp *descPanel, ipan IPanel) error {
- // If layout types not declared, nothing to do
- if dp.Layout == nil {
- return nil
- }
- dl := dp.Layout
- panel := ipan.GetPanel()
- // HBox layout
- if dl.Type == descTypeHBoxLayout {
- hbl := NewHBoxLayout()
- if dl.Spacing != nil {
- hbl.SetSpacing(*dl.Spacing)
- }
- if dl.Align != "" {
- align, ok := mapAlignName[dl.Align]
- if !ok {
- return b.err("align", "Invalid align name:"+dl.Align)
- }
- hbl.SetAlignH(align)
- }
- panel.SetLayout(hbl)
- return nil
- }
- // VBox layout
- if dl.Type == descTypeVBoxLayout {
- vbl := NewVBoxLayout()
- if dl.Spacing != nil {
- vbl.SetSpacing(*dl.Spacing)
- }
- if dl.Align != "" {
- align, ok := mapAlignName[dl.Align]
- if !ok {
- return b.err("align", "Invalid align name:"+dl.Align)
- }
- vbl.SetAlignV(align)
- }
- panel.SetLayout(vbl)
- return nil
- }
- // Grid layout
- if dl.Type == descTypeGridLayout {
- log.Error("set grid layout")
- grl := NewGridLayout()
- panel.SetLayout(grl)
- return nil
- }
- return b.err("layout", "Invalid layout type:"+dl.Type)
- }
- func (b *Builder) setMenuShortcut(mi *MenuItem, fname, field string) error {
- field = strings.Trim(field, " ")
- if field == "" {
- return nil
- }
- parts := strings.Split(field, "+")
- var mods window.ModifierKey
- for i := 0; i < len(parts)-1; i++ {
- switch parts[i] {
- case "Shift":
- mods |= window.ModShift
- case "Ctrl":
- mods |= window.ModControl
- case "Alt":
- mods |= window.ModAlt
- default:
- return b.err(fname, "Invalid shortcut:"+field)
- }
- }
- // The last part must be a key
- key := parts[len(parts)-1]
- for kcode, kname := range mapKeyText {
- if kname == key {
- mi.SetShortcut(mods, kcode)
- return nil
- }
- }
- return b.err(fname, "Invalid shortcut:"+field)
- }
- // parseBorderSizes parses a string field which can contain one float value or
- // float values. In the first case all borders has the same width
- func (b *Builder) parseBorderSizes(fname, field string) (*BorderSizes, error) {
- va, err := b.parseFloats(fname, field, 1, 4)
- if va == nil || err != nil {
- return nil, err
- }
- if len(va) == 1 {
- return &BorderSizes{va[0], va[0], va[0], va[0]}, nil
- }
- return &BorderSizes{va[0], va[1], va[2], va[3]}, nil
- }
- // parseColor parses a string field which can contain a color name or
- // a list of 3 or 4 float values for the color components
- func (b *Builder) parseColor(fname, field string) (*math32.Color4, error) {
- // Checks if field is empty
- field = strings.Trim(field, " ")
- if field == "" {
- return nil, nil
- }
- // If string has 1 or 2 fields it must be a color name and optional alpha
- parts := strings.Fields(field)
- if len(parts) == 1 || len(parts) == 2 {
- // First part must be a color name
- if !math32.IsColor(parts[0]) {
- return nil, b.err(fname, fmt.Sprintf("Invalid color name:%s", parts[0]))
- }
- c := math32.ColorName(parts[0])
- c4 := math32.Color4{c.R, c.G, c.B, 1}
- if len(parts) == 2 {
- val, err := strconv.ParseFloat(parts[1], 32)
- if err != nil {
- return nil, b.err(fname, fmt.Sprintf("Invalid float32 value:%s", parts[1]))
- }
- c4.A = float32(val)
- }
- return &c4, nil
- }
- // Accept 3 or 4 floats values
- va, err := b.parseFloats(fname, field, 3, 4)
- if err != nil {
- return nil, err
- }
- if len(va) == 3 {
- return &math32.Color4{va[0], va[1], va[2], 1}, nil
- }
- return &math32.Color4{va[0], va[1], va[2], va[3]}, nil
- }
- // parseIconNames parses a string with a list of icon names or codepoints and
- // returns a string with the icons codepoints encoded in UTF8
- func (b *Builder) parseIconNames(fname, field string) (string, error) {
- text := ""
- parts := strings.Fields(field)
- for i := 0; i < len(parts); i++ {
- cp, err := b.parseIconName(fname, parts[i])
- if err != nil {
- return "", err
- }
- text = text + string(cp)
- }
- return text, nil
- }
- // parseIconName parses a string with an icon name or codepoint in hex
- // and returns the icon codepoints value and an error
- func (b *Builder) parseIconName(fname, field string) (string, error) {
- // Try name first
- cp := icon.Codepoint(field)
- if cp != "" {
- return cp, nil
- }
- // Try to parse as hex value
- cp2, err := strconv.ParseUint(field, 16, 32)
- if err != nil {
- return "", b.err(fname, fmt.Sprintf("Invalid icon codepoint value/name:%v", field))
- }
- return string(cp2), nil
- }
- // parseFloats parses a string with a list of floats with the specified size
- // and returns a slice. The specified size is 0 any number of floats is allowed.
- // The individual values can be separated by spaces or commas
- func (b *Builder) parseFloats(fname, field string, min, max int) ([]float32, error) {
- // Checks if field is empty
- field = strings.Trim(field, " ")
- if field == "" {
- return nil, nil
- }
- // Separate individual fields
- var parts []string
- if strings.Index(field, ",") < 0 {
- parts = strings.Fields(field)
- } else {
- parts = strings.Split(field, ",")
- }
- if len(parts) < min || len(parts) > max {
- return nil, b.err(fname, "Invalid number of float32 values")
- }
- // Parse each field value and appends to slice
- var values []float32
- for i := 0; i < len(parts); i++ {
- val, err := strconv.ParseFloat(strings.Trim(parts[i], " "), 32)
- if err != nil {
- return nil, b.err(fname, err.Error())
- }
- values = append(values, float32(val))
- }
- return values, nil
- }
- // err creates and returns an error for the current object, field name and with the specified message
- func (b *Builder) err(fname, msg string) error {
- return fmt.Errorf("Error in object:%s field:%s -> %s", b.objpath.path(), fname, msg)
- }
- // setupDescTree sets the types of all description tree elements to lower case and
- // sets the items "parent" attribute pointing the respective parent description
- func (b *Builder) setupDescTree(dp *descPanel) {
- dp.Type = strings.ToLower(dp.Type)
- if dp.Layout != nil {
- dp.Layout.Type = strings.ToLower(dp.Layout.Type)
- }
- for i := 0; i < len(dp.Items); i++ {
- dp.Items[i].parent = dp
- b.setupDescTree(dp.Items[i])
- }
- }
- // strStack is a stack of strings
- type strStack struct {
- stack []string
- }
- // clear removes all elements from the stack
- func (ss *strStack) clear() {
- ss.stack = []string{}
- }
- // push pushes a string to the top of the stack
- func (ss *strStack) push(v string) {
- ss.stack = append(ss.stack, v)
- }
- // pop removes and returns the string at the top of the stack.
- // Returns an empty string if the stack is empty
- func (ss *strStack) pop() string {
- if len(ss.stack) == 0 {
- return ""
- }
- length := len(ss.stack)
- v := ss.stack[length-1]
- ss.stack = ss.stack[:length-1]
- return v
- }
- // path returns a string composed of all the strings in the
- // stack separated by a forward slash.
- func (ss *strStack) path() string {
- return strings.Join(ss.stack, "/")
- }
|