##2. Renderer subclasses srOpengl : the window now grabs the focus (which it DOESN'T NEED  in 
##order to receive mouse events) so that it can receive keyboard events, which are dispatched to 
##corresponding event calls according to a dictionary. CAVEAT: For some reason, the window 
##doesn't grab the focus right away, so that it is necessary to click back and forth between windows
##a few times before keypress events actually reach their proper destination. Renderer also keeps
##track of framerate and prints it out each frame.


from sropengl import *
import time
from compass import *

class Renderer(srOpengl):

    def __init__(self, master=None, cnf={}, **kw):
        srOpengl.__init__(self,cnf,kw)
        self.keydict = {'p':'fovplus','m':'fovminus','a':'do_a','b':'do_b','r':'backlessred','R':'backmorered','g':'backlessgreen','G':'backmoregreen','b':'backlessblue','B':'backmoreblue',}
        self.focus_set()
        self.bind('<KeyPress>', self.keypress)
        self.then = time.time()
	self.r_back = 0
        
        self.compass_north = Compass((50,0,0))
        self.compass_south = Compass((-50,0,0))
        self.compass_east = Compass((0,50,0))
        self.compass_west = Compass((0,-50,0))
	
    def redraw(self,dummy=None):
	now = time.time()
	dt = now - self.then
	fps = 1.0/dt
	print fps,'FPS; Camera At:',self.camera_x,self.camera_y,self.camera_z,'OOA at:',self.object_of_attention.x,self.object_of_attention.y,self.object_of_attention.z
	self.then=now
	self.draw()
	

    def keypress(self,event):
        try:
            k = self.keydict[event.keysym]
	    print k
            exec('self.'+k+'()')
        except KeyError:
	    print event.keysym
            print 'ORDERS ARE UNCLEAR'

    def do_a(self):
        print 'YES'
	
    def do_b(self):
        print 'no'
    
    def backlessred(self):
        if self.r_back > 0.0:
            self.r_back = self.r_back - .05

    def backmorered(self):
        if self.r_back < 1.0:
            self.r_back = self.r_back + .05
	
    def backlessgreen(self):
	if self.g_back > 0.0:
            self.g_back = self.g_back - .05

    def backmoregreen(self):
	if self.g_back < 1.0:
            self.g_back = self.g_back + .05

    def backlessblue(self):
	if self.b_back > 0.0:
            self.b_back = self.b_back - .05

    def backmoreblue(self):
	if self.b_back < 1.0:
            self.b_back = self.b_back + .05

    def fovplus(self):
	self.fovy = self.fovy+2
	print self.fovy 

    def fovminus(self):
	self.fovy = self.fovy-2
	print 'FOV!!!!!!!!!!!!',self.fovy 
	
   
