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
|
"""
Similar to simple_drag_demo, put one circle inside a scrolled container
"""
from numpy import array
from traits.api import Enum, Float, Instance, Tuple
from enable.example_support import DemoFrame, demo_main
from enable.api import Component, Scrolled, Container, Pointer, Window
class Circle(Component):
"""
The circle moves with the mouse cursor but leaves a translucent version of
itself in its original position until the mouse button is released.
"""
color = (0.3, 0.4, 0.8, 1.0)
bgcolor = "none"
normal_pointer = Pointer("arrow")
moving_pointer = Pointer("hand")
offset_x = Float
offset_y = Float
shadow_type = Enum("light", "dashed")
shadow = Instance(Component)
def __init__(self, **traits):
Component.__init__(self, **traits)
self.pointer = self.normal_pointer
return
def _draw_mainlayer(self, gc, view_bounds=None, mode="default"):
with gc:
gc.set_fill_color(self.color)
dx, dy = self.bounds
x, y = self.position
radius = min(dx/2.0, dy/2.0)
gc.arc(x+dx/2.0, y+dy/2.0, radius, 0.0, 2*3.14159)
gc.fill_path()
return
def normal_left_down(self, event):
self.event_state = "moving"
self.pointer = self.moving_pointer
# Create our shadow
if self.shadow_type == "light":
klass = LightCircle
else:
klass = DashedCircle
dx, dy = self.bounds
self.shadow = klass(bounds=self.bounds, position=self.position,
color=self.color)
self.container.add(self.shadow)
x, y = self.position
self.offset_x = event.x - x
self.offset_y = event.y - y
return
def moving_mouse_move(self, event):
self.position = [event.x-self.offset_x, event.y-self.offset_y]
self.request_redraw()
return
def moving_left_up(self, event):
self.event_state = "normal"
self.pointer = self.normal_pointer
self.request_redraw()
# Remove our shadow
self.container.remove(self.shadow)
return
def moving_mouse_leave(self, event):
self.moving_left_up(event)
return
class LightCircle(Component):
color = Tuple
bgcolor = "none"
radius = Float(1.0)
def _draw_mainlayer(self, gc, view_bounds=None, mode="default"):
with gc:
gc.set_fill_color(self.color[0:3] + (self.color[3]*0.3,))
dx, dy = self.bounds
x, y = self.position
radius = min(dx/2.0, dy/2.0)
gc.arc(x+dx/2.0, y+dy/2.0, radius, 0.0, 2*3.14159)
gc.fill_path()
return
class DashedCircle(Component):
color = Tuple
bgcolor = "none"
radius = Float(1.0)
line_dash = array([2.0, 2.0])
def _draw_mainlayer(self, gc, view_bounds=None, mode="default"):
with gc:
gc.set_fill_color(self.color)
dx, dy = self.bounds
x, y = self.position
radius = min(dx/2.0, dy/2.0)
gc.arc(x+dx/2.0, y+dy/2.0, radius, 0.0, 2*3.14159)
gc.set_stroke_color(self.color[0:3] + (self.color[3]*0.8,))
gc.set_line_dash(self.line_dash)
gc.stroke_path()
return
class MyFrame(DemoFrame):
def _create_window(self):
container = Container(bounds=[800,600], bgcolor=(0.9, 0.7, 0.7, 1.0),
auto_size=False, fit_window=False)
circle1 = Circle(bounds=[75,75], position=[100,100],
shadow_type="dashed")
container.add(circle1)
scr = Scrolled(container, bounds=[200,200], position=[50,50],
fit_window=False)
return Window(self, -1, component=scr)
if __name__ == "__main__":
# Save demo so that it doesn't get garbage collected when run within
# existing event loop (i.e. from ipython).
demo = demo_main(MyFrame, title="Click and drag to move the circles")
|