wind.py

"""Animated leave-like polygons.
   by stephan)
"""
from slut import *
from random import randint

class Atlantis(World):
    def onSetup(self):
        self.name = "Form Follows Computation Here"
        self.width = 400
        self.height = 300
        self.fullscreen = False

        #globally set some openGL states
        glClearColor(0.0, 0.0, 0.0, 1.0)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        glDisable(GL_DEPTH_TEST)

        #generate a lot of sprites and set them in motion
        #with semi-random thrusts and tweens
        for x in range(20):
            Polygon(self, 'gen' + str(x))
            self.sprites['gen' + str(x)].moveBy(SineTween(
                randint(-x, x), randint(-x, x), randint(-x, x), 3.0))
            self.sprites['gen' + str(x)].orbitBy(Thrust(0.0, randint(-x, 0), 0.0))
            self.sprites['gen' + str(x)].rotBy(
                Thrust(randint(0, 999*x)/199.0,
                    randint(0, 999*x)/199.0, randint(0, 999*x)/199.0))
            self.sprites['gen' + str(x)].enableMouseEvents()
        self.sprite_focused = None

        #add some typography and a box that 
        #will track the clicked sprite
        TagText(self, 'tag', 24, 'times')
        self.tags['tag'].setText('TAG*')
        Box(self, 'box', 0, 100, 50, 50)
        self.tags['box'].setColor(1.0, 0.0, 0.0, 1.0)

        #define camera position and movement
        self.camera.moveBy(SineTween(0.0, 0.0, -20.0, 5.0))
        self.camera.clip_far = 200.0  #increase far clipping plane
        #self.camera.moveTo(0.0, 0.0, -20.0)

        #make a mpeg movie
        #from slut.context import MpegMovieMaker
        #self.addContext(MpegMovieMaker(0, 500))


    def setFocus(self, sprite_obj):
        if self.sprite_focused is not None:
            self.sprite_focused.disableTracking()
            self.sprite_focused.setColor(1.0, 1.0, 1.0, 0.5)
            self.sprite_focused.scaleTo(SineTween(1.0, 1.0, 1.0, 0.5))
        self.sprite_focused = sprite_obj
        self.sprite_focused.enableTracking()
        self.sprite_focused.setColor(1.0, 0.0, 0.0, 0.8)
        self.sprite_focused.scaleTo(SineTween(4.0, 4.0, 4.0, 0.5))


    def onDraw(self):
        p1 = self.sprite_focused
        if p1 is not None:
            self.tags['box'].setVertices( p1.min_x, p1.min_y,
                p1.max_x-p1.min_x, p1.max_y-p1.min_y)
            self.tags['tag'].moveTo(p1.max_x, p1.min_y)



class Polygon(Sprite):
    def __init__(self, parent, name):
        Sprite.__init__(self, parent, name)
        self._color = (1.0, 1.0, 1.0, 0.3)

    def onDraw(self):
        glColor4f(self._color[0], self._color[1],
            self._color[2], self._color[3])
        glBegin(GL_POLYGON)
        glVertex3f(0.0, 0.0, 0.0)
        glVertex3f(1.0, 0.0, 0.0)
        glVertex3f(1.0, 1.0, 0.0)
        glVertex3f(-0.5, 1.5, 0.0)
        glVertex3f(0.0, 0.0, 0.0)
        glEnd()

    def onMouseIn(self, event):
        if not glo.world.sprite_focused is self:
            self.setColor(1.0, 1.0, 1.0, 1.0)

    def onMouseOut(self, event):
        if not glo.world.sprite_focused is self:
            self.setColor(1.0, 1.0, 1.0, 0.3)

    def onMouseButtonDown(self, event):
        glo.world.setFocus(self)

    def setColor(self, r, g, b, a):
        self._color = (r, g, b, a)



atlantis = Atlantis()
atlantis.run()
Initiated by Stephan Hechenberger
Thanks to CADRE's 103