Hi

I want to allow the user to enter text on screen. As I couldn't see
anything in Cocos I looked at Pyglet. There is a example called
text_input.py. If there is an easy way I missd I'd love to hear that
too.

Now I tried to get this demo to work in Cocos with only limited
success and I was wondering if anyone can point out what I was doing
wrong.

When I run the code below I see the label, Name. If the mouse cursor
is moved to the right of Name it eventually switches to a Text input
cursor. Clicking when the mouse cursor is a text cursor registers as
being over the text widgit.

Some of the events are printed if it is run from the command line.

My first question is why I cannot see the text that I think is in the
text entry box? So the problem is probably in the TextWidget class. I
might be able to get the rest working once I can see the initial text.

I'm out of ideas and things I can try, I suspect I need to add it to
the layer somehow but I don't know enough about the internals of Cocos
to make a type that inherits from
pyglet.text.document.UnformattedDocument and CocosNode. Tried
following some of the source in cocos/text.py but didn't quite get
what it was working.

My hacked code is below, pyglet example is in the examples
subdirectory of the pyglet docs section.

Any help appreciated

Thanks

import pyglet
import cocos
from cocos.director import *
from cocos.text import Label, RichLabel
from cocos.cocosnode import CocosNode

class TextWidget(CocosNode):
    def __init__(self, text, x, y, width, batch):
        super(TextWidget, self).__init__()
        self.document = pyglet.text.document.UnformattedDocument(text)
        self.document.set_style(0, len(self.document.text),
            dict(color=(0, 0, 0, 255))
        )
        font = self.document.get_font()
        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

    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 textInputLayer(cocos.layer.Layer):
    # For the layer to receive events this variable must be set to
'True'
    is_event_handler = True

    def __init__(self, *args, **kwargs):
        super(textInputLayer, self).__init__()

        self.batch = pyglet.graphics.Batch()
        self.labels = [
            Label('Name', x=10, y=100, anchor_y='bottom', color=(0, 0,
0, 255)),
        ]
        map(self.add, self.labels)

        self.width, self.height = director.get_window_size()
        self.widgets = [
             TextWidget('Zip', 200, 100, self.width - 210,
self.batch),
        ]
        self.text_cursor =
director.window.get_system_mouse_cursor('text')
        map(self.add, self.widgets)

        self.focus = None
        self.set_focus(self.widgets[0])

    def on_draw(self):
        pyglet.gl.glClearColor(1, 1, 1, 1)
        director.window.clear()
        self.batch.draw()

    def on_mouse_motion(self, x, y, dx, dy):
        for widget in self.widgets:
            if widget.hit_test(x, y):
                director.window.set_mouse_cursor(self.text_cursor)
                break
        else:
            #self.set_mouse_cursor(None)
            director.window.set_mouse_cursor(None)

    def on_mouse_press(self, x, y, button, modifiers):
        print 'db: on_mouse_press'
        for widget in self.widgets:
            if widget.hit_test(x, y):
                print "Hit a widget"
                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):
        print 'db: on_mouse_drag'
        if self.focus:
            self.focus.caret.on_mouse_drag(x, y, dx, dy, buttons,
modifiers)

    def on_text(self, text):
        print 'db: on_text'
        if self.focus:
            self.focus.caret.on_text(text)

    def on_text_motion(self, motion):
        print 'db: on_text_motion'
        if self.focus:
            self.focus.caret.on_text_motion(motion)

    def on_text_motion_select(self, motion):
        print 'db: on_text_motion_select'
        if self.focus:
            self.focus.caret.on_text_motion_select(motion)

    def on_key_press(self, symbol, modifiers):
        print 'db: on_key_press'
        if 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):
        print 'db: set_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)

# basic test code
if __name__ == "__main__":

  from cocos.director import director
  from pyglet import font

  cocos.director.director.init()
  text_layer = textInputLayer ()
  main_scene = cocos.scene.Scene (text_layer)
  cocos.director.director.run (main_scene)



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"cocos2d discuss" 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/cocos-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to