panel.go 27 KB

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