1# ----------------------------------------------------------------------------
2# pyglet
3# Copyright (c) 2006-2008 Alex Holkner
4# Copyright (c) 2008-2020 pyglet contributors
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10#
11#  * Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13#  * Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in
15#    the documentation and/or other materials provided with the
16#    distribution.
17#  * Neither the name of pyglet nor the names of its
18#    contributors may be used to endorse or promote products
19#    derived from this software without specific prior written
20#    permission.
21#
22# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33# POSSIBILITY OF SUCH DAMAGE.
34# ----------------------------------------------------------------------------
35
36"""Mouse constants and utilities for pyglet.window.
37"""
38
39
40class MouseStateHandler(dict):
41    """Simple handler that tracks the state of buttons from the mouse. If a
42    button is pressed then this handler holds a True value for it.
43
44    For example::
45
46        >>> win = window.Window
47        >>> mousebuttons = mouse.MouseStateHandler()
48        >>> win.push_handlers(mousebuttons)
49
50        # Hold down the "left" button...
51
52        >>> mousebuttons[mouse.LEFT]
53        True
54        >>> mousebuttons[mouse.RIGHT]
55        False
56
57    """
58    def on_mouse_press(self, x, y, button, modifiers):
59        self[button] = True
60
61    def on_mouse_release(self, x, y, button, modifiers):
62        self[button] = False
63
64    def __getitem__(self, key):
65        return self.get(key, False)
66
67
68def buttons_string(buttons):
69    """Return a string describing a set of active mouse buttons.
70
71    Example::
72
73        >>> buttons_string(LEFT | RIGHT)
74        'LEFT|RIGHT'
75
76    :Parameters:
77        `buttons` : int
78            Bitwise combination of mouse button constants.
79
80    :rtype: str
81    """
82    button_names = []
83    if buttons & LEFT:
84        button_names.append('LEFT')
85    if buttons & MIDDLE:
86        button_names.append('MIDDLE')
87    if buttons & RIGHT:
88        button_names.append('RIGHT')
89    return '|'.join(button_names)
90
91
92# Symbolic names for the mouse buttons
93LEFT = 1 << 0
94MIDDLE = 1 << 1
95RIGHT = 1 << 2
96