panel.go 29 KB

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