tree.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. // 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.tree = tree
  183. n.parNode = parNode
  184. n.update()
  185. n.recalc()
  186. return n
  187. }
  188. // Len returns the number of immediate children of this node
  189. func (n *TreeNode) Len() int {
  190. return len(n.items)
  191. }
  192. // SetExpanded sets the expanded state of this node
  193. func (n *TreeNode) SetExpanded(state bool) {
  194. n.expanded = state
  195. n.update()
  196. n.updateItems()
  197. }
  198. // FindChild searches for the specified child in this node and
  199. // all its children. If found, returns the parent node and
  200. // its position relative to the parent.
  201. // If not found returns nil and -1
  202. func (n *TreeNode) FindChild(child IPanel) (*TreeNode, int) {
  203. for pos, curr := range n.items {
  204. if curr == child {
  205. return n, pos
  206. }
  207. node, ok := curr.(*TreeNode)
  208. if ok {
  209. par, pos := node.FindChild(child)
  210. if par != nil {
  211. return par, pos
  212. }
  213. }
  214. }
  215. return nil, -1
  216. }
  217. // InsertAt inserts a child panel at the specified position in this node
  218. // If the position is invalid, the function panics
  219. func (n *TreeNode) InsertAt(pos int, child IPanel) {
  220. if pos < 0 || pos > len(n.items) {
  221. panic("TreeNode.InsertAt(): Invalid position")
  222. }
  223. // Insert item in the items array
  224. n.items = append(n.items, nil)
  225. copy(n.items[pos+1:], n.items[pos:])
  226. n.items[pos] = child
  227. if n.expanded {
  228. n.updateItems()
  229. }
  230. }
  231. // Add adds a child panel to this node
  232. func (n *TreeNode) Add(child IPanel) {
  233. n.InsertAt(n.Len(), child)
  234. }
  235. // InsertNodeAt inserts a new node at the specified position in this node
  236. // If the position is invalid, the function panics
  237. func (n *TreeNode) InsertNodeAt(pos int, text string) *TreeNode {
  238. if pos < 0 || pos > len(n.items) {
  239. panic("TreeNode.InsertNodeAt(): Invalid position")
  240. }
  241. childNode := newTreeNode(text, n.tree, n)
  242. // Insert item in the items array
  243. n.items = append(n.items, nil)
  244. copy(n.items[pos+1:], n.items[pos:])
  245. n.items[pos] = childNode
  246. if n.expanded {
  247. n.updateItems()
  248. }
  249. return childNode
  250. }
  251. // AddNode adds a new node to this one and return its pointer
  252. func (n *TreeNode) AddNode(text string) *TreeNode {
  253. return n.InsertNodeAt(n.Len(), text)
  254. }
  255. // Remove removes the specified child from this node or any
  256. // of its children nodes
  257. func (n *TreeNode) Remove(child IPanel) {
  258. for pos, curr := range n.items {
  259. if curr == child {
  260. copy(n.items[pos:], n.items[pos+1:])
  261. n.items[len(n.items)-1] = nil
  262. n.items = n.items[:len(n.items)-1]
  263. node, ok := curr.(*TreeNode)
  264. if ok {
  265. node.remove()
  266. } else {
  267. n.tree.List.Remove(curr)
  268. }
  269. n.updateItems()
  270. return
  271. }
  272. node, ok := curr.(*TreeNode)
  273. if ok {
  274. node.Remove(child)
  275. }
  276. }
  277. }
  278. // onMouse receives mouse button events over the tree node panel
  279. func (n *TreeNode) onMouse(evname string, ev interface{}) {
  280. switch evname {
  281. case OnMouseDown:
  282. n.expanded = !n.expanded
  283. n.update()
  284. n.recalc()
  285. n.updateItems()
  286. default:
  287. return
  288. }
  289. }
  290. // level returns the level of this node from the start of the tree
  291. func (n *TreeNode) level() int {
  292. level := 0
  293. parNode := n.parNode
  294. for parNode != nil {
  295. parNode = parNode.parNode
  296. level++
  297. }
  298. return level
  299. }
  300. // applyStyles applies the specified style to this tree node
  301. func (n *TreeNode) applyStyle(s *TreeNodeStyle) {
  302. n.SetMarginsFrom(&s.Margins)
  303. n.SetBordersColor4(&s.BorderColor)
  304. n.SetBordersFrom(&s.Border)
  305. icode := 0
  306. if n.expanded {
  307. icode = 1
  308. }
  309. n.icon.SetText(string(s.Icons[icode]))
  310. }
  311. // update updates this tree node style
  312. func (n *TreeNode) update() {
  313. n.applyStyle(&n.tree.styles.Node.Normal)
  314. }
  315. // recalc recalculates the positions of the internal node panels
  316. func (n *TreeNode) recalc() {
  317. // icon position
  318. n.icon.SetPosition(0, 0)
  319. // Label position and width
  320. n.label.SetPosition(n.icon.Width()+4, 0)
  321. n.Panel.SetContentHeight(n.label.Height())
  322. n.Panel.SetWidth(n.tree.ContentWidth())
  323. }
  324. // remove removes this node and all children from the tree list
  325. func (n *TreeNode) remove() {
  326. n.tree.List.Remove(n)
  327. n.removeItems()
  328. }
  329. // removeItems removes this node children from the tree list
  330. func (n *TreeNode) removeItems() {
  331. for _, ipanel := range n.items {
  332. // Remove item from scroller
  333. n.tree.List.Remove(ipanel)
  334. // If item is a node, remove all children
  335. node, ok := ipanel.(*TreeNode)
  336. if ok {
  337. node.removeItems()
  338. continue
  339. }
  340. }
  341. }
  342. // insert inserts this node and its expanded children in the tree list
  343. // at the specified position
  344. func (n *TreeNode) insert(pos int) int {
  345. n.update()
  346. n.tree.List.InsertAt(pos, n)
  347. var padLeft float32 = n.tree.styles.Padlevel * float32(n.level())
  348. n.tree.List.SetItemPadLeftAt(pos, padLeft)
  349. pos++
  350. return n.insertItems(pos)
  351. }
  352. // insertItems inserts this node items in the tree list
  353. // at the specified position
  354. func (n *TreeNode) insertItems(pos int) int {
  355. if !n.expanded {
  356. return pos
  357. }
  358. level := n.level() + 1
  359. var padLeft float32 = n.tree.styles.Padlevel * float32(level)
  360. for _, ipanel := range n.items {
  361. // Insert node and its children
  362. node, ok := ipanel.(*TreeNode)
  363. if ok {
  364. node.update()
  365. n.tree.List.InsertAt(pos, ipanel)
  366. n.tree.List.SetItemPadLeftAt(pos, padLeft)
  367. pos++
  368. pos = node.insertItems(pos)
  369. continue
  370. }
  371. // Insert item
  372. n.tree.List.InsertAt(pos, ipanel)
  373. n.tree.List.SetItemPadLeftAt(pos, padLeft)
  374. pos++
  375. }
  376. return pos
  377. }
  378. // updateItems updates this node items, removing or inserting them into the tree scroller
  379. func (n *TreeNode) updateItems() {
  380. pos := n.tree.ItemPosition(n)
  381. if pos < 0 {
  382. return
  383. }
  384. n.removeItems()
  385. n.insertItems(pos + 1)
  386. }