panel.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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. // Quad geometry shared by ALL Panels
  90. var panelQuadGeometry *geometry.Geometry
  91. // NewPanel creates and returns a pointer to a new panel with the
  92. // specified dimensions in pixels and a default quad geometry
  93. func NewPanel(width, height float32) *Panel {
  94. p := new(Panel)
  95. p.Initialize(width, height)
  96. return p
  97. }
  98. // Initialize initializes this panel and is normally used by other types which embed a panel.
  99. func (p *Panel) Initialize(width, height float32) {
  100. p.width = width
  101. p.height = height
  102. // If necessary, creates panel quad geometry
  103. if panelQuadGeometry == nil {
  104. // Builds array with vertex positions and texture coordinates
  105. positions := math32.NewArrayF32(0, 20)
  106. positions.Append(
  107. 0, 0, 0, 0, 1,
  108. 0, -1, 0, 0, 0,
  109. 1, -1, 0, 1, 0,
  110. 1, 0, 0, 1, 1,
  111. )
  112. // Builds array of indices
  113. indices := math32.NewArrayU32(0, 6)
  114. indices.Append(0, 1, 2, 0, 2, 3)
  115. // Creates geometry
  116. geom := geometry.NewGeometry()
  117. geom.SetIndices(indices)
  118. geom.AddVBO(gls.NewVBO().
  119. AddAttrib("VertexPosition", 3).
  120. AddAttrib("VertexTexcoord", 2).
  121. SetBuffer(positions),
  122. )
  123. panelQuadGeometry = geom
  124. }
  125. // Initialize material
  126. p.mat = material.NewMaterial()
  127. p.mat.SetShader("panel")
  128. p.mat.SetShaderUnique(true)
  129. // Initialize graphic
  130. p.Graphic = graphic.NewGraphic(panelQuadGeometry.Incref(), gls.TRIANGLES)
  131. p.AddMaterial(p, p.mat, 0, 0)
  132. // Initialize uniforms location caches
  133. p.uniMatrix.Init("ModelMatrix")
  134. p.uniPanel.Init("Panel")
  135. // Set defaults
  136. p.udata.bordersColor = math32.Color4{0, 0, 0, 1}
  137. p.bounded = true
  138. p.enabled = true
  139. p.resize(width, height, true)
  140. }
  141. // InitializeGraphic initializes this panel with a different graphic
  142. func (p *Panel) InitializeGraphic(width, height float32, gr *graphic.Graphic) {
  143. p.Graphic = gr
  144. p.width = width
  145. p.height = height
  146. // Initializes uniforms location caches
  147. p.uniMatrix.Init("ModelMatrix")
  148. p.uniPanel.Init("Panel")
  149. // Set defaults
  150. p.udata.bordersColor = math32.Color4{0, 0, 0, 1}
  151. p.bounded = true
  152. p.enabled = true
  153. p.resize(width, height, true)
  154. }
  155. // GetPanel satisfies the IPanel interface and
  156. // returns pointer to this panel
  157. func (pan *Panel) GetPanel() *Panel {
  158. return pan
  159. }
  160. // SetRoot satisfies the IPanel interface
  161. // Sets the pointer to the root panel for this panel and all its children
  162. func (p *Panel) SetRoot(root *Root) {
  163. p.root = root
  164. for i := 0; i < len(p.Children()); i++ {
  165. cpan := p.Children()[i].(IPanel).GetPanel()
  166. cpan.SetRoot(root)
  167. }
  168. }
  169. // LostKeyFocus satisfies the IPanel interface and is called by gui root
  170. // container when the panel loses the key focus
  171. func (p *Panel) LostKeyFocus() {
  172. }
  173. // TotalHeight satisfies the IPanel interface and returns the total
  174. // height of this panel considering visible not bounded children
  175. func (p *Panel) TotalHeight() float32 {
  176. return p.Height()
  177. }
  178. // Material returns a pointer for this panel core.Material
  179. func (p *Panel) Material() *material.Material {
  180. return p.mat
  181. }
  182. // Root returns the pointer for this panel root panel
  183. func (p *Panel) Root() *Root {
  184. return p.root
  185. }
  186. // SetTopChild sets the Z coordinate of the specified panel to
  187. // be on top of all other children of this panel.
  188. // The function does not check if the specified panel is a
  189. // child of this one.
  190. func (p *Panel) SetTopChild(ipan IPanel) {
  191. // Remove panel and if found appends to the end
  192. found := p.Remove(ipan)
  193. if found {
  194. p.Add(ipan)
  195. }
  196. }
  197. // SetPosition sets this panel absolute position in pixel coordinates
  198. // from left to right and from top to bottom of the screen.
  199. func (p *Panel) SetPosition(x, y float32) {
  200. p.Node.SetPositionX(math32.Round(x))
  201. p.Node.SetPositionY(math32.Round(y))
  202. }
  203. // SetSize sets this panel external width and height in pixels.
  204. func (p *Panel) SetSize(width, height float32) {
  205. if width < 0 {
  206. log.Warn("Invalid panel width:%v", width)
  207. width = 0
  208. }
  209. if height < 0 {
  210. log.Warn("Invalid panel height:%v", height)
  211. height = 0
  212. }
  213. p.resize(width, height, true)
  214. }
  215. // SetWidth sets this panel external width in pixels.
  216. // The internal panel areas and positions are recalculated
  217. func (p *Panel) SetWidth(width float32) {
  218. p.SetSize(width, p.height)
  219. }
  220. // SetHeight sets this panel external height in pixels.
  221. // The internal panel areas and positions are recalculated
  222. func (p *Panel) SetHeight(height float32) {
  223. p.SetSize(p.width, height)
  224. }
  225. // SetContentAspectWidth sets the width of the content area of the panel
  226. // to the specified value and adjusts its height to keep the same aspect radio.
  227. func (p *Panel) SetContentAspectWidth(width float32) {
  228. aspect := p.content.Width / p.content.Height
  229. height := width / aspect
  230. p.SetContentSize(width, height)
  231. }
  232. // SetContentAspectHeight sets the height of the content area of the panel
  233. // to the specified value and adjusts its width to keep the same aspect ratio.
  234. func (p *Panel) SetContentAspectHeight(height float32) {
  235. aspect := p.content.Width / p.content.Height
  236. width := height / aspect
  237. p.SetContentSize(width, height)
  238. }
  239. // Size returns this panel current external width and height in pixels
  240. func (p *Panel) Size() (float32, float32) {
  241. return p.width, p.height
  242. }
  243. // Width returns the current panel external width in pixels
  244. func (p *Panel) Width() float32 {
  245. return p.width
  246. }
  247. // Height returns the current panel external height in pixels
  248. func (p *Panel) Height() float32 {
  249. return p.height
  250. }
  251. // ContentWidth returns the current width of the content area in pixels
  252. func (p *Panel) ContentWidth() float32 {
  253. return p.content.Width
  254. }
  255. // ContentHeight returns the current height of the content area in pixels
  256. func (p *Panel) ContentHeight() float32 {
  257. return p.content.Height
  258. }
  259. // SetMargins set this panel margin sizes in pixels
  260. // and recalculates the panel external size
  261. func (p *Panel) SetMargins(top, right, bottom, left float32) {
  262. p.marginSizes.Set(top, right, bottom, left)
  263. p.resize(p.calcWidth(), p.calcHeight(), true)
  264. }
  265. // SetMarginsFrom sets this panel margins sizes from the specified
  266. // BorderSizes pointer and recalculates the panel external size
  267. func (p *Panel) SetMarginsFrom(src *BorderSizes) {
  268. p.marginSizes = *src
  269. p.resize(p.calcWidth(), p.calcHeight(), true)
  270. }
  271. // Margins returns the current margin sizes in pixels
  272. func (p *Panel) Margins() BorderSizes {
  273. return p.marginSizes
  274. }
  275. // SetBorders sets this panel border sizes in pixels
  276. // and recalculates the panel external size
  277. func (p *Panel) SetBorders(top, right, bottom, left float32) {
  278. p.borderSizes.Set(top, right, bottom, left)
  279. p.resize(p.calcWidth(), p.calcHeight(), true)
  280. }
  281. // SetBordersFrom sets this panel border sizes from the specified
  282. // BorderSizes pointer and recalculates the panel size
  283. func (p *Panel) SetBordersFrom(src *BorderSizes) {
  284. p.borderSizes = *src
  285. p.resize(p.calcWidth(), p.calcHeight(), true)
  286. }
  287. // Borders returns this panel current border sizes
  288. func (p *Panel) Borders() BorderSizes {
  289. return p.borderSizes
  290. }
  291. // SetPaddings sets the panel padding sizes in pixels
  292. func (p *Panel) SetPaddings(top, right, bottom, left float32) {
  293. p.paddingSizes.Set(top, right, bottom, left)
  294. p.resize(p.calcWidth(), p.calcHeight(), true)
  295. }
  296. // SetPaddingsFrom sets this panel padding sizes from the specified
  297. // BorderSizes pointer and recalculates the panel size
  298. func (p *Panel) SetPaddingsFrom(src *BorderSizes) {
  299. p.paddingSizes = *src
  300. p.resize(p.calcWidth(), p.calcHeight(), true)
  301. }
  302. // Paddings returns this panel padding sizes in pixels
  303. func (p *Panel) Paddings() BorderSizes {
  304. return p.paddingSizes
  305. }
  306. // SetBordersColor sets the color of this panel borders
  307. // The borders opacity is set to 1.0 (full opaque)
  308. func (p *Panel) SetBordersColor(color *math32.Color) {
  309. p.udata.bordersColor = math32.Color4{color.R, color.G, color.B, 1}
  310. }
  311. // SetBordersColor4 sets the color and opacity of this panel borders
  312. func (p *Panel) SetBordersColor4(color *math32.Color4) {
  313. p.udata.bordersColor = *color
  314. }
  315. // BorderColor4 returns current border color
  316. func (p *Panel) BordersColor4() math32.Color4 {
  317. return p.udata.bordersColor
  318. }
  319. // SetPaddingsColor sets the color of this panel paddings.
  320. func (p *Panel) SetPaddingsColor(color *math32.Color) {
  321. p.udata.paddingsColor = math32.Color4{color.R, color.G, color.B, 1}
  322. }
  323. // SetColor sets the color of the panel paddings and content area
  324. func (p *Panel) SetColor(color *math32.Color) *Panel {
  325. p.udata.paddingsColor = math32.Color4{color.R, color.G, color.B, 1}
  326. p.udata.contentColor = p.udata.paddingsColor
  327. return p
  328. }
  329. // SetColor4 sets the color of the panel paddings and content area
  330. func (p *Panel) SetColor4(color *math32.Color4) *Panel {
  331. p.udata.paddingsColor = *color
  332. p.udata.contentColor = *color
  333. return p
  334. }
  335. // Color4 returns the current color of the panel content area
  336. func (p *Panel) Color4() math32.Color4 {
  337. return p.udata.contentColor
  338. }
  339. // SetContentSize sets this panel content size to the specified dimensions.
  340. // The external size of the panel may increase or decrease to acomodate
  341. // the new content size.
  342. func (p *Panel) SetContentSize(width, height float32) {
  343. p.setContentSize(width, height, true)
  344. }
  345. // SetContentWidth sets this panel content width to the specified dimension in pixels.
  346. // The external size of the panel may increase or decrease to acomodate the new width
  347. func (p *Panel) SetContentWidth(width float32) {
  348. p.SetContentSize(width, p.content.Height)
  349. }
  350. // SetContentHeight sets this panel content height to the specified dimension in pixels.
  351. // The external size of the panel may increase or decrease to acomodate the new width
  352. func (p *Panel) SetContentHeight(height float32) {
  353. p.SetContentSize(p.content.Width, height)
  354. }
  355. // MinWidth returns the minimum width of this panel (ContentWidth = 0)
  356. func (p *Panel) MinWidth() float32 {
  357. return p.paddingSizes.Left + p.paddingSizes.Right +
  358. p.borderSizes.Left + p.borderSizes.Right +
  359. p.marginSizes.Left + p.marginSizes.Right
  360. }
  361. // MinHeight returns the minimum height of this panel (ContentHeight = 0)
  362. func (p *Panel) MinHeight() float32 {
  363. return p.paddingSizes.Top + p.paddingSizes.Bottom +
  364. p.borderSizes.Top + p.borderSizes.Bottom +
  365. p.marginSizes.Top + p.marginSizes.Bottom
  366. }
  367. // Pospix returns this panel absolute coordinate in pixels
  368. func (p *Panel) Pospix() math32.Vector3 {
  369. return p.pospix
  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. // LayoutParams returns this panel current layout parameters
  468. func (p *Panel) LayoutParams() interface{} {
  469. return p.layoutParams
  470. }
  471. // ContentCoords converts the specified window absolute coordinates in pixels
  472. // (as informed by OnMouse event) to this panel internal content area pixel coordinates
  473. func (p *Panel) ContentCoords(wx, wy float32) (float32, float32) {
  474. cx := wx - p.pospix.X -
  475. p.paddingSizes.Left -
  476. p.borderSizes.Left -
  477. p.marginSizes.Left
  478. cy := wy - p.pospix.Y -
  479. p.paddingSizes.Top -
  480. p.borderSizes.Top -
  481. p.marginSizes.Top
  482. return cx, cy
  483. }
  484. // NDC2Pix converts the specified NDC coordinates (-1,1) to relative pixel coordinates
  485. // for this panel content area.
  486. // 0,0 1,0 0,0 w,0
  487. // +--------+ +---------+
  488. // | | -------> | |
  489. // +--------+ +---------+
  490. // 0,-1 1,-1 0,h w,h
  491. func (p *Panel) NDC2Pix(nx, ny float32) (x, y float32) {
  492. w := p.ContentWidth()
  493. h := p.ContentHeight()
  494. return w * nx, -h * ny
  495. }
  496. // Pix2NDC converts the specified relative pixel coordinates to NDC coordinates for this panel
  497. // content area
  498. // 0,0 w,0 0,0 1,0
  499. // +---------+ +---------+
  500. // | | ------> | |
  501. // +---------+ +---------+
  502. // 0,h w,h 0,-1 1,-1
  503. func (p *Panel) Pix2NDC(px, py float32) (nx, ny float32) {
  504. w := p.ContentWidth()
  505. h := p.ContentHeight()
  506. return px / w, -py / h
  507. }
  508. // setContentSize is an internal version of SetContentSize() which allows
  509. // to determine if the panel will recalculate its layout and dispatch event.
  510. // It is normally used by layout managers when setting the panel content size
  511. // to avoid another invokation of the layout manager.
  512. func (p *Panel) setContentSize(width, height float32, dispatch bool) {
  513. // Calculates the new desired external width and height
  514. eWidth := width +
  515. p.paddingSizes.Left + p.paddingSizes.Right +
  516. p.borderSizes.Left + p.borderSizes.Right +
  517. p.marginSizes.Left + p.marginSizes.Right
  518. eHeight := height +
  519. p.paddingSizes.Top + p.paddingSizes.Bottom +
  520. p.borderSizes.Top + p.borderSizes.Bottom +
  521. p.marginSizes.Top + p.marginSizes.Bottom
  522. p.resize(eWidth, eHeight, dispatch)
  523. }
  524. // setZ sets the Z coordinate for this panel and its children recursively
  525. // starting at the specified z and zunb coordinates.
  526. // The z coordinate is used for bound panels and zunb for unbounded panels.
  527. // The z coordinate is set so panels added later are closer to the screen.
  528. // All unbounded panels and its children are closer than any of the bounded panels.
  529. func (p *Panel) setZ(z, zunb float32) (float32, float32) {
  530. // Bounded panel
  531. if p.bounded {
  532. p.SetPositionZ(z)
  533. z += deltaZ
  534. for _, ichild := range p.Children() {
  535. z, zunb = ichild.(IPanel).GetPanel().setZ(z, zunb)
  536. }
  537. return z, zunb
  538. // Unbounded panel
  539. } else {
  540. p.SetPositionZ(zunb)
  541. zchild := zunb + deltaZ
  542. zunb += deltaZunb
  543. for _, ichild := range p.Children() {
  544. _, zunb = ichild.(IPanel).GetPanel().setZ(zchild, zunb)
  545. }
  546. return z, zunb
  547. }
  548. }
  549. // updateBounds is called by UpdateMatrixWorld() and calculates this panel
  550. // bounds considering the bounds of its parent
  551. func (p *Panel) updateBounds(par *Panel) {
  552. // If no parent, it is the root panel
  553. if par == nil {
  554. p.pospix = p.Position()
  555. p.xmin = -math.MaxFloat32
  556. p.ymin = -math.MaxFloat32
  557. p.xmax = math.MaxFloat32
  558. p.ymax = math.MaxFloat32
  559. p.udata.bounds = math32.Vector4{0, 0, 1, 1}
  560. return
  561. }
  562. // If this panel is bounded to its parent, its coordinates are relative
  563. // to the parent internal content rectangle.
  564. if p.bounded {
  565. p.pospix.X = p.Position().X + par.pospix.X + par.marginSizes.Left + par.borderSizes.Left + par.paddingSizes.Left
  566. p.pospix.Y = p.Position().Y + par.pospix.Y + par.marginSizes.Top + par.borderSizes.Top + par.paddingSizes.Top
  567. // Otherwise its coordinates are relative to the parent outer coordinates.
  568. } else {
  569. p.pospix.X = p.Position().X + par.pospix.X
  570. p.pospix.Y = p.Position().Y + par.pospix.Y
  571. }
  572. // Maximum x,y coordinates for this panel
  573. p.xmin = p.pospix.X
  574. p.ymin = p.pospix.Y
  575. p.xmax = p.pospix.X + p.width
  576. p.ymax = p.pospix.Y + p.height
  577. if p.bounded {
  578. // Get the parent content area minimum and maximum absolute coordinates in pixels
  579. pxmin := par.pospix.X + par.marginSizes.Left + par.borderSizes.Left + par.paddingSizes.Left
  580. if pxmin < par.xmin {
  581. pxmin = par.xmin
  582. }
  583. pymin := par.pospix.Y + par.marginSizes.Top + par.borderSizes.Top + par.paddingSizes.Top
  584. if pymin < par.ymin {
  585. pymin = par.ymin
  586. }
  587. pxmax := par.pospix.X + par.width - (par.marginSizes.Right + par.borderSizes.Right + par.paddingSizes.Right)
  588. if pxmax > par.xmax {
  589. pxmax = par.xmax
  590. }
  591. pymax := par.pospix.Y + par.height - (par.marginSizes.Bottom + par.borderSizes.Bottom + par.paddingSizes.Bottom)
  592. if pymax > par.ymax {
  593. pymax = par.ymax
  594. }
  595. // Update this panel minimum x and y coordinates.
  596. if p.xmin < pxmin {
  597. p.xmin = pxmin
  598. }
  599. if p.ymin < pymin {
  600. p.ymin = pymin
  601. }
  602. // Update this panel maximum x and y coordinates.
  603. if p.xmax > pxmax {
  604. p.xmax = pxmax
  605. }
  606. if p.ymax > pymax {
  607. p.ymax = pymax
  608. }
  609. }
  610. // Set default values for bounds in texture coordinates
  611. xmintex := float32(0.0)
  612. ymintex := float32(0.0)
  613. xmaxtex := float32(1.0)
  614. ymaxtex := float32(1.0)
  615. // If this panel is bounded to its parent, calculates the bounds
  616. // for clipping in texture coordinates
  617. if p.bounded {
  618. if p.pospix.X < p.xmin {
  619. xmintex = (p.xmin - p.pospix.X) / p.width
  620. }
  621. if p.pospix.Y < p.ymin {
  622. ymintex = (p.ymin - p.pospix.Y) / p.height
  623. }
  624. if p.pospix.X+p.width > p.xmax {
  625. xmaxtex = (p.xmax - p.pospix.X) / p.width
  626. }
  627. if p.pospix.Y+p.height > p.ymax {
  628. ymaxtex = (p.ymax - p.pospix.Y) / p.height
  629. }
  630. }
  631. // Sets bounds uniform
  632. p.udata.bounds = math32.Vector4{xmintex, ymintex, xmaxtex, ymaxtex}
  633. }
  634. // calcWidth calculates the panel external width in pixels
  635. func (p *Panel) calcWidth() float32 {
  636. return p.content.Width +
  637. p.paddingSizes.Left + p.paddingSizes.Right +
  638. p.borderSizes.Left + p.borderSizes.Right +
  639. p.marginSizes.Left + p.marginSizes.Right
  640. }
  641. // calcHeight calculates the panel external height in pixels
  642. func (p *Panel) calcHeight() float32 {
  643. return p.content.Height +
  644. p.paddingSizes.Top + p.paddingSizes.Bottom +
  645. p.borderSizes.Top + p.borderSizes.Bottom +
  646. p.marginSizes.Top + p.marginSizes.Bottom
  647. }
  648. // resize tries to set the external size of the panel to the specified
  649. // dimensions and recalculates the size and positions of the internal areas.
  650. // The margins, borders and padding sizes are kept and the content
  651. // area size is adjusted. So if the panel is decreased, its minimum
  652. // size is determined by the margins, borders and paddings.
  653. // Normally it should be called with dispatch=true to recalculate the
  654. // panel layout and dispatch OnSize event.
  655. func (p *Panel) resize(width, height float32, dispatch bool) {
  656. var padding Rect
  657. var border Rect
  658. width = math32.Round(width)
  659. height = math32.Round(height)
  660. // Adjusts content width
  661. p.content.Width = width -
  662. p.marginSizes.Left - p.marginSizes.Right -
  663. p.borderSizes.Left - p.borderSizes.Right -
  664. p.paddingSizes.Left - p.paddingSizes.Right
  665. if p.content.Width < 0 {
  666. p.content.Width = 0
  667. }
  668. // Adjust other area widths
  669. padding.Width = p.paddingSizes.Left + p.content.Width + p.paddingSizes.Right
  670. border.Width = p.borderSizes.Left + padding.Width + p.borderSizes.Right
  671. // Adjusts content height
  672. p.content.Height = height -
  673. p.marginSizes.Top - p.marginSizes.Bottom -
  674. p.borderSizes.Top - p.borderSizes.Bottom -
  675. p.paddingSizes.Top - p.paddingSizes.Bottom
  676. if p.content.Height < 0 {
  677. p.content.Height = 0
  678. }
  679. // Adjust other area heights
  680. padding.Height = p.paddingSizes.Top + p.content.Height + p.paddingSizes.Bottom
  681. border.Height = p.borderSizes.Top + padding.Height + p.borderSizes.Bottom
  682. // Sets area positions
  683. border.X = p.marginSizes.Left
  684. border.Y = p.marginSizes.Top
  685. padding.X = border.X + p.borderSizes.Left
  686. padding.Y = border.Y + p.borderSizes.Top
  687. p.content.X = padding.X + p.paddingSizes.Left
  688. p.content.Y = padding.Y + p.paddingSizes.Top
  689. // Sets final panel dimensions (may be different from requested dimensions)
  690. p.width = p.marginSizes.Left + border.Width + p.marginSizes.Right
  691. p.height = p.marginSizes.Top + border.Height + p.marginSizes.Bottom
  692. // Updates border uniform in texture coordinates (0,0 -> 1,1)
  693. p.udata.borders = math32.Vector4{
  694. float32(border.X) / float32(p.width),
  695. float32(border.Y) / float32(p.height),
  696. float32(border.Width) / float32(p.width),
  697. float32(border.Height) / float32(p.height),
  698. }
  699. // Updates padding uniform in texture coordinates (0,0 -> 1,1)
  700. p.udata.paddings = math32.Vector4{
  701. float32(padding.X) / float32(p.width),
  702. float32(padding.Y) / float32(p.height),
  703. float32(padding.Width) / float32(p.width),
  704. float32(padding.Height) / float32(p.height),
  705. }
  706. // Updates content uniform in texture coordinates (0,0 -> 1,1)
  707. p.udata.content = math32.Vector4{
  708. float32(p.content.X) / float32(p.width),
  709. float32(p.content.Y) / float32(p.height),
  710. float32(p.content.Width) / float32(p.width),
  711. float32(p.content.Height) / float32(p.height),
  712. }
  713. // Update layout and dispatch event
  714. if !dispatch {
  715. return
  716. }
  717. if p.layout != nil {
  718. p.layout.Recalc(p)
  719. }
  720. p.Dispatch(OnResize, nil)
  721. }
  722. // RenderSetup is called by the Engine before drawing the object
  723. func (p *Panel) RenderSetup(gl *gls.GLS, rinfo *core.RenderInfo) {
  724. // Sets texture valid flag in uniforms
  725. // depending if the material has texture
  726. if p.mat.TextureCount() > 0 {
  727. p.udata.textureValid = 1
  728. } else {
  729. p.udata.textureValid = 0
  730. }
  731. // Sets model matrix
  732. var mm math32.Matrix4
  733. p.SetModelMatrix(gl, &mm)
  734. // Transfer model matrix uniform
  735. location := p.uniMatrix.Location(gl)
  736. gl.UniformMatrix4fv(location, 1, false, &mm[0])
  737. // Transfer panel parameters combined uniform
  738. location = p.uniPanel.Location(gl)
  739. const vec4count = 8
  740. gl.Uniform4fvUP(location, vec4count, unsafe.Pointer(&p.udata))
  741. }
  742. // SetModelMatrix calculates and sets the specified matrix with the model matrix for this panel
  743. func (p *Panel) SetModelMatrix(gl *gls.GLS, mm *math32.Matrix4) {
  744. // Get the current viewport width and height
  745. _, _, width, height := gl.GetViewport()
  746. fwidth := float32(width)
  747. fheight := float32(height)
  748. // Scale the quad for the viewport so it has fixed dimensions in pixels.
  749. p.wclip = 2 * float32(p.width) / fwidth
  750. p.hclip = 2 * float32(p.height) / fheight
  751. var scale math32.Vector3
  752. scale.Set(p.wclip, p.hclip, 1)
  753. // Convert absolute position in pixel coordinates from the top/left to
  754. // standard OpenGL clip coordinates of the quad center
  755. p.posclip.X = (p.pospix.X - fwidth/2) / (fwidth / 2)
  756. p.posclip.Y = -(p.pospix.Y - fheight/2) / (fheight / 2)
  757. p.posclip.Z = p.Position().Z
  758. // Calculates the model matrix
  759. var quat math32.Quaternion
  760. quat.SetIdentity()
  761. mm.Compose(&p.posclip, &quat, &scale)
  762. }