Re: The use of pywidget with cocos2d

39 views
Skip to first unread message

claudio canepa

unread,
Apr 6, 2013, 2:26:00 PM4/6/13
to cocos2d discuss

On Sat, Apr 6, 2013 at 12:30 PM, Doug Linder <douglas...@gmail.com> wrote:
Hey there,

I've been using pywidget (https://pywidget.googlecode.com/svn/trunk) with cocos2d quite successfully as a GUI, but in order to do so I've had to hack the cocos director class.

Basically, modifying Director::on_draw to read, at the end:

       # Draw the GUI
        if self.gui != None:
          self.gui.on_draw()

Where you pass in the appropriate pywidget gui object by assigning it to the director:

class Client:
  def run(self):

    cocos.director.director.init()
    cocos.director.director.gui = dialog
    cocos.director.director.window.push_handlers(dialog)

    ui = rpg.ui.Test()
    main_scene = cocos.scene.Scene (ui)
    cocos.director.director.run (main_scene)

This seems fairly successful in that the cocos event handlers still work, and the event bindings for the UI work, so all in all, pretty good.

However, it's annoying having to patch the cocos source to do this.

I feel like I should just be able to extend CocosNode and override the draw() method, then add the object to the scene, but my draw() function never seems to get called when I do this.


If you subclass CocosNode , instantiate your subclass and adds to an active scene, then his visit and draw methods will be called [0].

If you show code for your CocosNode attempt that is not working for you 
maybe a fix can be suggested.
 
Is there some special magic I need to do?


I don't think so
 
I'd prefer not to hack up the source just to have a gui if I can avoid it.


Yeah.
Pastebin your original attemp (not modifyng director) , or a simplified one and w'ill see.



[0] Technically your overriden visit can decide to skip calls to draw, drawing the node and childs with custom code, presumably for performance reasons.


Doug Linder

unread,
Apr 7, 2013, 1:29:21 AM4/7/13
to cocos-...@googlegroups.com
Example code:
http://pastebin.com/8pbrJ9xJ

When I add print('Draw') to the draw function, I get no terminal output.

Can you see what's wrong with it?

~
Doug.

hell...@gmail.com

unread,
Apr 7, 2013, 10:20:19 AM4/7/13
to cocos-...@googlegroups.com
Running your example code from pastebin works if I comment out the pywidget imports and the entire on_enter method. The draw method is called. Whenever I press some keyboard buttons it will print to the console. In addition it is called two times when the program runs without any action on my part.

claudio canepa

unread,
Apr 7, 2013, 12:02:55 PM4/7/13
to cocos2d discuss
Well, running the code shows nothing for me.
I totally expect that because you define some classes but instantiate anything :-)

I suppose that you forgot to paste some more lines.

Adding

if __name__ == '__main__':
      client = Client()
      client.run()

at the end of your script I got a visual of a live widget: it displays, can be dragged, reacts visually and in console to mouse clicks.
Snapshot attached.

Your code looks right [0]

If you can confirm that adding the two lines you don't see a widget, then I would suspect you need to set some gl state (expected by pywidget) just before calling draw.
As a blind and easy stab,  you can try adding 
     glColor4ub(255, 255, 255, 255)

just before the line
    self.dialog.on_draw()



[0]nitpick: you are not calling super in the overrided methods on_enter and on_exit, which in general will stop the propagation of on_enter on_exit to childs.
Is not a problem with this particular script because the node has no childs, but is better to be consistent in the code. 

 



--
You received this message because you are subscribed to the Google Groups "cocos2d discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cocos-discus...@googlegroups.com.
To post to this group, send email to cocos-...@googlegroups.com.
Visit this group at http://groups.google.com/group/cocos-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

pywidget_in_cocos.png

claudio canepa

unread,
Apr 7, 2013, 2:47:49 PM4/7/13
to cocos2d discuss
follow - up:

probably not impacting on your visibility issue but your CocosNode subclass should at least do:

def draw(self):
      glPushMatrix()
      self.transform()
      self.dialog.on_draw()
      glPopMatrix()

Otherwise the node position, rotation and transform_anchor will be ignored 



Doug Linder

unread,
Apr 8, 2013, 12:27:46 AM4/8/13
to cocos-...@googlegroups.com
Wow, tried on a different machine and it works like a charm.

Looks like just my mac mini is being weird.

Might try burning python away and reinstalling the whole lot from scratch and see if that helps.

Thanks for you help, for what it's worth.

~
Doug.
Reply all
Reply to author
Forward
0 new messages