package org.cocos2d.nodes;

import org.cocos2d.opengl.CCTexture2D;
import org.cocos2d.protocols.CCLabelProtocol;
import org.cocos2d.types.CGRect;
import org.cocos2d.types.CGSize;

/** CCLabel is a subclass of CCTextureNode that knows how to render text labels
 *
 * All features from CCTextureNode are valid in CCLabel
 *
 * CCLabel objects are slow. Consider using CCLabelAtlas or CCBitmapFontAtlas instead.
 */
public class CCLabel extends CCSprite implements CCLabelProtocol {

    public enum TextAlignment {
        LEFT,
        CENTER,
        RIGHT
    }

    private CGSize _dimensions;
    private TextAlignment _alignment;
    private String _fontName;
    private float _fontSize;

    /** creates a CCLabel from a fontname, alignment, dimension and font size */
    public static CCLabel makeLabel(String string, final CGSize dimensions, TextAlignment alignment, 
                                    String fontname, float fontsize) {
        return new CCLabel(string, dimensions, alignment, fontname, fontsize);
    }

    /** creates a CCLabel from a fontname and font size */
    public static CCLabel makeLabel(String string, String fontname, float fontsize) {
        return new CCLabel(string, CGSize.make(0, 0), TextAlignment.CENTER, fontname, fontsize);
    }

    /** initializes the CCLabel with a font name and font size */
    protected CCLabel(String string, String fontname, float fontsize) {
        this(string, CGSize.make(0,0), TextAlignment.CENTER, fontname, fontsize);
    }

    /** initializes the CCLabel with a font name, alignment, dimension and font size */
    protected CCLabel(String string, final CGSize dimensions, TextAlignment alignment,
                        String name, float size) {
    	super();
        _dimensions = dimensions;
        _alignment = alignment;
        _fontName = name;
        _fontSize = size;

        setString(string);
    }

    /** changes the string to render
     * @warning Changing the string is as expensive as creating a new CCLabel.
        To obtain better performance use CCLabelAtlas
     */
    public void setString(String string) {
        CCTexture2D tex = null;
        if (CGSize.equalToSize(_dimensions, CGSize.getZero())) {
            tex = new CCTexture2D(string, _fontName, _fontSize);
        } else {
            tex = new CCTexture2D(string, _dimensions, _alignment, _fontName, _fontSize);
        }
//        CCTextureCache.sharedTextureCache().addTexture(tex);
        setTexture(tex);

	    CGSize size = texture_.getContentSize();
	    setTextureRect(0, 0, size.width, size.height);
    }


    public String toString() {
        return "CCLabel <" + CCLabel.class.getSimpleName() + " = " + this.hashCode()
                + " | FontName = " + _fontName + ", FontSize = " + _fontSize + ">";
    }

}


@@ -21,16 +21,43 @@
 nodes;%0A%0A
+import java.util.HashSet;%0A%0A
 import o
@@ -137,41 +137,8 @@
 ol;%0A
-import org.cocos2d.types.CGRect;%0A
 impo
@@ -675,24 +675,240 @@
 t _fontSize;
+%0A    private String _string;%0A    private boolean _active = false;%0A    private int _dirtyId = -1;%0A%0A    private static int _globalDirtyId = 0;%0A    private static HashSet%3CCCLabel%3E _activeLabels = new HashSet%3CCCLabel%3E();
 %0A%0A    /** cr
@@ -2327,24 +2327,218 @@
 ring) %7B%0A
-        
+%09if (_string == null %7C%7C !_string.equals(string))%0A%09%7B%0A%09%09_string = string;%0A%09%09_dirtyId = -1;%0A%09%09updateTexture();%0A%09%7D%0A    %7D%0A%0A    private void updateTexture()%0A    %7B%0A%09if (_dirtyId == _globalDirtyId)%0A%09%09return;%0A%0A%09
 CCTextur
@@ -2644,32 +2644,33 @@
 new CCTexture2D(
+_
 string, _fontNam
@@ -2735,16 +2735,17 @@
 xture2D(
+_
 string, 
@@ -2989,22 +2989,508 @@
 eight);%0A
+%0A%09    _dirtyId = _globalDirtyId;%0A    %7D%0A%0A    @Override%0A    public void onEnter()%0A    %7B%0A%09super.onEnter();%0A%09_active = true;%0A%09_activeLabels.add(this);%0A%09updateTexture();%0A    %7D%0A%0A    @Override%0A    public void onExit()%0A    %7B%0A%09super.onExit();%0A%09_activeLabels.remove(this);%0A%09_active = false;%0A    %7D%0A%0A    // this is a temporary solution for white texture problem,%0A    public static void reloadTextures()%0A    %7B%0A%09_globalDirtyId += 1;%0A%09for (CCLabel label : _activeLabels)%0A%09%7B%0A%09%09label.updateTexture();%0A%09%7D%0A
     %7D
-%0A
 %0A%0A    pu

apply Urs's patch on CCLabel white screen problem