As a part of working out how to allow the user to enter text with
different styles I modified the example file text_input.py
The mods allow the user to toggle the bold and italic styles, using
<ctrl> b and <ctrl> i, as they enter the text. Uses the functionality
provided by the Caret class to do this, as well as changing the style
of a highlighted region.
Nothing fantastic but does show a few more capabilities of text entry
with almost the same code.
Code is below in case anyone is interested. Is there a way to attach a
file to these posts rather than pasting in the code.
Regards
john
------------------- Code below
#!/usr/bin/env python
'''Demonstrates basic use of IncrementalTextLayout and Caret.
A simple widget-like system is created in this example supporting
keyboard and
mouse focus.
'''
__docformat__ = 'restructuredtext'
__version__ = '$Id: $'
import pyglet
from pyglet.text import *
from pyglet.text.document import *
class Rectangle(object):
'''Draws a rectangle into a batch.'''
def __init__(self, x1, y1, x2, y2, batch):
self.vertex_list = batch.add(4, pyglet.gl.GL_QUADS, None,
('v2i', [x1, y1, x2, y1, x2, y2, x1, y2]),
('c4B', [200, 200, 220, 255] * 4)
)
class TextWidget(object):
def __init__(self, text, x, y, width, batch):
# Original
# self.document =
pyglet.text.document.UnformattedDocument(text)
self.document = pyglet.text.document.FormattedDocument(text)
self.document.set_style(0, len(self.document.text),
dict(color=(0, 0, 0, 255))
)
font = self.document.get_font(0)
height = font.ascent - font.descent
self.layout = pyglet.text.layout.IncrementalTextLayout(
self.document, width, height, multiline=False,
batch=batch)
self.caret = pyglet.text.caret.Caret(self.layout)
self.layout.x = x
self.layout.y = y
# Rectangular outline
pad = 2
self.rectangle = Rectangle(x - pad, y - pad,
x + width + pad, y + height + pad,
batch)
def hit_test(self, x, y):
return (0 < x - self.layout.x < self.layout.width and
0 < y - self.layout.y < self.layout.height)
class Window(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(400, 180, caption='Text entry')
self.batch = pyglet.graphics.Batch()
self.labels = [
pyglet.text.Label('Name', x=10, y=140, anchor_y='bottom',
color=(0, 0, 0, 255), batch=self.batch),
pyglet.text.Label('Species', x=10, y=100,
anchor_y='bottom',
color=(0, 0, 0, 255), batch=self.batch),
pyglet.text.Label('Special abilities', x=10, y=60,
anchor_y='bottom', color=(0, 0, 0,
255),
batch=self.batch),
]
self.comment = pyglet.text.Label('<ctrl>b toggles bold,
<ctrl>i toggles italic', x=10, y=20,
anchor_y='bottom', color=(128, 128, 128,
255),
batch=self.batch)
self.widgets = [
TextWidget('', 200, 140, self.width - 210, self.batch),
TextWidget('', 200, 100, self.width - 210, self.batch),
TextWidget('', 200, 60, self.width - 210, self.batch)
]
self.text_cursor = self.get_system_mouse_cursor('text')
self.focus = None
self.set_focus(self.widgets[0])
def ToggleStyle(self, attribute):
"""Toggle the style in the active text window there are a number
of cases
that must be handled:
1 At end of text - toggle flag
2 In the middle of text - change flag on both sides, in
opposite directions
3 Block of text is highlighted - more complicated
cases 1, 2 nad 3 (where the block highlighted has only one
style) are
handled by the Caret class.
Where the block highlighted has more than a single style this
demo just
sets the style to true; ie turns it on.
Parameters
attribute string that is a legal pyglet style attribute
"""
if self.focus:
attrVal = self.focus.caret.get_style(attribute)
if attrVal == pyglet.text.document.STYLE_INDETERMINATE:
# selection has 2, or more, different style flags
# just assume we want to turn it on
attrVal = False
#print "%s attr = " % attribute, str(attrVal)
if attrVal is None or attrVal == False:
self.focus.caret.set_style({attribute:True})
else:
self.focus.caret.set_style({attribute:False})
def on_resize(self, width, height):
super(Window, self).on_resize(width, height)
for widget in self.widgets:
widget.width = width - 110
def on_draw(self):
pyglet.gl.glClearColor(1, 1, 1, 1)
self.clear()
self.batch.draw()
def on_mouse_motion(self, x, y, dx, dy):
for widget in self.widgets:
if widget.hit_test(x, y):
self.set_mouse_cursor(self.text_cursor)
break
else:
self.set_mouse_cursor(None)
def on_mouse_press(self, x, y, button, modifiers):
for widget in self.widgets:
if widget.hit_test(x, y):
self.set_focus(widget)
break
else:
self.set_focus(None)
if self.focus:
self.focus.caret.on_mouse_press(x, y, button, modifiers)
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
if self.focus:
self.focus.caret.on_mouse_drag(x, y, dx, dy, buttons,
modifiers)
def on_text(self, text):
if self.focus:
self.focus.caret.on_text(text)
def on_text_motion(self, motion):
if self.focus:
self.focus.caret.on_text_motion(motion)
def on_text_motion_select(self, motion):
if self.focus:
self.focus.caret.on_text_motion_select(motion)
def on_key_press(self, symbol, modifiers):
# style modifiers
if symbol == pyglet.window.key.B and modifiers &
pyglet.window.key.MOD_CTRL:
self.ToggleStyle('bold')
elif symbol == pyglet.window.key.I and modifiers &
pyglet.window.key.MOD_CTRL:
self.ToggleStyle('italic')
elif symbol == pyglet.window.key.TAB:
if modifiers & pyglet.window.key.MOD_SHIFT:
dir = -1
else:
dir = 1
if self.focus in self.widgets:
i = self.widgets.index(self.focus)
else:
i = 0
dir = 0
self.set_focus(self.widgets[(i + dir) %
len(self.widgets)])
elif symbol == pyglet.window.key.ESCAPE:
pyglet.app.exit()
def set_focus(self, focus):
if self.focus:
self.focus.caret.visible = False
self.focus.caret.mark = self.focus.caret.position = 0
self.focus = focus
if self.focus:
self.focus.caret.visible = True
self.focus.caret.mark = 0
self.focus.caret.position = len(self.focus.document.text)
window = Window(resizable=True)
pyglet.app.run()
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---