panel.go 29 KB

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