builder.go 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gui
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "sort"
  11. "strconv"
  12. "strings"
  13. "github.com/g3n/engine/gui/assets/icon"
  14. "github.com/g3n/engine/math32"
  15. "github.com/g3n/engine/window"
  16. "gopkg.in/yaml.v2"
  17. )
  18. // Builder builds GUI objects from a declarative description in YAML format
  19. type Builder struct {
  20. am map[string]interface{} // parsed map with gui object atttributes
  21. imgpath string // base path for image panels files
  22. builders map[string]BuilderFunc // map of builder functions by type
  23. attribs map[string]AttribCheckFunc // map of attribute name with check functions
  24. layouts map[string]IBuilderLayout // map of layout type to layout builder
  25. }
  26. // IBuilderLayout is the interface for all layout builders
  27. type IBuilderLayout interface {
  28. BuildLayout(b *Builder, am map[string]interface{}) (ILayout, error)
  29. BuildParams(b *Builder, am map[string]interface{}) (interface{}, error)
  30. }
  31. // BuilderFunc is type for functions which build a gui object from an attribute map
  32. type BuilderFunc func(*Builder, map[string]interface{}) (IPanel, error)
  33. // BuilderFunc is type for functions which builds a layout object from an attribute map
  34. type LayoutFunc func(*Builder, map[string]interface{}) (ILayout, error)
  35. //// descLayout contains all layout attributes
  36. //type descLayout struct {
  37. // Type string // Type of the layout: HBox, VBox, Grid, Dock, others...
  38. // Cols int // Number of columns for Grid layout
  39. // Spacing float32 // Spacing in pixels for HBox and VBox
  40. // AlignH string // HBox group alignment type
  41. // AlignV string // VBox group alignment type
  42. // MinHeight bool // HBox, VBox minimum height flag
  43. // MinWidth bool // HBox, VBox minimum width flag
  44. // ExpandH bool // Grid
  45. // ExpandV bool // Grid
  46. //}
  47. //
  48. //// descLayoutParam describes all layout parameters types
  49. //type descLayoutParams struct {
  50. // Expand *float32 // HBox, VBox expand factor
  51. // ColSpan int // Grid layout colspan
  52. // AlignH string // horizontal alignment
  53. // AlignV string // vertical alignment
  54. // Edge string // Dock layout edge: top,right,bottom,left,center
  55. //}
  56. //// descPanel describes all panel attributes
  57. //type descPanel struct {
  58. // Type string // Gui object type: Panel, Label, Edit, etc ...
  59. // Name string // Optional name for identification
  60. // Position string // Optional position as: x y | x,y
  61. // Width *float32 // Optional width (default = 0)
  62. // Height *float32 // Optional height (default = 0)
  63. // AspectWidth *float32 // Optional aspectwidth (default = nil)
  64. // AspectHeight *float32 // Optional aspectwidth (default = nil)
  65. // Margins string // Optional margins as 1 or 4 float values
  66. // Borders string // Optional borders as 1 or 4 float values
  67. // BorderColor string // Optional border color as name or 3 or 4 float values
  68. // Paddings string // Optional paddings as 1 or 4 float values
  69. // Color string // Optional color as 1 or 4 float values
  70. // Enabled *bool // All:
  71. // Visible *bool // All:
  72. // Renderable *bool // All:
  73. // Imagefile string // For Panel, Button
  74. // Layout *descLayout // Optional pointer to layout
  75. // LayoutParams *descLayoutParams // Optional layout parameters
  76. // Text string // Label, Button
  77. // Icons string // Label
  78. // BgColor string // Label
  79. // FontColor string // Label
  80. // FontSize *float32 // Label
  81. // FontDPI *float32 // Label
  82. // LineSpacing *float32 // Label
  83. // PlaceHolder string // Edit
  84. // MaxLength *uint // Edit
  85. // Icon string // Button
  86. // Group string // RadioButton
  87. // Checked bool // CheckBox, RadioButton
  88. // ImageLabel *descPanel // DropDown
  89. // Items []*descPanel // Menu, MenuBar
  90. // Shortcut string // Menu
  91. // Value *float32 // Slider
  92. // ScaleFactor *float32 // Slider
  93. // Title string // Window
  94. // Resizable string // Window resizable borders
  95. // P0 *descPanel // Splitter panel 0
  96. // P1 *descPanel // Splitter panel 1
  97. // Split *float32 // Splitter split value
  98. // parent *descPanel // used internally
  99. //}
  100. // Panel and layout types
  101. const (
  102. TypePanel = "panel"
  103. TypeImagePanel = "imagepanel"
  104. TypeLabel = "label"
  105. TypeImageLabel = "imagelabel"
  106. TypeButton = "button"
  107. TypeCheckBox = "checkbox"
  108. TypeRadioButton = "radiobutton"
  109. TypeEdit = "edit"
  110. TypeVList = "vlist"
  111. TypeHList = "hlist"
  112. TypeDropDown = "dropdown"
  113. TypeHSlider = "hslider"
  114. TypeVSlider = "vslider"
  115. TypeHSplitter = "hsplitter"
  116. TypeVSplitter = "vsplitter"
  117. TypeSeparator = "separator"
  118. TypeTree = "tree"
  119. TypeTreeNode = "node"
  120. TypeMenuBar = "menubar"
  121. TypeMenu = "menu"
  122. TypeWindow = "window"
  123. TypeHBoxLayout = "hbox"
  124. TypeVBoxLayout = "vbox"
  125. TypeGridLayout = "grid"
  126. TypeDockLayout = "dock"
  127. )
  128. // Common attribute names
  129. const (
  130. AttribAlignv = "alignv" // Align
  131. AttribAlignh = "alignh" // Align
  132. AttribAspectHeight = "aspectheight" // float32
  133. AttribAspectWidth = "aspectwidth" // float32
  134. AttribBgColor = "bgcolor" // Color4
  135. AttribBorders = "borders" // BorderSizes
  136. AttribBorderColor = "bordercolor" // Color4
  137. AttribChecked = "checked" // bool
  138. AttribColor = "color" // Color4
  139. AttribCols = "cols" // Int
  140. AttribColSpan = "colspan" // Int
  141. AttribEdge = "edge" // int
  142. AttribEnabled = "enabled" // bool
  143. AttribExpand = "expand" // float32
  144. AttribExpandh = "expandh" // bool
  145. AttribExpandv = "expandv" // bool
  146. AttribFontColor = "fontcolor" // Color4
  147. AttribFontDPI = "fontdpi" // float32
  148. AttribFontSize = "fontsize" // float32
  149. AttribGroup = "group" // string
  150. AttribHeight = "height" // float32
  151. AttribIcon = "icon" // string
  152. AttribImageFile = "imagefile" // string
  153. AttribImageLabel = "imagelabel" // []map[string]interface{}
  154. AttribItems = "items" // []map[string]interface{}
  155. AttribLayout = "layout" // map[string]interface{}
  156. AttribLayoutParams = "layoutparams" // map[string]interface{}
  157. AttribLineSpacing = "linespacing" // float32
  158. AttribMinHeight = "minheight" // bool
  159. AttribMinWidth = "minwidth" // bool
  160. AttribMargins = "margins" // BorderSizes
  161. AttribName = "name" // string
  162. AttribPaddings = "paddings" // BorderSizes
  163. AttribPanel0 = "panel0" // map[string]interface{}
  164. AttribPanel1 = "panel1" // map[string]interface{}
  165. AttribParent_ = "parent_" // string (internal attribute)
  166. AttribPlaceHolder = "placeholder" // string
  167. AttribPosition = "position" // []float32
  168. AttribRender = "render" // bool
  169. AttribResizable = "resizable" // Resizable
  170. AttribScaleFactor = "scalefactor" // float32
  171. AttribShortcut = "shortcut" // []int
  172. AttribSpacing = "spacing" // float32
  173. AttribSplit = "split" // float32
  174. AttribText = "text" // string
  175. AttribTitle = "title" // string
  176. AttribType = "type" // string
  177. AttribWidth = "width" // float32
  178. AttribValue = "value" // float32
  179. AttribVisible = "visible" // bool
  180. )
  181. const (
  182. aPOS = 1 << iota // attribute position
  183. aSIZE = 1 << iota // attribute size
  184. aNAME = 1 << iota // attribute name
  185. aMARGINS = 1 << iota // attribute margins widths
  186. aBORDERS = 1 << iota // attribute borders widths
  187. aBORDERCOLOR = 1 << iota // attribute border color
  188. aPADDINGS = 1 << iota // attribute paddings widths
  189. aCOLOR = 1 << iota // attribute panel bgcolor
  190. aENABLED = 1 << iota // attribute enabled for events
  191. aRENDER = 1 << iota // attribute renderable
  192. aVISIBLE = 1 << iota // attribute visible
  193. asPANEL = 0xFF // attribute set for panels
  194. asWIDGET = aPOS | aNAME | aSIZE | aENABLED | aVISIBLE // attribute set for widgets
  195. )
  196. // maps align name with align parameter
  197. var mapAlignh = map[string]Align{
  198. "none": AlignNone,
  199. "left": AlignLeft,
  200. "right": AlignRight,
  201. "width": AlignWidth,
  202. "center": AlignCenter,
  203. }
  204. // maps align name with align parameter
  205. var mapAlignv = map[string]Align{
  206. "none": AlignNone,
  207. "top": AlignTop,
  208. "bottom": AlignBottom,
  209. "height": AlignHeight,
  210. "center": AlignCenter,
  211. }
  212. // maps edge name (dock layout) with edge parameter
  213. var mapEdgeName = map[string]int{
  214. "top": DockTop,
  215. "right": DockRight,
  216. "bottom": DockBottom,
  217. "left": DockLeft,
  218. "center": DockCenter,
  219. }
  220. // maps resize border name (window) with parameter value
  221. var mapResizable = map[string]Resizable{
  222. "top": ResizeTop,
  223. "right": ResizeRight,
  224. "bottom": ResizeBottom,
  225. "left": ResizeLeft,
  226. "all": ResizeAll,
  227. }
  228. type AttribCheckFunc func(b *Builder, am map[string]interface{}, fname string) error
  229. // NewBuilder creates and returns a pointer to a new gui Builder object
  230. func NewBuilder() *Builder {
  231. b := new(Builder)
  232. // Sets map of object type to builder function
  233. b.builders = map[string]BuilderFunc{
  234. TypePanel: buildPanel,
  235. TypeImagePanel: buildImagePanel,
  236. TypeLabel: buildLabel,
  237. TypeImageLabel: buildImageLabel,
  238. TypeButton: buildButton,
  239. TypeEdit: buildEdit,
  240. TypeCheckBox: buildCheckBox,
  241. TypeRadioButton: buildRadioButton,
  242. TypeVList: buildVList,
  243. TypeHList: buildHList,
  244. TypeDropDown: buildDropDown,
  245. TypeMenu: buildMenu,
  246. TypeMenuBar: buildMenu,
  247. TypeHSlider: buildSlider,
  248. TypeVSlider: buildSlider,
  249. TypeHSplitter: buildSplitter,
  250. TypeVSplitter: buildSplitter,
  251. TypeTree: buildTree,
  252. TypeWindow: buildWindow,
  253. }
  254. // Sets map of layout type name to layout function
  255. b.layouts = map[string]IBuilderLayout{
  256. TypeHBoxLayout: &BuilderLayoutHBox{},
  257. TypeVBoxLayout: &BuilderLayoutVBox{},
  258. TypeGridLayout: &BuilderLayoutGrid{},
  259. TypeDockLayout: &BuilderLayoutDock{},
  260. }
  261. // Sets map of attribute name to check function
  262. b.attribs = map[string]AttribCheckFunc{
  263. AttribAlignv: AttribCheckAlign,
  264. AttribAlignh: AttribCheckAlign,
  265. AttribAspectWidth: AttribCheckFloat,
  266. AttribAspectHeight: AttribCheckFloat,
  267. AttribHeight: AttribCheckFloat,
  268. AttribMargins: AttribCheckBorderSizes,
  269. AttribBgColor: AttribCheckColor,
  270. AttribBorders: AttribCheckBorderSizes,
  271. AttribBorderColor: AttribCheckColor,
  272. AttribChecked: AttribCheckBool,
  273. AttribColor: AttribCheckColor,
  274. AttribCols: AttribCheckInt,
  275. AttribColSpan: AttribCheckInt,
  276. AttribEdge: AttribCheckEdge,
  277. AttribEnabled: AttribCheckBool,
  278. AttribExpand: AttribCheckFloat,
  279. AttribExpandh: AttribCheckBool,
  280. AttribExpandv: AttribCheckBool,
  281. AttribFontColor: AttribCheckColor,
  282. AttribFontDPI: AttribCheckFloat,
  283. AttribFontSize: AttribCheckFloat,
  284. AttribGroup: AttribCheckString,
  285. AttribIcon: AttribCheckIcons,
  286. AttribImageFile: AttribCheckString,
  287. AttribImageLabel: AttribCheckMap,
  288. AttribItems: AttribCheckListMap,
  289. AttribLayout: AttribCheckLayout,
  290. AttribLayoutParams: AttribCheckMap,
  291. AttribLineSpacing: AttribCheckFloat,
  292. AttribMinHeight: AttribCheckBool,
  293. AttribMinWidth: AttribCheckBool,
  294. AttribName: AttribCheckString,
  295. AttribPaddings: AttribCheckBorderSizes,
  296. AttribPanel0: AttribCheckMap,
  297. AttribPanel1: AttribCheckMap,
  298. AttribPlaceHolder: AttribCheckString,
  299. AttribPosition: AttribCheckPosition,
  300. AttribRender: AttribCheckBool,
  301. AttribResizable: AttribCheckResizable,
  302. AttribScaleFactor: AttribCheckFloat,
  303. AttribShortcut: AttribCheckMenuShortcut,
  304. AttribSpacing: AttribCheckFloat,
  305. AttribSplit: AttribCheckFloat,
  306. AttribText: AttribCheckString,
  307. AttribTitle: AttribCheckString,
  308. AttribType: AttribCheckStringLower,
  309. AttribValue: AttribCheckFloat,
  310. AttribVisible: AttribCheckBool,
  311. AttribWidth: AttribCheckFloat,
  312. }
  313. return b
  314. }
  315. // ParseString parses a string with gui objects descriptions in YAML format
  316. // It there was a previously parsed description, it is cleared.
  317. func (b *Builder) ParseString(desc string) error {
  318. // Parses descriptor string in YAML format saving result in
  319. // a map of interface{} to interface{} as YAML allows numeric keys.
  320. var mii map[interface{}]interface{}
  321. err := yaml.Unmarshal([]byte(desc), &mii)
  322. if err != nil {
  323. return err
  324. }
  325. // If all the values of the top level map keys are other maps,
  326. // then it is a description of several objects, otherwise it is
  327. // a description of a single object.
  328. single := false
  329. for _, v := range mii {
  330. _, ok := v.(map[interface{}]interface{})
  331. if !ok {
  332. single = true
  333. break
  334. }
  335. }
  336. log.Error("single:%v", single)
  337. // Internal function which converts map[interface{}]interface{} to
  338. // map[string]interface{} recursively and lower case of all map keys.
  339. // It also sets a field named "parent_", which pointer to the parent map
  340. // This field causes a circular reference in the result map which prevents
  341. // the use of Go's Printf to print the result map.
  342. var visitor func(v, par interface{}) (interface{}, error)
  343. visitor = func(v, par interface{}) (interface{}, error) {
  344. switch vt := v.(type) {
  345. case []interface{}:
  346. ls := []interface{}{}
  347. for _, item := range vt {
  348. ci, err := visitor(item, par)
  349. if err != nil {
  350. return nil, err
  351. }
  352. ls = append(ls, ci)
  353. }
  354. return ls, nil
  355. case map[interface{}]interface{}:
  356. ms := make(map[string]interface{})
  357. for k, v := range vt {
  358. // Checks key
  359. ks, ok := k.(string)
  360. if !ok {
  361. return nil, fmt.Errorf("Keys must be strings")
  362. }
  363. ks = strings.ToLower(ks)
  364. // Checks value
  365. vi, err := visitor(v, ms)
  366. if err != nil {
  367. return nil, err
  368. }
  369. ms[ks] = vi
  370. // If has panel has parent or is a single top level panel, checks attributes
  371. if par != nil || single {
  372. // Get attribute check function
  373. acf, ok := b.attribs[ks]
  374. if !ok {
  375. return nil, fmt.Errorf("Invalid attribute:%s", ks)
  376. }
  377. // Checks attribute
  378. err = acf(b, ms, ks)
  379. if err != nil {
  380. return nil, err
  381. }
  382. }
  383. }
  384. if par != nil {
  385. ms[AttribParent_] = par
  386. }
  387. return ms, nil
  388. default:
  389. return v, nil
  390. }
  391. return nil, nil
  392. }
  393. // Get map[string]interface{} with lower case keys from parsed descritor
  394. res, err := visitor(mii, nil)
  395. if err != nil {
  396. return err
  397. }
  398. msi, ok := res.(map[string]interface{})
  399. if !ok {
  400. return fmt.Errorf("Parsed result is not a map")
  401. }
  402. b.am = msi
  403. //b.debugPrint(b.am, 1)
  404. return nil
  405. }
  406. // ParseFile parses a file with gui objects descriptions in YAML format
  407. // It there was a previously parsed description, it is cleared.
  408. func (b *Builder) ParseFile(filepath string) error {
  409. // Reads all file data
  410. f, err := os.Open(filepath)
  411. if err != nil {
  412. return err
  413. }
  414. data, err := ioutil.ReadAll(f)
  415. if err != nil {
  416. return err
  417. }
  418. err = f.Close()
  419. if err != nil {
  420. return err
  421. }
  422. // Parses file data
  423. return b.ParseString(string(data))
  424. }
  425. // Names returns a sorted list of names of top level previously parsed objects.
  426. // Only objects with defined types are returned.
  427. // If there is only a single object with no name, its name is returned
  428. // as an empty string
  429. func (b *Builder) Names() []string {
  430. var objs []string
  431. if b.am[AttribType] != nil {
  432. objs = append(objs, "")
  433. return objs
  434. }
  435. for name, _ := range b.am {
  436. objs = append(objs, name)
  437. }
  438. sort.Strings(objs)
  439. return objs
  440. }
  441. // Build builds a gui object and all its children recursively.
  442. // The specified name should be a top level name from a
  443. // from a previously parsed description
  444. // If the descriptions contains a single object with no name,
  445. // It should be specified the empty string to build this object.
  446. func (b *Builder) Build(name string) (IPanel, error) {
  447. // Only one object
  448. if name == "" {
  449. log.Error("TYPE:---------->%T", b.am)
  450. return b.build(b.am, nil)
  451. }
  452. // Map of gui objects
  453. am, ok := b.am[name]
  454. if !ok {
  455. return nil, fmt.Errorf("Object name:%s not found", name)
  456. }
  457. return b.build(am.(map[string]interface{}), nil)
  458. }
  459. // Sets the path for image panels relative image files
  460. func (b *Builder) SetImagepath(path string) {
  461. b.imgpath = path
  462. }
  463. func (b *Builder) AddBuilder(typename string, bf BuilderFunc) {
  464. b.builders[typename] = bf
  465. }
  466. // build builds the gui object from the specified description.
  467. // All its children are also built recursively
  468. // Returns the built object or an error
  469. func (b *Builder) build(am map[string]interface{}, iparent IPanel) (IPanel, error) {
  470. // Get panel type
  471. itype := am[AttribType]
  472. if itype == nil {
  473. return nil, fmt.Errorf("Type not specified")
  474. }
  475. typename := itype.(string)
  476. // Get builder function for this type name
  477. builder := b.builders[typename]
  478. if builder == nil {
  479. return nil, fmt.Errorf("Invalid type:%v", typename)
  480. }
  481. // Builds panel
  482. pan, err := builder(b, am)
  483. if err != nil {
  484. return nil, err
  485. }
  486. // Adds built panel to parent
  487. if iparent != nil {
  488. iparent.GetPanel().Add(pan)
  489. }
  490. return pan, nil
  491. }
  492. // buildPanel builds an object of type Panel
  493. func buildPanel(b *Builder, am map[string]interface{}) (IPanel, error) {
  494. pan := NewPanel(0, 0)
  495. err := b.setAttribs(am, pan, asPANEL)
  496. if err != nil {
  497. return nil, err
  498. }
  499. // Builds children recursively
  500. if am[AttribItems] != nil {
  501. items := am[AttribItems].([]map[string]interface{})
  502. for i := 0; i < len(items); i++ {
  503. item := items[i]
  504. child, err := b.build(item, pan)
  505. if err != nil {
  506. return nil, err
  507. }
  508. pan.Add(child)
  509. }
  510. }
  511. return pan, nil
  512. }
  513. // buildImagePanel builds a gui object of type ImagePanel
  514. func buildImagePanel(b *Builder, am map[string]interface{}) (IPanel, error) {
  515. // Checks imagefile attribute
  516. if am[AttribImageFile] == nil {
  517. return nil, b.err(am, AttribImageFile, "Must be supplied")
  518. }
  519. // If path is not absolute join with user supplied image base path
  520. imagefile := am[AttribImageFile].(string)
  521. if !filepath.IsAbs(imagefile) {
  522. imagefile = filepath.Join(b.imgpath, imagefile)
  523. }
  524. // Builds panel and set common attributes
  525. panel, err := NewImage(imagefile)
  526. if err != nil {
  527. return nil, err
  528. }
  529. err = b.setAttribs(am, panel, asPANEL)
  530. if err != nil {
  531. return nil, err
  532. }
  533. // Sets optional AspectWidth attribute
  534. if aw := am[AttribAspectWidth]; aw != nil {
  535. panel.SetContentAspectWidth(aw.(float32))
  536. }
  537. // Sets optional AspectHeight attribute
  538. if ah := am[AttribAspectHeight]; ah != nil {
  539. panel.SetContentAspectHeight(ah.(float32))
  540. }
  541. // Builds children recursively
  542. if am[AttribItems] != nil {
  543. items := am[AttribItems].([]map[string]interface{})
  544. for i := 0; i < len(items); i++ {
  545. item := items[i]
  546. child, err := b.build(item, panel)
  547. if err != nil {
  548. return nil, err
  549. }
  550. panel.Add(child)
  551. }
  552. }
  553. return panel, nil
  554. }
  555. // buildLabel builds a gui object of type Label
  556. func buildLabel(b *Builder, am map[string]interface{}) (IPanel, error) {
  557. var label *Label
  558. if am[AttribIcon] != nil {
  559. label = NewLabel(am[AttribIcon].(string), true)
  560. } else if am[AttribText] != nil {
  561. label = NewLabel(am[AttribText].(string))
  562. } else {
  563. label = NewLabel("")
  564. }
  565. // Sets common attributes
  566. err := b.setAttribs(am, label, asPANEL)
  567. if err != nil {
  568. return nil, err
  569. }
  570. // Set optional background color
  571. if bgc := am[AttribBgColor]; bgc != nil {
  572. label.SetBgColor4(bgc.(*math32.Color4))
  573. }
  574. // Set optional font color
  575. if fc := am[AttribFontColor]; fc != nil {
  576. label.SetColor4(fc.(*math32.Color4))
  577. }
  578. // Sets optional font size
  579. if fs := am[AttribFontSize]; fs != nil {
  580. label.SetFontSize(float64(fs.(float32)))
  581. }
  582. // Sets optional font dpi
  583. if fdpi := am[AttribFontDPI]; fdpi != nil {
  584. label.SetFontDPI(float64(fdpi.(float32)))
  585. }
  586. // Sets optional line spacing
  587. if ls := am[AttribLineSpacing]; ls != nil {
  588. label.SetLineSpacing(float64(ls.(float32)))
  589. }
  590. return label, nil
  591. }
  592. // buildImageLabel builds a gui object of type: ImageLabel
  593. func buildImageLabel(b *Builder, am map[string]interface{}) (IPanel, error) {
  594. // Builds image label and set common attributes
  595. var text string
  596. if am[AttribText] != nil {
  597. text = am[AttribText].(string)
  598. }
  599. imglabel := NewImageLabel(text)
  600. err := b.setAttribs(am, imglabel, asPANEL)
  601. if err != nil {
  602. return nil, err
  603. }
  604. // Sets optional icon(s)
  605. if icon := am[AttribIcon]; icon != nil {
  606. imglabel.SetIcon(icon.(string))
  607. }
  608. // Sets optional image from file
  609. // If path is not absolute join with user supplied image base path
  610. if imgf := am[AttribImageFile]; imgf != nil {
  611. path := imgf.(string)
  612. if !filepath.IsAbs(path) {
  613. path = filepath.Join(b.imgpath, path)
  614. }
  615. err := imglabel.SetImageFromFile(path)
  616. if err != nil {
  617. return nil, err
  618. }
  619. }
  620. return imglabel, nil
  621. }
  622. // buildButton builds a gui object of type: Button
  623. func buildButton(b *Builder, am map[string]interface{}) (IPanel, error) {
  624. // Builds button and set commont attributes
  625. var text string
  626. if am[AttribText] != nil {
  627. text = am[AttribText].(string)
  628. }
  629. button := NewButton(text)
  630. err := b.setAttribs(am, button, asWIDGET)
  631. if err != nil {
  632. return nil, err
  633. }
  634. // Sets optional icon(s)
  635. if icon := am[AttribIcon]; icon != nil {
  636. button.SetIcon(icon.(string))
  637. }
  638. // Sets optional image from file
  639. // If path is not absolute join with user supplied image base path
  640. if imgf := am[AttribImageFile]; imgf != nil {
  641. path := imgf.(string)
  642. if !filepath.IsAbs(path) {
  643. path = filepath.Join(b.imgpath, path)
  644. }
  645. err := button.SetImage(path)
  646. if err != nil {
  647. return nil, err
  648. }
  649. }
  650. return button, nil
  651. }
  652. // buildEdit builds a gui object of type: "Edit"
  653. func buildEdit(b *Builder, am map[string]interface{}) (IPanel, error) {
  654. // Builds button and set attributes
  655. var width float32
  656. var placeholder string
  657. if aw := am[AttribWidth]; aw != nil {
  658. width = aw.(float32)
  659. }
  660. if ph := am[AttribPlaceHolder]; ph != nil {
  661. placeholder = ph.(string)
  662. }
  663. edit := NewEdit(int(width), placeholder)
  664. err := b.setAttribs(am, edit, asWIDGET)
  665. if err != nil {
  666. return nil, err
  667. }
  668. return edit, nil
  669. }
  670. // buildCheckBox builds a gui object of type: CheckBox
  671. func buildCheckBox(b *Builder, am map[string]interface{}) (IPanel, error) {
  672. // Builds check box and set commont attributes
  673. var text string
  674. if am[AttribText] != nil {
  675. text = am[AttribText].(string)
  676. }
  677. cb := NewCheckBox(text)
  678. err := b.setAttribs(am, cb, asWIDGET)
  679. if err != nil {
  680. return nil, err
  681. }
  682. // Sets optional checked value
  683. if checked := am[AttribChecked]; checked != nil {
  684. cb.SetValue(checked.(bool))
  685. }
  686. return cb, nil
  687. }
  688. // buildRadioButton builds a gui object of type: RadioButton
  689. func buildRadioButton(b *Builder, am map[string]interface{}) (IPanel, error) {
  690. // Builds check box and set commont attributes
  691. var text string
  692. if am[AttribText] != nil {
  693. text = am[AttribText].(string)
  694. }
  695. rb := NewRadioButton(text)
  696. err := b.setAttribs(am, rb, asWIDGET)
  697. if err != nil {
  698. return nil, err
  699. }
  700. // Sets optional radio button group
  701. if gr := am[AttribGroup]; gr != nil {
  702. rb.SetGroup(gr.(string))
  703. }
  704. // Sets optional checked value
  705. if checked := am[AttribChecked]; checked != nil {
  706. rb.SetValue(checked.(bool))
  707. }
  708. return rb, nil
  709. }
  710. // buildVList builds a gui object of type: VList
  711. func buildVList(b *Builder, am map[string]interface{}) (IPanel, error) {
  712. // Builds list and set commont attributes
  713. list := NewVList(0, 0)
  714. err := b.setAttribs(am, list, asWIDGET)
  715. if err != nil {
  716. return nil, err
  717. }
  718. // Builds children
  719. if am[AttribItems] != nil {
  720. items := am[AttribItems].([]map[string]interface{})
  721. for i := 0; i < len(items); i++ {
  722. item := items[i]
  723. child, err := b.build(item, list)
  724. if err != nil {
  725. return nil, err
  726. }
  727. list.Add(child)
  728. }
  729. }
  730. return list, nil
  731. }
  732. // buildHList builds a gui object of type: VList
  733. func buildHList(b *Builder, am map[string]interface{}) (IPanel, error) {
  734. // Builds list and set commont attributes
  735. list := NewHList(0, 0)
  736. err := b.setAttribs(am, list, asWIDGET)
  737. if err != nil {
  738. return nil, err
  739. }
  740. // Builds children
  741. if am[AttribItems] != nil {
  742. items := am[AttribItems].([]map[string]interface{})
  743. for i := 0; i < len(items); i++ {
  744. item := items[i]
  745. child, err := b.build(item, list)
  746. if err != nil {
  747. return nil, err
  748. }
  749. list.Add(child)
  750. }
  751. }
  752. return list, nil
  753. }
  754. // buildDropDown builds a gui object of type: DropDown
  755. func buildDropDown(b *Builder, am map[string]interface{}) (IPanel, error) {
  756. // If image label attribute defined use it, otherwise
  757. // uses default value.
  758. var imglabel *ImageLabel
  759. if iv := am[AttribImageLabel]; iv != nil {
  760. imgl := iv.(map[string]interface{})
  761. imgl[AttribType] = TypeImageLabel
  762. ipan, err := b.build(imgl, nil)
  763. if err != nil {
  764. return nil, err
  765. }
  766. imglabel = ipan.(*ImageLabel)
  767. } else {
  768. imglabel = NewImageLabel("")
  769. }
  770. // Builds drop down and set common attributes
  771. dd := NewDropDown(0, imglabel)
  772. err := b.setAttribs(am, dd, asWIDGET)
  773. if err != nil {
  774. return nil, err
  775. }
  776. // Builds children
  777. if am[AttribItems] != nil {
  778. items := am[AttribItems].([]map[string]interface{})
  779. for i := 0; i < len(items); i++ {
  780. item := items[i]
  781. child, err := b.build(item, dd)
  782. if err != nil {
  783. return nil, err
  784. }
  785. dd.Add(child.(*ImageLabel))
  786. }
  787. }
  788. return dd, nil
  789. }
  790. // buildMenu builds a gui object of type: Menu or MenuBar from the
  791. // specified panel descriptor.
  792. func buildMenu(b *Builder, am map[string]interface{}) (IPanel, error) {
  793. // Builds menu bar or menu
  794. var menu *Menu
  795. if am[AttribType].(string) == TypeMenuBar {
  796. menu = NewMenuBar()
  797. } else {
  798. menu = NewMenu()
  799. }
  800. // Only sets attribs for top level menus
  801. if pi := am[AttribParent_]; pi != nil {
  802. par := pi.(map[string]interface{})
  803. ptype := ""
  804. if ti := par[AttribType]; ti != nil {
  805. ptype = ti.(string)
  806. }
  807. if ptype != TypeMenu && ptype != TypeMenuBar {
  808. err := b.setAttribs(am, menu, asWIDGET)
  809. if err != nil {
  810. return nil, err
  811. }
  812. }
  813. }
  814. // Builds and adds menu items
  815. if am[AttribItems] != nil {
  816. items := am[AttribItems].([]map[string]interface{})
  817. for i := 0; i < len(items); i++ {
  818. // Get the item optional type and text
  819. item := items[i]
  820. itype := ""
  821. itext := ""
  822. if iv := item[AttribType]; iv != nil {
  823. itype = iv.(string)
  824. }
  825. if iv := item[AttribText]; iv != nil {
  826. itext = iv.(string)
  827. }
  828. // Item is another menu
  829. if itype == TypeMenu {
  830. subm, err := buildMenu(b, item)
  831. if err != nil {
  832. return nil, err
  833. }
  834. menu.AddMenu(itext, subm.(*Menu))
  835. continue
  836. }
  837. // Item is a separator
  838. if itext == TypeSeparator {
  839. menu.AddSeparator()
  840. continue
  841. }
  842. // Item must be a menu option
  843. mi := menu.AddOption(itext)
  844. // Set item optional icon(s)
  845. if icon := item[AttribIcon]; icon != nil {
  846. mi.SetIcon(icon.(string))
  847. }
  848. // Sets optional menu item shortcut
  849. if sci := item[AttribShortcut]; sci != nil {
  850. sc := sci.([]int)
  851. mi.SetShortcut(window.ModifierKey(sc[0]), window.Key(sc[1]))
  852. }
  853. }
  854. }
  855. return menu, nil
  856. }
  857. // buildSlider builds a gui object of type: HSlider or VSlider
  858. func buildSlider(b *Builder, am map[string]interface{}) (IPanel, error) {
  859. // Builds horizontal or vertical slider
  860. var slider *Slider
  861. if am[AttribType].(string) == TypeHSlider {
  862. slider = NewHSlider(0, 0)
  863. } else {
  864. slider = NewVSlider(0, 0)
  865. }
  866. // Sets common attributes
  867. err := b.setAttribs(am, slider, asWIDGET)
  868. if err != nil {
  869. return nil, err
  870. }
  871. // Sets optional text
  872. if itext := am[AttribText]; itext != nil {
  873. slider.SetText(itext.(string))
  874. }
  875. // Sets optional scale factor
  876. if isf := am[AttribScaleFactor]; isf != nil {
  877. slider.SetScaleFactor(isf.(float32))
  878. }
  879. // Sets optional value
  880. if iv := am[AttribValue]; iv != nil {
  881. slider.SetValue(iv.(float32))
  882. }
  883. return slider, nil
  884. }
  885. // buildSplitter builds a gui object of type: HSplitterr or VSplitter
  886. func buildSplitter(b *Builder, am map[string]interface{}) (IPanel, error) {
  887. // Builds horizontal or vertical splitter
  888. var splitter *Splitter
  889. if am[AttribType].(string) == TypeHSplitter {
  890. splitter = NewHSplitter(0, 0)
  891. } else {
  892. splitter = NewVSplitter(0, 0)
  893. }
  894. // Sets common attributes
  895. err := b.setAttribs(am, splitter, asWIDGET)
  896. if err != nil {
  897. return nil, err
  898. }
  899. // Sets optional split value
  900. if v := am[AttribSplit]; v != nil {
  901. splitter.SetSplit(v.(float32))
  902. }
  903. // Internal function to set each of the splitter's panel attributes and items
  904. setpan := func(attrib string, pan *Panel) error {
  905. // Get internal panel attributes
  906. ipattribs := am[attrib]
  907. if ipattribs == nil {
  908. return nil
  909. }
  910. pattr := ipattribs.(map[string]interface{})
  911. // Set panel attributes
  912. err := b.setAttribs(pattr, pan, asPANEL)
  913. if err != nil {
  914. return nil
  915. }
  916. // Builds panel children
  917. if pattr[AttribItems] != nil {
  918. items := pattr[AttribItems].([]map[string]interface{})
  919. for i := 0; i < len(items); i++ {
  920. item := items[i]
  921. child, err := b.build(item, pan)
  922. if err != nil {
  923. return err
  924. }
  925. pan.Add(child)
  926. }
  927. }
  928. return nil
  929. }
  930. // Set optional splitter panel's attributes
  931. err = setpan(AttribPanel0, &splitter.P0)
  932. if err != nil {
  933. return nil, err
  934. }
  935. err = setpan(AttribPanel1, &splitter.P1)
  936. if err != nil {
  937. return nil, err
  938. }
  939. return splitter, nil
  940. }
  941. // buildTree builds a gui object of type: Tree
  942. func buildTree(b *Builder, am map[string]interface{}) (IPanel, error) {
  943. // Builds tree and sets its common attributes
  944. tree := NewTree(0, 0)
  945. err := b.setAttribs(am, tree, asWIDGET)
  946. if err != nil {
  947. return nil, err
  948. }
  949. // Internal function to build tree nodes recursively
  950. var buildItems func(am map[string]interface{}, pnode *TreeNode) error
  951. buildItems = func(am map[string]interface{}, pnode *TreeNode) error {
  952. v := am[AttribItems]
  953. if v == nil {
  954. return nil
  955. }
  956. items := v.([]map[string]interface{})
  957. for i := 0; i < len(items); i++ {
  958. // Get the item type
  959. item := items[i]
  960. itype := ""
  961. if v := item[AttribType]; v != nil {
  962. itype = v.(string)
  963. }
  964. itext := ""
  965. if v := item[AttribText]; v != nil {
  966. itext = v.(string)
  967. }
  968. // Item is a tree node
  969. if itype == "" || itype == TypeTreeNode {
  970. var node *TreeNode
  971. if pnode == nil {
  972. node = tree.AddNode(itext)
  973. } else {
  974. node = pnode.AddNode(itext)
  975. }
  976. err := buildItems(item, node)
  977. if err != nil {
  978. return err
  979. }
  980. continue
  981. }
  982. // Other controls
  983. ipan, err := b.build(item, nil)
  984. if err != nil {
  985. return err
  986. }
  987. if pnode == nil {
  988. tree.Add(ipan)
  989. } else {
  990. pnode.Add(ipan)
  991. }
  992. }
  993. return nil
  994. }
  995. // Build nodes
  996. err = buildItems(am, nil)
  997. if err != nil {
  998. return nil, err
  999. }
  1000. return tree, nil
  1001. }
  1002. // buildWindow builds a gui object of type: Window
  1003. func buildWindow(b *Builder, am map[string]interface{}) (IPanel, error) {
  1004. // Builds window and sets its common attributes
  1005. win := NewWindow(0, 0)
  1006. err := b.setAttribs(am, win, asWIDGET)
  1007. if err != nil {
  1008. return nil, err
  1009. }
  1010. // Sets optional title
  1011. if title := am[AttribTitle]; title != nil {
  1012. win.SetTitle(title.(string))
  1013. }
  1014. // Set optional resizable borders
  1015. if resiz := am[AttribResizable]; resiz != nil {
  1016. win.SetResizable(resiz.(Resizable))
  1017. }
  1018. // Builds window children
  1019. if v := am[AttribItems]; v != nil {
  1020. items := v.([]map[string]interface{})
  1021. for i := 0; i < len(items); i++ {
  1022. item := items[i]
  1023. child, err := b.build(item, win)
  1024. if err != nil {
  1025. return nil, err
  1026. }
  1027. win.Add(child)
  1028. }
  1029. }
  1030. return win, nil
  1031. }
  1032. // setLayout sets the optional layout of the specified panel
  1033. func (b *Builder) setLayout(am map[string]interface{}, ipan IPanel) error {
  1034. // Get layout type
  1035. lai := am[AttribLayout]
  1036. if lai == nil {
  1037. return nil
  1038. }
  1039. lam := lai.(map[string]interface{})
  1040. ltype := lam[AttribType]
  1041. if ltype == nil {
  1042. return b.err(am, AttribType, "Layout must have a type")
  1043. }
  1044. // Get layout builder
  1045. lbuilder := b.layouts[ltype.(string)]
  1046. if lbuilder == nil {
  1047. return b.err(am, AttribType, "Invalid layout type")
  1048. }
  1049. // Builds layout builder and set to panel
  1050. layout, err := lbuilder.BuildLayout(b, lam)
  1051. if err != nil {
  1052. return err
  1053. }
  1054. ipan.SetLayout(layout)
  1055. return nil
  1056. }
  1057. func (b *Builder) setLayoutParams(am map[string]interface{}, ipan IPanel) error {
  1058. // Get layout params attributes
  1059. lpi := am[AttribLayoutParams]
  1060. if lpi == nil {
  1061. return nil
  1062. }
  1063. lp := lpi.(map[string]interface{})
  1064. // Get layout type from parent
  1065. pi := am[AttribParent_]
  1066. if pi == nil {
  1067. return b.err(am, AttribType, "Panel has no parent")
  1068. }
  1069. par := pi.(map[string]interface{})
  1070. v := par[AttribLayout]
  1071. if v == nil {
  1072. return nil
  1073. }
  1074. playout := v.(map[string]interface{})
  1075. pltype := playout[AttribType].(string)
  1076. // Get layout builder and builds layout params
  1077. lbuilder := b.layouts[pltype]
  1078. params, err := lbuilder.BuildParams(b, lp)
  1079. if err != nil {
  1080. return err
  1081. }
  1082. ipan.GetPanel().SetLayoutParams(params)
  1083. return nil
  1084. }
  1085. func AttribCheckResizable(b *Builder, am map[string]interface{}, fname string) error {
  1086. // If attribute not found, ignore
  1087. v := am[fname]
  1088. if v == nil {
  1089. return nil
  1090. }
  1091. // Attribute must be string
  1092. vs, ok := v.(string)
  1093. if !ok {
  1094. return b.err(am, fname, "Invalid resizable attribute")
  1095. }
  1096. // Each string field must be a valid resizable name
  1097. parts := strings.Fields(vs)
  1098. var res Resizable
  1099. for _, name := range parts {
  1100. v, ok := mapResizable[name]
  1101. if !ok {
  1102. return b.err(am, fname, "Invalid resizable name:"+name)
  1103. }
  1104. res |= v
  1105. }
  1106. am[fname] = res
  1107. return nil
  1108. }
  1109. func AttribCheckEdge(b *Builder, am map[string]interface{}, fname string) error {
  1110. v := am[fname]
  1111. if v == nil {
  1112. return nil
  1113. }
  1114. vs, ok := v.(string)
  1115. if !ok {
  1116. return b.err(am, fname, "Invalid edge name")
  1117. }
  1118. edge, ok := mapEdgeName[vs]
  1119. if !ok {
  1120. return b.err(am, fname, "Invalid edge name")
  1121. }
  1122. am[fname] = edge
  1123. return nil
  1124. }
  1125. func AttribCheckLayout(b *Builder, am map[string]interface{}, fname string) error {
  1126. v := am[fname]
  1127. if v == nil {
  1128. return nil
  1129. }
  1130. msi, ok := v.(map[string]interface{})
  1131. if !ok {
  1132. return b.err(am, fname, "Not a map")
  1133. }
  1134. lti := msi[AttribType]
  1135. if lti == nil {
  1136. return b.err(am, fname, "Layout must have a type")
  1137. }
  1138. lfunc := b.layouts[lti.(string)]
  1139. if lfunc == nil {
  1140. return b.err(am, fname, "Invalid layout type")
  1141. }
  1142. return nil
  1143. }
  1144. func AttribCheckAlign(b *Builder, am map[string]interface{}, fname string) error {
  1145. v := am[fname]
  1146. if v == nil {
  1147. return nil
  1148. }
  1149. vs, ok := v.(string)
  1150. if !ok {
  1151. return b.err(am, fname, "Invalid alignment")
  1152. }
  1153. var align Align
  1154. if fname == AttribAlignh {
  1155. align, ok = mapAlignh[vs]
  1156. } else {
  1157. align, ok = mapAlignv[vs]
  1158. }
  1159. if !ok {
  1160. return b.err(am, fname, "Invalid alignment")
  1161. }
  1162. am[fname] = align
  1163. return nil
  1164. }
  1165. func AttribCheckMenuShortcut(b *Builder, am map[string]interface{}, fname string) error {
  1166. v := am[fname]
  1167. if v == nil {
  1168. return nil
  1169. }
  1170. vs, ok := v.(string)
  1171. if !ok {
  1172. return b.err(am, fname, "Not a string")
  1173. }
  1174. sc := strings.Trim(vs, " ")
  1175. if sc == "" {
  1176. return nil
  1177. }
  1178. parts := strings.Split(sc, "+")
  1179. var mods window.ModifierKey
  1180. for i := 0; i < len(parts)-1; i++ {
  1181. switch parts[i] {
  1182. case "Shift":
  1183. mods |= window.ModShift
  1184. case "Ctrl":
  1185. mods |= window.ModControl
  1186. case "Alt":
  1187. mods |= window.ModAlt
  1188. default:
  1189. return b.err(am, fname, "Invalid shortcut:"+sc)
  1190. }
  1191. }
  1192. // The last part must be a key
  1193. keyname := parts[len(parts)-1]
  1194. var keycode int
  1195. found := false
  1196. for kcode, kname := range mapKeyText {
  1197. if kname == keyname {
  1198. keycode = int(kcode)
  1199. found = true
  1200. break
  1201. }
  1202. }
  1203. if !found {
  1204. return b.err(am, fname, "Invalid shortcut:"+sc)
  1205. }
  1206. am[fname] = []int{int(mods), keycode}
  1207. return nil
  1208. }
  1209. func AttribCheckListMap(b *Builder, am map[string]interface{}, fname string) error {
  1210. v := am[fname]
  1211. if v == nil {
  1212. return nil
  1213. }
  1214. li, ok := v.([]interface{})
  1215. if !ok {
  1216. return b.err(am, fname, "Not a list")
  1217. }
  1218. lmsi := make([]map[string]interface{}, 0)
  1219. for i := 0; i < len(li); i++ {
  1220. item := li[i]
  1221. msi, ok := item.(map[string]interface{})
  1222. if !ok {
  1223. return b.err(am, fname, "Item is not a map")
  1224. }
  1225. lmsi = append(lmsi, msi)
  1226. }
  1227. am[fname] = lmsi
  1228. return nil
  1229. }
  1230. func AttribCheckMap(b *Builder, am map[string]interface{}, fname string) error {
  1231. v := am[fname]
  1232. if v == nil {
  1233. return nil
  1234. }
  1235. msi, ok := v.(map[string]interface{})
  1236. if !ok {
  1237. return b.err(am, fname, "Not a map")
  1238. }
  1239. am[fname] = msi
  1240. return nil
  1241. }
  1242. func AttribCheckIcons(b *Builder, am map[string]interface{}, fname string) error {
  1243. v := am[fname]
  1244. if v == nil {
  1245. return nil
  1246. }
  1247. fs, ok := v.(string)
  1248. if !ok {
  1249. return b.err(am, fname, "Not a string")
  1250. }
  1251. text := ""
  1252. parts := strings.Fields(fs)
  1253. for i := 0; i < len(parts); i++ {
  1254. // Try name first
  1255. cp := icon.Codepoint(parts[i])
  1256. if cp != "" {
  1257. text += string(cp)
  1258. continue
  1259. }
  1260. // Try to parse as hex value
  1261. val, err := strconv.ParseUint(parts[i], 16, 32)
  1262. if err != nil {
  1263. return b.err(am, fname, fmt.Sprintf("Invalid icon codepoint value/name:%v", parts[i]))
  1264. }
  1265. text += string(val)
  1266. }
  1267. am[fname] = text
  1268. return nil
  1269. }
  1270. func AttribCheckColor(b *Builder, am map[string]interface{}, fname string) error {
  1271. // Checks if field is nil
  1272. v := am[fname]
  1273. if v == nil {
  1274. return nil
  1275. }
  1276. // Converts to string
  1277. fs, ok := v.(string)
  1278. if !ok {
  1279. return b.err(am, fname, "Not a string")
  1280. }
  1281. // Checks if string field is empty
  1282. fs = strings.Trim(fs, " ")
  1283. if fs == "" {
  1284. return nil
  1285. }
  1286. // If string has 1 or 2 fields it must be a color name and optional alpha
  1287. parts := strings.Fields(fs)
  1288. if len(parts) == 1 || len(parts) == 2 {
  1289. // First part must be a color name
  1290. c, ok := math32.IsColorName(parts[0])
  1291. if !ok {
  1292. return b.err(am, fname, fmt.Sprintf("Invalid color name:%s", parts[0]))
  1293. }
  1294. c4 := math32.Color4{c.R, c.G, c.B, 1}
  1295. if len(parts) == 2 {
  1296. val, err := strconv.ParseFloat(parts[1], 32)
  1297. if err != nil {
  1298. return b.err(am, fname, fmt.Sprintf("Invalid float32 value:%s", parts[1]))
  1299. }
  1300. c4.A = float32(val)
  1301. }
  1302. am[fname] = &c4
  1303. return nil
  1304. }
  1305. // Accept 3 or 4 floats values
  1306. va, err := b.parseFloats(am, fname, 3, 4)
  1307. if err != nil {
  1308. return err
  1309. }
  1310. if len(va) == 3 {
  1311. am[fname] = &math32.Color4{va[0], va[1], va[2], 1}
  1312. return nil
  1313. }
  1314. am[fname] = &math32.Color4{va[0], va[1], va[2], va[3]}
  1315. return nil
  1316. }
  1317. func AttribCheckBorderSizes(b *Builder, am map[string]interface{}, fname string) error {
  1318. va, err := b.parseFloats(am, fname, 1, 4)
  1319. if err != nil {
  1320. return err
  1321. }
  1322. if va == nil {
  1323. return nil
  1324. }
  1325. if len(va) == 1 {
  1326. am[fname] = &BorderSizes{va[0], va[0], va[0], va[0]}
  1327. return nil
  1328. }
  1329. am[fname] = &BorderSizes{va[0], va[1], va[2], va[3]}
  1330. return nil
  1331. }
  1332. func AttribCheckPosition(b *Builder, am map[string]interface{}, fname string) error {
  1333. v := am[fname]
  1334. if v == nil {
  1335. return nil
  1336. }
  1337. af, err := b.parseFloats(am, fname, 2, 2)
  1338. if err != nil {
  1339. return err
  1340. }
  1341. am[fname] = af
  1342. return nil
  1343. }
  1344. func AttribCheckStringLower(b *Builder, am map[string]interface{}, fname string) error {
  1345. err := AttribCheckString(b, am, fname)
  1346. if err != nil {
  1347. return err
  1348. }
  1349. if v := am[fname]; v != nil {
  1350. am[fname] = strings.ToLower(v.(string))
  1351. }
  1352. return nil
  1353. }
  1354. func AttribCheckFloat(b *Builder, am map[string]interface{}, fname string) error {
  1355. v := am[fname]
  1356. if v == nil {
  1357. return nil
  1358. }
  1359. switch n := v.(type) {
  1360. case int:
  1361. am[fname] = float32(n)
  1362. return nil
  1363. case float64:
  1364. am[fname] = float32(n)
  1365. return nil
  1366. default:
  1367. return b.err(am, fname, fmt.Sprintf("Not a number:%T", v))
  1368. }
  1369. return nil
  1370. }
  1371. func AttribCheckInt(b *Builder, am map[string]interface{}, fname string) error {
  1372. v := am[fname]
  1373. if v == nil {
  1374. return nil
  1375. }
  1376. vint, ok := v.(int)
  1377. if !ok {
  1378. return b.err(am, fname, "Not an integer")
  1379. }
  1380. am[fname] = vint
  1381. return nil
  1382. }
  1383. func AttribCheckString(b *Builder, am map[string]interface{}, fname string) error {
  1384. v := am[fname]
  1385. if v == nil {
  1386. return nil
  1387. }
  1388. s, ok := v.(string)
  1389. if !ok {
  1390. return b.err(am, fname, "Not a string")
  1391. }
  1392. am[fname] = s
  1393. return nil
  1394. }
  1395. func AttribCheckBool(b *Builder, am map[string]interface{}, fname string) error {
  1396. v := am[fname]
  1397. if v == nil {
  1398. return nil
  1399. }
  1400. bv, ok := v.(bool)
  1401. if !ok {
  1402. return b.err(am, fname, "Not a bool")
  1403. }
  1404. am[fname] = bv
  1405. return nil
  1406. }
  1407. /***
  1408. // buildImageLabel builds a gui object of type: ImageLabel
  1409. func (b *Builder) buildImageLabel(pd *descPanel) (IPanel, error) {
  1410. // Builds image label and set common attributes
  1411. imglabel := NewImageLabel(pd.Text)
  1412. err := b.setAttribs(pd, imglabel, asPANEL)
  1413. if err != nil {
  1414. return nil, err
  1415. }
  1416. // Sets optional icon(s)
  1417. icons, err := b.parseIconNames("icons", pd.Icons)
  1418. if err != nil {
  1419. return nil, err
  1420. }
  1421. if icons != "" {
  1422. imglabel.SetIcon(icons)
  1423. }
  1424. // Sets optional image from file
  1425. // If path is not absolute join with user supplied image base path
  1426. if pd.Imagefile != "" {
  1427. path := pd.Imagefile
  1428. if !filepath.IsAbs(path) {
  1429. path = filepath.Join(b.imgpath, path)
  1430. }
  1431. err := imglabel.SetImageFromFile(path)
  1432. if err != nil {
  1433. return nil, err
  1434. }
  1435. }
  1436. return imglabel, nil
  1437. }
  1438. // buildButton builds a gui object of type: Button
  1439. func (b *Builder) buildButton(pd *descPanel) (IPanel, error) {
  1440. // Builds button and set commont attributes
  1441. button := NewButton(pd.Text)
  1442. err := b.setAttribs(pd, button, asBUTTON)
  1443. if err != nil {
  1444. return nil, err
  1445. }
  1446. // Sets optional icon
  1447. if pd.Icon != "" {
  1448. cp, err := b.parseIconName("icon", pd.Icon)
  1449. if err != nil {
  1450. return nil, err
  1451. }
  1452. button.SetIcon(cp)
  1453. }
  1454. // Sets optional image from file
  1455. // If path is not absolute join with user supplied image base path
  1456. if pd.Imagefile != "" {
  1457. path := pd.Imagefile
  1458. if !filepath.IsAbs(path) {
  1459. path = filepath.Join(b.imgpath, path)
  1460. }
  1461. err := button.SetImage(path)
  1462. if err != nil {
  1463. return nil, err
  1464. }
  1465. }
  1466. return button, nil
  1467. }
  1468. // buildCheckBox builds a gui object of type: CheckBox
  1469. func (b *Builder) buildCheckBox(pd *descPanel) (IPanel, error) {
  1470. // Builds check box and set commont attributes
  1471. cb := NewCheckBox(pd.Text)
  1472. err := b.setAttribs(pd, cb, asWIDGET)
  1473. if err != nil {
  1474. return nil, err
  1475. }
  1476. cb.SetValue(pd.Checked)
  1477. return cb, nil
  1478. }
  1479. // buildRadioButton builds a gui object of type: RadioButton
  1480. func (b *Builder) buildRadioButton(pd *descPanel) (IPanel, error) {
  1481. // Builds check box and set commont attributes
  1482. rb := NewRadioButton(pd.Text)
  1483. err := b.setAttribs(pd, rb, asWIDGET)
  1484. if err != nil {
  1485. return nil, err
  1486. }
  1487. // Sets optional radio button group
  1488. if pd.Group != "" {
  1489. rb.SetGroup(pd.Group)
  1490. }
  1491. rb.SetValue(pd.Checked)
  1492. return rb, nil
  1493. }
  1494. // buildEdit builds a gui object of type: "Edit"
  1495. func (b *Builder) buildEdit(dp *descPanel) (IPanel, error) {
  1496. // Builds button and set attributes
  1497. width, _ := b.size(dp)
  1498. edit := NewEdit(int(width), dp.PlaceHolder)
  1499. err := b.setAttribs(dp, edit, asWIDGET)
  1500. if err != nil {
  1501. return nil, err
  1502. }
  1503. edit.SetText(dp.Text)
  1504. return edit, nil
  1505. }
  1506. // buildVList builds a gui object of type: VList
  1507. func (b *Builder) buildVList(dp *descPanel) (IPanel, error) {
  1508. // Builds list and set commont attributes
  1509. width, height := b.size(dp)
  1510. list := NewVList(width, height)
  1511. err := b.setAttribs(dp, list, asWIDGET)
  1512. if err != nil {
  1513. return nil, err
  1514. }
  1515. // Builds list children
  1516. for i := 0; i < len(dp.Items); i++ {
  1517. item := dp.Items[i]
  1518. b.objpath.push(item.Name)
  1519. child, err := b.build(item, list)
  1520. b.objpath.pop()
  1521. if err != nil {
  1522. return nil, err
  1523. }
  1524. list.Add(child)
  1525. }
  1526. return list, nil
  1527. }
  1528. // buildHList builds a gui object of type: VList
  1529. func (b *Builder) buildHList(dp *descPanel) (IPanel, error) {
  1530. // Builds list and set commont attributes
  1531. width, height := b.size(dp)
  1532. list := NewHList(width, height)
  1533. err := b.setAttribs(dp, list, asWIDGET)
  1534. if err != nil {
  1535. return nil, err
  1536. }
  1537. // Builds list children
  1538. for i := 0; i < len(dp.Items); i++ {
  1539. item := dp.Items[i]
  1540. b.objpath.push(item.Name)
  1541. child, err := b.build(item, list)
  1542. b.objpath.pop()
  1543. if err != nil {
  1544. return nil, err
  1545. }
  1546. list.Add(child)
  1547. }
  1548. return list, nil
  1549. }
  1550. // buildDropDown builds a gui object of type: DropDown
  1551. func (b *Builder) buildDropDown(pd *descPanel) (IPanel, error) {
  1552. // If image label attribute defined use it, otherwise
  1553. // uses default value.
  1554. var imglabel *ImageLabel
  1555. if pd.ImageLabel != nil {
  1556. pd.ImageLabel.Type = descTypeImageLabel
  1557. ipan, err := b.build(pd.ImageLabel, nil)
  1558. if err != nil {
  1559. return nil, err
  1560. }
  1561. imglabel = ipan.(*ImageLabel)
  1562. } else {
  1563. imglabel = NewImageLabel("")
  1564. }
  1565. // Builds drop down and set common attributes
  1566. width, _ := b.size(pd)
  1567. dd := NewDropDown(width, imglabel)
  1568. err := b.setAttribs(pd, dd, asWIDGET)
  1569. if err != nil {
  1570. return nil, err
  1571. }
  1572. // Builds drop down children
  1573. for i := 0; i < len(pd.Items); i++ {
  1574. item := pd.Items[i]
  1575. item.Type = descTypeImageLabel
  1576. b.objpath.push(item.Name)
  1577. child, err := b.build(item, dd)
  1578. b.objpath.pop()
  1579. if err != nil {
  1580. return nil, err
  1581. }
  1582. dd.Add(child.(*ImageLabel))
  1583. }
  1584. return dd, nil
  1585. }
  1586. // buildSlider builds a gui object of type: HSlider or VSlider
  1587. func (b *Builder) buildSlider(pd *descPanel, horiz bool) (IPanel, error) {
  1588. // Builds slider and sets its position
  1589. width, height := b.size(pd)
  1590. var slider *Slider
  1591. if horiz {
  1592. slider = NewHSlider(width, height)
  1593. } else {
  1594. slider = NewVSlider(width, height)
  1595. }
  1596. err := b.setAttribs(pd, slider, asWIDGET)
  1597. if err != nil {
  1598. return nil, err
  1599. }
  1600. // Sets optional text
  1601. if pd.Text != "" {
  1602. slider.SetText(pd.Text)
  1603. }
  1604. // Sets optional scale factor
  1605. if pd.ScaleFactor != nil {
  1606. slider.SetScaleFactor(*pd.ScaleFactor)
  1607. }
  1608. // Sets optional value
  1609. if pd.Value != nil {
  1610. slider.SetValue(*pd.Value)
  1611. }
  1612. return slider, nil
  1613. }
  1614. // buildSplitter builds a gui object of type: HSplitterr or VSplitter
  1615. func (b *Builder) buildSplitter(pd *descPanel, horiz bool) (IPanel, error) {
  1616. // Builds splitter and sets its common attributes
  1617. width, height := b.size(pd)
  1618. var splitter *Splitter
  1619. if horiz {
  1620. splitter = NewHSplitter(width, height)
  1621. } else {
  1622. splitter = NewVSplitter(width, height)
  1623. }
  1624. err := b.setAttribs(pd, splitter, asWIDGET)
  1625. if err != nil {
  1626. return nil, err
  1627. }
  1628. // Optional split value
  1629. if pd.Split != nil {
  1630. splitter.SetSplit(*pd.Split)
  1631. }
  1632. // Splitter panel 0 attributes and items
  1633. if pd.P0 != nil {
  1634. err := b.setAttribs(pd.P0, &splitter.P0, asPANEL)
  1635. if err != nil {
  1636. return nil, err
  1637. }
  1638. err = b.addPanelItems(pd.P0, &splitter.P0)
  1639. if err != nil {
  1640. return nil, err
  1641. }
  1642. }
  1643. // Splitter panel 1 attributes and items
  1644. if pd.P1 != nil {
  1645. err := b.setAttribs(pd.P1, &splitter.P1, asPANEL)
  1646. if err != nil {
  1647. return nil, err
  1648. }
  1649. err = b.addPanelItems(pd.P1, &splitter.P1)
  1650. if err != nil {
  1651. return nil, err
  1652. }
  1653. }
  1654. return splitter, nil
  1655. }
  1656. // buildTree builds a gui object of type: Tree
  1657. func (b *Builder) buildTree(dp *descPanel) (IPanel, error) {
  1658. // Builds tree and sets its common attributes
  1659. width, height := b.size(dp)
  1660. tree := NewTree(width, height)
  1661. err := b.setAttribs(dp, tree, asWIDGET)
  1662. if err != nil {
  1663. return nil, err
  1664. }
  1665. // Internal function to build tree nodes recursively
  1666. var buildItems func(dp *descPanel, pnode *TreeNode) error
  1667. buildItems = func(dp *descPanel, pnode *TreeNode) error {
  1668. for i := 0; i < len(dp.Items); i++ {
  1669. item := dp.Items[i]
  1670. // Item is a tree node
  1671. if item.Type == "" || item.Type == descTypeTreeNode {
  1672. var node *TreeNode
  1673. if pnode == nil {
  1674. node = tree.AddNode(item.Text)
  1675. } else {
  1676. node = pnode.AddNode(item.Text)
  1677. }
  1678. err := buildItems(item, node)
  1679. if err != nil {
  1680. return err
  1681. }
  1682. continue
  1683. }
  1684. // Other controls
  1685. ipan, err := b.build(item, nil)
  1686. if err != nil {
  1687. return err
  1688. }
  1689. if pnode == nil {
  1690. tree.Add(ipan)
  1691. } else {
  1692. pnode.Add(ipan)
  1693. }
  1694. }
  1695. return nil
  1696. }
  1697. // Build nodes
  1698. err = buildItems(dp, nil)
  1699. if err != nil {
  1700. return nil, err
  1701. }
  1702. return tree, nil
  1703. }
  1704. // buildMenu builds a gui object of type: Menu or MenuBar from the
  1705. // specified panel descriptor.
  1706. func (b *Builder) buildMenu(pd *descPanel, child, bar bool) (IPanel, error) {
  1707. // Builds menu bar or menu
  1708. var menu *Menu
  1709. if bar {
  1710. menu = NewMenuBar()
  1711. } else {
  1712. menu = NewMenu()
  1713. }
  1714. // Only sets attribs for top level menus
  1715. if !child {
  1716. err := b.setAttribs(pd, menu, asWIDGET)
  1717. if err != nil {
  1718. return nil, err
  1719. }
  1720. }
  1721. // Builds and adds menu items
  1722. for i := 0; i < len(pd.Items); i++ {
  1723. item := pd.Items[i]
  1724. // Item is another menu
  1725. if item.Type == descTypeMenu {
  1726. subm, err := b.buildMenu(item, true, false)
  1727. if err != nil {
  1728. return nil, err
  1729. }
  1730. menu.AddMenu(item.Text, subm.(*Menu))
  1731. continue
  1732. }
  1733. // Item is a separator
  1734. if item.Type == "Separator" {
  1735. menu.AddSeparator()
  1736. continue
  1737. }
  1738. // Item must be a menu option
  1739. mi := menu.AddOption(item.Text)
  1740. // Set item optional icon(s)
  1741. icons, err := b.parseIconNames("icon", item.Icon)
  1742. if err != nil {
  1743. return nil, err
  1744. }
  1745. if icons != "" {
  1746. mi.SetIcon(string(icons))
  1747. }
  1748. // Sets optional menu item shortcut
  1749. err = b.setMenuShortcut(mi, "shortcut", item.Shortcut)
  1750. if err != nil {
  1751. return nil, err
  1752. }
  1753. }
  1754. return menu, nil
  1755. }
  1756. // buildWindow builds a gui object of type: Window from the
  1757. // specified panel descriptor.
  1758. func (b *Builder) buildWindow(dp *descPanel) (IPanel, error) {
  1759. // Builds window and sets its common attributes
  1760. width, height := b.size(dp)
  1761. win := NewWindow(width, height)
  1762. err := b.setAttribs(dp, win, asWIDGET)
  1763. if err != nil {
  1764. return nil, err
  1765. }
  1766. // Title attribute
  1767. win.SetTitle(dp.Title)
  1768. // Parse resizable borders
  1769. if dp.Resizable != "" {
  1770. parts := strings.Fields(dp.Resizable)
  1771. var res Resizable
  1772. for _, name := range parts {
  1773. v, ok := mapResizable[name]
  1774. if !ok {
  1775. return nil, b.err("resizable", "Invalid resizable name:"+name)
  1776. }
  1777. res |= v
  1778. }
  1779. win.SetResizable(res)
  1780. }
  1781. // Builds window client panel children recursively
  1782. for i := 0; i < len(dp.Items); i++ {
  1783. item := dp.Items[i]
  1784. b.objpath.push(item.Name)
  1785. child, err := b.build(item, win)
  1786. b.objpath.pop()
  1787. if err != nil {
  1788. return nil, err
  1789. }
  1790. win.Add(child)
  1791. }
  1792. return win, nil
  1793. }
  1794. // addPanelItems adds the items in the panel descriptor to the specified panel
  1795. func (b *Builder) addPanelItems(dp *descPanel, ipan IPanel) error {
  1796. pan := ipan.GetPanel()
  1797. for i := 0; i < len(dp.Items); i++ {
  1798. item := dp.Items[i]
  1799. b.objpath.push(item.Name)
  1800. child, err := b.build(item, pan)
  1801. b.objpath.pop()
  1802. if err != nil {
  1803. return err
  1804. }
  1805. pan.Add(child)
  1806. }
  1807. return nil
  1808. }
  1809. // setAttribs sets common attributes from the description to the specified panel
  1810. // The attributes which are set can be specified by the specified bitmask.
  1811. func (b *Builder) setAttribs(pd *descPanel, ipan IPanel, attr uint) error {
  1812. panel := ipan.GetPanel()
  1813. // Set optional position
  1814. if attr&aPOS != 0 && pd.Position != "" {
  1815. va, err := b.parseFloats("position", pd.Position, 2, 2)
  1816. if va == nil || err != nil {
  1817. return err
  1818. }
  1819. panel.SetPosition(va[0], va[1])
  1820. }
  1821. // Set optional size
  1822. if attr&aSIZE != 0 {
  1823. if pd.Width != nil {
  1824. panel.SetWidth(*pd.Width)
  1825. }
  1826. if pd.Height != nil {
  1827. panel.SetHeight(*pd.Height)
  1828. }
  1829. }
  1830. // Set optional margin sizes
  1831. if attr&aMARGINS != 0 {
  1832. bs, err := b.parseBorderSizes(fieldMargins, pd.Margins)
  1833. if err != nil {
  1834. return err
  1835. }
  1836. if bs != nil {
  1837. panel.SetMarginsFrom(bs)
  1838. }
  1839. }
  1840. // Set optional border sizes
  1841. if attr&aBORDERS != 0 {
  1842. bs, err := b.parseBorderSizes(fieldBorders, pd.Borders)
  1843. if err != nil {
  1844. return err
  1845. }
  1846. if bs != nil {
  1847. panel.SetBordersFrom(bs)
  1848. }
  1849. }
  1850. // Set optional border color
  1851. if attr&aBORDERCOLOR != 0 {
  1852. c, err := b.parseColor(fieldBorderColor, pd.BorderColor)
  1853. if err != nil {
  1854. return err
  1855. }
  1856. if c != nil {
  1857. panel.SetBordersColor4(c)
  1858. }
  1859. }
  1860. // Set optional paddings sizes
  1861. if attr&aPADDINGS != 0 {
  1862. bs, err := b.parseBorderSizes(fieldPaddings, pd.Paddings)
  1863. if err != nil {
  1864. return err
  1865. }
  1866. if bs != nil {
  1867. panel.SetPaddingsFrom(bs)
  1868. }
  1869. }
  1870. // Set optional color
  1871. if attr&aCOLOR != 0 {
  1872. c, err := b.parseColor(fieldColor, pd.Color)
  1873. if err != nil {
  1874. return err
  1875. }
  1876. if c != nil {
  1877. panel.SetColor4(c)
  1878. }
  1879. }
  1880. if attr&aNAME != 0 && pd.Name != "" {
  1881. panel.SetName(pd.Name)
  1882. }
  1883. if attr&aVISIBLE != 0 && pd.Visible != nil {
  1884. panel.SetVisible(*pd.Visible)
  1885. }
  1886. if attr&aENABLED != 0 && pd.Enabled != nil {
  1887. panel.SetEnabled(*pd.Enabled)
  1888. }
  1889. if attr&aRENDER != 0 && pd.Renderable != nil {
  1890. panel.SetRenderable(*pd.Renderable)
  1891. }
  1892. err := b.setLayoutParams(pd, ipan)
  1893. if err != nil {
  1894. return err
  1895. }
  1896. return b.setLayout(pd, ipan)
  1897. }
  1898. // setLayoutParams sets the optional layout params attribute for specified the panel
  1899. func (b *Builder) setLayoutParams(dp *descPanel, ipan IPanel) error {
  1900. // If layout params not declared, nothing to do
  1901. if dp.LayoutParams == nil {
  1902. return nil
  1903. }
  1904. // Get the parent layout
  1905. if dp.parent == nil {
  1906. return b.err("layoutparams", "No parent defined")
  1907. }
  1908. playout := dp.parent.Layout
  1909. if playout == nil {
  1910. return b.err("layoutparams", "Parent does not have layout")
  1911. }
  1912. panel := ipan.GetPanel()
  1913. dlp := dp.LayoutParams
  1914. // HBoxLayout parameters
  1915. if playout.Type == descTypeHBoxLayout {
  1916. // Creates layout parameter
  1917. params := HBoxLayoutParams{Expand: 0, AlignV: AlignTop}
  1918. // Sets optional expand parameter
  1919. if dlp.Expand != nil {
  1920. params.Expand = *dlp.Expand
  1921. }
  1922. // Sets optional align parameter
  1923. if dlp.AlignV != "" {
  1924. align, ok := mapAlignName[dlp.AlignV]
  1925. if !ok {
  1926. return b.err("align", "Invalid align name:"+dlp.AlignV)
  1927. }
  1928. params.AlignV = align
  1929. }
  1930. panel.SetLayoutParams(&params)
  1931. return nil
  1932. }
  1933. // VBoxLayout parameters
  1934. if playout.Type == descTypeVBoxLayout {
  1935. // Creates layout parameter
  1936. params := VBoxLayoutParams{Expand: 0, AlignH: AlignLeft}
  1937. // Sets optional expand parameter
  1938. if dlp.Expand != nil {
  1939. params.Expand = *dlp.Expand
  1940. }
  1941. // Sets optional align parameter
  1942. if dlp.AlignH != "" {
  1943. align, ok := mapAlignName[dlp.AlignH]
  1944. if !ok {
  1945. return b.err("align", "Invalid align name:"+dlp.AlignH)
  1946. }
  1947. params.AlignH = align
  1948. }
  1949. panel.SetLayoutParams(&params)
  1950. return nil
  1951. }
  1952. // GridLayout parameters
  1953. if playout.Type == descTypeGridLayout {
  1954. // Creates layout parameter
  1955. params := GridLayoutParams{
  1956. ColSpan: 0,
  1957. AlignH: AlignNone,
  1958. AlignV: AlignNone,
  1959. }
  1960. params.ColSpan = dlp.ColSpan
  1961. // Sets optional alignh parameter
  1962. if dlp.AlignH != "" {
  1963. align, ok := mapAlignName[dlp.AlignH]
  1964. if !ok {
  1965. return b.err("alignh", "Invalid align name:"+dlp.AlignH)
  1966. }
  1967. params.AlignH = align
  1968. }
  1969. // Sets optional alignv parameter
  1970. if dlp.AlignV != "" {
  1971. align, ok := mapAlignName[dlp.AlignV]
  1972. if !ok {
  1973. return b.err("alignv", "Invalid align name:"+dlp.AlignV)
  1974. }
  1975. params.AlignV = align
  1976. }
  1977. panel.SetLayoutParams(&params)
  1978. return nil
  1979. }
  1980. // DockLayout parameters
  1981. if playout.Type == descTypeDockLayout {
  1982. if dlp.Edge != "" {
  1983. edge, ok := mapEdgeName[dlp.Edge]
  1984. if !ok {
  1985. return b.err("edge", "Invalid edge name:"+dlp.Edge)
  1986. }
  1987. params := DockLayoutParams{Edge: edge}
  1988. panel.SetLayoutParams(&params)
  1989. return nil
  1990. }
  1991. }
  1992. return b.err("layoutparams", "Invalid parent layout:"+playout.Type)
  1993. }
  1994. // setLayout sets the optional panel layout and layout parameters
  1995. func (b *Builder) setLayout(dp *descPanel, ipan IPanel) error {
  1996. // If layout types not declared, nothing to do
  1997. if dp.Layout == nil {
  1998. return nil
  1999. }
  2000. dl := dp.Layout
  2001. // HBox layout
  2002. if dl.Type == descTypeHBoxLayout {
  2003. hbl := NewHBoxLayout()
  2004. hbl.SetSpacing(dl.Spacing)
  2005. if dl.AlignH != "" {
  2006. align, ok := mapAlignName[dl.AlignH]
  2007. if !ok {
  2008. return b.err("align", "Invalid align name:"+dl.AlignV)
  2009. }
  2010. hbl.SetAlignH(align)
  2011. }
  2012. hbl.SetMinHeight(dl.MinHeight)
  2013. hbl.SetMinWidth(dl.MinWidth)
  2014. ipan.SetLayout(hbl)
  2015. return nil
  2016. }
  2017. // VBox layout
  2018. if dl.Type == descTypeVBoxLayout {
  2019. vbl := NewVBoxLayout()
  2020. vbl.SetSpacing(dl.Spacing)
  2021. if dl.AlignV != "" {
  2022. align, ok := mapAlignName[dl.AlignV]
  2023. if !ok {
  2024. return b.err("align", "Invalid align name:"+dl.AlignV)
  2025. }
  2026. vbl.SetAlignV(align)
  2027. }
  2028. vbl.SetMinHeight(dl.MinHeight)
  2029. vbl.SetMinWidth(dl.MinWidth)
  2030. ipan.SetLayout(vbl)
  2031. return nil
  2032. }
  2033. // Grid layout
  2034. if dl.Type == descTypeGridLayout {
  2035. // Number of columns
  2036. if dl.Cols == 0 {
  2037. return b.err("cols", "Invalid number of columns:"+dl.AlignH)
  2038. }
  2039. grl := NewGridLayout(dl.Cols)
  2040. // Global horizontal alignment
  2041. if dl.AlignH != "" {
  2042. alignh, ok := mapAlignName[dl.AlignH]
  2043. if !ok {
  2044. return b.err("alignh", "Invalid horizontal align:"+dl.AlignH)
  2045. }
  2046. grl.SetAlignH(alignh)
  2047. }
  2048. // Global vertical alignment
  2049. if dl.AlignV != "" {
  2050. alignv, ok := mapAlignName[dl.AlignV]
  2051. if !ok {
  2052. return b.err("alignv", "Invalid vertical align:"+dl.AlignH)
  2053. }
  2054. grl.SetAlignV(alignv)
  2055. }
  2056. // Expansion flags
  2057. grl.SetExpandH(dl.ExpandH)
  2058. grl.SetExpandV(dl.ExpandV)
  2059. ipan.SetLayout(grl)
  2060. return nil
  2061. }
  2062. // Dock layout
  2063. if dl.Type == descTypeDockLayout {
  2064. dockl := NewDockLayout()
  2065. ipan.SetLayout(dockl)
  2066. return nil
  2067. }
  2068. return b.err("layout", "Invalid layout type:"+dl.Type)
  2069. }
  2070. ****/
  2071. // setAttribs sets common attributes from the description to the specified panel
  2072. // The attributes which are set can be specified by the specified bitmask.
  2073. func (b *Builder) setAttribs(am map[string]interface{}, ipan IPanel, attr uint) error {
  2074. panel := ipan.GetPanel()
  2075. // Set optional position
  2076. if attr&aPOS != 0 && am[AttribPosition] != nil {
  2077. va := am[AttribPosition].([]float32)
  2078. panel.SetPosition(va[0], va[1])
  2079. }
  2080. // Set optional panel width
  2081. if attr&aSIZE != 0 && am[AttribWidth] != nil {
  2082. panel.SetWidth(am[AttribWidth].(float32))
  2083. log.Error("set width:%v", am[AttribWidth])
  2084. }
  2085. // Sets optional panel height
  2086. if attr&aSIZE != 0 && am[AttribHeight] != nil {
  2087. panel.SetHeight(am[AttribHeight].(float32))
  2088. }
  2089. // Set optional margin sizes
  2090. if attr&aMARGINS != 0 && am[AttribMargins] != nil {
  2091. panel.SetMarginsFrom(am[AttribMargins].(*BorderSizes))
  2092. }
  2093. // Set optional border sizes
  2094. if attr&aBORDERS != 0 && am[AttribBorders] != nil {
  2095. panel.SetBordersFrom(am[AttribBorders].(*BorderSizes))
  2096. }
  2097. // Set optional border color
  2098. if attr&aBORDERCOLOR != 0 && am[AttribBorderColor] != nil {
  2099. panel.SetBordersColor4(am[AttribBorderColor].(*math32.Color4))
  2100. }
  2101. // Set optional paddings sizes
  2102. if attr&aPADDINGS != 0 && am[AttribPaddings] != nil {
  2103. panel.SetPaddingsFrom(am[AttribPaddings].(*BorderSizes))
  2104. }
  2105. // Set optional panel color
  2106. if attr&aCOLOR != 0 && am[AttribColor] != nil {
  2107. panel.SetColor4(am[AttribColor].(*math32.Color4))
  2108. }
  2109. if attr&aNAME != 0 && am[AttribName] != nil {
  2110. panel.SetName(am[AttribName].(string))
  2111. }
  2112. if attr&aVISIBLE != 0 && am[AttribVisible] != nil {
  2113. panel.SetVisible(am[AttribVisible].(bool))
  2114. }
  2115. if attr&aENABLED != 0 && am[AttribEnabled] != nil {
  2116. panel.SetEnabled(am[AttribEnabled].(bool))
  2117. }
  2118. if attr&aRENDER != 0 && am[AttribRender] != nil {
  2119. panel.SetRenderable(am[AttribRender].(bool))
  2120. }
  2121. // Sets optional layout (must pass IPanel not *Panel)
  2122. err := b.setLayout(am, ipan)
  2123. if err != nil {
  2124. return nil
  2125. }
  2126. // Sets optional layout params
  2127. err = b.setLayoutParams(am, panel)
  2128. return err
  2129. }
  2130. //func (b *Builder) setMenuShortcut(mi *MenuItem, fname, field string) error {
  2131. //
  2132. // field = strings.Trim(field, " ")
  2133. // if field == "" {
  2134. // return nil
  2135. // }
  2136. // parts := strings.Split(field, "+")
  2137. // var mods window.ModifierKey
  2138. // for i := 0; i < len(parts)-1; i++ {
  2139. // switch parts[i] {
  2140. // case "Shift":
  2141. // mods |= window.ModShift
  2142. // case "Ctrl":
  2143. // mods |= window.ModControl
  2144. // case "Alt":
  2145. // mods |= window.ModAlt
  2146. // default:
  2147. // return b.err(am, fname, "Invalid shortcut:"+field)
  2148. // }
  2149. // }
  2150. // // The last part must be a key
  2151. // key := parts[len(parts)-1]
  2152. // for kcode, kname := range mapKeyText {
  2153. // if kname == key {
  2154. // mi.SetShortcut(mods, kcode)
  2155. // return nil
  2156. // }
  2157. // }
  2158. // return b.err(fname, "Invalid shortcut:"+field)
  2159. //}
  2160. // parseFloats parses a string with a list of floats with the specified size
  2161. // and returns a slice. The specified size is 0 any number of floats is allowed.
  2162. // The individual values can be separated by spaces or commas
  2163. func (b *Builder) parseFloats(am map[string]interface{}, fname string, min, max int) ([]float32, error) {
  2164. // Checks if field is empty
  2165. v := am[fname]
  2166. if v == nil {
  2167. return nil, nil
  2168. }
  2169. // If field has only one value, it is an int or a float64
  2170. switch ft := v.(type) {
  2171. case int:
  2172. return []float32{float32(ft)}, nil
  2173. case float64:
  2174. return []float32{float32(ft)}, nil
  2175. }
  2176. // Converts to string
  2177. fs, ok := v.(string)
  2178. if !ok {
  2179. return nil, b.err(am, fname, "Not a string")
  2180. }
  2181. // Checks if string field is empty
  2182. fs = strings.Trim(fs, " ")
  2183. if fs == "" {
  2184. return nil, nil
  2185. }
  2186. // Separate individual fields
  2187. var parts []string
  2188. if strings.Index(fs, ",") < 0 {
  2189. parts = strings.Fields(fs)
  2190. } else {
  2191. parts = strings.Split(fs, ",")
  2192. }
  2193. if len(parts) < min || len(parts) > max {
  2194. return nil, b.err(am, fname, "Invalid number of float32 values")
  2195. }
  2196. // Parse each field value and appends to slice
  2197. var values []float32
  2198. for i := 0; i < len(parts); i++ {
  2199. val, err := strconv.ParseFloat(strings.Trim(parts[i], " "), 32)
  2200. if err != nil {
  2201. return nil, b.err(am, fname, err.Error())
  2202. }
  2203. values = append(values, float32(val))
  2204. }
  2205. return values, nil
  2206. }
  2207. // err creates and returns an error for the current object, field name and with the specified message
  2208. func (b *Builder) err(am map[string]interface{}, fname, msg string) error {
  2209. return fmt.Errorf("Error in object:%s field:%s -> %s", am[AttribName], fname, msg)
  2210. }
  2211. // debugPrint prints the internal attribute map of the builder for debugging.
  2212. // This map cannot be printed by fmt.Printf() because it has cycles.
  2213. // A map contains a key: _parent, which pointer to is parent map, if any.
  2214. func (b *Builder) debugPrint(v interface{}, level int) {
  2215. switch vt := v.(type) {
  2216. case map[string]interface{}:
  2217. level += 3
  2218. fmt.Printf("\n")
  2219. for mk, mv := range vt {
  2220. if mk == AttribParent_ {
  2221. continue
  2222. }
  2223. fmt.Printf("%s%s:", strings.Repeat(" ", level), mk)
  2224. b.debugPrint(mv, level)
  2225. }
  2226. case []map[string]interface{}:
  2227. for _, v := range vt {
  2228. b.debugPrint(v, level)
  2229. }
  2230. default:
  2231. fmt.Printf(" %v (%T)\n", vt, vt)
  2232. }
  2233. }