UIInput.cs 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. [AddComponentMenu("NGUI/UI/Input Field")]
  6. public class UIInput : MonoBehaviour
  7. {
  8. public string defaultText
  9. {
  10. get
  11. {
  12. if (this.mDoInit)
  13. {
  14. this.Init();
  15. }
  16. return this.mDefaultText;
  17. }
  18. set
  19. {
  20. if (this.mDoInit)
  21. {
  22. this.Init();
  23. }
  24. this.mDefaultText = value;
  25. this.UpdateLabel();
  26. }
  27. }
  28. public bool inputShouldBeHidden
  29. {
  30. get
  31. {
  32. return this.hideInput && this.label != null && !this.label.multiLine && this.inputType != UIInput.InputType.Password;
  33. }
  34. }
  35. [Obsolete("Use UIInput.value instead")]
  36. public string text
  37. {
  38. get
  39. {
  40. return this.value;
  41. }
  42. set
  43. {
  44. this.value = value;
  45. }
  46. }
  47. public string value
  48. {
  49. get
  50. {
  51. if (this.mDoInit)
  52. {
  53. this.Init();
  54. }
  55. return this.mValue;
  56. }
  57. set
  58. {
  59. if (this.mDoInit)
  60. {
  61. this.Init();
  62. }
  63. UIInput.mDrawStart = 0;
  64. if (Application.platform == RuntimePlatform.BlackBerryPlayer)
  65. {
  66. value = value.Replace("\\b", "\b");
  67. }
  68. value = this.Validate(value);
  69. if (this.mValue != value)
  70. {
  71. this.mValue = value;
  72. this.mLoadSavedValue = false;
  73. if (this.isSelected)
  74. {
  75. if (string.IsNullOrEmpty(value))
  76. {
  77. this.mSelectionStart = 0;
  78. this.mSelectionEnd = 0;
  79. }
  80. else
  81. {
  82. this.mSelectionStart = value.Length;
  83. this.mSelectionEnd = this.mSelectionStart;
  84. }
  85. }
  86. else
  87. {
  88. this.SaveToPlayerPrefs(value);
  89. }
  90. this.UpdateLabel();
  91. this.ExecuteOnChange();
  92. }
  93. }
  94. }
  95. [Obsolete("Use UIInput.isSelected instead")]
  96. public bool selected
  97. {
  98. get
  99. {
  100. return this.isSelected;
  101. }
  102. set
  103. {
  104. this.isSelected = value;
  105. }
  106. }
  107. public bool isSelected
  108. {
  109. get
  110. {
  111. return UIInput.selection == this;
  112. }
  113. set
  114. {
  115. if (!value)
  116. {
  117. if (this.isSelected)
  118. {
  119. UICamera.selectedObject = null;
  120. }
  121. }
  122. else
  123. {
  124. UICamera.selectedObject = base.gameObject;
  125. }
  126. }
  127. }
  128. public int cursorPosition
  129. {
  130. get
  131. {
  132. return (!this.isSelected) ? this.value.Length : this.mSelectionEnd;
  133. }
  134. set
  135. {
  136. if (this.isSelected)
  137. {
  138. this.mSelectionEnd = value;
  139. this.UpdateLabel();
  140. }
  141. }
  142. }
  143. public int selectionStart
  144. {
  145. get
  146. {
  147. return (!this.isSelected) ? this.value.Length : this.mSelectionStart;
  148. }
  149. set
  150. {
  151. if (this.isSelected)
  152. {
  153. this.mSelectionStart = value;
  154. this.UpdateLabel();
  155. }
  156. }
  157. }
  158. public int selectionEnd
  159. {
  160. get
  161. {
  162. return (!this.isSelected) ? this.value.Length : this.mSelectionEnd;
  163. }
  164. set
  165. {
  166. if (this.isSelected)
  167. {
  168. this.mSelectionEnd = value;
  169. this.UpdateLabel();
  170. }
  171. }
  172. }
  173. public UITexture caret
  174. {
  175. get
  176. {
  177. return this.mCaret;
  178. }
  179. }
  180. public string Validate(string val)
  181. {
  182. if (string.IsNullOrEmpty(val))
  183. {
  184. return string.Empty;
  185. }
  186. StringBuilder stringBuilder = new StringBuilder(val.Length);
  187. foreach (char c in val)
  188. {
  189. if (this.onValidate != null)
  190. {
  191. c = this.onValidate(stringBuilder.ToString(), stringBuilder.Length, c);
  192. }
  193. else if (this.validation != UIInput.Validation.None)
  194. {
  195. c = this.Validate(stringBuilder.ToString(), stringBuilder.Length, c);
  196. }
  197. if (c != '\0')
  198. {
  199. stringBuilder.Append(c);
  200. }
  201. }
  202. if (this.characterLimit > 0 && stringBuilder.Length > this.characterLimit)
  203. {
  204. return stringBuilder.ToString(0, this.characterLimit);
  205. }
  206. return stringBuilder.ToString();
  207. }
  208. private void Start()
  209. {
  210. if (this.selectOnTab != null)
  211. {
  212. UIKeyNavigation uikeyNavigation = base.GetComponent<UIKeyNavigation>();
  213. if (uikeyNavigation == null)
  214. {
  215. uikeyNavigation = base.gameObject.AddComponent<UIKeyNavigation>();
  216. uikeyNavigation.onDown = this.selectOnTab;
  217. }
  218. this.selectOnTab = null;
  219. NGUITools.SetDirty(this);
  220. }
  221. if (this.mLoadSavedValue && !string.IsNullOrEmpty(this.savedAs))
  222. {
  223. this.LoadValue();
  224. }
  225. else
  226. {
  227. this.value = this.mValue.Replace("\\n", "\n");
  228. }
  229. }
  230. protected void Init()
  231. {
  232. if (this.mDoInit && this.label != null)
  233. {
  234. this.mDoInit = false;
  235. this.mDefaultText = this.label.text;
  236. this.mDefaultColor = this.label.color;
  237. this.label.supportEncoding = false;
  238. if (this.label.alignment == NGUIText.Alignment.Justified)
  239. {
  240. this.label.alignment = NGUIText.Alignment.Left;
  241. Debug.LogWarning("Input fields using labels with justified alignment are not supported at this time", this);
  242. }
  243. this.mPivot = this.label.pivot;
  244. this.mPosition = this.label.cachedTransform.localPosition.x;
  245. this.UpdateLabel();
  246. }
  247. }
  248. protected void SaveToPlayerPrefs(string val)
  249. {
  250. if (!string.IsNullOrEmpty(this.savedAs))
  251. {
  252. if (string.IsNullOrEmpty(val))
  253. {
  254. PlayerPrefs.DeleteKey(this.savedAs);
  255. }
  256. else
  257. {
  258. PlayerPrefs.SetString(this.savedAs, val);
  259. }
  260. }
  261. }
  262. protected virtual void OnSelect(bool isSelected)
  263. {
  264. if (isSelected)
  265. {
  266. if (this.mOnGUI == null)
  267. {
  268. this.mOnGUI = base.gameObject.AddComponent<UIInputOnGUI>();
  269. }
  270. this.OnSelectEvent();
  271. }
  272. else
  273. {
  274. if (this.mOnGUI != null)
  275. {
  276. UnityEngine.Object.Destroy(this.mOnGUI);
  277. this.mOnGUI = null;
  278. }
  279. this.OnDeselectEvent();
  280. }
  281. }
  282. protected void OnSelectEvent()
  283. {
  284. UIInput.selection = this;
  285. if (this.mDoInit)
  286. {
  287. this.Init();
  288. }
  289. if (this.label != null && NGUITools.GetActive(this))
  290. {
  291. this.mSelectMe = Time.frameCount;
  292. }
  293. }
  294. protected void OnDeselectEvent()
  295. {
  296. if (this.mDoInit)
  297. {
  298. this.Init();
  299. }
  300. if (this.label != null && NGUITools.GetActive(this))
  301. {
  302. this.mValue = this.value;
  303. if (string.IsNullOrEmpty(this.mValue))
  304. {
  305. this.label.text = this.mDefaultText;
  306. this.label.color = this.mDefaultColor;
  307. }
  308. else
  309. {
  310. this.label.text = this.mValue;
  311. }
  312. Input.imeCompositionMode = IMECompositionMode.Auto;
  313. this.RestoreLabelPivot();
  314. }
  315. UIInput.selection = null;
  316. this.UpdateLabel();
  317. }
  318. protected virtual void Update()
  319. {
  320. if (this.isSelected)
  321. {
  322. if (this.mDoInit)
  323. {
  324. this.Init();
  325. }
  326. if (this.mSelectMe != -1 && this.mSelectMe != Time.frameCount)
  327. {
  328. this.mSelectMe = -1;
  329. this.mSelectionEnd = ((!string.IsNullOrEmpty(this.mValue)) ? this.mValue.Length : 0);
  330. UIInput.mDrawStart = 0;
  331. this.mSelectionStart = ((!this.selectAllTextOnFocus) ? this.mSelectionEnd : 0);
  332. this.label.color = this.activeTextColor;
  333. Vector2 compositionCursorPos = (!(UICamera.current != null) || !(UICamera.current.cachedCamera != null)) ? this.label.worldCorners[0] : UICamera.current.cachedCamera.WorldToScreenPoint(this.label.worldCorners[0]);
  334. compositionCursorPos.y = (float)UICamera.ScreenHeight - compositionCursorPos.y;
  335. Input.imeCompositionMode = IMECompositionMode.On;
  336. Input.compositionCursorPos = compositionCursorPos;
  337. this.UpdateLabel();
  338. if (string.IsNullOrEmpty(Input.inputString))
  339. {
  340. return;
  341. }
  342. }
  343. string compositionString = Input.compositionString;
  344. if (string.IsNullOrEmpty(compositionString) && !string.IsNullOrEmpty(Input.inputString))
  345. {
  346. if (!NInput.GetMouseButtonDown(0) && !NInput.GetMouseButtonDown(1) && !NInput.GetMouseButtonDown(2))
  347. {
  348. string inputString = Input.inputString;
  349. Debug.Log("Input " + Input.inputString);
  350. this.InputJapaneseInMiddleOfSentence = true;
  351. foreach (char c in inputString)
  352. {
  353. if (c >= ' ')
  354. {
  355. if (c != '')
  356. {
  357. if (c != '')
  358. {
  359. if (c != '')
  360. {
  361. if (c != '')
  362. {
  363. this.Insert(c.ToString());
  364. }
  365. }
  366. }
  367. }
  368. }
  369. }
  370. this.InputJapaneseInMiddleOfSentence = false;
  371. }
  372. else
  373. {
  374. this.mSelectionEnd = this.mValue.Length;
  375. this.mSelectionStart = this.mSelectionEnd;
  376. }
  377. }
  378. if (UIInput.mLastIME != compositionString)
  379. {
  380. if (this.mValue.Length != this.mSelectionStart)
  381. {
  382. this.mSelectionEnd = ((!string.IsNullOrEmpty(compositionString)) ? (this.mSelectionStart + compositionString.Length) : this.mSelectionStart);
  383. this.InputJapaneseInMiddleOfSentence = true;
  384. }
  385. else
  386. {
  387. this.mSelectionEnd = ((!string.IsNullOrEmpty(compositionString)) ? (this.mValue.Length + compositionString.Length) : this.mSelectionStart);
  388. }
  389. UIInput.mLastIME = compositionString;
  390. this.UpdateLabel();
  391. this.ExecuteOnChange();
  392. this.InputJapaneseInMiddleOfSentence = false;
  393. }
  394. if (this.mCaret != null && this.mNextBlink < RealTime.time)
  395. {
  396. this.mNextBlink = RealTime.time + 0.5f;
  397. this.mCaret.enabled = !this.mCaret.enabled;
  398. }
  399. if (this.isSelected && this.mLastAlpha != this.label.finalAlpha)
  400. {
  401. this.UpdateLabel();
  402. }
  403. if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter))
  404. {
  405. bool flag = this.onReturnKey == UIInput.OnReturnKey.NewLine || (this.onReturnKey == UIInput.OnReturnKey.Default && this.label.multiLine && !Input.GetKey(KeyCode.LeftControl) && !Input.GetKey(KeyCode.RightControl) && this.label.overflowMethod != UILabel.Overflow.ClampContent && this.validation == UIInput.Validation.None);
  406. if (flag)
  407. {
  408. this.Insert("\n");
  409. }
  410. else
  411. {
  412. UICamera.currentScheme = UICamera.ControlScheme.Controller;
  413. UICamera.currentKey = KeyCode.Return;
  414. this.Submit();
  415. UICamera.currentKey = KeyCode.None;
  416. }
  417. }
  418. }
  419. else if (this.mCaret)
  420. {
  421. this.mCaret.enabled = false;
  422. }
  423. }
  424. protected void DoBackspace()
  425. {
  426. if (!string.IsNullOrEmpty(this.mValue))
  427. {
  428. if (this.mSelectionStart == this.mSelectionEnd)
  429. {
  430. if (this.mSelectionStart < 1)
  431. {
  432. return;
  433. }
  434. this.mSelectionEnd--;
  435. }
  436. this.Insert(string.Empty);
  437. }
  438. }
  439. public virtual bool ProcessEvent(Event ev)
  440. {
  441. if (this.label == null)
  442. {
  443. return false;
  444. }
  445. if (!this.isSelected)
  446. {
  447. return false;
  448. }
  449. if (this.mCaret)
  450. {
  451. this.mCaret.enabled = false;
  452. }
  453. RuntimePlatform platform = Application.platform;
  454. bool flag = platform == RuntimePlatform.OSXEditor || platform == RuntimePlatform.OSXPlayer || platform == RuntimePlatform.OSXWebPlayer;
  455. bool flag2 = (!flag) ? ((ev.modifiers & EventModifiers.Control) != EventModifiers.None) : ((ev.modifiers & EventModifiers.Command) != EventModifiers.None);
  456. if ((ev.modifiers & EventModifiers.Alt) != EventModifiers.None)
  457. {
  458. flag2 = false;
  459. }
  460. bool flag3 = (ev.modifiers & EventModifiers.Shift) != EventModifiers.None;
  461. KeyCode keyCode = ev.keyCode;
  462. switch (keyCode)
  463. {
  464. case KeyCode.UpArrow:
  465. ev.Use();
  466. if (!string.IsNullOrEmpty(this.mValue))
  467. {
  468. this.mSelectionEnd = this.label.GetCharacterIndex(this.mSelectionEnd, KeyCode.UpArrow);
  469. if (this.mSelectionEnd != 0)
  470. {
  471. this.mSelectionEnd += UIInput.mDrawStart;
  472. }
  473. if (!flag3)
  474. {
  475. this.mSelectionStart = this.mSelectionEnd;
  476. }
  477. this.UpdateLabel();
  478. }
  479. return true;
  480. case KeyCode.DownArrow:
  481. ev.Use();
  482. if (!string.IsNullOrEmpty(this.mValue))
  483. {
  484. this.mSelectionEnd = this.label.GetCharacterIndex(this.mSelectionEnd, KeyCode.DownArrow);
  485. if (this.mSelectionEnd != this.label.processedText.Length)
  486. {
  487. this.mSelectionEnd += UIInput.mDrawStart;
  488. }
  489. else
  490. {
  491. this.mSelectionEnd = this.mValue.Length;
  492. }
  493. if (!flag3)
  494. {
  495. this.mSelectionStart = this.mSelectionEnd;
  496. }
  497. this.UpdateLabel();
  498. }
  499. return true;
  500. case KeyCode.RightArrow:
  501. ev.Use();
  502. if (!string.IsNullOrEmpty(this.mValue))
  503. {
  504. this.mSelectionEnd = Mathf.Min(this.mSelectionEnd + 1, this.mValue.Length);
  505. if (!flag3)
  506. {
  507. this.mSelectionStart = this.mSelectionEnd;
  508. }
  509. this.UpdateLabel();
  510. }
  511. return true;
  512. case KeyCode.LeftArrow:
  513. ev.Use();
  514. if (!string.IsNullOrEmpty(this.mValue))
  515. {
  516. this.mSelectionEnd = Mathf.Max(this.mSelectionEnd - 1, 0);
  517. if (!flag3)
  518. {
  519. this.mSelectionStart = this.mSelectionEnd;
  520. }
  521. this.UpdateLabel();
  522. }
  523. return true;
  524. default:
  525. switch (keyCode)
  526. {
  527. case KeyCode.A:
  528. if (flag2)
  529. {
  530. ev.Use();
  531. this.mSelectionStart = 0;
  532. this.mSelectionEnd = this.mValue.Length;
  533. this.UpdateLabel();
  534. }
  535. return true;
  536. default:
  537. switch (keyCode)
  538. {
  539. case KeyCode.V:
  540. if (flag2)
  541. {
  542. ev.Use();
  543. this.Insert(NGUITools.clipboard);
  544. }
  545. return true;
  546. default:
  547. if (keyCode == KeyCode.Backspace)
  548. {
  549. ev.Use();
  550. this.DoBackspace();
  551. return true;
  552. }
  553. if (keyCode != KeyCode.Delete)
  554. {
  555. return false;
  556. }
  557. ev.Use();
  558. if (!string.IsNullOrEmpty(this.mValue))
  559. {
  560. if (this.mSelectionStart == this.mSelectionEnd)
  561. {
  562. if (this.mSelectionStart >= this.mValue.Length)
  563. {
  564. return true;
  565. }
  566. this.mSelectionEnd++;
  567. }
  568. this.Insert(string.Empty);
  569. }
  570. return true;
  571. case KeyCode.X:
  572. if (flag2)
  573. {
  574. ev.Use();
  575. NGUITools.clipboard = this.GetSelection();
  576. this.Insert(string.Empty);
  577. }
  578. return true;
  579. }
  580. break;
  581. case KeyCode.C:
  582. if (flag2)
  583. {
  584. ev.Use();
  585. NGUITools.clipboard = this.GetSelection();
  586. }
  587. return true;
  588. }
  589. break;
  590. case KeyCode.Home:
  591. ev.Use();
  592. if (!string.IsNullOrEmpty(this.mValue))
  593. {
  594. if (this.label.multiLine)
  595. {
  596. this.mSelectionEnd = this.label.GetCharacterIndex(this.mSelectionEnd, KeyCode.Home);
  597. }
  598. else
  599. {
  600. this.mSelectionEnd = 0;
  601. }
  602. if (!flag3)
  603. {
  604. this.mSelectionStart = this.mSelectionEnd;
  605. }
  606. this.UpdateLabel();
  607. }
  608. return true;
  609. case KeyCode.End:
  610. ev.Use();
  611. if (!string.IsNullOrEmpty(this.mValue))
  612. {
  613. if (this.label.multiLine)
  614. {
  615. this.mSelectionEnd = this.label.GetCharacterIndex(this.mSelectionEnd, KeyCode.End);
  616. }
  617. else
  618. {
  619. this.mSelectionEnd = this.mValue.Length;
  620. }
  621. if (!flag3)
  622. {
  623. this.mSelectionStart = this.mSelectionEnd;
  624. }
  625. this.UpdateLabel();
  626. }
  627. return true;
  628. case KeyCode.PageUp:
  629. ev.Use();
  630. if (!string.IsNullOrEmpty(this.mValue))
  631. {
  632. this.mSelectionEnd = 0;
  633. if (!flag3)
  634. {
  635. this.mSelectionStart = this.mSelectionEnd;
  636. }
  637. this.UpdateLabel();
  638. }
  639. return true;
  640. case KeyCode.PageDown:
  641. ev.Use();
  642. if (!string.IsNullOrEmpty(this.mValue))
  643. {
  644. this.mSelectionEnd = this.mValue.Length;
  645. if (!flag3)
  646. {
  647. this.mSelectionStart = this.mSelectionEnd;
  648. }
  649. this.UpdateLabel();
  650. }
  651. return true;
  652. }
  653. }
  654. protected virtual void Insert(string text)
  655. {
  656. string leftText = this.GetLeftText();
  657. string rightText = this.GetRightText();
  658. int length = rightText.Length;
  659. StringBuilder stringBuilder = new StringBuilder(leftText.Length + rightText.Length + text.Length);
  660. stringBuilder.Append(leftText);
  661. int i = 0;
  662. int length2 = text.Length;
  663. while (i < length2)
  664. {
  665. char c = text[i];
  666. if (c == '\b')
  667. {
  668. this.DoBackspace();
  669. }
  670. else
  671. {
  672. if (this.characterLimit > 0 && stringBuilder.Length + length >= this.characterLimit)
  673. {
  674. break;
  675. }
  676. if (this.onValidate != null)
  677. {
  678. c = this.onValidate(stringBuilder.ToString(), stringBuilder.Length, c);
  679. }
  680. else if (this.validation != UIInput.Validation.None)
  681. {
  682. c = this.Validate(stringBuilder.ToString(), stringBuilder.Length, c);
  683. }
  684. if (c != '\0')
  685. {
  686. stringBuilder.Append(c);
  687. }
  688. }
  689. i++;
  690. }
  691. this.mSelectionStart = stringBuilder.Length;
  692. this.mSelectionEnd = this.mSelectionStart;
  693. int j = 0;
  694. int length3 = rightText.Length;
  695. while (j < length3)
  696. {
  697. char c2 = rightText[j];
  698. if (this.onValidate != null)
  699. {
  700. c2 = this.onValidate(stringBuilder.ToString(), stringBuilder.Length, c2);
  701. }
  702. else if (this.validation != UIInput.Validation.None)
  703. {
  704. c2 = this.Validate(stringBuilder.ToString(), stringBuilder.Length, c2);
  705. }
  706. if (c2 != '\0')
  707. {
  708. stringBuilder.Append(c2);
  709. }
  710. j++;
  711. }
  712. this.mValue = stringBuilder.ToString();
  713. this.UpdateLabel();
  714. this.ExecuteOnChange();
  715. }
  716. protected string GetLeftText()
  717. {
  718. int num = Mathf.Min(this.mSelectionStart, this.mSelectionEnd);
  719. if (string.IsNullOrEmpty(this.mValue) || num < 0)
  720. {
  721. return string.Empty;
  722. }
  723. if (this.mValue.Length >= num)
  724. {
  725. return this.mValue.Substring(0, num);
  726. }
  727. return string.Empty;
  728. }
  729. protected string GetRightText()
  730. {
  731. int num;
  732. if (this.InputJapaneseInMiddleOfSentence)
  733. {
  734. num = this.mSelectionStart;
  735. }
  736. else
  737. {
  738. num = Mathf.Max(this.mSelectionStart, this.mSelectionEnd);
  739. }
  740. return (!string.IsNullOrEmpty(this.mValue) && num < this.mValue.Length) ? this.mValue.Substring(num) : string.Empty;
  741. }
  742. protected string GetSelection()
  743. {
  744. if (string.IsNullOrEmpty(this.mValue) || this.mSelectionStart == this.mSelectionEnd)
  745. {
  746. return string.Empty;
  747. }
  748. int num = Mathf.Min(this.mSelectionStart, this.mSelectionEnd);
  749. int num2 = Mathf.Max(this.mSelectionStart, this.mSelectionEnd);
  750. return this.mValue.Substring(num, num2 - num);
  751. }
  752. protected int GetCharUnderMouse()
  753. {
  754. Vector3[] worldCorners = this.label.worldCorners;
  755. Ray currentRay = UICamera.currentRay;
  756. Plane plane = new Plane(worldCorners[0], worldCorners[1], worldCorners[2]);
  757. float distance;
  758. return (!plane.Raycast(currentRay, out distance)) ? 0 : (UIInput.mDrawStart + this.label.GetCharacterIndexAtPosition(currentRay.GetPoint(distance), false));
  759. }
  760. protected virtual void OnPress(bool isPressed)
  761. {
  762. if (isPressed && this.isSelected && this.label != null && (UICamera.currentScheme == UICamera.ControlScheme.Mouse || UICamera.currentScheme == UICamera.ControlScheme.Touch))
  763. {
  764. this.selectionEnd = this.GetCharUnderMouse();
  765. if (!Input.GetKey(KeyCode.LeftShift) && !Input.GetKey(KeyCode.RightShift))
  766. {
  767. this.selectionStart = this.mSelectionEnd;
  768. }
  769. }
  770. }
  771. protected virtual void OnDrag(Vector2 delta)
  772. {
  773. if (this.label != null && (UICamera.currentScheme == UICamera.ControlScheme.Mouse || UICamera.currentScheme == UICamera.ControlScheme.Touch))
  774. {
  775. this.selectionEnd = this.GetCharUnderMouse();
  776. }
  777. }
  778. private void OnDisable()
  779. {
  780. this.Cleanup();
  781. }
  782. protected virtual void Cleanup()
  783. {
  784. if (this.mHighlight)
  785. {
  786. this.mHighlight.enabled = false;
  787. }
  788. if (this.mCaret)
  789. {
  790. this.mCaret.enabled = false;
  791. }
  792. if (this.mBlankTex)
  793. {
  794. NGUITools.Destroy(this.mBlankTex);
  795. this.mBlankTex = null;
  796. }
  797. }
  798. public void Submit()
  799. {
  800. if (NGUITools.GetActive(this))
  801. {
  802. this.mValue = this.value;
  803. if (UIInput.current == null)
  804. {
  805. UIInput.current = this;
  806. EventDelegate.Execute(this.onSubmit);
  807. UIInput.current = null;
  808. }
  809. this.SaveToPlayerPrefs(this.mValue);
  810. }
  811. }
  812. public void UpdateLabel()
  813. {
  814. if (this.label != null)
  815. {
  816. if (this.mDoInit)
  817. {
  818. this.Init();
  819. }
  820. bool isSelected = this.isSelected;
  821. string value = this.value;
  822. bool flag = string.IsNullOrEmpty(value) && string.IsNullOrEmpty(Input.compositionString);
  823. this.label.color = ((!flag || isSelected) ? this.activeTextColor : this.mDefaultColor);
  824. string text;
  825. if (flag)
  826. {
  827. text = ((!isSelected) ? this.mDefaultText : string.Empty);
  828. this.RestoreLabelPivot();
  829. }
  830. else
  831. {
  832. if (this.inputType == UIInput.InputType.Password)
  833. {
  834. text = string.Empty;
  835. string str = "*";
  836. if (this.label.bitmapFont != null && this.label.bitmapFont.bmFont != null && this.label.bitmapFont.bmFont.GetGlyph(42) == null)
  837. {
  838. str = "x";
  839. }
  840. int i = 0;
  841. int length = value.Length;
  842. while (i < length)
  843. {
  844. text += str;
  845. i++;
  846. }
  847. }
  848. else
  849. {
  850. text = value;
  851. }
  852. int num;
  853. if (this.InputJapaneseInMiddleOfSentence)
  854. {
  855. num = this.mSelectionStart;
  856. }
  857. else
  858. {
  859. num = ((!isSelected) ? 0 : Mathf.Min(text.Length, this.cursorPosition));
  860. }
  861. string text2 = null;
  862. if (text != null)
  863. {
  864. int length2 = text.Length;
  865. if (length2 < num)
  866. {
  867. text2 = text.Substring(0, length2);
  868. }
  869. else
  870. {
  871. text2 = text.Substring(0, num);
  872. }
  873. }
  874. if (isSelected)
  875. {
  876. text2 += Input.compositionString;
  877. }
  878. if (text.Length > num)
  879. {
  880. text = text2 + text.Substring(num, text.Length - num);
  881. }
  882. else
  883. {
  884. text = text2;
  885. }
  886. if (isSelected && this.label.overflowMethod == UILabel.Overflow.ClampContent && this.label.maxLineCount == 1)
  887. {
  888. int num2 = this.label.CalculateOffsetToFit(text);
  889. if (num2 == 0)
  890. {
  891. UIInput.mDrawStart = 0;
  892. this.RestoreLabelPivot();
  893. }
  894. else if (num < UIInput.mDrawStart)
  895. {
  896. UIInput.mDrawStart = num;
  897. this.SetPivotToLeft();
  898. }
  899. else if (num2 < UIInput.mDrawStart)
  900. {
  901. UIInput.mDrawStart = num2;
  902. this.SetPivotToLeft();
  903. }
  904. else
  905. {
  906. num2 = this.label.CalculateOffsetToFit(text.Substring(0, num));
  907. if (num2 > UIInput.mDrawStart)
  908. {
  909. UIInput.mDrawStart = num2;
  910. this.SetPivotToRight();
  911. }
  912. }
  913. if (UIInput.mDrawStart != 0)
  914. {
  915. text = text.Substring(UIInput.mDrawStart, text.Length - UIInput.mDrawStart);
  916. }
  917. }
  918. else
  919. {
  920. UIInput.mDrawStart = 0;
  921. this.RestoreLabelPivot();
  922. }
  923. }
  924. this.label.text = text;
  925. if (isSelected)
  926. {
  927. int num3 = this.mSelectionStart - UIInput.mDrawStart;
  928. int num4 = this.mSelectionEnd - UIInput.mDrawStart;
  929. if (this.mBlankTex == null)
  930. {
  931. this.mBlankTex = new Texture2D(2, 2, TextureFormat.ARGB32, false);
  932. for (int j = 0; j < 2; j++)
  933. {
  934. for (int k = 0; k < 2; k++)
  935. {
  936. this.mBlankTex.SetPixel(k, j, Color.white);
  937. }
  938. }
  939. this.mBlankTex.Apply();
  940. }
  941. if (num3 != num4)
  942. {
  943. if (this.mHighlight == null)
  944. {
  945. this.mHighlight = NGUITools.AddWidget<UITexture>(this.label.cachedGameObject);
  946. this.mHighlight.name = "Input Highlight";
  947. this.mHighlight.mainTexture = this.mBlankTex;
  948. this.mHighlight.fillGeometry = false;
  949. this.mHighlight.pivot = this.label.pivot;
  950. this.mHighlight.SetAnchor(this.label.cachedTransform);
  951. }
  952. else
  953. {
  954. this.mHighlight.pivot = this.label.pivot;
  955. this.mHighlight.mainTexture = this.mBlankTex;
  956. this.mHighlight.MarkAsChanged();
  957. this.mHighlight.enabled = true;
  958. }
  959. }
  960. if (this.mCaret == null)
  961. {
  962. this.mCaret = NGUITools.AddWidget<UITexture>(this.label.cachedGameObject);
  963. this.mCaret.name = "Input Caret";
  964. this.mCaret.mainTexture = this.mBlankTex;
  965. this.mCaret.fillGeometry = false;
  966. this.mCaret.pivot = this.label.pivot;
  967. this.mCaret.SetAnchor(this.label.cachedTransform);
  968. }
  969. else
  970. {
  971. this.mCaret.pivot = this.label.pivot;
  972. this.mCaret.mainTexture = this.mBlankTex;
  973. this.mCaret.MarkAsChanged();
  974. this.mCaret.enabled = true;
  975. }
  976. if (num3 != num4)
  977. {
  978. this.label.PrintOverlay(num3, num4, this.mCaret.geometry, this.mHighlight.geometry, this.caretColor, this.selectionColor);
  979. this.mHighlight.enabled = this.mHighlight.geometry.hasVertices;
  980. }
  981. else
  982. {
  983. this.label.PrintOverlay(num3, num4, this.mCaret.geometry, null, this.caretColor, this.selectionColor);
  984. if (this.mHighlight != null)
  985. {
  986. this.mHighlight.enabled = false;
  987. }
  988. }
  989. this.mNextBlink = RealTime.time + 0.5f;
  990. this.mLastAlpha = this.label.finalAlpha;
  991. }
  992. else
  993. {
  994. this.Cleanup();
  995. }
  996. }
  997. }
  998. protected void SetPivotToLeft()
  999. {
  1000. Vector2 pivotOffset = NGUIMath.GetPivotOffset(this.mPivot);
  1001. pivotOffset.x = 0f;
  1002. this.label.pivot = NGUIMath.GetPivot(pivotOffset);
  1003. }
  1004. protected void SetPivotToRight()
  1005. {
  1006. Vector2 pivotOffset = NGUIMath.GetPivotOffset(this.mPivot);
  1007. pivotOffset.x = 1f;
  1008. this.label.pivot = NGUIMath.GetPivot(pivotOffset);
  1009. }
  1010. protected void RestoreLabelPivot()
  1011. {
  1012. if (this.label != null && this.label.pivot != this.mPivot)
  1013. {
  1014. this.label.pivot = this.mPivot;
  1015. }
  1016. }
  1017. protected char Validate(string text, int pos, char ch)
  1018. {
  1019. if (this.validation == UIInput.Validation.None || !base.enabled)
  1020. {
  1021. return ch;
  1022. }
  1023. if (this.validation == UIInput.Validation.Integer)
  1024. {
  1025. if (ch >= '0' && ch <= '9')
  1026. {
  1027. return ch;
  1028. }
  1029. if (ch == '-' && pos == 0 && !text.Contains("-"))
  1030. {
  1031. return ch;
  1032. }
  1033. }
  1034. else if (this.validation == UIInput.Validation.Float)
  1035. {
  1036. if (ch >= '0' && ch <= '9')
  1037. {
  1038. return ch;
  1039. }
  1040. if (ch == '-' && pos == 0 && !text.Contains("-"))
  1041. {
  1042. return ch;
  1043. }
  1044. if (ch == '.' && !text.Contains("."))
  1045. {
  1046. return ch;
  1047. }
  1048. }
  1049. else if (this.validation == UIInput.Validation.Alphanumeric)
  1050. {
  1051. if (ch >= 'A' && ch <= 'Z')
  1052. {
  1053. return ch;
  1054. }
  1055. if (ch >= 'a' && ch <= 'z')
  1056. {
  1057. return ch;
  1058. }
  1059. if (ch >= '0' && ch <= '9')
  1060. {
  1061. return ch;
  1062. }
  1063. }
  1064. else if (this.validation == UIInput.Validation.Username)
  1065. {
  1066. if (ch >= 'A' && ch <= 'Z')
  1067. {
  1068. return ch - 'A' + 'a';
  1069. }
  1070. if (ch >= 'a' && ch <= 'z')
  1071. {
  1072. return ch;
  1073. }
  1074. if (ch >= '0' && ch <= '9')
  1075. {
  1076. return ch;
  1077. }
  1078. }
  1079. else if (this.validation == UIInput.Validation.Name)
  1080. {
  1081. char c = (text.Length <= 0) ? ' ' : text[Mathf.Clamp(pos, 0, text.Length - 1)];
  1082. char c2 = (text.Length <= 0) ? '\n' : text[Mathf.Clamp(pos + 1, 0, text.Length - 1)];
  1083. if (ch >= 'a' && ch <= 'z')
  1084. {
  1085. if (c == ' ')
  1086. {
  1087. return ch - 'a' + 'A';
  1088. }
  1089. return ch;
  1090. }
  1091. else if (ch >= 'A' && ch <= 'Z')
  1092. {
  1093. if (c != ' ' && c != '\'')
  1094. {
  1095. return ch - 'A' + 'a';
  1096. }
  1097. return ch;
  1098. }
  1099. else if (ch == '\'')
  1100. {
  1101. if (c != ' ' && c != '\'' && c2 != '\'' && !text.Contains("'"))
  1102. {
  1103. return ch;
  1104. }
  1105. }
  1106. else if (ch == ' ' && c != ' ' && c != '\'' && c2 != ' ' && c2 != '\'')
  1107. {
  1108. return ch;
  1109. }
  1110. }
  1111. return '\0';
  1112. }
  1113. protected void ExecuteOnChange()
  1114. {
  1115. if (UIInput.current == null && EventDelegate.IsValid(this.onChange))
  1116. {
  1117. UIInput.current = this;
  1118. EventDelegate.Execute(this.onChange);
  1119. UIInput.current = null;
  1120. }
  1121. }
  1122. public void RemoveFocus()
  1123. {
  1124. this.isSelected = false;
  1125. }
  1126. public void SaveValue()
  1127. {
  1128. this.SaveToPlayerPrefs(this.mValue);
  1129. }
  1130. public void LoadValue()
  1131. {
  1132. if (!string.IsNullOrEmpty(this.savedAs))
  1133. {
  1134. string text = this.mValue.Replace("\\n", "\n");
  1135. this.mValue = string.Empty;
  1136. this.value = ((!PlayerPrefs.HasKey(this.savedAs)) ? text : PlayerPrefs.GetString(this.savedAs));
  1137. }
  1138. }
  1139. private bool InputJapaneseInMiddleOfSentence;
  1140. public static UIInput current;
  1141. public static UIInput selection;
  1142. public UILabel label;
  1143. public UIInput.InputType inputType;
  1144. public UIInput.OnReturnKey onReturnKey;
  1145. public UIInput.KeyboardType keyboardType;
  1146. public bool hideInput;
  1147. [NonSerialized]
  1148. public bool selectAllTextOnFocus;
  1149. public UIInput.Validation validation;
  1150. public int characterLimit;
  1151. public string savedAs;
  1152. [HideInInspector]
  1153. [SerializeField]
  1154. private GameObject selectOnTab;
  1155. public Color activeTextColor = Color.white;
  1156. public Color caretColor = new Color(1f, 1f, 1f, 0.8f);
  1157. public Color selectionColor = new Color(1f, 0.8745098f, 0.5529412f, 0.5f);
  1158. public List<EventDelegate> onSubmit = new List<EventDelegate>();
  1159. public List<EventDelegate> onChange = new List<EventDelegate>();
  1160. public UIInput.OnValidate onValidate;
  1161. [SerializeField]
  1162. [HideInInspector]
  1163. protected string mValue;
  1164. [NonSerialized]
  1165. protected string mDefaultText = string.Empty;
  1166. [NonSerialized]
  1167. protected Color mDefaultColor = Color.white;
  1168. [NonSerialized]
  1169. protected float mPosition;
  1170. [NonSerialized]
  1171. protected bool mDoInit = true;
  1172. [NonSerialized]
  1173. protected UIWidget.Pivot mPivot;
  1174. [NonSerialized]
  1175. protected bool mLoadSavedValue = true;
  1176. protected static int mDrawStart;
  1177. protected static string mLastIME = string.Empty;
  1178. [NonSerialized]
  1179. protected int mSelectionStart;
  1180. [NonSerialized]
  1181. protected int mSelectionEnd;
  1182. [NonSerialized]
  1183. protected UITexture mHighlight;
  1184. [NonSerialized]
  1185. protected UITexture mCaret;
  1186. [NonSerialized]
  1187. protected Texture2D mBlankTex;
  1188. [NonSerialized]
  1189. protected float mNextBlink;
  1190. [NonSerialized]
  1191. protected float mLastAlpha;
  1192. [NonSerialized]
  1193. protected string mCached = string.Empty;
  1194. [NonSerialized]
  1195. protected int mSelectMe = -1;
  1196. [NonSerialized]
  1197. private UIInputOnGUI mOnGUI;
  1198. public enum InputType
  1199. {
  1200. Standard,
  1201. AutoCorrect,
  1202. Password
  1203. }
  1204. public enum Validation
  1205. {
  1206. None,
  1207. Integer,
  1208. Float,
  1209. Alphanumeric,
  1210. Username,
  1211. Name
  1212. }
  1213. public enum KeyboardType
  1214. {
  1215. Default,
  1216. ASCIICapable,
  1217. NumbersAndPunctuation,
  1218. URL,
  1219. NumberPad,
  1220. PhonePad,
  1221. NamePhonePad,
  1222. EmailAddress
  1223. }
  1224. public enum OnReturnKey
  1225. {
  1226. Default,
  1227. Submit,
  1228. NewLine
  1229. }
  1230. public delegate char OnValidate(string text, int charIndex, char addedChar);
  1231. }