panel.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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 RectBounds // external margin sizes in pixel coordinates
  55. borderSizes RectBounds // border sizes in pixel coordinates
  56. paddingSizes RectBounds // 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. p.SetChanged(true)
  196. }
  197. }
  198. // SetPosition sets this panel absolute position in pixel coordinates
  199. // from left to right and from top to bottom of the screen.
  200. func (p *Panel) SetPosition(x, y float32) {
  201. p.Node.SetPositionX(math32.Round(x))
  202. p.Node.SetPositionY(math32.Round(y))
  203. }
  204. // SetSize sets this panel external width and height in pixels.
  205. func (p *Panel) SetSize(width, height float32) {
  206. if width < 0 {
  207. log.Warn("Invalid panel width:%v", width)
  208. width = 0
  209. }
  210. if height < 0 {
  211. log.Warn("Invalid panel height:%v", height)
  212. height = 0
  213. }
  214. p.resize(width, height, true)
  215. }
  216. // SetWidth sets this panel external width in pixels.
  217. // The internal panel areas and positions are recalculated
  218. func (p *Panel) SetWidth(width float32) {
  219. p.SetSize(width, p.height)
  220. }
  221. // SetHeight sets this panel external height in pixels.
  222. // The internal panel areas and positions are recalculated
  223. func (p *Panel) SetHeight(height float32) {
  224. p.SetSize(p.width, height)
  225. }
  226. // SetContentAspectWidth sets the width of the content area of the panel
  227. // to the specified value and adjusts its height to keep the same aspect radio.
  228. func (p *Panel) SetContentAspectWidth(width float32) {
  229. aspect := p.content.Width / p.content.Height
  230. height := width / aspect
  231. p.SetContentSize(width, height)
  232. }
  233. // SetContentAspectHeight sets the height of the content area of the panel
  234. // to the specified value and adjusts its width to keep the same aspect ratio.
  235. func (p *Panel) SetContentAspectHeight(height float32) {
  236. aspect := p.content.Width / p.content.Height
  237. width := height / aspect
  238. p.SetContentSize(width, height)
  239. }
  240. // Size returns this panel current external width and height in pixels
  241. func (p *Panel) Size() (float32, float32) {
  242. return p.width, p.height
  243. }
  244. // Width returns the current panel external width in pixels
  245. func (p *Panel) Width() float32 {
  246. return p.width
  247. }
  248. // Height returns the current panel external height in pixels
  249. func (p *Panel) Height() float32 {
  250. return p.height
  251. }
  252. // ContentWidth returns the current width of the content area in pixels
  253. func (p *Panel) ContentWidth() float32 {
  254. return p.content.Width
  255. }
  256. // ContentHeight returns the current height of the content area in pixels
  257. func (p *Panel) ContentHeight() float32 {
  258. return p.content.Height
  259. }
  260. // SetMargins set this panel margin sizes in pixels
  261. // and recalculates the panel external size
  262. func (p *Panel) SetMargins(top, right, bottom, left float32) {
  263. p.marginSizes.Set(top, right, bottom, left)
  264. p.resize(p.calcWidth(), p.calcHeight(), true)
  265. }
  266. // SetMarginsFrom sets this panel margins sizes from the specified
  267. // RectBounds pointer and recalculates the panel external size
  268. func (p *Panel) SetMarginsFrom(src *RectBounds) {
  269. p.marginSizes = *src
  270. p.resize(p.calcWidth(), p.calcHeight(), true)
  271. }
  272. // Margins returns the current margin sizes in pixels
  273. func (p *Panel) Margins() RectBounds {
  274. return p.marginSizes
  275. }
  276. // SetBorders sets this panel border sizes in pixels
  277. // and recalculates the panel external size
  278. func (p *Panel) SetBorders(top, right, bottom, left float32) {
  279. p.borderSizes.Set(top, right, bottom, left)
  280. p.resize(p.calcWidth(), p.calcHeight(), true)
  281. }
  282. // SetBordersFrom sets this panel border sizes from the specified
  283. // RectBounds pointer and recalculates the panel size
  284. func (p *Panel) SetBordersFrom(src *RectBounds) {
  285. p.borderSizes = *src
  286. p.resize(p.calcWidth(), p.calcHeight(), true)
  287. }
  288. // Borders returns this panel current border sizes
  289. func (p *Panel) Borders() RectBounds {
  290. return p.borderSizes
  291. }
  292. // SetPaddings sets the panel padding sizes in pixels
  293. func (p *Panel) SetPaddings(top, right, bottom, left float32) {
  294. p.paddingSizes.Set(top, right, bottom, left)
  295. p.resize(p.calcWidth(), p.calcHeight(), true)
  296. }
  297. // SetPaddingsFrom sets this panel padding sizes from the specified
  298. // RectBounds pointer and recalculates the panel size
  299. func (p *Panel) SetPaddingsFrom(src *RectBounds) {
  300. p.paddingSizes = *src
  301. p.resize(p.calcWidth(), p.calcHeight(), true)
  302. }
  303. // Paddings returns this panel padding sizes in pixels
  304. func (p *Panel) Paddings() RectBounds {
  305. return p.paddingSizes
  306. }
  307. // SetBordersColor sets the color of this panel borders
  308. // The borders opacity is set to 1.0 (full opaque)
  309. func (p *Panel) SetBordersColor(color *math32.Color) {
  310. p.udata.bordersColor = math32.Color4{color.R, color.G, color.B, 1}
  311. p.SetChanged(true)
  312. }
  313. // SetBordersColor4 sets the color and opacity of this panel borders
  314. func (p *Panel) SetBordersColor4(color *math32.Color4) {
  315. p.udata.bordersColor = *color
  316. p.SetChanged(true)
  317. }
  318. // BorderColor4 returns current border color
  319. func (p *Panel) BordersColor4() math32.Color4 {
  320. return p.udata.bordersColor
  321. }
  322. // SetPaddingsColor sets the color of this panel paddings.
  323. func (p *Panel) SetPaddingsColor(color *math32.Color) {
  324. p.udata.paddingsColor = math32.Color4{color.R, color.G, color.B, 1}
  325. p.SetChanged(true)
  326. }
  327. // SetColor sets the color of the panel paddings and content area
  328. func (p *Panel) SetColor(color *math32.Color) *Panel {
  329. p.udata.paddingsColor = math32.Color4{color.R, color.G, color.B, 1}
  330. p.udata.contentColor = p.udata.paddingsColor
  331. p.SetChanged(true)
  332. return p
  333. }
  334. // SetColor4 sets the color of the panel paddings and content area
  335. func (p *Panel) SetColor4(color *math32.Color4) *Panel {
  336. p.udata.paddingsColor = *color
  337. p.udata.contentColor = *color
  338. p.SetChanged(true)
  339. return p
  340. }
  341. // Color4 returns the current color of the panel content area
  342. func (p *Panel) Color4() math32.Color4 {
  343. return p.udata.contentColor
  344. }
  345. // SetContentSize sets this panel content size to the specified dimensions.
  346. // The external size of the panel may increase or decrease to acomodate
  347. // the new content size.
  348. func (p *Panel) SetContentSize(width, height float32) {
  349. p.setContentSize(width, height, true)
  350. }
  351. // SetContentWidth sets this panel content width to the specified dimension in pixels.
  352. // The external size of the panel may increase or decrease to acomodate the new width
  353. func (p *Panel) SetContentWidth(width float32) {
  354. p.SetContentSize(width, p.content.Height)
  355. }
  356. // SetContentHeight sets this panel content height to the specified dimension in pixels.
  357. // The external size of the panel may increase or decrease to acomodate the new width
  358. func (p *Panel) SetContentHeight(height float32) {
  359. p.SetContentSize(p.content.Width, height)
  360. }
  361. // MinWidth returns the minimum width of this panel (ContentWidth = 0)
  362. func (p *Panel) MinWidth() float32 {
  363. return p.paddingSizes.Left + p.paddingSizes.Right +
  364. p.borderSizes.Left + p.borderSizes.Right +
  365. p.marginSizes.Left + p.marginSizes.Right
  366. }
  367. // MinHeight returns the minimum height of this panel (ContentHeight = 0)
  368. func (p *Panel) MinHeight() float32 {
  369. return p.paddingSizes.Top + p.paddingSizes.Bottom +
  370. p.borderSizes.Top + p.borderSizes.Bottom +
  371. p.marginSizes.Top + p.marginSizes.Bottom
  372. }
  373. // Pospix returns this panel absolute coordinate in pixels
  374. func (p *Panel) Pospix() math32.Vector3 {
  375. return p.pospix
  376. }
  377. // Add adds a child panel to this one
  378. func (p *Panel) Add(ichild IPanel) *Panel {
  379. p.Node.Add(ichild)
  380. node := ichild.GetPanel()
  381. node.SetParent(p)
  382. if p.root != nil {
  383. ichild.SetRoot(p.root)
  384. p.root.setZ(0, deltaZunb)
  385. }
  386. if p.layout != nil {
  387. p.layout.Recalc(p)
  388. }
  389. p.Dispatch(OnChild, nil)
  390. return p
  391. }
  392. // Remove removes the specified child from this panel
  393. func (p *Panel) Remove(ichild IPanel) bool {
  394. res := p.Node.Remove(ichild)
  395. if res {
  396. if p.layout != nil {
  397. p.layout.Recalc(p)
  398. }
  399. p.Dispatch(OnChild, nil)
  400. }
  401. return res
  402. }
  403. // Bounded returns this panel bounded state
  404. func (p *Panel) Bounded() bool {
  405. return p.bounded
  406. }
  407. // SetBounded sets this panel bounded state
  408. func (p *Panel) SetBounded(bounded bool) {
  409. p.bounded = bounded
  410. p.SetChanged(true)
  411. }
  412. // UpdateMatrixWorld overrides the standard core.Node version which is called by
  413. // the Engine before rendering the frame.
  414. func (p *Panel) UpdateMatrixWorld() {
  415. // Panel has no parent should be the root panel
  416. par := p.Parent()
  417. if par == nil {
  418. p.updateBounds(nil)
  419. // Panel has parent
  420. } else {
  421. parpan := par.(*Panel)
  422. p.updateBounds(parpan)
  423. }
  424. // Update this panel children
  425. for _, ichild := range p.Children() {
  426. ichild.UpdateMatrixWorld()
  427. }
  428. }
  429. // ContainsPosition returns indication if this panel contains
  430. // the specified screen position in pixels.
  431. func (p *Panel) ContainsPosition(x, y float32) bool {
  432. if x < p.pospix.X || x >= (p.pospix.X+p.width) {
  433. return false
  434. }
  435. if y < p.pospix.Y || y >= (p.pospix.Y+p.height) {
  436. return false
  437. }
  438. return true
  439. }
  440. // InsideBorders returns indication if the specified screen
  441. // position in pixels is inside the panel borders, including the borders width.
  442. // Unlike "ContainsPosition" is does not consider the panel margins.
  443. func (p *Panel) InsideBorders(x, y float32) bool {
  444. if x < (p.pospix.X+p.marginSizes.Left) || x >= (p.pospix.X+p.width-p.marginSizes.Right) {
  445. return false
  446. }
  447. if y < (p.pospix.Y+p.marginSizes.Top) || y >= (p.pospix.Y+p.height-p.marginSizes.Bottom) {
  448. return false
  449. }
  450. return true
  451. }
  452. // Intersects returns if this panel intersects with the other panel
  453. func (p *Panel) Intersects(other *Panel) bool {
  454. // Checks if one panel is completely on the left side of the other
  455. if p.pospix.X+p.width <= other.pospix.X || other.pospix.X+other.width <= p.pospix.X {
  456. return false
  457. }
  458. // Checks if one panel is completely above the other
  459. if p.pospix.Y+p.height <= other.pospix.Y || other.pospix.Y+other.height <= p.pospix.Y {
  460. return false
  461. }
  462. return true
  463. }
  464. // SetEnabled sets the panel enabled state
  465. // A disabled panel do not process key or mouse events.
  466. func (p *Panel) SetEnabled(state bool) {
  467. p.enabled = state
  468. p.Dispatch(OnEnable, nil)
  469. }
  470. // Enabled returns the current enabled state of this panel
  471. func (p *Panel) Enabled() bool {
  472. return p.enabled
  473. }
  474. // SetLayout sets the layout to use to position the children of this panel
  475. // To remove the layout, call this function passing nil as parameter.
  476. func (p *Panel) SetLayout(ilayout ILayout) {
  477. p.layout = ilayout
  478. if p.layout != nil {
  479. p.layout.Recalc(p)
  480. }
  481. }
  482. // Layout returns this panel current layout
  483. func (p *Panel) Layout() ILayout {
  484. return p.layout
  485. }
  486. // SetLayoutParams sets the layout parameters for this panel
  487. func (p *Panel) SetLayoutParams(params interface{}) {
  488. p.layoutParams = params
  489. }
  490. // LayoutParams returns this panel current layout parameters
  491. func (p *Panel) LayoutParams() interface{} {
  492. return p.layoutParams
  493. }
  494. // ContentCoords converts the specified window absolute coordinates in pixels
  495. // (as informed by OnMouse event) to this panel internal content area pixel coordinates
  496. func (p *Panel) ContentCoords(wx, wy float32) (float32, float32) {
  497. cx := wx - p.pospix.X -
  498. p.paddingSizes.Left -
  499. p.borderSizes.Left -
  500. p.marginSizes.Left
  501. cy := wy - p.pospix.Y -
  502. p.paddingSizes.Top -
  503. p.borderSizes.Top -
  504. p.marginSizes.Top
  505. return cx, cy
  506. }
  507. // NDC2Pix converts the specified NDC coordinates (-1,1) to relative pixel coordinates
  508. // for this panel content area.
  509. // 0,0 1,0 0,0 w,0
  510. // +--------+ +---------+
  511. // | | -------> | |
  512. // +--------+ +---------+
  513. // 0,-1 1,-1 0,h w,h
  514. func (p *Panel) NDC2Pix(nx, ny float32) (x, y float32) {
  515. w := p.ContentWidth()
  516. h := p.ContentHeight()
  517. return w * nx, -h * ny
  518. }
  519. // Pix2NDC converts the specified relative pixel coordinates to NDC coordinates for this panel
  520. // content area
  521. // 0,0 w,0 0,0 1,0
  522. // +---------+ +---------+
  523. // | | ------> | |
  524. // +---------+ +---------+
  525. // 0,h w,h 0,-1 1,-1
  526. func (p *Panel) Pix2NDC(px, py float32) (nx, ny float32) {
  527. w := p.ContentWidth()
  528. h := p.ContentHeight()
  529. return px / w, -py / h
  530. }
  531. // setContentSize is an internal version of SetContentSize() which allows
  532. // to determine if the panel will recalculate its layout and dispatch event.
  533. // It is normally used by layout managers when setting the panel content size
  534. // to avoid another invokation of the layout manager.
  535. func (p *Panel) setContentSize(width, height float32, dispatch bool) {
  536. // Calculates the new desired external width and height
  537. eWidth := width +
  538. p.paddingSizes.Left + p.paddingSizes.Right +
  539. p.borderSizes.Left + p.borderSizes.Right +
  540. p.marginSizes.Left + p.marginSizes.Right
  541. eHeight := height +
  542. p.paddingSizes.Top + p.paddingSizes.Bottom +
  543. p.borderSizes.Top + p.borderSizes.Bottom +
  544. p.marginSizes.Top + p.marginSizes.Bottom
  545. p.resize(eWidth, eHeight, dispatch)
  546. }
  547. // setZ sets the Z coordinate for this panel and its children recursively
  548. // starting at the specified z and zunb coordinates.
  549. // The z coordinate is used for bound panels and zunb for unbounded panels.
  550. // The z coordinate is set so panels added later are closer to the screen.
  551. // All unbounded panels and its children are closer than any of the bounded panels.
  552. func (p *Panel) setZ(z, zunb float32) (float32, float32) {
  553. // Bounded panel
  554. if p.bounded {
  555. p.SetPositionZ(z)
  556. z += deltaZ
  557. for _, ichild := range p.Children() {
  558. z, zunb = ichild.(IPanel).GetPanel().setZ(z, zunb)
  559. }
  560. return z, zunb
  561. // Unbounded panel
  562. } else {
  563. p.SetPositionZ(zunb)
  564. zchild := zunb + deltaZ
  565. zunb += deltaZunb
  566. for _, ichild := range p.Children() {
  567. _, zunb = ichild.(IPanel).GetPanel().setZ(zchild, zunb)
  568. }
  569. return z, zunb
  570. }
  571. }
  572. // updateBounds is called by UpdateMatrixWorld() and calculates this panel
  573. // bounds considering the bounds of its parent
  574. func (p *Panel) updateBounds(par *Panel) {
  575. // If no parent, it is the root panel
  576. if par == nil {
  577. p.pospix = p.Position()
  578. p.xmin = -math.MaxFloat32
  579. p.ymin = -math.MaxFloat32
  580. p.xmax = math.MaxFloat32
  581. p.ymax = math.MaxFloat32
  582. p.udata.bounds = math32.Vector4{0, 0, 1, 1}
  583. return
  584. }
  585. // If this panel is bounded to its parent, its coordinates are relative
  586. // to the parent internal content rectangle.
  587. if p.bounded {
  588. p.pospix.X = p.Position().X + par.pospix.X + par.marginSizes.Left + par.borderSizes.Left + par.paddingSizes.Left
  589. p.pospix.Y = p.Position().Y + par.pospix.Y + par.marginSizes.Top + par.borderSizes.Top + par.paddingSizes.Top
  590. // Otherwise its coordinates are relative to the parent outer coordinates.
  591. } else {
  592. p.pospix.X = p.Position().X + par.pospix.X
  593. p.pospix.Y = p.Position().Y + par.pospix.Y
  594. }
  595. // Maximum x,y coordinates for this panel
  596. p.xmin = p.pospix.X
  597. p.ymin = p.pospix.Y
  598. p.xmax = p.pospix.X + p.width
  599. p.ymax = p.pospix.Y + p.height
  600. if p.bounded {
  601. // Get the parent content area minimum and maximum absolute coordinates in pixels
  602. pxmin := par.pospix.X + par.marginSizes.Left + par.borderSizes.Left + par.paddingSizes.Left
  603. if pxmin < par.xmin {
  604. pxmin = par.xmin
  605. }
  606. pymin := par.pospix.Y + par.marginSizes.Top + par.borderSizes.Top + par.paddingSizes.Top
  607. if pymin < par.ymin {
  608. pymin = par.ymin
  609. }
  610. pxmax := par.pospix.X + par.width - (par.marginSizes.Right + par.borderSizes.Right + par.paddingSizes.Right)
  611. if pxmax > par.xmax {
  612. pxmax = par.xmax
  613. }
  614. pymax := par.pospix.Y + par.height - (par.marginSizes.Bottom + par.borderSizes.Bottom + par.paddingSizes.Bottom)
  615. if pymax > par.ymax {
  616. pymax = par.ymax
  617. }
  618. // Update this panel minimum x and y coordinates.
  619. if p.xmin < pxmin {
  620. p.xmin = pxmin
  621. }
  622. if p.ymin < pymin {
  623. p.ymin = pymin
  624. }
  625. // Update this panel maximum x and y coordinates.
  626. if p.xmax > pxmax {
  627. p.xmax = pxmax
  628. }
  629. if p.ymax > pymax {
  630. p.ymax = pymax
  631. }
  632. }
  633. // Set default values for bounds in texture coordinates
  634. xmintex := float32(0.0)
  635. ymintex := float32(0.0)
  636. xmaxtex := float32(1.0)
  637. ymaxtex := float32(1.0)
  638. // If this panel is bounded to its parent, calculates the bounds
  639. // for clipping in texture coordinates
  640. if p.bounded {
  641. if p.pospix.X < p.xmin {
  642. xmintex = (p.xmin - p.pospix.X) / p.width
  643. }
  644. if p.pospix.Y < p.ymin {
  645. ymintex = (p.ymin - p.pospix.Y) / p.height
  646. }
  647. if p.pospix.X+p.width > p.xmax {
  648. xmaxtex = (p.xmax - p.pospix.X) / p.width
  649. }
  650. if p.pospix.Y+p.height > p.ymax {
  651. ymaxtex = (p.ymax - p.pospix.Y) / p.height
  652. }
  653. }
  654. // Sets bounds uniform
  655. p.udata.bounds = math32.Vector4{xmintex, ymintex, xmaxtex, ymaxtex}
  656. }
  657. // calcWidth calculates the panel external width in pixels
  658. func (p *Panel) calcWidth() float32 {
  659. return p.content.Width +
  660. p.paddingSizes.Left + p.paddingSizes.Right +
  661. p.borderSizes.Left + p.borderSizes.Right +
  662. p.marginSizes.Left + p.marginSizes.Right
  663. }
  664. // calcHeight calculates the panel external height in pixels
  665. func (p *Panel) calcHeight() float32 {
  666. return p.content.Height +
  667. p.paddingSizes.Top + p.paddingSizes.Bottom +
  668. p.borderSizes.Top + p.borderSizes.Bottom +
  669. p.marginSizes.Top + p.marginSizes.Bottom
  670. }
  671. // resize tries to set the external size of the panel to the specified
  672. // dimensions and recalculates the size and positions of the internal areas.
  673. // The margins, borders and padding sizes are kept and the content
  674. // area size is adjusted. So if the panel is decreased, its minimum
  675. // size is determined by the margins, borders and paddings.
  676. // Normally it should be called with dispatch=true to recalculate the
  677. // panel layout and dispatch OnSize event.
  678. func (p *Panel) resize(width, height float32, dispatch bool) {
  679. var padding Rect
  680. var border Rect
  681. width = math32.Round(width)
  682. height = math32.Round(height)
  683. // Adjusts content width
  684. p.content.Width = width -
  685. p.marginSizes.Left - p.marginSizes.Right -
  686. p.borderSizes.Left - p.borderSizes.Right -
  687. p.paddingSizes.Left - p.paddingSizes.Right
  688. if p.content.Width < 0 {
  689. p.content.Width = 0
  690. }
  691. // Adjust other area widths
  692. padding.Width = p.paddingSizes.Left + p.content.Width + p.paddingSizes.Right
  693. border.Width = p.borderSizes.Left + padding.Width + p.borderSizes.Right
  694. // Adjusts content height
  695. p.content.Height = height -
  696. p.marginSizes.Top - p.marginSizes.Bottom -
  697. p.borderSizes.Top - p.borderSizes.Bottom -
  698. p.paddingSizes.Top - p.paddingSizes.Bottom
  699. if p.content.Height < 0 {
  700. p.content.Height = 0
  701. }
  702. // Adjust other area heights
  703. padding.Height = p.paddingSizes.Top + p.content.Height + p.paddingSizes.Bottom
  704. border.Height = p.borderSizes.Top + padding.Height + p.borderSizes.Bottom
  705. // Sets area positions
  706. border.X = p.marginSizes.Left
  707. border.Y = p.marginSizes.Top
  708. padding.X = border.X + p.borderSizes.Left
  709. padding.Y = border.Y + p.borderSizes.Top
  710. p.content.X = padding.X + p.paddingSizes.Left
  711. p.content.Y = padding.Y + p.paddingSizes.Top
  712. // Sets final panel dimensions (may be different from requested dimensions)
  713. p.width = p.marginSizes.Left + border.Width + p.marginSizes.Right
  714. p.height = p.marginSizes.Top + border.Height + p.marginSizes.Bottom
  715. // Updates border uniform in texture coordinates (0,0 -> 1,1)
  716. p.udata.borders = math32.Vector4{
  717. float32(border.X) / float32(p.width),
  718. float32(border.Y) / float32(p.height),
  719. float32(border.Width) / float32(p.width),
  720. float32(border.Height) / float32(p.height),
  721. }
  722. // Updates padding uniform in texture coordinates (0,0 -> 1,1)
  723. p.udata.paddings = math32.Vector4{
  724. float32(padding.X) / float32(p.width),
  725. float32(padding.Y) / float32(p.height),
  726. float32(padding.Width) / float32(p.width),
  727. float32(padding.Height) / float32(p.height),
  728. }
  729. // Updates content uniform in texture coordinates (0,0 -> 1,1)
  730. p.udata.content = math32.Vector4{
  731. float32(p.content.X) / float32(p.width),
  732. float32(p.content.Y) / float32(p.height),
  733. float32(p.content.Width) / float32(p.width),
  734. float32(p.content.Height) / float32(p.height),
  735. }
  736. p.SetChanged(true)
  737. // Update layout and dispatch event
  738. if !dispatch {
  739. return
  740. }
  741. if p.layout != nil {
  742. p.layout.Recalc(p)
  743. }
  744. p.Dispatch(OnResize, nil)
  745. }
  746. // RenderSetup is called by the Engine before drawing the object
  747. func (p *Panel) RenderSetup(gl *gls.GLS, rinfo *core.RenderInfo) {
  748. // Sets texture valid flag in uniforms
  749. // depending if the material has texture
  750. if p.mat.TextureCount() > 0 {
  751. p.udata.textureValid = 1
  752. } else {
  753. p.udata.textureValid = 0
  754. }
  755. // Sets model matrix
  756. var mm math32.Matrix4
  757. p.SetModelMatrix(gl, &mm)
  758. // Transfer model matrix uniform
  759. location := p.uniMatrix.Location(gl)
  760. gl.UniformMatrix4fv(location, 1, false, &mm[0])
  761. // Transfer panel parameters combined uniform
  762. location = p.uniPanel.Location(gl)
  763. const vec4count = 8
  764. gl.Uniform4fvUP(location, vec4count, unsafe.Pointer(&p.udata))
  765. }
  766. // SetModelMatrix calculates and sets the specified matrix with the model matrix for this panel
  767. func (p *Panel) SetModelMatrix(gl *gls.GLS, mm *math32.Matrix4) {
  768. // Get the current viewport width and height
  769. _, _, width, height := gl.GetViewport()
  770. fwidth := float32(width)
  771. fheight := float32(height)
  772. // Scale the quad for the viewport so it has fixed dimensions in pixels.
  773. p.wclip = 2 * float32(p.width) / fwidth
  774. p.hclip = 2 * float32(p.height) / fheight
  775. var scale math32.Vector3
  776. scale.Set(p.wclip, p.hclip, 1)
  777. // Convert absolute position in pixel coordinates from the top/left to
  778. // standard OpenGL clip coordinates of the quad center
  779. p.posclip.X = (p.pospix.X - fwidth/2) / (fwidth / 2)
  780. p.posclip.Y = -(p.pospix.Y - fheight/2) / (fheight / 2)
  781. p.posclip.Z = p.Position().Z
  782. // Calculates the model matrix
  783. var quat math32.Quaternion
  784. quat.SetIdentity()
  785. mm.Compose(&p.posclip, &quat, &scale)
  786. }