File: latency_demo.py

package info (click to toggle)
python-enable 4.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,280 kB
  • ctags: 13,899
  • sloc: cpp: 48,447; python: 28,502; ansic: 9,004; makefile: 315; sh: 44
file content (96 lines) | stat: -rw-r--r-- 2,957 bytes parent folder | download
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
"""
Test to see what level of click latency is noticeable.
"""
import time

from traits.api import Float
from enable.api import (Component, Container, ColorTrait, black_color_trait,
                        Window)
from enable.example_support import DemoFrame, demo_main
from kiva.constants import SWISS
from kiva.fonttools import Font


font = Font(family=SWISS)


class Box(Component):
    color = ColorTrait("red")

    delay = Float(0.50)

    def _draw_mainlayer(self, gc, view=None, mode="default"):
        if self.event_state == "clicked":
            print "waiting %0.4f seconds... " % self.delay,
            time.sleep(self.delay)
            print "done."

            with gc:
                gc.set_fill_color(self.color_)
                gc.rect(*(self.position + self.bounds))
                gc.fill_path()

        else:
            with gc:
                gc.set_stroke_color(self.color_)
                gc.set_fill_color(self.color_)
                gc.set_line_width(1.0)
                gc.rect(*(self.position + self.bounds))
                gc.stroke_path()

                gc.set_font(font)
                x,y = self.position
                dx,dy = self.bounds
                tx, ty, tdx, tdy = gc.get_text_extent(str(self.delay))
                gc.set_text_position(x+dx/2-tdx/2, y+dy/2-tdy/2)
                gc.show_text(str(self.delay))

    def normal_left_down(self, event):
        self.event_state = "clicked"
        event.handled = True
        self.request_redraw()

    def clicked_left_up(self, event):
        self.event_state = "normal"
        event.handled = True
        self.request_redraw()


class MyContainer(Container):
    text_color = black_color_trait

    def _draw_container_mainlayer(self, gc, view_bounds=None, mode="default"):
        s = "Hold down the mouse button on the boxes."
        with gc:
            gc.set_font(font)
            gc.set_fill_color(self.text_color_)
            tx, ty, tdx, tdy = gc.get_text_extent(s)
            x,y = self.position
            dx,dy = self.bounds
            gc.set_text_position(x+dx/2-tdx/2, y+dy-tdy-20)
            gc.show_text(s)


class PlotFrame(DemoFrame):
    def _create_window(self):
        return Window(self, -1, component=container)


if __name__ == "__main__":
    times_and_bounds = {0.5 : (60,200,100,100),
                        0.33 : (240,200,100,100),
                        0.25: (60,50,100,100),
                        0.10: (240,50,100,100)}

    container = MyContainer(auto_size = False)
    for delay, bounds in times_and_bounds.items():
        box = Box()
        container.add(box)
        box.position = list(bounds[:2])
        box.bounds = list(bounds[2:])
        box.delay = delay

    # Save demo so that it doesn't get garbage collected when run within
    # existing event loop (i.e. from ipython).
    demo = demo_main(PlotFrame, size=(400, 400),
                     title="Latency Test - Click a box")