| 1 |
"""Demonstrates basic use of IncrementalTextLayout and Caret. |
| 2 |
|
| 3 |
A simple widget-like system is created in this example supporting keyboard and |
| 4 |
mouse focus. |
| 5 |
""" |
| 6 |
|
| 7 |
import pyglet |
| 8 |
|
| 9 |
|
| 10 |
class TextWidget: |
| 11 |
def __init__(self, text, x, y, width, batch, window): |
| 12 |
self.document = pyglet.text.document.UnformattedDocument(text) |
| 13 |
self.document.set_style(0, len(self.document.text), dict(color=(0, 0, 0, 255))) |
| 14 |
font = self.document.get_font() |
| 15 |
height = font.ascent - font.descent |
| 16 |
|
| 17 |
self.layout = pyglet.text.layout.IncrementalTextLayout(self.document, x, y, 0, width, height, |
| 18 |
batch=batch) |
| 19 |
self.caret = pyglet.text.caret.Caret(self.layout, window=window) |
| 20 |
|
| 21 |
pad = 2 |
| 22 |
self.rectangle = pyglet.shapes.Rectangle(x - pad, y - pad, width + pad, height + pad, |
| 23 |
color=(200, 200, 220), batch=batch) |
| 24 |
|
| 25 |
def hit_test(self, x, y): |
| 26 |
return (0 < x - self.layout.x < self.layout.width and |
| 27 |
0 < y - self.layout.y < self.layout.height) |
| 28 |
|
| 29 |
|
| 30 |
class Window(pyglet.window.Window): |
| 31 |
def __init__(self, *args, **kwargs): |
| 32 |
super().__init__(400, 140, caption='Text entry', *args, **kwargs) |
| 33 |
|
| 34 |
self.batch = pyglet.graphics.Batch() |
| 35 |
self.labels = [ |
| 36 |
pyglet.text.Label('Name', x=10, y=100, anchor_y='bottom', |
| 37 |
color=(0, 0, 0, 255), batch=self.batch), |
| 38 |
pyglet.text.Label('Species', x=10, y=60, anchor_y='bottom', |
| 39 |
color=(0, 0, 0, 255), batch=self.batch), |
| 40 |
pyglet.text.Label('Special abilities', x=10, y=20, |
| 41 |
anchor_y='bottom', color=(0, 0, 0, 255), |
| 42 |
batch=self.batch), |
| 43 |
] |
| 44 |
self.widgets = [ |
| 45 |
TextWidget('This is a test', 200, 100, self.width - 210, self.batch, self), |
| 46 |
TextWidget('This is a test', 200, 60, self.width - 210, self.batch, self), |
| 47 |
TextWidget('This is a test', 200, 20, self.width - 210, self.batch, self), |
| 48 |
] |
| 49 |
self.text_cursor = self.get_system_mouse_cursor('text') |
| 50 |
|
| 51 |
self.focus = None |
| 52 |
self.set_focus(self.widgets[0]) |
| 53 |
|
| 54 |
def on_resize(self, width, height): |
| 55 |
super().on_resize(width, height) |
| 56 |
for widget in self.widgets: |
| 57 |
widget.width = width - 110 |
| 58 |
|
| 59 |
def on_draw(self): |
| 60 |
pyglet.gl.glClearColor(1, 1, 1, 1) |
| 61 |
self.clear() |
| 62 |
self.batch.draw() |
| 63 |
|
| 64 |
def on_mouse_motion(self, x, y, dx, dy): |
| 65 |
for widget in self.widgets: |
| 66 |
if widget.hit_test(x, y): |
| 67 |
self.set_mouse_cursor(self.text_cursor) |
| 68 |
break |
| 69 |
else: |
| 70 |
self.set_mouse_cursor(None) |
| 71 |
|
| 72 |
def on_mouse_press(self, x, y, button, modifiers): |
| 73 |
for widget in self.widgets: |
| 74 |
if widget.hit_test(x, y): |
| 75 |
self.set_focus(widget) |
| 76 |
break |
| 77 |
else: |
| 78 |
self.set_focus(None) |
| 79 |
|
| 80 |
if self.focus: |
| 81 |
self.focus.caret.on_mouse_press(x, y, button, modifiers) |
| 82 |
|
| 83 |
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers): |
| 84 |
if self.focus: |
| 85 |
self.focus.caret.on_mouse_drag(x, y, dx, dy, buttons, modifiers) |
| 86 |
|
| 87 |
def on_text(self, text): |
| 88 |
if self.focus: |
| 89 |
self.focus.caret.on_text(text) |
| 90 |
|
| 91 |
def on_text_motion(self, motion): |
| 92 |
if self.focus: |
| 93 |
self.focus.caret.on_text_motion(motion) |
| 94 |
|
| 95 |
def on_text_motion_select(self, motion): |
| 96 |
if self.focus: |
| 97 |
self.focus.caret.on_text_motion_select(motion) |
| 98 |
|
| 99 |
def on_key_press(self, symbol, modifiers): |
| 100 |
if symbol == pyglet.window.key.TAB: |
| 101 |
if modifiers & pyglet.window.key.MOD_SHIFT: |
| 102 |
direction = -1 |
| 103 |
else: |
| 104 |
direction = 1 |
| 105 |
|
| 106 |
if self.focus in self.widgets: |
| 107 |
i = self.widgets.index(self.focus) |
| 108 |
else: |
| 109 |
i = 0 |
| 110 |
direction = 0 |
| 111 |
|
| 112 |
self.set_focus(self.widgets[(i + direction) % len(self.widgets)]) |
| 113 |
|
| 114 |
elif symbol == pyglet.window.key.ESCAPE: |
| 115 |
pyglet.app.exit() |
| 116 |
|
| 117 |
def set_focus(self, focus): |
| 118 |
if focus is self.focus: |
| 119 |
return |
| 120 |
|
| 121 |
if self.focus: |
| 122 |
self.focus.caret.visible = False |
| 123 |
self.focus.caret.mark = self.focus.caret.position = 0 |
| 124 |
|
| 125 |
self.focus = focus |
| 126 |
if self.focus: |
| 127 |
self.focus.caret.visible = True |
| 128 |
|
| 129 |
|
| 130 |
window = Window(resizable=True) |
| 131 |
pyglet.app.run() |