panel.go 29 KB

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