clipper.go 3.5 KB

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