123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- using UnityEngine;
- using CommonUI.Cell.Game;
- using CommonUI.Display;
- using UnityImage = CommonUI_Unity3D.Impl.UnityImage;
- using AttributedString = CommonUI.Display.Text.AttributedString;
- using BaseRichTextLayer = CommonUI.Display.Text.BaseRichTextLayer;
- using TextAttribute = CommonUI.Display.Text.TextAttribute;
- using CommonUI.Display.Text;
- using UnityEngine.EventSystems;
- using System;
- using CommonUI.Data;
- namespace CommonUnity3D.UGUI
- {
- //----------------------------------------------------------------------------------------
- /// <summary>
- /// 可滚动富文本
- /// </summary>
- public class RichTextBox : ScrollablePanel, ITextComponent
- {
- private readonly UGUIRichTextLayer mLayer;
- private bool mIsNeedScroll;
- private bool mScrollable = true;
- protected readonly bool mUseBitmapFont;
- public DisplayNode Binding { get { return mContent; } }
- public UGUIRichTextLayer RichTextLayer
- {
- get { return mLayer; }
- }
- public AttributedString AText
- {
- get { return mLayer.GetText(); }
- set
- {
- if (IsDispose) return;
- mLayer.SetString(value);
- }
- }
- public string XmlText
- {
- set
- {
- if (IsDispose) return;
- mLayer.XmlText = value;
- }
- }
- public string UnityRichText
- {
- set
- {
- if (IsDispose) return;
- this.XmlText = UIUtils.UnityRichTextToXmlText(value);
- }
- }
- public bool IsNeedScroll
- {
- get { return mIsNeedScroll; }
- }
- public bool Scrollable
- {
- get { return mScrollable; }
- set { this.mScrollable = value; }
- }
-
- public RichTextBox(string name = "", bool use_bitmap = false) : base(name)
- {
- this.mUseBitmapFont = use_bitmap;
- this.mLayer = UIFactory.Instance.CreateRichTextLayer(mContent, mUseBitmapFont);
- this.mMaskScrollRect.enabled = false;
- this.mMaskScrollRect.horizontal = false;
- this.mMaskScrollRect.vertical = true;
- this.mMaskScrollRect.AutoUpdateContentSize = false;
- this.mRectMask.enabled = true;
- this.mMaskGraphics.enabled = false;
- // this.mMask.enabled = false;
- this.mContent.EnableChildren = false;
- }
- public bool TestClick(Vector2 point, out RichTextClickInfo info)
- {
- Rect scroll_rect = this.ScrollRect2D;
- return RichTextLayer.Click(point.x + scroll_rect.x, point.y + scroll_rect.y, out info);
- }
- protected override void OnDispose()
- {
- mLayer.Dispose();
- base.OnDispose();
- }
- protected override void OnUpdateContentSize()
- {
- Vector2 bsize = this.Size2D;
- this.mLayer.SetWidth(bsize.x);
- Rect scroll_rect = this.ScrollRect2D;
- this.mContent.Size2D = new Vector2(bsize.x, mLayer.ContentHeight);
- this.mIsNeedScroll = mScrollable && (mLayer.ContentHeight > bsize.y);
- this.mMaskScrollRect.enabled = mIsNeedScroll;
- //this.mRectMask.enabled = mIsNeedScroll;
- this.mMaskGraphics.enabled = mIsNeedScroll;
- this.EnableChildren = mIsNeedScroll;
- if (!mIsNeedScroll)
- {
- switch (this.Anchor)
- {
- case CommonUI.Data.TextAnchor.L_T:
- case CommonUI.Data.TextAnchor.C_T:
- case CommonUI.Data.TextAnchor.R_T:
- break;
- case CommonUI.Data.TextAnchor.L_C:
- case CommonUI.Data.TextAnchor.C_C:
- case CommonUI.Data.TextAnchor.R_C:
- scroll_rect.y = -(bsize.y - this.mLayer.ContentHeight) * 0.5f;
- this.Container.Position2D = -scroll_rect.position;
- break;
- case CommonUI.Data.TextAnchor.L_B:
- case CommonUI.Data.TextAnchor.C_B:
- case CommonUI.Data.TextAnchor.R_B:
- scroll_rect.y = -(bsize.y - this.mLayer.ContentHeight);
- this.Container.Position2D = -scroll_rect.position;
- break;
- }
- }
- this.mLayer.Render(NullGraphics.SimpleGraphics, 0, 0, bsize.x, bsize.y, scroll_rect.x, scroll_rect.y);
- }
- protected override void OnSizeChanged(Vector2 size)
- {
- base.OnSizeChanged(size);
- this.mLayer.SetWidth(size.x);
- }
- #region ITextComponent
- public string Text
- {
- get { return mLayer.Text; }
- set
- {
- if (IsDispose) return;
- mLayer.Text = value;
- }
- }
- public int FontSize
- {
- get { return mLayer.FontSize; }
- set { mLayer.FontSize = value; }
- }
- public UnityEngine.Color FontColor
- {
- get { return mLayer.FontColor; }
- set { mLayer.FontColor = value; }
- }
- public CommonUI.Data.FontStyle Style
- {
- get { return mLayer.Style; }
- set { mLayer.Style = value; }
- }
- public bool IsUnderline
- {
- get { return mLayer.IsUnderline; }
- set { mLayer.IsUnderline = value; }
- }
- public Vector2 TextOffset
- {
- get { return mLayer.TextOffset; }
- set { mLayer.TextOffset = value; }
- }
- public CommonUI.Data.TextAnchor Anchor
- {
- get { return mLayer.Anchor; }
- set { mLayer.Anchor = value; }
- }
- public Vector2 PreferredSize
- {
- get { return mLayer.PreferredSize; }
- }
- public Rect LastCaretPosition
- {
- get
- {
- var bounds = mLayer.LastCaretPosition;
- bounds.position += mContent.Position2D;
- return bounds;
- }
- }
- public void SetBorder(UnityEngine.Color bc, Vector2 distance)
- {
- mLayer.SetBorder(bc, distance);
- }
- public void SetShadow(UnityEngine.Color bc, Vector2 distance)
- {
- mLayer.SetShadow(bc, distance);
- }
- public void SetFont(Font font)
- {
- mLayer.SetFont(font);
- }
- #endregion
- }
- //----------------------------------------------------------------------------------------
- /// <summary>
- /// 不可滚动富文本
- /// </summary>
- public class RichTextPan : DisplayText
- {
- private readonly UGUIRichTextLayer mLayer;
- private bool mScrollToCaret = false;
- private Rect mScrollRect = new Rect();
- protected readonly bool mUseBitmapFont;
- public UGUIRichTextLayer RichTextLayer
- {
- get { return mLayer; }
- }
- public AttributedString AText
- {
- get { return mLayer.GetText(); }
- set { mLayer.SetString(value); }
- }
- public string XmlText
- {
- set { mLayer.XmlText = value; }
- }
- public string UnityRichText
- {
- set { this.XmlText = UIUtils.UnityRichTextToXmlText(value); }
- }
- public bool AutoScrollToCaret
- {
- get { return mScrollToCaret; }
- set { mScrollToCaret = value; }
- }
- public Vector2 ContentSize
- {
- get
- {
- return new Vector2(mLayer.ContentWidth, mLayer.ContentHeight);
- }
- }
- public RichTextPan(bool use_bitmap, string name = "") : base(name)
- {
- this.mUseBitmapFont = use_bitmap;
- this.mLayer = UIFactory.Instance.CreateRichTextLayer(this, use_bitmap);
- this.mLayer.SetWidth(this.Width);
- }
- public bool TestClick(Vector2 point, out RichTextClickInfo info)
- {
- return RichTextLayer.Click(point.x, point.y, out info);
- }
- protected override void OnDispose()
- {
- mLayer.Dispose();
- base.OnDispose();
- }
- protected override void OnUpdate()
- {
- base.OnUpdate();
- this.mScrollRect.size = this.Size2D;
- this.mScrollRect.position = Vector2.zero;
- this.mLayer.SetWidth(mScrollRect.width);
- if (mScrollToCaret)
- {
- // todo
- float dh = mLayer.ContentHeight - Binding.Height;
- if (dh > 0)
- {
- mScrollRect.y += dh;
- }
- }
- else
- {
- switch (this.Anchor)
- {
- case CommonUI.Data.TextAnchor.L_T:
- case CommonUI.Data.TextAnchor.C_T:
- case CommonUI.Data.TextAnchor.R_T:
- break;
- case CommonUI.Data.TextAnchor.L_C:
- case CommonUI.Data.TextAnchor.C_C:
- case CommonUI.Data.TextAnchor.R_C:
- mScrollRect.y = -(mScrollRect.height - this.mLayer.ContentHeight) * 0.5f;
- break;
- case CommonUI.Data.TextAnchor.L_B:
- case CommonUI.Data.TextAnchor.C_B:
- case CommonUI.Data.TextAnchor.R_B:
- mScrollRect.y = -(mScrollRect.height - this.mLayer.ContentHeight);
- break;
- }
- }
- this.mLayer.Render(NullGraphics.SimpleGraphics, -mScrollRect.x, -mScrollRect.y, mScrollRect.width, mScrollRect.height, mScrollRect.x, mScrollRect.y);
- }
- #region ITextComponent
- public override string Text
- {
- get { return mLayer.Text; }
- set { mLayer.Text = value; }
- }
- public override int FontSize
- {
- get { return mLayer.FontSize; }
- set { mLayer.FontSize = value; }
- }
- public override UnityEngine.Color FontColor
- {
- get { return mLayer.FontColor; }
- set { mLayer.FontColor = value; }
- }
- public override CommonUI.Data.FontStyle Style
- {
- get { return mLayer.Style; }
- set { mLayer.Style = value; }
- }
- public override bool IsUnderline
- {
- get { return mLayer.IsUnderline; }
- set { mLayer.IsUnderline = value; }
- }
- public override Vector2 TextOffset
- {
- get { return mLayer.TextOffset; }
- set { mLayer.TextOffset = value; }
- }
- public override CommonUI.Data.TextAnchor Anchor
- {
- get { return mLayer.Anchor; }
- set { mLayer.Anchor = value; }
- }
- public override Vector2 PreferredSize
- {
- get { return mLayer.PreferredSize; }
- }
- public override Rect LastCaretPosition
- {
- get
- {
- var bounds = mLayer.LastCaretPosition;
- bounds.position -= mScrollRect.position;
- return bounds;
- }
- }
- public override void SetBorder(UnityEngine.Color bc, Vector2 distance)
- {
- mLayer.SetBorder(bc, distance);
- }
- public override void SetShadow(UnityEngine.Color bc, Vector2 distance)
- {
- mLayer.SetShadow(bc, distance);
- }
- public override void SetFont(Font font)
- {
- mLayer.SetFont(font);
- }
- #endregion
- }
- //----------------------------------------------------------------------------------------
- }
|