collisionWindow.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #################################################################
  2. # collisionWindow.py
  3. # Written by Yi-Hong Lin, [email protected], 2004
  4. #################################################################
  5. # Import Tkinter, Pmw, and the floater code from this directory tree.
  6. from direct.tkwidgets.AppShell import *
  7. from direct.showbase.TkGlobal import *
  8. from seColorEntry import *
  9. from direct.tkwidgets import VectorWidgets
  10. from direct.tkwidgets import Floater
  11. from direct.tkwidgets import Slider
  12. from Tkinter import *
  13. import string, math, types
  14. class collisionWindow(AppShell):
  15. #################################################################
  16. # This will open a talk window for user to set the collision object
  17. # In here, we won't finish the whole process to generate the
  18. # collision object, half of process will be finished by dataHolder.
  19. #################################################################
  20. # Override class variables
  21. appname = 'Creating Collision Object'
  22. frameWidth = 600
  23. frameHeight = 300
  24. widgetsDict = {}
  25. # Define the types of collision we take care here
  26. collisionType = ['collisionPolygon',
  27. 'collisionSphere',
  28. 'collisionSegment',
  29. 'collisionRay']
  30. def __init__(self, nodePath, parent = None, **kw):
  31. self.nodePath = nodePath
  32. self.objType = 'collisionSphere' # set default type to Collision Sphere
  33. INITOPT = Pmw.INITOPT
  34. optiondefs = (
  35. ('title', self.appname, None),
  36. )
  37. self.defineoptions(kw, optiondefs)
  38. # Initialize the superclass
  39. AppShell.__init__(self)
  40. # Execute option callbacks
  41. self.initialiseoptions(collisionWindow)
  42. self.parent.resizable(False,False) ## Disable the ability to resize for this Window.
  43. def createInterface(self):
  44. # Handle to the toplevels interior
  45. interior = self.interior()
  46. menuBar = self.menuBar
  47. self.menuBar.destroy()
  48. # Create a frame to hold all stuff
  49. mainFrame = Frame(interior)
  50. frame = Frame(mainFrame)
  51. self.collisionTypeEntry = self.createcomponent(
  52. 'Collision Type', (), None,
  53. Pmw.ComboBox, (frame,),
  54. labelpos = W, label_text='Collision Object Type:', entry_width = 20,
  55. selectioncommand = self.setObjectType,
  56. scrolledlist_items = self.collisionType)
  57. self.collisionTypeEntry.pack(side=LEFT, padx=3)
  58. label = Label(frame, text='Parent NodePath: '+ self.nodePath.getName(), font=('MSSansSerif', 12),
  59. relief = RIDGE)
  60. label.pack(side=LEFT,expand=0,fill=X, padx=20)
  61. frame.pack(side=TOP, fill=X, expand=True, padx=3)
  62. self.collisionTypeEntry.selectitem('collisionSphere', setentry=True)
  63. self.inputZone = Pmw.Group(mainFrame, tag_pyclass = None)
  64. self.inputZone.pack(fill='both',expand=1)
  65. settingFrame = self.inputZone.interior()
  66. ############################################
  67. # Notebook pages for specific object setting
  68. ############################################
  69. self.objNotebook = Pmw.NoteBook(settingFrame, tabpos = None,
  70. borderwidth = 0)
  71. PolygonPage = self.objNotebook.add('Polygon')
  72. SpherePage = self.objNotebook.add('Sphere')
  73. SegmentPage = self.objNotebook.add('Segment')
  74. RayPage = self.objNotebook.add('Ray')
  75. self.objNotebook.selectpage('Sphere')
  76. # Put this here so it isn't called right away
  77. self.objNotebook['raisecommand'] = self.updateObjInfo
  78. # Polygon object setting
  79. Interior = Frame(PolygonPage)
  80. label = Label(Interior, text='Attention! All Coordinates Are Related To Its Parent Node!')
  81. label.pack(side=LEFT,expand=0,fill=X, padx=1)
  82. Interior.pack(side=TOP, expand=0,fill=X)
  83. self.createPosEntry(PolygonPage, catagory='Polygon', id='Point A')
  84. self.createPosEntry(PolygonPage, catagory='Polygon', id='Point B')
  85. self.createPosEntry(PolygonPage, catagory='Polygon', id='Point C')
  86. # Sphere object setting
  87. Interior = Frame(SpherePage)
  88. label = Label(Interior, text='Attention! All Coordinates Are Related To Its Parent Node!')
  89. label.pack(side=LEFT,expand=0,fill=X, padx=1)
  90. Interior.pack(side=TOP, expand=0,fill=X)
  91. self.createPosEntry(SpherePage, catagory='Sphere', id='Center Point')
  92. self.createEntryField(SpherePage,catagory='Sphere', id='Size',
  93. value = 1.0,
  94. command = None,
  95. initialState='normal',
  96. side = 'top')
  97. # Segment object setting
  98. Interior = Frame(SegmentPage)
  99. label = Label(Interior, text='Attention! All Coordinates Are Related To Its Parent Node!')
  100. label.pack(side=LEFT,expand=0,fill=X, padx=1)
  101. Interior.pack(side=TOP, expand=0,fill=X)
  102. self.createPosEntry(SegmentPage, catagory='Segment', id='Point A')
  103. self.createPosEntry(SegmentPage, catagory='Segment', id='Point B')
  104. # Ray object setting
  105. Interior = Frame(RayPage)
  106. label = Label(Interior, text='Attention! All Coordinates Are Related To Its Parent Node!')
  107. label.pack(side=LEFT,expand=0,fill=X, padx=1)
  108. Interior.pack(side=TOP, expand=0,fill=X)
  109. self.createPosEntry(RayPage, catagory='Ray', id='Origin')
  110. self.createPosEntry(RayPage, catagory='Ray', id='Direction')
  111. self.objNotebook.setnaturalsize()
  112. self.objNotebook.pack(expand = 1, fill = BOTH)
  113. self.okButton = Button(mainFrame, text="OK", command=self.okPress,width=10)
  114. self.okButton.pack(fill=BOTH,expand=0,side=RIGHT)
  115. mainFrame.pack(expand=1, fill = BOTH)
  116. def onDestroy(self, event):
  117. messenger.send('CW_close')
  118. '''
  119. If you have open any thing, please rewrite here!
  120. '''
  121. pass
  122. def setObjectType(self, typeName = 'collisionSphere'):
  123. #################################################################
  124. # setObjectType(self, typeName = 'collisionSphere')
  125. # Call back function
  126. # This function will be called when user select target collision
  127. # type on the combo box on the panel.
  128. # Basically, this function's job is to switch the notebook page to right one.
  129. #################################################################
  130. self.objType = typeName
  131. if self.objType=='collisionPolygon':
  132. self.objNotebook.selectpage('Polygon')
  133. elif self.objType=='collisionSphere':
  134. self.objNotebook.selectpage('Sphere')
  135. elif self.objType=='collisionSegment':
  136. self.objNotebook.selectpage('Segment')
  137. elif self.objType=='collisionRay':
  138. self.objNotebook.selectpage('Ray')
  139. return
  140. def updateObjInfo(self, page=None):
  141. #################################################################
  142. # Nothing. Unlike in the lighting panel, we don't have to keep data
  143. # once user switch the page.
  144. #################################################################
  145. return
  146. def okPress(self):
  147. #################################################################
  148. # okPress(self)
  149. # This function will be called when user click on the Ok button.
  150. # Then this function will collect all parameters that we need to create
  151. # a collision Object from the panel and generate the colision Object.
  152. # In the last, it will send the object out with a message to dataHolder to
  153. # put the object into a CollisionNode and attach it to the target nodePath
  154. #################################################################
  155. collisionObject = None
  156. print self.objType
  157. if self.objType=='collisionPolygon':
  158. pointA = Point3(float(self.widgetDict['PolygonPoint A'][0]._entry.get()),
  159. float(self.widgetDict['PolygonPoint A'][1]._entry.get()),
  160. float(self.widgetDict['PolygonPoint A'][2]._entry.get()))
  161. pointB = Point3(float(self.widgetDict['PolygonPoint B'][0]._entry.get()),
  162. float(self.widgetDict['PolygonPoint B'][1]._entry.get()),
  163. float(self.widgetDict['PolygonPoint B'][2]._entry.get()))
  164. pointC = Point3(float(self.widgetDict['PolygonPoint C'][0]._entry.get()),
  165. float(self.widgetDict['PolygonPoint C'][1]._entry.get()),
  166. float(self.widgetDict['PolygonPoint C'][2]._entry.get()))
  167. collisionObject = CollisionPolygon(pointA, pointB, pointC)
  168. elif self.objType=='collisionSphere':
  169. collisionObject = CollisionSphere(float(self.widgetDict['SphereCenter Point'][0]._entry.get()),
  170. float(self.widgetDict['SphereCenter Point'][1]._entry.get()),
  171. float(self.widgetDict['SphereCenter Point'][2]._entry.get()),
  172. float(self.widgetDict['SphereSize'].getvalue()))
  173. elif self.objType=='collisionSegment':
  174. pointA = Point3(float(self.widgetDict['SegmentPoint A'][0]._entry.get()),
  175. float(self.widgetDict['SegmentPoint A'][1]._entry.get()),
  176. float(self.widgetDict['SegmentPoint A'][2]._entry.get()))
  177. pointB = Point3(float(self.widgetDict['SegmentPoint B'][0]._entry.get()),
  178. float(self.widgetDict['SegmentPoint B'][1]._entry.get()),
  179. float(self.widgetDict['SegmentPoint B'][2]._entry.get()))
  180. collisionObject = CollisionSegment()
  181. collisionObject.setPointA(pointA)
  182. collisionObject.setFromLens(base.cam.node(), Point2( -1, 1 )) ## You must set up the camera lensNode before you set point B....
  183. collisionObject.setPointB(pointB)
  184. elif self.objType=='collisionRay':
  185. point = Point3(float(self.widgetDict['RayOrigin'][0]._entry.get()),
  186. float(self.widgetDict['RayOrigin'][1]._entry.get()),
  187. float(self.widgetDict['RayOrigin'][2]._entry.get()))
  188. vector = Vec3(float(self.widgetDict['RayDirection'][0]._entry.get()),
  189. float(self.widgetDict['RayDirection'][1]._entry.get()),
  190. float(self.widgetDict['RayDirection'][2]._entry.get()))
  191. print vector, point
  192. collisionObject = CollisionRay()
  193. collisionObject.setOrigin(point)
  194. collisionObject.setDirection(vector)
  195. #collisionObject.setFromLens(base.cam.node(), Point2( -1, 1 )) ## You must set up the camera lensNode before you set up others...
  196. if self.objType=='collisionPolygon':
  197. messenger.send('CW_addCollisionObj', [collisionObject, self.nodePath, pointA, pointB, pointC])
  198. else:
  199. messenger.send('CW_addCollisionObj', [collisionObject, self.nodePath])
  200. self.quit()
  201. return
  202. def createPosEntry(self, contentFrame, catagory, id):
  203. posInterior = Frame(contentFrame)
  204. label = Label(posInterior, text=id+':')
  205. label.pack(side=LEFT,expand=0,fill=X, padx=1)
  206. self.posX = self.createcomponent('posX'+catagory+id, (), None,
  207. Floater.Floater, (posInterior,),
  208. text = 'X', relief = FLAT,
  209. value = 0.0,
  210. entry_width = 6)
  211. self.posX.pack(side=LEFT,expand=0,fill=X, padx=1)
  212. self.posY = self.createcomponent('posY'+catagory+id, (), None,
  213. Floater.Floater, (posInterior,),
  214. text = 'Y', relief = FLAT,
  215. value = 0.0,
  216. entry_width = 6)
  217. self.posY.pack(side=LEFT, expand=0,fill=X, padx=1)
  218. self.posZ = self.createcomponent('posZ'+catagory+id, (), None,
  219. Floater.Floater, (posInterior,),
  220. text = 'Z', relief = FLAT,
  221. value = 0.0,
  222. entry_width = 6)
  223. self.posZ.pack(side=LEFT, expand=0,fill=X, padx=1)
  224. self.widgetDict[catagory+id]=[self.posX, self.posY, self.posZ]
  225. posInterior.pack(side=TOP, expand=0,fill=X, padx=3, pady=3)
  226. return
  227. def createEntryField(self, parent, catagory, id, value,
  228. command, initialState, labelWidth = 6,
  229. side = 'left', fill = X, expand = 0,
  230. validate = None,
  231. defaultButton = False, buttonText = 'Default',defaultFunction = None ):
  232. frame = Frame(parent)
  233. widget = Pmw.EntryField(frame, labelpos='w', label_text = id+':',
  234. value = value, entry_font=('MSSansSerif', 10),label_font=('MSSansSerif', 10),
  235. modifiedcommand=command, validate = validate,
  236. label_width = labelWidth)
  237. widget.configure(entry_state = initialState)
  238. widget.pack(side=LEFT)
  239. self.widgetDict[catagory+id] = widget
  240. if defaultButton and (defaultFunction!=None):
  241. widget = Button(frame, text=buttonText, font=('MSSansSerif', 10), command = defaultFunction)
  242. widget.pack(side=LEFT, padx=3)
  243. self.widgetDict[catagory+id+'-'+'DefaultButton']=widget
  244. frame.pack(side = side, fill = fill, expand = expand,pady=3)