pastebin - collaborative debugging tool
alicesucks.kpaste.net RSS


uoae
Posted by Anonymous on Sun 19th Dec 2010 20:15
raw | new post

  1. '''
  2. 1. Create an image in paint or something with aspect ratio 4:3.
  3. 2. Make a billboard with it using: File > Make Billboard... in Alice.
  4. 3. Move and resize it so it covers the whole screen.
  5. 4. Place the script "main(<billboard name>)" in world.my_first_method. If the name starts with a number, place an underscore before it.
  6. 5. Create a new world method, handleMouseClick
  7. 6. Create a "When mouse is clicked on" event with the billboard and world.handleMouseClick
  8. 7. In the handleMouseClick method, call the script "handleMouseClick()".
  9. 8 (Optional). Set the ambientLightColor world variable to "no color" to have white cells instead of grey.
  10. '''
  11. # apparently nested functions can't see outside variables without this, silly jython
  12. from __future__ import nested_scopes
  13.  
  14. # core classes
  15. from edu.cmu.cs.stage3.alice.core import *
  16. from edu.cmu.cs.stage3.alice.core.event import *
  17. from edu.cmu.cs.stage3.alice.core.response import *
  18. from edu.cmu.cs.stage3.alice.core.property import *
  19. from edu.cmu.cs.stage3.alice.core.question import *
  20.  
  21. # constants
  22. from edu.cmu.cs.stage3.alice.core.Direction import *
  23. from edu.cmu.cs.stage3.alice.core.style.TraditionalAnimationStyle import *
  24.  
  25. # image
  26. from java.awt import *
  27. from java.awt.image import *
  28. from java.awt.geom import *
  29.  
  30. # for profiling
  31. import time
  32.  
  33. # misc
  34. from edu.cmu.cs.stage3.alice.authoringtool import *
  35. from edu.cmu.cs.stage3.alice.authoringtool.util import *
  36.  
  37. def main(screen):
  38.     def add(key, value):
  39.         globals()[key] = value
  40.     # check if vars already defined
  41.     if not globals().get('dims'):
  42.         # the Billboard object that represents the screen
  43.         add('screenObj', screen)
  44.         # holds all pixel dimensions
  45.         add('dims', Generic())
  46.         add('screenTexture', TextureMap())
  47.     # the actual Component of the screen
  48.     add('screenComponent', world.camera.renderTarget.getAWTComponent())
  49.     # set dimension constants
  50.     _size = screenComponent.getSize()
  51.  
  52.     dims.WIDTH = _size.width
  53.     dims.HEIGHT = _size.height
  54.     # make a square grid
  55.     dims.GRID_WIDTH = dims.HEIGHT
  56.     # get as close to 20 px per cell as possible, is a float
  57.     dims.CELL_WIDTH = dims.GRID_WIDTH / round(dims.GRID_WIDTH / 20)
  58.  
  59.     dims.MENU_WIDTH = dims.WIDTH - dims.GRID_WIDTH
  60.  
  61.     # store images
  62.     makeImage('main', dims.WIDTH, dims.HEIGHT)
  63.     makeGrids()
  64.     makeImage('menu', dims.MENU_WIDTH, dims.HEIGHT)
  65.  
  66.     # initialize grids
  67.     for gridX in xrange(int(dims.GRID_WIDTH / dims.CELL_WIDTH)):
  68.         for gridY in xrange(int(dims.HEIGHT / dims.CELL_WIDTH)):
  69.             changeCell(gridX, gridY, OFF)
  70.             changeCell(gridX, gridY, OFF, false)
  71.     updateGrid()
  72.  
  73. ### CLASSES
  74. class Generic: # TODO: find a better way to have dicts that work with . instead of []
  75.     pass
  76.  
  77. ### FUNCTIONS
  78. # images
  79.  
  80. images = {}
  81.  
  82. def makeImage(name, w, h):
  83.     '''
  84.     adds an (empty BufferedImage, its Graphics2D) tuple to images if doesn't already exist
  85.     '''
  86.     if name not in images.keys():
  87.         image = AuthoringTool.getHack().getJAliceFrame().getGraphicsConfiguration().createCompatibleImage(w, h)
  88.         modifiable = image.createGraphics()
  89.         images[name] = (image, modifiable)
  90.  
  91. def getImage(name):
  92.     return images[name][0]
  93.  
  94. def getModifiable(name):
  95.     return images[name][1]
  96.  
  97. # textures
  98. def setTexture(object, image):
  99.     '''
  100.     takes java.awt.Image, makes it texture of object
  101.  
  102.     0 - 0.001
  103.     '''
  104.     # these two lines prevent memory leaks, probably -_-
  105.     object.getSceneGraphAppearance().getDiffuseColorMap().setImage(None)
  106.     object.getSceneGraphAppearance().getDiffuseColorMap().setBonus(None)
  107.     # must remove image to set it again
  108.     screenTexture.propertyChanged(PropertyEvent(screenTexture.image, None))
  109.     screenTexture.propertyChanged(PropertyEvent(screenTexture.image, image))
  110.     object.propertyChanged(PropertyEvent(object._diffuseColorMap, screenTexture))
  111.  
  112. # grid
  113. def makeGrids():
  114.     makeImage('grid0', dims.GRID_WIDTH, dims.HEIGHT)
  115.     makeImage('grid1', dims.GRID_WIDTH, dims.HEIGHT)
  116.  
  117. curGrid = 0
  118.  
  119. def getCurGridName():
  120.     return 'grid' + str(curGrid)
  121.  
  122. def getOtherGridName():
  123.     return 'grid' + str(1 - curGrid)
  124.  
  125. def switchGrids():
  126.     global curGrid
  127.     curGrid = 1 - curGrid
  128.  
  129. def isOn(gridX, gridY, isCurGrid = true):
  130.     '''
  131.     0.0000333 over 10000 runs
  132.     '''
  133.     if isCurGrid:
  134.         name = getCurGridName()
  135.     else:
  136.         name = getOtherGridName()
  137.     x, y = gridX * dims.CELL_WIDTH, gridY * dims.CELL_WIDTH
  138.     return getImage(name).getRGB(int(x + dims.CELL_WIDTH / 2), int(y + dims.CELL_WIDTH / 2)) == Color.BLACK.getRGB()
  139.  
  140. TOGGLE = 0
  141. ON = 1
  142. OFF = 2
  143. def changeCell(gridX, gridY, setting = TOGGLE, isCurGrid = true):
  144.     '''
  145.     0 - 0.001, 0.000071 over 10000 runs
  146.     '''
  147.     if isCurGrid:
  148.         name = getCurGridName()
  149.     else:
  150.         name = getOtherGridName()
  151.     x, y = gridX * dims.CELL_WIDTH, gridY * dims.CELL_WIDTH
  152.     rect = Rectangle2D.Double(x, y, dims.CELL_WIDTH, dims.CELL_WIDTH)
  153.     g2d = getModifiable(name)
  154.     if setting == ON:
  155.         g2d.setPaint(Color.BLACK)
  156.     elif setting == OFF:
  157.         g2d.setPaint(Color.WHITE)
  158.     else:
  159.         if isOn(gridX, gridY, isCurGrid):
  160.             g2d.setPaint(Color.WHITE)
  161.         else:
  162.             g2d.setPaint(Color.BLACK)
  163.     g2d.fill(rect)
  164.     g2d.setPaint(Color.BLACK)
  165.     g2d.draw(rect)
  166.  
  167. def updateGrid():
  168.     '''
  169.     blit current grid to main image, then update it as the texture
  170.  
  171.     0.001 - 0.002
  172.     '''
  173.     g2d = getModifiable('main')
  174.     g2d.drawImage(getImage(getCurGridName()), None, 0, 0)
  175.     setTexture(screenObj, getImage('main'))
  176.  
  177. def runOneStep():
  178.     start = time.time()
  179.     gridW = int(dims.GRID_WIDTH / dims.CELL_WIDTH)
  180.     gridH = int(dims.HEIGHT / dims.CELL_WIDTH)
  181.     pxArray = getImage(getCurGridName()).getRaster().getDataBuffer().getData()
  182.     def _isOn(gridX, gridY):
  183.         x, y = gridX * dims.CELL_WIDTH, gridY * dims.CELL_WIDTH
  184.         return pxArray[int(x + dims.CELL_WIDTH / 2) + int(y + dims.CELL_WIDTH / 2) * dims.GRID_WIDTH] == Color.BLACK.getRGB()
  185.     for gridX in xrange(gridW):
  186.         for gridY in xrange(gridH):
  187.             count = 0
  188.             curIsOn = _isOn(gridX, gridY)
  189.             if curIsOn:
  190.                 for offX in xrange(-1, 2):
  191.                     for offY in xrange(-1, 2):
  192.                         if offX or offY:
  193.                             if _isOn((gridX + offX) % gridW, (gridY + offY) % gridH):
  194.                                 count += 1
  195.                                 if count == 4:
  196.                                     break
  197.                     if count == 4:
  198.                         break
  199.                 if count <= 1 or count == 4:
  200.                     changeCell(gridX, gridY, OFF, false)
  201.                 else:
  202.                     changeCell(gridX, gridY, ON, false)
  203.             else:
  204.                 for offX in xrange(-1, 2):
  205.                     for offY in xrange(-1, 2):
  206.                         if offX or offY:
  207.                             if _isOn((gridX + offX) % gridW, (gridY + offY) % gridH):
  208.                                 count += 1
  209.                                 if count == 4:
  210.                                     break
  211.                     if count == 4:
  212.                         break
  213.                 if count == 3:
  214.                     changeCell(gridX, gridY, ON, false)
  215.                 else:
  216.                     changeCell(gridX, gridY, OFF, false)
  217.     switchGrids()
  218.     updateGrid()
  219.     print time.time() - start
  220.  
  221. # mouse
  222. def handleMouseClick():
  223.     pos = screenComponent.getMousePosition()
  224.     changeCell(int(pos.x / dims.CELL_WIDTH), int(pos.y / dims.CELL_WIDTH))
  225.     updateGrid()
  226.  
  227. # misc
  228. def onScreen(obj):
  229.     world.bool = camera.canSee(obj, false)

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}





All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at