Package sparked :: Package graphics :: Module stage
[hide private]
[frames] | no frames]

Source Code for Module sparked.graphics.stage

  1  # Copyright (c) 2010 Arjan Scherpenisse 
  2  # See LICENSE for details. 
  3   
  4  """ 
  5  A grapihical window implemented using the 
  6  U{clutter<http://www.clutter-project.org/>} library, for interactive 
  7  displays. 
  8   
  9  F11 toggles fullscreen. 
 10  """ 
 11   
 12  import gtk 
 13  import clutter 
 14   
 15  from twisted.internet import reactor 
 16   
 17  from sparked import events 
 18   
 19   
20 -class Stage (clutter.Stage):
21 """ 22 Stage class. 23 24 @ivar keys: Keymap with commonly used keys and their actions. 25 @ivar debug: Whether debugging is enabled. This is retrieved from the --debug flag from the sparkd runner. 26 """ 27 28 keys = {'fullscreen': gtk.keysyms.F11, 29 'quit': (clutter.CONTROL_MASK, gtk.keysyms.q)} 30 debug = False 31
32 - def __init__(self, app):
33 clutter.Stage.__init__(self) 34 self.app = app 35 self.app.state.addListener(self) 36 self.show() 37 self.set_title(app.title + " - Stage") 38 39 self.connect("destroy", lambda _: stageEvents.dispatch("stage-closed", self)) 40 self.connect("key-press-event", self.keyPress) 41 42 self.debug = self.app.baseOpts['debug'] 43 self.addMonitors() 44 45 # shutdown when stage is closed 46 stageEvents.addObserver("stage-closed", lambda _: reactor.stop()) 47 # go fullscreen if not --debug 48 if not self.debug: 49 self.toggleFullscreen() 50 51 self.created()
52 53
54 - def created(self):
55 """ 56 Callback function when the construction of the stage is 57 complete and it is ready to be shown at the screen. 58 """
59 60
61 - def addMonitors(self):
62 """ 63 Add a widget to the stage which shows when one of the monitors 64 reports an error. 65 """ 66 self.app.monitors.events.addObserver("updated", self.updateMonitors) 67 self._monitorText = clutter.Text() 68 self._monitorText.set_font_name("helvetica bold 18px") 69 self._monitorText.set_color(clutter.color_from_string("#ff0000")) 70 self._monitorText.set_x(3) 71 self.add(self._monitorText)
72 73
74 - def updateMonitors(self, container):
75 if container.ok(): 76 self._monitorText.hide() 77 return 78 txt = "ERROR: " 79 for m in container.monitors: 80 if not m.ok: 81 txt += m.title+", " 82 txt = txt[:-2] 83 self._monitorText.set_text(txt) 84 self._monitorText.show() 85 self._monitorText.raise_top()
86 87
88 - def keyPress(self, actor, event):
89 """ 90 Handle keypresses. 91 """ 92 if self.isKey(event, self.keys['fullscreen']): 93 self.toggleFullscreen() 94 if self.isKey(event, self.keys['quit']): 95 reactor.stop()
96 97
98 - def isKey(self, event, keydef):
99 if type(keydef) != tuple: 100 return event.keyval == keydef 101 102 for m in keydef[:-1]: 103 if not (event.modifier_state & m): 104 return False 105 return event.keyval == keydef[-1]
106 107 108
109 - def toggleFullscreen(self):
110 """ 111 Toggle this stage fullscreen or not. 112 """ 113 self.set_fullscreen(not self.get_fullscreen()) 114 stageEvents.dispatch("stage-fullscreentoggled", stage=self) 115 self.show_cursor() 116 117 if not self.get_fullscreen(): 118 # we're fullscreen here 119 if not self.debug: 120 self.hide_cursor() 121 122 self.app.screensaverInhibit("Fullscreen presentation") 123 else: 124 self.app.screensaverUnInhibit()
125 126
127 -def positionInBox(actor, box):
128 """ 129 Given an actor and a box (also an actor), position the actor 130 inside the box, adjusting its position and size, while preserving 131 the aspect ratio of the actor. 132 """ 133 aw, ah = actor.get_width(), actor.get_height() 134 bw, bh = box.get_width(), box.get_height() 135 136 aspect = ah/float(aw) 137 138 if bw*aspect <= bh: 139 actor.set_width(bw) 140 actor.set_height(bw*aspect) 141 actor.set_x(box.get_x()) 142 actor.set_y(box.get_y()+(bh-bw*aspect)*0.5) 143 else: 144 actor.set_height(bh) 145 actor.set_width(bh/aspect) 146 actor.set_y(box.get_y()) 147 actor.set_x(box.get_x()+(bw-bh/aspect)*0.5)
148 149 150 stageEvents = events.EventDispatcher() 151