panel.go 26 KB

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