Cocos2d-x change Text wrap style (cocos2dx change line ),

Source: Internet
Author: User

Cocos2d-x change Text wrap style (cocos2dx change line ),
Cocos2dx change line

In the implementation of cocos2dx change line, we can simply usedimensionsAttribute control line feed. You only need to pass the corresponding parameter values to the constructor or call the setDimensions function.

Its line feed policy is: when a word exceeds the limit length, move it to the next line. Sometimes this policy is not suitable. For example, in some languages, words are long. If this policy is used, only one word exists in each row. Therefore, you need to change the line feed policy. When the length exceeds the limit, use-As a sign to separate words.


Line feed policy call

By looking at the implementation of line feed in the Cocos2d-x, I found that line feed policies distinguish the code programmed by the platform. That is to say, to change the line feed policy, I need to write IOS and Java code separately. Because my target platform is Android, I only made changes to the Java file. The line feed policy of IOS platform calls system functions. Readers interested in the research can track source code changes. The entry function is:

bool CCTexture2D::initWithString(const char *text, ccFontDefinition *textDefinition){    ......     CCImage* pImage = new CCImage();    do    {        CC_BREAK_IF(NULL == pImage);                bRet = pImage->initWithStringShadowStroke(text,                                                  (int)textDefinition->m_dimensions.width,                                                  (int)textDefinition->m_dimensions.height,                                                  eAlign,                                                  textDefinition->m_fontName.c_str(),                                                  textDefinition->m_fontSize,                                                  textDefinition->m_fontFillColor.r / 255,                                                  textDefinition->m_fontFillColor.g / 255,                                                  textDefinition->m_fontFillColor.b / 255,                                                  shadowEnabled,                                                  shadowDX,                                                  shadowDY,                                                  shadowOpacity,                                                  shadowBlur,                                                  strokeEnabled,                                                  strokeColorR,                                                  strokeColorG,                                                  strokeColorB,                                                  strokeSize);                        CC_BREAK_IF(!bRet);        bRet = initWithImage(pImage);            } while (0);        ......}

Change Java Implementation

The Java file corresponding to the Android platform is located in the $ (2DX-Root)/cocos2dx/platform/android/java/src/org/cocos2dx/lib folder, and the file name is Cocos2dxBitmap. java

First, I added the divideStringWithMaxWidthByFlag function, which serves as an implementation function of another line feed policy.

// Add by fansy for "-" style words private static variable list <String> divideStringWithMaxWidthByFlag (final String pString, final int pMaxWidth, final Paint pPaint) {final int charLength = pString. length (); int start = 0; int tempWidth = 0; shortlist <String> strList = new shortlist <String> (); if (! IsChinese (pString) {/* Break a String into String [] by the width & shoshould wrap the word. */for (int I = 1; I <charLength-1; ++ I) {tempWidth = (int) FloatMath. ceil (pPaint. measureText (pString, start, I + 1); if (tempWidth> = pMaxWidth) {if (pString. charAt (I) = '') // end with" "{// change line at I strList. add (pString. substring (start, I); I = I + 1; // skip space} else if (I> 1 & pString. charAt (I-2) = '') // only one"-"left after change line {// change line at I-2 strList. add (pString. substring (start, I-2); I = I-2; // skip space} else if (I> 0 & pString. charAt (I-1) = '') // only one"-"left after change line {// change line at I-1 strList. add (pString. substring (start, I-1); I = I-1; // skip space} else if (I> 0) // replace "-" at I-2 {// split at I-1 add "-" at tail change line at I-1 strList. add (pString. substring (start, I-1) + "-"); I --;}/* Remove spaces at the beginning of a new line. */while (pString. charAt (I) = '') {++ I;} start = I ;}}/* Add the last chars. */if (start <charLength) {strList. add (pString. substring (start) ;}} else {strList = divideStringWithMaxWidth (pString, pMaxWidth, pPaint);} return strList ;} // judge Chinese characters and symbols private static boolean isChinese (char c) {Character based on Unicode encoding. unicodeBlock ub = Character. unicodeBlock. of (c); if (ub = Character. unicodeBlock. cjk_uniied_ideographs | ub = Character. unicodeBlock. CJK_COMPATIBILITY_IDEOGRAPHS | ub = Character. unicodeBlock. CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A | ub = Character. unicodeBlock. CJK_UNIFIED_IDEOGRAPHS_EXTENSION_ B | ub = Character. unicodeBlock. CJK_SYMBOLS_AND_PUNCTUATION | ub = Character. unicodeBlock. HALFWIDTH_AND_FULLWIDTH_FORMS | ub = Character. unicodeBlock. GENERAL_PUNCTUATION) {return true;} return false;} // complete judgment of Chinese characters and symbols public static boolean isChinese (String strName) {char [] ch = strName. toCharArray (); for (int I = 0; I <ch. length; I ++) {char c = ch [I]; if (isChinese (c) {return true ;}} return false ;}// end add by fansy

After adding a function, modify the call in splitString:

private static String[] splitString(final String pString,            final int pMaxWidth, final int pMaxHeight, final Paint pPaint) {        final String[] lines = pString.split("\\n");        String[] ret = null;        final FontMetricsInt fm = pPaint.getFontMetricsInt();        final int heightPerLine = (int) Math.ceil(fm.bottom - fm.top);        final int maxLines = pMaxHeight / heightPerLine;         if (pMaxWidth != 0) {            final LinkedList<String> strList = new LinkedList<String>();            for (final String line : lines) {                /*                 * The width of line is exceed maxWidth, should divide it into                 * two or more lines.                 */                final int lineWidth = (int) FloatMath.ceil(pPaint                        .measureText(line));                if (lineWidth > pMaxWidth) {                    strList.addAll(Cocos2dxBitmap.divideStringWithMaxWidthByFlag(                            line, pMaxWidth, pPaint));                } else {                    strList.add(line);        ......}

After the function is changed, compile and package it, and run the program to see different line breaks.


This article from: http://blog.csdn.net/fansongy/[Songyang blog] prohibited for commercial purposes reprint please indicate the source

Link: http://www.songyang.net/cocos2dx-change-line/






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.