"""
very simple CocosNodes subclasses
They draw in open gl inmediate mode for clarity, so they are slow.
Better implementations would use display lists or batches.

A CocosNode designed to draw something must care about:

1. get or sintethize the vertices
2. get additional parameters ( colors, textures,...)
3. draw:
     i. set the appropriate gl state.
    ii. apply the transform, caring to push-pop the matrix
   iii. use the appropriate(d) gl function(s) to draw the object

If your CocosNode subclass breaks other stock CocosNode objects drawing, then
you may need to preserve some parts of gl state, using glPushAttrib with the
appropriate flags before setting state and poping after drawing.

"""
from pyglet.gl import *

import cocos
from cocos.director import director

def point_float(x,y):
    return float(x), float(y)

class SingleLine(cocos.cocosnode.CocosNode):
    def __init__(self, p1,p2, color = None):
        super(SingleLine,self).__init__()
        self.vertexes = [point_float(*p1),point_float(*p2)]
        if color is None:
            color = (255,255,255,255)
        elif len(color)==3:
            color = tuple(list(color).append(255))
        self.color = color

    def draw(self):
        glPushMatrix()
        self.transform()
        glBegin(GL_LINES)
        glColor4ub( *self.color )
        for v in self.vertexes:
            glVertex2f(*v)
        glEnd()
        glPopMatrix()

class PointBag(cocos.cocosnode.CocosNode):
    def __init__(self, pointsize=4, color=None):
        super(PointBag,self).__init__()
        self.pointsize = pointsize
        if color is None:
            color = (255,255,255,255)
        elif len(color)==3:
            color = tuple(list(color).append(255))
        self.color = color
        self.vertexes = []

    def add_xy(self, x,y):
        self.vertexes.append(point_float(x,y))

    def extend_points(self, points):
        points = [point_float(*p) for p in points]
        self.vertexes.extend(points)

    def set_state(self):
        glPointSize(self.pointsize)
        glColor4ub(*self.color)

    def draw(self):
        glPushMatrix()
        self.transform()
        self.set_state()
        glBegin(GL_POINTS)
        for p in self.vertexes:
            glVertex2f(*p)
        glEnd()
            
        glPopMatrix()

class SingleQuad(cocos.cocosnode.CocosNode):
    def __init__(self,p1,p2,p3,p4, color=None):
        super(SingleQuad,self).__init__()
        #ensure float coords
        self.vertexes = [point_float(*p1),point_float(*p2),point_float(*p3),point_float(*p4)]
        if color is None:
            color = (255,255,255,255)
        elif len(color)==3:
            color = tuple(list(color).append(255))
        self.color = color

    def draw(self):
        glPushMatrix()
        self.transform()
        glBegin(GL_QUADS)
        glColor4ub( *self.color )
        for v in self.vertexes:
            glVertex2f(*v)
        glEnd()
        glPopMatrix()


    

director.init( width=640, height=480, resizable=False )
director.window.set_caption('example simple cocosnodes')

director.show_FPS = True
scene = cocos.scene.Scene()
test_layer = cocos.layer.Layer()

# adding some quads
for i in xrange(0,91,15):
    obj = SingleQuad( (0,0),(250,150), (500,0),(250,-150), color=(255,0,0,128) )
    obj.rotation = -i
    test_layer.add(obj, z=0)
    
# adding some lines
for i in xrange(3):
    test_layer.add( SingleLine( ( 50, 100+i*50), (590, 100+i*50), color=(0,0,255,255)), z=1) 

# adding some green points:
obj = PointBag(color=(0,255,0,255))
obj.extend_points( [(i, 100) for i in xrange(0,500,50)])
test_layer.add(obj, z=2)

# adding some yellow points    
obj = PointBag(color=(255,255,0,255))
obj.extend_points( [(i, 400) for i in xrange(0,500,50)])
test_layer.add(obj, z=3)

scene.add(test_layer)
director.run( scene )

        
