panel.go 27 KB

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