| 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 __future__ import division |
|---|
| 27 |
import time |
|---|
| 28 |
from wxPython import wx |
|---|
| 29 |
from RBFoundation.SubObs.Event import ObjectEventSubject, ObjectEventProperty |
|---|
| 30 |
|
|---|
| 31 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 32 |
#~ Definitions |
|---|
| 33 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 34 |
|
|---|
| 35 |
class InorderObjectEventSubject(ObjectEventSubject): |
|---|
| 36 |
prepend = False |
|---|
| 37 |
class InorderObjectEventProperty(ObjectEventProperty): |
|---|
| 38 |
EventFactory = InorderObjectEventSubject |
|---|
| 39 |
|
|---|
| 40 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 41 |
|
|---|
| 42 |
class GLViewSetup(object): |
|---|
| 43 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 44 |
#~ Constants / Variables / Etc. |
|---|
| 45 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 46 |
|
|---|
| 47 |
OnRender = InorderObjectEventProperty() #(glviewsetup, canvas) |
|---|
| 48 |
OnRenderingError = ObjectEventProperty() #(glviewsetup, error) |
|---|
| 49 |
_PendingRefresh = False |
|---|
| 50 |
|
|---|
| 51 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 52 |
#~ Public Methods |
|---|
| 53 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 54 |
|
|---|
| 55 |
def __init__(self, canvas, fps=33): |
|---|
| 56 |
self.canvas = canvas |
|---|
| 57 |
|
|---|
| 58 |
timerId = wx.wxNewId() |
|---|
| 59 |
timerOwner = self.canvas.GetEventHandler() |
|---|
| 60 |
self._timer = wx.wxTimer(timerOwner, timerId) |
|---|
| 61 |
wx.EVT_TIMER(timerOwner, timerId, self.OnTimer) |
|---|
| 62 |
|
|---|
| 63 |
self.SetTargetFPS(fps) |
|---|
| 64 |
|
|---|
| 65 |
wx.EVT_ERASE_BACKGROUND(self.canvas, self.OnEraseViewBackground) |
|---|
| 66 |
wx.EVT_PAINT(self.canvas, self.OnPaint) |
|---|
| 67 |
|
|---|
| 68 |
def GetTargetFrameRate(self): |
|---|
| 69 |
return self.rate |
|---|
| 70 |
def SetTargetFrameRate(self, rate): |
|---|
| 71 |
self.rate = rate |
|---|
| 72 |
self._ratedirty = True |
|---|
| 73 |
|
|---|
| 74 |
def GetTargetFPS(self): |
|---|
| 75 |
if self.rate: |
|---|
| 76 |
return 1./self.rate |
|---|
| 77 |
else: return None |
|---|
| 78 |
def SetTargetFPS(self, framesPerSecond): |
|---|
| 79 |
if framesPerSecond: |
|---|
| 80 |
self.SetTargetFrameRate(1./framesPerSecond) |
|---|
| 81 |
else: |
|---|
| 82 |
self.SetTargetFrameRate(None) |
|---|
| 83 |
|
|---|
| 84 |
def IsRendering(self): |
|---|
| 85 |
return self._timer.IsRunning() |
|---|
| 86 |
|
|---|
| 87 |
def StartRendering(self): |
|---|
| 88 |
self._ratedirty = False |
|---|
| 89 |
if self.rate: |
|---|
| 90 |
self._timer.Start(int(1000*self.rate)) |
|---|
| 91 |
else: |
|---|
| 92 |
self._timer.Stop() |
|---|
| 93 |
self._Refresh() |
|---|
| 94 |
|
|---|
| 95 |
def StopRendering(self): |
|---|
| 96 |
self._timer.Stop() |
|---|
| 97 |
|
|---|
| 98 |
def _Refresh(self): |
|---|
| 99 |
if self.canvas: |
|---|
| 100 |
if not self._PendingRefresh: |
|---|
| 101 |
self.canvas.Refresh() |
|---|
| 102 |
self._PendingRefresh = True |
|---|
| 103 |
if self._ratedirty: |
|---|
| 104 |
self.StartRendering() |
|---|
| 105 |
else: |
|---|
| 106 |
self.StopRendering() |
|---|
| 107 |
|
|---|
| 108 |
#~ wx Events ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 109 |
|
|---|
| 110 |
def OnTimer(self, evt): |
|---|
| 111 |
self._Refresh() |
|---|
| 112 |
|
|---|
| 113 |
def OnPaint(self, evt): |
|---|
| 114 |
self._PendingRefresh = False |
|---|
| 115 |
try: |
|---|
| 116 |
dc = wx.wxPaintDC(self.canvas) |
|---|
| 117 |
self.canvas.SetCurrent() |
|---|
| 118 |
self.OnRender(self.canvas) |
|---|
| 119 |
self.canvas.SwapBuffers() |
|---|
| 120 |
except Exception, e: |
|---|
| 121 |
self.ErrorHandled = False |
|---|
| 122 |
self.OnRenderingError(e) |
|---|
| 123 |
if not self.ErrorHandled: |
|---|
| 124 |
self.StopRendering() |
|---|
| 125 |
raise |
|---|
| 126 |
|
|---|
| 127 |
def OnEraseViewBackground(self, evt): |
|---|
| 128 |
pass |
|---|
| 129 |
|
|---|