From ce4f78f31277d400a0ead3992ab05b3eefc779b4 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Wed, 13 Apr 2016 15:35:27 +0800 Subject: [PATCH] add creator files --- .../cocos2d_libs.xcodeproj/project.pbxproj | 24 +- native/cocos/Android.mk | 2 + .../cocos/editor-support/creator/Android.mk | 16 + .../editor-support/creator/CCScale9Sprite.cpp | 1093 +++++++++++++++++ .../editor-support/creator/CCScale9Sprite.h | 168 +++ native/templates/cocos2dx_files.json | 5 + native/tools/tojs/creator.ini | 64 + native/tools/tojs/genbindings.py | 5 +- 8 files changed, 1374 insertions(+), 3 deletions(-) create mode 100644 native/cocos/editor-support/creator/Android.mk create mode 100644 native/cocos/editor-support/creator/CCScale9Sprite.cpp create mode 100755 native/cocos/editor-support/creator/CCScale9Sprite.h create mode 100755 native/tools/tojs/creator.ini diff --git a/native/build/cocos2d_libs.xcodeproj/project.pbxproj b/native/build/cocos2d_libs.xcodeproj/project.pbxproj index c3330511b2..ad5694a606 100644 --- a/native/build/cocos2d_libs.xcodeproj/project.pbxproj +++ b/native/build/cocos2d_libs.xcodeproj/project.pbxproj @@ -686,6 +686,10 @@ 4D76BE3B1A4AAF0A00102962 /* CCActionTimelineNode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4D76BE381A4AAF0A00102962 /* CCActionTimelineNode.cpp */; }; 4D76BE3C1A4AAF0A00102962 /* CCActionTimelineNode.h in Headers */ = {isa = PBXBuildFile; fileRef = 4D76BE391A4AAF0A00102962 /* CCActionTimelineNode.h */; }; 4D76BE3D1A4AAF0A00102962 /* CCActionTimelineNode.h in Headers */ = {isa = PBXBuildFile; fileRef = 4D76BE391A4AAF0A00102962 /* CCActionTimelineNode.h */; }; + 4DFA4C171CBCE34100E3B736 /* CCScale9Sprite.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4DFA4C151CBCE34100E3B736 /* CCScale9Sprite.cpp */; }; + 4DFA4C181CBCE34100E3B736 /* CCScale9Sprite.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4DFA4C151CBCE34100E3B736 /* CCScale9Sprite.cpp */; }; + 4DFA4C191CBCE34100E3B736 /* CCScale9Sprite.h in Headers */ = {isa = PBXBuildFile; fileRef = 4DFA4C161CBCE34100E3B736 /* CCScale9Sprite.h */; }; + 4DFA4C1A1CBCE34100E3B736 /* CCScale9Sprite.h in Headers */ = {isa = PBXBuildFile; fileRef = 4DFA4C161CBCE34100E3B736 /* CCScale9Sprite.h */; }; 5012168E1AC47380009A4BEA /* CCRenderState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5012168C1AC47380009A4BEA /* CCRenderState.cpp */; }; 5012168F1AC47380009A4BEA /* CCRenderState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5012168C1AC47380009A4BEA /* CCRenderState.cpp */; }; 501216901AC47380009A4BEA /* CCRenderState.h in Headers */ = {isa = PBXBuildFile; fileRef = 5012168D1AC47380009A4BEA /* CCRenderState.h */; }; @@ -1807,6 +1811,8 @@ 46A168651807AF4E005B8026 /* CCTableViewCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CCTableViewCell.h; sourceTree = ""; }; 4D76BE381A4AAF0A00102962 /* CCActionTimelineNode.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCActionTimelineNode.cpp; sourceTree = ""; }; 4D76BE391A4AAF0A00102962 /* CCActionTimelineNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCActionTimelineNode.h; sourceTree = ""; }; + 4DFA4C151CBCE34100E3B736 /* CCScale9Sprite.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CCScale9Sprite.cpp; path = creator/CCScale9Sprite.cpp; sourceTree = ""; }; + 4DFA4C161CBCE34100E3B736 /* CCScale9Sprite.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCScale9Sprite.h; path = creator/CCScale9Sprite.h; sourceTree = ""; }; 5012168C1AC47380009A4BEA /* CCRenderState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCRenderState.cpp; sourceTree = ""; }; 5012168D1AC47380009A4BEA /* CCRenderState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCRenderState.h; sourceTree = ""; }; 50272538190BF1B900AAF4ED /* cocos2d.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = cocos2d.h; path = ../cocos/cocos2d.h; sourceTree = ""; }; @@ -2773,6 +2779,7 @@ 1AD71CF7180E26E600808F54 /* editor-support */ = { isa = PBXGroup; children = ( + 4DFA4C141CBCE32800E3B736 /* creator */, 1AD71CF8180E26E600808F54 /* cocosbuilder */, 1A8C5946180E930E00EF57C3 /* cocostudio */, 1AD71D7C180E26E600808F54 /* spine */, @@ -3233,6 +3240,15 @@ path = ../cocos/math; sourceTree = ""; }; + 4DFA4C141CBCE32800E3B736 /* creator */ = { + isa = PBXGroup; + children = ( + 4DFA4C151CBCE34100E3B736 /* CCScale9Sprite.cpp */, + 4DFA4C161CBCE34100E3B736 /* CCScale9Sprite.h */, + ); + name = creator; + sourceTree = ""; + }; 500DC89819105D41007B91BF /* renderer */ = { isa = PBXGroup; children = ( @@ -3671,6 +3687,7 @@ 1A57007F180BC5A10088DEC7 /* CCActionInterval.h in Headers */, 15AE188719AAD33D00C27E9E /* CCBSequenceProperty.h in Headers */, 1A570083180BC5A10088DEC7 /* CCActionManager.h in Headers */, + 4DFA4C191CBCE34100E3B736 /* CCScale9Sprite.h in Headers */, 1A570087180BC5A10088DEC7 /* CCActionPageTurn3D.h in Headers */, 50ABBD911925AB4100A911A9 /* CCGLProgramCache.h in Headers */, 50ED2BDA19BE76D300A0AB90 /* UIVideoPlayer.h in Headers */, @@ -4064,6 +4081,7 @@ B29A7E2419EE1B7700872B35 /* IkConstraint.h in Headers */, 50CB247E19D9C5A100687767 /* AudioPlayer.h in Headers */, 1A57007C180BC5A10088DEC7 /* CCActionInstant.h in Headers */, + 4DFA4C1A1CBCE34100E3B736 /* CCScale9Sprite.h in Headers */, 15AE18CD19AAD33D00C27E9E /* CCNode+CCBRelativePositioning.h in Headers */, 50ABBE781925AB6F00A911A9 /* CCEventListenerTouch.h in Headers */, 15AE1BC019AADFF000C27E9E /* WebSocket.h in Headers */, @@ -4472,6 +4490,7 @@ 50ABBD501925AB0000A911A9 /* Quaternion.cpp in Sources */, 15AE190119AAD35000C27E9E /* CCComController.cpp in Sources */, 15AE1BE619AAE01E00C27E9E /* CCTableViewCell.cpp in Sources */, + 4DFA4C171CBCE34100E3B736 /* CCScale9Sprite.cpp in Sources */, 50ABBEBB1925AB6F00A911A9 /* ccUtils.cpp in Sources */, A679D0F71C74BA3600620A6C /* CCArmatureAnimation.cpp in Sources */, 15AE186A19AAD31D00C27E9E /* CocosDenshion.m in Sources */, @@ -4806,6 +4825,7 @@ 50ABBD941925AB4100A911A9 /* CCGLProgramState.cpp in Sources */, ED30578B1BEC774D0083C3ED /* ioapi_mem.cpp in Sources */, B257B44F1989D5E800D9A687 /* CCPrimitive.cpp in Sources */, + 4DFA4C181CBCE34100E3B736 /* CCScale9Sprite.cpp in Sources */, 50ABBE281925AB6F00A911A9 /* CCAutoreleasePool.cpp in Sources */, A60A9D641C200DE70070800A /* CCPhysicsSprite.cpp in Sources */, 15AE18D519AAD33D00C27E9E /* CCScale9SpriteLoader.cpp in Sources */, @@ -5255,7 +5275,7 @@ GCC_PREPROCESSOR_DEFINITIONS = "$(inherited)"; GCC_WARN_ABOUT_DEPRECATED_FUNCTIONS = YES; HEADER_SEARCH_PATHS = ""; - IPHONEOS_DEPLOYMENT_TARGET = 7.0; + IPHONEOS_DEPLOYMENT_TARGET = 6.0; LIBRARY_SEARCH_PATHS = "\"$(SRCROOT)/../external/ios/libs\""; ONLY_ACTIVE_ARCH = YES; OTHER_LDFLAGS = "-llibsql3"; @@ -5281,7 +5301,7 @@ GCC_PREPROCESSOR_DEFINITIONS = "$(inherited)"; GCC_WARN_ABOUT_DEPRECATED_FUNCTIONS = YES; HEADER_SEARCH_PATHS = ""; - IPHONEOS_DEPLOYMENT_TARGET = 7.0; + IPHONEOS_DEPLOYMENT_TARGET = 6.0; LIBRARY_SEARCH_PATHS = "\"$(SRCROOT)/../external/ios/libs\""; ONLY_ACTIVE_ARCH = NO; OTHER_CFLAGS = ""; diff --git a/native/cocos/Android.mk b/native/cocos/Android.mk index 7b7417b174..6cda88269b 100644 --- a/native/cocos/Android.mk +++ b/native/cocos/Android.mk @@ -218,6 +218,7 @@ LOCAL_MODULE_FILENAME := libcocos2d LOCAL_STATIC_LIBRARIES := cocostudio_static LOCAL_STATIC_LIBRARIES += cocosbuilder_static LOCAL_STATIC_LIBRARIES += spine_static +LOCAL_STATIC_LIBRARIES += creator_static LOCAL_STATIC_LIBRARIES += cocos_network_static LOCAL_STATIC_LIBRARIES += audioengine_static @@ -229,6 +230,7 @@ $(call import-module,android) $(call import-module,editor-support/cocostudio) $(call import-module,editor-support/cocosbuilder) $(call import-module,editor-support/spine) +$(call import-module,editor-support/creator) $(call import-module,platform/android) $(call import-module,audio/android) $(call import-module,network) diff --git a/native/cocos/editor-support/creator/Android.mk b/native/cocos/editor-support/creator/Android.mk new file mode 100644 index 0000000000..3bb903ab99 --- /dev/null +++ b/native/cocos/editor-support/creator/Android.mk @@ -0,0 +1,16 @@ +LOCAL_PATH := $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE := creator_static + +LOCAL_MODULE_FILENAME := libcreator + +LOCAL_SRC_FILES := CCScale9Sprite.cpp + +LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/.. + +LOCAL_C_INCLUDES := $(LOCAL_PATH)/.. + +LOCAL_STATIC_LIBRARIES := cocos2dx_internal_static + +include $(BUILD_STATIC_LIBRARY) diff --git a/native/cocos/editor-support/creator/CCScale9Sprite.cpp b/native/cocos/editor-support/creator/CCScale9Sprite.cpp new file mode 100644 index 0000000000..0e42b301f3 --- /dev/null +++ b/native/cocos/editor-support/creator/CCScale9Sprite.cpp @@ -0,0 +1,1093 @@ +/**************************************************************************** + Copyright (c) 2016 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + + ****************************************************************************/ + +#include "CCScale9Sprite.h" +#include "renderer/CCTextureCache.h" +#include "base/CCDirector.h" +#include "2d/CCSpriteFrameCache.h" +#include "renderer/CCRenderer.h" + +namespace creator { + +class simpleQuadGenerator +{ +public: + std::vector _rebuildQuads_base(cocos2d::SpriteFrame* spriteFrame, const cocos2d::Size& contentSize, const cocos2d::Color4B& colorOpacity, bool isTrimmedContentSize) { + std::vector quads; + //build vertices + auto vertices = this->_calculateVertices(spriteFrame, contentSize, isTrimmedContentSize); + + //build uvs + auto uvs = this->_calculateUVs(spriteFrame); + + //build quads + quads.resize(1); + auto& quad = quads[0]; + + quad.bl.colors = colorOpacity; + quad.br.colors = colorOpacity; + quad.tl.colors = colorOpacity; + quad.tr.colors = colorOpacity; + + quad.bl.vertices = cocos2d::Vec3(vertices[0].x, vertices[0].y, 0); + quad.br.vertices = cocos2d::Vec3(vertices[1].x, vertices[0].y, 0); + quad.tl.vertices = cocos2d::Vec3(vertices[0].x, vertices[1].y, 0); + quad.tr.vertices = cocos2d::Vec3(vertices[1].x, vertices[1].y, 0); + + if (!spriteFrame->isRotated()) { + quad.bl.texCoords = cocos2d::Tex2F(uvs[0].x, uvs[0].y); + quad.br.texCoords = cocos2d::Tex2F(uvs[1].x, uvs[0].y); + quad.tl.texCoords = cocos2d::Tex2F(uvs[0].x, uvs[1].y); + quad.tr.texCoords = cocos2d::Tex2F(uvs[1].x, uvs[1].y); + } else { + quad.bl.texCoords = cocos2d::Tex2F(uvs[0].x, uvs[1].y); + quad.br.texCoords = cocos2d::Tex2F(uvs[0].x, uvs[0].y); + quad.tl.texCoords = cocos2d::Tex2F(uvs[1].x, uvs[1].y); + quad.tr.texCoords = cocos2d::Tex2F(uvs[1].x, uvs[0].y); + } + + return quads; + } + + std::vector _calculateVertices(cocos2d::SpriteFrame* spriteFrame, const cocos2d::Size& contentSize, bool isTrimmedContentSize) { + float x0, x3; + float y0, y3; + if (isTrimmedContentSize) { + x0 = 0; + x3 = contentSize.width; + + y0 = 0; + y3 = contentSize.height; + } else { + auto originalSize = spriteFrame->getOriginalSize(); + auto rect = spriteFrame->getRect(); + auto offset = spriteFrame->getOffset(); + auto scaleX = contentSize.width / originalSize.width; + auto scaleY = contentSize.height / originalSize.height; + auto trimmLeft = offset.x + (originalSize.width - rect.size.width) / 2; + auto trimmRight = offset.x - (originalSize.width - rect.size.width) / 2; + auto trimmedBottom = offset.y + (originalSize.height - rect.size.height) / 2; + auto trimmedTop = offset.y - (originalSize.height - rect.size.height) / 2; + + x0 = trimmLeft * scaleX; + x3 = contentSize.width + trimmRight * scaleX; + y0 = trimmedBottom * scaleY; + y3 = contentSize.height + trimmedTop * scaleY; + } + + std::vector vertices(2); + vertices[0] = cocos2d::Vec2(x0,y0); + vertices[1] = cocos2d::Vec2(x3,y3); + + return vertices; + }; + + std::vector _calculateUVs(cocos2d::SpriteFrame* spriteFrame) { + auto atlasWidth = spriteFrame->getTexture()->getPixelsWide(); + auto atlasHeight = spriteFrame->getTexture()->getPixelsHigh(); + + auto textureRect = spriteFrame->getRectInPixels(); + + //uv computation should take spritesheet into account. + float u0, u3; + float v0, v3; + + if (spriteFrame->isRotated()) { + u0 = textureRect.origin.x / atlasWidth; + u3 = (textureRect.origin.x + textureRect.size.height) / atlasWidth; + + v0 = textureRect.origin.y / atlasHeight; + v3 = (textureRect.origin.y + textureRect.size.width) / atlasHeight; + } + else { + u0 = textureRect.origin.x / atlasWidth; + u3 = (textureRect.origin.x + textureRect.size.width) / atlasWidth; + + v0 = textureRect.origin.y / atlasHeight; + v3 = (textureRect.origin.y + textureRect.size.height) / atlasHeight; + } + + std::vector uvCoordinates(2); + uvCoordinates[0] = cocos2d::Vec2(u0, v3); + uvCoordinates[1] = cocos2d::Vec2(u3, v0); + + return uvCoordinates; + } +}; + +class scale9QuadGenerator +{ +public: + std::vector _rebuildQuads_base(cocos2d::SpriteFrame* spriteFrame, const cocos2d::Size& contentSize, const cocos2d::Color4B& colorOpacity, float insetLeft, float insetRight, float insetTop, float insetBottom) { + std::vector quads(9); + //build vertices + auto vertices = this->_calculateVertices(spriteFrame, contentSize, insetLeft, insetRight, insetTop, insetBottom); + + //build uvs + auto uvs = this->_calculateUVs(spriteFrame, insetLeft, insetRight, insetTop, insetBottom); + + //build quads + int quadIndex(0); + for (int i = 0; i < 3; ++i) { + for (int j = 0; j < 3; ++j) { + auto& quad = quads[quadIndex++]; + quad.bl.colors = colorOpacity; + quad.br.colors = colorOpacity; + quad.tl.colors = colorOpacity; + quad.tr.colors = colorOpacity; + + quad.bl.vertices = cocos2d::Vec3(vertices[i].x, vertices[j].y, 0); + quad.br.vertices = cocos2d::Vec3(vertices[i + 1].x, vertices[j].y, 0); + quad.tl.vertices = cocos2d::Vec3(vertices[i].x, vertices[j + 1].y, 0); + quad.tr.vertices = cocos2d::Vec3(vertices[i + 1].x, vertices[j + 1].y, 0); + + if (!spriteFrame->isRotated()) { + quad.bl.texCoords = cocos2d::Tex2F(uvs[i].x, uvs[j].y); + quad.br.texCoords = cocos2d::Tex2F(uvs[i + 1].x, uvs[j].y); + quad.tl.texCoords = cocos2d::Tex2F(uvs[i].x, uvs[j + 1].y); + quad.tr.texCoords = cocos2d::Tex2F(uvs[i + 1].x, uvs[j + 1].y); + } else { + quad.bl.texCoords = cocos2d::Tex2F(uvs[j].x, uvs[3 - i].y); + quad.br.texCoords = cocos2d::Tex2F(uvs[j].x, uvs[3 - (i + 1)].y); + quad.tl.texCoords = cocos2d::Tex2F(uvs[j + 1].x, uvs[3 - i].y); + quad.tr.texCoords = cocos2d::Tex2F(uvs[j + 1].x, uvs[3 - (i + 1)].y); + } + } + } + + return quads; + } + + std::vector _calculateVertices(cocos2d::SpriteFrame* spriteFrame, const cocos2d::Size& contentSize, float insetLeft, float insetRight, float insetTop, float insetBottom) { + float leftWidth, centerWidth, rightWidth; + float topHeight, centerHeight, bottomHeight; + + auto rect = spriteFrame->getRect(); + leftWidth = insetLeft; + rightWidth = insetRight; + centerWidth = rect.size.width - leftWidth - rightWidth; + + topHeight = insetTop; + bottomHeight = insetBottom; + centerHeight = rect.size.height - topHeight - bottomHeight; + + auto preferSize = contentSize; + auto sizableWidth = preferSize.width - leftWidth - rightWidth; + auto sizableHeight = preferSize.height - topHeight - bottomHeight; + auto xScale = preferSize.width / (leftWidth + rightWidth); + auto yScale = preferSize.height / (topHeight + bottomHeight); + xScale = xScale > 1 ? 1 : xScale; + yScale = yScale > 1 ? 1 : yScale; + sizableWidth = sizableWidth < 0 ? 0 : sizableWidth; + sizableHeight = sizableHeight < 0 ? 0 : sizableHeight; + float x0, x1, x2, x3; + float y0, y1, y2, y3; + x0 = 0; + x1 = leftWidth * xScale; + x2 = x1 + sizableWidth; + x3 = preferSize.width; + + y0 = 0; + y1 = bottomHeight * yScale; + y2 = y1 + sizableHeight; + y3 = preferSize.height; + + std::vector vertices(4); + vertices[0] = cocos2d::Vec2(x0,y0); + vertices[1] = cocos2d::Vec2(x1,y1); + vertices[2] = cocos2d::Vec2(x2,y2); + vertices[3] = cocos2d::Vec2(x3,y3); + + return vertices; + } + + std::vector _calculateUVs(cocos2d::SpriteFrame* spriteFrame, float insetLeft, float insetRight, float insetTop, float insetBottom) { + auto rect = spriteFrame->getRect(); + auto atlasWidth = spriteFrame->getTexture()->getPixelsWide(); + auto atlasHeight = spriteFrame->getTexture()->getPixelsHigh(); + + //caculate texture coordinate + float leftWidth, centerWidth, rightWidth; + float topHeight, centerHeight, bottomHeight; + leftWidth = insetLeft; + rightWidth = insetRight; + centerWidth = rect.size.width - leftWidth - rightWidth; + + topHeight = insetTop; + bottomHeight = insetBottom; + centerHeight = rect.size.height - topHeight - bottomHeight; + + auto textureRect = spriteFrame->getRectInPixels(); + + //uv computation should take spritesheet into account. + float u0, u1, u2, u3; + float v0, v1, v2, v3; + + if (spriteFrame->isRotated()) { + u0 = textureRect.origin.x / atlasWidth; + u1 = (bottomHeight + textureRect.origin.x) / atlasWidth; + u2 = (bottomHeight + centerHeight + textureRect.origin.x) / atlasWidth; + u3 = (textureRect.origin.x + textureRect.size.height) / atlasWidth; + + v0 = textureRect.origin.y / atlasHeight; + v1 = (leftWidth + textureRect.origin.y) / atlasHeight; + v2 = (leftWidth + centerWidth + textureRect.origin.y) / atlasHeight; + v3 = (textureRect.origin.y + textureRect.size.width) / atlasHeight; + } + else { + u0 = textureRect.origin.x / atlasWidth; + u1 = (leftWidth + textureRect.origin.x) / atlasWidth; + u2 = (leftWidth + centerWidth + textureRect.origin.x) / atlasWidth; + u3 = (textureRect.origin.x + textureRect.size.width) / atlasWidth; + + v0 = textureRect.origin.y / atlasHeight; + v1 = (topHeight + textureRect.origin.y) / atlasHeight; + v2 = (topHeight + centerHeight + textureRect.origin.y) / atlasHeight; + v3 = (textureRect.origin.y + textureRect.size.height) / atlasHeight; + } + + std::vector uvCoordinates(4); + uvCoordinates[0] = cocos2d::Vec2(u0, v3); + uvCoordinates[1] = cocos2d::Vec2(u1, v2); + uvCoordinates[2] = cocos2d::Vec2(u2, v1); + uvCoordinates[3] = cocos2d::Vec2(u3, v0); + + return uvCoordinates; + } +}; + +class tiledQuadGenerator +{ +public: + std::vector _rebuildQuads_base(cocos2d::SpriteFrame* spriteFrame, const cocos2d::Size& contentSize, const cocos2d::Color4B& colorOpacity) { + std::vector quads; + + //build uvs + auto uvs = this->_calculateUVs(spriteFrame); + auto u0 = uvs[0].x; + auto v0 = uvs[0].y; + auto u1 = uvs[1].x; + auto v1 = uvs[1].y; + auto rectWidth = spriteFrame->getRect().size.width; + auto rectHeight = spriteFrame->getRect().size.height; + //build quads + auto hRepeat = contentSize.width / rectWidth; + auto vRepeat = contentSize.height / rectHeight; + + quads.resize(ceilf(hRepeat) * ceilf(vRepeat)); + int quadIndex = 0; + for (int hindex = 0; hindex < ceilf(hRepeat); ++hindex) { + for (int vindex = 0; vindex < ceilf(vRepeat); ++vindex) { + auto& quad = quads[quadIndex++]; + + quad.bl.colors = colorOpacity; + quad.br.colors = colorOpacity; + quad.tl.colors = colorOpacity; + quad.tr.colors = colorOpacity; + + quad.bl.vertices = cocos2d::Vec3(rectWidth * hindex, rectHeight * vindex, 0); + quad.br.vertices = cocos2d::Vec3(rectWidth * fminf(hindex + 1, hRepeat), rectHeight * vindex, 0); + quad.tl.vertices = cocos2d::Vec3(rectWidth * hindex, rectHeight * fminf(vindex + 1, vRepeat), 0); + quad.tr.vertices = cocos2d::Vec3(rectWidth * fminf(hindex + 1, hRepeat), rectHeight * fminf(vindex + 1, vRepeat), 0); + + if (!spriteFrame->isRotated()) { + quad.bl.texCoords = cocos2d::Tex2F(u0, v0); + quad.br.texCoords = cocos2d::Tex2F(u0 + (u1 - u0) * fminf(1, hRepeat - hindex), v0); + quad.tl.texCoords = cocos2d::Tex2F(u0, v0 + (v1 - v0) * fminf(1, vRepeat - vindex)); + quad.tr.texCoords = cocos2d::Tex2F(u0 + (u1 - u0) * fminf(1, hRepeat - hindex), v0 + (v1 - v0) * fminf(1, vRepeat - vindex)); + } else { + quad.bl.texCoords = cocos2d::Tex2F(u0, v1); + quad.br.texCoords = cocos2d::Tex2F(u0, v1 + (v0 - v1) * fminf(1, hRepeat - hindex)); + quad.tl.texCoords = cocos2d::Tex2F(u0 + (u1 - u0) * fminf(1, vRepeat - vindex), v1); + quad.tr.texCoords = cocos2d::Tex2F(u0 + (u1 - u0) * fminf(1, vRepeat - vindex), v1 + (v0 - v1) * fminf(1, hRepeat - hindex)); + + } + } + } + return quads; + } + + std::vector _calculateUVs (cocos2d::SpriteFrame* spriteFrame) { + auto atlasWidth = spriteFrame->getTexture()->getPixelsWide(); + auto atlasHeight = spriteFrame->getTexture()->getPixelsHigh(); + + auto textureRect = spriteFrame->getRectInPixels(); + + //uv computation should take spritesheet into account. + float u0, u3; + float v0, v3; + + if (spriteFrame->isRotated()) { + u0 = textureRect.origin.x / atlasWidth; + u3 = (textureRect.origin.x + textureRect.size.height) / atlasWidth; + + v0 = textureRect.origin.y / atlasHeight; + v3 = (textureRect.origin.y + textureRect.size.width) / atlasHeight; + } + else { + u0 = textureRect.origin.x / atlasWidth; + u3 = (textureRect.origin.x + textureRect.size.width) / atlasWidth; + + v0 = textureRect.origin.y / atlasHeight; + v3 = (textureRect.origin.y + textureRect.size.height) / atlasHeight; + } + + std::vector uvCoordinates(2); + uvCoordinates[0] = cocos2d::Vec2(u0, v3); + uvCoordinates[1] = cocos2d::Vec2(u3, v0); + + return uvCoordinates; + } +}; + +class fillQuadGeneratorBar +{ +public: + //percentage from 0 to 1; + std::vector _rebuildQuads_base(cocos2d::SpriteFrame* spriteFrame, const cocos2d::Size& contentSize, const cocos2d::Color4B& colorOpacity, Scale9SpriteV2::FillType fillType, float fillStart, float fillRange) { + float fillEnd; + //build vertices + auto vertices = this->_calculateVertices(spriteFrame, contentSize); + + //build uvs + auto uvs = this->_calculateUVs(spriteFrame); + + //build quads + std::vector quads(1); + auto& quad = quads[0]; + + quad.bl.colors = colorOpacity; + quad.br.colors = colorOpacity; + quad.tl.colors = colorOpacity; + quad.tr.colors = colorOpacity; + + quad.bl.vertices.x = quad.tl.vertices.x = vertices[0].x; + quad.br.vertices.x = quad.tr.vertices.x = vertices[1].x; + + quad.bl.vertices.y = quad.br.vertices.y = vertices[0].y; + quad.tl.vertices.y = quad.tr.vertices.y = vertices[1].y; + + cocos2d::Tex2F quadUV[4]; + + if (!spriteFrame->isRotated()) { + quadUV[0] = cocos2d::Tex2F(uvs[0].x, uvs[0].y); + quadUV[1] = cocos2d::Tex2F(uvs[1].x, uvs[0].y); + quadUV[2] = cocos2d::Tex2F(uvs[0].x, uvs[1].y); + quadUV[3] = cocos2d::Tex2F(uvs[1].x, uvs[1].y); + } else { + quadUV[0] = cocos2d::Tex2F(uvs[0].x, uvs[1].y); + quadUV[1] = cocos2d::Tex2F(uvs[0].x, uvs[0].y); + quadUV[2]= cocos2d::Tex2F(uvs[1].x, uvs[1].y); + quadUV[3]= cocos2d::Tex2F(uvs[1].x, uvs[0].y); + } + + //do clamp + fillStart = fillStart > 1 ? 1 : fillStart; + fillStart = fillStart < 0 ? 0 : fillStart; + + fillRange = fillRange < 0 ? 0 : fillRange; + + fillEnd = fillStart + fillRange; + + fillEnd = fillEnd > 1 ? 1 : fillEnd; + + float progressStart, progressEnd; + switch (fillType) { + case Scale9SpriteV2::FillType::HORIZONTAL: + progressStart = vertices[0].x + (vertices[1].x - vertices[0].x) * fillStart; + progressEnd = vertices[0].x + (vertices[1].x - vertices[0].x) * fillEnd; + + quad.bl.vertices.x = progressStart; + quad.tl.vertices.x = progressStart; + + quad.br.vertices.x = progressEnd; + quad.tr.vertices.x = progressEnd; + + quad.bl.texCoords.u = quadUV[0].u + (quadUV[1].u - quadUV[0].u) * fillStart; + quad.bl.texCoords.v = quadUV[0].v + (quadUV[1].v - quadUV[0].v) * fillStart; + + quad.tl.texCoords.u = quadUV[2].u + (quadUV[3].u - quadUV[2].u) * fillStart; + quad.tl.texCoords.v = quadUV[2].v + (quadUV[3].v - quadUV[2].v) * fillStart; + + quad.br.texCoords.u = quadUV[0].u + (quadUV[1].u - quadUV[0].u) * fillEnd; + quad.br.texCoords.v = quadUV[0].v + (quadUV[1].v - quadUV[0].v) * fillEnd; + + quad.tr.texCoords.u = quadUV[2].u + (quadUV[3].u - quadUV[2].u) * fillEnd; + quad.tr.texCoords.v = quadUV[2].v + (quadUV[3].v - quadUV[2].v) * fillEnd; + break; + case Scale9SpriteV2::FillType::VERTICAL: + progressStart = vertices[0].y + (vertices[1].y - vertices[0].y) * fillStart; + progressEnd = vertices[0].y + (vertices[1].y - vertices[0].y) * fillEnd; + + quad.bl.vertices.y = progressStart; + quad.br.vertices.y = progressStart; + + quad.tl.vertices.y = progressEnd; + quad.tr.vertices.y = progressEnd; + + quad.bl.texCoords.u = quadUV[0].u + (quadUV[2].u - quadUV[0].u) * fillStart; + quad.bl.texCoords.v = quadUV[0].v + (quadUV[2].v - quadUV[0].v) * fillStart; + + quad.br.texCoords.u = quadUV[1].u + (quadUV[3].u - quadUV[1].u) * fillStart; + quad.br.texCoords.v = quadUV[1].v + (quadUV[3].v - quadUV[1].v) * fillStart; + + quad.tl.texCoords.u = quadUV[0].u + (quadUV[2].u - quadUV[0].u) * fillEnd; + quad.tl.texCoords.v = quadUV[0].v + (quadUV[2].v - quadUV[0].v) * fillEnd; + + quad.tr.texCoords.u = quadUV[1].u + (quadUV[3].u - quadUV[1].u) * fillEnd; + quad.tr.texCoords.v = quadUV[1].v + (quadUV[3].v - quadUV[1].v) * fillEnd; + break; + default: + cocos2d::log("Unrecognized fill type in bar fill"); + break; + } + + return quads; + } + + std::vector _calculateVertices (cocos2d::SpriteFrame* spriteFrame, const cocos2d::Size& contentSize) { + + float x0,x3; + float y0,y3; + x0 = 0; + x3 = contentSize.width; + + y0 = 0; + y3 = contentSize.height; + std::vector vertices(2); + vertices[0] = cocos2d::Vec2(x0,y0); + vertices[1] = cocos2d::Vec2(x3,y3); + return vertices; + } + + std::vector _calculateUVs(cocos2d::SpriteFrame* spriteFrame) { + auto atlasWidth = spriteFrame->getTexture()->getPixelsWide(); + auto atlasHeight = spriteFrame->getTexture()->getPixelsHigh(); + + auto textureRect = spriteFrame->getRectInPixels(); + + //uv computation should take spritesheet into account. + float u0, u3; + float v0, v3; + + if (spriteFrame->isRotated()) { + u0 = textureRect.origin.x / atlasWidth; + u3 = (textureRect.origin.x + textureRect.size.height) / atlasWidth; + + v0 = textureRect.origin.y / atlasHeight; + v3 = (textureRect.origin.y + textureRect.size.width) / atlasHeight; + } + else { + u0 = textureRect.origin.x / atlasWidth; + u3 = (textureRect.origin.x + textureRect.size.width) / atlasWidth; + + v0 = textureRect.origin.y / atlasHeight; + v3 = (textureRect.origin.y + textureRect.size.height) / atlasHeight; + } + std::vector uvs(2); + uvs[0] = cocos2d::Vec2(u0,v3); + uvs[1] = cocos2d::Vec2(u3,v0); + return uvs; + } +}; + +class fillQuadGeneratorRadial +{ +private: + cocos2d::Vec2 _vertPos[4]; + cocos2d::Vec2 _vertices[2]; + cocos2d::Vec2 _uvs[2]; + std::vector _intersectPoint_1; + std::vector _intersectPoint_2; +public: + fillQuadGeneratorRadial() { + _intersectPoint_1.resize(4); + _intersectPoint_2.resize(4); + + } + + std::vector _rebuildQuads_base(cocos2d::SpriteFrame* spriteFrame, const cocos2d::Size& contentSize, const cocos2d::Color4B& colorOpacity, const cocos2d::Vec2& fillCenter, float fillStart, float fillRange, cocos2d::V3F_C4B_T2F_Quad& rawQuad) { + + //do round fill start [0,1), include 0, exclude 1 + while (fillStart >= 1.0) fillStart -= 1.0; + while (fillStart < 0.0) fillStart += 1.0; + + auto center = cocos2d::Vec2(fillCenter); + + center.x *= contentSize.width; + center.y *= contentSize.height; + + fillStart *= M_PI * 2; + fillRange *= M_PI * 2; + float fillEnd = fillStart + fillRange; + + //build vertices + this->_calculateVertices(spriteFrame, contentSize); + //build uvs + this->_calculateUVs(spriteFrame); + + this->_vertPos[0].x = this->_vertPos[3].x = this->_vertices[0].x; + this->_vertPos[1].x = this->_vertPos[2].x = this->_vertices[1].x; + this->_vertPos[0].y = this->_vertPos[1].y = this->_vertices[0].y; + this->_vertPos[2].y = this->_vertPos[3].y = this->_vertices[1].y; + + //fallback + //todo remove it if outside is implemented + if(center.x > this->_vertices[1].x) { + center.x = this->_vertices[1].x; + } + if(center.x < this->_vertices[0].x) { + center.x = this->_vertices[0].x; + } + if(center.y < this->_vertices[0].y) { + center.y = this->_vertices[0].y; + } + if(center.y > this->_vertices[1].y) { + center.y = this->_vertices[1].y; + } + + rawQuad.bl.colors = colorOpacity; + rawQuad.br.colors = colorOpacity; + rawQuad.tl.colors = colorOpacity; + rawQuad.tr.colors = colorOpacity; + + rawQuad.bl.vertices.x = rawQuad.tl.vertices.x = this->_vertices[0].x; + rawQuad.br.vertices.x = rawQuad.tr.vertices.x = this->_vertices[1].x; + + rawQuad.bl.vertices.y = rawQuad.br.vertices.y = this->_vertices[0].y; + rawQuad.tl.vertices.y = rawQuad.tr.vertices.y = this->_vertices[1].y; + + + if (!spriteFrame->isRotated()) { + rawQuad.bl.texCoords.u = rawQuad.tl.texCoords.u = this->_uvs[0].x; + rawQuad.bl.texCoords.v = rawQuad.br.texCoords.v = this->_uvs[0].y; + rawQuad.br.texCoords.u = rawQuad.tr.texCoords.u = this->_uvs[1].x; + rawQuad.tl.texCoords.v = rawQuad.tr.texCoords.v = this->_uvs[1].y; + + } else { + rawQuad.bl.texCoords.u = rawQuad.br.texCoords.u = this->_uvs[0].x; + rawQuad.tl.texCoords.u = rawQuad.tr.texCoords.u = this->_uvs[1].x; + rawQuad.br.texCoords.v = rawQuad.tr.texCoords.v = this->_uvs[0].y; + rawQuad.bl.texCoords.v = rawQuad.tl.texCoords.v = this->_uvs[1].y; + + } + + int triangles[4][2] = {{-1,-1}, {-1,-1}, {-1,-1}, {-1,-1}}; + + if(center.x != this->_vertices[0].x) { + triangles[0][0] = 3; + triangles[0][1] = 0; + } + if(center.x != this->_vertices[1].x) { + triangles[2][0] = 1; + triangles[2][1] = 2; + } + if(center.y != this->_vertices[0].y) { + triangles[1][0] = 0; + triangles[1][1] = 1; + } + if(center.y != this->_vertices[1].y) { + triangles[3][0] = 2; + triangles[3][1] = 3; + + } + + this->_getInsectedPoints(this->_vertices[0].x, this->_vertices[1].x, this->_vertices[0].y, this->_vertices[1].y, center, fillStart, this->_intersectPoint_1); + this->_getInsectedPoints(this->_vertices[0].x, this->_vertices[1].x, this->_vertices[0].y, this->_vertices[1].y, center, fillStart + fillRange, this->_intersectPoint_2); + + std::vector quads; + + for(int triangleIndex = 0; triangleIndex < 4; ++triangleIndex ) { + auto& triangle = triangles[triangleIndex]; + if(triangle[0] == -1 || triangle[1] == -1) { + continue; + } + //all in + if(fillRange >= M_PI * 2) { + quads.push_back(this->_generateTriangle(rawQuad, center, this->_vertPos[triangle[0]], this->_vertPos[triangle[1]],colorOpacity)); + continue; + } + //test against + auto startAngle = this->_getVertAngle(center,this->_vertPos[triangle[0]]); + auto endAngle = this->_getVertAngle(center,this->_vertPos[triangle[1]]); + if(endAngle < startAngle) endAngle += M_PI * 2; + startAngle -= M_PI * 2; + endAngle -= M_PI * 2; + //testing + for(int testIndex = 0; testIndex < 3; ++testIndex) { + if(startAngle >= fillEnd) { + //all out + } else if (startAngle >= fillStart) { + if(endAngle >= fillEnd) { + //startAngle to fillEnd + quads.push_back(this->_generateTriangle(rawQuad, center, this->_vertPos[triangle[0]], this->_intersectPoint_2[triangleIndex],colorOpacity)); + } else { + //startAngle to endAngle + quads.push_back(this->_generateTriangle(rawQuad, center, this->_vertPos[triangle[0]], this->_vertPos[triangle[1]],colorOpacity)); + } + } else { + //startAngle < fillStart + if(endAngle <= fillStart) { + //all out + } else if(endAngle <= fillEnd) { + //fillStart to endAngle + quads.push_back(this->_generateTriangle(rawQuad, center, this->_intersectPoint_1[triangleIndex], this->_vertPos[triangle[1]],colorOpacity)); + } else { + //fillStart to fillEnd + quads.push_back(this->_generateTriangle(rawQuad, center, this->_intersectPoint_1[triangleIndex], this->_intersectPoint_2[triangleIndex],colorOpacity)); + } + } + + //add 2 * PI + startAngle += M_PI * 2; + endAngle += M_PI * 2; + } + + } + + return quads; + } + + cocos2d::V3F_C4B_T2F_Quad _generateTriangle(const cocos2d::V3F_C4B_T2F_Quad& rawQuad, const cocos2d::Vec2& vert0, const cocos2d::Vec2& vert1, const cocos2d::Vec2& vert2 , const cocos2d::Color4B& colorOpacity) { + const auto& rawQuad_bl = rawQuad.bl; + const auto& rawQuad_br = rawQuad.br; + const auto& rawQuad_tl = rawQuad.tl; + const auto& rawQuad_tr = rawQuad.tr; + + auto v0x = rawQuad_bl.vertices.x; + auto v0y = rawQuad_bl.vertices.y; + auto v1x = rawQuad_tr.vertices.x; + auto v1y = rawQuad_tr.vertices.y; + float progressX, progressY; + cocos2d::V3F_C4B_T2F_Quad quad; + quad.tl.vertices.x = vert0.x; + quad.tl.vertices.y = vert0.y; + + quad.bl.vertices.x = vert1.x; + quad.bl.vertices.y = vert1.y; + + quad.tr.vertices.x = vert2.x; + quad.tr.vertices.y = vert2.y; + quad.tl.colors = colorOpacity; + quad.bl.colors = colorOpacity; + quad.tr.colors = colorOpacity; + + progressX = (vert0.x - v0x) / (v1x - v0x); + progressY = (vert0.y - v0y) / (v1y - v0y); + this->_generateUV(progressX, progressY, rawQuad_bl.texCoords,rawQuad_br.texCoords,rawQuad_tr.texCoords,rawQuad_tl.texCoords, quad.tl.texCoords); + + progressX = (vert1.x - v0x) / (v1x - v0x); + progressY = (vert1.y - v0y) / (v1y - v0y); + this->_generateUV(progressX, progressY, rawQuad_bl.texCoords,rawQuad_br.texCoords,rawQuad_tr.texCoords,rawQuad_tl.texCoords,quad.bl.texCoords); + + progressX = (vert2.x - v0x) / (v1x - v0x); + progressY = (vert2.y - v0y) / (v1y - v0y); + this->_generateUV(progressX, progressY, rawQuad_bl.texCoords,rawQuad_br.texCoords,rawQuad_tr.texCoords,rawQuad_tl.texCoords,quad.tr.texCoords); + + return quad; + } + + bool _isAngleIn (float angle, float start, float rangeAngle) { + auto pi_2 = M_PI * 2; + while(angle < start || angle >= start + pi_2) { + if(angle < start) { + angle += pi_2; + } + if(angle >= start + pi_2) { + angle -= pi_2; + } + } + + return angle <= start + rangeAngle; + } + + //[0,PI * 2) + float _getVertAngle(const cocos2d::Vec2& start, const cocos2d::Vec2& end) { + float placementX, placementY; + placementX = end.x - start.x; + placementY = end.y - start.y; + + if(placementX == 0 && placementY == 0) { + return FLT_MAX; + } else if(placementX == 0) { + if(placementY > 0) { + return M_PI * 0.5; + } else { + return M_PI * 1.5; + } + } else { + float angle = atanf(placementY / placementX); + if(placementX < 0) { + angle += M_PI; + } + + return angle; + } + } + + void _getInsectedPoints(float left, float right, float bottom, float top, const cocos2d::Vec2& center, float angle, std::vector &intersectPoints) { + //left bottom, right, top + auto sinAngle = sinf(angle); + auto cosAngle = cosf(angle); + float tanAngle,cotAngle; + if(cosf(angle) != 0) { + tanAngle = sinAngle / cosAngle; + //calculate right and left + if((left - center.x) * cosAngle > 0) { + auto yleft = center.y + tanAngle * (left - center.x); + intersectPoints[0].x = left; + intersectPoints[0].y = yleft; + } + if((right - center.x) * cosAngle > 0) { + auto yright = center.y + tanAngle * (right - center.x); + + intersectPoints[2].x = right; + intersectPoints[2].y = yright; + } + + } + + if(sinf(angle) != 0) { + cotAngle = cosAngle / sinAngle; + //calculate top and bottom + if((top - center.y) * sinAngle > 0) { + auto xtop = center.x + cotAngle * (top-center.y); + intersectPoints[3].x = xtop; + intersectPoints[3].y = top; + } + if((bottom - center.y) * sinAngle > 0) { + auto xbottom = center.x + cotAngle * (bottom-center.y); + intersectPoints[1].x = xbottom; + intersectPoints[1].y = bottom; + } + + } + } + + void _generateUV (float progressX, float progressY, const cocos2d::Tex2F& uvbl, const cocos2d::Tex2F& uvbr, const cocos2d::Tex2F& uvtr, const cocos2d::Tex2F& uvtl, cocos2d::Tex2F& result) { + auto px1 = uvbl.u + (uvbr.u-uvbl.u) * progressX; + auto px2 = uvtl.u + (uvtr.u-uvtl.u) * progressX; + auto py1 = uvbl.v + (uvbr.v-uvbl.v) * progressX; + auto py2 = uvtl.v + (uvtr.v-uvtl.v) * progressX; + result.u = px1 + (px2 - px1) * progressY; + result.v = py1 + (py2 - py1) * progressY; + } + + + void _calculateVertices(cocos2d::SpriteFrame* spriteFrame, const cocos2d::Size& contentSize) { + + float x0,x3; + float y0,y3; + x0 = 0; + x3 = contentSize.width; + + y0 = 0; + y3 = contentSize.height; + + this->_vertices[0].x = x0; + this->_vertices[0].y = y0; + this->_vertices[1].x = x3; + this->_vertices[1].y = y3; + } + + void _calculateUVs(cocos2d::SpriteFrame* spriteFrame) { + auto atlasWidth = spriteFrame->getTexture()->getPixelsWide(); + auto atlasHeight = spriteFrame->getTexture()->getPixelsHigh(); + + auto textureRect = spriteFrame->getRectInPixels(); + + //uv computation should take spritesheet into account. + float u0, u3; + float v0, v3; + + if (spriteFrame->isRotated()) { + u0 = textureRect.origin.x / atlasWidth; + u3 = (textureRect.origin.x + textureRect.size.height) / atlasWidth; + + v0 = textureRect.origin.y / atlasHeight; + v3 = (textureRect.origin.y + textureRect.size.width) / atlasHeight; + } + else { + u0 = textureRect.origin.x / atlasWidth; + u3 = (textureRect.origin.x + textureRect.size.width) / atlasWidth; + + v0 = textureRect.origin.y / atlasHeight; + v3 = (textureRect.origin.y + textureRect.size.height) / atlasHeight; + } + + this->_uvs[0].x = u0; + this->_uvs[0].y = v3; + this->_uvs[1].x = u3; + this->_uvs[1].y = v0; + + } +}; + +//begin of Scale9 sprite implementation +Scale9SpriteV2::Scale9SpriteV2() : +_spriteFrame(nullptr), +_insetLeft(0), +_insetRight(0), +_insetTop(0), +_insetBottom(0), +_blendFunc(cocos2d::BlendFunc::ALPHA_NON_PREMULTIPLIED), +_renderingType(Scale9SpriteV2::RenderingType::SIMPLE), +_brightState(Scale9SpriteV2::State::NORMAL), +_quadsDirty(true), +_isTriangle(false), +_isTrimmedContentSize(true), +_fillType(Scale9SpriteV2::FillType::HORIZONTAL), +_fillCenter(cocos2d::Vec2(0,0)), +_fillStart(0), +_fillRange(0), +_needRebuildRenderCommand(true) +{ + this->setAnchorPoint(cocos2d::Vec2(0.5,0.5)); + this->setGLProgramState(cocos2d::GLProgramState::getOrCreateWithGLProgramName(cocos2d::GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP)); +} + +Scale9SpriteV2::~Scale9SpriteV2() { + CC_SAFE_RELEASE(this->_spriteFrame); +} + +bool Scale9SpriteV2::setTexture(const std::string &textureFile) +{ + auto texture = cocos2d::Director::getInstance()->getTextureCache()->addImage(textureFile); + return texture && this->setTexture(texture); +} +// +bool Scale9SpriteV2::setTexture(cocos2d::Texture2D* texture) +{ + auto spriteFrame = cocos2d::SpriteFrame::createWithTexture(texture, cocos2d::Rect(0,0,texture->getPixelsWide(),texture->getPixelsHigh())); + return spriteFrame && this->setSpriteFrame(spriteFrame); +} + +bool Scale9SpriteV2::setSpriteFrame(const std::string& sfName) +{ + auto spriteFrame = cocos2d::SpriteFrameCache::getInstance()->getSpriteFrameByName(sfName); + return spriteFrame && this->setSpriteFrame(spriteFrame); +} + +bool Scale9SpriteV2::setSpriteFrame(cocos2d::SpriteFrame* spriteFrame) +{ + if(!spriteFrame) return false; + CC_SAFE_RELEASE(this->_spriteFrame); + this->_spriteFrame = spriteFrame; + CC_SAFE_RETAIN(spriteFrame); + this->_quadsDirty = true; + if(this->_contentSize.equals(cocos2d::Size::ZERO)) + { + this->setContentSize(spriteFrame->getRect().size); + } + return true; +} + +void Scale9SpriteV2::setBlendFunc(const cocos2d::BlendFunc& blendFunc) { + this->setBlendFunc(blendFunc.src, blendFunc.dst); +} + +void Scale9SpriteV2::setBlendFunc(GLenum src, GLenum dst) { + this->_blendFunc.src = src; + this->_blendFunc.dst = dst; +} + +const cocos2d::BlendFunc& Scale9SpriteV2::getBlendFunc() const { + return this->_blendFunc; +} + +void Scale9SpriteV2::setContentSize(const cocos2d::Size &contentSize) { + if(contentSize.equals(this->_contentSize)) return; + Node::setContentSize(contentSize); + this->_quadsDirty = true; +} + +void Scale9SpriteV2::enableTrimmedContentSize(bool isTrimmed) { + if (this->_isTrimmedContentSize != isTrimmed) { + this->_isTrimmedContentSize = isTrimmed; + this->_quadsDirty = true; + } +} + +void Scale9SpriteV2::setState(State state) { + this->_brightState = state; + //todo add rendering code +} + +void Scale9SpriteV2::setRenderingType(RenderingType type) { + if(this->_renderingType == type) return; + this->_renderingType = type; + this->_quadsDirty = true; +} + +void Scale9SpriteV2::setInsetLeft(float value) { + this->_insetLeft = value; + this->_quadsDirty = true; +} + +void Scale9SpriteV2::setInsetTop(float value) { + this->_insetTop = value; + this->_quadsDirty = true; +} + +void Scale9SpriteV2::setInsetRight(float value) { + this->_insetRight = value; + this->_quadsDirty = true; +} + +void Scale9SpriteV2::setInsetBottom(float value) { + this->_insetBottom = value; + this->_quadsDirty = true; +} + +void Scale9SpriteV2::setFillType(FillType value) { + if(this->_fillType == value) return; + this->_fillType = value; + if(this->_renderingType == RenderingType::FILLED) this->_quadsDirty = true; +} + +void Scale9SpriteV2::setFillCenter(const cocos2d::Vec2& center) { + setFillCenter(center.x, center.y); +} + +void Scale9SpriteV2::setFillCenter(float x, float y) { + this->_fillCenter.x = x; + this->_fillCenter.y = y; + if(this->_renderingType == RenderingType::FILLED && this->_fillType == FillType::RADIAL) this->_quadsDirty = true; +} + +void Scale9SpriteV2::setFillStart(float value) { + this->_fillStart = value; + if(this->_renderingType == RenderingType::FILLED) this->_quadsDirty = true; +} + +void Scale9SpriteV2::setFillRange(float value) { + this->_fillRange = value; + if(this->_renderingType == RenderingType::FILLED) this->_quadsDirty = true; +} + +void Scale9SpriteV2::_rebuildQuads() +{ + if (this->_quadsDirty == false) return; + cocos2d::Color4B color(this->getDisplayedColor()); + color.a = this->getDisplayedOpacity(); + this->_isTriangle = false; + if (this->_renderingType == RenderingType::SIMPLE) { + this->_quads = simpleQuadGenerator()._rebuildQuads_base(this->_spriteFrame, this->getContentSize(), color, this->_isTrimmedContentSize); + } else if (this->_renderingType == RenderingType::SLICED) { + this->_quads = scale9QuadGenerator()._rebuildQuads_base(this->_spriteFrame, this->getContentSize(), color, this->_insetLeft, this->_insetRight, this->_insetTop, this->_insetBottom); + } else if (this->_renderingType == RenderingType::TILED) { + this->_quads = tiledQuadGenerator()._rebuildQuads_base(this->_spriteFrame, this->getContentSize(), color); + } else if (this->_renderingType == RenderingType::FILLED) { + auto fillstart = this->_fillStart; + auto fillRange = this->_fillRange; + if(fillRange < 0) { + fillstart += fillRange; + fillRange = -fillRange; + } + if(this->_fillType != FillType::RADIAL) { + fillRange = fillstart + fillRange; + fillstart = fillstart > 1.0 ? 1.0 : fillstart; + fillstart = fillstart < 0.0 ? 0.0 : fillstart; + + fillRange = fillRange > 1.0 ? 1.0 : fillRange; + fillRange = fillRange < 0.0 ? 0.0 : fillRange; + fillRange = fillRange - fillstart; + this->_quads = fillQuadGeneratorBar()._rebuildQuads_base(this->_spriteFrame, this->getContentSize(), color, this->_fillType, fillstart,fillRange); + } else { + this->_isTriangle = true; + this->_quads = fillQuadGeneratorRadial()._rebuildQuads_base(this->_spriteFrame, this->getContentSize(), color,this->_fillCenter,fillstart,fillRange, this->_rawQuad); + } + } else { + this->_quads.clear(); + cocos2d::log("Can not generate quad"); + } + this->_quadsDirty = false; +} + +void Scale9SpriteV2::updateColor() { + cocos2d::Color4B color(this->getDisplayedColor()); + color.a = this->getDisplayedOpacity(); + for (int index = 0; index < this->_quads.size(); ++index) { + this->_quads[index].bl.colors = color; + this->_quads[index].br.colors = color; + this->_quads[index].tl.colors = color; + this->_quads[index].tr.colors = color; + } + + this->_needRebuildRenderCommand = true; +} + +void Scale9SpriteV2::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags) { + if (!this->_spriteFrame || !this->_spriteFrame->getTexture()) { + return; + } + + if(this->_quadsDirty) { + //rebuild quads + this->_rebuildQuads(); + this->_needRebuildRenderCommand = true; + } + if(this->_needRebuildRenderCommand) { + //fill triangles + int vertsStep = this->_isTriangle ? 3 : 4; + int indicesStep = this->_isTriangle ? 3 : 6; + this->_verts.resize(_quads.size() * vertsStep); + this->_indices.resize(_quads.size() * indicesStep); + unsigned short indices[6]; + for(int index = 0; index < _quads.size(); ++index) { + memcpy(&this->_verts[vertsStep * index], &this->_quads[index], sizeof(cocos2d::V3F_C4B_T2F) * vertsStep); + if(this->_isTriangle) { + indices[0] = indicesStep * index; + indices[1] = indicesStep * index + 1; + indices[2] = indicesStep * index + 2; + + } else { + indices[0] = vertsStep * index; + indices[1] = vertsStep * index + 1; + indices[2] = vertsStep * index + 2; + indices[3] = vertsStep * index + 3; + indices[4] = vertsStep * index + 2; + indices[5] = vertsStep * index + 1; + + } + + memcpy(&this->_indices[indicesStep * index], indices, sizeof(unsigned short) * indicesStep); + + } + + this->_needRebuildRenderCommand = false; + + } + cocos2d::TrianglesCommand::Triangles triangles; + triangles.indices = &this->_indices[0]; + triangles.verts = &this->_verts[0]; + triangles.vertCount = this->_verts.size(); + triangles.indexCount = this->_indices.size(); + auto texture = this->_spriteFrame->getTexture(); + this->_renderCommand.init(this->_globalZOrder, texture->getName(),this->getGLProgramState(), this->_blendFunc, triangles, transform, 0); + renderer->addCommand(&this->_renderCommand); +} + +} diff --git a/native/cocos/editor-support/creator/CCScale9Sprite.h b/native/cocos/editor-support/creator/CCScale9Sprite.h new file mode 100755 index 0000000000..e7451e2364 --- /dev/null +++ b/native/cocos/editor-support/creator/CCScale9Sprite.h @@ -0,0 +1,168 @@ + +/**************************************************************************** + Copyright (c) 2016 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + + ****************************************************************************/ + + +#ifndef __CREATOR_CCSCALE9SPRITE_H__ +#define __CREATOR_CCSCALE9SPRITE_H__ + +#include "2d/CCNode.h" +#include "2d/CCSpriteFrame.h" +#include "renderer/CCTrianglesCommand.h" + +#include + +namespace creator { + +class Scale9SpriteV2 : public cocos2d::Node +{ +public: + enum class FillType + { + HORIZONTAL = 0, + VERTICAL = 1, + RADIAL = 2, + }; + + enum class RenderingType + { + SIMPLE = 0, + SLICED = 1, + TILED = 2, + FILLED = 3, + }; + + enum class State + { + NORMAL = 0, + GRAY = 1, + }; + +public: + Scale9SpriteV2(); + virtual ~Scale9SpriteV2(); + + bool initWithTexture (cocos2d::Texture2D* texture) { + return this->setTexture(texture); + } + + bool initWithTexture (const std::string& textureFile) { + return this->setTexture(textureFile); + } + + bool initWithSpriteFrame (cocos2d::SpriteFrame* spriteFrame) { + return this->setSpriteFrame(spriteFrame); + } + + bool initWithSpriteFrame (const std::string& sfName) { + return this->setSpriteFrame(sfName); + } + + bool setTexture (const std::string& textureFile); + bool setTexture (cocos2d::Texture2D* texture); + bool setSpriteFrame(const std::string& sfName); + bool setSpriteFrame(cocos2d::SpriteFrame* spriteFrame); + + void setBlendFunc(const cocos2d::BlendFunc& blendFunc); + void setBlendFunc(GLenum src, GLenum dst); + const cocos2d::BlendFunc& getBlendFunc() const; + + virtual void setContentSize(const cocos2d::Size& contentSize) override; + + void enableTrimmedContentSize (bool isTrimmed); + bool isTrimmedContentSizeEnabled() const { return this->_isTrimmedContentSize; } + + void setState(State state); + State getState() const { return this->_brightState; } + + void setRenderingType(RenderingType type); + RenderingType getRenderingType() const { return this->_renderingType; } + + void setInsetLeft(float value); + void setInsetTop(float value); + void setInsetRight(float value); + void setInsetBottom(float value); + + float getInsetLeft() const { return this->_insetLeft; } + float getInsetTop() const { return this->_insetTop; } + float getInsetRight() const { return this->_insetRight; } + float getInsetBottom() const { return this->_insetBottom; } + + void setFillType(FillType value); + void setFillCenter(const cocos2d::Vec2& center); + void setFillCenter(float x, float y); + void setFillStart(float value); + void setFillRange(float value); + + FillType getFillType() const { return this->_fillType; } + const cocos2d::Vec2& getFillCenter() const { return this->_fillCenter; } + float getFillStart() const { return this->_fillStart; } + float getFillRange() const { return this->_fillRange; } + + virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags) override; +protected: + virtual void updateColor() override; + +private: + void _rebuildQuads (); + //resource data + cocos2d::SpriteFrame* _spriteFrame; + + //scale 9 data + float _insetLeft; + float _insetRight; + float _insetTop; + float _insetBottom; + //blend function + cocos2d::BlendFunc _blendFunc; + //sliced or simple + RenderingType _renderingType; + //bright or not + State _brightState; + //rendering quads + std::vector _quads; + bool _quadsDirty; + cocos2d::V3F_C4B_T2F_Quad _rawQuad; + bool _isTriangle; + bool _isTrimmedContentSize; + //fill type + FillType _fillType; + //for fill radial + cocos2d::Vec2 _fillCenter; + //normalized filled start and range + float _fillStart; + float _fillRange; + //indicate that rendercommand need to be rebuild or not; + bool _needRebuildRenderCommand; + + std::vector _verts; + std::vector _indices; + cocos2d::TrianglesCommand _renderCommand; + +}; + +} + +#endif diff --git a/native/templates/cocos2dx_files.json b/native/templates/cocos2dx_files.json index 2fc4cb7ecf..74f25935fa 100644 --- a/native/templates/cocos2dx_files.json +++ b/native/templates/cocos2dx_files.json @@ -495,6 +495,9 @@ "cocos/editor-support/spine/spine-cocos2dx.cpp", "cocos/editor-support/spine/spine-cocos2dx.h", "cocos/editor-support/spine/spine.h", + "cocos/editor-support/creator/CCScale9Sprite.h", + "cocos/editor-support/creator/CCScale9Sprite.cpp", + "cocos/editor-support/creator/Android.mk", "cocos/math/CCAffineTransform.cpp", "cocos/math/CCAffineTransform.h", "cocos/math/CCGeometry.cpp", @@ -2722,6 +2725,8 @@ "cocos/scripting/js-bindings/auto/jsb_cocos2dx_studio_auto.hpp", "cocos/scripting/js-bindings/auto/jsb_cocos2dx_ui_auto.cpp", "cocos/scripting/js-bindings/auto/jsb_cocos2dx_ui_auto.hpp", + "cocos/scripting/js-bindings/auto/jsb_creator_auto.cpp", + "cocos/scripting/js-bindings/auto/jsb_creator_auto.hpp", "cocos/scripting/js-bindings/manual/ScriptingCore.cpp", "cocos/scripting/js-bindings/manual/ScriptingCore.h", "cocos/scripting/js-bindings/manual/chipmunk/js_bindings_chipmunk_auto_classes.cpp", diff --git a/native/tools/tojs/creator.ini b/native/tools/tojs/creator.ini new file mode 100755 index 0000000000..9c76b3381a --- /dev/null +++ b/native/tools/tojs/creator.ini @@ -0,0 +1,64 @@ +[creator] +# the prefix to be added to the generated functions. You might or might not use this in your own +# templates +prefix = creator + +# create a target namespace (in javascript, this would create some code like the equiv. to `ns = ns || {}`) +# all classes will be embedded in that namespace +target_namespace = cc + +# the native namespace in which this module locates, this parameter is used for avoid conflict of the same class name in different modules, as "cocos2d::Label" <-> "cocos2d::ui::Label". +cpp_namespace = creator + +android_headers = -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/include +android_flags = -D_SIZE_T_DEFINED_ + +clang_headers = -I%(clangllvmdir)s/lib/clang/%(clang_version)s/include +clang_flags = -nostdinc -x c++ -std=c++11 -U __SSE__ + +cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/editor-support -I%(cocosdir)s/cocos/platform/android -I%(cocosdir)s/external + +cocos_flags = -DANDROID + +cxxgenerator_headers = + +# extra arguments for clang +extra_arguments = %(android_headers)s %(clang_headers)s %(cxxgenerator_headers)s %(cocos_headers)s %(android_flags)s %(clang_flags)s %(cocos_flags)s %(extra_flags)s + +# what headers to parse +headers = %(creatordir)s/CCScale9Sprite.h + +# what classes to produce code for. You can use regular expressions here. When testing the regular +# expression, it will be enclosed in "^$", like this: "^Menu*$". +classes = Scale9SpriteV2 + +classes_need_extend = Scale9SpriteV2 + +# what should we skip? in the format ClassName::[function function] +# ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also +# regular expressions, they will not be surrounded by "^$". If you want to skip a whole class, just +# add a single "*" as functions. See bellow for several examples. A special class name is "*", which +# will apply to all class names. This is a convenience wildcard to be able to skip similar named +# functions from all classes. + +skip = *::[^visit$ ^draw$ copyWith.* onEnter.* onExit.* ^description$] + +rename_functions = + +rename_classes = + +# for all class names, should we remove something when registering in the target VM? +remove_prefix = + +# classes for which there will be no "parent" lookup +classes_have_no_parents = Helper + +# base classes which will be skipped when their sub-classes found them. +base_classes_to_skip = Ref + +# classes that create no constructor +# Set is special and we will use a hand-written constructor +abstract_classes = Helper LayoutManager AbstractCheckButton + +# Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'. +script_control_cpp = yes diff --git a/native/tools/tojs/genbindings.py b/native/tools/tojs/genbindings.py index ec8c6b0e32..214428c838 100755 --- a/native/tools/tojs/genbindings.py +++ b/native/tools/tojs/genbindings.py @@ -98,6 +98,7 @@ def main(): project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) cocos_root = os.path.abspath(project_root) + creator_root = os.path.abspath(os.path.join(cocos_root, 'cocos/editor-support/creator')) jsb_root = os.path.abspath(os.path.join(project_root, 'cocos/scripting/js-bindings')) cxx_generator_root = os.path.abspath(os.path.join(project_root, 'tools/bindings-generator')) @@ -105,6 +106,7 @@ def main(): config = ConfigParser.ConfigParser() config.set('DEFAULT', 'androidndkdir', ndk_root) config.set('DEFAULT', 'clangllvmdir', llvm_path) + config.set('DEFAULT', 'creatordir', creator_root) config.set('DEFAULT', 'cocosdir', cocos_root) config.set('DEFAULT', 'jsbdir', jsb_root) config.set('DEFAULT', 'cxxgeneratordir', cxx_generator_root) @@ -147,7 +149,8 @@ def main(): 'cocos2dx_studio.ini' : ('cocos2dx_studio', 'jsb_cocos2dx_studio_auto'), \ 'cocos2dx_spine.ini' : ('cocos2dx_spine', 'jsb_cocos2dx_spine_auto'), \ 'cocos2dx_experimental_webView.ini' : ('cocos2dx_experimental_webView', 'jsb_cocos2dx_experimental_webView_auto'), \ - 'cocos2dx_experimental_video.ini' : ('cocos2dx_experimental_video', 'jsb_cocos2dx_experimental_video_auto'), + 'cocos2dx_experimental_video.ini' : ('cocos2dx_experimental_video', 'jsb_cocos2dx_experimental_video_auto'), \ + 'creator.ini': ('creator', 'jsb_creator_auto'), } target = 'spidermonkey' generator_py = '%s/generator.py' % cxx_generator_root