How do you modify the batch an object belongs to outside of its constructor? Trying to edit the batch property directly doesn't appear to work with Layouts, though it works fine with Sprites.
Here's the code for the class I'm testing:
class Text_Widget(object):
def __init__(self, text, batch=None, group=None, attribs=None):
self.bar = attribs
self.x = self.bar.x
self.y = self.bar.y
self.width = self.bar.width
self.height = self.bar.height
self.document = pyglet.text.document.UnformattedDocument(text)
self.document.set_style(0, len(self.document.text),
dict(color=(255, 255, 255, 255), font_size=12))
self.layout = pyglet.text.layout.IncrementalTextLayout(self.document,
width=self.width, height=self.height, batch=batch, multiline=False, group=group)
self.layout.x = self.x
self.layout.y = self.y
def on_mouse_press(self, x, y, button, modifier):
print 'text widget is hit? ', self.hit_test(x,y)
def hit_test(self, x,y):
return (x > self.x and x< self.x + self.width and
y > self.y and y< self.y + self.height)
The attribs parameter is just a sprite object of a textBox image that I wanted the Layout attributes to match. The rest is basically copy/paste from the text_input.py pyglet example.
In my main, I set up the following:
text_entry_area = Text_Widget(text='Hello World!!', attribs=text_bar, group=foreground, batch=main_batch)
And that works fine. The main on_draw(): main_batch.draw() displays it just fine. However, If I try something like this:
main_batch = pyglet.graphics.Batch()
text_entry_area = Text_Widget(text='Hello World!!', attribs=text_bar, group=foreground, batch=main_batch)
text_entry_area.layout.batch = main_batch
Nothing gets displayed at runtime. The on_draw(): main_batch.draw() doesn't draw as expected.
So, how does one go about adding/removing objects from a batch?