panel.go 24 KB

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