def __init__(self, *args, **kwargs): # Initialise self kwargs['resizable'] = kwargs.get('resizable', True) interactive = kwargs.pop('interactive', False) super(PlotWindow, self).__init__(*args, **kwargs) self._init = False self.last_press = datetime.datetime.now() # Create figure object width, height = self.get_size() self.figure = GLFigure() self.figure.set_rect([0, 0, width, height]) # Create cursors used for interactivity self._cursors = {} for k, v in self.cursor_names.iteritems(): self._cursors[k] = self.get_system_mouse_cursor(v) # Create interactivity data self.set_interactive(interactive)
class PlotWindow(Window): """Subclass of Window that displays an interactive GLFigure""" double_click_time = datetime.timedelta(seconds=0.3) cursor_names = {None:Window.CURSOR_DEFAULT, 'pan':Window.CURSOR_HAND, 'zoom':Window.CURSOR_CROSSHAIR} def __init__(self, *args, **kwargs): # Initialise self kwargs['resizable'] = kwargs.get('resizable', True) interactive = kwargs.pop('interactive', False) super(PlotWindow, self).__init__(*args, **kwargs) self._init = False self.last_press = datetime.datetime.now() # Create figure object width, height = self.get_size() self.figure = GLFigure() self.figure.set_rect([0, 0, width, height]) # Create cursors used for interactivity self._cursors = {} for k, v in self.cursor_names.iteritems(): self._cursors[k] = self.get_system_mouse_cursor(v) # Create interactivity data self.set_interactive(interactive) def run(self): pyglet.app.run() def on_draw(self): if not self._init: self.figure._initgl() self._init = True self.figure._draw() def on_resize(self, width, height): self.figure._resize(width, height) def set_interactive(self, interactive): self._interactive = InteractiveState(interactive, self) def _set_cursor(self, mode): self.set_mouse_cursor(self._cursors[mode]) # Manually detect double-clicks def on_mouse_press(self, x, y, button, modifiers): self.on_mouse_down(x, y, button, modifiers) penultimate_press = self.last_press self.last_press = datetime.datetime.now() delta = self.last_press - penultimate_press if button == mouse.LEFT and delta < self.double_click_time: self.on_double_click(x, y, button, modifiers) def _point(self, x, y): width, height = self.get_size() return float(x) / float(width), float(y) / float(height) def on_mouse_down(self, x, y, button, modifiers): if button == mouse.LEFT: self._interactive.on_left_down(self._point(x, y)) def on_mouse_release(self, x, y, button, modifiers): if button == mouse.LEFT: self._interactive.on_left_up(self._point(x, y)) def on_double_click(self, x, y, button, modifiers): if button == mouse.LEFT: self._interactive.on_left_double_click(self._point(x, y)) def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers): if buttons & mouse.LEFT: self._interactive.on_mouse_drag(self._point(x, y)) def on_mouse_motion(self, x, y, dx, dy): self._interactive.on_mouse_move(self._point(x, y))