panel.go 29 KB

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