seGrid.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #################################################################
  2. # seGrid.py
  3. # Originally from DirectGrid.py
  4. # Altered by Yi-Hong Lin, [email protected], 2004
  5. #
  6. # We didn't change anything essential.
  7. # Just because we customized the seSession from DirectSession,
  8. # So we need related files can follow the change.
  9. # However, we don't want to change anything inside the original directool
  10. # to let them can work with our scene editor.
  11. # (If we do change original directools, it will force user has to install the latest version of OUR Panda)
  12. #
  13. #################################################################
  14. from direct.showbase.DirectObject import *
  15. from direct.directtools.DirectUtil import *
  16. from seGeometry import *
  17. class DirectGrid(NodePath,DirectObject):
  18. def __init__(self):
  19. # Initialize superclass
  20. NodePath.__init__(self)
  21. self.assign(hidden.attachNewNode('DirectGrid'))
  22. # Don't wireframe or light
  23. useDirectRenderStyle(self)
  24. # Load up grid parts to initialize grid object
  25. # Polygon used to mark grid plane
  26. self.gridBack = loader.loadModel('models/misc/gridBack')
  27. self.gridBack.reparentTo(self)
  28. self.gridBack.setColor(0.5,0.5,0.5,0.5)
  29. # Grid Lines
  30. self.lines = self.attachNewNode('gridLines')
  31. self.minorLines = LineNodePath(self.lines)
  32. self.minorLines.lineNode.setName('minorLines')
  33. self.minorLines.setColor(VBase4(0.3,0.55,1,1))
  34. self.minorLines.setThickness(1)
  35. self.majorLines = LineNodePath(self.lines)
  36. self.majorLines.lineNode.setName('majorLines')
  37. self.majorLines.setColor(VBase4(0.3,0.55,1,1))
  38. self.majorLines.setThickness(5)
  39. self.centerLines = LineNodePath(self.lines)
  40. self.centerLines.lineNode.setName('centerLines')
  41. self.centerLines.setColor(VBase4(1,0,0,0))
  42. self.centerLines.setThickness(3)
  43. # Small marker to hilight snap-to-grid point
  44. self.snapMarker = loader.loadModel('models/misc/sphere')
  45. self.snapMarker.node().setName('gridSnapMarker')
  46. self.snapMarker.reparentTo(self)
  47. self.snapMarker.setColor(1,0,0,1)
  48. self.snapMarker.setScale(0.3)
  49. self.snapPos = Point3(0)
  50. # Initialize Grid characteristics
  51. self.fXyzSnap = 1
  52. self.fHprSnap = 1
  53. self.gridSize = 100.0
  54. self.gridSpacing = 5.0
  55. self.snapAngle = 15.0
  56. self.enable()
  57. def enable(self):
  58. self.reparentTo(SEditor.group)
  59. self.updateGrid()
  60. self.fEnabled = 1
  61. def disable(self):
  62. self.reparentTo(hidden)
  63. self.fEnabled = 0
  64. def toggleGrid(self):
  65. if self.fEnabled:
  66. self.disable()
  67. else:
  68. self.enable()
  69. def isEnabled(self):
  70. return self.fEnabled
  71. def updateGrid(self):
  72. # Update grid lines based upon current grid spacing and grid size
  73. # First reset existing grid lines
  74. self.minorLines.reset()
  75. self.majorLines.reset()
  76. self.centerLines.reset()
  77. # Now redraw lines
  78. numLines = int(math.ceil(self.gridSize/self.gridSpacing))
  79. scaledSize = numLines * self.gridSpacing
  80. center = self.centerLines
  81. minor = self.minorLines
  82. major = self.majorLines
  83. for i in range(-numLines,numLines + 1):
  84. if i == 0:
  85. center.moveTo(i * self.gridSpacing, -scaledSize, 0)
  86. center.drawTo(i * self.gridSpacing, scaledSize, 0)
  87. center.moveTo(-scaledSize, i * self.gridSpacing, 0)
  88. center.drawTo(scaledSize, i * self.gridSpacing, 0)
  89. else:
  90. if (i % 5) == 0:
  91. major.moveTo(i * self.gridSpacing, -scaledSize, 0)
  92. major.drawTo(i * self.gridSpacing, scaledSize, 0)
  93. major.moveTo(-scaledSize, i * self.gridSpacing, 0)
  94. major.drawTo(scaledSize, i * self.gridSpacing, 0)
  95. else:
  96. minor.moveTo(i * self.gridSpacing, -scaledSize, 0)
  97. minor.drawTo(i * self.gridSpacing, scaledSize, 0)
  98. minor.moveTo(-scaledSize, i * self.gridSpacing, 0)
  99. minor.drawTo(scaledSize, i * self.gridSpacing, 0)
  100. center.create()
  101. minor.create()
  102. major.create()
  103. self.gridBack.setScale(scaledSize)
  104. def setXyzSnap(self, fSnap):
  105. self.fXyzSnap = fSnap
  106. def getXyzSnap(self):
  107. return self.fXyzSnap
  108. def setHprSnap(self, fSnap):
  109. self.fHprSnap = fSnap
  110. def getHprSnap(self):
  111. return self.fHprSnap
  112. def computeSnapPoint(self, point):
  113. # Start of with current point
  114. self.snapPos.assign(point)
  115. # Snap if necessary
  116. if self.fXyzSnap:
  117. self.snapPos.set(
  118. ROUND_TO(self.snapPos[0], self.gridSpacing),
  119. ROUND_TO(self.snapPos[1], self.gridSpacing),
  120. ROUND_TO(self.snapPos[2], self.gridSpacing))
  121. # Move snap marker to this point
  122. self.snapMarker.setPos(self.snapPos)
  123. # Return the hit point
  124. return self.snapPos
  125. def computeSnapAngle(self, angle):
  126. return ROUND_TO(angle, self.snapAngle)
  127. def setSnapAngle(self, angle):
  128. self.snapAngle = angle
  129. def getSnapAngle(self):
  130. return self.snapAngle
  131. def setGridSpacing(self, spacing):
  132. self.gridSpacing = spacing
  133. self.updateGrid()
  134. def getGridSpacing(self):
  135. return self.gridSpacing
  136. def setGridSize(self, size):
  137. # Set size of grid back and redraw lines
  138. self.gridSize = size
  139. self.updateGrid()
  140. def getGridSize(self):
  141. return self.gridSize