1# ----------------------------------------------------------------------------
2# pyglet
3# Copyright (c) 2006-2008 Alex Holkner
4# Copyright (c) 2008-2021 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"""Events for :py:mod:`pyglet.window`.
37
38See :py:class:`~pyglet.window.Window` for a description of the window event types.
39"""
40
41import sys
42
43from pyglet.window import key
44from pyglet.window import mouse
45
46
47class WindowEventLogger:
48    """Print all events to a file.
49
50    When this event handler is added to a window it prints out all events
51    and their parameters; useful for debugging or discovering which events
52    you need to handle.
53
54    Example::
55
56        win = window.Window()
57        win.push_handlers(WindowEventLogger())
58
59    """
60    def __init__(self, logfile=None):
61        """Create a `WindowEventLogger` which writes to `logfile`.
62
63        :Parameters:
64            `logfile` : file-like object
65                The file to write to.  If unspecified, stdout will be used.
66
67        """
68        if logfile is None:
69            logfile = sys.stdout
70        self.file = logfile
71
72    def on_key_press(self, symbol, modifiers):
73        print('on_key_press(symbol=%s, modifiers=%s)' % (
74            key.symbol_string(symbol), key.modifiers_string(modifiers)), file=self.file)
75
76    def on_key_release(self, symbol, modifiers):
77        print('on_key_release(symbol=%s, modifiers=%s)' % (
78            key.symbol_string(symbol), key.modifiers_string(modifiers)), file=self.file)
79
80    def on_text(self, text):
81        print('on_text(text=%r)' % text, file=self.file)
82
83    def on_text_motion(self, motion):
84        print('on_text_motion(motion=%s)' % (
85            key.motion_string(motion)), file=self.file)
86
87    def on_text_motion_select(self, motion):
88        print('on_text_motion_select(motion=%s)' % (
89            key.motion_string(motion)), file=self.file)
90
91    def on_mouse_motion(self, x, y, dx, dy):
92        print('on_mouse_motion(x=%d, y=%d, dx=%d, dy=%d)' % (
93            x, y, dx, dy), file=self.file)
94
95    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
96        print('on_mouse_drag(x=%d, y=%d, dx=%d, dy=%d, buttons=%s, modifiers=%s)' % (
97              x, y, dx, dy, mouse.buttons_string(buttons), key.modifiers_string(modifiers)),
98              file=self.file)
99
100    def on_mouse_press(self, x, y, button, modifiers):
101        print('on_mouse_press(x=%d, y=%d, button=%r, modifiers=%s)' % (
102            x, y, mouse.buttons_string(button), key.modifiers_string(modifiers)), file=self.file)
103
104    def on_mouse_release(self, x, y, button, modifiers):
105        print('on_mouse_release(x=%d, y=%d, button=%r, modifiers=%s)' % (
106            x, y, mouse.buttons_string(button), key.modifiers_string(modifiers)), file=self.file)
107
108    def on_mouse_scroll(self, x, y, dx, dy):
109        print('on_mouse_scroll(x=%f, y=%f, dx=%f, dy=%f)' % (
110            x, y, dx, dy), file=self.file)
111
112    def on_close(self):
113        print('on_close()', file=self.file)
114
115    def on_mouse_enter(self, x, y):
116        print('on_mouse_enter(x=%d, y=%d)' % (x, y), file=self.file)
117
118    def on_mouse_leave(self, x, y):
119        print('on_mouse_leave(x=%d, y=%d)' % (x, y), file=self.file)
120
121    def on_expose(self):
122        print('on_expose()', file=self.file)
123
124    def on_resize(self, width, height):
125        print('on_resize(width=%d, height=%d)' % (width, height), file=self.file)
126
127    def on_move(self, x, y):
128        print('on_move(x=%d, y=%d)' % (x, y), file=self.file)
129
130    def on_activate(self):
131        print('on_activate()', file=self.file)
132
133    def on_deactivate(self):
134        print('on_deactivate()', file=self.file)
135
136    def on_show(self):
137        print('on_show()', file=self.file)
138
139    def on_hide(self):
140        print('on_hide()', file=self.file)
141
142    def on_context_lost(self):
143        print('on_context_lost()', file=self.file)
144
145    def on_context_state_lost(self):
146        print('on_context_state_lost()', file=self.file)
147
148    def on_draw(self):
149        print('on_draw()', file=self.file)
150