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