chiark / gitweb /
Some cleanup, move the animation code to openglGUI, move scaled quad code to opengl.
authordaid303 <daid303@gmail.com>
Wed, 10 Apr 2013 07:22:42 +0000 (09:22 +0200)
committerdaid303 <daid303@gmail.com>
Wed, 10 Apr 2013 07:22:42 +0000 (09:22 +0200)
Cura/gui/sceneView.py
Cura/gui/util/opengl.py
Cura/gui/util/openglGui.py

index d7b3bb941109cf1f17f1c80de5f0f414040ea005..9dea6ff4fa8c43e60b29e3b9b73b3c04ca0d784c 100644 (file)
@@ -24,26 +24,6 @@ from Cura.gui.util import previewTools
 from Cura.gui.util import opengl
 from Cura.gui.util import openglGui
 
-class anim(object):
-       def __init__(self, start, end, runTime):
-               self._start = start
-               self._end = end
-               self._startTime = time.time()
-               self._runTime = runTime
-
-       def isDone(self):
-               return time.time() > self._startTime + self._runTime
-
-       def getPosition(self):
-               if self.isDone():
-                       return self._end
-               f = (time.time() - self._startTime) / self._runTime
-               ts = f*f
-               tc = f*f*f
-               #f = 6*tc*ts + -15*ts*ts + 10*tc
-               f = tc + -3*ts + 3*f
-               return self._start + (self._end - self._start) * f
-
 class SceneView(openglGui.glGuiPanel):
        def __init__(self, parent):
                super(SceneView, self).__init__(parent)
@@ -118,7 +98,6 @@ class SceneView(openglGui.glGuiPanel):
 
                self.OnToolSelect(0)
                self.updateProfileToControls()
-               wx.EVT_IDLE(self, self.OnIdle)
 
        def ShowLoadModel(self, button):
                if button == 1:
@@ -162,15 +141,6 @@ class SceneView(openglGui.glGuiPanel):
 
                                shutil.copy(self._slicer.getGCodeFilename(), filename)
 
-       def OnIdle(self, e):
-               if self._animView is not None or self._animZoom is not None:
-                       self.Refresh()
-                       return
-               for obj in self._scene.objects():
-                       if obj._loadAnim is not None:
-                               self.Refresh()
-                               return
-
        def OnToolSelect(self, button):
                if self.rotateToolButton.getSelected():
                        self.tool = previewTools.toolRotate(self)
@@ -262,7 +232,7 @@ class SceneView(openglGui.glGuiPanel):
                                traceback.print_exc()
                        else:
                                for obj in objList:
-                                       obj._loadAnim = anim(1, 0, 1.5)
+                                       obj._loadAnim = openglGui.animation(self, 1, 0, 1.5)
                                        self._scene.add(obj)
                                        self._scene.centerAll()
                                        self._selectObject(obj)
@@ -287,11 +257,11 @@ class SceneView(openglGui.glGuiPanel):
                        self.updateProfileToControls()
                if zoom:
                        newViewPos = numpy.array([obj.getPosition()[0], obj.getPosition()[1], obj.getMaximum()[2] / 2])
-                       self._animView = anim(self._viewTarget.copy(), newViewPos, 0.5)
+                       self._animView = openglGui.animation(self, self._viewTarget.copy(), newViewPos, 0.5)
                        newZoom = obj.getBoundaryCircle() * 6
                        if newZoom > numpy.max(self._machineSize) * 3:
                                newZoom = numpy.max(self._machineSize) * 3
-                       self._animZoom = anim(self._zoom, newZoom, 0.5)
+                       self._animZoom = openglGui.animation(self, self._zoom, newZoom, 0.5)
 
        def updateProfileToControls(self):
                oldSimpleMode = self._isSimpleMode
@@ -332,7 +302,7 @@ class SceneView(openglGui.glGuiPanel):
                        self._objectLoadShader.release()
                        self._objectLoadShader = s
                        for obj in self._scene.objects():
-                               obj._loadAnim = anim(1, 0, 1.5)
+                               obj._loadAnim = openglGui.animation(self, 1, 0, 1.5)
                        self.Refresh()
 
        def OnMouseDown(self,e):
index 0ad7a09e61cde3f47f3c84495bac2865ccd2dcf9..6e7d30656dcf32919fe2660c40bc66645a62261e 100644 (file)
@@ -286,6 +286,109 @@ def glDrawTexturedQuad(x, y, w, h, texID, mirror = 0):
        glEnd()
        glPopMatrix()
 
+def glDrawStretchedQuad(x, y, w, h, cornerSize, texID):
+       tx0 = float(texID % 4) / 4
+       ty0 = float(int(texID / 4)) / 8
+       tx1 = tx0 + 0.25 / 2.0
+       ty1 = ty0 + 0.125 / 2.0
+       tx2 = tx0 + 0.25
+       ty2 = ty0 + 0.125
+
+       glPushMatrix()
+       glTranslatef(x, y, 0)
+       glEnable(GL_TEXTURE_2D)
+       glBegin(GL_QUADS)
+       #TopLeft
+       glTexCoord2f(tx1, ty0)
+       glVertex2f( cornerSize, 0)
+       glTexCoord2f(tx0, ty0)
+       glVertex2f( 0, 0)
+       glTexCoord2f(tx0, ty1)
+       glVertex2f( 0, cornerSize)
+       glTexCoord2f(tx1, ty1)
+       glVertex2f( cornerSize, cornerSize)
+       #TopRight
+       glTexCoord2f(tx2, ty0)
+       glVertex2f( w, 0)
+       glTexCoord2f(tx1, ty0)
+       glVertex2f( w - cornerSize, 0)
+       glTexCoord2f(tx1, ty1)
+       glVertex2f( w - cornerSize, cornerSize)
+       glTexCoord2f(tx2, ty1)
+       glVertex2f( w, cornerSize)
+       #BottomLeft
+       glTexCoord2f(tx1, ty1)
+       glVertex2f( cornerSize, h - cornerSize)
+       glTexCoord2f(tx0, ty1)
+       glVertex2f( 0, h - cornerSize)
+       glTexCoord2f(tx0, ty2)
+       glVertex2f( 0, h)
+       glTexCoord2f(tx1, ty2)
+       glVertex2f( cornerSize, h)
+       #BottomRight
+       glTexCoord2f(tx2, ty1)
+       glVertex2f( w, h - cornerSize)
+       glTexCoord2f(tx1, ty1)
+       glVertex2f( w - cornerSize, h - cornerSize)
+       glTexCoord2f(tx1, ty2)
+       glVertex2f( w - cornerSize, h)
+       glTexCoord2f(tx2, ty2)
+       glVertex2f( w, h)
+
+       #Center
+       glTexCoord2f(tx1, ty1)
+       glVertex2f( w-cornerSize, cornerSize)
+       glTexCoord2f(tx1, ty1)
+       glVertex2f( cornerSize, cornerSize)
+       glTexCoord2f(tx1, ty1)
+       glVertex2f( cornerSize, h-cornerSize)
+       glTexCoord2f(tx1, ty1)
+       glVertex2f( w-cornerSize, h-cornerSize)
+
+       #Right
+       glTexCoord2f(tx2, ty1)
+       glVertex2f( w, cornerSize)
+       glTexCoord2f(tx1, ty1)
+       glVertex2f( w-cornerSize, cornerSize)
+       glTexCoord2f(tx1, ty1)
+       glVertex2f( w-cornerSize, h-cornerSize)
+       glTexCoord2f(tx2, ty1)
+       glVertex2f( w, h-cornerSize)
+
+       #Left
+       glTexCoord2f(tx1, ty1)
+       glVertex2f( cornerSize, cornerSize)
+       glTexCoord2f(tx0, ty1)
+       glVertex2f( 0, cornerSize)
+       glTexCoord2f(tx0, ty1)
+       glVertex2f( 0, h-cornerSize)
+       glTexCoord2f(tx1, ty1)
+       glVertex2f( cornerSize, h-cornerSize)
+
+       #Top
+       glTexCoord2f(tx1, ty0)
+       glVertex2f( w-cornerSize, 0)
+       glTexCoord2f(tx1, ty0)
+       glVertex2f( cornerSize, 0)
+       glTexCoord2f(tx1, ty1)
+       glVertex2f( cornerSize, cornerSize)
+       glTexCoord2f(tx1, ty1)
+       glVertex2f( w-cornerSize, cornerSize)
+
+       #Bottom
+       glTexCoord2f(tx1, ty1)
+       glVertex2f( w-cornerSize, h-cornerSize)
+       glTexCoord2f(tx1, ty1)
+       glVertex2f( cornerSize, h-cornerSize)
+       glTexCoord2f(tx1, ty2)
+       glVertex2f( cornerSize, h)
+       glTexCoord2f(tx1, ty2)
+       glVertex2f( w-cornerSize, h)
+
+       glEnd()
+       glDisable(GL_TEXTURE_2D)
+       glPopMatrix()
+
 def unproject(winx, winy, winz, modelMatrix, projMatrix, viewport):
        npModelMatrix = numpy.matrix(numpy.array(modelMatrix, numpy.float64).reshape((4,4)))
        npProjMatrix = numpy.matrix(numpy.array(projMatrix, numpy.float64).reshape((4,4)))
index a6b64a44cc9a5e347639cc217d171584d47fff4b..2b1ddd3f648eb8b74da0609445baa4fd4092122e 100644 (file)
@@ -5,6 +5,7 @@ import wx
 import traceback
 import sys
 import os
+import time
 
 from wx import glcanvas
 import OpenGL
@@ -13,6 +14,27 @@ from OpenGL.GL import *
 
 from Cura.gui.util import opengl
 
+class animation(object):
+       def __init__(self, gui, start, end, runTime):
+               self._start = start
+               self._end = end
+               self._startTime = time.time()
+               self._runTime = runTime
+               gui._animationList.append(self)
+
+       def isDone(self):
+               return time.time() > self._startTime + self._runTime
+
+       def getPosition(self):
+               if self.isDone():
+                       return self._end
+               f = (time.time() - self._startTime) / self._runTime
+               ts = f*f
+               tc = f*f*f
+               #f = 6*tc*ts + -15*ts*ts + 10*tc
+               f = tc + -3*ts + 3*f
+               return self._start + (self._end - self._start) * f
+
 class glGuiControl(object):
        def __init__(self, parent, pos):
                self._parent = parent
@@ -114,6 +136,7 @@ class glGuiPanel(glcanvas.GLCanvas):
                self._glRobotTexture = None
                self._buttonSize = 64
 
+               self._animationList = []
                self.glReleaseList = []
 
                wx.EVT_PAINT(self, self._OnGuiPaint)
@@ -131,6 +154,14 @@ class glGuiPanel(glcanvas.GLCanvas):
                wx.EVT_MOTION(self, self._OnGuiMouseMotion)
                wx.EVT_CHAR(self, self._OnGuiKeyChar)
                wx.EVT_KILL_FOCUS(self, self.OnFocusLost)
+               wx.EVT_IDLE(self, self._OnIdle)
+
+       def _OnIdle(self, e):
+               if len(self._animationList) > 0:
+                       for anim in self._animationList:
+                               if anim.isDone():
+                                       self._animationList.remove(anim)
+                       self.Refresh()
 
        def _OnGuiKeyChar(self, e):
                if self._focus is not None:
@@ -577,115 +608,9 @@ class glFrame(glGuiContainer):
                bs = self._parent._buttonSize
                pos = self._getPixelPos()
 
-               glPushMatrix()
-               glTranslatef(pos[0], pos[1], 0)
-               glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
-               glEnable(GL_TEXTURE_2D)
-
                size = self._layout.getLayoutSize()
                glColor4ub(255,255,255,255)
-               glBegin(GL_QUADS)
-               bs /= 2
-               tc = 1 / 4 / 2
-
-#              glTexCoord2f(1, 0)
-#              glVertex2f( size[0], 0)
-#              glTexCoord2f(0, 0)
-#              glVertex2f( 0, 0)
-#              glTexCoord2f(0, 1)
-#              glVertex2f( 0, size[1])
-#              glTexCoord2f(1, 1)
-#              glVertex2f( size[0], size[1])
-               #TopLeft
-               glTexCoord2f(tc, 0)
-               glVertex2f( bs, 0)
-               glTexCoord2f(0, 0)
-               glVertex2f( 0, 0)
-               glTexCoord2f(0, tc/2)
-               glVertex2f( 0, bs)
-               glTexCoord2f(tc, tc/2)
-               glVertex2f( bs, bs)
-               #TopRight
-               glTexCoord2f(tc+tc, 0)
-               glVertex2f( size[0], 0)
-               glTexCoord2f(tc, 0)
-               glVertex2f( size[0] - bs, 0)
-               glTexCoord2f(tc, tc/2)
-               glVertex2f( size[0] - bs, bs)
-               glTexCoord2f(tc+tc, tc/2)
-               glVertex2f( size[0], bs)
-               #BottomLeft
-               glTexCoord2f(tc, tc/2)
-               glVertex2f( bs, size[1] - bs)
-               glTexCoord2f(0, tc/2)
-               glVertex2f( 0, size[1] - bs)
-               glTexCoord2f(0, tc/2+tc/2)
-               glVertex2f( 0, size[1])
-               glTexCoord2f(tc, tc/2+tc/2)
-               glVertex2f( bs, size[1])
-               #BottomRight
-               glTexCoord2f(tc+tc, tc/2)
-               glVertex2f( size[0], size[1] - bs)
-               glTexCoord2f(tc, tc/2)
-               glVertex2f( size[0] - bs, size[1] - bs)
-               glTexCoord2f(tc, tc/2+tc/2)
-               glVertex2f( size[0] - bs, size[1])
-               glTexCoord2f(tc+tc, tc/2+tc/2)
-               glVertex2f( size[0], size[1])
-
-               #Center
-               glTexCoord2f(tc, tc/2)
-               glVertex2f( size[0]-bs, bs)
-               glTexCoord2f(tc, tc/2)
-               glVertex2f( bs, bs)
-               glTexCoord2f(tc, tc/2)
-               glVertex2f( bs, size[1]-bs)
-               glTexCoord2f(tc, tc/2)
-               glVertex2f( size[0]-bs, size[1]-bs)
-
-               #Right
-               glTexCoord2f(tc+tc, tc/2)
-               glVertex2f( size[0], bs)
-               glTexCoord2f(tc, tc/2)
-               glVertex2f( size[0]-bs, bs)
-               glTexCoord2f(tc, tc/2)
-               glVertex2f( size[0]-bs, size[1]-bs)
-               glTexCoord2f(tc+tc, tc/2)
-               glVertex2f( size[0], size[1]-bs)
-
-               #Left
-               glTexCoord2f(tc, tc/2)
-               glVertex2f( bs, bs)
-               glTexCoord2f(0, tc/2)
-               glVertex2f( 0, bs)
-               glTexCoord2f(0, tc/2)
-               glVertex2f( 0, size[1]-bs)
-               glTexCoord2f(tc, tc/2)
-               glVertex2f( bs, size[1]-bs)
-
-               #Top
-               glTexCoord2f(tc, 0)
-               glVertex2f( size[0]-bs, 0)
-               glTexCoord2f(tc, 0)
-               glVertex2f( bs, 0)
-               glTexCoord2f(tc, tc/2)
-               glVertex2f( bs, bs)
-               glTexCoord2f(tc, tc/2)
-               glVertex2f( size[0]-bs, bs)
-
-               #Bottom
-               glTexCoord2f(tc, tc/2)
-               glVertex2f( size[0]-bs, size[1]-bs)
-               glTexCoord2f(tc, tc/2)
-               glVertex2f( bs, size[1]-bs)
-               glTexCoord2f(tc, tc/2+tc/2)
-               glVertex2f( bs, size[1])
-               glTexCoord2f(tc, tc/2+tc/2)
-               glVertex2f( size[0]-bs, size[1])
-
-               glEnd()
-               glDisable(GL_TEXTURE_2D)
-               glPopMatrix()
+               opengl.glDrawStretchedQuad(pos[0], pos[1], size[0], size[1], bs/2, 0)
                #Draw the controls on the frame
                super(glFrame, self).draw()