step6_controllable_system.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #!/usr/bin/env python
  2. # Author: Shao Zhang and Phil Saltzman
  3. # Last Updated: 2015-03-13
  4. #
  5. # This tutorial will cover events and how they can be used in Panda
  6. # Specifically, this lesson will use events to capture keyboard presses and
  7. # mouse clicks to trigger actions in the world. It will also use events
  8. # to count the number of orbits the Earth makes around the sun. This
  9. # tutorial uses the same base code from the solar system tutorial.
  10. from direct.showbase.ShowBase import ShowBase
  11. base = ShowBase()
  12. from panda3d.core import TextNode
  13. from direct.interval.IntervalGlobal import *
  14. from direct.gui.DirectGui import *
  15. from direct.showbase.DirectObject import DirectObject
  16. import sys
  17. # We start this tutorial with the standard class. However, the class is a
  18. # subclass of an object called DirectObject. This gives the class the ability
  19. # to listen for and respond to events. From now on the main class in every
  20. # tutorial will be a subclass of DirectObject
  21. class World(DirectObject):
  22. # Macro-like function used to reduce the amount to code needed to create the
  23. # on screen instructions
  24. def genLabelText(self, text, i):
  25. return OnscreenText(text=text, pos=(0.06, -.06 * (i + 0.5)), fg=(1, 1, 1, 1),
  26. parent=base.a2dTopLeft,align=TextNode.ALeft, scale=.05)
  27. def __init__(self):
  28. # The standard camera position and background initialization
  29. base.setBackgroundColor(0, 0, 0)
  30. base.disableMouse()
  31. camera.setPos(0, 0, 45)
  32. camera.setHpr(0, -90, 0)
  33. # The global variables we used to control the speed and size of objects
  34. self.yearscale = 60
  35. self.dayscale = self.yearscale / 365.0 * 5
  36. self.orbitscale = 10
  37. self.sizescale = 0.6
  38. self.loadPlanets() # Load, texture, and position the planets
  39. self.rotatePlanets() # Set up the motion to start them moving
  40. # The standard title text that's in every tutorial
  41. # Things to note:
  42. #-fg represents the forground color of the text in (r,g,b,a) format
  43. #-pos represents the position of the text on the screen.
  44. # The coordinate system is a x-y based wih 0,0 as the center of the
  45. # screen
  46. #-align sets the alingment of the text relative to the pos argument.
  47. # Default is center align.
  48. #-scale set the scale of the text
  49. #-mayChange argument lets us change the text later in the program.
  50. # By default mayChange is set to 0. Trying to change text when
  51. # mayChange is set to 0 will cause the program to crash.
  52. self.title = OnscreenText(
  53. text="Panda3D: Tutorial 3 - Events",
  54. parent=base.a2dBottomRight, align=TextNode.A_right,
  55. style=1, fg=(1, 1, 1, 1), pos=(-0.1, 0.1), scale=.07)
  56. self.mouse1EventText = self.genLabelText(
  57. "Mouse Button 1: Toggle entire Solar System [RUNNING]", 1)
  58. self.skeyEventText = self.genLabelText("[S]: Toggle Sun [RUNNING]", 2)
  59. self.ykeyEventText = self.genLabelText("[Y]: Toggle Mercury [RUNNING]", 3)
  60. self.vkeyEventText = self.genLabelText("[V]: Toggle Venus [RUNNING]", 4)
  61. self.ekeyEventText = self.genLabelText("[E]: Toggle Earth [RUNNING]", 5)
  62. self.mkeyEventText = self.genLabelText("[M]: Toggle Mars [RUNNING]", 6)
  63. self.yearCounterText = self.genLabelText("0 Earth years completed", 7)
  64. self.yearCounter = 0 # year counter for earth years
  65. self.simRunning = True # boolean to keep track of the
  66. # state of the global simulation
  67. # Events
  68. # Each self.accept statement creates an event handler object that will call
  69. # the specified function when that event occurs.
  70. # Certain events like "mouse1", "a", "b", "c" ... "z", "1", "2", "3"..."0"
  71. # are references to keyboard keys and mouse buttons. You can also define
  72. # your own events to be used within your program. In this tutorial, the
  73. # event "newYear" is not tied to a physical input device, but rather
  74. # is sent by the function that rotates the Earth whenever a revolution
  75. # completes to tell the counter to update
  76. # Exit the program when escape is pressed
  77. self.accept("escape", sys.exit)
  78. self.accept("mouse1", self.handleMouseClick)
  79. self.accept("e", self.handleEarth)
  80. self.accept("s", # message name
  81. self.togglePlanet, # function to call
  82. ["Sun", # arguments to be passed to togglePlanet
  83. # See togglePlanet's definition below for
  84. # an explanation of what they are
  85. self.day_period_sun,
  86. None,
  87. self.skeyEventText])
  88. # Repeat the structure above for the other planets
  89. self.accept("y", self.togglePlanet,
  90. ["Mercury", self.day_period_mercury,
  91. self.orbit_period_mercury, self.ykeyEventText])
  92. self.accept("v", self.togglePlanet,
  93. ["Venus", self.day_period_venus,
  94. self.orbit_period_venus, self.vkeyEventText])
  95. self.accept("m", self.togglePlanet,
  96. ["Mars", self.day_period_mars,
  97. self.orbit_period_mars, self.mkeyEventText])
  98. self.accept("newYear", self.incYear)
  99. # end __init__
  100. def handleMouseClick(self):
  101. # When the mouse is clicked, if the simulation is running pause all the
  102. # planets and sun, otherwise resume it
  103. if self.simRunning:
  104. print("Pausing Simulation")
  105. # changing the text to reflect the change from "RUNNING" to
  106. # "PAUSED"
  107. self.mouse1EventText.setText(
  108. "Mouse Button 1: Toggle entire Solar System [PAUSED]")
  109. # For each planet, check if it is moving and if so, pause it
  110. # Sun
  111. if self.day_period_sun.isPlaying():
  112. self.togglePlanet("Sun", self.day_period_sun, None,
  113. self.skeyEventText)
  114. if self.day_period_mercury.isPlaying():
  115. self.togglePlanet("Mercury", self.day_period_mercury,
  116. self.orbit_period_mercury, self.ykeyEventText)
  117. # Venus
  118. if self.day_period_venus.isPlaying():
  119. self.togglePlanet("Venus", self.day_period_venus,
  120. self.orbit_period_venus, self.vkeyEventText)
  121. #Earth and moon
  122. if self.day_period_earth.isPlaying():
  123. self.togglePlanet("Earth", self.day_period_earth,
  124. self.orbit_period_earth, self.ekeyEventText)
  125. self.togglePlanet("Moon", self.day_period_moon,
  126. self.orbit_period_moon)
  127. # Mars
  128. if self.day_period_mars.isPlaying():
  129. self.togglePlanet("Mars", self.day_period_mars,
  130. self.orbit_period_mars, self.mkeyEventText)
  131. else:
  132. #"The simulation is paused, so resume it
  133. print("Resuming Simulation")
  134. self.mouse1EventText.setText(
  135. "Mouse Button 1: Toggle entire Solar System [RUNNING]")
  136. # the not operator does the reverse of the previous code
  137. if not self.day_period_sun.isPlaying():
  138. self.togglePlanet("Sun", self.day_period_sun, None,
  139. self.skeyEventText)
  140. if not self.day_period_mercury.isPlaying():
  141. self.togglePlanet("Mercury", self.day_period_mercury,
  142. self.orbit_period_mercury, self.ykeyEventText)
  143. if not self.day_period_venus.isPlaying():
  144. self.togglePlanet("Venus", self.day_period_venus,
  145. self.orbit_period_venus, self.vkeyEventText)
  146. if not self.day_period_earth.isPlaying():
  147. self.togglePlanet("Earth", self.day_period_earth,
  148. self.orbit_period_earth, self.ekeyEventText)
  149. self.togglePlanet("Moon", self.day_period_moon,
  150. self.orbit_period_moon)
  151. if not self.day_period_mars.isPlaying():
  152. self.togglePlanet("Mars", self.day_period_mars,
  153. self.orbit_period_mars, self.mkeyEventText)
  154. # toggle self.simRunning
  155. self.simRunning = not self.simRunning
  156. # end handleMouseClick
  157. # The togglePlanet function will toggle the intervals that are given to it
  158. # between paused and playing.
  159. # Planet is the name to print
  160. # Day is the interval that spins the planet
  161. # Orbit is the interval that moves around the orbit
  162. # Text is the OnscreenText object that needs to be updated
  163. def togglePlanet(self, planet, day, orbit=None, text=None):
  164. if day.isPlaying():
  165. print("Pausing " + planet)
  166. state = " [PAUSED]"
  167. else:
  168. print("Resuming " + planet)
  169. state = " [RUNNING]"
  170. # Update the onscreen text if it is given as an argument
  171. if text:
  172. old = text.getText()
  173. # strip out the last segment of text after the last white space
  174. # and append the string stored in 'state'
  175. text.setText(old[0:old.rfind(' ')] + state)
  176. # toggle the day interval
  177. self.toggleInterval(day)
  178. # if there is an orbit interval, toggle it
  179. if orbit:
  180. self.toggleInterval(orbit)
  181. # end togglePlanet
  182. # toggleInterval does exactly as its name implies
  183. # It takes an interval as an argument. Then it checks to see if it is playing.
  184. # If it is, it pauses it, otherwise it resumes it.
  185. def toggleInterval(self, interval):
  186. if interval.isPlaying():
  187. interval.pause()
  188. else:
  189. interval.resume()
  190. # end toggleInterval
  191. # Earth needs a special buffer function because the moon is tied to it
  192. # When the "e" key is pressed, togglePlanet is called on both the earth and
  193. # the moon.
  194. def handleEarth(self):
  195. self.togglePlanet("Earth", self.day_period_earth,
  196. self.orbit_period_earth, self.ekeyEventText)
  197. self.togglePlanet("Moon", self.day_period_moon,
  198. self.orbit_period_moon)
  199. # end handleEarth
  200. # the function incYear increments the variable yearCounter and then updates
  201. # the OnscreenText 'yearCounterText' every time the message "newYear" is
  202. # sent
  203. def incYear(self):
  204. self.yearCounter += 1
  205. self.yearCounterText.setText(
  206. str(self.yearCounter) + " Earth years completed")
  207. # end incYear
  208. #########################################################################
  209. # Except for the one commented line below, this is all as it was before #
  210. # Scroll down to the next comment to see an example of sending messages #
  211. #########################################################################
  212. def loadPlanets(self):
  213. self.orbit_root_mercury = render.attachNewNode('orbit_root_mercury')
  214. self.orbit_root_venus = render.attachNewNode('orbit_root_venus')
  215. self.orbit_root_mars = render.attachNewNode('orbit_root_mars')
  216. self.orbit_root_earth = render.attachNewNode('orbit_root_earth')
  217. self.orbit_root_moon = (
  218. self.orbit_root_earth.attachNewNode('orbit_root_moon'))
  219. self.sky = loader.loadModel("models/solar_sky_sphere")
  220. self.sky_tex = loader.loadTexture("models/stars_1k_tex.jpg")
  221. self.sky.setTexture(self.sky_tex, 1)
  222. self.sky.reparentTo(render)
  223. self.sky.setScale(40)
  224. self.sun = loader.loadModel("models/planet_sphere")
  225. self.sun_tex = loader.loadTexture("models/sun_1k_tex.jpg")
  226. self.sun.setTexture(self.sun_tex, 1)
  227. self.sun.reparentTo(render)
  228. self.sun.setScale(2 * self.sizescale)
  229. self.mercury = loader.loadModel("models/planet_sphere")
  230. self.mercury_tex = loader.loadTexture("models/mercury_1k_tex.jpg")
  231. self.mercury.setTexture(self.mercury_tex, 1)
  232. self.mercury.reparentTo(self.orbit_root_mercury)
  233. self.mercury.setPos(0.38 * self.orbitscale, 0, 0)
  234. self.mercury.setScale(0.385 * self.sizescale)
  235. self.venus = loader.loadModel("models/planet_sphere")
  236. self.venus_tex = loader.loadTexture("models/venus_1k_tex.jpg")
  237. self.venus.setTexture(self.venus_tex, 1)
  238. self.venus.reparentTo(self.orbit_root_venus)
  239. self.venus.setPos(0.72 * self.orbitscale, 0, 0)
  240. self.venus.setScale(0.923 * self.sizescale)
  241. self.mars = loader.loadModel("models/planet_sphere")
  242. self.mars_tex = loader.loadTexture("models/mars_1k_tex.jpg")
  243. self.mars.setTexture(self.mars_tex, 1)
  244. self.mars.reparentTo(self.orbit_root_mars)
  245. self.mars.setPos(1.52 * self.orbitscale, 0, 0)
  246. self.mars.setScale(0.515 * self.sizescale)
  247. self.earth = loader.loadModel("models/planet_sphere")
  248. self.earth_tex = loader.loadTexture("models/earth_1k_tex.jpg")
  249. self.earth.setTexture(self.earth_tex, 1)
  250. self.earth.reparentTo(self.orbit_root_earth)
  251. self.earth.setScale(self.sizescale)
  252. self.earth.setPos(self.orbitscale, 0, 0)
  253. self.orbit_root_moon.setPos(self.orbitscale, 0, 0)
  254. self.moon = loader.loadModel("models/planet_sphere")
  255. self.moon_tex = loader.loadTexture("models/moon_1k_tex.jpg")
  256. self.moon.setTexture(self.moon_tex, 1)
  257. self.moon.reparentTo(self.orbit_root_moon)
  258. self.moon.setScale(0.1 * self.sizescale)
  259. self.moon.setPos(0.1 * self.orbitscale, 0, 0)
  260. def rotatePlanets(self):
  261. self.day_period_sun = self.sun.hprInterval(20, (360, 0, 0))
  262. self.orbit_period_mercury = self.orbit_root_mercury.hprInterval(
  263. (0.241 * self.yearscale), (360, 0, 0))
  264. self.day_period_mercury = self.mercury.hprInterval(
  265. (59 * self.dayscale), (360, 0, 0))
  266. self.orbit_period_venus = self.orbit_root_venus.hprInterval(
  267. (0.615 * self.yearscale), (360, 0, 0))
  268. self.day_period_venus = self.venus.hprInterval(
  269. (243 * self.dayscale), (360, 0, 0))
  270. # Here the earth interval has been changed to rotate like the rest of the
  271. # planets and send a message before it starts turning again. To send a
  272. # message, the call is simply messenger.send("message"). The "newYear"
  273. # message is picked up by the accept("newYear"...) statement earlier, and
  274. # calls the incYear function as a result
  275. self.orbit_period_earth = Sequence(
  276. self.orbit_root_earth.hprInterval(
  277. self.yearscale, (360, 0, 0)),
  278. Func(messenger.send, "newYear"))
  279. self.day_period_earth = self.earth.hprInterval(
  280. self.dayscale, (360, 0, 0))
  281. self.orbit_period_moon = self.orbit_root_moon.hprInterval(
  282. (.0749 * self.yearscale), (360, 0, 0))
  283. self.day_period_moon = self.moon.hprInterval(
  284. (.0749 * self.yearscale), (360, 0, 0))
  285. self.orbit_period_mars = self.orbit_root_mars.hprInterval(
  286. (1.881 * self.yearscale), (360, 0, 0))
  287. self.day_period_mars = self.mars.hprInterval(
  288. (1.03 * self.dayscale), (360, 0, 0))
  289. self.day_period_sun.loop()
  290. self.orbit_period_mercury.loop()
  291. self.day_period_mercury.loop()
  292. self.orbit_period_venus.loop()
  293. self.day_period_venus.loop()
  294. self.orbit_period_earth.loop()
  295. self.day_period_earth.loop()
  296. self.orbit_period_moon.loop()
  297. self.day_period_moon.loop()
  298. self.orbit_period_mars.loop()
  299. self.day_period_mars.loop()
  300. # end RotatePlanets()
  301. # end class world
  302. w = World()
  303. base.run()