Want more features on Pastebin? Sign Up, it's FREE!
Guest

my_camera.py

By: jstokes75 on Feb 26th, 2013  |  syntax: Python  |  size: 3.19 KB  |  views: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from __future__ import division
  2. import math
  3. from math import pi , tan, radians, sqrt, sin, cos
  4. import pyglet
  5. from pyglet.gl import *
  6. from pyglet.window import key, mouse, Window
  7.  
  8. class camera_move_target(object):
  9.     def __init__(self, camera):
  10.         self.x = camera.x
  11.         self.y = camera.y
  12.         self.scale = camera.scale
  13.        
  14. class camera(object):
  15.     def __init__(self,x, y, w, h):
  16.         self.x = x
  17.         self.y = y
  18.         self.h = h
  19.         self.w = w
  20.         self.page_w = self.w
  21.         self.near = 1
  22.         self.far = -1
  23.         self.speed = 0.1
  24.         self.scale = 1.0
  25.         self.angle = 0
  26.         self.max_world_x = self.w
  27.         self.target = camera_move_target(self)
  28.  
  29.     def pan(self, x, y):
  30.         self.target.x -= x
  31.         self.target.y -= y
  32.        
  33.     def pan_lr(self, length):
  34.         self.target.x += length
  35.        
  36.     def goto_x(self, x_location):
  37.         self.target.x = x_location
  38.        
  39.     def pan_ud(self, length):
  40.         self.target.y += length
  41.        
  42.     def zoom(self, scale):
  43.         self.target.scale += scale
  44.        
  45.     def update(self):
  46.         #print self.x, self.target.x
  47.  
  48.         if self.x > self.max_world_x - self.w/2 :
  49.             self.x = self.x - 1
  50.             self.target.x = self.max_world_x - self.w/2
  51.         elif self.x < 0 + self.w/2:
  52.             self.target.x = 0 + self.w/2
  53.             self.x = self.x + 1.0
  54.         else:
  55.             self.x += (self.target.x - self.x) * self.speed
  56.    
  57.         self.y += (self.target.y - self.y) * self.speed
  58.         self.scale += int(self.target.scale - self.scale) * self.speed
  59.        
  60.     def update_no_delay(self):
  61.         print "no_delay"
  62.         #self.target.x = 0
  63.         #self.target.y = 0
  64.         print self.x, self.target.x
  65.         if self.x > self.max_world_x - self.w/2 :
  66.             self.x = self.x - 1
  67.             self.target.x = self.max_world_x - self.w/2
  68.         elif self.x < 0 + self.w/2:
  69.             self.target.x = 0 + self.w/2
  70.             self.x = self.x + 1.0
  71.         else:
  72.             self.x = int(self.target.x - self.x)
  73.    
  74.         self.y = int(self.target.y - self.y)
  75.         self.scale += int(self.target.scale - self.scale)
  76.        
  77.     def project(self, w, h):
  78.         glMatrixMode(GL_PROJECTION)
  79.         #gluPerspective(45, 1.0 * w/h, 0.1, 100.0)
  80.         glLoadIdentity()
  81.         glOrtho( self.x - self.w /2 , self.x + self.w /2, self.y - self.h/2 , self.y + self.h/2 ,  -10, 10);
  82.         #gluOrtho2D( self.x - self.w /2 , self.x + self.w /2, self.y - self.h/2 , self.y + self.h/2 );
  83.         glMatrixMode(GL_MODELVIEW)
  84.         glLoadIdentity()
  85.  
  86.            
  87.     def headsup(self, width, height):
  88.         glMatrixMode(GL_PROJECTION)
  89.         glLoadIdentity()
  90.         glOrtho(0, self.w ,  0, self.h, -10, 10)
  91.         glMatrixMode(GL_MODELVIEW)
  92.         glLoadIdentity()
  93.        
  94.     def hud_mode(self, width, height):
  95.         glMatrixMode(GL_PROJECTION)
  96.         glLoadIdentity()
  97.         gluOrtho2D(0, width, 0, height)
  98.         glMatrixMode(GL_MODELVIEW)
  99.         glLoadIdentity()
  100.  
  101.     def calc_pos(self, mx, my):
  102.         rx = ( mx + self.x - self.w/2 )
  103.         ry = ( my +self.y - self.h/2 )
  104.         return rx, ry
clone this paste RAW Paste Data