hboxlayout.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. // HBoxLayout implements a panel layout which arranges the panel children horizontally.
  6. // The children can be separated by a space in pixels set by SetSpacing().
  7. // The whole group of children can be aligned horizontally by SetAlignH() which can
  8. // accept the following types of alignment:
  9. //
  10. // AlignLeft: Try to align the group of children to the left if the panel width is
  11. // greater the the sum of the children widths + spacing.
  12. //
  13. // AlignRight: Try to align the group of children to the right if the panel width is
  14. // greater the the sum of the children widths + spacing.
  15. //
  16. // AlignCenter: Try to align the group of children in the center if the panel width is
  17. // greater the the sum of the children widths + spacing.
  18. //
  19. // AlignWidth - Try to align the individual children with the same same space between each other.
  20. // Each individual child can be aligned vertically by SetLayoutParameters()
  21. //
  22. // If the layout method SetMinHeight(true) is called, the panel minimum content height will be the
  23. // height of the child with the largest height.
  24. //
  25. // If the layout method SetMinWidth(true) is called, the panel minimum content width will be the
  26. // sum of its children's widths plus the spacing.
  27. type HBoxLayout struct {
  28. pan IPanel
  29. spacing float32
  30. alignH Align
  31. minHeight bool
  32. minWidth bool
  33. }
  34. // HBoxLayoutParams specify the vertical alignment of each individual child.
  35. type HBoxLayoutParams struct {
  36. Expand float32 // item expand horizontally factor (0 - no expand)
  37. AlignV Align // item vertical alignment
  38. }
  39. // NewHBoxLayout creates and returns a pointer to a new horizontal box layout
  40. func NewHBoxLayout() *HBoxLayout {
  41. bl := new(HBoxLayout)
  42. bl.spacing = 0
  43. bl.alignH = AlignLeft
  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 *HBoxLayout) SetSpacing(spacing float32) {
  49. bl.spacing = spacing
  50. bl.Recalc(bl.pan)
  51. }
  52. // SetAlignH sets the horizontal alignment of the whole group of items
  53. // inside the parent panel and updates the layout.
  54. // This only has any effect if there are no expanded items.
  55. func (bl *HBoxLayout) SetAlignH(align Align) {
  56. bl.alignH = 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 *HBoxLayout) 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 *HBoxLayout) 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 *HBoxLayout) 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 maximum height of all the panel's children
  83. // and if the panel content height is less than this maximum, set its content height to this value.
  84. if bl.minHeight {
  85. var maxHeight float32
  86. for _, ichild := range parent.Children() {
  87. child := ichild.(IPanel).GetPanel()
  88. if !child.Visible() {
  89. continue
  90. }
  91. if child.Height() > maxHeight {
  92. maxHeight = child.Height()
  93. }
  94. }
  95. if parent.ContentHeight() < maxHeight {
  96. parent.setContentSize(parent.ContentWidth(), maxHeight, false)
  97. }
  98. }
  99. // If minWidth is set, get the sum of widths of this panel's children plus the spacings.
  100. // If the panel content width is less than this width, set its content width to this value.
  101. if bl.minWidth {
  102. var totalWidth float32
  103. for _, ichild := range parent.Children() {
  104. child := ichild.(IPanel).GetPanel()
  105. if !child.Visible() {
  106. continue
  107. }
  108. totalWidth += child.Width()
  109. }
  110. // Adds spacing
  111. totalWidth += bl.spacing * float32(len(parent.Children())-1)
  112. if parent.ContentWidth() < totalWidth {
  113. parent.setContentSize(totalWidth, parent.ContentHeight(), false)
  114. }
  115. }
  116. // Calculates the total width, expanded width, fixed width and
  117. // the sum of the expand factor for all items.
  118. var twidth float32
  119. var ewidth float32
  120. var fwidth float32
  121. var texpand float32
  122. ecount := 0
  123. paramsDef := HBoxLayoutParams{Expand: 0, AlignV: AlignTop}
  124. for pos, obj := range parent.Children() {
  125. pan := obj.(IPanel).GetPanel()
  126. if !pan.Visible() {
  127. continue
  128. }
  129. // Get item layout parameters or use default
  130. params := paramsDef
  131. if pan.layoutParams != nil {
  132. params = *pan.layoutParams.(*HBoxLayoutParams)
  133. }
  134. // Calculate total width
  135. twidth += pan.Width()
  136. if pos > 0 {
  137. twidth += bl.spacing
  138. }
  139. // Calculate width of expanded items
  140. if params.Expand > 0 {
  141. texpand += params.Expand
  142. ewidth += pan.Width()
  143. if pos > 0 {
  144. ewidth += bl.spacing
  145. }
  146. ecount++
  147. // Calculate width of fixed items
  148. } else {
  149. fwidth += pan.Width()
  150. if pos > 0 {
  151. fwidth += 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 posX float32
  158. if texpand > 0 {
  159. // If there is free space, distribute space between expanded items
  160. totalSpace := parent.ContentWidth() - twidth
  161. if totalSpace > 0 {
  162. for _, obj := range parent.Children() {
  163. pan := obj.(IPanel).GetPanel()
  164. if !pan.Visible() {
  165. continue
  166. }
  167. // Get item layout parameters or use default
  168. params := paramsDef
  169. if pan.layoutParams != nil {
  170. params = *pan.layoutParams.(*HBoxLayoutParams)
  171. }
  172. if params.Expand > 0 {
  173. iwidth := totalSpace * params.Expand / texpand
  174. pan.SetWidth(pan.Width() + iwidth)
  175. }
  176. }
  177. // No free space: distribute expanded items widths
  178. } else {
  179. for _, obj := range parent.Children() {
  180. pan := obj.(IPanel).GetPanel()
  181. if !pan.Visible() {
  182. continue
  183. }
  184. // Get item layout parameters or use default
  185. params := paramsDef
  186. if pan.layoutParams != nil {
  187. params = *pan.layoutParams.(*HBoxLayoutParams)
  188. }
  189. if params.Expand > 0 {
  190. spacing := bl.spacing * (float32(ecount) - 1)
  191. iwidth := (parent.ContentWidth() - spacing - fwidth - bl.spacing) * params.Expand / texpand
  192. pan.SetWidth(iwidth)
  193. }
  194. }
  195. }
  196. // No expanded items: checks block horizontal alignment
  197. } else {
  198. // Calculates initial x position which depends
  199. // on the current horizontal alignment.
  200. switch bl.alignH {
  201. case AlignLeft:
  202. posX = 0
  203. case AlignCenter:
  204. posX = (parent.ContentWidth() - twidth) / 2
  205. case AlignRight:
  206. posX = parent.ContentWidth() - twidth
  207. case AlignWidth:
  208. space := parent.ContentWidth() - twidth + 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. posX = spaceMiddle
  214. default:
  215. log.Fatal("HBoxLayout: invalid global horizontal alignment")
  216. }
  217. }
  218. // Calculates the Y position of each item considering its vertical alignment
  219. var posY float32
  220. height := parent.ContentHeight()
  221. for pos, obj := range parent.Children() {
  222. pan := obj.(IPanel).GetPanel()
  223. if !pan.Visible() {
  224. continue
  225. }
  226. // Get item layout parameters or use default
  227. params := paramsDef
  228. if pan.layoutParams != nil {
  229. params = *pan.layoutParams.(*HBoxLayoutParams)
  230. }
  231. cheight := pan.Height()
  232. switch params.AlignV {
  233. case AlignTop:
  234. posY = 0
  235. case AlignCenter:
  236. posY = (height - cheight) / 2
  237. case AlignBottom:
  238. posY = height - cheight
  239. case AlignHeight:
  240. posY = 0
  241. pan.SetHeight(height)
  242. default:
  243. log.Fatal("HBoxLayout: invalid item vertical alignment")
  244. }
  245. // Sets the child position
  246. pan.SetPosition(posX, posY)
  247. // Calculates next position
  248. posX += pan.Width()
  249. if pos < len(parent.Children())-1 {
  250. posX += spaceMiddle
  251. }
  252. }
  253. }