tree.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. "github.com/g3n/engine/math32"
  7. "github.com/g3n/engine/window"
  8. )
  9. type Tree struct {
  10. List // Embedded list panel
  11. styles *TreeStyles // Pointer to styles
  12. }
  13. type TreeStyles struct {
  14. List *ListStyles // Styles for the embedded list
  15. Node *TreeNodeStyles // Styles for the node panel
  16. Padlevel float32 // Left padding indentation
  17. }
  18. type TreeNodeStyles struct {
  19. Normal TreeNodeStyle
  20. }
  21. type TreeNodeStyle struct {
  22. Margins BorderSizes
  23. Border BorderSizes
  24. Paddings BorderSizes
  25. BorderColor math32.Color4
  26. BgColor math32.Color
  27. FgColor math32.Color
  28. Icons [2]int
  29. }
  30. type TreeNode struct {
  31. Panel // Embedded panel
  32. label Label // Node label
  33. icon Label // Node icon
  34. tree *Tree // Parent tree
  35. parNode *TreeNode // Parent node
  36. items []IPanel // List of node items
  37. expanded bool // Node expanded flag
  38. }
  39. // NewTree creates and returns a pointer to a new tree widget
  40. func NewTree(width, height float32) *Tree {
  41. t := new(Tree)
  42. t.Initialize(width, height)
  43. return t
  44. }
  45. // Initialize initializes the tree with the specified initial width and height
  46. // It is normally used when the folder is embedded in another object
  47. func (t *Tree) Initialize(width, height float32) {
  48. t.List.initialize(true, width, height)
  49. t.SetStyles(&StyleDefault.Tree)
  50. t.List.Subscribe(OnKeyDown, t.onKey)
  51. t.List.Subscribe(OnKeyUp, t.onKey)
  52. }
  53. // SetStyles set the tree styles overriding the default style
  54. func (t *Tree) SetStyles(s *TreeStyles) {
  55. t.styles = s
  56. t.List.SetStyles(t.styles.List)
  57. t.update()
  58. }
  59. // InsertAt inserts a child panel at the specified position in the tree
  60. func (t *Tree) InsertAt(pos int, child IPanel) {
  61. t.List.InsertAt(pos, child)
  62. }
  63. // Add child panel to the end tree
  64. func (t *Tree) Add(ichild IPanel) {
  65. t.List.Add(ichild)
  66. }
  67. // InsertNodeAt inserts at the specified position a new tree node
  68. // with the specified text at the end of this tree
  69. // and returns pointer to the new node
  70. func (t *Tree) InsertNodeAt(pos int, text string) *TreeNode {
  71. n := newTreeNode(text, t, nil)
  72. n.update()
  73. n.recalc()
  74. t.List.InsertAt(pos, n)
  75. return n
  76. }
  77. // Add adds a new tree node with the specified text
  78. // at the end of this tree and returns pointer to the new node
  79. func (t *Tree) AddNode(text string) *TreeNode {
  80. n := newTreeNode(text, t, nil)
  81. n.update()
  82. n.recalc()
  83. t.List.Add(n)
  84. return n
  85. }
  86. // Remove removes the specified child from the tree or any
  87. // of its children nodes.
  88. func (t *Tree) Remove(child IPanel) {
  89. for idx := 0; idx < t.List.Len(); idx++ {
  90. curr := t.List.ItemAt(idx)
  91. if curr == child {
  92. node, ok := curr.(*TreeNode)
  93. if ok {
  94. node.remove()
  95. } else {
  96. t.List.Remove(child)
  97. }
  98. return
  99. }
  100. node, ok := curr.(*TreeNode)
  101. if ok {
  102. node.Remove(child)
  103. }
  104. }
  105. }
  106. //// Clear removes all items from the tree
  107. //func (t *Tree) Clear() {
  108. //
  109. // for t.List.Len() > 0 {
  110. // curr := t.List.ItemAt(0)
  111. // node, ok := curr.(*TreeNode)
  112. // if ok {
  113. // node.remove()
  114. // } else {
  115. // t.List.Remove(curr)
  116. // }
  117. // }
  118. //}
  119. // Selected returns the currently selected element or nil
  120. func (t *Tree) Selected() IPanel {
  121. sel := t.List.Selected()
  122. if len(sel) == 0 {
  123. return nil
  124. }
  125. return sel[0]
  126. }
  127. // FindChild searches for the specified child in the tree and
  128. // all its children. If found, returns the parent node and
  129. // its position relative to the parent.
  130. // If the parent is the tree returns nil as the parent
  131. // If not found returns nil and -1
  132. func (t *Tree) FindChild(child IPanel) (*TreeNode, int) {
  133. for idx := 0; idx < t.List.Len(); idx++ {
  134. curr := t.List.ItemAt(idx)
  135. if curr == child {
  136. return nil, idx
  137. }
  138. node, ok := curr.(*TreeNode)
  139. if ok {
  140. par, pos := node.FindChild(child)
  141. if pos >= 0 {
  142. return par, pos
  143. }
  144. }
  145. }
  146. return nil, -1
  147. }
  148. // onKey receives key down events for the embedded list
  149. func (t *Tree) onKey(evname string, ev interface{}) {
  150. // Get selected item
  151. item := t.Selected()
  152. if item == nil {
  153. return
  154. }
  155. // If item is not a tree node, dispatch event to item
  156. node, ok := item.(*TreeNode)
  157. if !ok {
  158. item.SetRoot(t.root)
  159. item.GetPanel().Dispatch(evname, ev)
  160. return
  161. }
  162. // If not enter key pressed, ignore
  163. kev := ev.(*window.KeyEvent)
  164. if evname != OnKeyDown || kev.Keycode != window.KeyEnter {
  165. return
  166. }
  167. // Toggles the expansion state of the node
  168. node.expanded = !node.expanded
  169. node.update()
  170. node.updateItems()
  171. }
  172. //
  173. // TreeNode methods
  174. //
  175. // newTreeNode creates and returns a pointer to a new TreeNode with
  176. // the specified text, tree and parent node
  177. func newTreeNode(text string, tree *Tree, parNode *TreeNode) *TreeNode {
  178. n := new(TreeNode)
  179. n.Panel.Initialize(0, 0)
  180. // Initialize node label
  181. n.label.initialize(text, StyleDefault.Font)
  182. n.Panel.Add(&n.label)
  183. // Create node icon
  184. n.icon.initialize("", StyleDefault.FontIcon)
  185. n.icon.SetFontSize(n.label.FontSize() * 1.3)
  186. n.Panel.Add(&n.icon)
  187. // Subscribe to events
  188. n.Panel.Subscribe(OnMouseDown, n.onMouse)
  189. n.tree = tree
  190. n.parNode = parNode
  191. n.update()
  192. n.recalc()
  193. return n
  194. }
  195. // Len returns the number of immediate children of this node
  196. func (n *TreeNode) Len() int {
  197. return len(n.items)
  198. }
  199. // SetExpanded sets the expanded state of this node
  200. func (n *TreeNode) SetExpanded(state bool) {
  201. n.expanded = state
  202. n.update()
  203. n.updateItems()
  204. }
  205. // FindChild searches for the specified child in this node and
  206. // all its children. If found, returns the parent node and
  207. // its position relative to the parent.
  208. // If not found returns nil and -1
  209. func (n *TreeNode) FindChild(child IPanel) (*TreeNode, int) {
  210. for pos, curr := range n.items {
  211. if curr == child {
  212. return n, pos
  213. }
  214. node, ok := curr.(*TreeNode)
  215. if ok {
  216. par, pos := node.FindChild(child)
  217. if par != nil {
  218. return par, pos
  219. }
  220. }
  221. }
  222. return nil, -1
  223. }
  224. // InsertAt inserts a child panel at the specified position in this node
  225. // If the position is invalid, the function panics
  226. func (n *TreeNode) InsertAt(pos int, child IPanel) {
  227. if pos < 0 || pos > len(n.items) {
  228. panic("TreeNode.InsertAt(): Invalid position")
  229. }
  230. // Insert item in the items array
  231. n.items = append(n.items, nil)
  232. copy(n.items[pos+1:], n.items[pos:])
  233. n.items[pos] = child
  234. if n.expanded {
  235. n.updateItems()
  236. }
  237. }
  238. // Add adds a child panel to this node
  239. func (n *TreeNode) Add(child IPanel) {
  240. n.InsertAt(n.Len(), child)
  241. }
  242. // InsertNodeAt inserts a new node at the specified position in this node
  243. // If the position is invalid, the function panics
  244. func (n *TreeNode) InsertNodeAt(pos int, text string) *TreeNode {
  245. if pos < 0 || pos > len(n.items) {
  246. panic("TreeNode.InsertNodeAt(): Invalid position")
  247. }
  248. childNode := newTreeNode(text, n.tree, n)
  249. // Insert item in the items array
  250. n.items = append(n.items, nil)
  251. copy(n.items[pos+1:], n.items[pos:])
  252. n.items[pos] = childNode
  253. if n.expanded {
  254. n.updateItems()
  255. }
  256. return childNode
  257. }
  258. // AddNode adds a new node to this one and return its pointer
  259. func (n *TreeNode) AddNode(text string) *TreeNode {
  260. return n.InsertNodeAt(n.Len(), text)
  261. }
  262. // Remove removes the specified child from this node or any
  263. // of its children nodes
  264. func (n *TreeNode) Remove(child IPanel) {
  265. for pos, curr := range n.items {
  266. if curr == child {
  267. copy(n.items[pos:], n.items[pos+1:])
  268. n.items[len(n.items)-1] = nil
  269. n.items = n.items[:len(n.items)-1]
  270. node, ok := curr.(*TreeNode)
  271. if ok {
  272. node.remove()
  273. } else {
  274. n.tree.List.Remove(curr)
  275. }
  276. n.updateItems()
  277. return
  278. }
  279. node, ok := curr.(*TreeNode)
  280. if ok {
  281. node.Remove(child)
  282. }
  283. }
  284. }
  285. // onMouse receives mouse button events over the tree node panel
  286. func (n *TreeNode) onMouse(evname string, ev interface{}) {
  287. switch evname {
  288. case OnMouseDown:
  289. n.expanded = !n.expanded
  290. n.update()
  291. n.recalc()
  292. n.updateItems()
  293. default:
  294. return
  295. }
  296. }
  297. // level returns the level of this node from the start of the tree
  298. func (n *TreeNode) level() int {
  299. level := 0
  300. parNode := n.parNode
  301. for parNode != nil {
  302. parNode = parNode.parNode
  303. level++
  304. }
  305. return level
  306. }
  307. // applyStyles applies the specified style to this tree node
  308. func (n *TreeNode) applyStyle(s *TreeNodeStyle) {
  309. n.SetMarginsFrom(&s.Margins)
  310. n.SetBordersColor4(&s.BorderColor)
  311. n.SetBordersFrom(&s.Border)
  312. icode := 0
  313. if n.expanded {
  314. icode = 1
  315. }
  316. n.icon.SetText(string(s.Icons[icode]))
  317. }
  318. // update updates this tree node style
  319. func (n *TreeNode) update() {
  320. n.applyStyle(&n.tree.styles.Node.Normal)
  321. }
  322. // recalc recalculates the positions of the internal node panels
  323. func (n *TreeNode) recalc() {
  324. // icon position
  325. n.icon.SetPosition(0, 0)
  326. // Label position and width
  327. n.label.SetPosition(n.icon.Width()+4, 0)
  328. n.Panel.SetContentHeight(n.label.Height())
  329. n.Panel.SetWidth(n.tree.ContentWidth())
  330. }
  331. // remove removes this node and all children from the tree list
  332. func (n *TreeNode) remove() {
  333. n.tree.List.Remove(n)
  334. n.removeItems()
  335. }
  336. // removeItems removes this node children from the tree list
  337. func (n *TreeNode) removeItems() {
  338. for _, ipanel := range n.items {
  339. // Remove item from scroller
  340. n.tree.List.Remove(ipanel)
  341. // If item is a node, remove all children
  342. node, ok := ipanel.(*TreeNode)
  343. if ok {
  344. node.removeItems()
  345. continue
  346. }
  347. }
  348. }
  349. // insert inserts this node and its expanded children in the tree list
  350. // at the specified position
  351. func (n *TreeNode) insert(pos int) int {
  352. n.update()
  353. n.tree.List.InsertAt(pos, n)
  354. var padLeft float32 = n.tree.styles.Padlevel * float32(n.level())
  355. n.tree.List.SetItemPadLeftAt(pos, padLeft)
  356. pos++
  357. return n.insertItems(pos)
  358. }
  359. // insertItems inserts this node items in the tree list
  360. // at the specified position
  361. func (n *TreeNode) insertItems(pos int) int {
  362. if !n.expanded {
  363. return pos
  364. }
  365. level := n.level() + 1
  366. var padLeft float32 = n.tree.styles.Padlevel * float32(level)
  367. for _, ipanel := range n.items {
  368. // Insert node and its children
  369. node, ok := ipanel.(*TreeNode)
  370. if ok {
  371. node.update()
  372. n.tree.List.InsertAt(pos, ipanel)
  373. n.tree.List.SetItemPadLeftAt(pos, padLeft)
  374. pos++
  375. pos = node.insertItems(pos)
  376. continue
  377. }
  378. // Insert item
  379. n.tree.List.InsertAt(pos, ipanel)
  380. n.tree.List.SetItemPadLeftAt(pos, padLeft)
  381. pos++
  382. }
  383. return pos
  384. }
  385. // updateItems updates this node items, removing or inserting them into the tree scroller
  386. func (n *TreeNode) updateItems() {
  387. pos := n.tree.ItemPosition(n)
  388. if pos < 0 {
  389. return
  390. }
  391. n.removeItems()
  392. n.insertItems(pos + 1)
  393. }