protected void formarRenderers() { int num = 0; int num2 = 1; int num3 = 27; int count = this._elemRenderArr.Count; for (int i = 0; i < count; i++) { bool flag = false; LRenderElement lRenderElement = this._elemRenderArr[i]; bool isNewLine = lRenderElement.isNewLine; if (isNewLine) { num = 0; num2++; lRenderElement.width = 10; lRenderElement.height = num3; lRenderElement.pos = new Vector2((float)num, (float)(-(float)num2 * num3)); } else { bool flag2 = num + lRenderElement.width > this.maxLineWidth; if (flag2) { bool flag3 = lRenderElement.type == RichType.TEXT; if (flag3) { bool flag4 = this.isChinese(lRenderElement.strChar) || lRenderElement.strChar == " "; if (flag4) { num = 0; num2++; lRenderElement.pos = new Vector2((float)num, (float)(-(float)num2 * num3)); num = lRenderElement.width; } else { int num4 = 0; int j = i; while (j > 0) { j--; bool flag5 = this._elemRenderArr[j].strChar == " " && this._elemRenderArr[j].pos.y == this._elemRenderArr[i - 1].pos.y; if (flag5) { num4 = j; break; } } bool flag6 = num4 == 0; if (flag6) { num = 0; num2++; lRenderElement.pos = new Vector2((float)num, (float)(-(float)num2 * num3)); num = lRenderElement.width; } else { num = 0; num2++; flag = true; for (int k = num4 + 1; k <= i; k++) { LRenderElement lRenderElement2 = this._elemRenderArr[k]; lRenderElement2.pos = new Vector2((float)num, (float)(-(float)num2 * num3)); num += lRenderElement2.width; this._elemRenderArr[k] = lRenderElement2; } } } } else { bool flag7 = lRenderElement.type == RichType.ANIM || lRenderElement.type == RichType.IMAGE; if (flag7) { num2++; lRenderElement.pos = new Vector2(0f, (float)(-(float)num2 * num3)); num = lRenderElement.width; } } } else { lRenderElement.pos = new Vector2((float)num, (float)(-(float)num2 * num3)); num += lRenderElement.width + 1 + this.countNumber(lRenderElement.strChar); } } bool flag8 = !flag; if (flag8) { this._elemRenderArr[i] = lRenderElement; } } Dictionary <int, List <LRenderElement> > dictionary = new Dictionary <int, List <LRenderElement> >(); List <int> list = new List <int>(); count = this._elemRenderArr.Count; for (int l = 0; l < count; l++) { LRenderElement lRenderElement3 = this._elemRenderArr[l]; bool flag9 = !dictionary.ContainsKey((int)lRenderElement3.pos.y); if (flag9) { List <LRenderElement> value = new List <LRenderElement>(); dictionary[(int)lRenderElement3.pos.y] = value; } dictionary[(int)lRenderElement3.pos.y].Add(lRenderElement3); } List <List <LRenderElement> > list2 = new List <List <LRenderElement> >(); foreach (KeyValuePair <int, List <LRenderElement> > current in dictionary) { list.Add(-1 * current.Key); } list.Sort(); count = list.Count; for (int m = 0; m < count; m++) { int key = -1 * list[m]; string text = ""; LRenderElement lRenderElement4 = dictionary[key][0]; LRenderElement lRenderElement5 = dictionary[key][0]; bool flag10 = dictionary[key].Count > 0; if (flag10) { List <LRenderElement> list3 = new List <LRenderElement>(); foreach (LRenderElement current2 in dictionary[key]) { bool flag11 = lRenderElement4.type == RichType.TEXT && current2.type == RichType.TEXT; if (flag11) { bool flag12 = lRenderElement4.color == current2.color && (lRenderElement4.data == current2.data || current2.data == ""); if (flag12) { text += current2.strChar; } else { bool flag13 = lRenderElement5.type == RichType.TEXT; if (flag13) { LRenderElement item = lRenderElement5.Clone(); item.strChar = text; list3.Add(item); lRenderElement5 = current2; text = current2.strChar; } } } else { bool flag14 = current2.type == RichType.IMAGE || current2.type == RichType.ANIM || current2.type == RichType.NEWLINE; if (flag14) { bool flag15 = lRenderElement5.type == RichType.TEXT; if (flag15) { LRenderElement item2 = lRenderElement5.Clone(); item2.strChar = text; text = ""; list3.Add(item2); } list3.Add(current2); } else { bool flag16 = lRenderElement4.type > RichType.TEXT; if (flag16) { lRenderElement5 = current2; bool flag17 = current2.type == RichType.TEXT; if (flag17) { text = current2.strChar; } } } } lRenderElement4 = current2; } bool flag18 = lRenderElement5.type == RichType.TEXT; if (flag18) { LRenderElement item3 = lRenderElement5.Clone(); item3.strChar = text; list3.Add(item3); } list2.Add(list3); } } int num5 = 0; int num6 = this.font.fontSize; this.realLineHeight = 0; count = list2.Count; for (int n = 0; n < count; n++) { List <LRenderElement> list4 = list2[n]; int num7 = 0; foreach (LRenderElement current3 in list4) { num7 = Mathf.Max(num7, current3.height); } num6 = num7; this.realLineHeight += num7; num5 += num7 - num3; int count2 = list4.Count; for (int num8 = 0; num8 < count2; num8++) { LRenderElement lRenderElement6 = list4[num8]; lRenderElement6.pos = new Vector2(lRenderElement6.pos.x, lRenderElement6.pos.y - (float)num5); this.realLineHeight = Mathf.Max(this.realLineHeight, (int)Mathf.Abs(lRenderElement6.pos.y)); list4[num8] = lRenderElement6; } list2[n] = list4; } this.realLineWidth = 0; GameObject gameObject = null; foreach (List <LRenderElement> current4 in list2) { int num9 = 0; foreach (LRenderElement current5 in current4) { bool flag19 = current5.type != RichType.NEWLINE; if (flag19) { bool flag20 = current5.type == RichType.TEXT; if (flag20) { gameObject = this.getCacheLabel(); this.makeLabel(gameObject, current5); num9 += (int)gameObject.GetComponent <Text>().preferredWidth; } else { bool flag21 = current5.type == RichType.IMAGE; if (flag21) { gameObject = this.getCacheImage(true); this.makeImage(gameObject, current5); num9 += (int)gameObject.GetComponent <Image>().preferredWidth; } else { bool flag22 = current5.type == RichType.ANIM; if (flag22) { gameObject = this.getCacheFramAnim(); this.makeFramAnim(gameObject, current5); num9 += current5.width; } } } gameObject.transform.SetParent(base.transform); gameObject.transform.localPosition = new Vector2(current5.pos.x, (Mathf.Abs(current5.pos.y) != (float)num6) ? (current5.pos.y + (float)this.verticalOffset) : current5.pos.y); this._objectDataMap[gameObject] = current5.data; } } this.realLineWidth = Mathf.Max(num9, this.realLineWidth); } RectTransform component = base.GetComponent <RectTransform>(); bool flag23 = this.alignType == RichAlignType.DESIGN_CENTER; if (flag23) { component.sizeDelta = new Vector2((float)this.maxLineWidth, (float)this.realLineHeight); } else { bool flag24 = this.alignType == RichAlignType.LEFT_TOP; if (flag24) { component.sizeDelta = new Vector2((float)this.realLineWidth, (float)this.realLineHeight); } } }
protected void formatRenderers() { int oneLine = 0; int lines = 1; bool isReplaceInSpace = false; int len = _elemRenderArr.Count; for (int i = 0; i < len; i++) { isReplaceInSpace = false; LRenderElement elem = _elemRenderArr[i]; if (elem.isNewLine) // new line { oneLine = 0; lines++; elem.width = 10; elem.height = 27; elem.pos = new Vector2(oneLine, -lines * 27); } else //other elements { if (oneLine + elem.width > maxLineWidth) { if (elem.type == RichType.TEXT) { if (isChinese(elem.strChar) || elem.strChar == " ") { oneLine = 0; lines++; elem.pos = new Vector2(oneLine, -lines * 27); oneLine = elem.width; } else // en { int spaceIdx = 0; int idx = i; while (idx > 0) { idx--; if (_elemRenderArr[idx].strChar == " " && _elemRenderArr[idx].pos.y == _elemRenderArr[i - 1].pos.y) // just for the same line { spaceIdx = idx; break; } } // can't find space , force new line if (spaceIdx == 0) { oneLine = 0; lines++; elem.pos = new Vector2(oneLine, -lines * 27); oneLine = elem.width; } else { oneLine = 0; lines++; isReplaceInSpace = true; //reset cuting words position for (int _i = spaceIdx + 1; _i <= i; ++_i) { LRenderElement _elem = _elemRenderArr[_i]; _elem.pos = new Vector2(oneLine, -lines * 27); oneLine += _elem.width; _elemRenderArr[_i] = _elem; } } } } else if (elem.type == RichType.ANIM || elem.type == RichType.IMAGE) { lines++; elem.pos = new Vector2(0, -lines * 27); oneLine = elem.width; } } else { elem.pos = new Vector2(oneLine, -lines * 27); oneLine += elem.width; } } if (isReplaceInSpace == false) { _elemRenderArr[i] = elem; } } //sort all lines Dictionary <int, List <LRenderElement> > rendElemLineMap = new Dictionary <int, List <LRenderElement> >(); List <int> lineKeyList = new List <int>(); len = _elemRenderArr.Count; for (int i = 0; i < len; i++) { LRenderElement elem = _elemRenderArr[i]; List <LRenderElement> lineList; if (!rendElemLineMap.ContainsKey((int)elem.pos.y)) { lineList = new List <LRenderElement>(); rendElemLineMap[(int)elem.pos.y] = lineList; } rendElemLineMap[(int)elem.pos.y].Add(elem); } // all lines in arr List <List <LRenderElement> > rendLineArrs = new List <List <LRenderElement> >(); foreach (var item in rendElemLineMap) { lineKeyList.Add(-1 * item.Key); } lineKeyList.Sort(); len = lineKeyList.Count; for (int i = 0; i < len; i++) { int posY = -1 * lineKeyList[i]; string lineString = ""; LRenderElement _lastEleme = rendElemLineMap[posY][0]; LRenderElement _lastDiffStartEleme = rendElemLineMap[posY][0]; if (rendElemLineMap[posY].Count > 0) { List <LRenderElement> lineElemArr = new List <LRenderElement>(); foreach (LRenderElement elem in rendElemLineMap[posY]) { if (_lastEleme.type == RichType.TEXT && elem.type == RichType.TEXT) { if (_lastEleme.isSameStyle(elem)) { // the same style can mergin one element lineString += elem.strChar; } else // diff style { if (_lastDiffStartEleme.type == RichType.TEXT) { LRenderElement _newElem = _lastDiffStartEleme.Clone(); _newElem.strChar = lineString; lineElemArr.Add(_newElem); _lastDiffStartEleme = elem; lineString = elem.strChar; } } } else if (elem.type == RichType.IMAGE || elem.type == RichType.ANIM || elem.type == RichType.NEWLINE) { //interrupt if (_lastDiffStartEleme.type == RichType.TEXT) { LRenderElement _newEleme = _lastDiffStartEleme.Clone(); _newEleme.strChar = lineString; lineString = ""; lineElemArr.Add(_newEleme); } lineElemArr.Add(elem); } else if (_lastEleme.type != RichType.TEXT) { //interrupt _lastDiffStartEleme = elem; if (elem.type == RichType.TEXT) { lineString = elem.strChar; } } _lastEleme = elem; } // the last elementText if (_lastDiffStartEleme.type == RichType.TEXT) { LRenderElement _newElem = _lastDiffStartEleme.Clone(); _newElem.strChar = lineString; lineElemArr.Add(_newElem); } rendLineArrs.Add(lineElemArr); } } // offset position int _offsetLineY = 0; realLineHeight = 0; len = rendLineArrs.Count; for (int i = 0; i < len; i++) { List <LRenderElement> _lines = rendLineArrs[i]; int _lineHeight = 0; foreach (LRenderElement elem in _lines) { _lineHeight = Mathf.Max(_lineHeight, elem.height); } realLineHeight += _lineHeight; _offsetLineY += (_lineHeight - 27); int _len = _lines.Count; for (int j = 0; j < _len; j++) { LRenderElement elem = _lines[j]; elem.pos = new Vector2(elem.pos.x, elem.pos.y - _offsetLineY); realLineHeight = Mathf.Max(realLineHeight, (int)Mathf.Abs(elem.pos.y)); _lines[j] = elem; } rendLineArrs[i] = _lines; } // place all position realLineWidth = 0; GameObject obj = null; foreach (List <LRenderElement> _lines in rendLineArrs) { int _lineWidth = 0; foreach (LRenderElement elem in _lines) { if (elem.type != RichType.NEWLINE) { if (elem.type == RichType.TEXT) { obj = getCacheLabel(); makeLabel(obj, elem); _lineWidth += (int)obj.GetComponent <Text>().preferredWidth; } else if (elem.type == RichType.IMAGE) { obj = getCacheImage(true); makeImage(obj, elem); _lineWidth += (int)obj.GetComponent <Image>().preferredWidth; } else if (elem.type == RichType.ANIM) { obj = getCacheFramAnim(); makeFramAnim(obj, elem); _lineWidth += elem.width; } obj.SetActive(true); obj.transform.SetParent(transform); obj.transform.localPosition = new Vector2(elem.pos.x, elem.pos.y /*+ realLineHeight*/); obj.transform.localScale = new Vector2(1, 1); _objectDataMap[obj] = elem.data; } } realLineWidth = Mathf.Max(_lineWidth, realLineWidth); } RectTransform rtran = this.GetComponent <RectTransform>(); //align if (alignType == RichAlignType.DESIGN_CENTER) { rtran.sizeDelta = new Vector2(maxLineWidth, realLineHeight); } else if (alignType == RichAlignType.LEFT_TOP) { rtran.sizeDelta = new Vector2(realLineWidth, realLineHeight); } }
protected void formarRenderers(float newElemOffset) { int oneLine = 0; int lines = 1; int height = 27; bool isReplaceInSpace = false; int len = _elemRenderArr.Count; for (int i = 0; i < len; i++) { isReplaceInSpace = false; LRenderElement elem = _elemRenderArr[i]; if (elem.isNewLine) // new line { oneLine = 0; lines++; elem.width = 10; elem.height = height; elem.pos = new Vector2(oneLine, -lines * height); } else //other elements { if (oneLine + elem.width > maxLineWidth) { if (elem.type == RichType.TEXT) { if (isChinese(elem.strChar) || elem.strChar == " ") { oneLine = 0; lines++; elem.pos = new Vector2(oneLine, -lines * height); oneLine = elem.width; } else // en { int spaceIdx = 0; int idx = i; while (idx > 0) { idx--; if (_elemRenderArr[idx].strChar == " " && _elemRenderArr[idx].pos.y == _elemRenderArr[i - 1].pos.y) // just for the same line// { spaceIdx = idx; break; } } // can't find space , force new line if (spaceIdx == 0) { oneLine = 0; lines++; elem.pos = new Vector2(oneLine, -lines * height); oneLine = elem.width; } else { oneLine = 0; lines++; isReplaceInSpace = true; //reset cuting words position for (int _i = spaceIdx + 1; _i <= i; ++_i) { LRenderElement _elem = _elemRenderArr[_i]; _elem.pos = new Vector2(oneLine, -lines * height); oneLine += _elem.width; _elemRenderArr[_i] = _elem; } } } } else if (elem.type == RichType.ANIM || elem.type == RichType.IMAGE) { lines++; elem.pos = new Vector2(0, -lines * height); oneLine = elem.width; } } else { elem.pos = new Vector2(oneLine, -lines * height); oneLine += (elem.width + 1 + countNumber(elem.strChar));//元素间距 } } if (isReplaceInSpace == false) { _elemRenderArr[i] = elem; } } //sort all lines Dictionary <int, List <LRenderElement> > rendElemLineMap = new Dictionary <int, List <LRenderElement> >(); List <int> lineKeyList = new List <int>(); len = _elemRenderArr.Count; for (int i = 0; i < len; i++) { LRenderElement elem = _elemRenderArr[i]; List <LRenderElement> lineList; if (!rendElemLineMap.ContainsKey((int)elem.pos.y)) { lineList = new List <LRenderElement>(); rendElemLineMap[(int)elem.pos.y] = lineList; } rendElemLineMap[(int)elem.pos.y].Add(elem); } // all lines in arr List <List <LRenderElement> > rendLineArrs = new List <List <LRenderElement> >(); foreach (var item in rendElemLineMap) { lineKeyList.Add(-1 * item.Key); } lineKeyList.Sort(); len = lineKeyList.Count; for (int i = 0; i < len; i++) { int posY = -1 * lineKeyList[i]; string lineString = ""; LRenderElement _lastEleme = rendElemLineMap[posY][0]; LRenderElement _lastDiffStartEleme = rendElemLineMap[posY][0]; Vector2 counter = Vector2.zero; if (rendElemLineMap[posY].Count > 0) { List <LRenderElement> lineElemArr = new List <LRenderElement>(); foreach (LRenderElement elem in rendElemLineMap[posY]) { if (_lastEleme.type == RichType.TEXT && elem.type == RichType.TEXT) { if (_lastEleme.color == elem.color && (_lastEleme.data == elem.data || elem.data == ""))//fixed:相同颜色,不同data触发结果相同的BUG { // the same color and same data can mergin one string lineString += elem.strChar; } else // diff color { if (_lastDiffStartEleme.type == RichType.TEXT) { LRenderElement _newElem = _lastDiffStartEleme.Clone(); _newElem.strChar = lineString; //_newElem.pos += new Vector2(-5, 0); lineElemArr.Add(_newElem); _lastDiffStartEleme = elem; counter = counter + new Vector2(newElemOffset, 0); _lastDiffStartEleme.pos = _lastDiffStartEleme.pos + counter; lineString = elem.strChar; } } } else if (elem.type == RichType.IMAGE || elem.type == RichType.ANIM || elem.type == RichType.NEWLINE) { //interrupt if (_lastDiffStartEleme.type == RichType.TEXT) { LRenderElement _newEleme = _lastDiffStartEleme.Clone(); _newEleme.strChar = lineString; lineString = ""; lineElemArr.Add(_newEleme); } lineElemArr.Add(elem); } else if (_lastEleme.type != RichType.TEXT) { //interrupt _lastDiffStartEleme = elem; if (elem.type == RichType.TEXT) { lineString = elem.strChar; } } _lastEleme = elem; } // the last elementText if (_lastDiffStartEleme.type == RichType.TEXT) { LRenderElement _newElem = _lastDiffStartEleme.Clone(); _newElem.strChar = lineString; lineElemArr.Add(_newElem); } rendLineArrs.Add(lineElemArr); } } // offset position int _offsetLineY = 0; int oneLineHeight = font.fontSize; realLineHeight = 0; len = rendLineArrs.Count; for (int i = 0; i < len; i++) { List <LRenderElement> _lines = rendLineArrs[i]; int _lineHeight = 0; foreach (LRenderElement elem in _lines) { _lineHeight = Mathf.Max(_lineHeight, elem.height); } oneLineHeight = _lineHeight; realLineHeight += _lineHeight; _offsetLineY += (_lineHeight - height); int _len = _lines.Count; for (int j = 0; j < _len; j++) { LRenderElement elem = _lines[j]; elem.pos = new Vector2(elem.pos.x, elem.pos.y - _offsetLineY); realLineHeight = Mathf.Max(realLineHeight, (int)Mathf.Abs(elem.pos.y)); _lines[j] = elem; } rendLineArrs[i] = _lines; } // place all position realLineWidth = 0; GameObject obj = null; GameObject _lastGo = null; float _lastGoWidth = 0; foreach (List <LRenderElement> _lines in rendLineArrs) { int _lineWidth = 0; _lastGo = null; foreach (LRenderElement elem in _lines) { if (elem.type != RichType.NEWLINE) { if (elem.type == RichType.TEXT) { obj = getCacheLabel(); makeLabel(obj, elem); _lineWidth += ( int )obj.GetComponent <Text>().preferredWidth; } else if (elem.type == RichType.IMAGE) { obj = getCacheImage(true); makeImage(obj, elem); _lineWidth += ( int )obj.GetComponent <Image>().preferredWidth; } else if (elem.type == RichType.ANIM) { obj = getCacheFramAnim(); makeFramAnim(obj, elem); _lineWidth += elem.width; } obj.transform.SetParent(transform); float newPosY = elem.pos.y; if (Mathf.Abs(elem.pos.y) != oneLineHeight) { uVerticalOffset = verticalOffset + uVerticalOffset; newPosY = newPosY + uVerticalOffset; } obj.transform.localPosition = new Vector2( x: _lastGo == null ? elem.pos.x : _lastGo.transform.localPosition.x + _lastGoWidth, //y: newPosY y: _lastGo == null ? newPosY : _lastGo.transform.localPosition.y ); _objectDataMap[obj] = elem.data; _lastGo = obj; if (elem.type == RichType.ANIM) { _lastGoWidth = elem.width; continue; } _lastGoWidth = elem.type == RichType.TEXT ? obj.GetComponent <Text>().preferredWidth : obj.GetComponent <Image>().preferredWidth; } } realLineWidth = Mathf.Max(_lineWidth, realLineWidth); } RectTransform rtran = this.GetComponent <RectTransform>(); //align if (alignType == RichAlignType.DESIGN_CENTER) { rtran.sizeDelta = new Vector2(maxLineWidth, realLineHeight); } else if (alignType == RichAlignType.LEFT_TOP) { rtran.sizeDelta = new Vector2(realLineWidth, realLineHeight); } }