Is there any Cocos functionality which allows you to draw a border around a node?

Topics: Sprites and SpriteBatch
May 18, 2014 at 3:48 PM
Edited May 18, 2014 at 3:48 PM
Just wondering if there is any Cocos functionality in the current xna version which allows you to draw a border around a sprite? Something like PhotoShop's "stroke" would be fantastic! I specifically need this as I'm looking for ways to make the labels on a button stand out more when the contrast between the background is too little to easily distinguish. It's an issue for me as I have a whole spectrum of colors used for labels, generated randomly, but half of them blend into the background too much.
Coordinator
May 19, 2014 at 1:00 AM
The draw primitives node will allow you to draw line segments. You use the "bounding box" of the node to get the segment bounds, and then send the segments to CCDrawNode, which would be a child of your target node.

The tests have a sample of this - see the Primitives tests.
May 19, 2014 at 9:58 AM
Thanks for pointing me there. I will check it out soon. In the meantime I found an old solution to this written for cocos2d-iphone in objective-c and rewrote it in c# for my project. In case anyone is interested, I have pasted the code below. It makes use of CCRenderTexture, so I'm not sure how it compares to the primitive method you mentioned performance wise. This comes from: http://stackoverflow.com/questions/18493943/how-to-create-a-stroke-on-a-cclabelttf/18548587#18548587 (I removed the CCBlendFunc aspects as it did not render nicely for me in cocos2d-xna).
        private CCRenderTexture CreateStroke(CCLabelTTF label, float size, CCColor3B color)
        {
            var rt = new CCRenderTexture((int)(label.ContentSize.Width + size * 2), (int)(label.ContentSize.Height + size * 2));
    
            var originalPos = label.Position;
            var originalColor = label.Color;
            var originalVisibility = label.Visible;
    
            label.Color = color;
            label.Visible = true;
          
            var bottomLeft = new CCPoint(label.ContentSize.Width * label.AnchorPoint.X + size, label.ContentSize.Height * label.AnchorPoint.Y + size);

            var positionOffset = new CCPoint(-label.ContentSize.Width/2,-label.ContentSize.Height/2);
            var position = originalPos.Sub(ref positionOffset);

            rt.Begin();

            for (var i=0; i<360; i+=60) 
            {
                label.Position = new CCPoint((float)(bottomLeft.X + Math.Sin(CCMacros.CCDegreesToRadians(i)) * size), (float)(bottomLeft.Y + Math.Cos(CCMacros.CCDegreesToRadians(i)) * size));
                label.Visit();
            }
            rt.End();

            rt.Sprite.Texture.IsAntialiased = true;
            label.Position = originalPos;
            label.Color = originalColor;
            label.Visible = originalVisibility;
            rt.Position = position;

            return rt;
        }