1
+ /****************************************************************************
2
+ Copyright (c) 2013-2014 Chukong Technologies Inc.
3
+
4
+ http://www.cocos2d-x.org
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
23
+ ****************************************************************************/
24
+
25
+ cc . TMXLayer . CanvasRenderCmd = function ( renderableObject ) {
26
+ cc . Node . CanvasRenderCmd . call ( this , renderableObject ) ;
27
+ this . _childrenRenderCmds = [ ] ;
28
+ this . _needDraw = true ;
29
+ } ;
30
+
31
+ cc . TMXLayer . CanvasRenderCmd . prototype = Object . create ( cc . Node . CanvasRenderCmd . prototype ) ;
32
+ cc . TMXLayer . CanvasRenderCmd . prototype . constructor = cc . TMXLayer . CanvasRenderCmd ;
33
+
34
+ cc . TMXLayer . CanvasRenderCmd . prototype . _copyRendererCmds = function ( rendererCmds ) {
35
+ if ( ! rendererCmds )
36
+ return ;
37
+
38
+ var locCacheCmds = this . _childrenRenderCmds ;
39
+ locCacheCmds . length = 0 ;
40
+ for ( var i = 0 , len = rendererCmds . length ; i < len ; i ++ ) {
41
+ locCacheCmds [ i ] = rendererCmds [ i ] ;
42
+ }
43
+ } ;
44
+
45
+ cc . TMXLayer . CanvasRenderCmd . prototype . _renderingChildToCache = function ( scaleX , scaleY ) {
46
+ var locNode = this . _node ;
47
+ if ( locNode . _cacheDirty ) {
48
+ var locCacheCmds = this . _childrenRenderCmds , locCacheContext = locNode . _cacheContext , locCanvas = locNode . _cacheCanvas ;
49
+
50
+ locCacheContext . save ( ) ;
51
+ locCacheContext . clearRect ( 0 , 0 , locCanvas . width , - locCanvas . height ) ;
52
+ //reset the cache context
53
+ var t = cc . affineTransformInvert ( locNode . _transformWorld ) ;
54
+ locCacheContext . transform ( t . a , t . c , t . b , t . d , t . tx * scaleX , - t . ty * scaleY ) ;
55
+
56
+ for ( var i = 0 , len = locCacheCmds . length ; i < len ; i ++ ) {
57
+ locCacheCmds [ i ] . rendering ( locCacheContext , scaleX , scaleY ) ;
58
+ if ( locCacheCmds [ i ] . _node )
59
+ locCacheCmds [ i ] . _node . _cacheDirty = false ;
60
+ }
61
+ locCacheContext . restore ( ) ;
62
+ locNode . _cacheDirty = false ;
63
+ }
64
+ } ;
65
+
66
+ cc . TMXLayer . CanvasRenderCmd . prototype . rendering = function ( ctx , scaleX , scaleY ) {
67
+ var node = this . _node ;
68
+ var alpha = node . _displayedOpacity / 255 ;
69
+ if ( alpha <= 0 )
70
+ return ;
71
+
72
+ this . _renderingChildToCache ( scaleX , scaleY ) ;
73
+ var context = ctx || cc . _renderContext ;
74
+ context . globalAlpha = alpha ;
75
+ var posX = 0 | ( - node . _anchorPointInPoints . x ) , posY = 0 | ( - node . _anchorPointInPoints . y ) ;
76
+ var locCacheCanvas = node . _cacheCanvas , t = node . _transformWorld ;
77
+ //direct draw image by canvas drawImage
78
+ if ( locCacheCanvas && locCacheCanvas . width !== 0 && locCacheCanvas . height !== 0 ) {
79
+ context . save ( ) ;
80
+ //transform
81
+ context . transform ( t . a , t . c , t . b , t . d , t . tx * scaleX , - t . ty * scaleY ) ;
82
+
83
+ var locCanvasHeight = locCacheCanvas . height * scaleY ;
84
+
85
+ if ( node . layerOrientation === cc . TMX_ORIENTATION_HEX ) {
86
+ var halfTileSize = node . _mapTileSize . height * 0.5 * scaleY ;
87
+ context . drawImage ( locCacheCanvas , 0 , 0 , locCacheCanvas . width , locCacheCanvas . height ,
88
+ posX , - ( posY + locCanvasHeight ) + halfTileSize , locCacheCanvas . width * scaleX , locCanvasHeight ) ;
89
+ } else {
90
+ context . drawImage ( locCacheCanvas , 0 , 0 , locCacheCanvas . width , locCacheCanvas . height ,
91
+ posX , - ( posY + locCanvasHeight ) , locCacheCanvas . width * scaleX , locCanvasHeight ) ;
92
+ }
93
+ context . restore ( ) ;
94
+ }
95
+ cc . g_NumberOfDraws ++ ;
96
+ } ;
97
+
98
+ cc . TMXLayer . WebGLRenderCmd = function ( renderableObject ) {
99
+ cc . Node . WebGLRenderCmd . call ( this , renderableObject ) ;
100
+ this . _needDraw = true ;
101
+ } ;
102
+
103
+ cc . TMXLayer . WebGLRenderCmd . prototype . rendering = cc . SpriteBatchNodeRenderCmdWebGL . prototype . rendering ;
0 commit comments