panel.go 28 KB

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