221 lines
5.3 KiB
C++
221 lines
5.3 KiB
C++
#ifndef _Utility2_H_
|
|
#define _Utility2_H_
|
|
|
|
#include "cocos2d.h"
|
|
|
|
#include <vector>
|
|
#include <string>
|
|
#include "types.h"
|
|
|
|
USING_NS_CC;
|
|
|
|
namespace utility
|
|
{
|
|
|
|
#define a_u8c(str) utility::a_u8(str).c_str()
|
|
|
|
int utf8_len(std::string utf8);
|
|
//gbk转utf8
|
|
std::string a_u8(std::string gbk);
|
|
//utf8转gbk
|
|
std::string u8_a(std::string utf8);
|
|
|
|
//gbk转为UTF16
|
|
std::string a2l(const char *inbuf);
|
|
//utf16转为UTF8
|
|
std::string l2u(const char *inbuf);
|
|
|
|
void mlog(const char * format, ...);
|
|
|
|
float fAbs(const float& fValue);
|
|
|
|
template<typename T>
|
|
inline T Abs(const T& iValue)
|
|
{
|
|
return iValue > 0 ? iValue : -iValue;
|
|
}
|
|
|
|
Texture2D* getCircleHead(const std::string& strPath);
|
|
|
|
float getRotation(cocos2d::Vec2 kSrcPos, cocos2d::Vec2 kDestPoint);
|
|
|
|
void setImagic(cocos2d::Node* pNode, std::string kImagic, bool bSame, bool bCircle = false);
|
|
|
|
void setButtonImagic(cocos2d::Node* pNode, std::string kFile, int bAutoPress);
|
|
|
|
void setButtonImagic(cocos2d::Node* pNode, std::string kName, std::string kFile, int bAutoPress);
|
|
|
|
void setSpriteScaleBySize(cocos2d::Node* pSprite, cocos2d::Size kSize);
|
|
|
|
void StringReplace(std::string &strBase, std::string strSrc, std::string strDes, bool bReplaceAll = false);
|
|
|
|
template<typename T>
|
|
inline std::string toString(T p)
|
|
{
|
|
std::ostringstream stream;
|
|
stream << p;
|
|
return stream.str();
|
|
}
|
|
|
|
template<typename T>
|
|
bool haveInVector(const std::vector<T>& pVector, T pValue)
|
|
{
|
|
typename std::vector<T>::const_iterator itor = pVector.begin();
|
|
for (; itor != pVector.end(); itor++)
|
|
{
|
|
if (*itor == pValue)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
template<class T>
|
|
inline bool removeInVector(std::vector<T>& a, const T& b)
|
|
{
|
|
typename std::vector<T>::iterator itor = a.begin();
|
|
for (; itor != a.end(); itor++)
|
|
{
|
|
if ((*itor) == b)
|
|
{
|
|
a.erase(itor);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
inline const std::string& toString(const std::string& _value)
|
|
{
|
|
return _value;
|
|
}
|
|
|
|
template<typename T1, typename T2>
|
|
inline std::string toString(T1 p1, T2 p2)
|
|
{
|
|
std::ostringstream stream;
|
|
stream << p1 << p2;
|
|
return stream.str();
|
|
}
|
|
|
|
template<typename T1, typename T2, typename T3>
|
|
inline std::string toString(T1 p1, T2 p2, T3 p3)
|
|
{
|
|
std::ostringstream stream;
|
|
stream << p1 << p2 << p3;
|
|
return stream.str();
|
|
}
|
|
|
|
template<typename T1, typename T2, typename T3, typename T4>
|
|
inline std::string toString(T1 p1, T2 p2, T3 p3, T4 p4)
|
|
{
|
|
std::ostringstream stream;
|
|
stream << p1 << p2 << p3 << p4;
|
|
return stream.str();
|
|
}
|
|
|
|
template<typename T1, typename T2, typename T3, typename T4, typename T5>
|
|
inline std::string toString(T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
|
|
{
|
|
std::ostringstream stream;
|
|
stream << p1 << p2 << p3 << p4 << p5;
|
|
return stream.str();
|
|
}
|
|
|
|
template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
|
|
inline std::string toString(T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6)
|
|
{
|
|
std::ostringstream stream;
|
|
stream << p1 << p2 << p3 << p4 << p5 << p6;
|
|
return stream.str();
|
|
}
|
|
|
|
template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
|
|
inline std::string toString(T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6, T7 p7)
|
|
{
|
|
std::ostringstream stream;
|
|
stream << p1 << p2 << p3 << p4 << p5 << p6 << p7;
|
|
return stream.str();
|
|
}
|
|
|
|
template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
|
|
inline std::string toString(T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6, T7 p7, T8 p8)
|
|
{
|
|
std::ostringstream stream;
|
|
stream << p1 << p2 << p3 << p4 << p5 << p6 << p7 << p8;
|
|
return stream.str();
|
|
}
|
|
|
|
template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
|
|
inline std::string toString(T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6, T7 p7, T8 p8, T9 p9)
|
|
{
|
|
std::ostringstream stream;
|
|
stream << p1 << p2 << p3 << p4 << p5 << p6 << p7 << p8 << p9;
|
|
return stream.str();
|
|
}
|
|
|
|
template<>
|
|
inline std::string toString<bool>(bool _value)
|
|
{
|
|
return _value ? "true" : "false";
|
|
}
|
|
|
|
|
|
inline std::string toString(cocos2d::Vec2 p)
|
|
{
|
|
return toString(p.x, " ", p.y);
|
|
}
|
|
inline std::string toString(cocos2d::Size p)
|
|
{
|
|
return toString(p.width, " ", p.height);
|
|
}
|
|
|
|
template<typename T>
|
|
inline void split(std::vector<std::string>& _ret, const std::string& _source, const std::string& _delims)
|
|
{
|
|
if (_source == "")
|
|
{
|
|
return;
|
|
}
|
|
size_t start = 0;
|
|
size_t end = _source.find(_delims);
|
|
while (start != _source.npos)
|
|
{
|
|
if (end != _source.npos)
|
|
_ret.push_back(_source.substr(start, end - start));
|
|
else
|
|
{
|
|
_ret.push_back(_source.substr(start));
|
|
break;
|
|
}
|
|
start = end;
|
|
if (start != _source.npos)
|
|
{
|
|
start += _delims.size();
|
|
}
|
|
end = _source.find(_delims, start);
|
|
}
|
|
}
|
|
|
|
inline std::vector<std::string> split(const std::string& _source, const std::string& _delims = "\t\n ")
|
|
{
|
|
std::vector<std::string> result;
|
|
utility::split<void>(result, _source, _delims);
|
|
return result;
|
|
}
|
|
|
|
//超出宽度用星号代替(参数:昵称,字体文件,字体大小,超出宽度)
|
|
std::string getShortName(const std::string& strName, const std::string& strFont, float fFontSize, float fOverWidth);
|
|
|
|
std::string getShortName(const std::string& strName, int nFontNum);
|
|
|
|
// 积分转字符串;
|
|
std::string ScoreToString(SCORE lScore);
|
|
|
|
std::string GetUserLevel(SCORE lGrade);
|
|
|
|
std::string getIdentifyingCode(uint32 dwGameID);
|
|
}
|
|
|
|
#endif // _Utility_H_
|