panel.go 25 KB

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