Author: ArcRiley
Date: 2007-07-06 18:35:26 -0400 (Fri, 06 Jul 2007)
New Revision: 386

Modified:
   trunk/pysoy/src/textures/Image.pxi
   trunk/pysoy/src/textures/Texture.pxi
   trunk/pysoy/src/textures/soy.textures.pxd
   trunk/pysoy/src/widgets/Canvas.pxi
   trunk/pysoy/src/widgets/Projector.pxi
   trunk/pysoy/src/widgets/StackZ.pxi
   trunk/pysoy/src/widgets/Widget.pxi
   trunk/pysoy/src/widgets/soy.widgets.pxd
   trunk/pysoy/tests/tex_pyramid.py
Log:
textures now have an aspect ratio, pass it to Canvas w/o explicit aspect


Modified: trunk/pysoy/src/textures/Image.pxi
===================================================================
--- trunk/pysoy/src/textures/Image.pxi  2007-07-06 22:19:01 UTC (rev 385)
+++ trunk/pysoy/src/textures/Image.pxi  2007-07-06 22:35:26 UTC (rev 386)
@@ -45,6 +45,7 @@
       ly = self._squareup(img.size[1])
       if lx != img.size[0] or ly != img.size[1] :
         idata = idata.resize((lx, ly))
+      self._aspect = float(img.size[0]) / float(img.size[1])
     self._resize(modes[img.mode], lx, ly, 1)
     for b from 0 <= b < lx*ly :
       d = idata[b]

Modified: trunk/pysoy/src/textures/Texture.pxi
===================================================================
--- trunk/pysoy/src/textures/Texture.pxi        2007-07-06 22:19:01 UTC (rev 
385)
+++ trunk/pysoy/src/textures/Texture.pxi        2007-07-06 22:35:26 UTC (rev 
386)
@@ -32,8 +32,26 @@
     self._oFormats[3] = gl.GL_RGB
     self._oFormats[4] = gl.GL_RGBA
 
+
+  property aspect :
+    '''Texture's aspect ratio
+
+    This is the ratio of width:height which, if set, will be maintained.
+
+    Defaults to disabled (0.0)
+    '''
+    def __get__(self) :
+      return self._aspect
+    def __set__(self, aspect) :
+      if aspect < 0.0 :
+        raise ValueError('aspect cannot be negetive')
+      self._aspect = aspect
+    def __del__(self) :
+      self._aspect = 0.0
+
+
   property size :
-    '''Texture.size
+    '''Texture's size
 
     Size of the image file
     '''

Modified: trunk/pysoy/src/textures/soy.textures.pxd
===================================================================
--- trunk/pysoy/src/textures/soy.textures.pxd   2007-07-06 22:19:01 UTC (rev 
385)
+++ trunk/pysoy/src/textures/soy.textures.pxd   2007-07-06 22:35:26 UTC (rev 
386)
@@ -30,6 +30,7 @@
   cdef int          _width
   cdef int          _height
   cdef int          _depth
+  cdef float        _aspect
   cdef gl.GLubyte  *_texels
   #
   # Lookup Arrays

Modified: trunk/pysoy/src/widgets/Canvas.pxi
===================================================================
--- trunk/pysoy/src/widgets/Canvas.pxi  2007-07-06 22:19:01 UTC (rev 385)
+++ trunk/pysoy/src/widgets/Canvas.pxi  2007-07-06 22:35:26 UTC (rev 386)
@@ -22,7 +22,7 @@
 
      This is a simple widget used to display a texture.
   '''
-  def __new__(self, parent, margin=None, aspect=0.0, 
+  def __new__(self, parent, margin=None, aspect=-1.0, 
               texture=None, 
               *args, **keywords) :
     self._verts[1].px = 1.0
@@ -41,6 +41,8 @@
     self._faces[1].c = 0
     if isinstance(texture, soy.textures.Texture) or texture == None :    
       self._texture = texture      
+      if aspect < 0.0 :
+        self._aspect = (<soy.textures.Texture> self._texture)._aspect
 
   property texture :
     '''Canvas texture

Modified: trunk/pysoy/src/widgets/Projector.pxi
===================================================================
--- trunk/pysoy/src/widgets/Projector.pxi       2007-07-06 22:19:01 UTC (rev 
385)
+++ trunk/pysoy/src/widgets/Projector.pxi       2007-07-06 22:35:26 UTC (rev 
386)
@@ -18,7 +18,7 @@
 # $Id$
 
 cdef class Projector(Widget) :
-  def __new__(self, parent, margin=None, aspect=0.0, 
+  def __new__(self, parent, margin=None, aspect=-1.0, 
               camera=None,
               *args, **keywords) :
     self._camera = camera
@@ -35,8 +35,8 @@
     cdef float _aspect
     if not self._connected :
       return
-    if self._aspectRatio :
-      _aspect = self._aspectRatio
+    if self._aspect :
+      _aspect = self._aspect
     else :
       _aspect = (<float> self._width) / (<float> self._height)    
     gl.glViewport(self._xpos, self._ypos, self._width, self._height)

Modified: trunk/pysoy/src/widgets/StackZ.pxi
===================================================================
--- trunk/pysoy/src/widgets/StackZ.pxi  2007-07-06 22:19:01 UTC (rev 385)
+++ trunk/pysoy/src/widgets/StackZ.pxi  2007-07-06 22:35:26 UTC (rev 386)
@@ -23,7 +23,7 @@
      This stacking widget overlaps children as "layers".  This behavior is
      identical to how a soy.Window stacks top-level widgets.
   '''
-  def __new__(self, parent, margin=None, aspect=0.0, 
+  def __new__(self, parent, margin=None, aspect=-1.0, 
               *args, **keywords) :
     self._widgets = soy._internals.Children()
 

Modified: trunk/pysoy/src/widgets/Widget.pxi
===================================================================
--- trunk/pysoy/src/widgets/Widget.pxi  2007-07-06 22:19:01 UTC (rev 385)
+++ trunk/pysoy/src/widgets/Widget.pxi  2007-07-06 22:35:26 UTC (rev 386)
@@ -23,20 +23,12 @@
      A Widget is a 2d section/layer in a soy.window.Window.  This is the base 
      class which all widgets inherit, it has no render functionality.
   '''
-  def __new__(self, parent, margin=None, aspect=0.0, 
+  def __new__(self, parent, margin=None, aspect=-1.0, 
               *args, **keywords) :
-    if margin :
-      if type(margin) == int :
-        margin = (margin, margin, margin, margin)      
-      if len(margin)!=4 or type(margin[0])!=int or type(margin[1])!=int \
-                        or type(margin[2])!=int or type(margin[3])!=int :
-        raise TypeError('margin must be a (int, int, int, int)')
-      self._marginTop    = margin[0]
-      self._marginRight  = margin[1]
-      self._marginBottom = margin[2]
-      self._marginLeft   = margin[3]
+    if not self._setMargin(margin) :
+      raise TypeError('margin must be a (int, int, int, int)')
     if aspect > 0.0 :
-      self._aspectRatio = aspect
+      self._aspect = aspect
     if isinstance(parent, soy._core.Window) :
       self._window = parent
       self._parent = None
@@ -69,6 +61,26 @@
     return '<Widget in Window id %d>' % (self._window._windowID)
 
 
+  cdef int _setMargin(self, object margin) :
+    if not margin :
+      margin = (0, 0, 0, 0)
+    if type(margin) == int :
+      margin = (margin, margin, margin, margin)      
+    elif len(margin) == 1 :
+      margin = (margin[0], margin[0], margin[0], margin[0])
+    elif len(margin) == 2 :
+      margin = (margin[0], margin[1], margin[0], margin[1])
+    elif len(margin) == 3 :
+      margin = (margin[0], margin[1], margin[2], margin[1])
+    if len(margin)!=4 or type(margin[0])!=int or type(margin[1])!=int \
+                      or type(margin[2])!=int or type(margin[3])!=int :
+      return 0
+    self._marginTop    = margin[0]
+    self._marginRight  = margin[1]
+    self._marginBottom = margin[2]
+    self._marginLeft   = margin[3]
+    return 1
+
   cdef void _render(self) :
     return
 
@@ -81,13 +93,13 @@
     _mr = _ml + _width  - (self._marginLeft + self._marginRight)
     _mb = _ypos + self._marginBottom
     _mt = _mb + _height - (self._marginBottom + self._marginTop)
-    if self._aspectRatio :
+    if self._aspect :
       _aspect = <float> (_mr - _ml) / <float> (_mt - _mb)
-      if _aspect < self._aspectRatio :
+      if _aspect < self._aspect :
         # Wide aspect == full width, aligned by height
         self._xpos = _ml
         self._width = _mr - _ml
-        self._height = <int> (<float> self._width / self._aspectRatio)
+        self._height = <int> (<float> self._width / self._aspect)
         if self._alignHeight < 0 :
           self._ypos = _mb
         else :
@@ -100,7 +112,7 @@
         # Narrow aspect == full height, aligned by width
         self._ypos = _mb
         self._height = _mt - _mb
-        self._width = <int> (self._aspectRatio * <float> self._height)
+        self._width = <int> (self._aspect * <float> self._height)
         if self._alignWidth < 0 :
           self._xpos = _ml
         else :
@@ -129,19 +141,12 @@
       return (self._marginTop, self._marginRight, 
               self._marginBottom, self._marginLeft)
     def __set__(self, margin) :
-      if type(margin) == int :
-        margin = (margin, margin, margin, margin)      
-      if len(margin)!=4 or type(margin[0])!=int or type(margin[1])!=int \
-                        or type(margin[2])!=int or type(margin[3])!=int :
+      if not self._setMargin(margin) :
         raise TypeError('margin must be a (int, int, int, int)')
-      self._window._widgets.lock()
-      self._marginTop    = margin[0]
-      self._marginRight  = margin[1]
-      self._marginBottom = margin[2]
-      self._marginLeft   = margin[3]
-      self._window._widgets.unlock()
-  
+    def __del__(self) :
+      self._setMargin(None)
 
+
   property aspect :
     '''Widget's aspect ratio
 
@@ -150,13 +155,13 @@
     Defaults to disabled (0.0)
     '''
     def __get__(self) :
-      return self._aspectRatio
+      return self._aspect
     def __set__(self, aspect) :
       if aspect < 0.0 :
         raise ValueError('aspect cannot be negetive')
-      self._aspectRatio = aspect
+      self._aspect = aspect
     def __del__(self) :
-      self._aspectRatio = 0.0
+      self._aspect = 0.0
 
 
   property position :

Modified: trunk/pysoy/src/widgets/soy.widgets.pxd
===================================================================
--- trunk/pysoy/src/widgets/soy.widgets.pxd     2007-07-06 22:19:01 UTC (rev 
385)
+++ trunk/pysoy/src/widgets/soy.widgets.pxd     2007-07-06 22:35:26 UTC (rev 
386)
@@ -32,13 +32,16 @@
   cdef int                      _ypos
   cdef int                      _width
   cdef int                      _height
+  cdef float                    _aspect
   cdef int                      _marginTop
   cdef int                      _marginRight
   cdef int                      _marginBottom
   cdef int                      _marginLeft
-  cdef float                    _aspectRatio
   cdef int                      _alignWidth
   cdef int                      _alignHeight
+  # Helper Functions
+  cdef int                      _setMargin(self, object)
+  # _coreLoop Functions
   cdef void                     _render(self)
   cdef void                     _resize(self, int, int, int, int)
 

Modified: trunk/pysoy/tests/tex_pyramid.py
===================================================================
--- trunk/pysoy/tests/tex_pyramid.py    2007-07-06 22:19:01 UTC (rev 385)
+++ trunk/pysoy/tests/tex_pyramid.py    2007-07-06 22:35:26 UTC (rev 386)
@@ -20,9 +20,9 @@
 cam = soy.bodies.Camera(sce)
 cam.position = (0.0, 0.0, 5.0)
 
-ca1 = soy.widgets.Canvas(win, margin=10, texture=lava)
+ca1 = soy.widgets.Canvas(win, margin=10, aspect=0.0, texture=lava)
 pro = soy.widgets.Projector(win, camera=cam)
-ca2 = soy.widgets.Canvas(win, aspect=64.0/96.0, texture=face)
+ca2 = soy.widgets.Canvas(win, texture=face)
 pyr = soy.bodies.Pyramid(sce, luma)
 pyr.rotation = (1.0, 1.0, 0.0)
 

_______________________________________________
PySoy-SVN mailing list
[email protected]
http://www.pysoy.org/mailman/listinfo/pysoy-svn

Reply via email to