| 1 |
#!/usr/bin/env python |
|---|
| 2 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 3 |
##~ License |
|---|
| 4 |
##~ |
|---|
| 5 |
##- The RuneBlade Foundation library is intended to ease some |
|---|
| 6 |
##- aspects of writing intricate Jabber, XML, and User Interface (wxPython, etc.) |
|---|
| 7 |
##- applications, while providing the flexibility to modularly change the |
|---|
| 8 |
##- architecture. Enjoy. |
|---|
| 9 |
##~ |
|---|
| 10 |
##~ Copyright (C) 2002 TechGame Networks, LLC. |
|---|
| 11 |
##~ |
|---|
| 12 |
##~ This library is free software; you can redistribute it and/or |
|---|
| 13 |
##~ modify it under the terms of the BSD style License as found in the |
|---|
| 14 |
##~ LICENSE file included with this distribution. |
|---|
| 15 |
##~ |
|---|
| 16 |
##~ TechGame Networks, LLC can be reached at: |
|---|
| 17 |
##~ 3578 E. Hartsel Drive #211 |
|---|
| 18 |
##~ Colorado Springs, Colorado, USA, 80920 |
|---|
| 19 |
##~ |
|---|
| 20 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 21 |
|
|---|
| 22 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 23 |
#~ Imports |
|---|
| 24 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 25 |
|
|---|
| 26 |
from OpenGL import GL, GLU |
|---|
| 27 |
|
|---|
| 28 |
from RBRapier.Renderer.Appearance import Blending |
|---|
| 29 |
from RBRapier.Renderer.View import Transformations |
|---|
| 30 |
|
|---|
| 31 |
from RBRapier.Tools import ViewBox |
|---|
| 32 |
from RBRapier.Tools import Animation |
|---|
| 33 |
|
|---|
| 34 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 35 |
#~ Definitions |
|---|
| 36 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 37 |
|
|---|
| 38 |
class ViewBoxNavigation(object): |
|---|
| 39 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 40 |
#~ Constants / Variables / Etc. |
|---|
| 41 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 42 |
|
|---|
| 43 |
useblending = True |
|---|
| 44 |
projection = None |
|---|
| 45 |
animate = False |
|---|
| 46 |
drawAnimation = False |
|---|
| 47 |
|
|---|
| 48 |
bgcolor = (1.,1.,1.,.2) |
|---|
| 49 |
fgcolor = (1.,1.,1.,.8) |
|---|
| 50 |
|
|---|
| 51 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 52 |
#~ Public Methods |
|---|
| 53 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 54 |
|
|---|
| 55 |
def __init__(self, useblending=useblending): |
|---|
| 56 |
self.useblending = useblending |
|---|
| 57 |
self.viewboxes = {} |
|---|
| 58 |
self.viewboxes['origonal'] = ViewBox.ViewBox() |
|---|
| 59 |
self.viewboxes[None] = self.viewboxes['origonal'] |
|---|
| 60 |
self.animation = lambda: 1. |
|---|
| 61 |
self.EasyInEasyOutCurve = ViewBox.Curves.CubicBezier(0., 0., 1., 1.) |
|---|
| 62 |
|
|---|
| 63 |
self.projection = Transformations.OrthographicMgd(GL.GL_PROJECTION, True, -1, 1, -1, 1, -1, 1) |
|---|
| 64 |
self.modelview = Transformations.IdentityMgd(GL.GL_MODELVIEW, True) |
|---|
| 65 |
if self.useblending: |
|---|
| 66 |
self.blending = Blending.Blend() |
|---|
| 67 |
|
|---|
| 68 |
def GLSelect(self, context): |
|---|
| 69 |
self.projection.GLSelect(context) |
|---|
| 70 |
self.modelview.GLSelect(context) |
|---|
| 71 |
|
|---|
| 72 |
def GLDeselect(self, context): |
|---|
| 73 |
self.modelview.GLDeselect(context) |
|---|
| 74 |
self.projection.GLDeselect(context) |
|---|
| 75 |
|
|---|
| 76 |
def GLExecute(self, context): |
|---|
| 77 |
vb = None |
|---|
| 78 |
if not vb and self.drawAnimation: |
|---|
| 79 |
vbAnimation = self.viewboxes.get('animate') |
|---|
| 80 |
if vbAnimation: |
|---|
| 81 |
vb = vbAnimation.viewbox1 |
|---|
| 82 |
alpha = (1.-vbAnimation.GetBlend()) |
|---|
| 83 |
if not vb: |
|---|
| 84 |
vb = self.GetViewBox('draw') |
|---|
| 85 |
alpha = 1. |
|---|
| 86 |
|
|---|
| 87 |
if vb is not None: |
|---|
| 88 |
if self.useblending: |
|---|
| 89 |
self.blending.GLSelect(context) |
|---|
| 90 |
self._DrawViewBox(vb, alpha) |
|---|
| 91 |
self.blending.GLDeselect(context) |
|---|
| 92 |
else: |
|---|
| 93 |
self._DrawViewBox(vb) |
|---|
| 94 |
|
|---|
| 95 |
def _DrawViewBox(self, drawVB, bgAlpha=1., fgAlpha=1.): |
|---|
| 96 |
if self.useblending: |
|---|
| 97 |
r,g,b,a = self.bgcolor |
|---|
| 98 |
GL.glColor4f(r,g,b,a*bgAlpha) |
|---|
| 99 |
drawVB.Draw(GL.GL_TRIANGLE_FAN) |
|---|
| 100 |
|
|---|
| 101 |
r,g,b,a = self.fgcolor |
|---|
| 102 |
GL.glColor4f(r,g,b,a*fgAlpha) |
|---|
| 103 |
drawVB.Draw() |
|---|
| 104 |
|
|---|
| 105 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 106 |
|
|---|
| 107 |
def SetAspectRatio(self, aspectratio): |
|---|
| 108 |
if self.animate: |
|---|
| 109 |
blend = self.animation() |
|---|
| 110 |
animateVB = self.GetViewBox('animate') |
|---|
| 111 |
if animateVB is not None: |
|---|
| 112 |
if 0. < blend < 1.: |
|---|
| 113 |
blend = self.EasyInEasyOutCurve.CurveAtPoints(blend)[0] |
|---|
| 114 |
animateVB.SetBlend(blend) |
|---|
| 115 |
if blend >= 1.: |
|---|
| 116 |
if self.GetViewBox() is self.GetViewBox('animate'): |
|---|
| 117 |
self.SetViewBox(animateVB.Reduce()) |
|---|
| 118 |
self.SetViewBox(None, 'animate') |
|---|
| 119 |
|
|---|
| 120 |
viewbox = self.GetViewBox().copy() # make a copy so we don't "erode" |
|---|
| 121 |
viewbox.SetAspectRatio(aspectratio) |
|---|
| 122 |
self.projection.ProjectionModel = viewbox |
|---|
| 123 |
self.viewboxes['displayed'] = viewbox |
|---|
| 124 |
|
|---|
| 125 |
def AnimateToViewBox(self, viewbox, animationspan=0.5, key=None, drawAnimation=True): |
|---|
| 126 |
viewbox0 = self.GetViewBox().copy() |
|---|
| 127 |
self.viewboxes['animate'] = ViewBox.BlendedViewBox(viewbox0, viewbox) |
|---|
| 128 |
self.SetViewBox(self.viewboxes['animate'], key) |
|---|
| 129 |
self.animation = Animation.TimeBlend.fromSpan(animationspan) |
|---|
| 130 |
self.drawAnimation = drawAnimation |
|---|
| 131 |
|
|---|
| 132 |
def GetViewBox(self, which=None): |
|---|
| 133 |
return self.viewboxes.get(which) |
|---|
| 134 |
def GetDisplayedViewBox(self): |
|---|
| 135 |
try: |
|---|
| 136 |
return self.viewboxes['displayed'] |
|---|
| 137 |
except KeyError: |
|---|
| 138 |
return self.viewboxes[None] |
|---|
| 139 |
def SetViewBox(self, viewbox, key=None): |
|---|
| 140 |
if 'draw' in self.viewboxes: |
|---|
| 141 |
del self.viewboxes['draw'] |
|---|
| 142 |
if viewbox is None: |
|---|
| 143 |
try: del self.viewboxes[key] |
|---|
| 144 |
except KeyError: pass |
|---|
| 145 |
else: |
|---|
| 146 |
self.viewboxes[key] = viewbox |
|---|
| 147 |
|
|---|
| 148 |
|
|---|