panel.go 28 KB

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