HtmlLink.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 
  2. namespace FairyGUI.Utils
  3. {
  4. /// <summary>
  5. ///
  6. /// </summary>
  7. public class HtmlLink : IHtmlObject
  8. {
  9. RichTextField _owner;
  10. HtmlElement _element;
  11. SelectionShape _shape;
  12. EventCallback1 _clickHandler;
  13. EventCallback1 _rolloverHandler;
  14. EventCallback0 _rolloutHandler;
  15. public HtmlLink()
  16. {
  17. _shape = new SelectionShape();
  18. _shape.gameObject.name = "HtmlLink";
  19. _shape.cursor = "text-link";
  20. _clickHandler = (EventContext context) =>
  21. {
  22. _owner.BubbleEvent("onClickLink", _element.GetString("href"));
  23. };
  24. _rolloverHandler = (EventContext context) =>
  25. {
  26. if (_owner.htmlParseOptions.linkHoverBgColor.a > 0)
  27. _shape.color = _owner.htmlParseOptions.linkHoverBgColor;
  28. };
  29. _rolloutHandler = () =>
  30. {
  31. if (_owner.htmlParseOptions.linkHoverBgColor.a > 0)
  32. _shape.color = _owner.htmlParseOptions.linkBgColor;
  33. };
  34. }
  35. public DisplayObject displayObject
  36. {
  37. get { return _shape; }
  38. }
  39. public HtmlElement element
  40. {
  41. get { return _element; }
  42. }
  43. public float width
  44. {
  45. get { return 0; }
  46. }
  47. public float height
  48. {
  49. get { return 0; }
  50. }
  51. public void Create(RichTextField owner, HtmlElement element)
  52. {
  53. _owner = owner;
  54. _element = element;
  55. _shape.onClick.Add(_clickHandler);
  56. _shape.onRollOver.Add(_rolloverHandler);
  57. _shape.onRollOut.Add(_rolloutHandler);
  58. _shape.color = _owner.htmlParseOptions.linkBgColor;
  59. }
  60. public void SetArea(int startLine, float startCharX, int endLine, float endCharX)
  61. {
  62. if (startLine == endLine && startCharX > endCharX)
  63. {
  64. float tmp = startCharX;
  65. startCharX = endCharX;
  66. endCharX = tmp;
  67. }
  68. _shape.rects.Clear();
  69. _owner.textField.GetLinesShape(startLine, startCharX, endLine, endCharX, true, _shape.rects);
  70. _shape.Refresh();
  71. }
  72. public void SetPosition(float x, float y)
  73. {
  74. _shape.SetXY(x, y);
  75. }
  76. public void Add()
  77. {
  78. //add below _shape
  79. _owner.AddChildAt(_shape, 0);
  80. }
  81. public void Remove()
  82. {
  83. if (_shape.parent != null)
  84. _owner.RemoveChild(_shape);
  85. }
  86. public void Release()
  87. {
  88. _shape.RemoveEventListeners();
  89. _owner = null;
  90. _element = null;
  91. }
  92. public void Dispose()
  93. {
  94. _shape.Dispose();
  95. _shape = null;
  96. }
  97. }
  98. }