"""Defines the GLPluginInterface class, which is a unified base class for
plugins to the klampt.vis module.
"""
[docs]class GLPluginInterface:
"""Users can add their own hooks into the visualizer by overloading this
class's methods. Each method should return True if the user event was
processed. A return value of True stops cascading
events to a parent interface.
Attributes:
window (QtGLWindow or GLUTWindow): the window to which this plugin
is attached. May be None if not attached to an OpenGL window.
view (GLViewport): the viewport that is currently being used for this
plugin.
actions (list): internally used.
"""
def __init__(self):
self.window = None
self.view = None
self.actions = []
[docs] def initialize(self):
"""Called by backend after the GL context is created but before the event loop starts."""
return True
[docs] def displayfunc(self):
return False
[docs] def display(self):
return False
[docs] def display_screen(self):
return False
[docs] def reshapefunc(self,w,h):
return False
[docs] def keyboardfunc(self,c,x,y):
return False
[docs] def keyboardupfunc(self,c,x,y):
return False
[docs] def mousefunc(self,button,state,x,y):
return False
[docs] def motionfunc(self,x,y,dx,dy):
return False
[docs] def idle(self):
return True
[docs] def eventfunc(self,type,args=""):
"""Generic hook for other events, e.g., button presses, from the GUI"""
return False
[docs] def closefunc(self):
return False
[docs] def add_action(self,callback,short_name,key,description=None):
"""Defines a new generic GUI action. The action will be available in
a menu in Qt or as keyboard commands in GLUT."""
if not callable(callback):
raise ValueError("Invalid callback given to add_action(callback,short_name,key,description=None)")
self.actions.append((callback,short_name,key,description))
#functions to request operations of the backend
[docs] def reshape(self,w,h):
"""Asks to resize the GL window"""
if self.window:
return self.window.reshape(w,h)
[docs] def idlesleep(self,seconds):
"""Asks to sleep the idle function for seconds seconds."""
if self.window:
self.window.idlesleep(seconds)
[docs] def modifiers(self):
"""Retrieves a list of currently pressed keyboard modifiers.
Values can be any combination of 'ctrl', 'shift', 'alt'.
"""
return self.window.modifiers()
[docs] def refresh(self):
"""Asks for a redraw"""
if self.window:
self.window.refresh()
[docs] def draw_text(self,point,text,size=12,color=None):
"""Draws text of the given size and color at the point (x,y) or
(x,y,z). The former method is usually called during display_screen.
"""
if self.window:
self.window.draw_text(point,text,size,color)
#3D viewport accessors -- not supported directly through the backend
[docs] def click_ray(self,x,y):
"""Returns the world-space ray associated with the camera click at
pixel coordinates x,y."""
if self.view is None:
raise RuntimeError("Can't get click_ray for a GLPluginInterface that's not bound to a GLProgram")
return self.view.click_ray(x,y)
[docs] def viewport(self):
"""Returns the :class:`~klampt.robotsim.Viewport` instance associated
with the current GL view.
"""
if self.view is None:
raise RuntimeError("Can't get viewport for a GLPluginInterface that's not bound to a GLProgram")
return self.view.toViewport()