vboxlayout.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. // VBoxLayout implements a panel layout which arranges the panel children vertically.
  6. // The children can be separated by a space in pixels set by SetSpacing().
  7. // The whole group of children can be aligned vertically by SetAlignV() which can
  8. // accept the following types of alignment:
  9. //
  10. // AlignTop: Try to align the group of children to the top if the panel height is
  11. // greater the the sum of the children heights + spacing.
  12. //
  13. // AlignBottom: Try to align the group of children to the bottoom if the panel height is
  14. // greater the the sum of the children heights + spacing.
  15. //
  16. // AlignCenter: Try to align the group of children in the center if the panel height is
  17. // greater the the sum of the children heights + spacing.
  18. //
  19. // AlignHeight - Try to align the individual children vertically with the same same space between each other.
  20. // Each individual child can be aligned horizontally by SetLayoutParameters()
  21. //
  22. // If the layout method SetMinHeight(true) is called, the panel minimum content height will be the
  23. // sum of its children's heights plus the spacing.
  24. //
  25. // If the layout method SetMinWidth(true) is called, the panel minimum content width will be the
  26. // width of the widest child.
  27. type VBoxLayout struct {
  28. pan IPanel
  29. spacing float32
  30. alignV Align
  31. minHeight bool
  32. minWidth bool
  33. }
  34. // VBoxLayoutParams specify the horizontal alignment of each individual child.
  35. type VBoxLayoutParams struct {
  36. Expand float32 // item expand vertically factor (0 - no expand)
  37. AlignH Align // item horizontal alignment
  38. }
  39. // NewVBoxLayout creates and returns a pointer to a new horizontal box layout
  40. func NewVBoxLayout() *VBoxLayout {
  41. bl := new(VBoxLayout)
  42. bl.spacing = 0
  43. bl.alignV = AlignTop
  44. return bl
  45. }
  46. // SetSpacing sets the horizontal spacing between the items in pixels
  47. // and updates the layout if possible
  48. func (bl *VBoxLayout) SetSpacing(spacing float32) {
  49. bl.spacing = spacing
  50. bl.Recalc(bl.pan)
  51. }
  52. // SetAlignV sets the vertical alignment of the whole group of items
  53. // inside the parent panel and updates the layout if possible.
  54. // This only has any effect if there are no expanded items.
  55. func (bl *VBoxLayout) SetAlignV(align Align) {
  56. bl.alignV = align
  57. bl.Recalc(bl.pan)
  58. }
  59. // SetMinHeight sets if the panel minimum height should be the height of
  60. // the largest of its children's height.
  61. func (bl *VBoxLayout) SetMinHeight(state bool) {
  62. bl.minHeight = state
  63. bl.Recalc(bl.pan)
  64. }
  65. // SetMinWidth sets if the panel minimum width should be sum of its
  66. // children's width plus the spacing
  67. func (bl *VBoxLayout) SetMinWidth(state bool) {
  68. bl.minWidth = state
  69. bl.Recalc(bl.pan)
  70. }
  71. // Recalc recalculates and sets the position and sizes of all children
  72. func (bl *VBoxLayout) Recalc(ipan IPanel) {
  73. // Saves the received panel
  74. bl.pan = ipan
  75. if bl.pan == nil {
  76. return
  77. }
  78. parent := ipan.GetPanel()
  79. if len(parent.Children()) == 0 {
  80. return
  81. }
  82. // If minHeight is set, get the sum of heights of this panel's children plus the spacings.
  83. // If the panel content height is less than this height, set its content height to this value.
  84. if bl.minHeight {
  85. var totalHeight float32
  86. for _, ichild := range parent.Children() {
  87. child := ichild.(IPanel).GetPanel()
  88. if !child.Visible() {
  89. continue
  90. }
  91. totalHeight += child.Height()
  92. }
  93. // Adds spacing
  94. totalHeight += bl.spacing * float32(len(parent.Children())-1)
  95. if parent.ContentHeight() < totalHeight {
  96. parent.setContentSize(parent.ContentWidth(), totalHeight, false)
  97. }
  98. }
  99. // If minWidth is set, get the maximum width of all the panel's children
  100. // and if the panel content width is less than this maximum, set its content width to this value.
  101. if bl.minWidth {
  102. var maxWidth float32
  103. for _, ichild := range parent.Children() {
  104. child := ichild.(IPanel).GetPanel()
  105. if !child.Visible() {
  106. continue
  107. }
  108. if child.Width() > maxWidth {
  109. maxWidth = child.Width()
  110. }
  111. }
  112. if parent.ContentWidth() < maxWidth {
  113. parent.setContentSize(maxWidth, parent.ContentHeight(), false)
  114. }
  115. }
  116. // Calculates the total height, expanded height, fixed height and
  117. // the sum of the expand factor for all items.
  118. var theight float32
  119. var eheight float32
  120. var fheight float32
  121. var texpand float32
  122. ecount := 0
  123. paramsDef := VBoxLayoutParams{Expand: 0, AlignH: AlignLeft}
  124. for pos, obj := range parent.Children() {
  125. pan := obj.(IPanel).GetPanel()
  126. // Get item layout parameters or use default
  127. params := paramsDef
  128. if pan.layoutParams != nil {
  129. params = *pan.layoutParams.(*VBoxLayoutParams)
  130. }
  131. // Calculate total height
  132. theight += pan.Height()
  133. if pos > 0 {
  134. theight += bl.spacing
  135. }
  136. // Calculate height of expanded items
  137. if params.Expand > 0 {
  138. texpand += params.Expand
  139. eheight += pan.Height()
  140. if pos > 0 {
  141. eheight += bl.spacing
  142. }
  143. ecount++
  144. // Calculate width of fixed items
  145. } else {
  146. fheight += pan.Height()
  147. if pos > 0 {
  148. fheight += bl.spacing
  149. }
  150. }
  151. }
  152. // If there is at least on expanded item, all free space will be occupied
  153. spaceMiddle := bl.spacing
  154. var posY float32
  155. if texpand > 0 {
  156. // If there is free space, distribute space between expanded items
  157. totalSpace := parent.ContentHeight() - theight
  158. if totalSpace > 0 {
  159. for _, obj := range parent.Children() {
  160. pan := obj.(IPanel).GetPanel()
  161. // Get item layout parameters or use default
  162. params := paramsDef
  163. if pan.layoutParams != nil {
  164. params = *pan.layoutParams.(*VBoxLayoutParams)
  165. }
  166. if params.Expand > 0 {
  167. iheight := totalSpace * params.Expand / texpand
  168. pan.SetHeight(pan.Height() + iheight)
  169. }
  170. }
  171. // No free space: distribute expanded items heights
  172. } else {
  173. for _, obj := range parent.Children() {
  174. pan := obj.(IPanel).GetPanel()
  175. // Get item layout parameters or use default
  176. params := paramsDef
  177. if pan.layoutParams != nil {
  178. params = *pan.layoutParams.(*VBoxLayoutParams)
  179. }
  180. if params.Expand > 0 {
  181. spacing := bl.spacing * float32(ecount-1)
  182. iheight := (parent.ContentHeight() - spacing - fheight - bl.spacing) * params.Expand / texpand
  183. pan.SetHeight(iheight)
  184. }
  185. }
  186. }
  187. // No expanded items: checks block vertical alignment
  188. } else {
  189. // Calculates initial y position which depends
  190. // on the current horizontal alignment.
  191. switch bl.alignV {
  192. case AlignTop:
  193. posY = 0
  194. case AlignCenter:
  195. posY = (parent.ContentHeight() - theight) / 2
  196. case AlignBottom:
  197. posY = parent.ContentHeight() - theight
  198. case AlignHeight:
  199. space := parent.ContentHeight() - theight + bl.spacing*float32(len(parent.Children())-1)
  200. if space < 0 {
  201. space = bl.spacing * float32(len(parent.Children())-1)
  202. }
  203. spaceMiddle = space / float32(len(parent.Children())+1)
  204. posY = spaceMiddle
  205. default:
  206. log.Fatal("VBoxLayout: invalid global vertical alignment")
  207. }
  208. }
  209. // Calculates the X position of each item considering
  210. // it horizontal alignment
  211. var posX float32
  212. width := parent.ContentWidth()
  213. for pos, obj := range parent.Children() {
  214. pan := obj.(IPanel).GetPanel()
  215. // Get item layout parameters or use default
  216. params := paramsDef
  217. if pan.layoutParams != nil {
  218. params = *pan.layoutParams.(*VBoxLayoutParams)
  219. }
  220. cwidth := pan.Width()
  221. switch params.AlignH {
  222. case AlignLeft:
  223. posX = 0
  224. case AlignCenter:
  225. posX = (width - cwidth) / 2
  226. case AlignRight:
  227. posX = width - cwidth
  228. case AlignWidth:
  229. posX = 0
  230. pan.SetWidth(width)
  231. default:
  232. log.Fatal("VBoxLayout: invalid item horizontal alignment")
  233. }
  234. // Sets the child position
  235. pan.SetPosition(posX, posY)
  236. // Calculates next position
  237. posY += pan.Height()
  238. if pos < len(parent.Children())-1 {
  239. posY += spaceMiddle
  240. }
  241. }
  242. }