vboxlayout.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 SetAutoHeight(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 SetAutoWidth(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. autoHeight bool
  32. autoWidth 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. // SetAutoHeight sets if the panel minimum height should be the height of
  60. // the largest of its children's height.
  61. func (bl *VBoxLayout) SetAutoHeight(state bool) {
  62. bl.autoHeight = state
  63. bl.Recalc(bl.pan)
  64. }
  65. // SetAutoWidth sets if the panel minimum width should be sum of its
  66. // children's width plus the spacing
  67. func (bl *VBoxLayout) SetAutoWidth(state bool) {
  68. bl.autoWidth = 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 autoHeight 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.autoHeight {
  85. var totalHeight float32
  86. for _, ichild := range parent.Children() {
  87. child := ichild.(IPanel).GetPanel()
  88. if !child.Visible() || !child.Bounded() {
  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 autoWidth 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.autoWidth {
  102. var maxWidth float32
  103. for _, ichild := range parent.Children() {
  104. child := ichild.(IPanel).GetPanel()
  105. if !child.Visible() || !child.Bounded() {
  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. if !pan.Visible() || !pan.Bounded() {
  127. continue
  128. }
  129. // Get item layout parameters or use default
  130. params := paramsDef
  131. if pan.layoutParams != nil {
  132. params = *pan.layoutParams.(*VBoxLayoutParams)
  133. }
  134. // Calculate total height
  135. theight += pan.Height()
  136. if pos > 0 {
  137. theight += bl.spacing
  138. }
  139. // Calculate height of expanded items
  140. if params.Expand > 0 {
  141. texpand += params.Expand
  142. //eheight += pan.Height()
  143. //if pos > 0 {
  144. // eheight += bl.spacing
  145. //}
  146. ecount++
  147. // Calculate width of fixed items
  148. } else {
  149. fheight += pan.Height()
  150. if pos > 0 {
  151. fheight += bl.spacing
  152. }
  153. }
  154. }
  155. // If there is at least on expanded item, all free space will be occupied
  156. spaceMiddle := bl.spacing
  157. var posY float32
  158. if texpand > 0 {
  159. // If there is free space, distribute space between expanded items
  160. totalSpace := parent.ContentHeight() - theight
  161. if totalSpace > 0 {
  162. for _, obj := range parent.Children() {
  163. pan := obj.(IPanel).GetPanel()
  164. if !pan.Visible() || !pan.Bounded() {
  165. continue
  166. }
  167. // Get item layout parameters or use default
  168. params := paramsDef
  169. if pan.layoutParams != nil {
  170. params = *pan.layoutParams.(*VBoxLayoutParams)
  171. }
  172. if params.Expand > 0 {
  173. iheight := totalSpace * params.Expand / texpand
  174. pan.SetHeight(pan.Height() + iheight)
  175. }
  176. }
  177. // No free space: distribute expanded items heights
  178. } else {
  179. for _, obj := range parent.Children() {
  180. pan := obj.(IPanel).GetPanel()
  181. if !pan.Visible() || !pan.Bounded() {
  182. continue
  183. }
  184. // Get item layout parameters or use default
  185. params := paramsDef
  186. if pan.layoutParams != nil {
  187. params = *pan.layoutParams.(*VBoxLayoutParams)
  188. }
  189. if params.Expand > 0 {
  190. spacing := bl.spacing * float32(ecount-1)
  191. iheight := (parent.ContentHeight() - spacing - fheight - bl.spacing) * params.Expand / texpand
  192. pan.SetHeight(iheight)
  193. }
  194. }
  195. }
  196. // No expanded items: checks block vertical alignment
  197. } else {
  198. // Calculates initial y position which depends
  199. // on the current horizontal alignment.
  200. switch bl.alignV {
  201. case AlignNone, AlignTop:
  202. posY = 0
  203. case AlignCenter:
  204. posY = (parent.ContentHeight() - theight) / 2
  205. case AlignBottom:
  206. posY = parent.ContentHeight() - theight
  207. case AlignHeight:
  208. space := parent.ContentHeight() - theight + bl.spacing*float32(len(parent.Children())-1)
  209. if space < 0 {
  210. space = bl.spacing * float32(len(parent.Children())-1)
  211. }
  212. spaceMiddle = space / float32(len(parent.Children())+1)
  213. posY = spaceMiddle
  214. default:
  215. log.Fatal("VBoxLayout: invalid global vertical alignment")
  216. }
  217. }
  218. // Calculates the X position of each item considering
  219. // it horizontal alignment
  220. var posX float32
  221. width := parent.ContentWidth()
  222. for pos, obj := range parent.Children() {
  223. pan := obj.(IPanel).GetPanel()
  224. if !pan.Visible() || !pan.Bounded() {
  225. continue
  226. }
  227. // Get item layout parameters or use default
  228. params := paramsDef
  229. if pan.layoutParams != nil {
  230. params = *pan.layoutParams.(*VBoxLayoutParams)
  231. }
  232. cwidth := pan.Width()
  233. switch params.AlignH {
  234. case AlignNone, AlignLeft:
  235. posX = 0
  236. case AlignCenter:
  237. posX = (width - cwidth) / 2
  238. case AlignRight:
  239. posX = width - cwidth
  240. case AlignWidth:
  241. posX = 0
  242. pan.SetWidth(width)
  243. default:
  244. log.Fatal("VBoxLayout: invalid item horizontal alignment")
  245. }
  246. // Sets the child position
  247. pan.SetPosition(posX, posY)
  248. // Calculates next position
  249. posY += pan.Height()
  250. if pos < len(parent.Children())-1 {
  251. posY += spaceMiddle
  252. }
  253. }
  254. }