/****************************************************************************
Copyright (c) 2010 Lam Pham
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2017 Chukong Technologies Inc
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
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 "2d/CCProgressTimer.h"
#include <algorithm>
#include <stddef.h> // offsetof
#include "base/ccTypes.h"
#include "base/ccMacros.h"
#include "base/CCDirector.h"
#include "2d/CCSprite.h"
#include "renderer/CCRenderer.h"
#include "base/ccUtils.h"
#include "renderer/ccShaders.h"
#include "renderer/backend/ProgramState.h"
NS_CC_BEGIN
#define kProgressTextureCoordsCount 4
// kProgressTextureCoords holds points {0,1} {0,0} {1,0} {1,1} we can represent it as bits
const char kProgressTextureCoords = 0x4b;
namespace
{
bool isEqual(float a, float b)
{
return abs(a - b) < 0.0001;
}
backend::ProgramState* initPipelineDescriptor(cocos2d::CustomCommand& command, bool ridal, backend::UniformLocation &locMVP, backend::UniformLocation &locTexture)
{
auto& pipelieDescriptor = command.getPipelineDescriptor();
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR);
auto programState = new (std::nothrow) backend::ProgramState(program);
CC_SAFE_RELEASE(pipelieDescriptor.programState);
pipelieDescriptor.programState = programState;
//set vertexLayout according to V2F_C4B_T2F structure
auto vertexLayout = programState->getVertexLayout();
const auto& attributeInfo = programState->getProgram()->getActiveAttributes();
auto iter = attributeInfo.find("a_position");
if(iter != attributeInfo.end())
{
vertexLayout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT2, 0, false);
}
iter = attributeInfo.find("a_texCoord");
if(iter != attributeInfo.end())
{
vertexLayout->setAttribute("a_texCoord", iter->second.location, backend::VertexFormat::FLOAT2, offsetof(V2F_C4B_T2F, texCoords), false);
}
iter = attributeInfo.find("a_color");
if(iter != attributeInfo.end())
{
vertexLayout->setAttribute("a_color", iter->second.location, backend::VertexFormat::UBYTE4, offsetof(V2F_C4B_T2F, colors), true);
}
vertexLayout->setLayout(sizeof(V2F_C4B_T2F));
if (ridal)
{
command.setDrawType(CustomCommand::DrawType::ELEMENT);
command.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE);
}
else
{
command.setDrawType(CustomCommand::DrawType::ARRAY);
command.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE_STRIP);
}
locMVP = programState->getUniformLocation("u_MVPMatrix");
locTexture = programState->getUniformLocation("u_texture");
return programState;
}
}
ProgressTimer* ProgressTimer::create(Sprite* sp)
{
ProgressTimer *progressTimer = new (std::nothrow) ProgressTimer();
if (progressTimer && progressTimer->initWithSprite(sp))
{
progressTimer->autorelease();
return progressTimer;
}
delete progressTimer;
return nullptr;
}
bool ProgressTimer::initWithSprite(Sprite* sp)
{
setAnchorPoint(Vec2(0.5f,0.5f));
setMidpoint(Vec2(0.5f, 0.5f));
setBarChangeRate(Vec2(1,1));
setSprite(sp);
CC_SAFE_RELEASE(_programState);
CC_SAFE_RELEASE(_programState2);
_programState = initPipelineDescriptor(_customCommand, true, _locMVP1, _locTex1);
_programState2 = initPipelineDescriptor(_customCommand2, false, _locMVP2, _locTex2);
return true;
}
ProgressTimer::~ProgressTimer()
{
CC_SAFE_RELEASE(_sprite);
CC_SAFE_RELEASE(_programState);
CC_SAFE_RELEASE(_programState2);
}
void ProgressTimer::setPercentage(float percentage)
{
if (_percentage != percentage)
{
_percentage = clampf(percentage, 0, 100);
updateProgress();
}
}
void ProgressTimer::setSprite(Sprite *sprite)
{
if (_sprite != sprite)
{
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
if (sEngine)
{
if (_sprite)
sEngine->releaseScriptObject(this, _sprite);
if (sprite)
sEngine->retainScriptObject(this, sprite);
}
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
CC_SAFE_RETAIN(sprite);
CC_SAFE_RELEASE(_sprite);
_sprite = sprite;
setContentSize(_sprite->getContentSize());
// Every time we set a new sprite, we free the current vertex data
if (!_vertexData.empty())
{
_vertexData.clear();
updateProgress();
}
}
}
void ProgressTimer::setType(Type type)
{
if (type == Type::RADIAL)
{
_customCommand.setDrawType(CustomCommand::DrawType::ELEMENT);
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE);
}
else
{
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE_STRIP);
_customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
}
if (type != _type)
{
// release all previous information
_vertexData.clear();
_type = type;
}
}
void ProgressTimer::setReverseDirection(bool reverse)
{
if( _reverseDirection != reverse ) {
_reverseDirection = reverse;
// release all previous information
_vertexData.clear();
}
}
// Interval
///
// @returns the vertex position from the texture coordinate
///
Tex2F ProgressTimer::textureCoordFromAlphaPoint(Vec2 alpha)
{
Tex2F ret(0.0f, 0.0f);
if (!_sprite) {
return ret;
}
V3F_C4B_T2F_Quad quad = _sprite->getQuad();
Vec2 min(quad.bl.texCoords.u,quad.bl.texCoords.v);
Vec2 max(quad.tr.texCoords.u,quad.tr.texCoords.v);
// Fix bug #1303 so that progress timer handles sprite frame texture rotation
if (_sprite->isTextureRectRotated()) {
std::swap(alpha.x, alpha.y);
}
return Tex2F(min.x * (1.f - alpha.x) + max.x * alpha.x, min.y * (1.f - alpha.y) + max.y * alpha.y);
}
Vec2 ProgressTimer::vertexFromAlphaPoint(Vec2 alpha)
{
Vec2 ret(0.0f, 0.0f);
if (!_sprite) {
return ret;
}
V3F_C4B_T2F_Quad quad = _sprite->getQuad();
Vec2 min(quad.bl.vertices.x,quad.bl.vertices.y);
Vec2 max(quad.tr.vertices.x,quad.tr.vertices.y);
ret.x = min.x * (1.f - alpha.x) + max.x * alpha.x;
ret.y = min.y * (1.f - alpha.y) + max.y * alpha.y;
return ret;
}
void ProgressTimer::updateColor()
{
if (!_sprite)
return;
if (!_vertexData.empty())
{
const Color4B& sc = _sprite->getQuad().tl.colors;
for (int i = 0; i < _vertexDat
没有合适的资源?快使用搜索试试~ 我知道了~
改写cocos2d的ProgressTimer实现任意起始点的Radial进度条
共2个文件
h:1个
cpp:1个
需积分: 5 1 下载量 38 浏览量
2023-06-20
11:37:32
上传
评论
收藏 10KB ZIP 举报
温馨提示
cocos2dx4.0版本的CCProgressTimer.h 和CCProgressTimer.cpp
资源推荐
资源详情
资源评论
收起资源包目录
CCProgressTimer.zip (2个子文件)
CCProgressTimer
CCProgressTimer.h 7KB
CCProgressTimer.cpp 29KB
共 2 条
- 1
资源评论
泡泡茶壶Wending
- 粉丝: 186
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功