File size: 2,324 Bytes
d82cf6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Mouse constants and utilities for pyglet.window.
"""


class MouseStateHandler:
    """Simple handler that tracks the state of buttons from the mouse. If a
    button is pressed then this handler holds a True value for it.
    If the window loses focus, all buttons will be reset to False in order
    to avoid a "sticky" button state.

    For example::

        >>> win = window.Window()
        >>> mousebuttons = mouse.MouseStateHandler()
        >>> win.push_handlers(mousebuttons)

        # Hold down the "left" button...

        >>> mousebuttons[mouse.LEFT]
        True
        >>> mousebuttons[mouse.RIGHT]
        False

    """

    def __init__(self):
        self.data = {
            "x": 0,
            "y": 0,
        }

    def on_mouse_press(self, x, y, button, modifiers):
        self.data[button] = True

    def on_mouse_release(self, x, y, button, modifiers):
        self.data[button] = False

    def on_deactivate(self):
        self.data.clear()

    def on_mouse_motion(self, x, y, dx, dy):
        self.data["x"] = x
        self.data["y"] = y

    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
        self.data["x"] = x
        self.data["y"] = y

    def __getitem__(self, key):
        return self.data.get(key, False)


def buttons_string(buttons):
    """Return a string describing a set of active mouse buttons.

    Example::

        >>> buttons_string(LEFT | RIGHT)
        'LEFT|RIGHT'

    :Parameters:
        `buttons` : int
            Bitwise combination of mouse button constants.

    :rtype: str
    """
    button_names = []
    if buttons & LEFT:
        button_names.append("LEFT")
    if buttons & MIDDLE:
        button_names.append("MIDDLE")
    if buttons & RIGHT:
        button_names.append("RIGHT")
    if buttons & MOUSE4:
        button_names.append("MOUSE4")
    if buttons & MOUSE5:
        button_names.append("MOUSE5")
    return "|".join(button_names)


#: Constant for the left mouse button.
#:
#: :meta hide-value:
LEFT = 1 << 0
#: Constant for the middle mouse button.
#:
#: :meta hide-value:
MIDDLE = 1 << 1
#: Constant for the right mouse button.
#:
#: :meta hide-value:
RIGHT = 1 << 2
#: Constant for the mouse4 button.
#:
#: :meta hide-value:
MOUSE4 = 1 << 3
#: Constant for the mouse5 button.
#:
#: :meta hide-value:
MOUSE5 = 1 << 4