123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320 |
- using CommonLang;
- using CommonLang.Concurrent;
- using CommonLang.Property;
- using CommonUnity3D.UGUIAction;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- namespace CommonUnity3D.UGUI
- {
- public class DisplayNode : IDisposable, IActionCompment
- {
- private static List<GameObject> s_fuck_list = new List<GameObject>();
- private static AtomicLong s_RefCount = new AtomicLong(0);
- private static AtomicLong s_AliveCount = new AtomicLong(0);
- public static long RefCount { get { return s_RefCount.Value; } }
- public static long AliveCount { get { return s_AliveCount.Value; } }
- protected internal static void FuckFuckList()
- {
- lock (s_fuck_list)
- {
- if (s_fuck_list.Count > 0)
- {
- foreach (var o in s_fuck_list)
- {
- GameObject.Destroy(o, 0.3f);
- }
- s_fuck_list.Clear();
- }
- }
- }
- private readonly DisplayNodeBehaviour mBinding;
- internal protected readonly GameObject mGameObject;
- internal protected readonly RectTransform mTransform;
- internal protected readonly CanvasRenderer mCanvasRender;
- private DisplayNode mParent;
- private DisplayRoot mRoot;
-
- private bool mVisibleInParent = true;
- private float mAlpha = 1f;
- private bool mIsGray = false;
- private bool mVisible = true;
- private bool mIsDispose = false;
- private bool mIsEnable = false;
- private bool mIsEnableChildren = false;
- private bool mIsPointerDown = false;
- private GrayMaterialModifier mGrayModifier;
- private IInteractiveComponent mSelectable;
- private bool mNeedRefreshInteractive = true;
- private static RectTransform mDisabledNode = null;
-
- public DisplayNode(string name = "")
- {
- s_RefCount++;
- s_AliveCount++;
- mGameObject = new GameObject(name);
- mGameObject.SetActive(false);
- mTransform = mGameObject.AddComponent<RectTransform>();
- mTransform.anchorMin = new Vector2(0, 1);
- mTransform.anchorMax = new Vector2(0, 1);
- mTransform.pivot = new Vector2(0, 1);
- mTransform.position = Vector3.zero;
- mTransform.anchoredPosition = Vector2.zero;
- mTransform.localScale = Vector3.one;
- mTransform.sizeDelta = new Vector2(100, 100);
- mCanvasRender = GenCanvasRenderer();
- mBinding = GenNodeBehavior();
- mBinding.mBinding = this;
- if (mDisabledNode == null)
- {
- GameObject o = new GameObject("DisabledNode");
- mDisabledNode = o.AddComponent<RectTransform>();
- UnityEngine.Object.DontDestroyOnLoad(o);
-
-
-
-
-
- o.SetActive(false);
- }
- }
- ~DisplayNode()
- {
- if (!mIsDispose)
- {
- lock (s_fuck_list)
- {
- s_fuck_list.Add(mGameObject);
- }
- }
- s_RefCount--;
- }
- public void Dispose()
- {
-
- if (mIsDispose)
- {
-
- return;
- }
- this.mUpdateNodes.Clear();
- if (this.event_disposed != null)
- {
- this.event_disposed.Invoke(this);
- }
- this.RemoveAllAction();
- this.OnDisposeEvents();
- this.OnDispose();
- this.mAttributes.Clear();
- foreach (var c in AllChildren)
- {
- c.Dispose();
- }
- this.mParent = null;
- this.mTransform.SetParent(mDisabledNode, false);
- GameObject.Destroy(mGameObject, 0.3f);
- this.mIsDispose = true;
- s_AliveCount--;
- }
- public virtual DisplayNode Clone()
- {
- return null;
- }
-
- public GameObject UnityObject { get { return mGameObject; } }
- public DisplayNode Parent { get { return mParent; } }
- public RectTransform Transform { get { return mTransform; } }
- public DisplayRoot Root
- {
- get
- {
- if (this is DisplayRoot)
- {
- mRoot = this as DisplayRoot;
- return mRoot;
- }
- else if (mRoot != null)
- {
- return mRoot;
- }
- else
- {
- DisplayNode parent = mParent;
- while (parent != null)
- {
- if (parent is DisplayRoot)
- {
- mRoot = parent as DisplayRoot;
- return mRoot;
- }
- parent = parent.mParent;
- }
- }
- return mRoot;
- }
- }
- public bool IsDispose { get { return mIsDispose; } }
- public int NumChildren { get { return mTransform.childCount; } }
- public string UserData { get; set; }
- public int UserTag { get; set; }
- public object Tag { get; set; }
- public string Name
- {
- get { return mGameObject.name; }
- set { mGameObject.name = value; }
- }
- public float X
- {
- get { return Position2D.x; }
- set { Position2D = new Vector2(value, Position2D.y); }
- }
- public float Y
- {
- get { return Position2D.y; }
- set { Position2D = new Vector2(Position2D.x, value); }
- }
- public float Width
- {
- get { return Size2D.x; }
- set { Size2D = new Vector2(value, Size2D.y); }
- }
- public float Height
- {
- get { return Size2D.y; }
- set { Size2D = new Vector2(Size2D.x, value); }
- }
-
-
-
- public bool Visible
- {
- get { return mVisible; }
- set
- {
- if (mVisible != value)
- {
- mVisible = value;
- if (mParent != null)
- {
- mGameObject.SetActive(mVisibleInParent && mVisible);
- }
- else
- {
- mGameObject.SetActive(false);
- }
- OnVisibleChanged(value);
- }
- }
- }
-
-
-
- public bool VisibleInParent
- {
- get { return mVisibleInParent; }
- set
- {
- if (mVisibleInParent != value)
- {
- mVisibleInParent = value;
- if (mParent != null)
- {
- mGameObject.SetActive(mVisibleInParent && mVisible);
- }
- else
- {
- mGameObject.SetActive(false);
- }
- OnVisibleChanged(value);
- }
- }
- }
-
-
-
- public float Alpha
- {
- get { return mAlpha; }
- set
- {
- if (mAlpha != value)
- {
- SetAlphaInternal(value);
- this.mAlpha = value;
- this.mBinding.Alpha = value;
- }
- }
- }
- public bool IsGray
- {
- get { return mIsGray; }
- set
- {
- if (mIsGray != value)
- {
- this.SetGrayInternal(value);
- this.mIsGray = value;
- this.mBinding.IsGray = value;
- }
- }
- }
-
-
-
- public Vector2 Scale
- {
- get
- {
- Vector3 temp = mTransform.localScale;
- return new Vector2(temp.x, temp.y);
- }
- set
- {
- mTransform.localScale = new Vector3(value.x, value.y);
- }
- }
-
-
-
- public bool EnableOutMove { get; set; }
-
-
-
- public bool Enable
- {
- get { return mIsEnable; }
- set
- {
- mBinding.IsEnable = value;
- if (mIsEnable != value)
- {
- mIsEnable = value;
- mNeedRefreshInteractive = true;
- }
- }
- }
-
-
-
- public bool EnableChildren
- {
- get { return mIsEnableChildren; }
- set
- {
- mBinding.IsEnableChildren = value;
- mIsEnableChildren = value;
- }
- }
-
-
-
- public bool IsInteractive
- {
- get { return mSelectable != null && mSelectable.AsSelectable.enabled; }
- set
- {
- mBinding.IsInteractive = value;
- if (IsInteractive != value)
- {
- if (value)
- {
- if (mSelectable == null)
- {
- mSelectable = GenInteractive();
- mSelectable.event_PointerDown = this.DoPointerDown;
- mSelectable.event_PointerUp = this.DoPointerUp;
- mSelectable.event_PointerClick = this.DoPointerClick;
- mSelectable.AsSelectable.enabled = true;
- }
- else
- {
- mSelectable.AsSelectable.enabled = true;
- }
- }
- else
- {
- if (mSelectable != null)
- {
- mSelectable.AsSelectable.enabled = false;
- }
- }
- mNeedRefreshInteractive = true;
- }
- }
- }
-
-
-
- public bool EnableTouchInParents
- {
- get
- {
- DisplayNode parent = mParent;
- while (parent != null)
- {
- if (!parent.EnableChildren)
- {
- return false;
- }
- parent = parent.mParent;
- }
- return true;
- }
- }
- public void SetAnchor(Vector2 v)
- {
- if (this.Parent == null || (mTransform.anchorMin == v && mTransform.anchorMax == v))
- {
- return;
- }
- Vector2 last = mTransform.anchoredPosition;
- Vector2 s = mTransform.sizeDelta;
- Vector2 p = this.Position2D;
- mTransform.anchorMin = v;
- mTransform.anchorMax = v;
- mTransform.offsetMin = Vector2.zero;
- mTransform.offsetMax = Vector2.zero;
- Vector2 half = new Vector2(this.Parent.Size2D.x * v.x, this.Parent.Size2D.y * v.y);
- Vector2 shalf = new Vector2(s.x * v.x, s.y * v.y);
- Vector2 offet = new Vector2(half.x - last.x - shalf.x, half.y + last.y - shalf.y);
- mTransform.anchoredPosition = offet;
- }
-
- public T AddComponent<T>() where T : Component
- {
- return mGameObject.AddComponent<T>();
- }
- public Component AddComponent(Type componentType)
- {
- return mGameObject.AddComponent(componentType);
- }
-
- #region _InternalCall_
- private void SetAlphaInternal(float _alpha)
- {
- Graphic grap = mTransform.GetComponent<Graphic>();
- if (grap != null)
- {
- var c = grap.color;
- c.a = _alpha * mAlpha;
- grap.color = c;
- }
- int count = mTransform.childCount;
- for (int i = 0; i < count; i++)
- {
- var child = mTransform.GetChild(i);
- DisplayNode node = AsDisplayNode(child.gameObject);
- if (node != null)
- {
- node.SetAlphaInternal(_alpha);
- }
- }
- }
- private void SetGrayInternal(bool isGray)
- {
- Graphic grap = mTransform.GetComponent<Graphic>();
- if (grap != null)
- {
- if (isGray)
- {
- if (this.mGrayModifier == null)
- {
- this.mGrayModifier = GenGrayMaterialModifier(grap);
- }
- }
- else
- {
- if (this.mGrayModifier != null)
- {
- MonoBehaviour.DestroyImmediate(mGrayModifier);
- mGrayModifier = null;
- }
- }
- grap.SetMaterialDirty();
- }
- int count = mTransform.childCount;
- for (int i = 0; i < count; i++)
- {
- var child = mTransform.GetChild(i);
- DisplayNode node = AsDisplayNode(child.gameObject);
- if (node != null)
- {
- node.SetGrayInternal(isGray);
- }
- }
- }
- #endregion
-
- #region __Events__
- internal void DoStart()
- {
- this.OnStart();
- }
- internal void DoUpdate()
- {
- if (mNeedRefreshInteractive)
- {
- mNeedRefreshInteractive = false;
- OnInteractiveChanged();
- }
- if (mSelectable != null && (EnableOutMove ? mIsPointerDown : mSelectable.IsPressDown))
- {
- DoPointerMove(mSelectable.LastPointerDown);
- }
- this.UpdateAction(Time.deltaTime);
- this.OnUpdate();
- }
- internal void DoEndUpdate()
- {
- this.OnEndUpdate();
- }
- private void DoPointerDown(PointerEventData e)
- {
- this.OnPointerDown(e);
- mIsPointerDown = true;
- if (this.event_PointerDown != null)
- {
- this.event_PointerDown.Invoke(this, e);
- }
- }
- private void DoPointerUp(PointerEventData e)
- {
- this.OnPointerUp(e);
- mIsPointerDown = false;
- if (this.event_PointerUp != null)
- {
- this.event_PointerUp.Invoke(this, e);
- }
- }
- private void DoPointerClick(PointerEventData e)
- {
- this.OnPointerClick(e);
- if (this.event_PointerClick != null)
- {
- this.event_PointerClick.Invoke(this, e);
- }
- }
- private void DoPointerMove(PointerEventData e)
- {
- this.OnPointerMove(e);
- if (this.event_PointerMove != null)
- {
- this.event_PointerMove.Invoke(this, e);
- }
- }
- protected virtual void OnDispose() { }
- protected virtual void OnStart() { }
- protected virtual void OnUpdate() { }
- protected virtual void OnEndUpdate() { }
- protected virtual void OnChildAdded(DisplayNode child) { }
- protected virtual void OnChildRemoved(DisplayNode child) { }
- protected virtual void OnSizeChanged(Vector2 size) { }
- protected virtual void OnVisibleChanged(bool visible) { }
- protected virtual void OnPointerDown(PointerEventData e) { }
- protected virtual void OnPointerUp(PointerEventData e) { }
- protected virtual void OnPointerClick(PointerEventData e) { }
- protected virtual void OnPointerMove(PointerEventData e) { }
- public delegate void PointerEventHandler(DisplayNode sender, PointerEventData e);
- public delegate void ChildEventHandler(DisplayNode sender, DisplayNode e);
- public delegate void DiposeEventHandle(DisplayNode sender);
- [Desc("子节点已添加")]
- public ChildEventHandler event_ChildAdded;
- [Desc("子节点已移除")]
- public ChildEventHandler event_ChildRemoved;
- [Desc("鼠标(触摸)已按下")]
- public PointerEventHandler event_PointerDown;
- [Desc("鼠标(触摸)已松开")]
- public PointerEventHandler event_PointerUp;
- [Desc("鼠标(触摸)已移动")]
- public PointerEventHandler event_PointerMove;
- [Desc("Click")]
- public PointerEventHandler event_PointerClick;
- [Desc("节点已销毁")]
- public DiposeEventHandle event_disposed;
- [Desc("节点已销毁")]
- public event DiposeEventHandle Disposed { add { event_disposed += value; } remove { event_disposed -= value; } }
- [Desc("子节点已添加")]
- public event ChildEventHandler ChildAdded { add { event_ChildAdded += value; } remove { event_ChildAdded -= value; } }
- [Desc("子节点已移除")]
- public event ChildEventHandler ChildRemoved { add { event_ChildRemoved += value; } remove { event_ChildRemoved -= value; } }
- [Desc("鼠标(触摸)已按下")]
- public event PointerEventHandler PointerDown { add { event_PointerDown += value; } remove { event_PointerDown -= value; } }
- [Desc("鼠标(触摸)已松开")]
- public event PointerEventHandler PointerUp { add { event_PointerUp += value; } remove { event_PointerUp -= value; } }
- [Desc("鼠标(触摸)已移动")]
- public event PointerEventHandler PointerMove { add { event_PointerMove += value; } remove { event_PointerMove -= value; } }
- [Desc("Click")]
- public event PointerEventHandler PointerClick { add { event_PointerClick += value; } remove { event_PointerClick -= value; } }
- protected virtual void OnDisposeEvents()
- {
- this.event_ChildAdded = null;
- this.event_ChildRemoved = null;
- this.event_PointerDown = null;
- this.event_PointerUp = null;
- this.event_PointerMove = null;
- this.event_PointerClick = null;
- this.event_disposed = null;
- }
- #endregion
-
- #region __Transform2D__
- public Rect Bounds2D
- {
- get
- {
- Rect rect = new Rect();
- rect.position = mTransform.localPosition;
- rect.size = mTransform.sizeDelta;
- rect.y = -rect.y;
- return rect;
- }
- set
- {
- this.Position2D = value.position;
- this.Size2D = value.size;
- }
- }
- public Vector2 Position2D
- {
- get
- {
- Vector2 pos = mTransform.localPosition;
- pos.y = -pos.y;
- return pos;
- }
- set
- {
- value.y = -value.y;
- this.mTransform.localPosition = value;
- }
- }
- public Vector2 Size2D
- {
- get { return mTransform.sizeDelta; }
- set
- {
- if (this.mTransform.sizeDelta != value)
- {
- this.mTransform.sizeDelta = value;
- OnSizeChanged(value);
- }
- }
- }
- public Vector2 ScreenToLocalPoint2D(PointerEventData e)
- {
- Vector2 local;
- RectTransformUtility.ScreenPointToLocalPointInRectangle(mTransform, e.position, e.pressEventCamera, out local);
- local.y = -local.y;
- return local;
- }
-
-
-
-
- public Vector2 LocalToGlobal()
- {
- return this.UnityObject.transform.parent.TransformPoint(mTransform.localPosition);
- }
-
-
-
-
-
-
- public Vector2 GlobalToLocal(Vector2 wordpos, bool isXMUGUIPos = true)
- {
- Vector2 ret = this.UnityObject.transform.InverseTransformPoint(wordpos);
- if (isXMUGUIPos)
- {
- ret.y = -ret.y;
- }
- return ret;
- }
- #endregion
-
- #region __Attribute__
- private HashMap<string, object> mAttributes = new HashMap<string, object>();
- public bool IsAttribute(string key)
- {
- return mAttributes.ContainsKey(key);
- }
- public void SetAttribute(string key, object value)
- {
- mAttributes.Put(key, value);
- }
- public object GetAttribute(string key)
- {
- return mAttributes.Get(key);
- }
- public T GetAttributeAs<T>(string key)
- {
- object obj = mAttributes.Get(key);
- if (obj != null)
- {
- return (T)obj;
- }
- return default(T);
- }
- #endregion
-
- #region __BindingMonoBehaviour__
- public static DisplayNode AsDisplayNode(GameObject obj)
- {
- DisplayNodeBehaviour binding = obj.GetComponent<DisplayNodeBehaviour>();
- if (binding != null)
- {
- return binding.Binding;
- }
- return null;
- }
- public static DisplayNode AsDisplayNode(Component obj)
- {
- DisplayNodeBehaviour binding = obj.GetComponent<DisplayNodeBehaviour>();
- if (binding != null)
- {
- return binding.Binding;
- }
- return null;
- }
- private bool mIsInit = false;
- private List<DisplayNode> mUpdateNodes = new List<DisplayNode>();
- private List<DisplayNode> RefreshUpdateNodes()
- {
- int num_children = NumChildren;
- if (mUpdateNodes.Count != num_children)
- {
- CUtils.SetListSize(mUpdateNodes, num_children);
- }
- for (int i = num_children - 1; i >= 0; --i)
- {
- Transform child = mTransform.GetChild(i);
- if (mUpdateNodes[i] != null && mUpdateNodes[i].mTransform == child)
- {
- continue;
- }
- else
- {
- mUpdateNodes[i] = AsDisplayNode(child);
- }
- }
- return mUpdateNodes;
- }
- internal virtual void InternalUpdate()
- {
- if (this.mIsInit == false)
- {
- this.mIsInit = true;
- this.DoStart();
- mBinding.IsEnable = this.Enable;
- mBinding.IsEnableChildren = this.EnableChildren;
- mBinding.IsInteractive = this.IsInteractive;
- mBinding.Alpha = this.Alpha;
- mBinding.IsGray = this.IsGray;
- }
- else
- {
- if (this.Enable != mBinding.IsEnable)
- {
- this.Enable = mBinding.IsEnable;
- }
- if (this.EnableChildren != mBinding.IsEnableChildren)
- {
- this.EnableChildren = mBinding.IsEnableChildren;
- }
- if (this.IsInteractive != mBinding.IsInteractive)
- {
- this.IsInteractive = mBinding.IsInteractive;
- }
- if (this.Alpha != mBinding.Alpha)
- {
- this.Alpha = mBinding.Alpha;
- }
- if (this.IsGray != mBinding.IsGray)
- {
- this.IsGray = mBinding.IsGray;
- }
- }
- if (mIsDispose) return;
- if (mGameObject.activeSelf)
- {
- this.DoUpdate();
- List<DisplayNode> childs = this.RefreshUpdateNodes();
- DisplayNode child;
- for (int i = childs.Count - 1; i >= 0 && i < childs.Count; --i)
- {
- if ((child = childs[i]) != null)
- {
- child.InternalUpdate();
- }
- }
- this.DoEndUpdate();
- }
- }
- protected virtual IInteractiveComponent GenInteractive()
- {
- return mGameObject.AddComponent<DisplayNodeInteractive>();
- }
- protected virtual DisplayNodeBehaviour GenNodeBehavior()
- {
- return mGameObject.AddComponent<DisplayNodeBehaviour>();
- }
- protected virtual CanvasRenderer GenCanvasRenderer()
- {
- return mGameObject.AddComponent<CanvasRenderer>();
- }
- protected virtual GrayMaterialModifier GenGrayMaterialModifier(UnityEngine.UI.Graphic g)
- {
- if (g is Text)
- {
- return mGameObject.AddComponent<TextGrayMaterialModifier>();
- }
- else
- {
- return mGameObject.AddComponent<GrayMaterialModifier>();
- }
- }
- protected virtual void OnInteractiveChanged()
- {
- var g = mGameObject.GetComponent<UnityEngine.UI.Graphic>();
- if (g != null)
- {
- g.raycastTarget = IsInteractive && Enable;
- }
-
- }
-
-
-
- public IInteractiveComponent Selectable
- {
- get { return mSelectable; }
- }
-
-
-
- public bool IsPressed
- {
- get { return (mSelectable != null) ? mSelectable.IsPressDown : false; }
- }
- #endregion
-
- #region __Container__
- public bool ContainsChild(DisplayNode child)
- {
- while (child != null)
- {
- if (child == this)
- return true;
- else
- child = child.Parent;
- }
- return false;
- }
- public void AddChild(DisplayNode child)
- {
- this.AddChildAt(child, NumChildren);
- }
- public bool AddChildAt(DisplayNode child, int index)
- {
- if (child == null || index < 0)
- {
- return false;
- }
- if (child.Parent == this)
- {
- SetChildIndex(child, index);
- }
- else
- {
- child.RemoveFromParent();
- child.mParent = this;
- child.mTransform.SetParent(this.mTransform, false);
- child.mGameObject.SetActive(child.mVisible);
- this.SetChildIndex(child, index);
- this.OnChildAdded(child);
- if (event_ChildAdded != null) event_ChildAdded.Invoke(this, child);
- return true;
- }
- return false;
- }
- public bool RemoveChild(DisplayNode child, bool dispose = false)
- {
- if (child.mParent == this)
- {
- child.mParent = null;
- child.mTransform.SetParent(mDisabledNode, false);
-
-
- this.OnChildRemoved(child);
- if (event_ChildRemoved != null) event_ChildRemoved.Invoke(this, child);
- if (dispose)
- {
- child.Dispose();
- }
- return true;
- }
- return false;
- }
- public DisplayNode RemoveChildByName(string name, bool dispose = false)
- {
- Transform child = mTransform.FindChild(name);
- if (child == null)
- {
- return null;
- }
- DisplayNode ret = AsDisplayNode(child.gameObject);
- if (ret != null)
- {
- ret.RemoveFromParent(dispose);
- }
- return ret;
- }
- public DisplayNode RemoveChildAt(int index, bool dispose = false)
- {
- DisplayNode ret = GetChildAt(index);
- if (ret != null)
- {
- ret.RemoveFromParent(dispose);
- }
- return ret;
- }
- public void RemoveChildren(int beginIndex = 0, int endIndex = -1, bool dispose = false)
- {
- if (endIndex < 0 || endIndex >= NumChildren)
- endIndex = NumChildren - 1;
- for (int i = beginIndex; i <= endIndex; ++i)
- RemoveChildAt(beginIndex, dispose);
- }
- public void RemoveAllChildren(bool dispose = true)
- {
- RemoveChildren(0, -1, dispose);
- }
- public void RemoveFromParent(bool dispose = false)
- {
- if (mParent != null)
- {
- mParent.RemoveChild(this, dispose);
- mParent = null;
- }
- }
-
- public DisplayNode GetChildAt(int index)
- {
- Transform child = mTransform.GetChild(index);
- DisplayNode ret = AsDisplayNode(child.gameObject);
- return ret;
- }
- public IEnumerable<DisplayNode> AllChildren
- {
- get { return GetAllChild(); }
- }
- public IEnumerable<DisplayNode> GetAllChild()
- {
- List<DisplayNode> ret = new List<DisplayNode>(mTransform.childCount);
- GetAllChild(ret);
- return ret;
- }
- public void GetAllChild(List<DisplayNode> ret)
- {
- int count = mTransform.childCount;
- for (int i = 0; i < count; i++)
- {
- var child = mTransform.GetChild(i);
- DisplayNode node = AsDisplayNode(child.gameObject);
- if (node != null)
- {
- ret.Add(node);
- }
- }
- }
- public void SetParentIndex(int index)
- {
- mTransform.SetSiblingIndex(index);
- }
- public void SetChildIndex(DisplayNode child, int index)
- {
- child.mTransform.SetSiblingIndex(index);
- }
- public int GetChildIndex(DisplayNode child)
- {
- return child.mTransform.GetSiblingIndex();
- }
- #endregion
-
- #region __ChildsUtils__
-
-
-
-
-
-
-
- public T FindChildByName<T>(string name, bool recursive = true) where T : DisplayNode
- {
- return FindChildAs<T>((child) =>
- {
- return (name == child.Name);
- },
- recursive);
- }
-
-
-
-
-
-
-
- public T FindChildAs<T>(Predicate<T> select, bool recursive = true) where T : DisplayNode
- {
- T child = null;
- T uicc = null;
- int i;
- int length = NumChildren;
- for (i = length - 1; i >= 0; --i)
- {
- child = this.GetChildAt(i) as T;
- if (child != null && select(child))
- {
- return child;
- }
- }
- if (recursive)
- {
- length = NumChildren;
- for (i = length - 1; i >= 0; --i)
- {
- DisplayNode sc = this.GetChildAt(i);
- if (sc != null)
- {
- uicc = sc.FindChildAs<T>(select, recursive);
- if (uicc != null)
- {
- return uicc;
- }
- }
- }
- }
- return null;
- }
- public void ForEachChilds<T>(Action<T> action, bool recursive = true) where T : DisplayNode
- {
- T child = null;
- int i;
- int length = NumChildren;
- for (i = length - 1; i >= 0; --i)
- {
- child = this.GetChildAt(i) as T;
- if (child != null)
- {
- action(child);
- }
- }
- if (recursive)
- {
- length = NumChildren;
- for (i = length - 1; i >= 0; --i)
- {
- DisplayNode sc = this.GetChildAt(i);
- if (sc != null)
- {
- sc.ForEachChilds<T>(action, recursive);
- }
- }
- }
- }
-
-
-
-
- public Vector2 GetChildsContentSize()
- {
- Vector2 ret = new Vector2(0, 0);
- int count = mTransform.childCount;
- for (int i = 0; i < mTransform.childCount; i++)
- {
- var child = mTransform.GetChild(i);
- DisplayNode node = AsDisplayNode(child.gameObject);
- if (node != null)
- {
- Vector2 size = node.Size2D;
- ret.x += Math.Max(ret.x, size.x);
- ret.y += Math.Max(ret.y, size.y);
- }
- }
- return ret;
- }
- #endregion
-
- #region XMAction.
- private readonly List<CellAction> mActionList = new List<CellAction>();
- private List<IAction> mWaitList = new List<IAction>();
- protected class CellAction
- {
- public string mActionType = null;
- public IAction mAction = null;
- public CellAction(IAction action)
- {
- mAction = action;
- mActionType = action.GetActionType();
- }
- public void Dispose()
- {
- mActionType = null;
- mAction = null;
- }
- }
- protected virtual string ParseActionType(IAction action)
- {
- return action.GetActionType();
- }
- public virtual void AddAction(IAction action)
- {
- if (action == null)
- {
- throw new Exception("action can not be null");
- }
- PushWaitList(action);
- }
- private void PushWaitList(IAction action)
- {
- mWaitList.Add(action);
- }
- private void CheckAction(IAction action)
- {
- string actionType = ParseActionType(action);
- IAction oldAction = null;
- if (HasAction(actionType))
- {
-
- oldAction = GetAction(actionType);
- if (oldAction != null)
- {
- RemoveAction(oldAction, false);
- }
- }
- StartAction(actionType, action);
- }
- private void UpdateWaitList()
- {
- for (int i = 0; i < mWaitList.Count; i++)
- {
- CheckAction(mWaitList[i]);
- }
- mWaitList.Clear();
- }
- protected virtual void StartAction(string actionType, IAction action)
- {
- CellAction ca = new CellAction(action);
-
- mActionList.Add(ca);
- action.onStart(this);
- }
- public virtual void RemoveAction(IAction action, bool sendCallBack)
- {
- RemoveAction(ParseActionType(action), sendCallBack);
- }
- public virtual void RemoveAction(string actionType, bool sendCallBack)
- {
- for (int i = 0; i < mActionList.Count; i++)
- {
- if (mActionList[i].mActionType == actionType)
- {
- mActionList[i].mAction.onStop(this, sendCallBack);
- mActionList[i].Dispose();
- mActionList.RemoveAt(i);
- i--;
- return;
- }
- }
- }
- public virtual bool HasAction(IAction action)
- {
- string name = ParseActionType(action);
- return HasAction(name);
- }
- public virtual bool HasAction(string ActionType)
- {
- for (int i = 0; i < mActionList.Count; i++)
- {
- if (mActionList[i].mActionType == ActionType)
- {
- return true;
- }
- }
- return false;
- }
- public virtual IAction GetAction(string actionType)
- {
- for (int i = 0; i < mActionList.Count; i++)
- {
- if (mActionList[i].mActionType == actionType)
- {
- return mActionList[i].mAction;
- }
- }
- return null;
- }
- public virtual void RemoveAllAction(bool sendCallBack = false)
- {
- for (int i = mActionList.Count - 1; i >= 0; i--)
- {
- mActionList[i].mAction.onStop(this, sendCallBack);
- mActionList[i].Dispose();
- }
- mActionList.Clear();
- mWaitList.Clear();
- }
- public virtual void UpdateAction(float deltaTime)
- {
- UpdateWaitList();
- if (mActionList != null && mActionList.Count > 0)
- {
- IAction act = null;
- for (int i = 0; i < mActionList.Count; i++)
- {
- CellAction ca = mActionList[i];
- act = ca.mAction;
- act.onUpdate(this, deltaTime);
- if (act.IsEnd())
- {
- act.onStop(this, true);
- ca.Dispose();
- mActionList.Remove(ca);
- i--;
- }
- }
- }
- }
- #endregion
-
- }
- }
|