jstokes75

objects.py

Feb 26th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.28 KB | None | 0 0
  1. from __future__ import division
  2. import os,  math
  3. from math import pi , tan, radians, sqrt
  4. import pyglet
  5. from pyglet.gl import *
  6. from pyglet.image.codecs import *
  7.  
  8. class TextureEnableGroup(pyglet.graphics.Group):
  9.     def set_state(self):
  10.         glEnable(GL_TEXTURE_2D)
  11.  
  12.     def unset_state(self):
  13.         glDisable(GL_TEXTURE_2D)
  14.  
  15. texture_enable_group = TextureEnableGroup()
  16.  
  17. class TextureBindGroup(pyglet.graphics.Group):
  18.     def __init__(self, texture):
  19.         super(TextureBindGroup, self).__init__(parent=texture_enable_group)
  20.         texture.target = GL_TEXTURE_2D
  21.         self.texture = texture
  22.  
  23.     def set_state(self):
  24.         glBindTexture(GL_TEXTURE_2D, self.texture.id)
  25.  
  26.     # No unset_state method required.
  27.  
  28.     def __eq__(self, other):
  29.         return (self.__class__ is other.__class__ and
  30.                 self.texture == other.__class__)
  31.  
  32.  
  33.  
  34.  
  35. class l_items_vectors:
  36.     def __init__(self, x, y, z):
  37.         self.x = x
  38.         self.y = y
  39.         self.z = z
  40.         self.vectors = [x, y, z]
  41.  
  42. class l_items(object):
  43.     def __init__(self, name, execute,icon,  cat_type, x, y, z,  w, h , bc, tc,  batch):
  44.         self.name = name
  45.         self.group = None
  46.         self.execute = execute
  47.         self.icon = icon
  48.         self.cat_type = cat_type
  49.         self.batch = batch
  50.         self.x = float(x)
  51.         self.y = float(y)
  52.         self.z = float(0)
  53.         self.w = float(w)
  54.         self.h = float(h)
  55.        
  56.         self.box_verts = [self.x,self.y,self.z,self.x,self.y+self.h,self.z,self.x+self.w,self.y+self.h,self.z,self.x+self.w,self.y,self.z]
  57.         self.box_verts2d = [self.x,self.y,self.x,self.y+self.h,self.x+self.w,self.y+self.h,self.x+self.w,self.y]
  58.         self.box_icon = [self.x+(self.w/2)-32, self.y+16, self.x+(self.w/2)+32, self.y+16, self.x+(self.w/2)+32, self.y+80, self.x+(self.w/2)-32, self.y+80]
  59.         self.lable_x = self.x
  60.         self.lable_y = self.y
  61.         self.lable_z = self.z
  62.         self.color = (255, 255,  255, 255)
  63.         self.box_color = bc
  64.         self.highted = False
  65.         #self.fimg = pyglet.image.load('texture.png')
  66.         self.fimg = pyglet.image.Texture.create(512, 512, GL_RGBA)
  67.         if self.icon:
  68.             try:
  69.                 self.icon_image = pyglet.image.load(self.icon)
  70.                 #self.icon_image = pyglet.image.load(self.icon, decoder=PNGImageDecoder())
  71.  
  72.             except :
  73.                 try:
  74.                     self.icon_image = pyglet.image.load(self.icon, decoder=GdkPixbuf2ImageDecode())
  75.                 except :
  76.                     self.icon_image = pyglet.resource.image('no_icon.png')
  77.         else:
  78.             self.icon_image = pyglet.resource.image('no_icon.png')
  79.    
  80.         #itc = self.icon_image.get_texture()  
  81.         #gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)                                                                                                                              
  82.         #itc.width = 64                                                                                                                                                                  
  83.         #itc.height = 64                                                    
  84.         #self.icon_image.width = 64
  85.         #self.icon_image.height = 64
  86.         iiw, iih = self.icon_image.width, self.icon_image.height
  87.  
  88.        
  89.         self.fimg_tex = self.fimg.get_texture()
  90.         print self.fimg
  91.         #itc.blit_into(self.fimg_tex, 0, 0, 0)
  92.         #self.icon_image.blit_to_texture(self.fimg_tex.target, 0, int(self.w/2 - iiw/2), 8, 8)
  93.         #self.icon_image.blit_to_texture(self.fimg_tex.target, 0, 0, 8, 8)
  94.  
  95.         self.fimg_tex.width = 200
  96.         self.fimg_tex.height = 80
  97.         self.fimg = self.fimg.get_region(0, 0, int(self.w), int(self.h))
  98.  
  99.         self.fimg_tex = self.fimg_tex.get_transform(self, rotate=90)
  100.        
  101.         self.img_tex = self.icon_image.get_texture()
  102.         self.img_tex.width = 64
  103.         self.img_tex.height = 64
  104.         self.img_tex = self.img_tex.get_region(0, 0, 64,64)
  105.  
  106.         #self.img_tex = self.img_tex.get_transform(self, rotate=90)
  107.        
  108.         #self.group = pyglet.sprite.SpriteGroup(self.fimg_tex,blend_src=GL_SRC_ALPHA, blend_dest=GL_ONE)
  109.         self.group = TextureBindGroup(self.img_tex)
  110.         self.text_group = pyglet.graphics.OrderedGroup(1)
  111.         self.background_group = pyglet.graphics.OrderedGroup(0)
  112.  
  113.         self.draw_box()
  114.         self.draw_title()
  115.        
  116.     def draw_box(self):
  117.  
  118.         #self.vertex_list = self.batch.add(4, pyglet.gl.GL_QUADS, None ,
  119.          #                            ('v2f', self.box_verts2d),
  120.           #                           ('c4f', self.box_color*4)
  121.            #                          )
  122.                                      
  123.         self.vertex_list = self.batch.add(4, pyglet.gl.GL_QUADS,self.group ,
  124.                                           #('c4f', self.box_color*4),
  125.                                           ('v2f/static', self.box_icon),
  126.                                           ('t3f/static', self.img_tex.tex_coords))
  127.        
  128.         self.vertex_list = self.batch.add(4, pyglet.gl.GL_QUADS, None ,
  129.                                             ('v3f/static', self.box_verts),
  130.                                             ('c4f/static', self.box_color*4)
  131.  
  132.                                           )
  133.            
  134.     def draw_title(self):
  135.         if self.name:
  136.             self.text_label  = pyglet.text.Label(self.name, x=self.x+self.w//2, y=self.y+14,
  137.                                              anchor_y='top',anchor_x='center' ,
  138.                                              color=self.color, bold=True,
  139.                                              font_name ='FreeSans', font_size = 8,
  140.                                              batch = self.batch, group = self.text_group
  141.                                              )
  142.        
  143.            
  144.     def draw_icon(self):  
  145.         pass
  146.        
  147.     def get_xyz(self):
  148.         return self.x, self.y, self.z
  149.        
  150.     def get_box_verts(self):
  151.         return self.box_verts
  152.        
  153.     def draw_all(self):
  154.         batch.draw()
  155.    
  156.     def update(self):
  157.         self.box_verts = [self.x,self.y,self.z,self.x,self.y+self.h,self.z,self.x+self.w,self.y+self.h,self.z,self.x+self.w,self.y,self.z]
  158.         self.box_verts2d = [self.x,self.y,self.x,self.y+self.h,self.x+self.w,self.y+self.h,self.x+self.w,self.y]
  159.  
  160.         self.vertex_list.vertices = self.box_verts
  161.         self.vertex_list.colors = self.box_color*4
  162.        
  163. class top_titles(object):
  164.         def __init__(self, x, y , n , ww,  wh,   text, batch):
  165.             self.x = x
  166.             self.y = y
  167.             self.wh = wh
  168.             self.ww = ww
  169.             self.text = text
  170.             self.cx = (n/2)+self.x
  171.             self.batch = batch
  172.             self.label = pyglet.text.Label(self.text,
  173.                           font_name='FreeSans',
  174.                           font_size=24,
  175.                           x = self.cx, y= self.wh-(self.y /2),
  176.                           anchor_x='center', anchor_y='center',  batch = self.batch)
  177.                          
  178.         def update(self, x ):
  179.             self.x = x
  180.             self.label.x = self.x
  181.            
  182.         def get_center_x(self):
  183.             return self.cx
Add Comment
Please, Sign In to add comment