| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- namespace WXB
- {
- public partial class SymbolText
- {
- public override float preferredWidth
- {
- get
- {
- UpdateByDirty();
- return getNodeWidth();
- }
- }
- public override float preferredHeight
- {
- get
- {
- UpdateByDirty();
- return getNodeHeight();
- }
- }
- public override void SetAllDirty()
- {
- base.SetAllDirty();
- SetTextDirty();
- }
- void UpdateByDirty()
- {
- if (m_textDirty)
- {
- UpdateByTextDirty();
- m_textDirty = false;
- UpdateTextHeight();
- m_layoutDirty = false;
- UpdateRenderElement();
- m_renderNodeDirty = false;
- }
- if (m_layoutDirty)
- {
- UpdateTextHeight();
- m_layoutDirty = false;
- UpdateRenderElement();
- m_renderNodeDirty = false;
- }
- if (m_renderNodeDirty)
- {
- UpdateRenderElement();
- m_renderNodeDirty = false;
- }
- }
- public override void Rebuild(CanvasUpdate update)
- {
- if (canvasRenderer.cull)
- return;
- if (pixelsPerUnit >= 10f)
- return;
- switch (update)
- {
- case CanvasUpdate.PreRender:
- {
- UpdateByDirty();
- base.Rebuild(update);
- }
- break;
- }
- }
- protected virtual void SetTextDirty()
- {
- m_textDirty = true;
- m_renderNodeDirty = true;
- SetMaterialDirty();
- FreeDraws();
- if (isActiveAndEnabled)
- CanvasUpdateRegistry.RegisterCanvasElementForGraphicRebuild(this);
- }
- protected override void UpdateMaterial()
- {
- if (!IsActive())
- return;
- if (m_UsedDraws.Count == 0)
- {
- return;
- }
- var components = ListPool<Component>.Get();
- GetComponents(typeof(IMaterialModifier), components);
- for (int i = 0; i < m_UsedDraws.Count; ++i)
- {
- Draw draw = m_UsedDraws[i];
- if (draw.srcMat == null)
- draw.srcMat = material;
- Material currentMat = draw.srcMat;
- for (var m = 0; m < components.Count; m++)
- currentMat = (components[m] as IMaterialModifier).GetModifiedMaterial(currentMat);
- draw.UpdateMaterial(currentMat);
- }
- ListPool<Component>.Release(components);
- }
- Around d_Around = new Around();
- public Around around { get { return d_Around; } }
- public ElementSegment elementSegment
- {
- get
- {
- if (string.IsNullOrEmpty(m_ElementSegment))
- return null;
- return ESFactory.Get(m_ElementSegment);
- }
- }
- protected static List<NodeBase> s_nodebases = new List<NodeBase>();
- // 得到外部结点
- public System.Func<TagAttributes, IExternalNode> getExternalNode { get; set; }
- // 根据新文本,解析结点
- public void UpdateByTextDirty()
- {
- Clear();
- s_nodebases.Clear();
- Parser.parser(this, text, CreateConfig(), s_nodebases, getExternalNode);
- s_nodebases.ForEach((NodeBase nb) =>
- {
- mNodeList.AddLast(nb);
- });
- s_nodebases.Clear();
- }
- public override void SetVerticesDirty()
- {
- base.SetVerticesDirty();
- SetMaterialDirty();
- }
- Vector2 last_size = new Vector2(-1000f, -1000f);
- protected override void OnRectTransformDimensionsChange()
- {
- if (gameObject.activeInHierarchy)
- {
- // prevent double dirtying...
- if (CanvasUpdateRegistry.IsRebuildingLayout())
- {
- if (last_size == rectTransform.rect.size)
- return;
- SetLayoutDirty();
- }
- else
- {
- if (last_size != rectTransform.rect.size)
- SetVerticesDirty();
- SetLayoutDirty();
- }
- }
- }
- public override void SetLayoutDirty()
- {
- base.SetLayoutDirty();
- SetMaterialDirty();
- SetRenderDirty();
- m_textDirty = true;
- m_layoutDirty = true;
- }
- public void UpdateTextHeight()
- {
- if (pixelsPerUnit <= 0f)
- return;
- renderCache.Release();
- float w = rectTransform.rect.size.x /** pixelsPerUnit*/;
- mLines.Clear();
- if (w <= 0f)
- return;
- d_Around.Clear();
- foreach (NodeBase node in mNodeList)
- {
- if (node is RectSpriteNode)
- {
- RectSpriteNode rsn = node as RectSpriteNode;
- d_Around.Add(rsn.rect);
- }
- }
- mLines.Add(new Line(Vector2.zero));
- Vector2 currentpos = Vector2.zero;
- float scale = pixelsPerUnit;
- foreach (NodeBase node in mNodeList)
- node.fill(ref currentpos, mLines, w, scale);
- for (int i = 0; i < mLines.Count; ++i)
- {
- mLines[i].y = Mathf.Max(mLines[i].y, m_MinLineHeight);
- }
- }
- // 更新渲染的文本
- void UpdateRenderElement()
- {
- if (pixelsPerUnit <= 0f)
- return;
- FreeDraws();
- renderCache.Release();
- Rect inputRect = rectTransform.rect;
- float w = inputRect.size.x/* * pixelsPerUnit*/;
- if (w <= 0f)
- return;
- float x = 0;
- uint yline = 0;
- LinkedListNode<NodeBase> itor = mNodeList.First;
- while (itor != null)
- {
- itor.Value.render(
- w,
- renderCache,
- ref x,
- ref yline,
- mLines,
- 0f,
- 0f);
- itor = itor.Next;
- }
- }
- public void FontTextureChangedOther()
- {
- // Only invoke if we are not destroyed.
- if (!this)
- {
- return;
- }
- if (m_DisableFontTextureRebuiltCallback)
- return;
- if (!IsActive())
- return;
- // this is a bit hacky, but it is currently the
- // cleanest solution....
- // if we detect the font texture has changed and are in a rebuild loop
- // we just regenerate the verts for the new UV's
- if (CanvasUpdateRegistry.IsRebuildingGraphics() || CanvasUpdateRegistry.IsRebuildingLayout())
- UpdateGeometry();
- else
- SetAllDirty();
- }
- }
- }
|