tree.go 10 KB

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