Skip to content

Commit 7931299

Browse files
committed
Issue cocos2d#1267: implemented ccui.Scale9Sprite's setState
1 parent 71efef0 commit 7931299

File tree

4 files changed

+97
-10
lines changed

4 files changed

+97
-10
lines changed

cocos2d/core/textures/CCTexture2D.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,47 @@ if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
399399
*/
400400
removeLoadedEventListener: function (target) {
401401
this.removeEventListener("load", target);
402+
},
403+
404+
//hack for gray effect
405+
_grayElementObj: null,
406+
_backupElement: null,
407+
_isGray: false,
408+
_switchToGray: function(toGray){
409+
if(!this._textureLoaded || this._isGray == toGray)
410+
return;
411+
this._isGray = toGray;
412+
if(this._isGray){
413+
this._backupElement = this._htmlElementObj;
414+
if(!this._grayElementObj)
415+
this._grayElementObj = cc.Texture2D._generateGrayTexture(this._htmlElementObj);
416+
this._htmlElementObj = this._grayElementObj;
417+
} else {
418+
if(this._backupElement != null)
419+
this._htmlElementObj = this._backupElement;
420+
}
402421
}
403422
});
404423

424+
cc.Texture2D._generateGrayTexture = function(texture, rect, renderCanvas){
425+
if (texture === null)
426+
return null;
427+
renderCanvas = renderCanvas || cc.newElement("canvas");
428+
rect = rect || cc.rect(0, 0, texture.width, texture.height);
429+
renderCanvas.width = rect.width;
430+
renderCanvas.height = rect.height;
431+
432+
var context = renderCanvas.getContext("2d");
433+
context.drawImage(texture, rect.x, rect.y, rect.width, rect.height, 0, 0, rect.width, rect.height);
434+
var imgData = context.getImageData(0, 0, rect.width, rect.height);
435+
var data = imgData.data;
436+
for (var i = 0, len = data.length; i < len; i += 4) {
437+
data[i] = data[i + 1] = data[i + 2] = 0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2];
438+
}
439+
context.putImageData(imgData, 0, 0);
440+
return renderCanvas;
441+
};
442+
405443
} else {
406444
cc.assert(cc.isFunction(cc._tmp.WebGLTexture2D), cc._LogInfos.MissingFile, "TexturesWebGL.js");
407445
cc._tmp.WebGLTexture2D();

extensions/ccui/base-classes/UIScale9Sprite.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,6 @@ ccui.Scale9Sprite = cc.Node.extend(/** @lends ccui.Scale9Sprite# */{
6767
_bottom: null,
6868
_bottomRight: null,
6969

70-
//cache in canvas on Canvas mode
71-
_cacheSprite: null,
72-
_cacheCanvas: null,
73-
_cacheContext: null,
74-
_cacheTexture: null,
7570
_scale9Dirty: true,
7671

7772
_opacityModifyRGB: false,
@@ -923,8 +918,13 @@ ccui.Scale9Sprite = cc.Node.extend(/** @lends ccui.Scale9Sprite# */{
923918
},
924919

925920
//v3.3
921+
/**
922+
* Sets ccui.Scale9Sprite's state
923+
* @since v3.3
924+
* @param {Number} state
925+
*/
926926
setState: function(state){
927-
//
927+
this._renderCmd.setState(state);
928928
},
929929

930930
//setScale9Enabled implement late
@@ -1099,4 +1099,6 @@ ccui.Scale9Sprite.POSITIONS_RIGHT = 3;
10991099
ccui.Scale9Sprite.POSITIONS_BOTTOM = 4;
11001100
ccui.Scale9Sprite.POSITIONS_TOPRIGHT = 5;
11011101
ccui.Scale9Sprite.POSITIONS_TOPLEFT = 6;
1102-
ccui.Scale9Sprite.POSITIONS_BOTTOMRIGHT = 7;
1102+
ccui.Scale9Sprite.POSITIONS_BOTTOMRIGHT = 7;
1103+
1104+
ccui.Scale9Sprite.state = {NORMAL: 0, GRAY: 1};

extensions/ccui/base-classes/UIScale9SpriteCanvasRenderCmd.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,17 @@
120120
if(!this._cacheSprite.getParent())
121121
node.addChild(this._cacheSprite, -1);
122122
};
123+
124+
proto.setState = function(state){
125+
var locScale9Image = this._node._scale9Image;
126+
if(!locScale9Image)
127+
return;
128+
var selTexture = locScale9Image.getTexture();
129+
if(state === ccui.Scale9Sprite.state.NORMAL){
130+
selTexture._switchToGray(false);
131+
} else if( state === ccui.Scale9Sprite.state.GRAY){
132+
selTexture._switchToGray(true);
133+
}
134+
this._cacheScale9Sprite();
135+
};
123136
})();

extensions/ccui/base-classes/UIScale9SpriteWebGLRenderCmd.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@
3434

3535
proto.visit = function(parentCmd){
3636
var node = this._node;
37-
if(!node._visible){
37+
if(!node._visible)
3838
return;
39-
}
4039

4140
if (node._positionsAreDirty) {
4241
node._updatePositions();
@@ -60,7 +59,6 @@
6059
}
6160
}
6261
}
63-
6462
};
6563

6664
proto._updateDisplayOpacity = function(parentColor){
@@ -77,7 +75,43 @@
7775
}
7876
}
7977
}
78+
};
79+
80+
proto.setState = function (state) {
81+
var scale9Image = this._node._scale9Image;
82+
if (state === ccui.Scale9Sprite.state.NORMAL) {
83+
scale9Image.setShaderProgram(cc.shaderCache.programForKey(cc.SHADER_POSITION_TEXTURECOLOR));
84+
} else if (state === ccui.Scale9Sprite.state.GRAY) {
85+
scale9Image.setShaderProgram(ccui.Scale9Sprite.WebGLRenderCmd._getGrayShaderProgram());
86+
}
87+
};
88+
89+
ccui.Scale9Sprite.WebGLRenderCmd._grayShaderProgram = null;
90+
ccui.Scale9Sprite.WebGLRenderCmd._getGrayShaderProgram = function(){
91+
var grayShader = ccui.Scale9Sprite.WebGLRenderCmd._grayShaderProgram;
92+
if(grayShader)
93+
return grayShader;
94+
95+
grayShader = new cc.GLProgram();
96+
grayShader.initWithVertexShaderByteArray(cc.SHADER_POSITION_TEXTURE_COLOR_VERT, ccui.Scale9Sprite.WebGLRenderCmd._grayShaderFragment);
97+
grayShader.addAttribute(cc.ATTRIBUTE_NAME_POSITION, cc.VERTEX_ATTRIB_POSITION);
98+
grayShader.addAttribute(cc.ATTRIBUTE_NAME_COLOR, cc.VERTEX_ATTRIB_COLOR);
99+
grayShader.addAttribute(cc.ATTRIBUTE_NAME_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORDS);
100+
grayShader.link();
101+
grayShader.updateUniforms();
80102

103+
ccui.Scale9Sprite.WebGLRenderCmd._grayShaderProgram = grayShader;
104+
return grayShader;
81105
};
82106

107+
ccui.Scale9Sprite.WebGLRenderCmd._grayShaderFragment =
108+
"precision lowp float;\n"
109+
+ "varying vec4 v_fragmentColor; \n"
110+
+ "varying vec2 v_texCoord; \n"
111+
+ "void main() \n"
112+
+ "{ \n"
113+
+ " vec4 c = texture2D(CC_Texture0, v_texCoord); \n"
114+
+ " gl_FragColor.xyz = vec3(0.2126*c.r + 0.7152*c.g + 0.0722*c.b); \n"
115+
+" gl_FragColor.w = c.w ; \n"
116+
+ "}";
83117
})();

0 commit comments

Comments
 (0)