| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- using UnityEngine;
- using UnityEngine.EventSystems;
- using System.Collections.Generic;
- using UnityEngine.UI;
- namespace WXB
- {
- // 缓存渲染元素
- public partial class RenderCache
- {
- Owner mOwner;
- public RenderCache(Owner st)
- {
- mOwner = st;
- materials = new List<Texture>();
- }
- public abstract class BaseData
- {
- Rect m_Rect;
- public Rect rect
- {
- set { m_Rect = value; }
- get { return m_Rect; }
- }
- public NodeBase node = null;
- public Line line { get; protected set; } // 当前所处的行
- public bool isContain(Vector2 pos)
- {
- var rect = new Rect(m_Rect);
- rect.y = rect.y + line.y - rect.height;
- return rect.Contains(pos);
- }
- public virtual bool isAlignByGeometry
- {
- get { return false; }
- }
- public abstract void Render(VertexHelper vh, Rect area, Vector2 offset, float pixelsPerUnit);
- public virtual void OnMouseEnter() { }
- public virtual void OnMouseLevel() { }
- public virtual void OnMouseUp(PointerEventData eventData) { }
- public int subMaterial { get; set; }
- public override string ToString()
- {
- return string.Format("rect:{0}", m_Rect);
- }
- public virtual void Release()
- {
- node = null;
- line = null;
- OnRelease();
- }
- protected abstract void OnRelease();
- public virtual void OnAlignByGeometry(ref Vector2 offset, float pixelsPerUnit)
- {
- }
- public virtual void OnLineYCheck(float pixelsPerUnit)
- {
- }
- protected LineAlignment lineAlignment
- {
- get
- {
- return (node.lineAlignment == LineAlignment.Default ? node.owner.lineAlignment : node.lineAlignment);
- }
- }
- public virtual Vector2 GetStartLeftBottom(float unitsPerPixel)
- {
- if (line == null)
- {
- return new Vector2(rect.x, rect.y + rect.height);
- }
- Vector2 leftBottomPos = new Vector2(rect.x, rect.y + rect.height);
- var la = lineAlignment;
- switch (la)
- {
- case LineAlignment.Top:
- break;
- case LineAlignment.Center:
- {
- if (line.y == rect.height)
- {
- }
- else
- {
- float offset = ((line.y - rect.height) * 0.5f);
- leftBottomPos.y += offset;
- }
- }
- break;
- case LineAlignment.Bottom:
- leftBottomPos.y = rect.y + line.y;
- break;
- }
- return leftBottomPos;
- }
- }
- List<BaseData> DataList = new List<BaseData>();
- public List<Texture> materials { get; protected set; }
- public void Release()
- {
- materials.Clear();
- BaseData bd = null;
- for (int i = 0; i < DataList.Count; ++i)
- {
- bd = DataList[i];
- bd.Release();
- if (bd is TextData)
- {
- PoolData<TextData>.Free((TextData)bd);
- }
- else if (bd is SpriteData)
- {
- PoolData<SpriteData>.Free((SpriteData)bd);
- }
- }
- DataList.Clear();
- }
- public void cacheText(Line l, TextNode n, string text, Rect rect)
- {
- TextData td = PoolData<TextData>.Get();
- td.Reset(n, text, rect, l);
- DataList.Add(td);
- td.subMaterial = materials.IndexOf(n.d_font.material.mainTexture);
- if (td.subMaterial == -1)
- {
- td.subMaterial = materials.Count;
- materials.Add(n.d_font.material.mainTexture);
- }
- }
- public void cacheSprite(Line l, NodeBase n, Sprite sprite, Rect rect)
- {
- if (sprite != null)
- {
- SpriteData sd = PoolData<SpriteData>.Get();
- sd.Reset(n, sprite, rect, l);
- DataList.Add(sd);
- sd.subMaterial = materials.IndexOf(sprite.texture);
- if (sd.subMaterial == -1)
- {
- sd.subMaterial = materials.Count;
- materials.Add(sprite.texture);
- }
- }
- }
- public void cacheCartoon(Line l, NodeBase n, Cartoon cartoon, Rect rect)
- {
- if (cartoon != null)
- {
- CartoonData cd = PoolData<CartoonData>.Get();
- cd.Reset(n, cartoon, rect, l);
- DataList.Add(cd);
- }
- }
- public bool isEmpty
- {
- get { return DataList.Count == 0; }
- }
- struct Key
- {
- public int subMaterial;
- public bool isBlink;
- public bool isOffset;
- public Rect offsetRect;
- public bool IsEquals(BaseData bd)
- {
- return subMaterial == bd.subMaterial &&
- isBlink == bd.node.d_bBlink &&
- (
- (isOffset == false && bd.node.d_bOffset == false) ||
- (isOffset == bd.node.d_bOffset && offsetRect == bd.node.d_rectOffset)
- );
- }
- public List<BaseData> nodes;
- public DrawType drawType
- {
- get
- {
- if (isBlink)
- {
- if (isOffset)
- return DrawType.OffsetAndAlpha;
- return DrawType.Alpha;
- }
- if (isOffset)
- {
- return DrawType.Offset;
- }
- return DrawType.Default;
- }
- }
- public Draw Get(Owner owner, Texture texture)
- {
- long key = nodes[0].node.keyPrefix;
- key += texture.GetInstanceID();
- Draw draw = owner.GetDraw(drawType, key,
- (Draw d, object p) =>
- {
- d.texture = texture;
- if (d is OffsetDraw)
- {
- OffsetDraw od = d as OffsetDraw;
- od.Set((Rect)p);
- }
- }, offsetRect);
- return draw;
- }
- }
- static List<Key> s_keys = new List<Key>();
- Vector2 DrawOffset;
- public void OnAlignByGeometry(ref Vector2 offset, float pixelsPerUnit, float firstHeight)
- {
- for (int m = 0; m < DataList.Count; ++m)
- {
- if (DataList[m].rect.y > firstHeight)
- continue;
- if (!DataList[m].isAlignByGeometry)
- {
- offset = Vector2.zero;
- return;
- }
- DataList[m].OnAlignByGeometry(ref offset, pixelsPerUnit);
- }
- }
- // 行修正
- public void OnCheckLineY(float pixelsPerUnit)
- {
- for (int m = 0; m < DataList.Count; ++m)
- {
- DataList[m].OnLineYCheck(pixelsPerUnit);
- }
- }
- public void Render(VertexHelper vh, Rect rect, Vector2 offset, float pixelsPerUnit, Mesh workerMesh, Material defaultMaterial)
- {
- DrawOffset = offset /** pixelsPerUnit*/;
- s_keys.Clear();
- for (int m = 0; m < DataList.Count; ++m)
- {
- BaseData bd = DataList[m];
- int index = s_keys.FindIndex((Key k) =>
- {
- return k.IsEquals(bd);
- });
- if (index == -1)
- {
- Key k = new Key();
- k.subMaterial = bd.subMaterial;
- k.isOffset = bd.node.d_bOffset;
- k.isBlink = bd.node.d_bBlink;
- k.offsetRect = bd.node.d_rectOffset;
- k.nodes = ListPool<BaseData>.Get();
- s_keys.Add(k);
- k.nodes.Add(bd);
- }
- else
- {
- s_keys[index].nodes.Add(bd);
- }
- }
- vh.Clear();
- for (int i = 0; i < s_keys.Count; ++i)
- {
- Key key = s_keys[i];
- for (int m = 0; m < key.nodes.Count; ++m)
- {
- key.nodes[m].Render(vh, rect, offset, pixelsPerUnit);
- }
- if (vh.currentVertCount != 0)
- {
- Draw draw = key.Get(mOwner, materials[key.subMaterial]);
- vh.FillMesh(workerMesh);
- draw.FillMesh(workerMesh);
- vh.Clear();
- }
- ListPool<BaseData>.Release(key.nodes);
- }
- s_keys.Clear();
- }
- public BaseData Get(Vector2 pos)
- {
- BaseData bd = null;
- pos -= DrawOffset;
- for (int i = 0; i < DataList.Count; ++i)
- {
- bd = DataList[i];
- if (bd.isContain(pos))
- return bd;
- }
- return null;
- }
- public void Get(List<BaseData> bds, NodeBase nb)
- {
- BaseData bd = null;
- for (int i = 0; i < DataList.Count; ++i)
- {
- bd = DataList[i];
- if (bd.node == nb)
- bds.Add(bd);
- }
- }
- }
- }
|