TextLayerInputField.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.Events;
  7. using UnityEngine.EventSystems;
  8. using UnityEngine.Serialization;
  9. using UnityEngine.UI;
  10. namespace CommonUnity3D.UGUI
  11. {
  12. public class TextLayerInputField : DisplayNodeInteractive, IEventSystemHandler, IUpdateSelectedHandler, ISubmitHandler, ICanvasElement, IInputField
  13. {
  14. //--------------------------------------------------------------------
  15. // property //
  16. private string m_Text = string.Empty;
  17. private char m_AsteriskChar = '*';
  18. private int m_CharacterLimit = 100;
  19. private InputField.ContentType m_ContentType = InputField.ContentType.Standard;
  20. private InputField.CharacterValidation m_CharacterValidation = InputField.CharacterValidation.None;
  21. private InputField.InputType m_InputType = InputField.InputType.Standard;
  22. private InputField.LineType m_LineType = InputField.LineType.SingleLine;
  23. private TouchScreenKeyboardType m_KeyboardType = TouchScreenKeyboardType.Default;
  24. private ITextComponent m_TextComponent;
  25. private Graphic m_Placeholder;
  26. private bool m_ShowCaret = true;
  27. private float m_CaretBlinkRate = 0.4f;
  28. public Action<string> event_EndEdit { get; set; }
  29. public Action<string> event_ValueChanged { get; set; }
  30. //--------------------------------------------------------------------
  31. // runtime //
  32. private Event m_ProcessingEvent = new Event();
  33. private TouchScreenKeyboard m_Keyboard = null;
  34. private string m_OriginalText = string.Empty;
  35. private bool m_WasCanceled = false;
  36. private bool m_ShouldActivateNextUpdate = false;
  37. private bool m_AllowInput = false;
  38. private CaretSprite m_CaretSprite;
  39. //--------------------------------------------------------------------
  40. public string Text
  41. {
  42. get
  43. {
  44. if (this.m_Keyboard != null && this.m_Keyboard.active && !this.InPlaceEditing() && EventSystem.current.currentSelectedGameObject == base.gameObject)
  45. {
  46. return this.m_Keyboard.text;
  47. }
  48. return this.m_Text;
  49. }
  50. set
  51. {
  52. if (this.Text == value)
  53. {
  54. return;
  55. }
  56. this.m_Text = value;
  57. if (!Application.isPlaying)
  58. {
  59. this.SendOnValueChangedAndUpdateLabel();
  60. return;
  61. }
  62. else
  63. {
  64. if (this.m_Keyboard != null)
  65. {
  66. this.m_Keyboard.text = this.m_Text;
  67. }
  68. this.SendOnValueChangedAndUpdateLabel();
  69. }
  70. }
  71. }
  72. public bool isFocused
  73. {
  74. get { return this.m_AllowInput; }
  75. }
  76. public InputField.InputType inputType
  77. {
  78. get { return m_InputType; }
  79. set { m_InputType = value; }
  80. }
  81. public InputField.ContentType contentType
  82. {
  83. get { return m_ContentType; }
  84. set { m_ContentType = value; }
  85. }
  86. public InputField.LineType lineType
  87. {
  88. get { return this.m_LineType; }
  89. set
  90. {
  91. if (this.m_LineType != value)
  92. {
  93. this.m_LineType = value;
  94. this.SetToCustomIfContentTypeIsNot(new InputField.ContentType[]
  95. {
  96. InputField.ContentType.Standard,
  97. InputField.ContentType.Autocorrected
  98. });
  99. }
  100. }
  101. }
  102. public bool multiLine
  103. {
  104. get { return m_LineType != InputField.LineType.SingleLine; }
  105. }
  106. public TouchScreenKeyboardType keyboardType
  107. {
  108. get { return m_KeyboardType; }
  109. set { m_KeyboardType = value; }
  110. }
  111. public int characterLimit
  112. {
  113. get { return m_CharacterLimit; }
  114. set { m_CharacterLimit = value; }
  115. }
  116. public InputField.CharacterValidation characterValidation
  117. {
  118. get { return m_CharacterValidation; }
  119. set { m_CharacterValidation = value; }
  120. }
  121. public char asteriskChar
  122. {
  123. get { return this.m_AsteriskChar; }
  124. set { this.m_AsteriskChar = value; }
  125. }
  126. public bool isShowCaret
  127. {
  128. get { return this.m_ShowCaret; }
  129. set { this.m_ShowCaret = value; }
  130. }
  131. public float caretBlinkRate
  132. {
  133. get { return m_CaretBlinkRate; }
  134. set { m_CaretBlinkRate = value; }
  135. }
  136. //--------------------------------------------------------------------
  137. public ITextComponent TextComponent
  138. {
  139. get { return m_TextComponent; }
  140. set { m_TextComponent = value; }
  141. }
  142. public Graphic Placeholder
  143. {
  144. get { return m_Placeholder; }
  145. set { m_Placeholder = value; }
  146. }
  147. //--------------------------------------------------------------------
  148. public override void OnDeselect(BaseEventData eventData)
  149. {
  150. base.OnDeselect(eventData);
  151. this.DeactivateInputField();
  152. }
  153. public override void OnSelect(BaseEventData eventData)
  154. {
  155. base.OnSelect(eventData);
  156. this.ActivateInputField();
  157. }
  158. public override void OnPointerClick(PointerEventData eventData)
  159. {
  160. base.OnPointerClick(eventData);
  161. this.ActivateInputField();
  162. }
  163. public override void OnPointerDown(PointerEventData eventData)
  164. {
  165. EventSystem.current.SetSelectedGameObject(base.gameObject, eventData);
  166. bool allowInput = this.m_AllowInput;
  167. base.OnPointerDown(eventData);
  168. if (!this.InPlaceEditing() && (this.m_Keyboard == null || !this.m_Keyboard.active))
  169. {
  170. this.OnSelect(eventData);
  171. return;
  172. }
  173. this.UpdateLabel();
  174. eventData.Use();
  175. }
  176. #if UNITY_EDITOR
  177. protected override void OnValidate()
  178. {
  179. base.OnValidate();
  180. this.EnforceContentType();
  181. if (!this.IsActive())
  182. {
  183. return;
  184. }
  185. this.UpdateLabel();
  186. }
  187. #endif
  188. //--------------------------------------------------------------------
  189. #region _text_append_
  190. protected virtual bool Append(char input)
  191. {
  192. if (input == '\0')
  193. {
  194. return false;
  195. }
  196. if (!this.InPlaceEditing())
  197. {
  198. return false;
  199. }
  200. if (this.characterValidation != InputField.CharacterValidation.None)
  201. {
  202. input = this.Validate(this.Text, input);
  203. }
  204. if (input == '\0')
  205. {
  206. return false;
  207. }
  208. string add = input.ToString();
  209. if (UnityEngine.Application.platform == RuntimePlatform.IPhonePlayer)
  210. {
  211. }
  212. else if (this.characterLimit > 0 && (this.Text.Length + add.Length) >= this.characterLimit)
  213. {
  214. return false;
  215. }
  216. this.m_Text += add;
  217. this.SendOnValueChanged();
  218. return true;
  219. }
  220. protected virtual bool DeleteAll()
  221. {
  222. if (m_Text.Length > 0)
  223. {
  224. this.m_Text = string.Empty;
  225. this.SendOnValueChanged();
  226. return true;
  227. }
  228. return false;
  229. }
  230. protected virtual bool Delete()
  231. {
  232. if (m_Text.Length > 0)
  233. {
  234. this.m_Text = m_Text.Substring(0, m_Text.Length - 1);
  235. this.SendOnValueChanged();
  236. return true;
  237. }
  238. return false;
  239. }
  240. protected virtual bool IsValidChar(char c)
  241. {
  242. return c != '\u007f';
  243. }
  244. protected char Validate(string text, char ch)
  245. {
  246. int pos = text.Length;
  247. if (this.characterValidation == InputField.CharacterValidation.None || !base.enabled)
  248. {
  249. return ch;
  250. }
  251. if (this.characterValidation == InputField.CharacterValidation.Integer || this.characterValidation == InputField.CharacterValidation.Decimal)
  252. {
  253. if (pos != 0 || text.Length <= 0 || text[0] != '-')
  254. {
  255. if (ch >= '0' && ch <= '9')
  256. {
  257. return ch;
  258. }
  259. if (ch == '-' && pos == 0)
  260. {
  261. return ch;
  262. }
  263. if (ch == '.' && this.characterValidation == InputField.CharacterValidation.Decimal && !text.Contains("."))
  264. {
  265. return ch;
  266. }
  267. }
  268. }
  269. else
  270. {
  271. if (this.characterValidation == InputField.CharacterValidation.Alphanumeric)
  272. {
  273. if (ch >= 'A' && ch <= 'Z')
  274. {
  275. return ch;
  276. }
  277. if (ch >= 'a' && ch <= 'z')
  278. {
  279. return ch;
  280. }
  281. if (ch >= '0' && ch <= '9')
  282. {
  283. return ch;
  284. }
  285. }
  286. else
  287. {
  288. if (this.characterValidation == InputField.CharacterValidation.Name)
  289. {
  290. char c = (text.Length <= 0) ? ' ' : text[Mathf.Clamp(pos, 0, text.Length - 1)];
  291. char c2 = (text.Length <= 0) ? '\n' : text[Mathf.Clamp(pos + 1, 0, text.Length - 1)];
  292. if (char.IsLetter(ch))
  293. {
  294. if (char.IsLower(ch) && c == ' ')
  295. {
  296. return char.ToUpper(ch);
  297. }
  298. if (char.IsUpper(ch) && c != ' ' && c != '\'')
  299. {
  300. return char.ToLower(ch);
  301. }
  302. return ch;
  303. }
  304. else
  305. {
  306. if (ch == '\'')
  307. {
  308. if (c != ' ' && c != '\'' && c2 != '\'' && !text.Contains("'"))
  309. {
  310. return ch;
  311. }
  312. }
  313. else
  314. {
  315. if (ch == ' ' && c != ' ' && c != '\'' && c2 != ' ' && c2 != '\'')
  316. {
  317. return ch;
  318. }
  319. }
  320. }
  321. }
  322. else
  323. {
  324. if (this.characterValidation == InputField.CharacterValidation.EmailAddress)
  325. {
  326. if (ch >= 'A' && ch <= 'Z')
  327. {
  328. return ch;
  329. }
  330. if (ch >= 'a' && ch <= 'z')
  331. {
  332. return ch;
  333. }
  334. if (ch >= '0' && ch <= '9')
  335. {
  336. return ch;
  337. }
  338. if (ch == '@' && text.IndexOf('@') == -1)
  339. {
  340. return ch;
  341. }
  342. if ("!#$%&'*+-/=?^_`{|}~".IndexOf(ch) != -1)
  343. {
  344. return ch;
  345. }
  346. if (ch == '.')
  347. {
  348. char c3 = (text.Length <= 0) ? ' ' : text[Mathf.Clamp(pos, 0, text.Length - 1)];
  349. char c4 = (text.Length <= 0) ? '\n' : text[Mathf.Clamp(pos + 1, 0, text.Length - 1)];
  350. if (c3 != '.' && c4 != '.')
  351. {
  352. return ch;
  353. }
  354. }
  355. }
  356. }
  357. }
  358. }
  359. return '\0';
  360. }
  361. protected void UpdateLabel()
  362. {
  363. if (this.m_TextComponent != null)
  364. {
  365. string text = this.Text;
  366. string text2 = this.Text;
  367. if (this.inputType == InputField.InputType.Password)
  368. {
  369. text2 = new string(this.asteriskChar, text.Length);
  370. }
  371. bool flag = string.IsNullOrEmpty(text);
  372. if (this.m_Placeholder != null)
  373. {
  374. this.m_Placeholder.enabled = flag;
  375. }
  376. this.m_TextComponent.Text = text2;
  377. }
  378. }
  379. //--------------------------------------------------------------------
  380. private void SendOnValueChangedAndUpdateLabel()
  381. {
  382. this.SendOnValueChanged();
  383. this.UpdateLabel();
  384. }
  385. private void SendOnValueChanged()
  386. {
  387. if (this.event_ValueChanged != null)
  388. {
  389. this.event_ValueChanged.Invoke(this.Text);
  390. }
  391. }
  392. protected void SendOnSubmit()
  393. {
  394. if (UnityEngine.Application.platform == RuntimePlatform.IPhonePlayer)
  395. {
  396. if (this.characterLimit > 0 && this.m_Text.Length > this.characterLimit)
  397. {
  398. this.m_Text = this.m_Text.Substring(0, this.characterLimit);
  399. }
  400. }
  401. if (this.event_EndEdit != null)
  402. {
  403. this.event_EndEdit.Invoke(this.m_Text);
  404. }
  405. }
  406. private void EnforceContentType()
  407. {
  408. switch (this.contentType)
  409. {
  410. case InputField.ContentType.Standard:
  411. this.m_InputType = InputField.InputType.Standard;
  412. this.m_KeyboardType = TouchScreenKeyboardType.Default;
  413. this.m_CharacterValidation = InputField.CharacterValidation.None;
  414. return;
  415. case InputField.ContentType.Autocorrected:
  416. this.m_InputType = InputField.InputType.AutoCorrect;
  417. this.m_KeyboardType = TouchScreenKeyboardType.Default;
  418. this.m_CharacterValidation = InputField.CharacterValidation.None;
  419. return;
  420. case InputField.ContentType.IntegerNumber:
  421. this.m_LineType = InputField.LineType.SingleLine;
  422. this.m_InputType = InputField.InputType.Standard;
  423. this.m_KeyboardType = TouchScreenKeyboardType.NumberPad;
  424. this.m_CharacterValidation = InputField.CharacterValidation.Integer;
  425. return;
  426. case InputField.ContentType.DecimalNumber:
  427. this.m_LineType = InputField.LineType.SingleLine;
  428. this.m_InputType = InputField.InputType.Standard;
  429. this.m_KeyboardType = TouchScreenKeyboardType.NumbersAndPunctuation;
  430. this.m_CharacterValidation = InputField.CharacterValidation.Decimal;
  431. return;
  432. case InputField.ContentType.Alphanumeric:
  433. this.m_LineType = InputField.LineType.SingleLine;
  434. this.m_InputType = InputField.InputType.Standard;
  435. this.m_KeyboardType = TouchScreenKeyboardType.ASCIICapable;
  436. this.m_CharacterValidation = InputField.CharacterValidation.Alphanumeric;
  437. return;
  438. case InputField.ContentType.Name:
  439. this.m_LineType = InputField.LineType.SingleLine;
  440. this.m_InputType = InputField.InputType.Standard;
  441. this.m_KeyboardType = TouchScreenKeyboardType.Default;
  442. this.m_CharacterValidation = InputField.CharacterValidation.Name;
  443. return;
  444. case InputField.ContentType.EmailAddress:
  445. this.m_LineType = InputField.LineType.SingleLine;
  446. this.m_InputType = InputField.InputType.Standard;
  447. this.m_KeyboardType = TouchScreenKeyboardType.EmailAddress;
  448. this.m_CharacterValidation = InputField.CharacterValidation.EmailAddress;
  449. return;
  450. case InputField.ContentType.Password:
  451. this.m_LineType = InputField.LineType.SingleLine;
  452. this.m_InputType = InputField.InputType.Password;
  453. this.m_KeyboardType = TouchScreenKeyboardType.Default;
  454. this.m_CharacterValidation = InputField.CharacterValidation.None;
  455. return;
  456. case InputField.ContentType.Pin:
  457. this.m_LineType = InputField.LineType.SingleLine;
  458. this.m_InputType = InputField.InputType.Password;
  459. this.m_KeyboardType = TouchScreenKeyboardType.NumberPad;
  460. this.m_CharacterValidation = InputField.CharacterValidation.Integer;
  461. return;
  462. default:
  463. return;
  464. }
  465. }
  466. private void SetToCustomIfContentTypeIsNot(params InputField.ContentType[] allowedContentTypes)
  467. {
  468. if (this.contentType == InputField.ContentType.Custom)
  469. {
  470. return;
  471. }
  472. for (int i = 0; i < allowedContentTypes.Length; i++)
  473. {
  474. if (this.contentType == allowedContentTypes[i])
  475. {
  476. return;
  477. }
  478. }
  479. this.contentType = InputField.ContentType.Custom;
  480. }
  481. private void SetToCustom()
  482. {
  483. if (this.contentType == InputField.ContentType.Custom)
  484. {
  485. return;
  486. }
  487. this.contentType = InputField.ContentType.Custom;
  488. }
  489. #endregion
  490. //--------------------------------------------------------------------
  491. #region _keyboard_
  492. private bool InPlaceEditing()
  493. {
  494. return !TouchScreenKeyboard.isSupported;
  495. }
  496. public void ProcessEvent(Event e)
  497. {
  498. this.KeyPressed(e);
  499. }
  500. /// <summary>
  501. ///
  502. /// </summary>
  503. /// <param name="evt"></param>
  504. /// <returns>finish</returns>
  505. protected bool KeyPressed(Event evt)
  506. {
  507. KeyCode keyCode = evt.keyCode;
  508. switch (evt.keyCode)
  509. {
  510. case KeyCode.KeypadEnter:
  511. if (this.lineType != InputField.LineType.MultiLineNewline)
  512. {
  513. return true;
  514. }
  515. break;
  516. case KeyCode.Delete:
  517. if (DeleteAll())
  518. {
  519. this.UpdateLabel();
  520. }
  521. return false;
  522. case KeyCode.Backspace:
  523. if (this.Delete())
  524. {
  525. this.UpdateLabel();
  526. }
  527. return false;
  528. }
  529. char c = evt.character;
  530. if (!this.multiLine && (c == '\t' || c == '\r' || c == '\n'))
  531. {
  532. return false;
  533. }
  534. if (c == '\r' || c == '\u0003')
  535. {
  536. c = '\n';
  537. }
  538. if (this.IsValidChar(c))
  539. {
  540. this.Append(c);
  541. this.UpdateLabel();
  542. }
  543. return false;
  544. }
  545. public virtual void OnUpdateSelected(BaseEventData eventData)
  546. {
  547. if (!this.isFocused)
  548. {
  549. return;
  550. }
  551. while (Event.PopEvent(this.m_ProcessingEvent))
  552. {
  553. if (this.m_ProcessingEvent.rawType == EventType.KeyDown)
  554. {
  555. bool finish = this.KeyPressed(this.m_ProcessingEvent);
  556. if (finish)
  557. {
  558. this.DeactivateInputField();
  559. break;
  560. }
  561. }
  562. }
  563. eventData.Use();
  564. }
  565. protected virtual void OnTouchKeoboardFinish(TouchScreenKeyboard keyboard)
  566. {
  567. string text = keyboard.text;
  568. if (this.m_Text != text)
  569. {
  570. this.m_Text = string.Empty;
  571. for (int i = 0; i < text.Length; i++)
  572. {
  573. char c = text[i];
  574. if (c == '\r' || c == '\u0003')
  575. {
  576. c = '\n';
  577. }
  578. if (this.characterValidation != InputField.CharacterValidation.None)
  579. {
  580. c = this.Validate(this.m_Text, c);
  581. }
  582. if (c == '\n')
  583. {
  584. keyboard.text = this.m_Text;
  585. this.OnDeselect(null);
  586. return;
  587. }
  588. if (c != '\0')
  589. {
  590. this.m_Text += c;
  591. }
  592. }
  593. if (UnityEngine.Application.platform == RuntimePlatform.IPhonePlayer)
  594. {
  595. }
  596. else if (this.characterLimit > 0 && this.m_Text.Length > this.characterLimit)
  597. {
  598. this.m_Text = this.m_Text.Substring(0, this.characterLimit);
  599. }
  600. int length = this.m_Text.Length;
  601. if (this.m_Text != text)
  602. {
  603. keyboard.text = this.m_Text;
  604. }
  605. this.SendOnValueChangedAndUpdateLabel();
  606. }
  607. }
  608. //--------------------------------------------------------------------
  609. public void ActivateInputField()
  610. {
  611. if (this.m_TextComponent == null || !this.IsActive() || !this.IsInteractable())
  612. {
  613. return;
  614. }
  615. if (this.m_Keyboard != null && !this.m_Keyboard.active)
  616. {
  617. this.m_Keyboard.active = true;
  618. this.m_Keyboard.text = this.m_Text;
  619. }
  620. this.m_ShouldActivateNextUpdate = true;
  621. }
  622. private void ActivateInputFieldInternal()
  623. {
  624. if (EventSystem.current.currentSelectedGameObject != base.gameObject)
  625. {
  626. EventSystem.current.SetSelectedGameObject(base.gameObject);
  627. }
  628. if (TouchScreenKeyboard.isSupported)
  629. {
  630. this.m_Keyboard = TouchScreenKeyboard.Open(
  631. this.m_Text,
  632. this.keyboardType,
  633. this.inputType == InputField.InputType.AutoCorrect,
  634. this.multiLine,
  635. this.inputType == InputField.InputType.Password);
  636. }
  637. else
  638. {
  639. Input.imeCompositionMode = IMECompositionMode.On;
  640. }
  641. this.m_OriginalText = this.Text;
  642. this.m_WasCanceled = false;
  643. this.m_AllowInput = true;
  644. }
  645. public void DeactivateInputField()
  646. {
  647. if (!this.m_AllowInput)
  648. {
  649. return;
  650. }
  651. this.m_AllowInput = false;
  652. if (this.m_TextComponent != null && this.IsInteractable())
  653. {
  654. if (this.m_WasCanceled)
  655. {
  656. this.Text = this.m_OriginalText;
  657. }
  658. if (this.m_Keyboard != null)
  659. {
  660. this.m_Keyboard.active = false;
  661. this.m_Keyboard = null;
  662. }
  663. this.SendOnSubmit();
  664. Input.imeCompositionMode = IMECompositionMode.Auto;
  665. }
  666. }
  667. #endregion
  668. //--------------------------------------------------------------------
  669. protected virtual void LateUpdate()
  670. {
  671. if (this.m_ShouldActivateNextUpdate)
  672. {
  673. if (!this.isFocused)
  674. {
  675. this.ActivateInputFieldInternal();
  676. this.m_ShouldActivateNextUpdate = false;
  677. return;
  678. }
  679. this.m_ShouldActivateNextUpdate = false;
  680. }
  681. UpdateCaret();
  682. if (this.InPlaceEditing() || !this.isFocused)
  683. {
  684. return;
  685. }
  686. if (this.m_Keyboard == null || !this.m_Keyboard.active)
  687. {
  688. if (this.m_Keyboard != null && this.m_Keyboard.wasCanceled)
  689. {
  690. this.m_WasCanceled = true;
  691. }
  692. this.OnDeselect(null);
  693. return;
  694. }
  695. this.OnTouchKeoboardFinish(m_Keyboard);
  696. if (this.m_Keyboard.done)
  697. {
  698. if (this.m_Keyboard.wasCanceled)
  699. {
  700. this.m_WasCanceled = true;
  701. }
  702. this.OnDeselect(null);
  703. }
  704. }
  705. //--------------------------------------------------------------------
  706. public void GraphicUpdateComplete()
  707. {
  708. }
  709. public void LayoutComplete()
  710. {
  711. }
  712. public void OnSubmit(BaseEventData eventData)
  713. {
  714. }
  715. public void Rebuild(CanvasUpdate executing)
  716. {
  717. }
  718. //--------------------------------------------------------------------
  719. private void UpdateCaret()
  720. {
  721. if (isShowCaret && isFocused)
  722. {
  723. if (m_TextComponent != null)
  724. {
  725. if (m_CaretSprite == null)
  726. {
  727. m_CaretSprite = new CaretSprite();
  728. Binding.AddChild(m_CaretSprite);
  729. }
  730. var cb = m_TextComponent.LastCaretPosition;
  731. cb.x += m_TextComponent.Binding.X;
  732. cb.y += m_TextComponent.Binding.Y;
  733. m_CaretSprite.Bounds2D = cb;
  734. m_CaretSprite.Visible = (cb.width > 0);
  735. m_CaretSprite.UpdateCaret(m_CaretBlinkRate);
  736. }
  737. }
  738. else
  739. {
  740. if (m_CaretSprite != null)
  741. {
  742. m_CaretSprite.Visible = false;
  743. }
  744. }
  745. }
  746. private class CaretSprite : TintSprite
  747. {
  748. private float m_CaretSEC;
  749. private int m_CaretTimer;
  750. internal CaretSprite() : base("caret") { }
  751. internal bool UpdateCaret(float blinkRate)
  752. {
  753. var delta = UnityEngine.Time.deltaTime;
  754. m_CaretSEC += delta;
  755. if (m_CaretSEC > blinkRate)
  756. {
  757. m_CaretSEC = m_CaretSEC % blinkRate;
  758. m_CaretTimer++;
  759. }
  760. this.VisibleInParent = (m_CaretTimer % 2 == 0);
  761. return this.VisibleInParent;
  762. }
  763. }
  764. }
  765. }