panel.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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. // Remove panel and if found appends to the end
  163. found := p.Remove(ipan)
  164. if found {
  165. p.Add(ipan)
  166. }
  167. }
  168. // SetTopChild sets this panel to be on the foreground
  169. // in relation to all its siblings.
  170. func (p *Panel) SetForeground() {
  171. // internal function to calculate the total minimum Z
  172. // for a panel hierarchy
  173. var getTopZ func(*Panel) float32
  174. getTopZ = func(pan *Panel) float32 {
  175. topZ := pan.Position().Z
  176. for _, iobj := range pan.Children() {
  177. child := iobj.(IPanel).GetPanel()
  178. cz := pan.Position().Z + getTopZ(child)
  179. if cz < topZ {
  180. topZ = cz
  181. }
  182. }
  183. return topZ
  184. }
  185. // Find the child of this panel with the minimum Z
  186. par := p.Parent().(IPanel).GetPanel()
  187. topZ := float32(10)
  188. for _, iobj := range par.Children() {
  189. child := iobj.(IPanel).GetPanel()
  190. cz := getTopZ(child)
  191. if cz < topZ {
  192. topZ = cz
  193. }
  194. }
  195. if p.Position().Z > topZ {
  196. p.SetPositionZ(topZ + deltaZ)
  197. }
  198. }
  199. // SetPosition sets this panel absolute position in pixel coordinates
  200. // from left to right and from top to bottom of the screen.
  201. func (p *Panel) SetPosition(x, y float32) {
  202. p.Node.SetPositionX(math32.Round(x))
  203. p.Node.SetPositionY(math32.Round(y))
  204. }
  205. // SetSize sets this panel external width and height in pixels.
  206. func (p *Panel) SetSize(width, height float32) {
  207. if width < 0 {
  208. log.Warn("Invalid panel width:%v", width)
  209. width = 0
  210. }
  211. if height < 0 {
  212. log.Warn("Invalid panel height:%v", height)
  213. height = 0
  214. }
  215. p.resize(width, height)
  216. }
  217. // SetWidth sets this panel external width in pixels.
  218. // The internal panel areas and positions are recalculated
  219. func (p *Panel) SetWidth(width float32) {
  220. p.SetSize(width, p.height)
  221. }
  222. // SetHeight sets this panel external height in pixels.
  223. // The internal panel areas and positions are recalculated
  224. func (p *Panel) SetHeight(height float32) {
  225. p.SetSize(p.width, height)
  226. }
  227. // SetContentAspectWidth sets the width of the content area of the panel
  228. // to the specified value and adjusts its height to keep the same aspect radio.
  229. func (p *Panel) SetContentAspectWidth(width float32) {
  230. aspect := p.content.Width / p.content.Height
  231. height := width / aspect
  232. p.SetContentSize(width, height)
  233. }
  234. // SetContentAspectHeight sets the height of the content area of the panel
  235. // to the specified value and adjusts its width to keep the same aspect ratio.
  236. func (p *Panel) SetContentAspectHeight(height float32) {
  237. aspect := p.content.Width / p.content.Height
  238. width := height / aspect
  239. p.SetContentSize(width, height)
  240. }
  241. // Width returns the current panel external width in pixels
  242. func (p *Panel) Width() float32 {
  243. return p.width
  244. }
  245. // Height returns the current panel external height in pixels
  246. func (p *Panel) Height() float32 {
  247. return p.height
  248. }
  249. // ContentWidth returns the current width of the content area in pixels
  250. func (p *Panel) ContentWidth() float32 {
  251. return p.content.Width
  252. }
  253. // ContentHeight returns the current height of the content area in pixels
  254. func (p *Panel) ContentHeight() float32 {
  255. return p.content.Height
  256. }
  257. // SetMargins set this panel margin sizes in pixels
  258. // and recalculates the panel external size
  259. func (p *Panel) SetMargins(top, right, bottom, left float32) {
  260. p.marginSizes.Set(top, right, bottom, left)
  261. p.resize(p.calcWidth(), p.calcHeight())
  262. }
  263. // SetMarginsFrom sets this panel margins sizes from the specified
  264. // BorderSizes pointer and recalculates the panel external size
  265. func (p *Panel) SetMarginsFrom(src *BorderSizes) {
  266. p.marginSizes = *src
  267. p.resize(p.calcWidth(), p.calcHeight())
  268. }
  269. // Margins returns the current margin sizes in pixels
  270. func (p *Panel) Margins() BorderSizes {
  271. return p.marginSizes
  272. }
  273. // SetBorders sets this panel border sizes in pixels
  274. // and recalculates the panel external size
  275. func (p *Panel) SetBorders(top, right, bottom, left float32) {
  276. p.borderSizes.Set(top, right, bottom, left)
  277. p.resize(p.calcWidth(), p.calcHeight())
  278. }
  279. // SetBordersFrom sets this panel border sizes from the specified
  280. // BorderSizes pointer and recalculates the panel size
  281. func (p *Panel) SetBordersFrom(src *BorderSizes) {
  282. p.borderSizes = *src
  283. p.resize(p.calcWidth(), p.calcHeight())
  284. }
  285. // Borders returns this panel current border sizes
  286. func (p *Panel) Borders() BorderSizes {
  287. return p.borderSizes
  288. }
  289. // SetPaddings sets the panel padding sizes in pixels
  290. func (p *Panel) SetPaddings(top, right, bottom, left float32) {
  291. p.paddingSizes.Set(top, right, bottom, left)
  292. p.resize(p.calcWidth(), p.calcHeight())
  293. }
  294. // SetPaddingsFrom sets this panel padding sizes from the specified
  295. // BorderSizes pointer and recalculates the panel size
  296. func (p *Panel) SetPaddingsFrom(src *BorderSizes) {
  297. p.paddingSizes = *src
  298. p.resize(p.calcWidth(), p.calcHeight())
  299. }
  300. // Paddings returns this panel padding sizes in pixels
  301. func (p *Panel) Paddings() BorderSizes {
  302. return p.paddingSizes
  303. }
  304. // SetBordersColor sets the color of this panel borders
  305. // The borders opacity is set to 1.0 (full opaque)
  306. func (p *Panel) SetBordersColor(color *math32.Color) {
  307. p.borderColorUni.Set(color.R, color.G, color.B, 1)
  308. }
  309. // SetBordersColor4 sets the color and opacity of this panel borders
  310. func (p *Panel) SetBordersColor4(color *math32.Color4) {
  311. p.borderColorUni.Set(color.R, color.G, color.B, color.A)
  312. }
  313. // BorderColor4 returns current border color
  314. func (p *Panel) BordersColor4() math32.Color4 {
  315. var color math32.Color4
  316. p.borderColorUni.SetColor4(&color)
  317. return color
  318. }
  319. // SetPaddingsColor sets the color of this panel paddings.
  320. func (p *Panel) SetPaddingsColor(color *math32.Color) {
  321. p.paddingColorUni.Set(color.R, color.G, color.B, 1.0)
  322. }
  323. // SetColor sets the color of the panel paddings and content area
  324. func (p *Panel) SetColor(color *math32.Color) *Panel {
  325. p.paddingColorUni.Set(color.R, color.G, color.B, 1.0)
  326. p.contentColorUni.Set(color.R, color.G, color.B, 1.0)
  327. return p
  328. }
  329. // SetColor4 sets the color of the panel paddings and content area
  330. func (p *Panel) SetColor4(color *math32.Color4) *Panel {
  331. p.paddingColorUni.Set(color.R, color.G, color.B, color.A)
  332. p.contentColorUni.Set(color.R, color.G, color.B, color.A)
  333. return p
  334. }
  335. // Color4 returns the current color of the panel content area
  336. func (p *Panel) Color4() math32.Color4 {
  337. return p.contentColorUni.GetColor4()
  338. }
  339. // SetContentSize sets this panel content size to the specified dimensions.
  340. // The external size of the panel may increase or decrease to acomodate
  341. // the new content size.
  342. func (p *Panel) SetContentSize(width, height float32) {
  343. // Calculates the new desired external width and height
  344. eWidth := width +
  345. p.paddingSizes.Left + p.paddingSizes.Right +
  346. p.borderSizes.Left + p.borderSizes.Right +
  347. p.marginSizes.Left + p.marginSizes.Right
  348. eHeight := height +
  349. p.paddingSizes.Top + p.paddingSizes.Bottom +
  350. p.borderSizes.Top + p.borderSizes.Bottom +
  351. p.marginSizes.Top + p.marginSizes.Bottom
  352. p.resize(eWidth, eHeight)
  353. }
  354. // SetContentWidth sets this panel content width 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) SetContentWidth(width float32) {
  357. p.SetContentSize(width, p.content.Height)
  358. }
  359. // SetContentHeight sets this panel content height to the specified dimension in pixels.
  360. // The external size of the panel may increase or decrease to acomodate the new width
  361. func (p *Panel) SetContentHeight(height float32) {
  362. p.SetContentSize(p.content.Width, height)
  363. }
  364. // MinWidth returns the minimum width of this panel (ContentWidth = 0)
  365. func (p *Panel) MinWidth() float32 {
  366. return p.paddingSizes.Left + p.paddingSizes.Right +
  367. p.borderSizes.Left + p.borderSizes.Right +
  368. p.marginSizes.Left + p.marginSizes.Right
  369. }
  370. // MinHeight returns the minimum height of this panel (ContentHeight = 0)
  371. func (p *Panel) MinHeight() float32 {
  372. return p.paddingSizes.Top + p.paddingSizes.Bottom +
  373. p.borderSizes.Top + p.borderSizes.Bottom +
  374. p.marginSizes.Top + p.marginSizes.Bottom
  375. }
  376. // Add adds a child panel to this one
  377. func (p *Panel) Add(ichild IPanel) *Panel {
  378. p.Node.Add(ichild)
  379. node := ichild.GetPanel()
  380. node.SetParent(p)
  381. //node.SetPositionZ(p.nextChildZ)
  382. //p.nextChildZ += deltaZ
  383. if p.layout != nil {
  384. p.layout.Recalc(p)
  385. }
  386. p.Dispatch(OnChild, nil)
  387. return p
  388. }
  389. // Remove removes the specified child from this panel
  390. func (p *Panel) Remove(ichild IPanel) bool {
  391. res := p.Node.Remove(ichild)
  392. if res {
  393. if p.layout != nil {
  394. p.layout.Recalc(p)
  395. }
  396. p.Dispatch(OnChild, nil)
  397. }
  398. return res
  399. }
  400. // Bounded returns this panel bounded state
  401. func (p *Panel) Bounded() bool {
  402. return p.bounded
  403. }
  404. // SetBounded sets this panel bounded state
  405. func (p *Panel) SetBounded(bounded bool) {
  406. p.bounded = bounded
  407. }
  408. // UpdateMatrixWorld overrides the standard Node version which is called by
  409. // the Engine before rendering the frame.
  410. func (p *Panel) UpdateMatrixWorld() {
  411. par := p.Parent()
  412. // Panel has no parent
  413. if par == nil {
  414. p.updateBounds(nil)
  415. p.setZ(0)
  416. // Panel has parent
  417. } else {
  418. par, ok := par.(*Panel)
  419. if ok {
  420. p.updateBounds(par)
  421. // Parent is not a panel
  422. } else {
  423. p.updateBounds(nil)
  424. }
  425. }
  426. // Update this panel children
  427. for _, ichild := range p.Children() {
  428. ichild.UpdateMatrixWorld()
  429. }
  430. }
  431. func (p *Panel) setZ(nextZ float32) float32 {
  432. //log.Error("setZ:%6.5f", nextZ)
  433. p.SetPositionZ(nextZ)
  434. nextZ += deltaZ
  435. for _, ichild := range p.Children() {
  436. nextZ = ichild.(IPanel).GetPanel().setZ(nextZ)
  437. }
  438. return nextZ
  439. }
  440. // ContainsPosition returns indication if this panel contains
  441. // the specified screen position in pixels.
  442. func (p *Panel) ContainsPosition(x, y float32) bool {
  443. if x < p.pospix.X || x >= (p.pospix.X+p.width) {
  444. return false
  445. }
  446. if y < p.pospix.Y || y >= (p.pospix.Y+p.height) {
  447. return false
  448. }
  449. return true
  450. }
  451. //// CancelMouse is called by the gui root panel when a mouse event occurs
  452. //// outside of this panel and the panel is not in focus.
  453. //func (p *Panel) CancelMouse(m *Root, mev MouseEvent) {
  454. //
  455. // if !p.enabled {
  456. // return
  457. // }
  458. // // If OnMouseEnter previously sent, sends OnMouseLeave
  459. // if p.cursorEnter {
  460. // p.DispatchPrefix(OnMouseLeave, &mev)
  461. // p.cursorEnter = false
  462. // }
  463. // // If click outside the panel
  464. // if mev.Button >= 0 {
  465. // if mev.Action == core.ActionPress {
  466. // p.DispatchPrefix(OnMouseButtonOut, &mev)
  467. // }
  468. // }
  469. //}
  470. // SetEnabled sets the panel enabled state
  471. // A disabled panel do not process key or mouse events.
  472. func (p *Panel) SetEnabled(state bool) {
  473. p.enabled = state
  474. p.Dispatch(OnEnable, nil)
  475. }
  476. // Enabled returns the current enabled state of this panel
  477. func (p *Panel) Enabled() bool {
  478. return p.enabled
  479. }
  480. // SetLayout sets the layout to use to position the children of this panel
  481. // To remove the layout, call this function passing nil as parameter.
  482. func (p *Panel) SetLayout(ilayout ILayout) {
  483. p.layout = ilayout
  484. if p.layout != nil {
  485. p.layout.Recalc(p)
  486. }
  487. }
  488. // SetLayoutParams sets the layout parameters for this panel
  489. func (p *Panel) SetLayoutParams(params interface{}) {
  490. p.layoutParams = params
  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 = p.pospix.X
  498. p.ymin = p.pospix.Y
  499. p.xmax = p.pospix.X + p.width
  500. p.ymax = p.pospix.Y + p.height
  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 + float32(par.marginSizes.Left+par.borderSizes.Left+par.paddingSizes.Left)
  508. p.pospix.Y = p.Position().Y + par.pospix.Y + float32(par.marginSizes.Top+par.borderSizes.Top+par.paddingSizes.Top)
  509. p.pospix.Z = p.Position().Z + par.pospix.Z
  510. // Otherwise its coordinates are relative to the parent outer coordinates.
  511. } else {
  512. p.pospix.X = p.Position().X + par.pospix.X
  513. p.pospix.Y = p.Position().Y + par.pospix.Y
  514. p.pospix.Z = p.Position().Z + par.pospix.Z
  515. }
  516. // Maximum x,y coordinates for this panel
  517. p.xmin = p.pospix.X
  518. p.ymin = p.pospix.Y
  519. p.xmax = p.pospix.X + p.width
  520. p.ymax = p.pospix.Y + p.height
  521. if p.bounded {
  522. // Get the parent minimum X and Y absolute coordinates in pixels
  523. pxmin := par.xmin + par.marginSizes.Right + par.borderSizes.Right + par.paddingSizes.Right
  524. pymin := par.ymin + par.marginSizes.Bottom + par.borderSizes.Bottom + par.paddingSizes.Bottom
  525. // Get the parent maximum X and Y absolute coordinates in pixels
  526. pxmax := par.xmax - (par.marginSizes.Right + par.borderSizes.Right + par.paddingSizes.Right)
  527. pymax := par.ymax - (par.marginSizes.Bottom + par.borderSizes.Bottom + par.paddingSizes.Bottom)
  528. // Update this panel minimum x and y coordinates.
  529. if p.xmin < pxmin {
  530. p.xmin = pxmin
  531. }
  532. if p.ymin < pymin {
  533. p.ymin = pymin
  534. }
  535. // Update this panel maximum x and y coordinates.
  536. if p.xmax > pxmax {
  537. p.xmax = pxmax
  538. }
  539. if p.ymax > pymax {
  540. p.ymax = pymax
  541. }
  542. }
  543. // Set default values for bounds in texture coordinates
  544. xmintex := float32(0.0)
  545. ymintex := float32(0.0)
  546. xmaxtex := float32(1.0)
  547. ymaxtex := float32(1.0)
  548. // If this panel is bounded to its parent, calculates the bounds
  549. // for clipping in texture coordinates
  550. if p.bounded {
  551. if p.pospix.X < p.xmin {
  552. xmintex = (p.xmin - p.pospix.X) / p.width
  553. }
  554. if p.pospix.Y < p.ymin {
  555. ymintex = (p.ymin - p.pospix.Y) / p.height
  556. }
  557. if p.pospix.X+p.width > p.xmax {
  558. xmaxtex = (p.xmax - p.pospix.X) / p.width
  559. }
  560. if p.pospix.Y+p.height > p.ymax {
  561. ymaxtex = (p.ymax - p.pospix.Y) / p.height
  562. }
  563. }
  564. // Sets bounds uniform
  565. p.boundsUni.Set(xmintex, ymintex, xmaxtex, ymaxtex)
  566. }
  567. // calcWidth calculates the panel external width in pixels
  568. func (p *Panel) calcWidth() float32 {
  569. return p.content.Width +
  570. p.paddingSizes.Left + p.paddingSizes.Right +
  571. p.borderSizes.Left + p.borderSizes.Right +
  572. p.marginSizes.Left + p.marginSizes.Right
  573. }
  574. // calcHeight calculates the panel external height in pixels
  575. func (p *Panel) calcHeight() float32 {
  576. return p.content.Height +
  577. p.paddingSizes.Top + p.paddingSizes.Bottom +
  578. p.borderSizes.Top + p.borderSizes.Bottom +
  579. p.marginSizes.Top + p.marginSizes.Bottom
  580. }
  581. // resize tries to set the external size of the panel to the specified
  582. // dimensions and recalculates the size and positions of the internal areas.
  583. // The margins, borders and padding sizes are kept and the content
  584. // area size is adjusted. So if the panel is decreased, its minimum
  585. // size is determined by the margins, borders and paddings.
  586. func (p *Panel) resize(width, height float32) {
  587. var padding Rect
  588. var border Rect
  589. width = math32.Round(width)
  590. height = math32.Round(height)
  591. // Adjusts content width
  592. p.content.Width = width -
  593. p.marginSizes.Left - p.marginSizes.Right -
  594. p.borderSizes.Left - p.borderSizes.Right -
  595. p.paddingSizes.Left - p.paddingSizes.Right
  596. if p.content.Width < 0 {
  597. p.content.Width = 0
  598. }
  599. // Adjust other area widths
  600. padding.Width = p.paddingSizes.Left + p.content.Width + p.paddingSizes.Right
  601. border.Width = p.borderSizes.Left + padding.Width + p.borderSizes.Right
  602. // Adjusts content height
  603. p.content.Height = height -
  604. p.marginSizes.Top - p.marginSizes.Bottom -
  605. p.borderSizes.Top - p.borderSizes.Bottom -
  606. p.paddingSizes.Top - p.paddingSizes.Bottom
  607. if p.content.Height < 0 {
  608. p.content.Height = 0
  609. }
  610. // Adjust other area heights
  611. padding.Height = p.paddingSizes.Top + p.content.Height + p.paddingSizes.Bottom
  612. border.Height = p.borderSizes.Top + padding.Height + p.borderSizes.Bottom
  613. // Sets area positions
  614. border.X = p.marginSizes.Left
  615. border.Y = p.marginSizes.Top
  616. padding.X = border.X + p.borderSizes.Left
  617. padding.Y = border.Y + p.borderSizes.Top
  618. p.content.X = padding.X + p.paddingSizes.Left
  619. p.content.Y = padding.Y + p.paddingSizes.Top
  620. // Sets final panel dimensions (may be different from requested dimensions)
  621. p.width = p.marginSizes.Left + border.Width + p.marginSizes.Right
  622. p.height = p.marginSizes.Top + border.Height + p.marginSizes.Bottom
  623. // Updates border uniform in texture coordinates (0,0 -> 1,1)
  624. p.borderUni.Set(
  625. float32(border.X)/float32(p.width),
  626. float32(border.Y)/float32(p.height),
  627. float32(border.Width)/float32(p.width),
  628. float32(border.Height)/float32(p.height),
  629. )
  630. // Updates padding uniform in texture coordinates (0,0 -> 1,1)
  631. p.paddingUni.Set(
  632. float32(padding.X)/float32(p.width),
  633. float32(padding.Y)/float32(p.height),
  634. float32(padding.Width)/float32(p.width),
  635. float32(padding.Height)/float32(p.height),
  636. )
  637. // Updates content uniform in texture coordinates (0,0 -> 1,1)
  638. p.contentUni.Set(
  639. float32(p.content.X)/float32(p.width),
  640. float32(p.content.Y)/float32(p.height),
  641. float32(p.content.Width)/float32(p.width),
  642. float32(p.content.Height)/float32(p.height),
  643. )
  644. // Update layout and dispatch event
  645. if p.layout != nil {
  646. p.layout.Recalc(p)
  647. }
  648. p.Dispatch(OnResize, nil)
  649. }
  650. // RenderSetup is called by the Engine before drawing the object
  651. func (p *Panel) RenderSetup(gl *gls.GLS, rinfo *core.RenderInfo) {
  652. // Sets model matrix
  653. var mm math32.Matrix4
  654. p.SetModelMatrix(gl, &mm)
  655. p.modelMatrixUni.SetMatrix4(&mm)
  656. // Transfer uniforms
  657. p.borderColorUni.Transfer(gl)
  658. p.paddingColorUni.Transfer(gl)
  659. p.contentColorUni.Transfer(gl)
  660. p.boundsUni.Transfer(gl)
  661. p.borderUni.Transfer(gl)
  662. p.paddingUni.Transfer(gl)
  663. p.contentUni.Transfer(gl)
  664. p.modelMatrixUni.Transfer(gl)
  665. }
  666. // SetModelMatrix calculates and sets the specified matrix with the model matrix for this panel
  667. func (p *Panel) SetModelMatrix(gl *gls.GLS, mm *math32.Matrix4) {
  668. // Get the current viewport width and height
  669. _, _, width, height := gl.GetViewport()
  670. fwidth := float32(width)
  671. fheight := float32(height)
  672. // Scale the quad for the viewport so it has fixed dimensions in pixels.
  673. fw := float32(p.width) / fwidth
  674. fh := float32(p.height) / fheight
  675. var scale math32.Vector3
  676. scale.Set(2*fw, 2*fh, 1)
  677. // Convert absolute position in pixel coordinates from the top/left to
  678. // standard OpenGL clip coordinates of the quad center
  679. var posclip math32.Vector3
  680. posclip.X = (p.pospix.X - fwidth/2) / (fwidth / 2)
  681. posclip.Y = -(p.pospix.Y - fheight/2) / (fheight / 2)
  682. posclip.Z = p.pospix.Z
  683. //log.Debug("panel posclip:%v\n", posclip)
  684. // Calculates the model matrix
  685. var quat math32.Quaternion
  686. quat.SetIdentity()
  687. mm.Compose(&posclip, &quat, &scale)
  688. }