File: show_input.py

package info (click to toggle)
pyglet 1.5.27%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,356 kB
  • sloc: python: 98,028; ansic: 171; makefile: 148; sh: 9
file content (402 lines) | stat: -rwxr-xr-x 13,232 bytes parent folder | download | duplicates (2)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env python

"""Graphically show all devices available via the pyglet.input interface.

Each device is shown in its own collapsed panel.  Click on a device panel
to expand it, revealing that device's controls.  The controls show the
current live values, and flash white when the value changes.
"""

from __future__ import print_function

__docformat__ = 'restructuredtext'
__version__ = '$Id: $'

import pyglet
from pyglet import gl


class LineGroup(pyglet.graphics.OrderedGroup):
    def set_state(self):
        gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_LINE)

    def unset_state(self):
        gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL)


class Box:
    def __init__(self, batch, group=None,
                 stroke_color=(255, 255, 255, 255),
                 fill_color=(200, 200, 200, 255)):
        self.x1 = 0
        self.y1 = 0
        self.x2 = 0
        self.y2 = 0

        self.fill_vertices = batch.add(4, gl.GL_QUADS,
                                       pyglet.graphics.OrderedGroup(0, group),
                                       'v2f', ('c4B', fill_color * 4))
        self.stroke_vertices = batch.add(4, gl.GL_QUADS,
                                         LineGroup(1, group),
                                         'v2f', ('c4B', stroke_color * 4))

    def set_bounds(self, x1, y1, x2, y2):
        self.x1 = 0
        self.y1 = 0
        self.x2 = 0
        self.y2 = 0
        self.fill_vertices.vertices[:] = (x1, y1, x2, y1, x2, y2, x1, y2)
        self.stroke_vertices.vertices[:] = (x1, y1, x2, y1, x2, y2, x1 - 1, y2)

    def set_fill(self, r, g, b):
        self.fill_vertices.colors[:] = (r, g, b, 255) * 4

    def delete(self):
        self.fill_vertices.delete()
        self.stroke_vertices.delete()


class DevicePanel:
    BORDER_MARGIN = 5
    CONTENT_MARGIN = 8

    def __init__(self, device):
        self.device = device

        self.box = Box(batch, group=background_group,
                       stroke_color=(0, 0, 200, 255),
                       fill_color=(200, 200, 255, 255))
        self.name_label = pyglet.text.Label(device.name or 'Unknown device',
                                            font_size=10,
                                            color=(0, 0, 0, 255),
                                            anchor_y='top',
                                            batch=batch, group=text_group)
        self.manufacturer_label = pyglet.text.Label(device.manufacturer or '',
                                                    font_size=10,
                                                    color=(0, 0, 0, 255), anchor_x='right',
                                                    anchor_y='top',
                                                    batch=batch, group=text_group)

        self.is_open = False
        self.widgets = []

    def set_bounds(self, left, right, top):
        self.left = left
        self.right = right
        self.top = top
        self.layout()

    def layout_widgets(self):
        max_row_width = self.right - self.left - self.CONTENT_MARGIN * 2

        row = []
        row_width = 0
        row_height = 0

        def layout_row(row, x1, y1, x2, y2):
            x = x1
            for widget in row:
                widget.set_bounds(x,
                                  y1,
                                  x + widget.min_width,
                                  y1 + widget.min_height)
                x += widget.min_width

        y = self.bottom + self.CONTENT_MARGIN
        for widget in self.widgets:
            if widget is None or row_width + widget.min_width > max_row_width:
                layout_row(row,
                           self.left + self.CONTENT_MARGIN,
                           y - row_height,
                           self.right - self.CONTENT_MARGIN,
                           y)
                row = []
                y -= row_height
                row_width = 0

                if widget is None:
                    break

            row.append(widget)
            row_width += widget.min_width
            row_height = max(row_height, widget.min_height)

        self.bottom = y - self.CONTENT_MARGIN

    def layout(self):
        self.title_bottom = self.top - \
                            self.name_label.content_height - self.CONTENT_MARGIN * 2
        self.bottom = self.title_bottom
        if self.is_open:
            self.layout_widgets()

        self.box.set_bounds(self.left + self.BORDER_MARGIN,
                            self.bottom + self.BORDER_MARGIN,
                            self.right - self.BORDER_MARGIN,
                            self.top - self.BORDER_MARGIN)

        self.name_label.x = self.left + self.CONTENT_MARGIN
        self.name_label.y = self.top - self.CONTENT_MARGIN
        self.manufacturer_label.x = self.right - self.CONTENT_MARGIN
        self.manufacturer_label.y = self.top - self.CONTENT_MARGIN

    def hit_test(self, x, y):
        return self.left < x < self.right and self.title_bottom < y < self.top

    def toggle(self):
        if self.is_open:
            self.close()
        else:
            self.open()

    def open(self):
        if self.is_open:
            return

        try:
            self.device.open()
        except pyglet.input.DeviceException as e:
            try:
                self.device.open(window)
            except pyglet.input.DeviceException as e:
                print(e)  # TODO show error
                return

        window.set_mouse_cursor(window.get_system_mouse_cursor('wait'))
        for control in self.device.get_controls():
            if isinstance(control, pyglet.input.Button):
                widget = ButtonWidget(control, batch, group=text_group)
            else:
                widget = ControlWidget(control, batch, group=text_group)
            self.widgets.append(widget)

        if not self.widgets:
            self.widgets.append(NoControlsWidget(batch, group=text_group))

        self.widgets.append(None)
        window.set_mouse_cursor(None)

        self.is_open = True

    def close(self):
        if not self.is_open:
            return

        for widget in self.widgets:
            if widget:
                widget.delete()
        del self.widgets[:]

        self.device.close()

        self.is_open = False


class ControlWidget:
    BORDER_MARGIN = 2
    CONTENT_MARGIN = 4

    def __init__(self, control, batch, group=None):
        self.control_name = control.name
        if not self.control_name:
            self.control_name = control.raw_name
        self.box = Box(batch, pyglet.graphics.OrderedGroup(0, group))
        self.name_label = pyglet.text.Label(self.control_name,
                                            font_size=10,
                                            anchor_x='left',
                                            anchor_y='bottom',
                                            color=(0, 0, 0, 255),
                                            batch=batch,
                                            group=pyglet.graphics.OrderedGroup(1, group))
        self.value_label = pyglet.text.Label('          ',
                                             font_size=8,
                                             anchor_x='right',
                                             anchor_y='bottom',
                                             color=(0, 0, 0, 255),
                                             batch=batch,
                                             group=pyglet.graphics.OrderedGroup(1, group))

        self.min_width = \
            self.name_label.content_width + \
            self.value_label.content_width + self.CONTENT_MARGIN * 2
        self.min_height = self.name_label.content_height + self.CONTENT_MARGIN * 2

        self.relative = isinstance(control, pyglet.input.RelativeAxis)
        self.fade = 200

        self.control = control
        control.push_handlers(self)

    def set_bounds(self, x1, y1, x2, y2):
        self.box.set_bounds(
            x1 + self.BORDER_MARGIN,
            y1 + self.BORDER_MARGIN,
            x2 - self.BORDER_MARGIN,
            y2 - self.BORDER_MARGIN)
        self.name_label.x = x1 + self.CONTENT_MARGIN
        self.name_label.y = y1 + self.CONTENT_MARGIN
        self.value_label.x = x2 - self.CONTENT_MARGIN
        self.value_label.y = y1 + self.CONTENT_MARGIN

    def delete(self):
        if self in changed_widgets:
            changed_widgets.remove(self)
        self.control.remove_handlers(self)
        self.name_label.delete()
        self.value_label.delete()
        self.box.delete()

    def on_change(self, value):
        self.value = value
        self.fade = 255
        changed_widgets.add(self)

    def update(self):
        self.value_label.text = str(self.value)
        if self.relative and self.value:
            self.value = 0
            changed_widgets.add(self)

        self.box.set_fill(self.fade, self.fade, self.fade)
        if self.fade > 200:
            self.fade = max(200, self.fade - 10)
            changed_widgets.add(self)


class ButtonWidget(ControlWidget):
    BORDER_MARGIN = 2
    CONTENT_MARGIN = 4

    def __init__(self, control, batch, group=None):
        self.control_name = control.name
        if not self.control_name:
            self.control_name = control.raw_name
        self.box = Box(batch, pyglet.graphics.OrderedGroup(0, group))
        self.name_label = pyglet.text.Label(self.control_name,
                                            font_size=10,
                                            anchor_x='center',
                                            anchor_y='bottom',
                                            color=(0, 0, 0, 255),
                                            batch=batch,
                                            group=pyglet.graphics.OrderedGroup(1, group))

        self.min_width = self.name_label.content_width + self.CONTENT_MARGIN * 2
        self.min_height = self.name_label.content_height + self.CONTENT_MARGIN * 2

        self.fade = 200

        self.control = control
        control.push_handlers(self)

    def set_bounds(self, x1, y1, x2, y2):
        self.box.set_bounds(
            x1 + self.BORDER_MARGIN,
            y1 + self.BORDER_MARGIN,
            x2 - self.BORDER_MARGIN,
            y2 - self.BORDER_MARGIN)
        self.name_label.x = (x1 + x2) // 2
        self.name_label.y = y1 + self.CONTENT_MARGIN

    def delete(self):
        if self in changed_widgets:
            changed_widgets.remove(self)
        self.control.remove_handlers(self)
        self.name_label.delete()
        self.box.delete()

    def on_change(self, value):
        self.value = value
        if value:
            self.fade = 255
        changed_widgets.add(self)

    def update(self):
        self.box.set_fill(self.fade, self.fade, self.fade)
        if not self.value and self.fade > 200:
            self.fade = max(200, self.fade - 10)
            changed_widgets.add(self)


class NoControlsWidget:
    CONTENT_MARGIN = 4

    def __init__(self, batch, group):
        self.label = pyglet.text.Label('No controls on this device.',
                                       font_size=10,
                                       color=(0, 0, 0, 255),
                                       anchor_y='bottom',
                                       batch=batch,
                                       group=group)

        self.min_width = self.label.content_width + self.CONTENT_MARGIN * 2
        self.min_height = self.label.content_height + self.CONTENT_MARGIN * 2

    def set_bounds(self, x1, y1, x2, y2):
        self.label.x = x1 + ControlWidget.CONTENT_MARGIN
        self.label.y = y1 + ControlWidget.CONTENT_MARGIN

    def delete(self):
        self.label.delete()


window = pyglet.window.Window(caption='Input Devices', resizable=True)
batch = pyglet.graphics.Batch()
background_group = pyglet.graphics.OrderedGroup(0)
text_group = pyglet.graphics.OrderedGroup(1)

panels = [DevicePanel(device) for device in pyglet.input.get_devices()]
help_label = pyglet.text.Label(
    'Click on a device name to show or hide its controls.',
    x=DevicePanel.CONTENT_MARGIN,
    anchor_y='top',
    font_size=10,
    color=(255, 255, 255, 255),
    batch=batch,
    group=background_group)


def layout_panels():
    y = window.height
    for panel in panels:
        panel.set_bounds(left=0, right=window.width, top=y)
        y = panel.bottom
    help_label.y = y


@window.event
def on_draw():
    gl.glClearColor(0.3, 0.3, 0.4, 1.0)
    window.clear()
    batch.draw()
    window.invalid = False


@window.event
def on_resize(width, height):
    layout_panels()
    window.invalid = True
    return pyglet.event.EVENT_UNHANDLED


@window.event
def on_mouse_press(x, y, button, modifiers):
    for panel in panels:
        if panel.hit_test(x, y):
            panel.toggle()
            layout_panels()
            window.invalid = True


changed_widgets = set()


def update(dt):
    pending = list(changed_widgets)
    changed_widgets.clear()
    for widget in pending:
        widget.update()
    window.invalid = True


pyglet.clock.schedule_interval(update, 0.05)
pyglet.app.run()