123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- using CommonLang.Xml;
- using CommonUnity3D.UGUI;
- using System;
- using System.Xml;
- using UnityEngine;
- using CommonUI.Data;
- using TextAnchor = CommonUI.Data.TextAnchor;
- using FontStyle = CommonUI.Data.FontStyle;
- using CommonUI.Cell;
- using CommonLang.Property;
- namespace CommonUnity3D.UGUIEditor.UI
- {
- public abstract class UETextComponent : UIComponent
- {
- protected DisplayText mTextSprite;
- protected ITextComponent mTextGraphics;
- protected readonly bool mUseBitmapFont;
- protected UETextComponent(bool use_bitmap)
- {
- this.mUseBitmapFont = use_bitmap;
- }
- public DisplayText TextSprite
- {
- get { return mTextSprite; }
- }
- public ITextComponent TextGraphics
- {
- get { return mTextGraphics; }
- }
- public virtual bool IsNoLayout
- {
- get
- {
- if (MetaData != null)
- {
- if (MetaData.Layout == null || MetaData.Layout.Style == UILayoutStyle.NULL)
- {
- return true;
- }
- return false;
- }
- return Layout != null;
- }
- }
- //-------------------------------------------------------------------------------------------------------
- public string Text
- {
- get
- {
- if (mTextGraphics != null)
- return mTextGraphics.Text;
- else
- return mTextSprite.Text;
- }
- set
- {
- if (IsDispose) return;
- if (value != this.Text)
- {
- if (mTextGraphics != null)
- mTextGraphics.Text = value;
- else
- mTextSprite.Text = value;
- DoTextChanged();
- }
- }
- }
- public TextAnchor EditTextAnchor
- {
- get
- {
- if (mTextGraphics != null)
- return mTextGraphics.Anchor;
- else
- return mTextSprite.Anchor;
- }
- set
- {
- if (mTextGraphics != null)
- mTextGraphics.Anchor = value;
- else
- mTextSprite.Anchor = value;
- }
- }
- public int FontSize
- {
- get
- {
- if (mTextGraphics != null)
- return mTextGraphics.FontSize;
- else
- return mTextSprite.FontSize;
- }
- set
- {
- if (mTextGraphics != null)
- mTextGraphics.FontSize = value;
- else
- mTextSprite.FontSize = value;
- }
- }
- public UnityEngine.Color FontColor
- {
- get
- {
- if (mTextGraphics != null)
- return mTextGraphics.FontColor;
- else
- return mTextSprite.FontColor;
- }
- set
- {
- if (mTextGraphics != null)
- mTextGraphics.FontColor = value;
- else
- mTextSprite.FontColor = value;
- }
- }
- public Vector2 TextOffset
- {
- get
- {
- if (mTextGraphics != null)
- return mTextGraphics.TextOffset;
- else
- return mTextSprite.TextOffset;
- }
- set
- {
- if (mTextGraphics != null)
- mTextGraphics.TextOffset = value;
- else
- mTextSprite.TextOffset = value;
- }
- }
- public Vector2 PreferredSize
- {
- get
- {
- if (mTextGraphics != null)
- return mTextGraphics.PreferredSize;
- else
- return mTextSprite.PreferredSize;
- }
- }
- public void SetBorder(UnityEngine.Color bc, Vector2 distance)
- {
- if (mTextGraphics != null)
- mTextGraphics.SetBorder(bc, distance);
- else
- mTextSprite.SetBorder(bc, distance);
- }
- //-------------------------------------------------------------------------------------------------------
- protected override UILayoutGraphics GenLayoutGraphics()
- {
- if (IsNoLayout) return null;
- return base.GenLayoutGraphics();
- }
- protected override void OnStart()
- {
- base.OnStart();
- this.DoTextChanged();
- }
- protected override void OnUpdate()
- {
- base.OnUpdate();
- if (mTextSprite != null)
- {
- mTextSprite.Size2D = this.Size2D;
- }
- }
- protected override void DecodeEnd(UIEditor.Decoder editor, UIComponentMeta e)
- {
- base.DecodeEnd(editor, e);
- var ue = e as UETextComponentMeta;
- if (IsNoLayout)
- {
- //无底图//
- if (!string.IsNullOrEmpty(ue.ImageFont))
- {
- this.Decode_ImageFontGraphics(editor, ue);
- }
- else if (mUseBitmapFont)
- {
- this.Decode_BitmapTextGraphics(editor, ue);
- }
- else
- {
- this.Decode_TextGraphics(editor, ue);
- }
- this.mTextGraphics.Text = ue.text;
- this.mTextGraphics.TextOffset = new Vector2(ue.text_offset_x, ue.text_offset_y);
- this.mTextGraphics.Anchor = ue.text_anchor;
- this.mTextGraphics.FontColor = UIUtils.UInt32_ARGB_To_Color(ue.textColor);
- }
- else
- {
- //有底图//
- if (!string.IsNullOrEmpty(ue.ImageFont))
- {
- this.Decode_ImageFont(editor, ue);
- }
- else if (mUseBitmapFont)
- {
- this.Decode_BitmapText(editor, ue);
- }
- else
- {
- this.Decode_Text(editor, ue);
- }
- this.mTextSprite.Text = ue.text;
- this.mTextSprite.TextOffset = new Vector2(ue.text_offset_x, ue.text_offset_y);
- this.mTextSprite.Anchor = ue.text_anchor;
- this.mTextSprite.FontColor = UIUtils.UInt32_ARGB_To_Color(ue.textColor);
- this.mTextSprite.Size2D = this.Size2D;
- }
- this.Enable = false;
- this.EnableChildren = false;
- }
- private void Decode_Text(UIEditor.Decoder editor, UETextComponentMeta e)
- {
- var text = new TextSprite("text");
- mTextSprite = text;
- AddChild(mTextSprite);
- text.Graphics.horizontalOverflow = HorizontalWrapMode.Overflow;
- text.Graphics.verticalOverflow = VerticalWrapMode.Overflow;
- if (e.textFontSize > 0)
- {
- text.FontSize = e.textFontSize;
- text.Graphics.resizeTextForBestFit = false;
- }
- else
- {
- text.Graphics.resizeTextForBestFit = true;
- }
- if (!string.IsNullOrEmpty(e.textFontName))
- {
- text.SetTextFont(editor.CreateFont(e.textFontName), this.FontSize, (UnityEngine.FontStyle)e.textFontStyle);
- }
- if (e.textBorderAlpha > 0)
- {
- Color border_color = UIUtils.UInt32_ARGB_To_Color(e.textBorderColor);
- border_color.a = e.textBorderAlpha / 100f;
- text.AddBorder(border_color, new Vector2(1, -1));
- }
- }
- private void Decode_ImageFont(UIEditor.Decoder editor, UETextComponentMeta e)
- {
- var text = new ImageFontSprite("image_font");
- mTextSprite = text;
- AddChild(mTextSprite);
- editor.editor.ParseImageFont(e.ImageFont, e.text, text.Graphics);
- }
- private void Decode_BitmapText(UIEditor.Decoder editor, UETextComponentMeta e)
- {
- var text = new BitmapTextSprite("bitmap_text");
- mTextSprite = text;
- AddChild(mTextSprite);
- text.Anchor = e.text_anchor;
- text.Style = e.textFontStyle;
- text.FontColor = UIUtils.UInt32_ARGB_To_Color(e.textColor);
- if (e.textFontSize > 0)
- {
- text.FontSize = e.textFontSize;
- }
- if (e.textBorderAlpha > 0)
- {
- Color border_color = UIUtils.UInt32_ARGB_To_Color(e.textBorderColor);
- border_color.a = e.textBorderAlpha / 100f;
- text.Graphics.BorderTime = TextBorderCount.Border;
- text.Graphics.BorderColor = border_color;
- }
- }
- private void Decode_TextGraphics(UIEditor.Decoder editor, UETextComponentMeta e)
- {
- var gfx = base.mGameObject.AddComponent<TextGraphics>();
- this.mTextGraphics = gfx;
- if (e.textFontSize > 0)
- {
- gfx.FontSize = e.textFontSize;
- gfx.resizeTextForBestFit = false;
- }
- else
- {
- gfx.resizeTextForBestFit = true;
- }
- if (!string.IsNullOrEmpty(e.textFontName))
- {
- gfx.SetTextFont(editor.CreateFont(e.textFontName), this.FontSize, (UnityEngine.FontStyle)e.textFontStyle);
- }
- gfx.FontColor = UIUtils.UInt32_ARGB_To_Color(e.textColor);
- if (e.textBorderAlpha > 0)
- {
- Color border_color = UIUtils.UInt32_ARGB_To_Color(e.textBorderColor);
- border_color.a = e.textBorderAlpha / 100f;
- gfx.AddBorder(border_color, new Vector2(1, -1));
- }
- }
- private void Decode_BitmapTextGraphics(UIEditor.Decoder editor, UETextComponentMeta e)
- {
- var gfx = mGameObject.AddComponent<BitmapTextGraphics>();
- this.mTextGraphics = gfx;
- gfx.Style = e.textFontStyle;
- if (e.textFontSize > 0)
- {
- gfx.FontSize = e.textFontSize;
- }
- gfx.FontColor = UIUtils.UInt32_ARGB_To_Color(e.textColor);
- if (e.textBorderAlpha > 0)
- {
- Color border_color = UIUtils.UInt32_ARGB_To_Color(e.textBorderColor);
- border_color.a = e.textBorderAlpha / 100f;
- gfx.BorderTime = TextBorderCount.Border;
- gfx.BorderColor = border_color;
- }
- }
- private void Decode_ImageFontGraphics(UIEditor.Decoder editor, UETextComponentMeta e)
- {
- var gfx = base.mGameObject.AddComponent<ImageFontGraphics>();
- this.mTextGraphics = gfx;
- editor.editor.ParseImageFont(e.ImageFont, this.Text, gfx);
- }
- //-----------------------------------------------------------------------------------------------
- protected override void OnDisposeEvents()
- {
- base.OnDisposeEvents();
- event_TextChanged = null;
- }
- private void DoTextChanged()
- {
- if (event_TextChanged != null)
- {
- event_TextChanged.Invoke(this, Text);
- }
- }
- public delegate void TextChangedHandler(UETextComponent sender, string value);
- [Desc("值改变")]
- public TextChangedHandler event_TextChanged;
- [Desc("值改变")]
- public event TextChangedHandler TextChanged { add { event_TextChanged += value; } remove { event_TextChanged -= value; } }
- }
- }
|