File: Togl.py

package info (click to toggle)
mgltools-opengltk 1.5.7-1
  • links: PTS, VCS
  • area: non-free
  • in suites: stretch
  • size: 8,592 kB
  • ctags: 38,393
  • sloc: ansic: 98,617; python: 3,818; cpp: 1,943; sh: 1,332; tcl: 1,127; makefile: 65
file content (125 lines) | stat: -rw-r--r-- 3,240 bytes parent folder | download | duplicates (7)
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

"""
Tkinter support for the Togl 2.X Tk OpenGL widget.

Copyright (C) 2006-2007  Greg Couch
See the LICENSE file for copyright details.
"""
__all__ = ['Togl', 'NORMAL', 'OVERLAY']

import Tkinter
import weakref, atexit

# Overlay constants
NORMAL = 1
OVERLAY = 2

class Togl(Tkinter.Widget):
	"""Tk OpenGL Widget"""
	_instances = weakref.WeakKeyDictionary()

	def __init__(self, master=None, cnf={}, **kw):
		"""Return new Togl widget"""
		if master is None:
			master = Tkinter._default_root
		master.tk.call('package', 'require', 'Togl', '2.0')
		Tkinter.Widget.__init__(self, master, "togl", cnf, kw)
		Togl._instances[self] = True

	def _cbsubst(self, *args):
		"""callback command argument substitution"""
		if len(args) != 1:
			return args
		return (self._nametowidget(args[0]),)

	def _options(self, cnf, kw = None):
		"""Internal function."""
		if kw:
			cnf = Tkinter._cnfmerge((cnf, kw))
		else:
			cnf = Tkinter._cnfmerge(cnf)
		res = ()
		for k, v in cnf.items():
			if v is not None:
				if k[-1] == '_': k = k[:-1]
				if callable(v):
					if k.endswith('command'):
						v = self._register(v, self._cbsubst)
					else:
						v = self._register(v)
				res = res + ('-'+k, v)
		return res

	# cget, configure are inherited

	def extensions(self):
		"""Return list of supported OpenGL extensions"""
		return self.tk.call(self._w, 'extensions')

	def postredisplay(self):
		"""Cause the displaycommand callback to be called
		the next time the event loop is idle."""
		self.tk.call(self._w, 'postredisplay')

	def render(self):
		"""Call the displaycommand callback immediately."""
		self.tk.call(self._w, 'render')

	def swapbuffers(self):
		"""If single-buffred, just flush OpenGL command buffer.  If
		double-buffered, swap front and back buffers.  (So this is
		appropriate to call after every frame is drawn.)"""
		self.tk.call(self._w, 'swapbuffers')

	def makecurrent(self):
		"""Make widget the current OpenGL context"""
		self.tk.call(self._w, 'makecurrent')

	def takephoto(self, imageName):
		"""Copy current contents of widget into the given photo image
		"""
		self.tk.call(self._w, 'takephoto', imageName)

	def loadbitmapfont(self, name):
		return self.tk.call(self._w, 'loadbitmapfont', name)

	def unloadbitmapfont(self, fontbase):
		self.tk.call(self._w, 'unloadbitmapfont', fontbase)

	def uselayer(self, layer):
		self.tk.call(self._w, 'uselayer', layer)

	def showoverlay(self):
		self.tk.call(self._w, 'showoverlay')

	def hideoverlay(self):
		self.tk.call(self._w, 'hideoverlay')

	def postredisplayoverlay(self):
		self.tk.call(self._w, 'postredisplayoverlay')

	def renderoverlay(self):
		self.tk.call(self._w, 'renderoverlay')

	def existsoverlay(self):
		return self.tk.call(self._w, 'existsoverlay')

	def ismappedoverlay(self):
		return self.tk.call(self._w, 'ismappedoverlay')

	def getoverlaytransparentvalue(self):
		return self.tk.call(self._w, 'getoverlaytransparentvalue')

	def destroy(self):
		del Togl._instances[self]
		Tkinter.Widget.destroy(self)

def _cleanup():
	# destroy OpenGL contexts early, so destroycommand's don't
	# try to make any OpenGL calls during exit.
	for t in Togl._instances.keys():
		try:
			t.destroy()
		except Tkinter.TclError:
			pass
atexit.register(_cleanup)