clipper.go 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/gls"
  8. "github.com/g3n/engine/graphic"
  9. "github.com/g3n/engine/material"
  10. "github.com/g3n/engine/math32"
  11. )
  12. // Clipper is a 2D graphic which optionally clips its children inside its boundary
  13. type Clipper struct {
  14. *graphic.Graphic // Embedded graphic
  15. root *Root // pointer to root container
  16. width float32 // external width in pixels
  17. height float32 // external height in pixels
  18. mat *material.Material // panel material
  19. modelMatrixUni gls.UniformMatrix4f // pointer to model matrix uniform
  20. pospix math32.Vector3 // absolute position in pixels
  21. xmin float32 // minimum absolute x this panel can use
  22. xmax float32 // maximum absolute x this panel can use
  23. ymin float32 // minimum absolute y this panel can use
  24. ymax float32 // maximum absolute y this panel can use
  25. bounded bool // panel is bounded by its parent
  26. enabled bool // enable event processing
  27. cursorEnter bool // mouse enter dispatched
  28. layout ILayout // current layout for children
  29. layoutParams interface{} // current layout parameters used by container panel
  30. }
  31. // NewClipper creates and returns a pointer to a new clipper with the
  32. // specified dimensions in pixels
  33. func NewClipper(width, height float32, gr *graphic.Graphic) *Clipper {
  34. c := new(Clipper)
  35. c.Initialize(width, height, gr)
  36. return c
  37. }
  38. // Initialize initializes this panel with a different geometry, material and OpenGL primitive
  39. func (c *Clipper) Initialize(width, height float32, gr *graphic.Graphic) {
  40. c.Graphic = gr
  41. c.width = width
  42. c.height = height
  43. // Creates and adds uniform
  44. c.modelMatrixUni.Init("ModelMatrix")
  45. // Set defaults
  46. c.bounded = true
  47. c.enabled = true
  48. //c.resize(width, height)
  49. }
  50. // RenderSetup is called by the Engine before drawing the object
  51. func (c *Clipper) RenderSetup(gl *gls.GLS, rinfo *core.RenderInfo) {
  52. // Sets model matrix
  53. var mm math32.Matrix4
  54. c.SetModelMatrix(gl, &mm)
  55. c.modelMatrixUni.SetMatrix4(&mm)
  56. // Transfer model matrix uniform
  57. c.modelMatrixUni.Transfer(gl)
  58. }
  59. // SetModelMatrix calculates and sets the specified matrix with the model matrix for this clipper
  60. func (c *Clipper) SetModelMatrix(gl *gls.GLS, mm *math32.Matrix4) {
  61. // Get the current viewport width and height
  62. _, _, width, height := gl.GetViewport()
  63. fwidth := float32(width)
  64. fheight := float32(height)
  65. // Scale the quad for the viewport so it has fixed dimensions in pixels.
  66. fw := float32(c.width) / fwidth
  67. fh := float32(c.height) / fheight
  68. var scale math32.Vector3
  69. scale.Set(2*fw, 2*fh, 1)
  70. // Convert absolute position in pixel coordinates from the top/left to
  71. // standard OpenGL clip coordinates of the quad center
  72. var posclip math32.Vector3
  73. posclip.X = (c.pospix.X - fwidth/2) / (fwidth / 2)
  74. posclip.Y = -(c.pospix.Y - fheight/2) / (fheight / 2)
  75. posclip.Z = c.Position().Z
  76. // Calculates the model matrix
  77. var quat math32.Quaternion
  78. quat.SetIdentity()
  79. mm.Compose(&posclip, &quat, &scale)
  80. }