tree.go 11 KB

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