panel.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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/core"
  7. "github.com/g3n/engine/geometry"
  8. "github.com/g3n/engine/gls"
  9. "github.com/g3n/engine/graphic"
  10. "github.com/g3n/engine/material"
  11. "github.com/g3n/engine/math32"
  12. )
  13. /*********************************************
  14. Panel areas:
  15. +------------------------------------------+
  16. | Margin area |
  17. | +------------------------------------+ |
  18. | | Border area | |
  19. | | +------------------------------+ | |
  20. | | | Padding area | | |
  21. | | | +------------------------+ | | |
  22. | | | | Content area | | | |
  23. | | | | | | | |
  24. | | | | | | | |
  25. | | | +------------------------+ | | |
  26. | | | | | |
  27. | | +------------------------------+ | |
  28. | | | |
  29. | +------------------------------------+ |
  30. | |
  31. +------------------------------------------+
  32. *********************************************/
  33. // IPanel is the interface for all panel types
  34. type IPanel interface {
  35. graphic.IGraphic
  36. GetPanel() *Panel
  37. SetRoot(*Root)
  38. LostKeyFocus()
  39. TotalHeight() float32
  40. }
  41. type Panel struct {
  42. graphic.Graphic // Embedded graphic
  43. root *Root // pointer to root container
  44. width float32 // external width in pixels
  45. height float32 // external height in pixels
  46. mat *material.Material // panel material
  47. marginSizes BorderSizes // external margin sizes in pixel coordinates
  48. borderSizes BorderSizes // border sizes in pixel coordinates
  49. paddingSizes BorderSizes // padding sizes in pixel coordinates
  50. content Rect // current content rectangle in pixel coordinates
  51. modelMatrixUni gls.UniformMatrix4f // pointer to model matrix uniform
  52. borderColorUni gls.Uniform4f // pointer to border color uniform
  53. paddingColorUni gls.Uniform4f // pointer to padding color uniform
  54. contentColorUni gls.Uniform4f // pointer to content color uniform
  55. boundsUni gls.Uniform4f // pointer to bounds uniform (texture coordinates)
  56. borderUni gls.Uniform4f // pointer to border uniform (texture coordinates)
  57. paddingUni gls.Uniform4f // pointer to padding uniform (texture coordinates)
  58. contentUni gls.Uniform4f // pointer to content uniform (texture coordinates)
  59. pospix math32.Vector3 // absolute position in pixels
  60. xmin float32 // minimum absolute x this panel can use
  61. xmax float32 // maximum absolute x this panel can use
  62. ymin float32 // minimum absolute y this panel can use
  63. ymax float32 // maximum absolute y this panel can use
  64. bounded bool // panel is bounded by its parent
  65. enabled bool // enable event processing
  66. cursorEnter bool // mouse enter dispatched
  67. layout ILayout // current layout for children
  68. layoutParams interface{} // current layout parameters used by container panel
  69. }
  70. const (
  71. deltaZ = -0.00001
  72. )
  73. // NewPanel creates and returns a pointer to a new panel with the
  74. // specified dimensions in pixels
  75. func NewPanel(width, height float32) *Panel {
  76. p := new(Panel)
  77. p.Initialize(width, height)
  78. return p
  79. }
  80. // Initialize initializes this panel and is normally used by other types which embed a panel.
  81. func (p *Panel) Initialize(width, height float32) {
  82. p.width = width
  83. p.height = height
  84. // Builds array with vertex positions and texture coordinates
  85. positions := math32.NewArrayF32(0, 20)
  86. positions.Append(
  87. 0, 0, 0, 0, 1,
  88. 0, -1, 0, 0, 0,
  89. 1, -1, 0, 1, 0,
  90. 1, 0, 0, 1, 1,
  91. )
  92. // Builds array of indices
  93. indices := math32.NewArrayU32(0, 6)
  94. indices.Append(0, 1, 2, 0, 2, 3)
  95. // Creates geometry
  96. geom := geometry.NewGeometry()
  97. geom.SetIndices(indices)
  98. geom.AddVBO(gls.NewVBO().
  99. AddAttrib("VertexPosition", 3).
  100. AddAttrib("VertexTexcoord", 2).
  101. SetBuffer(positions),
  102. )
  103. // Initialize material
  104. p.mat = material.NewMaterial()
  105. p.mat.SetShader("shaderPanel")
  106. // Initialize graphic
  107. p.Graphic.Init(geom, gls.TRIANGLES)
  108. p.AddMaterial(p, p.mat, 0, 0)
  109. // Creates and adds uniform
  110. p.modelMatrixUni.Init("ModelMatrix")
  111. p.borderColorUni.Init("BorderColor")
  112. p.paddingColorUni.Init("PaddingColor")
  113. p.contentColorUni.Init("ContentColor")
  114. p.boundsUni.Init("Bounds")
  115. p.borderUni.Init("Border")
  116. p.paddingUni.Init("Padding")
  117. p.contentUni.Init("Content")
  118. // Set defaults
  119. p.borderColorUni.Set(0, 0, 0, 1)
  120. p.bounded = true
  121. p.enabled = true
  122. p.resize(width, height)
  123. }
  124. // GetPanel satisfies the IPanel interface and
  125. // returns pointer to this panel
  126. func (pan *Panel) GetPanel() *Panel {
  127. return pan
  128. }
  129. // SetRoot satisfies the IPanel interface
  130. // and sets the pointer to this panel root container
  131. func (pan *Panel) SetRoot(root *Root) {
  132. pan.root = root
  133. }
  134. // LostKeyFocus satisfies the IPanel interface and is called by gui root
  135. // container when the panel loses the key focus
  136. func (p *Panel) LostKeyFocus() {
  137. }
  138. // TotalHeight satisfies the IPanel interface and returns the total
  139. // height of this panel considering visible not bounded children
  140. func (p *Panel) TotalHeight() float32 {
  141. return p.Height()
  142. }
  143. // SetSelected satisfies the IPanel interface and is normally called
  144. // by a list container to change the panel visual appearance
  145. func (p *Panel) SetSelected2(state bool) {
  146. }
  147. // SetHighlighted satisfies the IPanel interface and is normally called
  148. // by a list container to change the panel visual appearance
  149. func (p *Panel) SetHighlighted2(state bool) {
  150. }
  151. // Material returns a pointer for this panel core.Material
  152. func (p *Panel) Material() *material.Material {
  153. return p.mat
  154. }
  155. // SetTopChild sets the Z coordinate of the specified panel to
  156. // be on top of all other children of this panel.
  157. // The function does not check if the specified panel is a
  158. // child of this one.
  159. func (p *Panel) SetTopChild(ipan IPanel) {
  160. // Remove panel and if found appends to the end
  161. found := p.Remove(ipan)
  162. if found {
  163. p.Add(ipan)
  164. }
  165. }
  166. // SetPosition sets this panel absolute position in pixel coordinates
  167. // from left to right and from top to bottom of the screen.
  168. func (p *Panel) SetPosition(x, y float32) {
  169. p.Node.SetPositionX(math32.Round(x))
  170. p.Node.SetPositionY(math32.Round(y))
  171. }
  172. // SetSize sets this panel external width and height in pixels.
  173. func (p *Panel) SetSize(width, height float32) {
  174. if width < 0 {
  175. log.Warn("Invalid panel width:%v", width)
  176. width = 0
  177. }
  178. if height < 0 {
  179. log.Warn("Invalid panel height:%v", height)
  180. height = 0
  181. }
  182. p.resize(width, height)
  183. }
  184. // SetWidth sets this panel external width in pixels.
  185. // The internal panel areas and positions are recalculated
  186. func (p *Panel) SetWidth(width float32) {
  187. p.SetSize(width, p.height)
  188. }
  189. // SetHeight sets this panel external height in pixels.
  190. // The internal panel areas and positions are recalculated
  191. func (p *Panel) SetHeight(height float32) {
  192. p.SetSize(p.width, height)
  193. }
  194. // SetContentAspectWidth sets the width of the content area of the panel
  195. // to the specified value and adjusts its height to keep the same aspect radio.
  196. func (p *Panel) SetContentAspectWidth(width float32) {
  197. aspect := p.content.Width / p.content.Height
  198. height := width / aspect
  199. p.SetContentSize(width, height)
  200. }
  201. // SetContentAspectHeight sets the height of the content area of the panel
  202. // to the specified value and adjusts its width to keep the same aspect ratio.
  203. func (p *Panel) SetContentAspectHeight(height float32) {
  204. aspect := p.content.Width / p.content.Height
  205. width := height / aspect
  206. p.SetContentSize(width, height)
  207. }
  208. // Width returns the current panel external width in pixels
  209. func (p *Panel) Width() float32 {
  210. return p.width
  211. }
  212. // Height returns the current panel external height in pixels
  213. func (p *Panel) Height() float32 {
  214. return p.height
  215. }
  216. // ContentWidth returns the current width of the content area in pixels
  217. func (p *Panel) ContentWidth() float32 {
  218. return p.content.Width
  219. }
  220. // ContentHeight returns the current height of the content area in pixels
  221. func (p *Panel) ContentHeight() float32 {
  222. return p.content.Height
  223. }
  224. // SetMargins set this panel margin sizes in pixels
  225. // and recalculates the panel external size
  226. func (p *Panel) SetMargins(top, right, bottom, left float32) {
  227. p.marginSizes.Set(top, right, bottom, left)
  228. p.resize(p.calcWidth(), p.calcHeight())
  229. }
  230. // SetMarginsFrom sets this panel margins sizes from the specified
  231. // BorderSizes pointer and recalculates the panel external size
  232. func (p *Panel) SetMarginsFrom(src *BorderSizes) {
  233. p.marginSizes = *src
  234. p.resize(p.calcWidth(), p.calcHeight())
  235. }
  236. // Margins returns the current margin sizes in pixels
  237. func (p *Panel) Margins() BorderSizes {
  238. return p.marginSizes
  239. }
  240. // SetBorders sets this panel border sizes in pixels
  241. // and recalculates the panel external size
  242. func (p *Panel) SetBorders(top, right, bottom, left float32) {
  243. p.borderSizes.Set(top, right, bottom, left)
  244. p.resize(p.calcWidth(), p.calcHeight())
  245. }
  246. // SetBordersFrom sets this panel border sizes from the specified
  247. // BorderSizes pointer and recalculates the panel size
  248. func (p *Panel) SetBordersFrom(src *BorderSizes) {
  249. p.borderSizes = *src
  250. p.resize(p.calcWidth(), p.calcHeight())
  251. }
  252. // Borders returns this panel current border sizes
  253. func (p *Panel) Borders() BorderSizes {
  254. return p.borderSizes
  255. }
  256. // SetPaddings sets the panel padding sizes in pixels
  257. func (p *Panel) SetPaddings(top, right, bottom, left float32) {
  258. p.paddingSizes.Set(top, right, bottom, left)
  259. p.resize(p.calcWidth(), p.calcHeight())
  260. }
  261. // SetPaddingsFrom sets this panel padding sizes from the specified
  262. // BorderSizes pointer and recalculates the panel size
  263. func (p *Panel) SetPaddingsFrom(src *BorderSizes) {
  264. p.paddingSizes = *src
  265. p.resize(p.calcWidth(), p.calcHeight())
  266. }
  267. // Paddings returns this panel padding sizes in pixels
  268. func (p *Panel) Paddings() BorderSizes {
  269. return p.paddingSizes
  270. }
  271. // SetBordersColor sets the color of this panel borders
  272. // The borders opacity is set to 1.0 (full opaque)
  273. func (p *Panel) SetBordersColor(color *math32.Color) {
  274. p.borderColorUni.Set(color.R, color.G, color.B, 1)
  275. }
  276. // SetBordersColor4 sets the color and opacity of this panel borders
  277. func (p *Panel) SetBordersColor4(color *math32.Color4) {
  278. p.borderColorUni.Set(color.R, color.G, color.B, color.A)
  279. }
  280. // BorderColor4 returns current border color
  281. func (p *Panel) BordersColor4() math32.Color4 {
  282. var color math32.Color4
  283. p.borderColorUni.SetColor4(&color)
  284. return color
  285. }
  286. // SetPaddingsColor sets the color of this panel paddings.
  287. func (p *Panel) SetPaddingsColor(color *math32.Color) {
  288. p.paddingColorUni.Set(color.R, color.G, color.B, 1.0)
  289. }
  290. // SetColor sets the color of the panel paddings and content area
  291. func (p *Panel) SetColor(color *math32.Color) *Panel {
  292. p.paddingColorUni.Set(color.R, color.G, color.B, 1.0)
  293. p.contentColorUni.Set(color.R, color.G, color.B, 1.0)
  294. return p
  295. }
  296. // SetColor4 sets the color of the panel paddings and content area
  297. func (p *Panel) SetColor4(color *math32.Color4) *Panel {
  298. p.paddingColorUni.Set(color.R, color.G, color.B, color.A)
  299. p.contentColorUni.Set(color.R, color.G, color.B, color.A)
  300. return p
  301. }
  302. // Color4 returns the current color of the panel content area
  303. func (p *Panel) Color4() math32.Color4 {
  304. return p.contentColorUni.GetColor4()
  305. }
  306. // SetContentSize sets this panel content size to the specified dimensions.
  307. // The external size of the panel may increase or decrease to acomodate
  308. // the new content size.
  309. func (p *Panel) SetContentSize(width, height float32) {
  310. // Calculates the new desired external width and height
  311. eWidth := width +
  312. p.paddingSizes.Left + p.paddingSizes.Right +
  313. p.borderSizes.Left + p.borderSizes.Right +
  314. p.marginSizes.Left + p.marginSizes.Right
  315. eHeight := height +
  316. p.paddingSizes.Top + p.paddingSizes.Bottom +
  317. p.borderSizes.Top + p.borderSizes.Bottom +
  318. p.marginSizes.Top + p.marginSizes.Bottom
  319. p.resize(eWidth, eHeight)
  320. }
  321. // SetContentWidth sets this panel content width to the specified dimension in pixels.
  322. // The external size of the panel may increase or decrease to acomodate the new width
  323. func (p *Panel) SetContentWidth(width float32) {
  324. p.SetContentSize(width, p.content.Height)
  325. }
  326. // SetContentHeight sets this panel content height to the specified dimension in pixels.
  327. // The external size of the panel may increase or decrease to acomodate the new width
  328. func (p *Panel) SetContentHeight(height float32) {
  329. p.SetContentSize(p.content.Width, height)
  330. }
  331. // MinWidth returns the minimum width of this panel (ContentWidth = 0)
  332. func (p *Panel) MinWidth() float32 {
  333. return p.paddingSizes.Left + p.paddingSizes.Right +
  334. p.borderSizes.Left + p.borderSizes.Right +
  335. p.marginSizes.Left + p.marginSizes.Right
  336. }
  337. // MinHeight returns the minimum height of this panel (ContentHeight = 0)
  338. func (p *Panel) MinHeight() float32 {
  339. return p.paddingSizes.Top + p.paddingSizes.Bottom +
  340. p.borderSizes.Top + p.borderSizes.Bottom +
  341. p.marginSizes.Top + p.marginSizes.Bottom
  342. }
  343. // Add adds a child panel to this one
  344. func (p *Panel) Add(ichild IPanel) *Panel {
  345. p.Node.Add(ichild)
  346. node := ichild.GetPanel()
  347. node.SetParent(p)
  348. if p.layout != nil {
  349. p.layout.Recalc(p)
  350. }
  351. p.Dispatch(OnChild, nil)
  352. return p
  353. }
  354. // Remove removes the specified child from this panel
  355. func (p *Panel) Remove(ichild IPanel) bool {
  356. res := p.Node.Remove(ichild)
  357. if res {
  358. if p.layout != nil {
  359. p.layout.Recalc(p)
  360. }
  361. p.Dispatch(OnChild, nil)
  362. }
  363. return res
  364. }
  365. // Bounded returns this panel bounded state
  366. func (p *Panel) Bounded() bool {
  367. return p.bounded
  368. }
  369. // SetBounded sets this panel bounded state
  370. func (p *Panel) SetBounded(bounded bool) {
  371. p.bounded = bounded
  372. }
  373. // UpdateMatrixWorld overrides the standard core.Node version which is called by
  374. // the Engine before rendering the frame.
  375. func (p *Panel) UpdateMatrixWorld() {
  376. par := p.Parent()
  377. // Panel has no parent
  378. if par == nil {
  379. p.updateBounds(nil)
  380. p.setZ(0)
  381. // Panel has parent
  382. } else {
  383. par, ok := par.(*Panel)
  384. if ok {
  385. p.updateBounds(par)
  386. // Parent is not a panel
  387. } else {
  388. p.updateBounds(nil)
  389. }
  390. }
  391. // Update this panel children
  392. for _, ichild := range p.Children() {
  393. ichild.UpdateMatrixWorld()
  394. }
  395. }
  396. // setZ calculates the Z coordinate for each panel recursively
  397. // starting at the specified receiver with the specified Z coordinate.
  398. // The Z coordinate is set so panels added later are closed to the screen
  399. func (p *Panel) setZ(nextZ float32) float32 {
  400. p.SetPositionZ(nextZ)
  401. nextZ += deltaZ
  402. for _, ichild := range p.Children() {
  403. nextZ = ichild.(IPanel).GetPanel().setZ(nextZ)
  404. }
  405. return nextZ
  406. }
  407. // ContainsPosition returns indication if this panel contains
  408. // the specified screen position in pixels.
  409. func (p *Panel) ContainsPosition(x, y float32) bool {
  410. if x < p.pospix.X || x >= (p.pospix.X+p.width) {
  411. return false
  412. }
  413. if y < p.pospix.Y || y >= (p.pospix.Y+p.height) {
  414. return false
  415. }
  416. return true
  417. }
  418. // SetEnabled sets the panel enabled state
  419. // A disabled panel do not process key or mouse events.
  420. func (p *Panel) SetEnabled(state bool) {
  421. p.enabled = state
  422. p.Dispatch(OnEnable, nil)
  423. }
  424. // Enabled returns the current enabled state of this panel
  425. func (p *Panel) Enabled() bool {
  426. return p.enabled
  427. }
  428. // SetLayout sets the layout to use to position the children of this panel
  429. // To remove the layout, call this function passing nil as parameter.
  430. func (p *Panel) SetLayout(ilayout ILayout) {
  431. p.layout = ilayout
  432. if p.layout != nil {
  433. p.layout.Recalc(p)
  434. }
  435. }
  436. // SetLayoutParams sets the layout parameters for this panel
  437. func (p *Panel) SetLayoutParams(params interface{}) {
  438. p.layoutParams = params
  439. }
  440. // updateBounds is called by UpdateMatrixWorld() and calculates this panel
  441. // bounds considering the bounds of its parent
  442. func (p *Panel) updateBounds(par *Panel) {
  443. if par == nil {
  444. p.pospix = p.Position()
  445. p.xmin = p.pospix.X
  446. p.ymin = p.pospix.Y
  447. p.xmax = p.pospix.X + p.width
  448. p.ymax = p.pospix.Y + p.height
  449. p.boundsUni.Set(0, 0, 1, 1)
  450. return
  451. }
  452. // If this panel is bounded to its parent, its coordinates are relative
  453. // to the parent internal content rectangle.
  454. if p.bounded {
  455. p.pospix.X = p.Position().X + par.pospix.X + float32(par.marginSizes.Left+par.borderSizes.Left+par.paddingSizes.Left)
  456. p.pospix.Y = p.Position().Y + par.pospix.Y + float32(par.marginSizes.Top+par.borderSizes.Top+par.paddingSizes.Top)
  457. p.pospix.Z = p.Position().Z + par.pospix.Z
  458. // Otherwise its coordinates are relative to the parent outer coordinates.
  459. } else {
  460. p.pospix.X = p.Position().X + par.pospix.X
  461. p.pospix.Y = p.Position().Y + par.pospix.Y
  462. p.pospix.Z = p.Position().Z + par.pospix.Z
  463. }
  464. // Maximum x,y coordinates for this panel
  465. p.xmin = p.pospix.X
  466. p.ymin = p.pospix.Y
  467. p.xmax = p.pospix.X + p.width
  468. p.ymax = p.pospix.Y + p.height
  469. if p.bounded {
  470. // Get the parent minimum X and Y absolute coordinates in pixels
  471. pxmin := par.xmin + par.marginSizes.Right + par.borderSizes.Right + par.paddingSizes.Right
  472. pymin := par.ymin + par.marginSizes.Bottom + par.borderSizes.Bottom + par.paddingSizes.Bottom
  473. // Get the parent maximum X and Y absolute coordinates in pixels
  474. pxmax := par.xmax - (par.marginSizes.Right + par.borderSizes.Right + par.paddingSizes.Right)
  475. pymax := par.ymax - (par.marginSizes.Bottom + par.borderSizes.Bottom + par.paddingSizes.Bottom)
  476. // Update this panel minimum x and y coordinates.
  477. if p.xmin < pxmin {
  478. p.xmin = pxmin
  479. }
  480. if p.ymin < pymin {
  481. p.ymin = pymin
  482. }
  483. // Update this panel maximum x and y coordinates.
  484. if p.xmax > pxmax {
  485. p.xmax = pxmax
  486. }
  487. if p.ymax > pymax {
  488. p.ymax = pymax
  489. }
  490. }
  491. // Set default values for bounds in texture coordinates
  492. xmintex := float32(0.0)
  493. ymintex := float32(0.0)
  494. xmaxtex := float32(1.0)
  495. ymaxtex := float32(1.0)
  496. // If this panel is bounded to its parent, calculates the bounds
  497. // for clipping in texture coordinates
  498. if p.bounded {
  499. if p.pospix.X < p.xmin {
  500. xmintex = (p.xmin - p.pospix.X) / p.width
  501. }
  502. if p.pospix.Y < p.ymin {
  503. ymintex = (p.ymin - p.pospix.Y) / p.height
  504. }
  505. if p.pospix.X+p.width > p.xmax {
  506. xmaxtex = (p.xmax - p.pospix.X) / p.width
  507. }
  508. if p.pospix.Y+p.height > p.ymax {
  509. ymaxtex = (p.ymax - p.pospix.Y) / p.height
  510. }
  511. }
  512. // Sets bounds uniform
  513. p.boundsUni.Set(xmintex, ymintex, xmaxtex, ymaxtex)
  514. }
  515. // calcWidth calculates the panel external width in pixels
  516. func (p *Panel) calcWidth() float32 {
  517. return p.content.Width +
  518. p.paddingSizes.Left + p.paddingSizes.Right +
  519. p.borderSizes.Left + p.borderSizes.Right +
  520. p.marginSizes.Left + p.marginSizes.Right
  521. }
  522. // calcHeight calculates the panel external height in pixels
  523. func (p *Panel) calcHeight() float32 {
  524. return p.content.Height +
  525. p.paddingSizes.Top + p.paddingSizes.Bottom +
  526. p.borderSizes.Top + p.borderSizes.Bottom +
  527. p.marginSizes.Top + p.marginSizes.Bottom
  528. }
  529. // resize tries to set the external size of the panel to the specified
  530. // dimensions and recalculates the size and positions of the internal areas.
  531. // The margins, borders and padding sizes are kept and the content
  532. // area size is adjusted. So if the panel is decreased, its minimum
  533. // size is determined by the margins, borders and paddings.
  534. func (p *Panel) resize(width, height float32) {
  535. var padding Rect
  536. var border Rect
  537. width = math32.Round(width)
  538. height = math32.Round(height)
  539. // Adjusts content width
  540. p.content.Width = width -
  541. p.marginSizes.Left - p.marginSizes.Right -
  542. p.borderSizes.Left - p.borderSizes.Right -
  543. p.paddingSizes.Left - p.paddingSizes.Right
  544. if p.content.Width < 0 {
  545. p.content.Width = 0
  546. }
  547. // Adjust other area widths
  548. padding.Width = p.paddingSizes.Left + p.content.Width + p.paddingSizes.Right
  549. border.Width = p.borderSizes.Left + padding.Width + p.borderSizes.Right
  550. // Adjusts content height
  551. p.content.Height = height -
  552. p.marginSizes.Top - p.marginSizes.Bottom -
  553. p.borderSizes.Top - p.borderSizes.Bottom -
  554. p.paddingSizes.Top - p.paddingSizes.Bottom
  555. if p.content.Height < 0 {
  556. p.content.Height = 0
  557. }
  558. // Adjust other area heights
  559. padding.Height = p.paddingSizes.Top + p.content.Height + p.paddingSizes.Bottom
  560. border.Height = p.borderSizes.Top + padding.Height + p.borderSizes.Bottom
  561. // Sets area positions
  562. border.X = p.marginSizes.Left
  563. border.Y = p.marginSizes.Top
  564. padding.X = border.X + p.borderSizes.Left
  565. padding.Y = border.Y + p.borderSizes.Top
  566. p.content.X = padding.X + p.paddingSizes.Left
  567. p.content.Y = padding.Y + p.paddingSizes.Top
  568. // Sets final panel dimensions (may be different from requested dimensions)
  569. p.width = p.marginSizes.Left + border.Width + p.marginSizes.Right
  570. p.height = p.marginSizes.Top + border.Height + p.marginSizes.Bottom
  571. // Updates border uniform in texture coordinates (0,0 -> 1,1)
  572. p.borderUni.Set(
  573. float32(border.X)/float32(p.width),
  574. float32(border.Y)/float32(p.height),
  575. float32(border.Width)/float32(p.width),
  576. float32(border.Height)/float32(p.height),
  577. )
  578. // Updates padding uniform in texture coordinates (0,0 -> 1,1)
  579. p.paddingUni.Set(
  580. float32(padding.X)/float32(p.width),
  581. float32(padding.Y)/float32(p.height),
  582. float32(padding.Width)/float32(p.width),
  583. float32(padding.Height)/float32(p.height),
  584. )
  585. // Updates content uniform in texture coordinates (0,0 -> 1,1)
  586. p.contentUni.Set(
  587. float32(p.content.X)/float32(p.width),
  588. float32(p.content.Y)/float32(p.height),
  589. float32(p.content.Width)/float32(p.width),
  590. float32(p.content.Height)/float32(p.height),
  591. )
  592. // Update layout and dispatch event
  593. if p.layout != nil {
  594. p.layout.Recalc(p)
  595. }
  596. p.Dispatch(OnResize, nil)
  597. }
  598. // RenderSetup is called by the Engine before drawing the object
  599. func (p *Panel) RenderSetup(gl *gls.GLS, rinfo *core.RenderInfo) {
  600. // Sets model matrix
  601. var mm math32.Matrix4
  602. p.SetModelMatrix(gl, &mm)
  603. p.modelMatrixUni.SetMatrix4(&mm)
  604. // Transfer uniforms
  605. p.borderColorUni.Transfer(gl)
  606. p.paddingColorUni.Transfer(gl)
  607. p.contentColorUni.Transfer(gl)
  608. p.boundsUni.Transfer(gl)
  609. p.borderUni.Transfer(gl)
  610. p.paddingUni.Transfer(gl)
  611. p.contentUni.Transfer(gl)
  612. p.modelMatrixUni.Transfer(gl)
  613. }
  614. // SetModelMatrix calculates and sets the specified matrix with the model matrix for this panel
  615. func (p *Panel) SetModelMatrix(gl *gls.GLS, mm *math32.Matrix4) {
  616. // Get the current viewport width and height
  617. _, _, width, height := gl.GetViewport()
  618. fwidth := float32(width)
  619. fheight := float32(height)
  620. // Scale the quad for the viewport so it has fixed dimensions in pixels.
  621. fw := float32(p.width) / fwidth
  622. fh := float32(p.height) / fheight
  623. var scale math32.Vector3
  624. scale.Set(2*fw, 2*fh, 1)
  625. // Convert absolute position in pixel coordinates from the top/left to
  626. // standard OpenGL clip coordinates of the quad center
  627. var posclip math32.Vector3
  628. posclip.X = (p.pospix.X - fwidth/2) / (fwidth / 2)
  629. posclip.Y = -(p.pospix.Y - fheight/2) / (fheight / 2)
  630. posclip.Z = p.Position().Z
  631. //log.Debug("panel posclip:%v\n", posclip)
  632. // Calculates the model matrix
  633. var quat math32.Quaternion
  634. quat.SetIdentity()
  635. mm.Compose(&posclip, &quat, &scale)
  636. }