@@ -42,6 +42,10 @@ THE SOFTWARE.
42
42
#include " base/CCEventListenerAcceleration.h"
43
43
#include " base/ccUTF8.h"
44
44
45
+ #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
46
+ #include " platform/desktop/CCGLViewImpl-desktop.h"
47
+ #endif
48
+
45
49
NS_CC_BEGIN
46
50
47
51
// Layer
@@ -843,6 +847,245 @@ std::string LayerGradient::getDescription() const
843
847
return StringUtils::format (" <LayerGradient | Tag = %d>" , _tag);
844
848
}
845
849
850
+ /* *
851
+ * LayerRadialGradient
852
+ */
853
+ LayerRadialGradient* LayerRadialGradient::create (const Color4B& startColor, const Color4B& endColor, float radius, const Vec2& center, float expand)
854
+ {
855
+ auto layerGradient = new LayerRadialGradient ();
856
+ if (layerGradient && layerGradient->initWithColor (startColor, endColor, radius, center, expand))
857
+ {
858
+ layerGradient->autorelease ();
859
+ return layerGradient;
860
+ }
861
+
862
+ delete layerGradient;
863
+ return nullptr ;
864
+ }
865
+
866
+ LayerRadialGradient* LayerRadialGradient::create ()
867
+ {
868
+ auto layerGradient = new LayerRadialGradient ();
869
+ if (layerGradient && layerGradient->initWithColor (Color4B::BLACK, Color4B::BLACK, 0 , Vec2 (0 ,0 ), 0 ))
870
+ {
871
+ layerGradient->autorelease ();
872
+ return layerGradient;
873
+ }
874
+
875
+ delete layerGradient;
876
+ return nullptr ;
877
+ }
878
+
879
+ LayerRadialGradient::LayerRadialGradient ()
880
+ : _startColor(Color4B::BLACK)
881
+ , _startColorRend(Color4F::BLACK)
882
+ , _endColor(Color4B::BLACK)
883
+ , _endColorRend(Color4F::BLACK)
884
+ , _radius(0 .f)
885
+ , _expand(0 .f)
886
+ , _center(Vec2(0 ,0 ))
887
+ , _uniformLocationCenter(0 )
888
+ , _uniformLocationRadius(0 )
889
+ , _uniformLocationExpand(0 )
890
+ , _uniformLocationEndColor(0 )
891
+ , _uniformLocationStartColor(0 )
892
+ , _blendFunc(BlendFunc::ALPHA_NON_PREMULTIPLIED)
893
+ { }
894
+
895
+ LayerRadialGradient::~LayerRadialGradient ()
896
+ {}
897
+
898
+ bool LayerRadialGradient::initWithColor (const cocos2d::Color4B &startColor, const cocos2d::Color4B &endColor, float radius, const Vec2& center, float expand)
899
+ {
900
+ // should do it before Layer::init()
901
+ for (int i = 0 ; i < 4 ; ++i)
902
+ _vertices[i] = {0 .0f , 0 .0f };
903
+
904
+ if (Layer::init ())
905
+ {
906
+ convertColor4B24F (_startColorRend, startColor);
907
+ _startColor = startColor;
908
+
909
+ convertColor4B24F (_endColorRend, endColor);
910
+ _endColor = endColor;
911
+
912
+ _expand = expand;
913
+
914
+ setRadius (radius);
915
+ setCenter (center);
916
+
917
+ setGLProgramState (GLProgramState::getOrCreateWithGLProgramName (GLProgram::SHADER_LAYER_RADIAL_GRADIENT));
918
+ auto program = getGLProgram ();
919
+ _uniformLocationStartColor = program->getUniformLocation (" u_startColor" );
920
+ _uniformLocationEndColor = program->getUniformLocation (" u_endColor" );
921
+ _uniformLocationExpand = program->getUniformLocation (" u_expand" );
922
+ _uniformLocationRadius = program->getUniformLocation (" u_radius" );
923
+ _uniformLocationCenter = program->getUniformLocation (" u_center" );
924
+
925
+ return true ;
926
+ }
927
+
928
+ return false ;
929
+ }
930
+
931
+ void LayerRadialGradient::draw (Renderer *renderer, const Mat4 &transform, uint32_t flags)
932
+ {
933
+ _customCommand.init (_globalZOrder, transform, flags);
934
+ _customCommand.func = CC_CALLBACK_0 (LayerRadialGradient::onDraw, this , transform, flags);
935
+ renderer->addCommand (&_customCommand);
936
+ }
937
+
938
+ void LayerRadialGradient::onDraw (const Mat4& transform, uint32_t /* flags*/ )
939
+ {
940
+ auto program = getGLProgram ();
941
+ program->use ();
942
+ program->setUniformsForBuiltins (transform);
943
+ program->setUniformLocationWith4f (_uniformLocationStartColor, _startColorRend.r ,
944
+ _startColorRend.g , _startColorRend.b , _startColorRend.a );
945
+ program->setUniformLocationWith4f (_uniformLocationEndColor, _endColorRend.r ,
946
+ _endColorRend.g , _endColorRend.b , _endColorRend.a );
947
+ program->setUniformLocationWith2f (_uniformLocationCenter, _center.x , _center.y );
948
+ program->setUniformLocationWith1f (_uniformLocationRadius, _radius);
949
+ program->setUniformLocationWith1f (_uniformLocationExpand, _expand);
950
+
951
+
952
+ GL::enableVertexAttribs ( GL::VERTEX_ATTRIB_FLAG_POSITION);
953
+
954
+ //
955
+ // Attributes
956
+ //
957
+ glBindBuffer (GL_ARRAY_BUFFER, 0 );
958
+ glVertexAttribPointer (GLProgram::VERTEX_ATTRIB_POSITION, 2 , GL_FLOAT, GL_FALSE, 0 , _vertices);
959
+
960
+ GL::blendFunc (_blendFunc.src , _blendFunc.dst );
961
+
962
+ glDrawArrays (GL_TRIANGLE_STRIP, 0 , 4 );
963
+
964
+ CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES (1 ,4 );
965
+ }
966
+
967
+ void LayerRadialGradient::setContentSize (const Size& size)
968
+ {
969
+ _vertices[1 ].x = size.width ;
970
+ _vertices[2 ].y = size.height ;
971
+ _vertices[3 ].x = size.width ;
972
+ _vertices[3 ].y = size.height ;
973
+ Layer::setContentSize (size);
974
+ }
975
+
976
+ void LayerRadialGradient::setStartOpacity (GLubyte opacity)
977
+ {
978
+ _startColorRend.a = opacity / 255 .0f ;
979
+ _startColor.a = opacity;
980
+ }
981
+
982
+ GLubyte LayerRadialGradient::getStartOpacity () const
983
+ {
984
+ return _startColor.a ;
985
+ }
986
+
987
+ void LayerRadialGradient::setEndOpacity (GLubyte opacity)
988
+ {
989
+ _endColorRend.a = opacity / 255 .0f ;
990
+ _endColor.a = opacity;
991
+ }
992
+
993
+ GLubyte LayerRadialGradient::getEndOpacity () const
994
+ {
995
+ return _endColor.a ;
996
+ }
997
+
998
+ void LayerRadialGradient::setRadius (float radius)
999
+ {
1000
+ _radius = radius;
1001
+ }
1002
+
1003
+ float LayerRadialGradient::getRadius () const
1004
+ {
1005
+ return _radius;
1006
+ }
1007
+
1008
+ void LayerRadialGradient::setCenter (const Vec2& center)
1009
+ {
1010
+ _center = center;
1011
+ }
1012
+
1013
+ Vec2 LayerRadialGradient::getCenter () const
1014
+ {
1015
+ return _center;
1016
+ }
1017
+
1018
+ void LayerRadialGradient::setExpand (float expand)
1019
+ {
1020
+ _expand = expand;
1021
+ }
1022
+
1023
+ float LayerRadialGradient::getExpand () const
1024
+ {
1025
+ return _expand;
1026
+ }
1027
+
1028
+ void LayerRadialGradient::setStartColor (const Color3B& color)
1029
+ {
1030
+ setStartColor (Color4B (color));
1031
+ }
1032
+
1033
+ void LayerRadialGradient::setStartColor (const cocos2d::Color4B &color)
1034
+ {
1035
+ _startColor = color;
1036
+ convertColor4B24F (_startColorRend, _startColor);
1037
+ }
1038
+
1039
+ Color4B LayerRadialGradient::getStartColor () const
1040
+ {
1041
+ return _startColor;
1042
+ }
1043
+
1044
+ Color3B LayerRadialGradient::getStartColor3B () const
1045
+ {
1046
+ return Color3B (_startColor);
1047
+ }
1048
+
1049
+ void LayerRadialGradient::setEndColor (const Color3B& color)
1050
+ {
1051
+ setEndColor (Color4B (color));
1052
+ }
1053
+
1054
+ void LayerRadialGradient::setEndColor (const cocos2d::Color4B &color)
1055
+ {
1056
+ _endColor = color;
1057
+ convertColor4B24F (_endColorRend, _endColor);
1058
+ }
1059
+
1060
+ Color4B LayerRadialGradient::getEndColor () const
1061
+ {
1062
+ return _endColor;
1063
+ }
1064
+
1065
+ Color3B LayerRadialGradient::getEndColor3B () const
1066
+ {
1067
+ return Color3B (_endColor);
1068
+ }
1069
+
1070
+ void LayerRadialGradient::setBlendFunc (const BlendFunc& blendFunc)
1071
+ {
1072
+ _blendFunc = blendFunc;
1073
+ }
1074
+
1075
+ BlendFunc LayerRadialGradient::getBlendFunc () const
1076
+ {
1077
+ return _blendFunc;
1078
+ }
1079
+
1080
+ void LayerRadialGradient::convertColor4B24F (Color4F& outColor, const Color4B& inColor)
1081
+ {
1082
+ outColor.r = inColor.r / 255 .0f ;
1083
+ outColor.g = inColor.g / 255 .0f ;
1084
+ outColor.b = inColor.b / 255 .0f ;
1085
+ outColor.a = inColor.a / 255 .0f ;
1086
+ }
1087
+
1088
+
846
1089
// / MultiplexLayer
847
1090
848
1091
LayerMultiplex::LayerMultiplex ()
0 commit comments