######################################################
# Kambi: this script is based on Blender manual,
# exhanced/changed for various tests.
######################################################

import Blender
# Kambi reimplemented using new Mesh module
from Blender import Mesh

from Blender import Draw
from Blender import BGL
#from Blender.BGL import *
#from Blender.Draw import *

import math
#from math import *

# Polygon Parameters
T_NumberOfSides = Draw.Create(3)
T_Radius = Draw.Create(1.0)

# Events
EVENT_NOEVENT = 1
EVENT_DRAW = 2
EVENT_EXIT = 3

#####################################################
# GUI drawing
######################################################
def draw():
        global T_NumberOfSides
        global T_Radius
        global EVENT_NOEVENT,EVENT_DRAW,EVENT_EXIT

        ########## Titles
        BGL.glClear(BGL.GL_COLOR_BUFFER_BIT)

        # Kambi: fix (otherwise text has different color after shift+press
        # on some button (gray) or mousing over the screen (black)).
        # Note: I found that, like in OpenGL, glColor must be before glRasterPos
        # (glRasterPos fixes color for the raster).
        BGL.glColor3f(1.0, 1.0, 0.0)

        BGL.glRasterPos2d(8, 103)
        Draw.Text("Demo Polygon Script")

        ######### Parameters GUI Buttons
        BGL.glRasterPos2d(8, 83)
        Draw.Text("Parameters:")
        T_NumberOfSides = Draw.Number("No. of sides: ", EVENT_NOEVENT, 10, 55, 210, 18,
                T_NumberOfSides.val, 3, 20, "Number of sides of out polygon");
        T_Radius = Draw.Slider("Radius: ", EVENT_NOEVENT, 10, 35, 210, 18,
                T_Radius.val, 0.001, 20.0, 1, "Radius of the polygon");

        Draw.Create(50)
        Draw.Create(100)

        ######### Draw and Exit Buttons
        Draw.PushButton("Draw",EVENT_DRAW , 10, 10, 80, 18)
        Draw.PushButton("Exit",EVENT_EXIT , 140, 10, 80, 18)

        #name = "The Title %t|First Entry %x1|Second Entry %x2|Third Entry %x3"
        #menu = Draw.Menu(name, 2, 60, 120, 200, 40, 3, "Just a test menu.")

def event(evt, val):
        if (evt == Draw.QKEY and not val):
                Draw.Exit()

def bevent(evt):
        global T_NumberOfSides
        global T_Radius
        global EVENT_NOEVENT,EVENT_DRAW,EVENT_EXIT

        ######### Manages GUI events
        if (evt == EVENT_EXIT):
                Draw.Exit()
        elif (evt== EVENT_DRAW):
                Polygon(T_NumberOfSides.val, T_Radius.val)
                Blender.Redraw()
                Draw.Exit()

# Kambi: this registers events for draw, key/mouse press/release, button events.
# This also switches to "Scripts" window, and the callbacks registered work
# there. IOW, this registers events of "Scripts" window, switches to
# that window, and starts event loop.
Draw.Register(draw, event, bevent)

# Kambi: In Python you can mix function declarations "def" with normal
# code (Draw.Register above), just like in PHP the "def" is more-or-less
# a normal instruction that modifies the namespace.
# So function "Polygon" is defined after calling Draw.Register above,
# and that's no problem (Polygon is called by bevent, so it's not used
# immediately at Draw.Register call). This corresponds with manual
# statement "Python scope is lexical (e.g. constrained to single file), but
# resolved at runtime" (so function Polygon must be defined when it's called
# at runtime, not necessarily when it's used lexically).

def Polygon(NumberOfSides,Radius):

        ######### Creates a new mesh
        poly = Mesh.New() # New?

        ######### Populates it of vertices
        for i in range(0,NumberOfSides):
                phi = 3.141592653589 * 2 * i / NumberOfSides
                x = Radius * math.cos(phi)
                y = Radius * math.sin(phi)
                z = 0

                poly.verts.extend(x, y, z)

        ######### Adds a new vertex to the center
        poly.verts.extend(0.,0.,0.)

        ######### Connects the vertices to form faces
        for i in range(0,NumberOfSides):
                poly.faces.extend(
                        poly.verts[i],
                        poly.verts[(i+1)%NumberOfSides],
                        poly.verts[NumberOfSides])


        ######### Creates a new Object with the new Mesh
        Blender.Scene.GetCurrent().objects.new(poly, 'myObj')

