123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191 |
- using System;
- using System.Diagnostics;
- using UnityEngine;
- [ExecuteInEditMode]
- [AddComponentMenu("NGUI/UI/NGUI Widget")]
- public class UIWidget : UIRect
- {
- public UIDrawCall.OnRenderCallback onRender
- {
- get
- {
- return this.mOnRender;
- }
- set
- {
- if (this.mOnRender != value)
- {
- if (this.drawCall != null && this.drawCall.onRender != null && this.mOnRender != null)
- {
- UIDrawCall uidrawCall = this.drawCall;
- uidrawCall.onRender = (UIDrawCall.OnRenderCallback)Delegate.Remove(uidrawCall.onRender, this.mOnRender);
- }
- this.mOnRender = value;
- if (this.drawCall != null)
- {
- UIDrawCall uidrawCall2 = this.drawCall;
- uidrawCall2.onRender = (UIDrawCall.OnRenderCallback)Delegate.Combine(uidrawCall2.onRender, value);
- }
- }
- }
- }
- public Vector4 drawRegion
- {
- get
- {
- return this.mDrawRegion;
- }
- set
- {
- if (this.mDrawRegion != value)
- {
- this.mDrawRegion = value;
- if (this.autoResizeBoxCollider)
- {
- this.ResizeCollider();
- }
- this.MarkAsChanged();
- }
- }
- }
- public Vector2 pivotOffset
- {
- get
- {
- return NGUIMath.GetPivotOffset(this.pivot);
- }
- }
- public int width
- {
- get
- {
- return this.mWidth;
- }
- set
- {
- int minWidth = this.minWidth;
- if (value < minWidth)
- {
- value = minWidth;
- }
- if (this.mWidth != value && this.keepAspectRatio != UIWidget.AspectRatioSource.BasedOnHeight)
- {
- if (this.isAnchoredHorizontally)
- {
- if (this.leftAnchor.target != null && this.rightAnchor.target != null)
- {
- if (this.mPivot == UIWidget.Pivot.BottomLeft || this.mPivot == UIWidget.Pivot.Left || this.mPivot == UIWidget.Pivot.TopLeft)
- {
- NGUIMath.AdjustWidget(this, 0f, 0f, (float)(value - this.mWidth), 0f);
- }
- else if (this.mPivot == UIWidget.Pivot.BottomRight || this.mPivot == UIWidget.Pivot.Right || this.mPivot == UIWidget.Pivot.TopRight)
- {
- NGUIMath.AdjustWidget(this, (float)(this.mWidth - value), 0f, 0f, 0f);
- }
- else
- {
- int num = value - this.mWidth;
- num -= (num & 1);
- if (num != 0)
- {
- NGUIMath.AdjustWidget(this, (float)(-(float)num) * 0.5f, 0f, (float)num * 0.5f, 0f);
- }
- }
- }
- else if (this.leftAnchor.target != null)
- {
- NGUIMath.AdjustWidget(this, 0f, 0f, (float)(value - this.mWidth), 0f);
- }
- else
- {
- NGUIMath.AdjustWidget(this, (float)(this.mWidth - value), 0f, 0f, 0f);
- }
- }
- else
- {
- this.SetDimensions(value, this.mHeight);
- }
- }
- }
- }
- public int height
- {
- get
- {
- return this.mHeight;
- }
- set
- {
- int minHeight = this.minHeight;
- if (value < minHeight)
- {
- value = minHeight;
- }
- if (this.mHeight != value && this.keepAspectRatio != UIWidget.AspectRatioSource.BasedOnWidth)
- {
- if (this.isAnchoredVertically)
- {
- if (this.bottomAnchor.target != null && this.topAnchor.target != null)
- {
- if (this.mPivot == UIWidget.Pivot.BottomLeft || this.mPivot == UIWidget.Pivot.Bottom || this.mPivot == UIWidget.Pivot.BottomRight)
- {
- NGUIMath.AdjustWidget(this, 0f, 0f, 0f, (float)(value - this.mHeight));
- }
- else if (this.mPivot == UIWidget.Pivot.TopLeft || this.mPivot == UIWidget.Pivot.Top || this.mPivot == UIWidget.Pivot.TopRight)
- {
- NGUIMath.AdjustWidget(this, 0f, (float)(this.mHeight - value), 0f, 0f);
- }
- else
- {
- int num = value - this.mHeight;
- num -= (num & 1);
- if (num != 0)
- {
- NGUIMath.AdjustWidget(this, 0f, (float)(-(float)num) * 0.5f, 0f, (float)num * 0.5f);
- }
- }
- }
- else if (this.bottomAnchor.target != null)
- {
- NGUIMath.AdjustWidget(this, 0f, 0f, 0f, (float)(value - this.mHeight));
- }
- else
- {
- NGUIMath.AdjustWidget(this, 0f, (float)(this.mHeight - value), 0f, 0f);
- }
- }
- else
- {
- this.SetDimensions(this.mWidth, value);
- }
- }
- }
- }
- public Color color
- {
- get
- {
- return this.mColor;
- }
- set
- {
- if (this.mColor != value)
- {
- bool includeChildren = this.mColor.a != value.a;
- this.mColor = value;
- this.Invalidate(includeChildren);
- }
- }
- }
- public override float alpha
- {
- get
- {
- return this.mColor.a;
- }
- set
- {
- if (this.mColor.a != value)
- {
- this.mColor.a = value;
- this.Invalidate(true);
- }
- }
- }
- public bool isVisible
- {
- get
- {
- return this.mIsVisibleByPanel && this.mIsVisibleByAlpha && this.mIsInFront && this.finalAlpha > 0.001f && NGUITools.GetActive(this);
- }
- }
- public bool hasVertices
- {
- get
- {
- return this.geometry != null && this.geometry.hasVertices;
- }
- }
- public UIWidget.Pivot rawPivot
- {
- get
- {
- return this.mPivot;
- }
- set
- {
- if (this.mPivot != value)
- {
- this.mPivot = value;
- if (this.autoResizeBoxCollider)
- {
- this.ResizeCollider();
- }
- this.MarkAsChanged();
- }
- }
- }
- public UIWidget.Pivot pivot
- {
- get
- {
- return this.mPivot;
- }
- set
- {
- if (this.mPivot != value)
- {
- Vector3 vector = this.worldCorners[0];
- this.mPivot = value;
- this.mChanged = true;
- Vector3 vector2 = this.worldCorners[0];
- Transform cachedTransform = base.cachedTransform;
- Vector3 vector3 = cachedTransform.position;
- float z = cachedTransform.localPosition.z;
- vector3.x += vector.x - vector2.x;
- vector3.y += vector.y - vector2.y;
- base.cachedTransform.position = vector3;
- vector3 = base.cachedTransform.localPosition;
- vector3.x = Mathf.Round(vector3.x);
- vector3.y = Mathf.Round(vector3.y);
- vector3.z = z;
- base.cachedTransform.localPosition = vector3;
- }
- }
- }
- public int depth
- {
- get
- {
- return this.mDepth;
- }
- set
- {
- if (this.mDepth != value)
- {
- if (this.panel != null)
- {
- this.panel.RemoveWidget(this);
- }
- this.mDepth = value;
- if (this.panel != null)
- {
- this.panel.AddWidget(this);
- if (!Application.isPlaying)
- {
- this.panel.SortWidgets();
- this.panel.RebuildAllDrawCalls();
- }
- }
- }
- }
- }
- public int raycastDepth
- {
- get
- {
- if (this.panel == null)
- {
- this.CreatePanel();
- }
- return (!(this.panel != null)) ? this.mDepth : (this.mDepth + this.panel.depth * 1000);
- }
- }
- public override Vector3[] localCorners
- {
- get
- {
- Vector2 pivotOffset = this.pivotOffset;
- float num = -pivotOffset.x * (float)this.mWidth;
- float num2 = -pivotOffset.y * (float)this.mHeight;
- float x = num + (float)this.mWidth;
- float y = num2 + (float)this.mHeight;
- this.mCorners[0] = new Vector3(num, num2);
- this.mCorners[1] = new Vector3(num, y);
- this.mCorners[2] = new Vector3(x, y);
- this.mCorners[3] = new Vector3(x, num2);
- return this.mCorners;
- }
- }
- public virtual Vector2 localSize
- {
- get
- {
- Vector3[] localCorners = this.localCorners;
- return localCorners[2] - localCorners[0];
- }
- }
- public Vector3 localCenter
- {
- get
- {
- Vector3[] localCorners = this.localCorners;
- return Vector3.Lerp(localCorners[0], localCorners[2], 0.5f);
- }
- }
- public override Vector3[] worldCorners
- {
- get
- {
- Vector2 pivotOffset = this.pivotOffset;
- float num = -pivotOffset.x * (float)this.mWidth;
- float num2 = -pivotOffset.y * (float)this.mHeight;
- float x = num + (float)this.mWidth;
- float y = num2 + (float)this.mHeight;
- Transform cachedTransform = base.cachedTransform;
- this.mCorners[0] = cachedTransform.TransformPoint(num, num2, 0f);
- this.mCorners[1] = cachedTransform.TransformPoint(num, y, 0f);
- this.mCorners[2] = cachedTransform.TransformPoint(x, y, 0f);
- this.mCorners[3] = cachedTransform.TransformPoint(x, num2, 0f);
- return this.mCorners;
- }
- }
- public Vector3 worldCenter
- {
- get
- {
- return base.cachedTransform.TransformPoint(this.localCenter);
- }
- }
- public virtual Vector4 drawingDimensions
- {
- get
- {
- Vector2 pivotOffset = this.pivotOffset;
- float num = -pivotOffset.x * (float)this.mWidth;
- float num2 = -pivotOffset.y * (float)this.mHeight;
- float num3 = num + (float)this.mWidth;
- float num4 = num2 + (float)this.mHeight;
- return new Vector4((this.mDrawRegion.x != 0f) ? Mathf.Lerp(num, num3, this.mDrawRegion.x) : num, (this.mDrawRegion.y != 0f) ? Mathf.Lerp(num2, num4, this.mDrawRegion.y) : num2, (this.mDrawRegion.z != 1f) ? Mathf.Lerp(num, num3, this.mDrawRegion.z) : num3, (this.mDrawRegion.w != 1f) ? Mathf.Lerp(num2, num4, this.mDrawRegion.w) : num4);
- }
- }
- public virtual Material material
- {
- get
- {
- return null;
- }
- set
- {
- throw new NotImplementedException(base.GetType() + " has no material setter");
- }
- }
- public virtual Texture mainTexture
- {
- get
- {
- Material material = this.material;
- return (!(material != null)) ? null : material.mainTexture;
- }
- set
- {
- throw new NotImplementedException(base.GetType() + " has no mainTexture setter");
- }
- }
- public virtual Shader shader
- {
- get
- {
- Material material = this.material;
- return (!(material != null)) ? null : material.shader;
- }
- set
- {
- throw new NotImplementedException(base.GetType() + " has no shader setter");
- }
- }
- [Obsolete("There is no relative scale anymore. Widgets now have width and height instead")]
- public Vector2 relativeSize
- {
- get
- {
- return Vector2.one;
- }
- }
- public bool hasBoxCollider
- {
- get
- {
- BoxCollider x = base.GetComponent<Collider>() as BoxCollider;
- return x != null || base.GetComponent<BoxCollider2D>() != null;
- }
- }
- public void SetDimensions(int w, int h)
- {
- if (this.mWidth != w || this.mHeight != h)
- {
- this.mWidth = w;
- this.mHeight = h;
- if (this.keepAspectRatio == UIWidget.AspectRatioSource.BasedOnWidth)
- {
- this.mHeight = Mathf.RoundToInt((float)this.mWidth / this.aspectRatio);
- }
- else if (this.keepAspectRatio == UIWidget.AspectRatioSource.BasedOnHeight)
- {
- this.mWidth = Mathf.RoundToInt((float)this.mHeight * this.aspectRatio);
- }
- else if (this.keepAspectRatio == UIWidget.AspectRatioSource.Free)
- {
- this.aspectRatio = (float)this.mWidth / (float)this.mHeight;
- }
- this.mMoved = true;
- if (this.autoResizeBoxCollider)
- {
- this.ResizeCollider();
- }
- this.MarkAsChanged();
- }
- }
- public override Vector3[] GetSides(Transform relativeTo)
- {
- Vector2 pivotOffset = this.pivotOffset;
- float num = -pivotOffset.x * (float)this.mWidth;
- float num2 = -pivotOffset.y * (float)this.mHeight;
- float num3 = num + (float)this.mWidth;
- float num4 = num2 + (float)this.mHeight;
- float x = (num + num3) * 0.5f;
- float y = (num2 + num4) * 0.5f;
- Transform cachedTransform = base.cachedTransform;
- this.mCorners[0] = cachedTransform.TransformPoint(num, y, 0f);
- this.mCorners[1] = cachedTransform.TransformPoint(x, num4, 0f);
- this.mCorners[2] = cachedTransform.TransformPoint(num3, y, 0f);
- this.mCorners[3] = cachedTransform.TransformPoint(x, num2, 0f);
- if (relativeTo != null)
- {
- for (int i = 0; i < 4; i++)
- {
- this.mCorners[i] = relativeTo.InverseTransformPoint(this.mCorners[i]);
- }
- }
- return this.mCorners;
- }
- public override float CalculateFinalAlpha(int frameID)
- {
- if (this.mAlphaFrameID != frameID)
- {
- this.mAlphaFrameID = frameID;
- this.UpdateFinalAlpha(frameID);
- }
- return this.finalAlpha;
- }
- protected void UpdateFinalAlpha(int frameID)
- {
- if (!this.mIsVisibleByAlpha || !this.mIsInFront)
- {
- this.finalAlpha = 0f;
- }
- else
- {
- UIRect parent = base.parent;
- this.finalAlpha = ((!(base.parent != null)) ? this.mColor.a : (parent.CalculateFinalAlpha(frameID) * this.mColor.a));
- }
- }
- public override void Invalidate(bool includeChildren)
- {
- this.mChanged = true;
- this.mAlphaFrameID = -1;
- if (this.panel != null)
- {
- bool visibleByPanel = (!this.hideIfOffScreen && !this.panel.hasCumulativeClipping) || this.panel.IsVisible(this);
- this.UpdateVisibility(this.CalculateCumulativeAlpha(Time.frameCount) > 0.001f, visibleByPanel);
- this.UpdateFinalAlpha(Time.frameCount);
- if (includeChildren)
- {
- base.Invalidate(true);
- }
- }
- }
- public float CalculateCumulativeAlpha(int frameID)
- {
- UIRect parent = base.parent;
- return (!(parent != null)) ? this.mColor.a : (parent.CalculateFinalAlpha(frameID) * this.mColor.a);
- }
- public override void SetRect(float x, float y, float width, float height)
- {
- Vector2 pivotOffset = this.pivotOffset;
- float num = Mathf.Lerp(x, x + width, pivotOffset.x);
- float num2 = Mathf.Lerp(y, y + height, pivotOffset.y);
- int num3 = Mathf.FloorToInt(width + 0.5f);
- int num4 = Mathf.FloorToInt(height + 0.5f);
- if (pivotOffset.x == 0.5f)
- {
- num3 = num3 >> 1 << 1;
- }
- if (pivotOffset.y == 0.5f)
- {
- num4 = num4 >> 1 << 1;
- }
- Transform transform = base.cachedTransform;
- Vector3 localPosition = transform.localPosition;
- localPosition.x = Mathf.Floor(num + 0.5f);
- localPosition.y = Mathf.Floor(num2 + 0.5f);
- if (num3 < this.minWidth)
- {
- num3 = this.minWidth;
- }
- if (num4 < this.minHeight)
- {
- num4 = this.minHeight;
- }
- transform.localPosition = localPosition;
- this.width = num3;
- this.height = num4;
- if (base.isAnchored)
- {
- transform = transform.parent;
- if (this.leftAnchor.target)
- {
- this.leftAnchor.SetHorizontal(transform, x);
- }
- if (this.rightAnchor.target)
- {
- this.rightAnchor.SetHorizontal(transform, x + width);
- }
- if (this.bottomAnchor.target)
- {
- this.bottomAnchor.SetVertical(transform, y);
- }
- if (this.topAnchor.target)
- {
- this.topAnchor.SetVertical(transform, y + height);
- }
- }
- }
- public void ResizeCollider()
- {
- if (NGUITools.GetActive(this))
- {
- NGUITools.UpdateWidgetCollider(base.gameObject);
- }
- }
- [DebuggerHidden]
- [DebuggerStepThrough]
- public static int FullCompareFunc(UIWidget left, UIWidget right)
- {
- int num = UIPanel.CompareFunc(left.panel, right.panel);
- return (num != 0) ? num : UIWidget.PanelCompareFunc(left, right);
- }
- [DebuggerHidden]
- [DebuggerStepThrough]
- public static int PanelCompareFunc(UIWidget left, UIWidget right)
- {
- if (left.mDepth < right.mDepth)
- {
- return -1;
- }
- if (left.mDepth > right.mDepth)
- {
- return 1;
- }
- Material material = left.material;
- Material material2 = right.material;
- if (material == material2)
- {
- return 0;
- }
- if (material != null)
- {
- return -1;
- }
- if (material2 != null)
- {
- return 1;
- }
- return (material.GetInstanceID() >= material2.GetInstanceID()) ? 1 : -1;
- }
- public Bounds CalculateBounds()
- {
- return this.CalculateBounds(null);
- }
- public Bounds CalculateBounds(Transform relativeParent)
- {
- if (relativeParent == null)
- {
- Vector3[] localCorners = this.localCorners;
- Bounds result = new Bounds(localCorners[0], Vector3.zero);
- for (int i = 1; i < 4; i++)
- {
- result.Encapsulate(localCorners[i]);
- }
- return result;
- }
- Matrix4x4 worldToLocalMatrix = relativeParent.worldToLocalMatrix;
- Vector3[] worldCorners = this.worldCorners;
- Bounds result2 = new Bounds(worldToLocalMatrix.MultiplyPoint3x4(worldCorners[0]), Vector3.zero);
- for (int j = 1; j < 4; j++)
- {
- result2.Encapsulate(worldToLocalMatrix.MultiplyPoint3x4(worldCorners[j]));
- }
- return result2;
- }
- public void SetDirty()
- {
- if (this.drawCall != null)
- {
- this.drawCall.isDirty = true;
- }
- else if (this.isVisible && this.hasVertices)
- {
- this.CreatePanel();
- }
- }
- public void RemoveFromPanel()
- {
- if (this.panel != null)
- {
- this.panel.RemoveWidget(this);
- this.panel = null;
- }
- this.drawCall = null;
- }
- public virtual void MarkAsChanged()
- {
- if (NGUITools.GetActive(this))
- {
- this.mChanged = true;
- if (this.panel != null && base.enabled && NGUITools.GetActive(base.gameObject) && !this.mPlayMode)
- {
- this.SetDirty();
- this.CheckLayer();
- }
- }
- }
- public UIPanel CreatePanel()
- {
- if (this.mStarted && this.panel == null && base.enabled && NGUITools.GetActive(base.gameObject))
- {
- this.panel = UIPanel.Find(base.cachedTransform, true, base.cachedGameObject.layer);
- if (this.panel != null)
- {
- this.mParentFound = false;
- this.panel.AddWidget(this);
- this.CheckLayer();
- this.Invalidate(true);
- }
- }
- return this.panel;
- }
- public void CheckLayer()
- {
- if (this.panel != null && this.panel.gameObject.layer != base.gameObject.layer)
- {
- UnityEngine.Debug.LogWarning("You can't place widgets on a layer different than the UIPanel that manages them.\nIf you want to move widgets to a different layer, parent them to a new panel instead.", this);
- base.gameObject.layer = this.panel.gameObject.layer;
- }
- }
- public override void ParentHasChanged()
- {
- base.ParentHasChanged();
- if (this.panel != null)
- {
- UIPanel y = UIPanel.Find(base.cachedTransform, true, base.cachedGameObject.layer);
- if (this.panel != y)
- {
- this.RemoveFromPanel();
- this.CreatePanel();
- }
- }
- }
- protected virtual void Awake()
- {
- this.mGo = base.gameObject;
- this.mPlayMode = Application.isPlaying;
- }
- protected override void OnInit()
- {
- base.OnInit();
- this.RemoveFromPanel();
- this.mMoved = true;
- if (this.mWidth == 100 && this.mHeight == 100 && base.cachedTransform.localScale.magnitude > 8f)
- {
- this.UpgradeFrom265();
- base.cachedTransform.localScale = Vector3.one;
- }
- base.Update();
- }
- protected virtual void UpgradeFrom265()
- {
- Vector3 localScale = base.cachedTransform.localScale;
- this.mWidth = Mathf.Abs(Mathf.RoundToInt(localScale.x));
- this.mHeight = Mathf.Abs(Mathf.RoundToInt(localScale.y));
- NGUITools.UpdateWidgetCollider(base.gameObject, true);
- }
- protected override void OnStart()
- {
- this.CreatePanel();
- }
- protected override void OnAnchor()
- {
- Transform cachedTransform = base.cachedTransform;
- Transform parent = cachedTransform.parent;
- Vector3 localPosition = cachedTransform.localPosition;
- Vector2 pivotOffset = this.pivotOffset;
- float num;
- float num2;
- float num3;
- float num4;
- if (this.leftAnchor.target == this.bottomAnchor.target && this.leftAnchor.target == this.rightAnchor.target && this.leftAnchor.target == this.topAnchor.target)
- {
- Vector3[] sides = this.leftAnchor.GetSides(parent);
- if (sides != null)
- {
- num = NGUIMath.Lerp(sides[0].x, sides[2].x, this.leftAnchor.relative) + (float)this.leftAnchor.absolute;
- num2 = NGUIMath.Lerp(sides[0].x, sides[2].x, this.rightAnchor.relative) + (float)this.rightAnchor.absolute;
- num3 = NGUIMath.Lerp(sides[3].y, sides[1].y, this.bottomAnchor.relative) + (float)this.bottomAnchor.absolute;
- num4 = NGUIMath.Lerp(sides[3].y, sides[1].y, this.topAnchor.relative) + (float)this.topAnchor.absolute;
- this.mIsInFront = true;
- }
- else
- {
- Vector3 localPos = base.GetLocalPos(this.leftAnchor, parent);
- num = localPos.x + (float)this.leftAnchor.absolute;
- num3 = localPos.y + (float)this.bottomAnchor.absolute;
- num2 = localPos.x + (float)this.rightAnchor.absolute;
- num4 = localPos.y + (float)this.topAnchor.absolute;
- this.mIsInFront = (!this.hideIfOffScreen || localPos.z >= 0f);
- }
- }
- else
- {
- this.mIsInFront = true;
- if (this.leftAnchor.target)
- {
- Vector3[] sides2 = this.leftAnchor.GetSides(parent);
- if (sides2 != null)
- {
- num = NGUIMath.Lerp(sides2[0].x, sides2[2].x, this.leftAnchor.relative) + (float)this.leftAnchor.absolute;
- }
- else
- {
- num = base.GetLocalPos(this.leftAnchor, parent).x + (float)this.leftAnchor.absolute;
- }
- }
- else
- {
- num = localPosition.x - pivotOffset.x * (float)this.mWidth;
- }
- if (this.rightAnchor.target)
- {
- Vector3[] sides3 = this.rightAnchor.GetSides(parent);
- if (sides3 != null)
- {
- num2 = NGUIMath.Lerp(sides3[0].x, sides3[2].x, this.rightAnchor.relative) + (float)this.rightAnchor.absolute;
- }
- else
- {
- num2 = base.GetLocalPos(this.rightAnchor, parent).x + (float)this.rightAnchor.absolute;
- }
- }
- else
- {
- num2 = localPosition.x - pivotOffset.x * (float)this.mWidth + (float)this.mWidth;
- }
- if (this.bottomAnchor.target)
- {
- Vector3[] sides4 = this.bottomAnchor.GetSides(parent);
- if (sides4 != null)
- {
- num3 = NGUIMath.Lerp(sides4[3].y, sides4[1].y, this.bottomAnchor.relative) + (float)this.bottomAnchor.absolute;
- }
- else
- {
- num3 = base.GetLocalPos(this.bottomAnchor, parent).y + (float)this.bottomAnchor.absolute;
- }
- }
- else
- {
- num3 = localPosition.y - pivotOffset.y * (float)this.mHeight;
- }
- if (this.topAnchor.target)
- {
- Vector3[] sides5 = this.topAnchor.GetSides(parent);
- if (sides5 != null)
- {
- num4 = NGUIMath.Lerp(sides5[3].y, sides5[1].y, this.topAnchor.relative) + (float)this.topAnchor.absolute;
- }
- else
- {
- num4 = base.GetLocalPos(this.topAnchor, parent).y + (float)this.topAnchor.absolute;
- }
- }
- else
- {
- num4 = localPosition.y - pivotOffset.y * (float)this.mHeight + (float)this.mHeight;
- }
- }
- Vector3 vector = new Vector3(Mathf.Lerp(num, num2, pivotOffset.x), Mathf.Lerp(num3, num4, pivotOffset.y), localPosition.z);
- int num5 = Mathf.FloorToInt(num2 - num + 0.5f);
- int num6 = Mathf.FloorToInt(num4 - num3 + 0.5f);
- if (this.keepAspectRatio != UIWidget.AspectRatioSource.Free && this.aspectRatio != 0f)
- {
- if (this.keepAspectRatio == UIWidget.AspectRatioSource.BasedOnHeight)
- {
- num5 = Mathf.RoundToInt((float)num6 * this.aspectRatio);
- }
- else
- {
- num6 = Mathf.RoundToInt((float)num5 / this.aspectRatio);
- }
- }
- if (num5 < this.minWidth)
- {
- num5 = this.minWidth;
- }
- if (num6 < this.minHeight)
- {
- num6 = this.minHeight;
- }
- if (Vector3.SqrMagnitude(localPosition - vector) > 0.001f)
- {
- base.cachedTransform.localPosition = vector;
- if (this.mIsInFront)
- {
- this.mChanged = true;
- }
- }
- if (this.mWidth != num5 || this.mHeight != num6)
- {
- this.mWidth = num5;
- this.mHeight = num6;
- if (this.mIsInFront)
- {
- this.mChanged = true;
- }
- if (this.autoResizeBoxCollider)
- {
- this.ResizeCollider();
- }
- }
- }
- protected override void OnUpdate()
- {
- if (this.panel == null)
- {
- this.CreatePanel();
- }
- }
- private void OnApplicationPause(bool paused)
- {
- if (!paused)
- {
- this.MarkAsChanged();
- }
- }
- protected override void OnDisable()
- {
- this.RemoveFromPanel();
- base.OnDisable();
- }
- private void OnDestroy()
- {
- this.RemoveFromPanel();
- }
- public bool UpdateVisibility(bool visibleByAlpha, bool visibleByPanel)
- {
- if (this.mIsVisibleByAlpha != visibleByAlpha || this.mIsVisibleByPanel != visibleByPanel)
- {
- this.mChanged = true;
- this.mIsVisibleByAlpha = visibleByAlpha;
- this.mIsVisibleByPanel = visibleByPanel;
- return true;
- }
- return false;
- }
- public bool UpdateTransform(int frame)
- {
- if (!this.mMoved && !this.panel.widgetsAreStatic && base.cachedTransform.hasChanged)
- {
- this.mTrans.hasChanged = false;
- this.mLocalToPanel = this.panel.worldToLocal * base.cachedTransform.localToWorldMatrix;
- this.mMatrixFrame = frame;
- Vector2 pivotOffset = this.pivotOffset;
- float num = -pivotOffset.x * (float)this.mWidth;
- float num2 = -pivotOffset.y * (float)this.mHeight;
- float x = num + (float)this.mWidth;
- float y = num2 + (float)this.mHeight;
- Transform cachedTransform = base.cachedTransform;
- Vector3 vector = cachedTransform.TransformPoint(num, num2, 0f);
- Vector3 vector2 = cachedTransform.TransformPoint(x, y, 0f);
- vector = this.panel.worldToLocal.MultiplyPoint3x4(vector);
- vector2 = this.panel.worldToLocal.MultiplyPoint3x4(vector2);
- if (Vector3.SqrMagnitude(this.mOldV0 - vector) > 1E-06f || Vector3.SqrMagnitude(this.mOldV1 - vector2) > 1E-06f)
- {
- this.mMoved = true;
- this.mOldV0 = vector;
- this.mOldV1 = vector2;
- }
- }
- if (this.mMoved && this.onChange != null)
- {
- this.onChange();
- }
- return this.mMoved || this.mChanged;
- }
- public bool UpdateGeometry(int frame)
- {
- float num = this.CalculateFinalAlpha(frame);
- if (this.mIsVisibleByAlpha && this.mLastAlpha != num)
- {
- this.mChanged = true;
- }
- this.mLastAlpha = num;
- if (this.mChanged)
- {
- this.mChanged = false;
- if (this.mIsVisibleByAlpha && num > 0.001f && this.shader != null)
- {
- bool hasVertices = this.geometry.hasVertices;
- if (this.fillGeometry)
- {
- this.geometry.Clear();
- this.OnFill(this.geometry.verts, this.geometry.uvs, this.geometry.cols);
- }
- if (this.geometry.hasVertices)
- {
- if (this.mMatrixFrame != frame)
- {
- this.mLocalToPanel = this.panel.worldToLocal * base.cachedTransform.localToWorldMatrix;
- this.mMatrixFrame = frame;
- }
- this.geometry.ApplyTransform(this.mLocalToPanel);
- this.mMoved = false;
- return true;
- }
- return hasVertices;
- }
- else if (this.geometry.hasVertices)
- {
- if (this.fillGeometry)
- {
- this.geometry.Clear();
- }
- this.mMoved = false;
- return true;
- }
- }
- else if (this.mMoved && this.geometry.hasVertices)
- {
- if (this.mMatrixFrame != frame)
- {
- this.mLocalToPanel = this.panel.worldToLocal * base.cachedTransform.localToWorldMatrix;
- this.mMatrixFrame = frame;
- }
- this.geometry.ApplyTransform(this.mLocalToPanel);
- this.mMoved = false;
- return true;
- }
- this.mMoved = false;
- return false;
- }
- public void WriteToBuffers(BetterList<Vector3> v, BetterList<Vector2> u, BetterList<Color32> c, BetterList<Vector3> n, BetterList<Vector4> t)
- {
- this.geometry.WriteToBuffers(v, u, c, n, t);
- }
- public virtual void MakePixelPerfect()
- {
- Vector3 localPosition = base.cachedTransform.localPosition;
- localPosition.z = Mathf.Round(localPosition.z);
- localPosition.x = Mathf.Round(localPosition.x);
- localPosition.y = Mathf.Round(localPosition.y);
- base.cachedTransform.localPosition = localPosition;
- Vector3 localScale = base.cachedTransform.localScale;
- base.cachedTransform.localScale = new Vector3(Mathf.Sign(localScale.x), Mathf.Sign(localScale.y), 1f);
- }
- public virtual int minWidth
- {
- get
- {
- return 2;
- }
- }
- public virtual int minHeight
- {
- get
- {
- return 2;
- }
- }
- public virtual Vector4 border
- {
- get
- {
- return Vector4.zero;
- }
- set
- {
- }
- }
- public virtual void OnFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
- {
- }
- [HideInInspector]
- [SerializeField]
- protected Color mColor = Color.white;
- [HideInInspector]
- [SerializeField]
- protected UIWidget.Pivot mPivot = UIWidget.Pivot.Center;
- [HideInInspector]
- [SerializeField]
- protected int mWidth = 100;
- [HideInInspector]
- [SerializeField]
- protected int mHeight = 100;
- [HideInInspector]
- [SerializeField]
- protected int mDepth;
- public UIWidget.OnDimensionsChanged onChange;
- public UIWidget.OnPostFillCallback onPostFill;
- public UIDrawCall.OnRenderCallback mOnRender;
- public bool autoResizeBoxCollider;
- public bool hideIfOffScreen;
- public UIWidget.AspectRatioSource keepAspectRatio;
- public float aspectRatio = 1f;
- public UIWidget.HitCheck hitCheck;
- [NonSerialized]
- public UIPanel panel;
- [NonSerialized]
- public UIGeometry geometry = new UIGeometry();
- [NonSerialized]
- public bool fillGeometry = true;
- [NonSerialized]
- protected bool mPlayMode = true;
- [NonSerialized]
- protected Vector4 mDrawRegion = new Vector4(0f, 0f, 1f, 1f);
- [NonSerialized]
- private Matrix4x4 mLocalToPanel;
- [NonSerialized]
- private bool mIsVisibleByAlpha = true;
- [NonSerialized]
- private bool mIsVisibleByPanel = true;
- [NonSerialized]
- private bool mIsInFront = true;
- [NonSerialized]
- private float mLastAlpha;
- [NonSerialized]
- private bool mMoved;
- [NonSerialized]
- public UIDrawCall drawCall;
- [NonSerialized]
- protected Vector3[] mCorners = new Vector3[4];
- [NonSerialized]
- private int mAlphaFrameID = -1;
- private int mMatrixFrame = -1;
- private Vector3 mOldV0;
- private Vector3 mOldV1;
- public enum Pivot
- {
- TopLeft,
- Top,
- TopRight,
- Left,
- Center,
- Right,
- BottomLeft,
- Bottom,
- BottomRight
- }
- public delegate void OnDimensionsChanged();
- public delegate void OnPostFillCallback(UIWidget widget, int bufferOffset, BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols);
- public enum AspectRatioSource
- {
- Free,
- BasedOnWidth,
- BasedOnHeight
- }
- public delegate bool HitCheck(Vector3 worldPos);
- }
|