panel.go 24 KB

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