123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- [ExecuteInEditMode]
- [AddComponentMenu("NGUI/UI/NGUI Font")]
- public class UIFont : MonoBehaviour
- {
- public BMFont bmFont
- {
- get
- {
- return (!(this.mReplacement != null)) ? this.mFont : this.mReplacement.bmFont;
- }
- set
- {
- if (this.mReplacement != null)
- {
- this.mReplacement.bmFont = value;
- }
- else
- {
- this.mFont = value;
- }
- }
- }
- public int texWidth
- {
- get
- {
- return (!(this.mReplacement != null)) ? ((this.mFont == null) ? 1 : this.mFont.texWidth) : this.mReplacement.texWidth;
- }
- set
- {
- if (this.mReplacement != null)
- {
- this.mReplacement.texWidth = value;
- }
- else if (this.mFont != null)
- {
- this.mFont.texWidth = value;
- }
- }
- }
- public int texHeight
- {
- get
- {
- return (!(this.mReplacement != null)) ? ((this.mFont == null) ? 1 : this.mFont.texHeight) : this.mReplacement.texHeight;
- }
- set
- {
- if (this.mReplacement != null)
- {
- this.mReplacement.texHeight = value;
- }
- else if (this.mFont != null)
- {
- this.mFont.texHeight = value;
- }
- }
- }
- public bool hasSymbols
- {
- get
- {
- return (!(this.mReplacement != null)) ? (this.mSymbols != null && this.mSymbols.Count != 0) : this.mReplacement.hasSymbols;
- }
- }
- public List<BMSymbol> symbols
- {
- get
- {
- return (!(this.mReplacement != null)) ? this.mSymbols : this.mReplacement.symbols;
- }
- }
- public UIAtlas atlas
- {
- get
- {
- return (!(this.mReplacement != null)) ? this.mAtlas : this.mReplacement.atlas;
- }
- set
- {
- if (this.mReplacement != null)
- {
- this.mReplacement.atlas = value;
- }
- else if (this.mAtlas != value)
- {
- if (value == null)
- {
- if (this.mAtlas != null)
- {
- this.mMat = this.mAtlas.spriteMaterial;
- }
- if (this.sprite != null)
- {
- this.mUVRect = this.uvRect;
- }
- }
- this.mPMA = -1;
- this.mAtlas = value;
- this.MarkAsChanged();
- }
- }
- }
- public Material material
- {
- get
- {
- if (this.mReplacement != null)
- {
- return this.mReplacement.material;
- }
- if (this.mAtlas != null)
- {
- return this.mAtlas.spriteMaterial;
- }
- if (this.mMat != null)
- {
- if (this.mDynamicFont != null && this.mMat != this.mDynamicFont.material)
- {
- this.mMat.mainTexture = this.mDynamicFont.material.mainTexture;
- }
- return this.mMat;
- }
- if (this.mDynamicFont != null)
- {
- return this.mDynamicFont.material;
- }
- return null;
- }
- set
- {
- if (this.mReplacement != null)
- {
- this.mReplacement.material = value;
- }
- else if (this.mMat != value)
- {
- this.mPMA = -1;
- this.mMat = value;
- this.MarkAsChanged();
- }
- }
- }
- [Obsolete("Use UIFont.premultipliedAlphaShader instead")]
- public bool premultipliedAlpha
- {
- get
- {
- return this.premultipliedAlphaShader;
- }
- }
- public bool premultipliedAlphaShader
- {
- get
- {
- if (this.mReplacement != null)
- {
- return this.mReplacement.premultipliedAlphaShader;
- }
- if (this.mAtlas != null)
- {
- return this.mAtlas.premultipliedAlpha;
- }
- if (this.mPMA == -1)
- {
- Material material = this.material;
- this.mPMA = ((!(material != null) || !(material.shader != null) || !material.shader.name.Contains("Premultiplied")) ? 0 : 1);
- }
- return this.mPMA == 1;
- }
- }
- public bool packedFontShader
- {
- get
- {
- if (this.mReplacement != null)
- {
- return this.mReplacement.packedFontShader;
- }
- if (this.mAtlas != null)
- {
- return false;
- }
- if (this.mPacked == -1)
- {
- Material material = this.material;
- this.mPacked = ((!(material != null) || !(material.shader != null) || !material.shader.name.Contains("Packed")) ? 0 : 1);
- }
- return this.mPacked == 1;
- }
- }
- public Texture2D texture
- {
- get
- {
- if (this.mReplacement != null)
- {
- return this.mReplacement.texture;
- }
- Material material = this.material;
- return (!(material != null)) ? null : (material.mainTexture as Texture2D);
- }
- }
- public Rect uvRect
- {
- get
- {
- if (this.mReplacement != null)
- {
- return this.mReplacement.uvRect;
- }
- return (!(this.mAtlas != null) || this.sprite == null) ? new Rect(0f, 0f, 1f, 1f) : this.mUVRect;
- }
- set
- {
- if (this.mReplacement != null)
- {
- this.mReplacement.uvRect = value;
- }
- else if (this.sprite == null && this.mUVRect != value)
- {
- this.mUVRect = value;
- this.MarkAsChanged();
- }
- }
- }
- public string spriteName
- {
- get
- {
- return (!(this.mReplacement != null)) ? this.mFont.spriteName : this.mReplacement.spriteName;
- }
- set
- {
- if (this.mReplacement != null)
- {
- this.mReplacement.spriteName = value;
- }
- else if (this.mFont.spriteName != value)
- {
- this.mFont.spriteName = value;
- this.MarkAsChanged();
- }
- }
- }
- public bool isValid
- {
- get
- {
- return this.mDynamicFont != null || this.mFont.isValid;
- }
- }
- [Obsolete("Use UIFont.defaultSize instead")]
- public int size
- {
- get
- {
- return this.defaultSize;
- }
- set
- {
- this.defaultSize = value;
- }
- }
- public int defaultSize
- {
- get
- {
- if (this.mReplacement != null)
- {
- return this.mReplacement.defaultSize;
- }
- if (this.isDynamic || this.mFont == null)
- {
- return this.mDynamicFontSize;
- }
- return this.mFont.charSize;
- }
- set
- {
- if (this.mReplacement != null)
- {
- this.mReplacement.defaultSize = value;
- }
- else
- {
- this.mDynamicFontSize = value;
- }
- }
- }
- public UISpriteData sprite
- {
- get
- {
- if (this.mReplacement != null)
- {
- return this.mReplacement.sprite;
- }
- if (this.mSprite == null && this.mAtlas != null && !string.IsNullOrEmpty(this.mFont.spriteName))
- {
- this.mSprite = this.mAtlas.GetSprite(this.mFont.spriteName);
- if (this.mSprite == null)
- {
- this.mSprite = this.mAtlas.GetSprite(base.name);
- }
- if (this.mSprite == null)
- {
- this.mFont.spriteName = null;
- }
- else
- {
- this.UpdateUVRect();
- }
- int i = 0;
- int count = this.mSymbols.Count;
- while (i < count)
- {
- this.symbols[i].MarkAsChanged();
- i++;
- }
- }
- return this.mSprite;
- }
- }
- public UIFont replacement
- {
- get
- {
- return this.mReplacement;
- }
- set
- {
- UIFont uifont = value;
- if (uifont == this)
- {
- uifont = null;
- }
- if (this.mReplacement != uifont)
- {
- if (uifont != null && uifont.replacement == this)
- {
- uifont.replacement = null;
- }
- if (this.mReplacement != null)
- {
- this.MarkAsChanged();
- }
- this.mReplacement = uifont;
- if (uifont != null)
- {
- this.mPMA = -1;
- this.mMat = null;
- this.mFont = null;
- this.mDynamicFont = null;
- }
- this.MarkAsChanged();
- }
- }
- }
- public bool isDynamic
- {
- get
- {
- return (!(this.mReplacement != null)) ? (this.mDynamicFont != null) : this.mReplacement.isDynamic;
- }
- }
- public Font dynamicFont
- {
- get
- {
- return (!(this.mReplacement != null)) ? this.mDynamicFont : this.mReplacement.dynamicFont;
- }
- set
- {
- if (this.mReplacement != null)
- {
- this.mReplacement.dynamicFont = value;
- }
- else if (this.mDynamicFont != value)
- {
- if (this.mDynamicFont != null)
- {
- this.material = null;
- }
- this.mDynamicFont = value;
- this.MarkAsChanged();
- }
- }
- }
- public FontStyle dynamicFontStyle
- {
- get
- {
- return (!(this.mReplacement != null)) ? this.mDynamicFontStyle : this.mReplacement.dynamicFontStyle;
- }
- set
- {
- if (this.mReplacement != null)
- {
- this.mReplacement.dynamicFontStyle = value;
- }
- else if (this.mDynamicFontStyle != value)
- {
- this.mDynamicFontStyle = value;
- this.MarkAsChanged();
- }
- }
- }
- private void Trim()
- {
- Texture texture = this.mAtlas.texture;
- if (texture != null && this.mSprite != null)
- {
- Rect rect = NGUIMath.ConvertToPixels(this.mUVRect, this.texture.width, this.texture.height, true);
- Rect rect2 = new Rect((float)this.mSprite.x, (float)this.mSprite.y, (float)this.mSprite.width, (float)this.mSprite.height);
- int xMin = Mathf.RoundToInt(rect2.xMin - rect.xMin);
- int yMin = Mathf.RoundToInt(rect2.yMin - rect.yMin);
- int xMax = Mathf.RoundToInt(rect2.xMax - rect.xMin);
- int yMax = Mathf.RoundToInt(rect2.yMax - rect.yMin);
- this.mFont.Trim(xMin, yMin, xMax, yMax);
- }
- }
- private bool References(UIFont font)
- {
- return !(font == null) && (font == this || (this.mReplacement != null && this.mReplacement.References(font)));
- }
- public static bool CheckIfRelated(UIFont a, UIFont b)
- {
- return !(a == null) && !(b == null) && ((a.isDynamic && b.isDynamic && a.dynamicFont.fontNames[0] == b.dynamicFont.fontNames[0]) || a == b || a.References(b) || b.References(a));
- }
- private Texture dynamicTexture
- {
- get
- {
- if (this.mReplacement)
- {
- return this.mReplacement.dynamicTexture;
- }
- if (this.isDynamic)
- {
- return this.mDynamicFont.material.mainTexture;
- }
- return null;
- }
- }
- public void MarkAsChanged()
- {
- if (this.mReplacement != null)
- {
- this.mReplacement.MarkAsChanged();
- }
- this.mSprite = null;
- UILabel[] array = NGUITools.FindActive<UILabel>();
- int i = 0;
- int num = array.Length;
- while (i < num)
- {
- UILabel uilabel = array[i];
- if (uilabel.enabled && NGUITools.GetActive(uilabel.gameObject) && UIFont.CheckIfRelated(this, uilabel.bitmapFont))
- {
- UIFont bitmapFont = uilabel.bitmapFont;
- uilabel.bitmapFont = null;
- uilabel.bitmapFont = bitmapFont;
- }
- i++;
- }
- int j = 0;
- int count = this.symbols.Count;
- while (j < count)
- {
- this.symbols[j].MarkAsChanged();
- j++;
- }
- }
- public void UpdateUVRect()
- {
- if (this.mAtlas == null)
- {
- return;
- }
- Texture texture = this.mAtlas.texture;
- if (texture != null)
- {
- this.mUVRect = new Rect((float)(this.mSprite.x - this.mSprite.paddingLeft), (float)(this.mSprite.y - this.mSprite.paddingTop), (float)(this.mSprite.width + this.mSprite.paddingLeft + this.mSprite.paddingRight), (float)(this.mSprite.height + this.mSprite.paddingTop + this.mSprite.paddingBottom));
- this.mUVRect = NGUIMath.ConvertToTexCoords(this.mUVRect, texture.width, texture.height);
- if (this.mSprite.hasPadding)
- {
- this.Trim();
- }
- }
- }
- private BMSymbol GetSymbol(string sequence, bool createIfMissing)
- {
- int i = 0;
- int count = this.mSymbols.Count;
- while (i < count)
- {
- BMSymbol bmsymbol = this.mSymbols[i];
- if (bmsymbol.sequence == sequence)
- {
- return bmsymbol;
- }
- i++;
- }
- if (createIfMissing)
- {
- BMSymbol bmsymbol2 = new BMSymbol();
- bmsymbol2.sequence = sequence;
- this.mSymbols.Add(bmsymbol2);
- return bmsymbol2;
- }
- return null;
- }
- public BMSymbol MatchSymbol(string text, int offset, int textLength)
- {
- int count = this.mSymbols.Count;
- if (count == 0)
- {
- return null;
- }
- textLength -= offset;
- for (int i = 0; i < count; i++)
- {
- BMSymbol bmsymbol = this.mSymbols[i];
- int length = bmsymbol.length;
- if (length != 0 && textLength >= length)
- {
- bool flag = true;
- for (int j = 0; j < length; j++)
- {
- if (text[offset + j] != bmsymbol.sequence[j])
- {
- flag = false;
- break;
- }
- }
- if (flag && bmsymbol.Validate(this.atlas))
- {
- return bmsymbol;
- }
- }
- }
- return null;
- }
- public void AddSymbol(string sequence, string spriteName)
- {
- BMSymbol symbol = this.GetSymbol(sequence, true);
- symbol.spriteName = spriteName;
- this.MarkAsChanged();
- }
- public void RemoveSymbol(string sequence)
- {
- BMSymbol symbol = this.GetSymbol(sequence, false);
- if (symbol != null)
- {
- this.symbols.Remove(symbol);
- }
- this.MarkAsChanged();
- }
- public void RenameSymbol(string before, string after)
- {
- BMSymbol symbol = this.GetSymbol(before, false);
- if (symbol != null)
- {
- symbol.sequence = after;
- }
- this.MarkAsChanged();
- }
- public bool UsesSprite(string s)
- {
- if (!string.IsNullOrEmpty(s))
- {
- if (s.Equals(this.spriteName))
- {
- return true;
- }
- int i = 0;
- int count = this.symbols.Count;
- while (i < count)
- {
- BMSymbol bmsymbol = this.symbols[i];
- if (s.Equals(bmsymbol.spriteName))
- {
- return true;
- }
- i++;
- }
- }
- return false;
- }
- [HideInInspector]
- [SerializeField]
- private Material mMat;
- [HideInInspector]
- [SerializeField]
- private Rect mUVRect = new Rect(0f, 0f, 1f, 1f);
- [HideInInspector]
- [SerializeField]
- private BMFont mFont = new BMFont();
- [HideInInspector]
- [SerializeField]
- private UIAtlas mAtlas;
- [HideInInspector]
- [SerializeField]
- private UIFont mReplacement;
- [HideInInspector]
- [SerializeField]
- private List<BMSymbol> mSymbols = new List<BMSymbol>();
- [HideInInspector]
- [SerializeField]
- private Font mDynamicFont;
- [HideInInspector]
- [SerializeField]
- private int mDynamicFontSize = 16;
- [HideInInspector]
- [SerializeField]
- private FontStyle mDynamicFontStyle;
- [NonSerialized]
- private UISpriteData mSprite;
- private int mPMA = -1;
- private int mPacked = -1;
- }
|