/usr/share/pyshared/pymt/modules/keybinding.py is in python-pymt 0.5.1-0ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | '''
Use keyboard to do some action
'''
__all__ = ('start', 'stop')
import sys
import logging
from pymt.cache import Cache
from pymt.clock import getClock
from pymt.base import getWindow
from pymt.graphx import drawRectangle, drawLabel, set_color, drawLine, drawCircle
from pymt.logger import pymt_logger_history, pymt_logger
from pymt.ui.colors import css_reload
from pymt.ui.widgets import *
_toggle_state = ''
def toggle(id):
global _toggle_state
if _toggle_state == id:
_toggle_state = ''
else:
_toggle_state = id
if _toggle_state == '':
return
def _can_fullscreen():
return sys.platform not in ('win32', 'darwin', 'cygwin', 'freebsd7')
def _screenshot():
import os
import pygame
from OpenGL.GL import glReadBuffer, glReadPixels, GL_RGB, GL_UNSIGNED_BYTE, GL_FRONT
win = getWindow()
glReadBuffer(GL_FRONT)
data = glReadPixels(0, 0, win.width, win.height, GL_RGB, GL_UNSIGNED_BYTE)
surface = pygame.image.fromstring(str(buffer(data)), win.size, 'RGB', True)
filename = None
for i in xrange(9999):
path = os.path.join(os.getcwd(), 'screenshot%04d.jpg' % i)
if not os.path.exists(path):
filename = path
break
if filename:
try:
pygame.image.save(surface, filename)
pymt_logger.info('KeyBinding: Screenshot saved at %s' % filename)
except:
pymt_logger.exception('KeyBinding: Unable to take a screenshot')
else:
pymt_logger.warning('KeyBinding: Unable to take screenshot, no more slot available')
def _on_draw():
global _toggle_state
if _toggle_state == '':
return
win = getWindow()
#
# Show HELP screen
#
if _toggle_state == 'help':
# draw the usual window
win.on_draw()
# make background more black
set_color(0, 0, 0, .8)
drawRectangle(size=win.size)
# prepare calculation
w2 = win.width / 2.
h2 = win.height / 2.
y = 0
k = {'font_size': 24}
# draw help
drawLabel('PyMT Keybinding',
pos=(w2, win.height - 100), font_size=40)
drawLabel('Press F1 to leave help',
pos=(w2, win.height - 160), font_size=12)
drawLabel('FPS is %.3f' % getClock().get_fps(),
pos=(w2, win.height - 180), font_size=12)
drawLabel('F1 - Show Help',
pos=(w2, h2), **k)
y += 35
drawLabel('F2 - Show FPS (%s)' % str(win.show_fps),
pos=(w2, h2 - y), **k)
y += 35
drawLabel('F3 - Show Cache state',
pos=(w2, h2 - y), **k)
y += 35
drawLabel('F4 - Show Calibration screen',
pos=(w2, h2 - y), **k)
if _can_fullscreen():
y += 35
drawLabel('F5 - Toggle fullscreen',
pos=(w2, h2 - y), **k)
y += 35
drawLabel('F6 - Show log',
pos=(w2, h2 - y), **k)
y += 35
drawLabel('F12 - Screenshot',
pos=(w2, h2 - y), **k)
return True
#
# Draw cache state
#
elif _toggle_state == 'cachestat':
# draw the usual window
win.on_draw()
# make background more black
set_color(0, 0, 0, .8)
drawRectangle(size=win.size)
y = 0
for x in Cache._categories:
y += 25
cat = Cache._categories[x]
count = 0
usage = '-'
limit = cat['limit']
timeout = cat['timeout']
try:
count = len(Cache._objects[x])
except:
pass
try:
usage = 100 * count / limit
except:
pass
args = (x, usage, count, limit, timeout)
drawLabel('%s: usage=%s%% count=%d limit=%s timeout=%s' % args,
pos=(20, 20 + y), font_size=20, center=False, nocache=True)
return True
#
# Draw calibration screen
#
elif _toggle_state == 'calibration':
step = 8
ratio = win.height / float(win.width)
stepx = win.width / step
stepy = win.height / int(step * ratio)
# draw black background
set_color(0, 0, 0)
drawRectangle(size=win.size)
# draw lines
set_color(1, 1, 1)
for x in xrange(0, win.width, stepx):
drawLine((x, 0, x, win.height))
for y in xrange(0, win.height, stepy):
drawLine((0, y, win.width, y))
# draw circles
drawCircle(pos=(win.width / 2., win.height / 2.),
radius=win.width / step, linewidth = 2.)
drawCircle(pos=(win.width / 2., win.height / 2.),
radius=(win.width / step) * 2, linewidth = 2.)
drawCircle(pos=(win.width / 2., win.height / 2.),
radius=(win.width / step) * 3, linewidth = 2.)
return True
#
# Draw calibration screen 2 (colors)
#
elif _toggle_state == 'calibration2':
# draw black background
set_color(0, 0, 0)
drawRectangle(size=win.size)
# gray
step = 25
stepx = (win.width - 100) / step
stepy = stepx * 2
sizew = stepx * step
sizeh = stepy * step
w2 = win.width / 2.
h2 = win.height / 2.
for _x in xrange(step):
x = w2 - sizew / 2. + _x * stepx
drawLabel(chr(65+_x), pos=(x + stepx / 2., h2 + 190))
c = _x / float(step)
# grey
set_color(c, c, c)
drawRectangle(pos=(x, h2 + 100), size=(stepx, stepy))
# red
set_color(c, 0, 0)
drawRectangle(pos=(x, h2 + 80 - stepy), size=(stepx, stepy))
# green
set_color(0, c, 0)
drawRectangle(pos=(x, h2 + 60 - stepy * 2), size=(stepx, stepy))
# blue
set_color(0, 0, c)
drawRectangle(pos=(x, h2 + 40 - stepy * 3), size=(stepx, stepy))
return True
#
# Draw log screen
#
elif _toggle_state == 'log':
# draw the usual window
win.on_draw()
# make background more black
set_color(0, 0, 0, .8)
drawRectangle(size=win.size)
# calculation
w2 = win.width / 2.
h2 = win.height / 2.
k = {'font_size': 11, 'center': False}
y = win.height - 20
y = h2
max = int((h2 / 20))
levels = {
logging.DEBUG: ('DEBUG', (.4,.4,1)),
logging.INFO: ('INFO', (.4,1,.4)),
logging.WARNING: ('WARNING', (1,1,.4)),
logging.ERROR: ('ERROR', (1,.4,.4)),
logging.CRITICAL: ('CRITICAL', (1,.4,.4)),
}
# draw title
drawLabel('PyMT logger',
pos=(w2, win.height - 100), font_size=40)
# draw logs
for log in reversed(pymt_logger_history.history[:max]):
levelname, color = levels[log.levelno]
msg = log.message.split('\n')[0]
x = 10
s = drawLabel('[', pos=(x, y), **k)
x += s[0]
s = drawLabel(levelname, pos=(x, y), color=color, **k)
x += s[0]
s = drawLabel(']', pos=(x, y), **k)
x += s[0]
drawLabel(msg, pos=(100, y), **k)
y -= 20
return True
class SceneGraphNode(MTBoxLayout):
def __init__(self, **kwargs):
kwargs['invert'] = True
super(SceneGraphNode, self).__init__(**kwargs)
self.widget = kwargs['node']
self.selected = False
self.child_layout = MTBoxLayout(size_hint=(None, None), spacing=10, orientation="vertical")
for c in self.widget.children:
self.child_layout.add_widget(SceneGraphNode(node=c, size_hint=(None, None)))
self.add_widget(self.child_layout)
self.node_btn = MTToggleButton(label=str(self.widget.__class__.__name__), size=(150,30))
self.title = MTAnchorLayout(size_hint=(None, None), size=(200,self.child_layout.height))
self.title.add_widget(self.node_btn)
self.add_widget(self.title)
self.node_btn.connect('on_release',self.select)
def draw(self):
if self.selected:
set_color(1,0,0,0.3)
drawRectangle(self.to_widget(*self.widget.pos), self.widget.size)
set_color(1,.3,0)
for c in self.child_layout.children:
drawLine((self.node_btn.centerright,c.node_btn.centerleft), width=2)
def select(self, *args):
self.selected = not self.selected
def add_new_widget(self, *args):
new_widget = MTButton(label="I'm new!!!")
self.widget.add_widget(new_widget)
self.child_layout.add_widget(SceneGraphNode(node=new_widget, size_hint=(None, None)))
self.title.size=(200,self.child_layout.height)
def print_props(self, *args):
for prop in self.widget.__dict__:
if not prop.startswith("_"):
print prop, ":", getattr(self.widget, prop)
_scene_graph_modal_layover = None
def toggle_scene_graph():
global _scene_graph_modal_layover
win = getWindow()
if _scene_graph_modal_layover:
win.remove_widget(_scene_graph_modal_layover)
_scene_graph_modal_layover = None
return
else:
scene_graph = SceneGraphNode(node=win.children[0], size_hint=(None, None))
plane = MTScatterPlane(do_rotation=False)
plane.add_widget(scene_graph)
_scene_graph_modal_layover = MTModalWindow()
_scene_graph_modal_layover.add_widget(plane)
win.add_widget(_scene_graph_modal_layover)
def _on_keyboard_handler(key, scancode, unicode):
if key is None:
return
win = getWindow()
if key == 282: # F1
toggle('help')
elif key == 283: # F2
win.show_fps = not win.show_fps
elif key == 284: # F3
toggle('cachestat')
elif key == 285: # F4
# rotating between calibration screen
if _toggle_state == 'calibration':
toggle('calibration2')
elif _toggle_state == 'calibration2':
toggle('')
else:
toggle('calibration')
elif key == 286 and _can_fullscreen(): # F5
win.toggle_fullscreen()
elif key == 287: # F6
toggle('log')
elif key == 288: # F7
css_reload()
elif key == 289: # F8
toggle_scene_graph()
elif key == 293:
_screenshot()
def start(win, ctx):
win.push_handlers(on_keyboard=_on_keyboard_handler)
win.push_handlers(on_draw=_on_draw)
def stop(win, ctx):
win.remove_handlers(on_keyboard=_on_keyboard_handler)
win.remove_handlers(on_draw=_on_draw)
|