panel.go 26 KB

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