2026-02-13 17:40:07 +08:00
|
|
|
|
#include "Utility.h"
|
2026-02-13 14:34:15 +08:00
|
|
|
|
#include "ui/CocosGUI.h"
|
2026-02-13 17:40:07 +08:00
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
2026-02-13 14:34:15 +08:00
|
|
|
|
#include "iconv.h"
|
2026-02-13 17:40:07 +08:00
|
|
|
|
#else
|
|
|
|
|
|
#include <jni.h>
|
|
|
|
|
|
#include "platform/android/jni/JniHelper.h"
|
|
|
|
|
|
#endif
|
2026-02-13 14:34:15 +08:00
|
|
|
|
#include "Helps.h"
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-02-13 17:40:07 +08:00
|
|
|
|
* 获取utf8字符串长度
|
|
|
|
|
|
* @param utf8 utf8字符串
|
|
|
|
|
|
* @return 字符串长度
|
2026-02-13 14:34:15 +08:00
|
|
|
|
*/
|
|
|
|
|
|
int utf8_len(const char *utf8)
|
|
|
|
|
|
{
|
|
|
|
|
|
int i = 0, j = 0;
|
|
|
|
|
|
while (utf8[i]) {
|
|
|
|
|
|
if ((utf8[i] & 0xc0) != 0x80) j++;
|
|
|
|
|
|
i++;
|
|
|
|
|
|
}
|
|
|
|
|
|
return j;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int utf8_cmp(const char* str1, const char* str2)
|
|
|
|
|
|
{
|
|
|
|
|
|
unsigned int len1=(unsigned int)strlen((char*)str1);
|
|
|
|
|
|
unsigned int len2=(unsigned int)strlen((char*)str2);
|
|
|
|
|
|
unsigned int len = (len1<len2) ? len1 : len2;
|
|
|
|
|
|
|
|
|
|
|
|
int ret = memcmp(str1,str2,len);
|
|
|
|
|
|
if(ret == 0){
|
|
|
|
|
|
if(len1 > len2) ret = 1;
|
|
|
|
|
|
else if(len1 < len2) ret = -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-02-13 17:40:07 +08:00
|
|
|
|
* 获取ucs2字符串长度
|
|
|
|
|
|
* @param ucs2 ucs2字符串
|
|
|
|
|
|
* @return 字符串长度
|
2026-02-13 14:34:15 +08:00
|
|
|
|
*/
|
|
|
|
|
|
int ucs2_len(const unsigned short* ucs2)
|
|
|
|
|
|
{
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
while (ucs2[i] != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
char c0 = ucs2[i]&0xff;
|
|
|
|
|
|
char c1 = ucs2[i]>>8&0xff;
|
|
|
|
|
|
|
|
|
|
|
|
if (c0 == '\0' && c1 == '\0')
|
|
|
|
|
|
break;
|
|
|
|
|
|
++i;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return i;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
int code_convert(const char *from_charset, const char *to_charset, const char *inbuf, size_t inlen, char *outbuf, size_t outlen)
|
|
|
|
|
|
{
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
|
|
|
|
|
iconv_t cd;
|
|
|
|
|
|
const char *temp = inbuf;
|
|
|
|
|
|
const char **pin = &temp;
|
|
|
|
|
|
char **pout = &outbuf;
|
|
|
|
|
|
memset(outbuf, 0, outlen);
|
|
|
|
|
|
cd = iconv_open(to_charset, from_charset);
|
|
|
|
|
|
if (cd == 0) return -1;
|
|
|
|
|
|
if (-1 == iconv(cd, pin, &inlen, pout, &outlen)) return -1;
|
|
|
|
|
|
iconv_close(cd);
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
|
|
|
|
|
|
memset(outbuf,0,outlen);
|
2026-02-13 17:40:07 +08:00
|
|
|
|
JNIEnv* env = cocos2d::JniHelper::getEnv();
|
|
|
|
|
|
if (!env) return -1;
|
|
|
|
|
|
|
|
|
|
|
|
jbyteArray inputBytes = env->NewByteArray(inlen);
|
|
|
|
|
|
env->SetByteArrayRegion(inputBytes, 0, inlen, (const jbyte*)inbuf);
|
|
|
|
|
|
|
|
|
|
|
|
jstring fromCharset = env->NewStringUTF(from_charset);
|
|
|
|
|
|
jstring toCharset = env->NewStringUTF(to_charset);
|
|
|
|
|
|
|
|
|
|
|
|
jclass stringClass = env->FindClass("java/lang/String");
|
|
|
|
|
|
jmethodID ctor = env->GetMethodID(stringClass, "<init>", "([BLjava/lang/String;)V");
|
|
|
|
|
|
jobject jstr = env->NewObject(stringClass, ctor, inputBytes, fromCharset);
|
|
|
|
|
|
|
|
|
|
|
|
jmethodID getBytes = env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B");
|
|
|
|
|
|
jbyteArray outputBytes = (jbyteArray)env->CallObjectMethod(jstr, getBytes, toCharset);
|
|
|
|
|
|
|
|
|
|
|
|
if (outputBytes) {
|
|
|
|
|
|
jsize outputLen = env->GetArrayLength(outputBytes);
|
|
|
|
|
|
if ((size_t)outputLen < outlen) {
|
|
|
|
|
|
env->GetByteArrayRegion(outputBytes, 0, outputLen, (jbyte*)outbuf);
|
|
|
|
|
|
}
|
|
|
|
|
|
env->DeleteLocalRef(outputBytes);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
env->DeleteLocalRef(jstr);
|
|
|
|
|
|
env->DeleteLocalRef(toCharset);
|
|
|
|
|
|
env->DeleteLocalRef(fromCharset);
|
|
|
|
|
|
env->DeleteLocalRef(inputBytes);
|
|
|
|
|
|
env->DeleteLocalRef(stringClass);
|
2026-02-13 14:34:15 +08:00
|
|
|
|
return 0;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace utility
|
|
|
|
|
|
{
|
|
|
|
|
|
/**
|
2026-02-13 17:40:07 +08:00
|
|
|
|
* 获取utf8字符串长度
|
|
|
|
|
|
* @param utf8 utf8字符串
|
|
|
|
|
|
* @return 字符串长度
|
2026-02-13 14:34:15 +08:00
|
|
|
|
*/
|
|
|
|
|
|
int utf8_len(std::string utf8)
|
|
|
|
|
|
{
|
|
|
|
|
|
return strlen(utf8.c_str());
|
|
|
|
|
|
}
|
|
|
|
|
|
int IsTextUTF8(const char* str,unsigned int length)
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
unsigned int i;
|
2026-02-13 17:40:07 +08:00
|
|
|
|
unsigned long nBytes=0;//UFT8可用1-6个字节编码,ASCII用一个字节
|
2026-02-13 14:34:15 +08:00
|
|
|
|
unsigned char chr;
|
2026-02-13 17:40:07 +08:00
|
|
|
|
int bAllAscii=1; //如果全部都是ASCII, 说明不是UTF-8
|
2026-02-13 14:34:15 +08:00
|
|
|
|
for(i=0;i<length;i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
chr= *(str+i);
|
2026-02-13 17:40:07 +08:00
|
|
|
|
if( (chr&0x80) != 0 ) // 判断是否ASCII编码,如果不是,说明有可能是UTF-8,ASCII用7位编码,用一个字节存,最高位标记为0,o0xxxxxxx
|
2026-02-13 14:34:15 +08:00
|
|
|
|
bAllAscii= 0;
|
2026-02-13 17:40:07 +08:00
|
|
|
|
if(nBytes==0) //如果不是ASCII码,应该是多字节符,计算字节数
|
2026-02-13 14:34:15 +08:00
|
|
|
|
{
|
|
|
|
|
|
if(chr>=0x80)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(chr>=0xFC&&chr<=0xFD)
|
|
|
|
|
|
nBytes=6;
|
|
|
|
|
|
else if(chr>=0xF8)
|
|
|
|
|
|
nBytes=5;
|
|
|
|
|
|
else if(chr>=0xF0)
|
|
|
|
|
|
nBytes=4;
|
|
|
|
|
|
else if(chr>=0xE0)
|
|
|
|
|
|
nBytes=3;
|
|
|
|
|
|
else if(chr>=0xC0)
|
|
|
|
|
|
nBytes=2;
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
nBytes--;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-13 17:40:07 +08:00
|
|
|
|
else //多字节符的非首字节,应为 10xxxxxx
|
2026-02-13 14:34:15 +08:00
|
|
|
|
{
|
|
|
|
|
|
if( (chr&0xC0) != 0x80 )
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
nBytes--;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-13 17:40:07 +08:00
|
|
|
|
if( nBytes > 0 ) //违反规则
|
2026-02-13 14:34:15 +08:00
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
2026-02-13 17:40:07 +08:00
|
|
|
|
if( bAllAscii ) //如果全部都是ASCII, 说明不是UTF-8
|
2026-02-13 14:34:15 +08:00
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
std::string a_u8(std::string gbk)
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t inlen = strlen(gbk.c_str());
|
|
|
|
|
|
if (inlen <= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return std::string();
|
|
|
|
|
|
}
|
|
|
|
|
|
char * outbuf = new char[inlen * 4+2];
|
|
|
|
|
|
std::string strRet = gbk;
|
|
|
|
|
|
if(code_convert("gbk", "utf-8", gbk.c_str(), inlen, outbuf, inlen * 4+2 ) == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
strRet = outbuf;
|
|
|
|
|
|
}
|
|
|
|
|
|
delete [] outbuf;
|
|
|
|
|
|
return strRet;
|
|
|
|
|
|
}
|
|
|
|
|
|
std::string u8_a(std::string utf8)
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t inlen = strlen(utf8.c_str());
|
|
|
|
|
|
if (inlen <= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return "";
|
|
|
|
|
|
}
|
|
|
|
|
|
char * outbuf = new char[inlen * 4+2];
|
|
|
|
|
|
std::string strRet = utf8;
|
|
|
|
|
|
if(code_convert("utf-8", "gbk", utf8.c_str(), inlen, outbuf, inlen*4+2) == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
strRet = outbuf;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
strRet = "Error";
|
|
|
|
|
|
}
|
|
|
|
|
|
delete [] outbuf;
|
|
|
|
|
|
return strRet;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 17:40:07 +08:00
|
|
|
|
/*gbk转为UTF16*/
|
2026-02-13 14:34:15 +08:00
|
|
|
|
std::string a2l(const char *inbuf)
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t inlen = strlen(inbuf);
|
|
|
|
|
|
std::string strRet;
|
|
|
|
|
|
strRet.resize(inlen * 2 + 2);
|
|
|
|
|
|
|
|
|
|
|
|
if (code_convert("gbk", "UTF-16LE", inbuf, inlen, &strRet[0], strRet.size()))
|
|
|
|
|
|
return inbuf;
|
|
|
|
|
|
return strRet;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-02-13 17:40:07 +08:00
|
|
|
|
/*utf16转为UTF8*/
|
2026-02-13 14:34:15 +08:00
|
|
|
|
std::string l2u(const char *inbuf)
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t inlen = ucs2_len((const unsigned short*)&inbuf[0]);
|
|
|
|
|
|
std::string strRet;
|
|
|
|
|
|
strRet.resize(inlen * 2 + 2);
|
|
|
|
|
|
|
|
|
|
|
|
if (code_convert("UTF-16LE", "utf-8", inbuf, inlen * 2, &strRet[0], strRet.size()))
|
|
|
|
|
|
return inbuf;
|
|
|
|
|
|
|
|
|
|
|
|
return strRet;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static std::string s_kDebugFileName = "";
|
|
|
|
|
|
static std::string s_kTime = "";
|
|
|
|
|
|
|
|
|
|
|
|
std::string getDebugFileName()
|
|
|
|
|
|
{
|
|
|
|
|
|
return utility::toString(cocos2d::FileUtils::getInstance()->getWritablePath(), s_kDebugFileName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void mlog(const char * format, ...)
|
|
|
|
|
|
{
|
|
|
|
|
|
va_list args;
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
|
|
|
|
|
|
|
const int iMaxLenght = 1024;
|
|
|
|
|
|
char buf[iMaxLenght] = {0};
|
|
|
|
|
|
vsnprintf(buf, iMaxLenght - 3, format, args);
|
|
|
|
|
|
strcat(buf, "\n");
|
|
|
|
|
|
strcat(buf, "\0");
|
|
|
|
|
|
|
|
|
|
|
|
std::string kStr = utility::toString(s_kTime, " ", buf);
|
|
|
|
|
|
if (s_kDebugFileName != "")
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string kPathTxt = getDebugFileName();
|
|
|
|
|
|
FILE* file = fopen(kPathTxt.c_str(), "rb+");
|
|
|
|
|
|
if (!file)
|
|
|
|
|
|
{
|
|
|
|
|
|
file = fopen(kPathTxt.c_str(), "wb+");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (file) {
|
|
|
|
|
|
fseek(file, 0L, SEEK_END);
|
|
|
|
|
|
fwrite(kStr.c_str(), sizeof(char), strlen(kStr.c_str()), file);
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
cocos2d::log("%s", buf);
|
|
|
|
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float fAbs(const float& fValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (fValue < 0.f)
|
|
|
|
|
|
{
|
|
|
|
|
|
return -fValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
return fValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Texture2D* getCircleHead(const std::string& strPath)
|
|
|
|
|
|
{
|
|
|
|
|
|
Sprite* pSprFace = Sprite::create(strPath);
|
|
|
|
|
|
CC_ASSERT(pSprFace != nullptr);
|
|
|
|
|
|
if (pSprFace == nullptr) pSprFace = Sprite::create("common/Img/head_img_male.png");
|
|
|
|
|
|
if (pSprFace == nullptr) return nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
pSprFace->setPosition(0, 0);
|
|
|
|
|
|
pSprFace->setAnchorPoint(Vec2(0, 0));
|
|
|
|
|
|
pSprFace->setFlippedY(true);
|
|
|
|
|
|
|
2026-02-13 17:40:07 +08:00
|
|
|
|
// 头像遮罩
|
2026-02-13 14:34:15 +08:00
|
|
|
|
auto textureCache = Director::getInstance()->getTextureCache();
|
|
|
|
|
|
auto texMask = textureCache->addImage("Platform/lobby/head_mask.png");
|
|
|
|
|
|
CC_ASSERT(texMask != nullptr);
|
|
|
|
|
|
|
|
|
|
|
|
Sprite* sprMask = Sprite::createWithTexture(texMask);
|
|
|
|
|
|
CC_ASSERT(sprMask != nullptr);
|
|
|
|
|
|
|
|
|
|
|
|
setSpriteScaleBySize(sprMask, pSprFace->getContentSize());
|
|
|
|
|
|
|
|
|
|
|
|
sprMask->setPosition(0, 0);
|
|
|
|
|
|
sprMask->setAnchorPoint(Vec2(0, 0));
|
|
|
|
|
|
//sprMask->setPosition(pSprFace->getContentSize().width / 2, pSprFace->getContentSize().height/2);
|
|
|
|
|
|
|
|
|
|
|
|
RenderTexture* target = RenderTexture::create(pSprFace->getContentSize().width, pSprFace->getContentSize().height);
|
|
|
|
|
|
|
|
|
|
|
|
BlendFunc maskBlend;
|
|
|
|
|
|
maskBlend.src = GL_ONE;
|
|
|
|
|
|
maskBlend.dst = GL_ZERO;
|
|
|
|
|
|
sprMask->setBlendFunc(maskBlend);
|
|
|
|
|
|
|
|
|
|
|
|
BlendFunc flowerBlend;
|
|
|
|
|
|
flowerBlend.src = GL_DST_ALPHA;
|
|
|
|
|
|
flowerBlend.dst = GL_ZERO;
|
|
|
|
|
|
pSprFace->setBlendFunc(flowerBlend);
|
|
|
|
|
|
|
|
|
|
|
|
target->begin();
|
|
|
|
|
|
sprMask->visit();
|
|
|
|
|
|
pSprFace->visit();
|
|
|
|
|
|
target->end();
|
|
|
|
|
|
|
|
|
|
|
|
return target->getSprite()->getTexture();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float getRotation(cocos2d::Vec2 kSrcPos, cocos2d::Vec2 kDestPoint)
|
|
|
|
|
|
{
|
|
|
|
|
|
cocos2d::Vec2 kDestTance = kDestPoint - kSrcPos;
|
|
|
|
|
|
float fAngle = kDestTance.getAngle();
|
|
|
|
|
|
return (-fAngle*180.0f / 3.14f);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setSpriteScaleBySize(cocos2d::Node* pSprite, cocos2d::Size kSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!pSprite)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
float fScaleX = kSize.width / (float)pSprite->getContentSize().width;
|
|
|
|
|
|
float fScaleY = kSize.height / (float)pSprite->getContentSize().height;
|
|
|
|
|
|
|
|
|
|
|
|
pSprite->setScaleX(fScaleX);
|
|
|
|
|
|
pSprite->setScaleY(fScaleY);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setImagic(cocos2d::Node* pNode, std::string kImagic, bool bSame, bool bCircle/*=false*/)
|
|
|
|
|
|
{
|
|
|
|
|
|
CCAssert(pNode, "");
|
|
|
|
|
|
cocos2d::Sprite* pImagic = dynamic_cast<cocos2d::Sprite*>(pNode);
|
|
|
|
|
|
ui::ImageView* pImageView = dynamic_cast<ui::ImageView*>(pNode);
|
|
|
|
|
|
if (pImagic)
|
|
|
|
|
|
{
|
|
|
|
|
|
CCAssert(pImagic, "");
|
|
|
|
|
|
cocos2d::Size sz = pImagic->getContentSize();
|
|
|
|
|
|
float fx = pImagic->getScaleX()*sz.width;
|
|
|
|
|
|
float fy = pImagic->getScaleY()*sz.height;
|
|
|
|
|
|
if (bCircle)
|
|
|
|
|
|
{
|
|
|
|
|
|
Texture2D* pTex = getCircleHead(kImagic);
|
|
|
|
|
|
pImagic->setTexture(pTex);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
pImagic->setTexture(kImagic);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CCAssert(pImagic->getTexture(), kImagic.c_str());
|
|
|
|
|
|
if (bSame)
|
|
|
|
|
|
{
|
|
|
|
|
|
utility::setSpriteScaleBySize(pImagic, cocos2d::Size(fx, fy));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (pImageView)
|
|
|
|
|
|
{
|
|
|
|
|
|
CCAssert(pImageView, "");
|
|
|
|
|
|
cocos2d::Size sz = pImageView->getContentSize();
|
|
|
|
|
|
float fx = pImageView->getScaleX()*sz.width;
|
|
|
|
|
|
float fy = pImageView->getScaleY()*sz.height;
|
|
|
|
|
|
pImageView->loadTexture(kImagic);
|
|
|
|
|
|
|
|
|
|
|
|
if (bSame)
|
|
|
|
|
|
{
|
|
|
|
|
|
utility::setSpriteScaleBySize(pImagic, cocos2d::Size(fx, fy));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setButtonImagic(cocos2d::Node* pNode, std::string kFile, int bAutoPress)
|
|
|
|
|
|
{
|
|
|
|
|
|
cocos2d::ui::Button *pButton = dynamic_cast<cocos2d::ui::Button*>(pNode);
|
|
|
|
|
|
cocos2d::Size sz = pButton->getContentSize();
|
|
|
|
|
|
pButton->loadTextureNormal(kFile);
|
|
|
|
|
|
if (bAutoPress)
|
|
|
|
|
|
{
|
|
|
|
|
|
cocos2d::ResourceData res = pButton->getNormalFile();
|
|
|
|
|
|
pButton->loadTexturePressed(res.file);
|
|
|
|
|
|
cocos2d::ui::Scale9Sprite* btnRender = pButton->getRendererClicked();
|
|
|
|
|
|
btnRender->setColor(cocos2d::Color3B(150, 150, 150));
|
|
|
|
|
|
}
|
|
|
|
|
|
float fx = pButton->getScaleX()*sz.width;
|
|
|
|
|
|
float fy = pButton->getScaleY()*sz.height;
|
|
|
|
|
|
utility::setSpriteScaleBySize(pButton, cocos2d::Size(fx, fy));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setButtonImagic(cocos2d::Node* pNode, std::string kName, std::string kFile, int bAutoPress)
|
|
|
|
|
|
{
|
|
|
|
|
|
setButtonImagic(pNode->getChildByName(kName), kFile, bAutoPress);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StringReplace(std::string &strBase, std::string strSrc, std::string strDes, bool bReplaceAll)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string::size_type pos = 0;
|
|
|
|
|
|
std::string::size_type srcLen = strSrc.size();
|
|
|
|
|
|
std::string::size_type desLen = strDes.size();
|
|
|
|
|
|
pos = strBase.find(strSrc, pos);
|
|
|
|
|
|
while (pos != std::string::npos)
|
|
|
|
|
|
{
|
|
|
|
|
|
strBase.replace(pos, srcLen, strDes);
|
|
|
|
|
|
pos = strBase.find(strSrc, (pos + desLen));
|
|
|
|
|
|
|
|
|
|
|
|
if (!bReplaceAll)
|
|
|
|
|
|
{
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 17:40:07 +08:00
|
|
|
|
//根据宽度截取昵号长度(参数:昵称,字体文件名,字体大小,限定宽度)
|
2026-02-13 14:34:15 +08:00
|
|
|
|
std::string getShortName(const std::string& strName, const std::string& strFont, float fFontSize, float fOverWidth)
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t nCount = strName.size();
|
|
|
|
|
|
|
|
|
|
|
|
float fTotalWidth = 0.0f;
|
|
|
|
|
|
for (int i = 0; i < nCount;)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string subStr = strName.substr(i, 1);
|
|
|
|
|
|
bool bChinese = false;
|
|
|
|
|
|
if ((subStr.c_str()[0] & 0x80) != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
subStr = strName.substr(i, 3);
|
|
|
|
|
|
bChinese = true;
|
|
|
|
|
|
i += 3;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
i++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auto tempLabel = cocos2d::Label::createWithSystemFont(subStr.c_str(), strFont, fFontSize);
|
|
|
|
|
|
float tmpWidth = tempLabel->getContentSize().width;
|
|
|
|
|
|
//tempLabel->autorelease();
|
|
|
|
|
|
fTotalWidth += tmpWidth;
|
|
|
|
|
|
|
|
|
|
|
|
if (fTotalWidth >= fOverWidth)
|
|
|
|
|
|
{
|
2026-02-13 17:40:07 +08:00
|
|
|
|
return strName.substr(0, i - (bChinese ? 3 : 1)) + a_u8("...");
|
2026-02-13 14:34:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return strName;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string getShortName(const std::string& strName, int nFontNum)
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t nCount = strName.size();
|
|
|
|
|
|
|
|
|
|
|
|
int nTempNum = 0;
|
|
|
|
|
|
float fTotalWidth = 0.0f;
|
|
|
|
|
|
for (int i = 0; i < nCount;)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string subStr = strName.substr(i, 1);
|
|
|
|
|
|
bool bChinese = false;
|
|
|
|
|
|
if ((subStr.c_str()[0] & 0x80) != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
bChinese = true;
|
|
|
|
|
|
i += 3;
|
|
|
|
|
|
nTempNum += 2;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
i++;
|
|
|
|
|
|
nTempNum++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (nTempNum > nFontNum)
|
|
|
|
|
|
{
|
2026-02-13 17:40:07 +08:00
|
|
|
|
return strName.substr(0, i - (bChinese ? 3 : 1)) + a_u8("...");
|
2026-02-13 14:34:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return strName;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string ScoreToString(SCORE lScore)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (lScore > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return StringUtils::format("+%d", lScore);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return StringUtils::format("%d", lScore);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string GetUserLevel(SCORE lGrade)
|
|
|
|
|
|
{
|
|
|
|
|
|
static std::string Levels[] = {
|
2026-02-13 17:40:07 +08:00
|
|
|
|
"等级:雀民",
|
|
|
|
|
|
"等级:雀士",
|
|
|
|
|
|
"等级:雀头",
|
|
|
|
|
|
"等级:铜雀",
|
|
|
|
|
|
"等级:银雀",
|
|
|
|
|
|
"等级:金雀",
|
|
|
|
|
|
"等级:雀侠",
|
|
|
|
|
|
"等级:雀杰",
|
|
|
|
|
|
"等级:雀王",
|
|
|
|
|
|
"等级:雀圣",
|
|
|
|
|
|
"等级:雀神"
|
2026-02-13 14:34:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static SCORE lLevels[] = { 200, 500, 1000, 2000, 4000, 6000, 10000, 15000, 20000, 30000 };
|
|
|
|
|
|
|
|
|
|
|
|
int __ind = 0;
|
|
|
|
|
|
for (__ind = 0; __ind < countarray(lLevels); __ind++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (lGrade < lLevels[__ind])
|
|
|
|
|
|
{
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (__ind > countarray(Levels))
|
|
|
|
|
|
{
|
|
|
|
|
|
__ind = countarray(Levels) - 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Levels[__ind];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string getIdentifyingCode(uint32 dwGameID)
|
|
|
|
|
|
{
|
|
|
|
|
|
time_t t = time(NULL);
|
|
|
|
|
|
struct tm* tt = localtime(&t);
|
|
|
|
|
|
char timeString[32] = { 0 };
|
|
|
|
|
|
sprintf(timeString, "wn%04d%02dmj%d", tt->tm_year + 1900, tt->tm_mon + 1, dwGameID);
|
|
|
|
|
|
|
|
|
|
|
|
return Helps::MD5Encrypt(timeString);
|
|
|
|
|
|
}
|
2026-02-13 17:40:07 +08:00
|
|
|
|
}
|