panel.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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. xmin float32 // minimum absolute x this panel can use
  72. xmax float32 // maximum absolute x this panel can use
  73. ymin float32 // minimum absolute y this panel can use
  74. ymax float32 // maximum absolute y this panel can use
  75. // Uniforms sent to shader
  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. // Quad geometry shared by ALL Panels
  105. var panelQuadGeometry *geometry.Geometry
  106. // NewPanel creates and returns a pointer to a new panel with the
  107. // specified dimensions in pixels and a default quad geometry
  108. func NewPanel(width, height float32) *Panel {
  109. p := new(Panel)
  110. p.Initialize(p, width, height)
  111. return p
  112. }
  113. // Initialize initializes this panel and is normally used by other types which embed a panel.
  114. func (p *Panel) Initialize(ipan IPanel, width, height float32) { // TODO rename to Init
  115. p.width = width
  116. p.height = height
  117. // If first time, create panel quad geometry
  118. if panelQuadGeometry == nil {
  119. // Builds array with vertex positions and texture coordinates
  120. positions := math32.NewArrayF32(0, 20)
  121. positions.Append(
  122. 0, 0, 0, 0, 1,
  123. 0, -1, 0, 0, 0,
  124. 1, -1, 0, 1, 0,
  125. 1, 0, 0, 1, 1,
  126. )
  127. // Builds array of indices
  128. indices := math32.NewArrayU32(0, 6)
  129. indices.Append(0, 1, 2, 0, 2, 3)
  130. // Creates geometry
  131. geom := geometry.NewGeometry()
  132. geom.SetIndices(indices)
  133. geom.AddVBO(gls.NewVBO(positions).
  134. AddAttrib(gls.VertexPosition).
  135. AddAttrib(gls.VertexTexcoord),
  136. )
  137. panelQuadGeometry = geom
  138. }
  139. // Initialize material
  140. p.mat = material.NewMaterial()
  141. p.mat.SetUseLights(material.UseLightNone)
  142. p.mat.SetShader("panel")
  143. p.mat.SetShaderUnique(true)
  144. // For now set all panels as transparent by default
  145. // This means they are all rendered back to front, after and on top of everything else
  146. // TODO if we know a panel is opaque, setting it as such will improve rendering performance
  147. p.mat.SetTransparent(true)
  148. // Initialize graphic
  149. p.Graphic = graphic.NewGraphic(ipan, panelQuadGeometry.Incref(), gls.TRIANGLES)
  150. p.AddMaterial(p, p.mat, 0, 0)
  151. // Initialize uniforms location caches
  152. p.uniMatrix.Init("ModelMatrix")
  153. p.uniPanel.Init("Panel")
  154. // Set defaults
  155. p.udata.bordersColor = math32.Color4{0, 0, 0, 1}
  156. p.bounded = true
  157. p.enabled = true
  158. p.resize(width, height, true)
  159. }
  160. // InitializeGraphic initializes this panel with a different graphic
  161. func (p *Panel) InitializeGraphic(width, height float32, gr *graphic.Graphic) {
  162. p.Graphic = gr
  163. p.width = width
  164. p.height = height
  165. // Initializes uniforms location caches
  166. p.uniMatrix.Init("ModelMatrix")
  167. p.uniPanel.Init("Panel")
  168. // Set defaults
  169. p.udata.bordersColor = math32.Color4{0, 0, 0, 1}
  170. p.bounded = true
  171. p.enabled = true
  172. p.resize(width, height, true)
  173. }
  174. // GetPanel satisfies the IPanel interface and
  175. // returns pointer to this panel
  176. func (p *Panel) GetPanel() *Panel {
  177. return p
  178. }
  179. // Material returns a pointer for this panel's Material
  180. func (p *Panel) Material() *material.Material { // TODO remove - allow for setting and getting a single texture
  181. return p.mat
  182. }
  183. // SetTopChild sets the Z coordinate of the specified panel to
  184. // be on top of all other children of this panel.
  185. // The function does not check if the specified panel is a
  186. // child of this one.
  187. func (p *Panel) SetTopChild(ipan IPanel) {
  188. // Remove panel and if found appends to the end
  189. found := p.Remove(ipan)
  190. if found {
  191. p.Add(ipan)
  192. p.SetChanged(true)
  193. }
  194. }
  195. // SetZLayerDelta sets the Z-layer of the panel relative to its parent.
  196. func (p *Panel) SetZLayerDelta(zLayerDelta int) {
  197. p.zLayerDelta = zLayerDelta
  198. }
  199. // ZLayerDelta returns the Z-layer of the panel relative to its parent.
  200. func (p *Panel) ZLayerDelta() int {
  201. return p.zLayerDelta
  202. }
  203. // SetPosition sets this panel absolute position in pixel coordinates
  204. // from left to right and from top to bottom of the screen.
  205. func (p *Panel) SetPosition(x, y float32) {
  206. p.Node.SetPositionX(math32.Round(x))
  207. p.Node.SetPositionY(math32.Round(y))
  208. }
  209. // SetSize sets this panel external width and height in pixels.
  210. func (p *Panel) SetSize(width, height float32) {
  211. if width < 0 {
  212. log.Warn("Invalid panel width:%v", width)
  213. width = 0
  214. }
  215. if height < 0 {
  216. log.Warn("Invalid panel height:%v", height)
  217. height = 0
  218. }
  219. p.resize(width, height, true)
  220. }
  221. // SetWidth sets this panel external width in pixels.
  222. // The internal panel areas and positions are recalculated
  223. func (p *Panel) SetWidth(width float32) {
  224. p.SetSize(width, p.height)
  225. }
  226. // SetHeight sets this panel external height in pixels.
  227. // The internal panel areas and positions are recalculated
  228. func (p *Panel) SetHeight(height float32) {
  229. p.SetSize(p.width, height)
  230. }
  231. // SetContentAspectWidth sets the width of the content area of the panel
  232. // to the specified value and adjusts its height to keep the same aspect radio.
  233. func (p *Panel) SetContentAspectWidth(width float32) {
  234. aspect := p.content.Width / p.content.Height
  235. height := width / aspect
  236. p.SetContentSize(width, height)
  237. }
  238. // SetContentAspectHeight sets the height of the content area of the panel
  239. // to the specified value and adjusts its width to keep the same aspect ratio.
  240. func (p *Panel) SetContentAspectHeight(height float32) {
  241. aspect := p.content.Width / p.content.Height
  242. width := height / aspect
  243. p.SetContentSize(width, height)
  244. }
  245. // Size returns this panel current external width and height in pixels
  246. func (p *Panel) Size() (float32, float32) {
  247. return p.width, p.height
  248. }
  249. // Width returns the current panel external width in pixels
  250. func (p *Panel) Width() float32 {
  251. return p.width
  252. }
  253. // Height returns the current panel external height in pixels
  254. func (p *Panel) Height() float32 {
  255. return p.height
  256. }
  257. // ContentWidth returns the current width of the content area in pixels
  258. func (p *Panel) ContentWidth() float32 {
  259. return p.content.Width
  260. }
  261. // ContentHeight returns the current height of the content area in pixels
  262. func (p *Panel) ContentHeight() float32 {
  263. return p.content.Height
  264. }
  265. // SetMargins set this panel margin sizes in pixels
  266. // and recalculates the panel external size
  267. func (p *Panel) SetMargins(top, right, bottom, left float32) {
  268. p.marginSizes.Set(top, right, bottom, left)
  269. p.resize(p.calcWidth(), p.calcHeight(), true)
  270. }
  271. // SetMarginsFrom sets this panel margins sizes from the specified
  272. // RectBounds pointer and recalculates the panel external size
  273. func (p *Panel) SetMarginsFrom(src *RectBounds) {
  274. p.marginSizes = *src
  275. p.resize(p.calcWidth(), p.calcHeight(), true)
  276. }
  277. // Margins returns the current margin sizes in pixels
  278. func (p *Panel) Margins() RectBounds {
  279. return p.marginSizes
  280. }
  281. // SetBorders sets this panel border sizes in pixels
  282. // and recalculates the panel external size
  283. func (p *Panel) SetBorders(top, right, bottom, left float32) {
  284. p.borderSizes.Set(top, right, bottom, left)
  285. p.resize(p.calcWidth(), p.calcHeight(), true)
  286. }
  287. // SetBordersFrom sets this panel border sizes from the specified
  288. // RectBounds pointer and recalculates the panel size
  289. func (p *Panel) SetBordersFrom(src *RectBounds) {
  290. p.borderSizes = *src
  291. p.resize(p.calcWidth(), p.calcHeight(), true)
  292. }
  293. // Borders returns this panel current border sizes
  294. func (p *Panel) Borders() RectBounds {
  295. return p.borderSizes
  296. }
  297. // SetPaddings sets the panel padding sizes in pixels
  298. func (p *Panel) SetPaddings(top, right, bottom, left float32) {
  299. p.paddingSizes.Set(top, right, bottom, left)
  300. p.resize(p.calcWidth(), p.calcHeight(), true)
  301. }
  302. // SetPaddingsFrom sets this panel padding sizes from the specified
  303. // RectBounds pointer and recalculates the panel size
  304. func (p *Panel) SetPaddingsFrom(src *RectBounds) {
  305. p.paddingSizes = *src
  306. p.resize(p.calcWidth(), p.calcHeight(), true)
  307. }
  308. // Paddings returns this panel padding sizes in pixels
  309. func (p *Panel) Paddings() RectBounds {
  310. return p.paddingSizes
  311. }
  312. // SetBordersColor sets the color of this panel borders
  313. // The borders opacity is set to 1.0 (full opaque)
  314. func (p *Panel) SetBordersColor(color *math32.Color) {
  315. p.udata.bordersColor = math32.Color4{color.R, color.G, color.B, 1}
  316. p.SetChanged(true)
  317. }
  318. // SetBordersColor4 sets the color and opacity of this panel borders
  319. func (p *Panel) SetBordersColor4(color *math32.Color4) {
  320. p.udata.bordersColor = *color
  321. p.SetChanged(true)
  322. }
  323. // BordersColor4 returns current border color
  324. func (p *Panel) BordersColor4() math32.Color4 {
  325. return p.udata.bordersColor
  326. }
  327. // SetPaddingsColor sets the color of this panel paddings.
  328. func (p *Panel) SetPaddingsColor(color *math32.Color) {
  329. p.udata.paddingsColor = math32.Color4{color.R, color.G, color.B, 1}
  330. p.SetChanged(true)
  331. }
  332. // SetColor sets the color of the panel paddings and content area
  333. func (p *Panel) SetColor(color *math32.Color) *Panel {
  334. p.udata.paddingsColor = math32.Color4{color.R, color.G, color.B, 1}
  335. p.udata.contentColor = p.udata.paddingsColor
  336. p.SetChanged(true)
  337. return p
  338. }
  339. // SetColor4 sets the color of the panel paddings and content area
  340. func (p *Panel) SetColor4(color *math32.Color4) *Panel {
  341. p.udata.paddingsColor = *color
  342. p.udata.contentColor = *color
  343. p.SetChanged(true)
  344. return p
  345. }
  346. // Color4 returns the current color of the panel content area
  347. func (p *Panel) Color4() math32.Color4 {
  348. return p.udata.contentColor
  349. }
  350. // ApplyStyle applies the provided PanelStyle to the panel
  351. func (p *Panel) ApplyStyle(ps *PanelStyle) {
  352. p.udata.bordersColor = ps.BorderColor
  353. p.udata.paddingsColor = ps.BgColor
  354. p.udata.contentColor = ps.BgColor
  355. p.marginSizes = ps.Margin
  356. p.borderSizes = ps.Border
  357. p.paddingSizes = ps.Padding
  358. p.resize(p.calcWidth(), p.calcHeight(), true)
  359. }
  360. // SetContentSize sets this panel content size to the specified dimensions.
  361. // The external size of the panel may increase or decrease to acomodate
  362. // the new content size.
  363. func (p *Panel) SetContentSize(width, height float32) {
  364. p.setContentSize(width, height, true)
  365. }
  366. // SetContentWidth sets this panel content width to the specified dimension in pixels.
  367. // The external size of the panel may increase or decrease to accommodate the new width
  368. func (p *Panel) SetContentWidth(width float32) {
  369. p.SetContentSize(width, p.content.Height)
  370. }
  371. // SetContentHeight sets this panel content height to the specified dimension in pixels.
  372. // The external size of the panel may increase or decrease to accommodate the new width
  373. func (p *Panel) SetContentHeight(height float32) {
  374. p.SetContentSize(p.content.Width, height)
  375. }
  376. // MinWidth returns the minimum width of this panel (ContentWidth = 0)
  377. func (p *Panel) MinWidth() float32 {
  378. return p.paddingSizes.Left + p.paddingSizes.Right +
  379. p.borderSizes.Left + p.borderSizes.Right +
  380. p.marginSizes.Left + p.marginSizes.Right
  381. }
  382. // MinHeight returns the minimum height of this panel (ContentHeight = 0)
  383. func (p *Panel) MinHeight() float32 {
  384. return p.paddingSizes.Top + p.paddingSizes.Bottom +
  385. p.borderSizes.Top + p.borderSizes.Bottom +
  386. p.marginSizes.Top + p.marginSizes.Bottom
  387. }
  388. // Pospix returns this panel absolute coordinate in pixels
  389. func (p *Panel) Pospix() math32.Vector3 {
  390. return p.pospix
  391. }
  392. // Add adds a child panel to this one
  393. // This overrides the Node method to enforce that IPanels can only have IPanels as children
  394. func (p *Panel) Add(ichild IPanel) *Panel {
  395. p.Node.Add(ichild)
  396. if p.layout != nil {
  397. p.layout.Recalc(p)
  398. }
  399. return p
  400. }
  401. // Remove removes the specified child from this panel
  402. func (p *Panel) Remove(ichild IPanel) bool {
  403. res := p.Node.Remove(ichild)
  404. if res {
  405. if p.layout != nil {
  406. p.layout.Recalc(p)
  407. }
  408. }
  409. return res
  410. }
  411. // Bounded returns this panel bounded state
  412. func (p *Panel) Bounded() bool {
  413. return p.bounded
  414. }
  415. // SetBounded sets this panel bounded state
  416. func (p *Panel) SetBounded(bounded bool) {
  417. p.bounded = bounded
  418. p.SetChanged(true)
  419. }
  420. // UpdateMatrixWorld overrides the standard core.Node version which is called by
  421. // the Engine before rendering the frame.
  422. func (p *Panel) UpdateMatrixWorld() {
  423. par := p.Parent()
  424. if par == nil {
  425. p.updateBounds(nil)
  426. // Panel has parent
  427. } else {
  428. parpan, ok := par.(IPanel)
  429. if ok {
  430. p.updateBounds(parpan.GetPanel())
  431. } else {
  432. p.updateBounds(nil)
  433. }
  434. }
  435. // Update this panel children
  436. for _, ichild := range p.Children() {
  437. ichild.UpdateMatrixWorld()
  438. }
  439. }
  440. // ContainsPosition returns indication if this panel contains
  441. // the specified screen position in pixels.
  442. func (p *Panel) ContainsPosition(x, y float32) bool {
  443. if x < p.pospix.X || y < p.pospix.Y || x >= (p.pospix.X+p.width) || y >= (p.pospix.Y+p.height) {
  444. return false
  445. }
  446. return true
  447. }
  448. // InsideBorders returns indication if the specified screen
  449. // position in pixels is inside the panel borders, including the borders width.
  450. // Unlike "ContainsPosition" is does not consider the panel margins.
  451. func (p *Panel) InsideBorders(x, y float32) bool {
  452. if x < (p.pospix.X+p.marginSizes.Left) || x >= (p.pospix.X+p.width-p.marginSizes.Right) ||
  453. y < (p.pospix.Y+p.marginSizes.Top) || y >= (p.pospix.Y+p.height-p.marginSizes.Bottom) {
  454. return false
  455. }
  456. return true
  457. }
  458. // Intersects returns if this panel intersects with the other panel
  459. func (p *Panel) Intersects(other *Panel) bool {
  460. if p.pospix.X+p.width <= other.pospix.X || other.pospix.X+other.width <= p.pospix.X ||
  461. p.pospix.Y+p.height <= other.pospix.Y || other.pospix.Y+other.height <= p.pospix.Y {
  462. return false
  463. }
  464. return true
  465. }
  466. // SetEnabled sets the panel enabled state
  467. // A disabled panel does not process events.
  468. func (p *Panel) SetEnabled(state bool) {
  469. p.enabled = state
  470. p.Dispatch(OnEnable, nil)
  471. }
  472. // Enabled returns the current enabled state of this panel
  473. func (p *Panel) Enabled() bool {
  474. return p.enabled
  475. }
  476. // SetLayout sets the layout to use to position the children of this panel
  477. // To remove the layout, call this function passing nil as parameter.
  478. func (p *Panel) SetLayout(ilayout ILayout) {
  479. p.layout = ilayout
  480. if p.layout != nil {
  481. p.layout.Recalc(p)
  482. }
  483. }
  484. // Layout returns this panel current layout
  485. func (p *Panel) Layout() ILayout {
  486. return p.layout
  487. }
  488. // SetLayoutParams sets the layout parameters for this panel
  489. func (p *Panel) SetLayoutParams(params interface{}) {
  490. p.layoutParams = params
  491. }
  492. // LayoutParams returns this panel current layout parameters
  493. func (p *Panel) LayoutParams() interface{} {
  494. return p.layoutParams
  495. }
  496. // ContentCoords converts the specified window absolute coordinates in pixels
  497. // (as informed by OnMouse event) to this panel internal content area pixel coordinates
  498. func (p *Panel) ContentCoords(wx, wy float32) (float32, float32) {
  499. cx := wx - p.pospix.X -
  500. p.paddingSizes.Left -
  501. p.borderSizes.Left -
  502. p.marginSizes.Left
  503. cy := wy - p.pospix.Y -
  504. p.paddingSizes.Top -
  505. p.borderSizes.Top -
  506. p.marginSizes.Top
  507. return cx, cy
  508. }
  509. // setContentSize is an internal version of SetContentSize() which allows
  510. // to determine if the panel will recalculate its layout and dispatch event.
  511. // It is normally used by layout managers when setting the panel content size
  512. // to avoid another invocation of the layout manager.
  513. func (p *Panel) setContentSize(width, height float32, dispatch bool) {
  514. // Calculates the new desired external width and height
  515. eWidth := width +
  516. p.paddingSizes.Left + p.paddingSizes.Right +
  517. p.borderSizes.Left + p.borderSizes.Right +
  518. p.marginSizes.Left + p.marginSizes.Right
  519. eHeight := height +
  520. p.paddingSizes.Top + p.paddingSizes.Bottom +
  521. p.borderSizes.Top + p.borderSizes.Bottom +
  522. p.marginSizes.Top + p.marginSizes.Bottom
  523. p.resize(eWidth, eHeight, dispatch)
  524. }
  525. // updateBounds is called by UpdateMatrixWorld() and calculates this panel
  526. // bounds considering the bounds of its parent
  527. func (p *Panel) updateBounds(par *Panel) {
  528. // If this panel has no parent, its pixel position is its Position
  529. if par == nil {
  530. p.pospix.X = p.Position().X
  531. p.pospix.Y = p.Position().Y
  532. // If this panel is bounded to its parent, its coordinates are relative
  533. // to the parent internal content rectangle.
  534. } else if p.bounded {
  535. p.pospix.X = p.Position().X + par.pospix.X + par.marginSizes.Left + par.borderSizes.Left + par.paddingSizes.Left
  536. p.pospix.Y = p.Position().Y + par.pospix.Y + par.marginSizes.Top + par.borderSizes.Top + par.paddingSizes.Top
  537. // Otherwise its coordinates are relative to the parent outer coordinates.
  538. } else {
  539. p.pospix.X = p.Position().X + par.pospix.X
  540. p.pospix.Y = p.Position().Y + par.pospix.Y
  541. }
  542. // Maximum x,y coordinates for this panel
  543. p.xmin = p.pospix.X
  544. p.ymin = p.pospix.Y
  545. p.xmax = p.pospix.X + p.width
  546. p.ymax = p.pospix.Y + p.height
  547. // Set default bounds to be entire panel texture
  548. p.udata.bounds = math32.Vector4{0, 0, 1, 1}
  549. // If this panel has no parent or is unbounded then the default bounds are correct
  550. if par == nil || !p.bounded {
  551. return
  552. }
  553. // From here on panel has parent and is bounded by parent
  554. // TODO why not use par.xmin, etc here ? shouldn't they be already updated?
  555. // Get the parent content area minimum and maximum absolute coordinates in pixels
  556. pxmin := par.pospix.X + par.marginSizes.Left + par.borderSizes.Left + par.paddingSizes.Left
  557. if pxmin < par.xmin {
  558. pxmin = par.xmin
  559. }
  560. pymin := par.pospix.Y + par.marginSizes.Top + par.borderSizes.Top + par.paddingSizes.Top
  561. if pymin < par.ymin {
  562. pymin = par.ymin
  563. }
  564. pxmax := par.pospix.X + par.width - (par.marginSizes.Right + par.borderSizes.Right + par.paddingSizes.Right)
  565. if pxmax > par.xmax {
  566. pxmax = par.xmax
  567. }
  568. pymax := par.pospix.Y + par.height - (par.marginSizes.Bottom + par.borderSizes.Bottom + par.paddingSizes.Bottom)
  569. if pymax > par.ymax {
  570. pymax = par.ymax
  571. }
  572. // Update this panel minimum x and y coordinates.
  573. if p.xmin < pxmin {
  574. p.xmin = pxmin
  575. }
  576. if p.ymin < pymin {
  577. p.ymin = pymin
  578. }
  579. // Update this panel maximum x and y coordinates.
  580. if p.xmax > pxmax {
  581. p.xmax = pxmax
  582. }
  583. if p.ymax > pymax {
  584. p.ymax = pymax
  585. }
  586. // If this panel is bounded to its parent, calculates the bounds
  587. // for clipping in texture coordinates
  588. if p.pospix.X < p.xmin {
  589. p.udata.bounds.X = (p.xmin - p.pospix.X) / p.width
  590. }
  591. if p.pospix.Y < p.ymin {
  592. p.udata.bounds.Y = (p.ymin - p.pospix.Y) / p.height
  593. }
  594. if p.pospix.X+p.width > p.xmax {
  595. p.udata.bounds.Z = (p.xmax - p.pospix.X) / p.width
  596. }
  597. if p.pospix.Y+p.height > p.ymax {
  598. p.udata.bounds.W = (p.ymax - p.pospix.Y) / p.height
  599. }
  600. }
  601. // calcWidth calculates the panel external width in pixels
  602. func (p *Panel) calcWidth() float32 {
  603. return p.content.Width +
  604. p.paddingSizes.Left + p.paddingSizes.Right +
  605. p.borderSizes.Left + p.borderSizes.Right +
  606. p.marginSizes.Left + p.marginSizes.Right
  607. }
  608. // calcHeight calculates the panel external height in pixels
  609. func (p *Panel) calcHeight() float32 {
  610. return p.content.Height +
  611. p.paddingSizes.Top + p.paddingSizes.Bottom +
  612. p.borderSizes.Top + p.borderSizes.Bottom +
  613. p.marginSizes.Top + p.marginSizes.Bottom
  614. }
  615. // resize tries to set the external size of the panel to the specified
  616. // dimensions and recalculates the size and positions of the internal areas.
  617. // The margins, borders and padding sizes are kept and the content
  618. // area size is adjusted. So if the panel is decreased, its minimum
  619. // size is determined by the margins, borders and paddings.
  620. // Normally it should be called with dispatch=true to recalculate the
  621. // panel layout and dispatch OnSize event.
  622. func (p *Panel) resize(width, height float32, dispatch bool) {
  623. var padding Rect
  624. var border Rect
  625. width = math32.Round(width)
  626. height = math32.Round(height)
  627. // Adjust content width
  628. p.content.Width = width - p.marginSizes.Left - p.marginSizes.Right - p.borderSizes.Left - p.borderSizes.Right - p.paddingSizes.Left - p.paddingSizes.Right
  629. if p.content.Width < 0 {
  630. p.content.Width = 0
  631. }
  632. // Adjusts content height
  633. p.content.Height = height - p.marginSizes.Top - p.marginSizes.Bottom - p.borderSizes.Top - p.borderSizes.Bottom - p.paddingSizes.Top - p.paddingSizes.Bottom
  634. if p.content.Height < 0 {
  635. p.content.Height = 0
  636. }
  637. // Adjust other area widths
  638. padding.Width = p.paddingSizes.Left + p.content.Width + p.paddingSizes.Right
  639. border.Width = p.borderSizes.Left + padding.Width + p.borderSizes.Right
  640. // Adjust other area heights
  641. padding.Height = p.paddingSizes.Top + p.content.Height + p.paddingSizes.Bottom
  642. border.Height = p.borderSizes.Top + padding.Height + p.borderSizes.Bottom
  643. // Set area positions
  644. border.X = p.marginSizes.Left
  645. border.Y = p.marginSizes.Top
  646. padding.X = border.X + p.borderSizes.Left
  647. padding.Y = border.Y + p.borderSizes.Top
  648. p.content.X = padding.X + p.paddingSizes.Left
  649. p.content.Y = padding.Y + p.paddingSizes.Top
  650. // Set final panel dimensions (may be different from requested dimensions)
  651. p.width = p.marginSizes.Left + border.Width + p.marginSizes.Right
  652. p.height = p.marginSizes.Top + border.Height + p.marginSizes.Bottom
  653. // Update border uniform in texture coordinates (0,0 -> 1,1)
  654. p.udata.borders = math32.Vector4{
  655. float32(border.X) / float32(p.width),
  656. float32(border.Y) / float32(p.height),
  657. float32(border.Width) / float32(p.width),
  658. float32(border.Height) / float32(p.height),
  659. }
  660. // Update padding uniform in texture coordinates (0,0 -> 1,1)
  661. p.udata.paddings = math32.Vector4{
  662. float32(padding.X) / float32(p.width),
  663. float32(padding.Y) / float32(p.height),
  664. float32(padding.Width) / float32(p.width),
  665. float32(padding.Height) / float32(p.height),
  666. }
  667. // Update content uniform in texture coordinates (0,0 -> 1,1)
  668. p.udata.content = math32.Vector4{
  669. float32(p.content.X) / float32(p.width),
  670. float32(p.content.Y) / float32(p.height),
  671. float32(p.content.Width) / float32(p.width),
  672. float32(p.content.Height) / float32(p.height),
  673. }
  674. p.SetChanged(true) // TODO necessary?
  675. // Update layout and dispatch event
  676. if !dispatch {
  677. return
  678. }
  679. if p.layout != nil {
  680. p.layout.Recalc(p)
  681. }
  682. p.Dispatch(OnResize, nil)
  683. }
  684. // RenderSetup is called by the Engine before drawing the object
  685. func (p *Panel) RenderSetup(gl *gls.GLS, rinfo *core.RenderInfo) {
  686. // Sets texture valid flag in uniforms
  687. // depending if the material has texture
  688. if p.mat.TextureCount() > 0 {
  689. p.udata.textureValid = 1
  690. } else {
  691. p.udata.textureValid = 0
  692. }
  693. // Sets model matrix
  694. var mm math32.Matrix4
  695. p.SetModelMatrix(gl, &mm)
  696. // Transfer model matrix uniform
  697. location := p.uniMatrix.Location(gl)
  698. gl.UniformMatrix4fv(location, 1, false, &mm[0])
  699. // Transfer panel parameters combined uniform
  700. location = p.uniPanel.Location(gl)
  701. const vec4count = 8
  702. gl.Uniform4fv(location, vec4count, &p.udata.bounds.X)
  703. }
  704. // SetModelMatrix calculates and sets the specified matrix with the model matrix for this panel
  705. func (p *Panel) SetModelMatrix(gl *gls.GLS, mm *math32.Matrix4) {
  706. // Get scale of window (for HiDPI support)
  707. sX, sY := Manager().win.GetScale()
  708. // Get the current viewport width and height
  709. _, _, width, height := gl.GetViewport()
  710. // Compute common factors
  711. fX := 2 * float32(sX) / float32(width)
  712. fY := 2 * float32(sY) / float32(height)
  713. // Calculate the model matrix
  714. // Convert pixel coordinates to standard OpenGL clip coordinates and scale the quad for the viewport
  715. mm.Set(
  716. fX*float32(p.width), 0, 0, fX*p.pospix.X-1,
  717. 0, fY*float32(p.height), 0, 1-fY*p.pospix.Y,
  718. 0, 0, 1, p.Position().Z,
  719. 0, 0, 0, 1,
  720. )
  721. }