panel.go 26 KB

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