panel.go 24 KB

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