Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layers should have an access to their own width/height #282

Closed
Sbelletier opened this issue Jan 28, 2017 · 2 comments
Closed

Layers should have an access to their own width/height #282

Sbelletier opened this issue Jan 28, 2017 · 2 comments

Comments

@Sbelletier
Copy link

As from
http://stackoverflow.com/questions/41831063/how-to-find-layer-width-height-with-cocos-python

Layers should have a member that allows setting/getting of their current height/width (Scale alone just doesn't do the trick, it needs at least a second attribute that'd be width/height BEFORE scaling).
This would allow for more precise sizing of elements relative to others, more precise positioning relative to the size of the elements they interact with... etc.

This is especially important since cocos2d doesn't seem to have any layouts (though layouts would be unsufficient by themselves to solve this problem).

Is it possible to adress the problem without breaking the current architecture ?

Versions used in the stackoverflow snippet :
cocos2d v0.6.4
pyglet v1.2.4

@dangillet
Copy link
Contributor

dangillet commented Jan 28, 2017

Hello,

After reading the Stack Overflow post, I think you meant Label and not Layer.

Layer don't have a concept of width or height. You could actually imagine that they have an infinite width and height. They just have a position. All CocosNode children will be positioned relative to their own position.

In the example you showed, the Layer has two children, both a Label instance. In order to position the second Label in relation with the first Label you might want to know its width.

As explained in the documentation, cocos.text.Label is just a wrapper around a pyglet Label. The information regarding the width of the Label is as such contained in the underlying Label that you can find under the attribute element.

Although not super obvious in the pyglet documentation, the pyglet.text.Label inherits from pyglet.text.layout.TextLayout. There you can find that

Label and HTMLLabel provide a convenient interface to this class.

Variables:

  • content_width – Calculated width of the text in the layout. This may overflow the desired width if word-wrapping failed.
  • content_height – Calculated height of the text in the layout.

That's why you need to access to the content width of the cocos.text.Label with a self.basic_text.element.content_width.

I agree that these kind of attributes could be directly exposed through the cocos.text.Label API.
@ccanepa: how do you feel about this?

Dan

@Sbelletier
Copy link
Author

thanks for the clarification.
Indeed, i think that the documentation is a bit confusing for newbies like me as is ^^.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants