docklayout.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. type DockLayout struct {
  6. }
  7. type DockLayoutParams struct {
  8. Edge int
  9. }
  10. const (
  11. DockTop = iota + 1
  12. DockRight
  13. DockBottom
  14. DockLeft
  15. DockCenter
  16. )
  17. func NewDockLayout() *DockLayout {
  18. return new(DockLayout)
  19. }
  20. func (dl *DockLayout) Recalc(ipan IPanel) {
  21. pan := ipan.GetPanel()
  22. width := pan.Width()
  23. topY := float32(0)
  24. bottomY := pan.Height()
  25. leftX := float32(0)
  26. rightX := width
  27. // Top and bottom first
  28. for _, iobj := range pan.Children() {
  29. child := iobj.(IPanel).GetPanel()
  30. if child.layoutParams == nil {
  31. continue
  32. }
  33. params := child.layoutParams.(*DockLayoutParams)
  34. if params.Edge == DockTop {
  35. child.SetPosition(0, topY)
  36. topY += child.Height()
  37. child.SetWidth(width)
  38. continue
  39. }
  40. if params.Edge == DockBottom {
  41. child.SetPosition(0, bottomY-child.Height())
  42. bottomY -= child.Height()
  43. child.SetWidth(width)
  44. continue
  45. }
  46. }
  47. // Left and right
  48. for _, iobj := range pan.Children() {
  49. child := iobj.(IPanel).GetPanel()
  50. if child.layoutParams == nil {
  51. continue
  52. }
  53. params := child.layoutParams.(*DockLayoutParams)
  54. if params.Edge == DockLeft {
  55. child.SetPosition(leftX, topY)
  56. leftX += child.Width()
  57. child.SetHeight(bottomY - topY)
  58. continue
  59. }
  60. if params.Edge == DockRight {
  61. child.SetPosition(rightX-child.Width(), topY)
  62. rightX -= child.Width()
  63. child.SetHeight(bottomY - topY)
  64. continue
  65. }
  66. }
  67. // Center (only the first found)
  68. for _, iobj := range pan.Children() {
  69. child := iobj.(IPanel).GetPanel()
  70. if child.layoutParams == nil {
  71. continue
  72. }
  73. params := child.layoutParams.(*DockLayoutParams)
  74. if params.Edge == DockCenter {
  75. child.SetPosition(leftX, topY)
  76. child.SetSize(rightX-leftX, bottomY-topY)
  77. break
  78. }
  79. }
  80. }