File: View.py

package info (click to toggle)
fofix-dfsg 3.121-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 27,016 kB
  • ctags: 4,131
  • sloc: python: 44,002; makefile: 99; perl: 42; sh: 39
file content (216 lines) | stat: -rwxr-xr-x 7,782 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#####################################################################
# -*- coding: iso-8859-1 -*-                                        #
#                                                                   #
# Frets on Fire                                                     #
# Copyright (C) 2006 Sami Ky�stil�                                  #
#               2008 QQStarS                                        #
#                                                                   #
# This program is free software; you can redistribute it and/or     #
# modify it under the terms of the GNU General Public License       #
# as published by the Free Software Foundation; either version 2    #
# of the License, or (at your option) any later version.            #
#                                                                   #
# This program is distributed in the hope that it will be useful,   #
# but WITHOUT ANY WARRANTY; without even the implied warranty of    #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     #
# GNU General Public License for more details.                      #
#                                                                   #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software       #
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,        #
# MA  02110-1301, USA.                                              #
#####################################################################

from __future__ import division
from OpenGL.GL import *
from OpenGL.GLU import *

import Log

from Task import Task

class Layer(Task):
  def render(self, visibility, topMost):
    pass
    
  def shown(self):
    pass
  
  def hidden(self):
    pass

  def run(self, ticks):
    pass

  def isBackgroundLayer(self):
    return False

class BackgroundLayer(Layer):
  def isBackgroundLayer(self):
    return True

class View(Task):
  def __init__(self, engine, geometry = None, screens = 1):
    Task.__init__(self)
    self.layers = []
    self.incoming = []
    self.outgoing = []
    self.visibility = {}
    self.transitionTime = 512.0
    self.geometry = geometry or glGetIntegerv(GL_VIEWPORT)
    self.savedGeometry = None
    self.engine = engine
    w = self.geometry[2] - self.geometry[0]
    h = self.geometry[3] - self.geometry[1]
    self.aspectRatio = float(w) / float(h)

  def pushLayer(self, layer):
    Log.debug("View: Push: %s" % layer.__class__.__name__)
    
    if not layer in self.layers:
      self.layers.append(layer)
      self.incoming.append(layer)
      self.visibility[layer] = 0.0
      layer.shown()
    elif layer in self.outgoing:
      layer.hidden()
      layer.shown()
      self.outgoing.remove(layer)
    self.engine.addTask(layer)

  def topLayer(self):
    layers = list(self.layers)
    layers.reverse()
    for layer in layers:
      if layer not in self.outgoing:
        return layer

  def popLayer(self, layer):
    Log.debug("View: Pop: %s" % layer.__class__.__name__)
    
    if layer in self.incoming:
      self.incoming.remove(layer)
    if layer in self.layers and not layer in self.outgoing:
      self.outgoing.append(layer)

  def popAllLayers(self):
    Log.debug("View: Pop all")
    [self.popLayer(l) for l in list(self.layers)]

  def isTransitionInProgress(self):
    return self.incoming or self.outgoing
    
  def run(self, ticks):
    if not self.layers:
      return

    topLayer = self.topLayer()
    
    #t = ticks / self.transitionTime
    t = self.engine.clock.get_time() / self.transitionTime
    
    for layer in list(self.layers):
      if not layer in self.visibility:
        continue
      if layer in self.outgoing or (layer is not topLayer and not layer.isBackgroundLayer()):
        if self.visibility[layer] > 0.0:
          self.visibility[layer] = max(0.0, self.visibility[layer] - t)
        else:
          self.visibility[layer] = 0.0
          if layer in self.outgoing:
            self.outgoing.remove(layer)
            self.layers.remove(layer)
            del self.visibility[layer]
            self.engine.removeTask(layer)
            layer.hidden()
          if layer in self.incoming:
            self.incoming.remove(layer)
      elif layer in self.incoming or layer is topLayer:
        if self.visibility[layer] < 1.0:
          self.visibility[layer] = min(1.0, self.visibility[layer] + t)
        else:
          self.visibility[layer] = 1.0
          if layer in self.incoming:
            self.incoming.remove(layer)

  def setOrthogonalProjection(self, normalize = True, yIsDown = True):
    glMatrixMode(GL_PROJECTION)
    glPushMatrix()
    glLoadIdentity()

    viewport = glGetIntegerv(GL_VIEWPORT)
    if normalize:
      w = viewport[2] - viewport[0]
      h = viewport[3] - viewport[1]
      # aspect ratio correction
      h *= (float(w) / float(h)) / (4.0 / 3.0)
      viewport = [0, 0, 1, h / w]
  
    if yIsDown:
      glOrtho(viewport[0], viewport[2] - viewport[0],
              viewport[3] - viewport[1], viewport[1], -100, 100);
    else:
      glOrtho(viewport[0], viewport[2] - viewport[0],
              viewport[1], viewport[3] - viewport[1], -100, 100);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
  
  def resetProjection(self):
    glMatrixMode(GL_PROJECTION)
    glPopMatrix()
    glMatrixMode(GL_MODELVIEW)
    glPopMatrix()

  def setGeometry(self, geometry, screens=1):
    viewport = glGetIntegerv(GL_VIEWPORT)
    w = viewport[2] - viewport[0]
    h = viewport[3] - viewport[1]
    s = (w, h, w, h)

    geometry = [(type(coord) == float) and int(s[i] * coord) or int(coord) for i, coord in enumerate(geometry)]
    geometry[0] = int(geometry[0] / screens)
    geometry[2] = int(geometry[2] / screens)
    geometry = tuple(geometry)
    self.savedGeometry, self.geometry = viewport, geometry
    glViewport(int(geometry[0]), int(geometry[1]), int(geometry[2]), int(geometry[3]))
    glScissor(int(geometry[0]), int(geometry[1]), int(geometry[2]), int(geometry[3]))

  def setViewport(self, screens=1, screen=0):
    geometry = list(self.savedGeometry)
    if screens != 1:
      geometry[0] = int(geometry[0]+((screen-0.2)*geometry[2]/screens))
      geometry[2] = int(geometry[2]*1.4/screens)
      geometry[1] = int(geometry[1]) #QQstarS:
      geometry[3] = int(geometry[3]*1.6/screens)  #QQstarS
    geometry = tuple(geometry)
    self.geometry = geometry

    glViewport(int(geometry[0]), int(geometry[1]), int(geometry[2]), int(geometry[3]))
    glScissor(int(geometry[0]), int(geometry[1]), int(geometry[2]), int(geometry[3]))

  def setViewportHalf(self, screens=1, screen=0):
    geometry = list(self.savedGeometry)
    if screens != 1:
      geometry[0] = int(geometry[0]+(screen*geometry[2]/screens))
      geometry[2] = int(geometry[2]/screens) 
      geometry[1] = int(geometry[1])#+(geometry[3]/screens*2/3)) #QQstarS
      geometry[3] = int((geometry[3]/screens)*1.5) #QQstarS: make the Y postion smaller
    geometry = tuple(geometry)
    self.geometry = geometry

    glViewport(int(geometry[0]), int(geometry[1]), int(geometry[2]), int(geometry[3]))
    glScissor(int(geometry[0]), int(geometry[1]), int(geometry[2]), int(geometry[3]))    

  def resetGeometry(self):
    assert self.savedGeometry
    
    self.savedGeometry, geometry = None, self.savedGeometry
    self.geometry = geometry
    glViewport(int(geometry[0]), int(geometry[1]), int(geometry[2]), int(geometry[3]))
    glScissor(int(geometry[0]), int(geometry[1]), int(geometry[2]), int(geometry[3]))

  def render(self):
    #print [(str(m.__class__), v) for m, v in self.visibility.items()]
    for layer in self.layers:
      layer.render(self.visibility[layer], layer == self.layers[-1])