SCInputField.cs 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using UnityEngine.Events;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.Serialization;
  8. using UnityEngine.UI;
  9. using UnityEngine;
  10. #if UNITY_EDITOR
  11. using UnityEditor;
  12. #endif
  13. namespace SC.XR.Unity
  14. {
  15. /// <summary>
  16. /// Editable text input field.
  17. /// </summary>
  18. [AddComponentMenu("UI/SC Input Field", 31)]
  19. public class SCInputField
  20. : Selectable,
  21. IUpdateSelectedHandler,
  22. IBeginDragHandler,
  23. IDragHandler,
  24. IEndDragHandler,
  25. IPointerClickHandler,
  26. ISubmitHandler,
  27. ICanvasElement,
  28. ILayoutElement
  29. {
  30. // Setting the content type acts as a shortcut for setting a combination of InputType, CharacterValidation, LineType, and TouchScreenKeyboardType
  31. public enum ContentType
  32. {
  33. Standard,
  34. Autocorrected,
  35. IntegerNumber,
  36. DecimalNumber,
  37. Alphanumeric,
  38. Name,
  39. EmailAddress,
  40. Password,
  41. Pin,
  42. Custom
  43. }
  44. public enum InputType
  45. {
  46. Standard,
  47. AutoCorrect,
  48. Password,
  49. }
  50. public enum CharacterValidation
  51. {
  52. None,
  53. Integer,
  54. Decimal,
  55. Alphanumeric,
  56. Name,
  57. EmailAddress
  58. }
  59. public enum LineType
  60. {
  61. SingleLine,
  62. MultiLineSubmit,
  63. MultiLineNewline
  64. }
  65. public delegate char OnValidateInput(string text, int charIndex, char addedChar);
  66. [Serializable]
  67. public class SubmitEvent : UnityEvent<string> { }
  68. [Serializable]
  69. public class OnChangeEvent : UnityEvent<string> { }
  70. protected SCKeyboardBase m_Keyboard;
  71. static protected readonly char[] kSeparators = { ' ', '.', ',', '\t', '\r', '\n' };
  72. /// <summary>
  73. /// Text Text used to display the input's value.
  74. /// </summary>
  75. [SerializeField]
  76. [FormerlySerializedAs("text")]
  77. protected Text m_TextComponent;
  78. [SerializeField]
  79. protected Graphic m_Placeholder;
  80. [SerializeField]
  81. protected ContentType m_ContentType = ContentType.Standard;
  82. /// <summary>
  83. /// Type of data expected by the input field.
  84. /// </summary>
  85. [FormerlySerializedAs("inputType")]
  86. [SerializeField]
  87. protected InputType m_InputType = InputType.Standard;
  88. /// <summary>
  89. /// The character used to hide text in password field.
  90. /// </summary>
  91. [FormerlySerializedAs("asteriskChar")]
  92. [SerializeField]
  93. protected char m_AsteriskChar = '*';
  94. /// <summary>
  95. /// Keyboard type applies to mobile keyboards that get shown.
  96. /// </summary>
  97. [FormerlySerializedAs("keyboardType")]
  98. [SerializeField]
  99. protected TouchScreenKeyboardType m_KeyboardType = TouchScreenKeyboardType.Default;
  100. [SerializeField]
  101. protected LineType m_LineType = LineType.SingleLine;
  102. /// <summary>
  103. /// Should hide mobile input.
  104. /// </summary>
  105. [FormerlySerializedAs("hideMobileInput")]
  106. [SerializeField]
  107. protected bool m_HideMobileInput = false;
  108. /// <summary>
  109. /// What kind of validation to use with the input field's data.
  110. /// </summary>
  111. [FormerlySerializedAs("validation")]
  112. [SerializeField]
  113. protected CharacterValidation m_CharacterValidation = CharacterValidation.None;
  114. /// <summary>
  115. /// Maximum number of characters allowed before input no longer works.
  116. /// </summary>
  117. [FormerlySerializedAs("characterLimit")]
  118. [SerializeField]
  119. protected int m_CharacterLimit = 0;
  120. /// <summary>
  121. /// Event delegates triggered when the input field submits its data.
  122. /// </summary>
  123. [FormerlySerializedAs("onSubmit")]
  124. [FormerlySerializedAs("m_OnSubmit")]
  125. [FormerlySerializedAs("m_EndEdit")]
  126. [SerializeField]
  127. protected SubmitEvent m_OnEndEdit = new SubmitEvent();
  128. /// <summary>
  129. /// Event delegates triggered when the input field changes its data.
  130. /// </summary>
  131. [FormerlySerializedAs("onValueChange")]
  132. [FormerlySerializedAs("m_OnValueChange")]
  133. [SerializeField]
  134. protected OnChangeEvent m_OnValueChanged = new OnChangeEvent();
  135. /// <summary>
  136. /// Custom validation callback.
  137. /// </summary>
  138. [FormerlySerializedAs("onValidateInput")]
  139. [SerializeField]
  140. protected OnValidateInput m_OnValidateInput;
  141. [SerializeField]
  142. protected Color m_CaretColor = new Color(50f / 255f, 50f / 255f, 50f / 255f, 1f);
  143. [SerializeField]
  144. protected bool m_CustomCaretColor = false;
  145. [FormerlySerializedAs("selectionColor")]
  146. [SerializeField]
  147. protected Color m_SelectionColor = new Color(168f / 255f, 206f / 255f, 255f / 255f, 192f / 255f);
  148. /// <summary>
  149. /// Input field's value.
  150. /// </summary>
  151. [SerializeField]
  152. [FormerlySerializedAs("mValue")]
  153. protected string m_Text = string.Empty;
  154. [SerializeField]
  155. [Range(0f, 4f)]
  156. protected float m_CaretBlinkRate = 0.85f;
  157. [SerializeField]
  158. [Range(1, 5)]
  159. protected int m_CaretWidth = 1;
  160. [SerializeField]
  161. protected bool m_ReadOnly = false;
  162. [SerializeField]
  163. protected SCKeyboardEnum m_SCKeyboardEnum = SCKeyboardEnum.SCKeyboard3D;
  164. [SerializeField]
  165. protected bool m_UseCustomTransform;
  166. [SerializeField]
  167. protected Vector3 m_CustomPosition;
  168. [SerializeField]
  169. protected Vector3 m_CustomRotation;
  170. [SerializeField]
  171. protected Vector3 m_CustomLocalScale;
  172. protected int m_CaretPosition = 0;
  173. protected int m_CaretSelectPosition = 0;
  174. protected RectTransform caretRectTrans = null;
  175. protected UIVertex[] m_CursorVerts = null;
  176. protected TextGenerator m_InputTextCache;
  177. protected CanvasRenderer m_CachedInputRenderer;
  178. protected bool m_PreventFontCallback = false;
  179. [NonSerialized] protected Mesh m_Mesh;
  180. protected bool m_AllowInput = false;
  181. protected bool m_ShouldActivateNextUpdate = false;
  182. protected bool m_UpdateDrag = false;
  183. protected bool m_DragPositionOutOfBounds = false;
  184. protected const float kHScrollSpeed = 0.05f;
  185. protected const float kVScrollSpeed = 0.10f;
  186. protected bool m_CaretVisible;
  187. protected Coroutine m_BlinkCoroutine = null;
  188. protected float m_BlinkStartTime = 0.0f;
  189. protected int m_DrawStart = 0;
  190. protected int m_DrawEnd = 0;
  191. protected Coroutine m_DragCoroutine = null;
  192. protected string m_OriginalText = "";
  193. protected bool m_WasCanceled = false;
  194. protected bool m_HasDoneFocusTransition = false;
  195. protected BaseInput input
  196. {
  197. get
  198. {
  199. if (EventSystem.current && EventSystem.current.currentInputModule)
  200. return EventSystem.current.currentInputModule.input;
  201. return null;
  202. }
  203. }
  204. protected string compositionString
  205. {
  206. get { return input != null ? input.compositionString : Input.compositionString; }
  207. }
  208. // Doesn't include dot and @ on purpose! See usage for details.
  209. const string kEmailSpecialCharacters = "!#$%&'*+-/=?^_`{|}~";
  210. protected SCInputField()
  211. {
  212. EnforceTextHOverflow();
  213. }
  214. protected Mesh mesh
  215. {
  216. get
  217. {
  218. if (m_Mesh == null)
  219. m_Mesh = new Mesh();
  220. return m_Mesh;
  221. }
  222. }
  223. protected TextGenerator cachedInputTextGenerator
  224. {
  225. get
  226. {
  227. if (m_InputTextCache == null)
  228. m_InputTextCache = new TextGenerator();
  229. return m_InputTextCache;
  230. }
  231. }
  232. /// <summary>
  233. /// Should the mobile keyboard input be hidden.
  234. /// </summary>
  235. public virtual bool shouldHideMobileInput
  236. {
  237. set
  238. {
  239. SCSetPropertyUtility.SetStruct(ref m_HideMobileInput, value);
  240. }
  241. get
  242. {
  243. switch (Application.platform)
  244. {
  245. case RuntimePlatform.Android:
  246. case RuntimePlatform.IPhonePlayer:
  247. case RuntimePlatform.TizenPlayer:
  248. case RuntimePlatform.tvOS:
  249. return m_HideMobileInput;
  250. }
  251. return true;
  252. }
  253. }
  254. public bool m_ShouldActivateOnSelect;
  255. protected bool shouldActivateOnSelect
  256. {
  257. get
  258. {
  259. return m_ShouldActivateOnSelect = Application.platform != RuntimePlatform.tvOS;
  260. }
  261. }
  262. /// <summary>
  263. /// Input field's current text value.
  264. /// </summary>
  265. public virtual string text
  266. {
  267. get
  268. {
  269. return m_Text;
  270. }
  271. set
  272. {
  273. UnityEngine.Debug.Log("Set text value " + value);
  274. if (this.text == value)
  275. return;
  276. if (value == null)
  277. value = "";
  278. value = value.Replace("\0", string.Empty); // remove embedded nulls
  279. if (m_LineType == LineType.SingleLine)
  280. value = value.Replace("\n", "").Replace("\t", "");
  281. // If we have an input validator, validate the input and apply the character limit at the same time.
  282. if (onValidateInput != null || characterValidation != CharacterValidation.None)
  283. {
  284. m_Text = "";
  285. OnValidateInput validatorMethod = onValidateInput ?? Validate;
  286. m_CaretPosition = m_CaretSelectPosition = value.Length;
  287. int charactersToCheck = characterLimit > 0 ? Math.Min(characterLimit, value.Length) : value.Length;
  288. for (int i = 0; i < charactersToCheck; ++i)
  289. {
  290. char c = validatorMethod(m_Text, m_Text.Length, value[i]);
  291. if (c != 0)
  292. m_Text += c;
  293. }
  294. }
  295. else
  296. {
  297. m_Text = characterLimit > 0 && value.Length > characterLimit ? value.Substring(0, characterLimit) : value;
  298. }
  299. #if UNITY_EDITOR
  300. if (!Application.isPlaying)
  301. {
  302. SendOnValueChangedAndUpdateLabel();
  303. return;
  304. }
  305. #endif
  306. if (m_Keyboard != null)
  307. m_Keyboard.text = m_Text;
  308. if (m_CaretPosition > m_Text.Length)
  309. m_CaretPosition = m_CaretSelectPosition = m_Text.Length;
  310. else if (m_CaretSelectPosition > m_Text.Length)
  311. m_CaretSelectPosition = m_Text.Length;
  312. SendOnValueChangedAndUpdateLabel();
  313. }
  314. }
  315. public bool isFocused
  316. {
  317. get { return m_AllowInput; }
  318. }
  319. public float caretBlinkRate
  320. {
  321. get { return m_CaretBlinkRate; }
  322. set
  323. {
  324. if (SCSetPropertyUtility.SetStruct(ref m_CaretBlinkRate, value))
  325. {
  326. if (m_AllowInput)
  327. SetCaretActive();
  328. }
  329. }
  330. }
  331. public int caretWidth { get { return m_CaretWidth; } set { if (SCSetPropertyUtility.SetStruct(ref m_CaretWidth, value)) MarkGeometryAsDirty(); } }
  332. public Text textComponent
  333. {
  334. get { return m_TextComponent; }
  335. set
  336. {
  337. if (m_TextComponent != null)
  338. {
  339. m_TextComponent.UnregisterDirtyVerticesCallback(MarkGeometryAsDirty);
  340. m_TextComponent.UnregisterDirtyVerticesCallback(UpdateLabel);
  341. m_TextComponent.UnregisterDirtyMaterialCallback(UpdateCaretMaterial);
  342. }
  343. if (SCSetPropertyUtility.SetClass(ref m_TextComponent, value))
  344. {
  345. EnforceTextHOverflow();
  346. if (m_TextComponent != null)
  347. {
  348. m_TextComponent.RegisterDirtyVerticesCallback(MarkGeometryAsDirty);
  349. m_TextComponent.RegisterDirtyVerticesCallback(UpdateLabel);
  350. m_TextComponent.RegisterDirtyMaterialCallback(UpdateCaretMaterial);
  351. }
  352. }
  353. }
  354. }
  355. public Graphic placeholder { get { return m_Placeholder; } set { SCSetPropertyUtility.SetClass(ref m_Placeholder, value); } }
  356. public Color caretColor { get { return customCaretColor ? m_CaretColor : textComponent.color; } set { if (SCSetPropertyUtility.SetColor(ref m_CaretColor, value)) MarkGeometryAsDirty(); } }
  357. public bool customCaretColor { get { return m_CustomCaretColor; } set { if (m_CustomCaretColor != value) { m_CustomCaretColor = value; MarkGeometryAsDirty(); } } }
  358. public Color selectionColor { get { return m_SelectionColor; } set { if (SCSetPropertyUtility.SetColor(ref m_SelectionColor, value)) MarkGeometryAsDirty(); } }
  359. public SubmitEvent onEndEdit { get { return m_OnEndEdit; } set { SCSetPropertyUtility.SetClass(ref m_OnEndEdit, value); } }
  360. [Obsolete("onValueChange has been renamed to onValueChanged")]
  361. public OnChangeEvent onValueChange { get { return onValueChanged; } set { onValueChanged = value; } }
  362. public OnChangeEvent onValueChanged { get { return m_OnValueChanged; } set { SCSetPropertyUtility.SetClass(ref m_OnValueChanged, value); } }
  363. public OnValidateInput onValidateInput { get { return m_OnValidateInput; } set { SCSetPropertyUtility.SetClass(ref m_OnValidateInput, value); } }
  364. public virtual int characterLimit { get { return m_CharacterLimit; } set { if (SCSetPropertyUtility.SetStruct(ref m_CharacterLimit, Math.Max(0, value))) UpdateLabel(); } }
  365. // Content Type related
  366. public ContentType contentType { get { return m_ContentType; } set { if (SCSetPropertyUtility.SetStruct(ref m_ContentType, value)) EnforceContentType(); } }
  367. public LineType lineType
  368. {
  369. get { return m_LineType; }
  370. set
  371. {
  372. if (SCSetPropertyUtility.SetStruct(ref m_LineType, value))
  373. {
  374. SetToCustomIfContentTypeIsNot(ContentType.Standard, ContentType.Autocorrected);
  375. EnforceTextHOverflow();
  376. }
  377. }
  378. }
  379. public InputType inputType { get { return m_InputType; } set { if (SCSetPropertyUtility.SetStruct(ref m_InputType, value)) SetToCustom(); } }
  380. public virtual TouchScreenKeyboardType keyboardType
  381. {
  382. get { return m_KeyboardType; }
  383. set
  384. {
  385. #if UNITY_EDITOR
  386. if (EditorUserBuildSettings.activeBuildTarget != BuildTarget.WiiU)
  387. {
  388. if (value == TouchScreenKeyboardType.NintendoNetworkAccount) ;
  389. }
  390. #elif !UNITY_WIIU
  391. if (value == TouchScreenKeyboardType.NintendoNetworkAccount)
  392. Debug.LogWarning("Invalid InputField.keyboardType value set. TouchScreenKeyboardType.NintendoNetworkAccount only applies to the Wii U. InputField.keyboardType will default to TouchScreenKeyboardType.Default .");
  393. #endif
  394. if (SCSetPropertyUtility.SetStruct(ref m_KeyboardType, value))
  395. SetToCustom();
  396. }
  397. }
  398. public CharacterValidation characterValidation { get { return m_CharacterValidation; } set { if (SCSetPropertyUtility.SetStruct(ref m_CharacterValidation, value)) SetToCustom(); } }
  399. public bool readOnly { get { return m_ReadOnly; } set { m_ReadOnly = value; } }
  400. // Derived property
  401. public bool multiLine { get { return m_LineType == LineType.MultiLineNewline || lineType == LineType.MultiLineSubmit; } }
  402. // Not shown in Inspector.
  403. public char asteriskChar { get { return m_AsteriskChar; } set { if (SCSetPropertyUtility.SetStruct(ref m_AsteriskChar, value)) UpdateLabel(); } }
  404. public bool wasCanceled { get { return m_WasCanceled; } }
  405. protected void ClampPos(ref int pos)
  406. {
  407. if (pos < 0) pos = 0;
  408. else if (pos > text.Length) pos = text.Length;
  409. }
  410. /// <summary>
  411. /// Current position of the cursor.
  412. /// Getters are public Setters are protected
  413. /// </summary>
  414. protected int caretPositionInternal { get { return m_CaretPosition + compositionString.Length; } set { m_CaretPosition = value; ClampPos(ref m_CaretPosition); } }
  415. protected int caretSelectPositionInternal { get { return m_CaretSelectPosition + compositionString.Length; } set { m_CaretSelectPosition = value; ClampPos(ref m_CaretSelectPosition); } }
  416. protected bool hasSelection { get { return caretPositionInternal != caretSelectPositionInternal; } }
  417. #if UNITY_EDITOR
  418. [Obsolete("caretSelectPosition has been deprecated. Use selectionFocusPosition instead (UnityUpgradable) -> selectionFocusPosition", true)]
  419. public int caretSelectPosition { get { return selectionFocusPosition; } protected set { selectionFocusPosition = value; } }
  420. #endif
  421. /// <summary>
  422. /// Get: Returns the focus position as thats the position that moves around even during selection.
  423. /// Set: Set both the anchor and focus position such that a selection doesn't happen
  424. /// </summary>
  425. public int caretPosition
  426. {
  427. get { return m_CaretSelectPosition + compositionString.Length; }
  428. set { selectionAnchorPosition = value; selectionFocusPosition = value; }
  429. }
  430. /// <summary>
  431. /// Get: Returns the fixed position of selection
  432. /// Set: If Input.compositionString is 0 set the fixed position
  433. /// </summary>
  434. public int selectionAnchorPosition
  435. {
  436. get { return m_CaretPosition + compositionString.Length; }
  437. set
  438. {
  439. if (compositionString.Length != 0)
  440. return;
  441. m_CaretPosition = value;
  442. ClampPos(ref m_CaretPosition);
  443. }
  444. }
  445. /// <summary>
  446. /// Get: Returns the variable position of selection
  447. /// Set: If Input.compositionString is 0 set the variable position
  448. /// </summary>
  449. public int selectionFocusPosition
  450. {
  451. get { return m_CaretSelectPosition + compositionString.Length; }
  452. set
  453. {
  454. if (compositionString.Length != 0)
  455. return;
  456. m_CaretSelectPosition = value;
  457. ClampPos(ref m_CaretSelectPosition);
  458. }
  459. }
  460. #if UNITY_EDITOR
  461. // Remember: This is NOT related to text validation!
  462. // This is Unity's own OnValidate method which is invoked when changing values in the Inspector.
  463. protected override void OnValidate()
  464. {
  465. base.OnValidate();
  466. EnforceContentType();
  467. EnforceTextHOverflow();
  468. m_CharacterLimit = Math.Max(0, m_CharacterLimit);
  469. //This can be invoked before OnEnabled is called. So we shouldn't be accessing other objects, before OnEnable is called.
  470. if (!IsActive())
  471. return;
  472. UpdateLabel();
  473. if (m_AllowInput)
  474. SetCaretActive();
  475. }
  476. #endif // if UNITY_EDITOR
  477. protected override void OnEnable()
  478. {
  479. base.OnEnable();
  480. if (m_Text == null)
  481. m_Text = string.Empty;
  482. m_DrawStart = 0;
  483. m_DrawEnd = m_Text.Length;
  484. // If we have a cached renderer then we had OnDisable called so just restore the material.
  485. if (m_CachedInputRenderer != null)
  486. m_CachedInputRenderer.SetMaterial(m_TextComponent.GetModifiedMaterial(Graphic.defaultGraphicMaterial), Texture2D.whiteTexture);
  487. if (m_TextComponent != null)
  488. {
  489. m_TextComponent.RegisterDirtyVerticesCallback(MarkGeometryAsDirty);
  490. m_TextComponent.RegisterDirtyVerticesCallback(UpdateLabel);
  491. m_TextComponent.RegisterDirtyMaterialCallback(UpdateCaretMaterial);
  492. UpdateLabel();
  493. }
  494. }
  495. protected override void OnDisable()
  496. {
  497. // the coroutine will be terminated, so this will ensure it restarts when we are next activated
  498. m_BlinkCoroutine = null;
  499. DeactivateInputField();
  500. if (m_TextComponent != null)
  501. {
  502. m_TextComponent.UnregisterDirtyVerticesCallback(MarkGeometryAsDirty);
  503. m_TextComponent.UnregisterDirtyVerticesCallback(UpdateLabel);
  504. m_TextComponent.UnregisterDirtyMaterialCallback(UpdateCaretMaterial);
  505. }
  506. CanvasUpdateRegistry.UnRegisterCanvasElementForRebuild(this);
  507. // Clear needs to be called otherwise sync never happens as the object is disabled.
  508. if (m_CachedInputRenderer != null)
  509. m_CachedInputRenderer.Clear();
  510. if (m_Mesh != null)
  511. DestroyImmediate(m_Mesh);
  512. m_Mesh = null;
  513. base.OnDisable();
  514. }
  515. protected virtual IEnumerator CaretBlink()
  516. {
  517. // Always ensure caret is initially visible since it can otherwise be confusing for a moment.
  518. m_CaretVisible = true;
  519. yield return null;
  520. while (isFocused && m_CaretBlinkRate > 0)
  521. {
  522. // the blink rate is expressed as a frequency
  523. float blinkPeriod = 1f / m_CaretBlinkRate;
  524. // the caret should be ON if we are in the first half of the blink period
  525. bool blinkState = (Time.unscaledTime - m_BlinkStartTime) % blinkPeriod < blinkPeriod / 2;
  526. if (m_CaretVisible != blinkState)
  527. {
  528. m_CaretVisible = blinkState;
  529. if (!hasSelection)
  530. MarkGeometryAsDirty();
  531. }
  532. // Then wait again.
  533. yield return null;
  534. }
  535. m_BlinkCoroutine = null;
  536. }
  537. protected void SetCaretVisible()
  538. {
  539. if (!m_AllowInput)
  540. return;
  541. m_CaretVisible = true;
  542. m_BlinkStartTime = Time.unscaledTime;
  543. SetCaretActive();
  544. }
  545. // SetCaretActive will not set the caret immediately visible - it will wait for the next time to blink.
  546. // However, it will handle things correctly if the blink speed changed from zero to non-zero or non-zero to zero.
  547. protected void SetCaretActive()
  548. {
  549. if (!m_AllowInput)
  550. return;
  551. if (m_CaretBlinkRate > 0.0f)
  552. {
  553. if (m_BlinkCoroutine == null)
  554. m_BlinkCoroutine = StartCoroutine(CaretBlink());
  555. }
  556. else
  557. {
  558. m_CaretVisible = true;
  559. }
  560. }
  561. protected void UpdateCaretMaterial()
  562. {
  563. if (m_TextComponent != null && m_CachedInputRenderer != null)
  564. m_CachedInputRenderer.SetMaterial(m_TextComponent.GetModifiedMaterial(Graphic.defaultGraphicMaterial), Texture2D.whiteTexture);
  565. }
  566. protected void OnFocus()
  567. {
  568. SelectAll();
  569. }
  570. protected void SelectAll()
  571. {
  572. caretPositionInternal = text.Length;
  573. caretSelectPositionInternal = 0;
  574. }
  575. public void MoveTextEnd(bool shift)
  576. {
  577. int position = text.Length;
  578. if (shift)
  579. {
  580. caretSelectPositionInternal = position;
  581. }
  582. else
  583. {
  584. caretPositionInternal = position;
  585. caretSelectPositionInternal = caretPositionInternal;
  586. }
  587. UpdateLabel();
  588. }
  589. public void MoveTextStart(bool shift)
  590. {
  591. int position = 0;
  592. if (shift)
  593. {
  594. caretSelectPositionInternal = position;
  595. }
  596. else
  597. {
  598. caretPositionInternal = position;
  599. caretSelectPositionInternal = caretPositionInternal;
  600. }
  601. UpdateLabel();
  602. }
  603. static protected string clipboard
  604. {
  605. get
  606. {
  607. return GUIUtility.systemCopyBuffer;
  608. }
  609. set
  610. {
  611. GUIUtility.systemCopyBuffer = value;
  612. }
  613. }
  614. protected virtual bool InPlaceEditing()
  615. {
  616. return false;//!TouchScreenKeyboard.isSupported;
  617. }
  618. protected virtual void UpdateCaretFromKeyboard()
  619. {
  620. //Do nothing
  621. //var selectionRange = m_Keyboard.selection;
  622. //var selectionStart = selectionRange.start;
  623. //var selectionEnd = selectionRange.end;
  624. //var caretChanged = false;
  625. //if (caretPositionInternal != selectionStart)
  626. //{
  627. // caretChanged = true;
  628. // caretPositionInternal = selectionStart;
  629. //}
  630. //if (caretSelectPositionInternal != selectionEnd)
  631. //{
  632. // caretSelectPositionInternal = selectionEnd;
  633. // caretChanged = true;
  634. //}
  635. //if (caretChanged)
  636. //{
  637. // m_BlinkStartTime = Time.unscaledTime;
  638. // UpdateLabel();
  639. //}
  640. }
  641. /// <summary>
  642. /// Update the text based on input.
  643. /// </summary>
  644. // TODO: Make LateUpdate a coroutine instead. Allows us to control the update to only be when the field is active.
  645. protected virtual void LateUpdate()
  646. {
  647. // Only activate if we are not already activated.
  648. if (m_ShouldActivateNextUpdate)
  649. {
  650. if (!isFocused)
  651. {
  652. ActivateInputFieldInternal();
  653. m_ShouldActivateNextUpdate = false;
  654. return;
  655. }
  656. // Reset as we are already activated.
  657. m_ShouldActivateNextUpdate = false;
  658. }
  659. if (InPlaceEditing() || !isFocused)
  660. {
  661. return;
  662. }
  663. AssignPositioningIfNeeded();
  664. if (m_Keyboard == null || m_Keyboard.Done)
  665. {
  666. if (m_Keyboard != null)
  667. {
  668. if (!m_ReadOnly)
  669. {
  670. UnityEngine.Debug.Log("Set text Value");
  671. text = m_Keyboard.text;
  672. }
  673. if (m_Keyboard.WasCanceled)
  674. m_WasCanceled = true;
  675. }
  676. OnDeselect(null);
  677. return;
  678. }
  679. string val = m_Keyboard.text;
  680. if (m_Text != val)
  681. {
  682. UnityEngine.Debug.Log("m_Text != val " + m_Text + " " + val);
  683. if (m_ReadOnly)
  684. {
  685. m_Keyboard.text = m_Text;
  686. }
  687. else
  688. {
  689. m_Text = "";
  690. for (int i = 0; i < val.Length; ++i)
  691. {
  692. char c = val[i];
  693. if (c == '\r' || (int)c == 3)
  694. c = '\n';
  695. if (onValidateInput != null)
  696. c = onValidateInput(m_Text, m_Text.Length, c);
  697. else if (characterValidation != CharacterValidation.None)
  698. c = Validate(m_Text, m_Text.Length, c);
  699. if (lineType == LineType.MultiLineSubmit && c == '\n')
  700. {
  701. m_Keyboard.text = m_Text;
  702. OnDeselect(null);
  703. return;
  704. }
  705. if (c != 0)
  706. m_Text += c;
  707. }
  708. if (characterLimit > 0 && m_Text.Length > characterLimit)
  709. m_Text = m_Text.Substring(0, characterLimit);
  710. if (false)//(m_Keyboard.canGetSelection)
  711. {
  712. UpdateCaretFromKeyboard();
  713. }
  714. else
  715. {
  716. caretPositionInternal = caretSelectPositionInternal = m_Text.Length;
  717. }
  718. // Set keyboard text before updating label, as we might have changed it with validation
  719. // and update label will take the old value from keyboard if we don't change it here
  720. if (m_Text != val)
  721. m_Keyboard.text = m_Text;
  722. SendOnValueChangedAndUpdateLabel();
  723. }
  724. }
  725. else if (false)//(m_Keyboard.canGetSelection)
  726. {
  727. UpdateCaretFromKeyboard();
  728. }
  729. if (m_Keyboard.Done)
  730. {
  731. UnityEngine.Debug.Log("KeyboardDone");
  732. if (m_Keyboard.WasCanceled)
  733. m_WasCanceled = true;
  734. OnDeselect(null);
  735. }
  736. }
  737. [Obsolete("This function is no longer used. Please use RectTransformUtility.ScreenPointToLocalPointInRectangle() instead.")]
  738. public Vector2 ScreenToLocal(Vector2 screen)
  739. {
  740. var theCanvas = m_TextComponent.canvas;
  741. if (theCanvas == null)
  742. return screen;
  743. Vector3 pos = Vector3.zero;
  744. if (theCanvas.renderMode == RenderMode.ScreenSpaceOverlay)
  745. {
  746. pos = m_TextComponent.transform.InverseTransformPoint(screen);
  747. }
  748. else if (theCanvas.worldCamera != null)
  749. {
  750. Ray mouseRay = theCanvas.worldCamera.ScreenPointToRay(screen);
  751. float dist;
  752. Plane plane = new Plane(m_TextComponent.transform.forward, m_TextComponent.transform.position);
  753. plane.Raycast(mouseRay, out dist);
  754. pos = m_TextComponent.transform.InverseTransformPoint(mouseRay.GetPoint(dist));
  755. }
  756. return new Vector2(pos.x, pos.y);
  757. }
  758. private int GetUnclampedCharacterLineFromPosition(Vector2 pos, TextGenerator generator)
  759. {
  760. if (!multiLine)
  761. return 0;
  762. // transform y to local scale
  763. float y = pos.y * m_TextComponent.pixelsPerUnit;
  764. float lastBottomY = 0.0f;
  765. for (int i = 0; i < generator.lineCount; ++i)
  766. {
  767. float topY = generator.lines[i].topY;
  768. float bottomY = topY - generator.lines[i].height;
  769. // pos is somewhere in the leading above this line
  770. if (y > topY)
  771. {
  772. // determine which line we're closer to
  773. float leading = topY - lastBottomY;
  774. if (y > topY - 0.5f * leading)
  775. return i - 1;
  776. else
  777. return i;
  778. }
  779. if (y > bottomY)
  780. return i;
  781. lastBottomY = bottomY;
  782. }
  783. // Position is after last line.
  784. return generator.lineCount;
  785. }
  786. /// <summary>
  787. /// Given an input position in local space on the Text return the index for the selection cursor at this position.
  788. /// </summary>
  789. protected int GetCharacterIndexFromPosition(Vector2 pos)
  790. {
  791. TextGenerator gen = m_TextComponent.cachedTextGenerator;
  792. if (gen.lineCount == 0)
  793. return 0;
  794. int line = GetUnclampedCharacterLineFromPosition(pos, gen);
  795. if (line < 0)
  796. return 0;
  797. if (line >= gen.lineCount)
  798. return gen.characterCountVisible;
  799. int startCharIndex = gen.lines[line].startCharIdx;
  800. int endCharIndex = GetLineEndPosition(gen, line);
  801. for (int i = startCharIndex; i < endCharIndex; i++)
  802. {
  803. if (i >= gen.characterCountVisible)
  804. break;
  805. UICharInfo charInfo = gen.characters[i];
  806. Vector2 charPos = charInfo.cursorPos / m_TextComponent.pixelsPerUnit;
  807. float distToCharStart = pos.x - charPos.x;
  808. float distToCharEnd = charPos.x + (charInfo.charWidth / m_TextComponent.pixelsPerUnit) - pos.x;
  809. if (distToCharStart < distToCharEnd)
  810. return i;
  811. }
  812. return endCharIndex;
  813. }
  814. protected virtual bool MayDrag(PointerEventData eventData)
  815. {
  816. return IsActive() &&
  817. IsInteractable() &&
  818. eventData.button == PointerEventData.InputButton.Left &&
  819. m_TextComponent != null &&
  820. m_Keyboard == null;
  821. }
  822. public virtual void OnBeginDrag(PointerEventData eventData)
  823. {
  824. if (!MayDrag(eventData))
  825. return;
  826. m_UpdateDrag = true;
  827. }
  828. public virtual void OnDrag(PointerEventData eventData)
  829. {
  830. if (!MayDrag(eventData))
  831. return;
  832. Vector2 localMousePos;
  833. RectTransformUtility.ScreenPointToLocalPointInRectangle(textComponent.rectTransform, eventData.position, eventData.pressEventCamera, out localMousePos);
  834. caretSelectPositionInternal = GetCharacterIndexFromPosition(localMousePos) + m_DrawStart;
  835. MarkGeometryAsDirty();
  836. m_DragPositionOutOfBounds = !RectTransformUtility.RectangleContainsScreenPoint(textComponent.rectTransform, eventData.position, eventData.pressEventCamera);
  837. if (m_DragPositionOutOfBounds && m_DragCoroutine == null)
  838. m_DragCoroutine = StartCoroutine(MouseDragOutsideRect(eventData));
  839. eventData.Use();
  840. }
  841. protected virtual IEnumerator MouseDragOutsideRect(PointerEventData eventData)
  842. {
  843. while (m_UpdateDrag && m_DragPositionOutOfBounds)
  844. {
  845. Vector2 localMousePos;
  846. RectTransformUtility.ScreenPointToLocalPointInRectangle(textComponent.rectTransform, eventData.position, eventData.pressEventCamera, out localMousePos);
  847. Rect rect = textComponent.rectTransform.rect;
  848. if (multiLine)
  849. {
  850. if (localMousePos.y > rect.yMax)
  851. MoveUp(true, true);
  852. else if (localMousePos.y < rect.yMin)
  853. MoveDown(true, true);
  854. }
  855. else
  856. {
  857. if (localMousePos.x < rect.xMin)
  858. MoveLeft(true, false);
  859. else if (localMousePos.x > rect.xMax)
  860. MoveRight(true, false);
  861. }
  862. UpdateLabel();
  863. float delay = multiLine ? kVScrollSpeed : kHScrollSpeed;
  864. yield return new WaitForSecondsRealtime(delay);
  865. }
  866. m_DragCoroutine = null;
  867. }
  868. public virtual void OnEndDrag(PointerEventData eventData)
  869. {
  870. if (!MayDrag(eventData))
  871. return;
  872. m_UpdateDrag = false;
  873. }
  874. public override void OnPointerDown(PointerEventData eventData)
  875. {
  876. if (!MayDrag(eventData))
  877. return;
  878. EventSystem.current.SetSelectedGameObject(gameObject, eventData);
  879. bool hadFocusBefore = m_AllowInput;
  880. base.OnPointerDown(eventData);
  881. if (!InPlaceEditing())
  882. {
  883. if (m_Keyboard == null || !m_Keyboard.active)
  884. {
  885. OnSelect(eventData);
  886. return;
  887. }
  888. }
  889. // Only set caret position if we didn't just get focus now.
  890. // Otherwise it will overwrite the select all on focus.
  891. if (hadFocusBefore)
  892. {
  893. Vector2 localMousePos;
  894. RectTransformUtility.ScreenPointToLocalPointInRectangle(textComponent.rectTransform, eventData.position, eventData.pressEventCamera, out localMousePos);
  895. caretSelectPositionInternal = caretPositionInternal = GetCharacterIndexFromPosition(localMousePos) + m_DrawStart;
  896. }
  897. UpdateLabel();
  898. eventData.Use();
  899. }
  900. protected enum EditState
  901. {
  902. Continue,
  903. Finish
  904. }
  905. protected virtual EditState KeyPressed(Event evt)
  906. {
  907. var currentEventModifiers = evt.modifiers;
  908. bool ctrl = SystemInfo.operatingSystemFamily == OperatingSystemFamily.MacOSX ? (currentEventModifiers & EventModifiers.Command) != 0 : (currentEventModifiers & EventModifiers.Control) != 0;
  909. bool shift = (currentEventModifiers & EventModifiers.Shift) != 0;
  910. bool alt = (currentEventModifiers & EventModifiers.Alt) != 0;
  911. bool ctrlOnly = ctrl && !alt && !shift;
  912. switch (evt.keyCode)
  913. {
  914. case KeyCode.Backspace:
  915. {
  916. Backspace();
  917. return EditState.Continue;
  918. }
  919. case KeyCode.Delete:
  920. {
  921. ForwardSpace();
  922. return EditState.Continue;
  923. }
  924. case KeyCode.Home:
  925. {
  926. MoveTextStart(shift);
  927. return EditState.Continue;
  928. }
  929. case KeyCode.End:
  930. {
  931. MoveTextEnd(shift);
  932. return EditState.Continue;
  933. }
  934. // Select All
  935. case KeyCode.A:
  936. {
  937. if (ctrlOnly)
  938. {
  939. SelectAll();
  940. return EditState.Continue;
  941. }
  942. break;
  943. }
  944. // Copy
  945. case KeyCode.C:
  946. {
  947. if (ctrlOnly)
  948. {
  949. if (inputType != InputType.Password)
  950. clipboard = GetSelectedString();
  951. else
  952. clipboard = "";
  953. return EditState.Continue;
  954. }
  955. break;
  956. }
  957. // Paste
  958. case KeyCode.V:
  959. {
  960. if (ctrlOnly)
  961. {
  962. Append(clipboard);
  963. return EditState.Continue;
  964. }
  965. break;
  966. }
  967. // Cut
  968. case KeyCode.X:
  969. {
  970. if (ctrlOnly)
  971. {
  972. if (inputType != InputType.Password)
  973. clipboard = GetSelectedString();
  974. else
  975. clipboard = "";
  976. Delete();
  977. SendOnValueChangedAndUpdateLabel();
  978. return EditState.Continue;
  979. }
  980. break;
  981. }
  982. case KeyCode.LeftArrow:
  983. {
  984. MoveLeft(shift, ctrl);
  985. return EditState.Continue;
  986. }
  987. case KeyCode.RightArrow:
  988. {
  989. MoveRight(shift, ctrl);
  990. return EditState.Continue;
  991. }
  992. case KeyCode.UpArrow:
  993. {
  994. MoveUp(shift);
  995. return EditState.Continue;
  996. }
  997. case KeyCode.DownArrow:
  998. {
  999. MoveDown(shift);
  1000. return EditState.Continue;
  1001. }
  1002. // Submit
  1003. case KeyCode.Return:
  1004. case KeyCode.KeypadEnter:
  1005. {
  1006. if (lineType != LineType.MultiLineNewline)
  1007. {
  1008. return EditState.Finish;
  1009. }
  1010. break;
  1011. }
  1012. case KeyCode.Escape:
  1013. {
  1014. m_WasCanceled = true;
  1015. return EditState.Finish;
  1016. }
  1017. }
  1018. char c = evt.character;
  1019. // Don't allow return chars or tabulator key to be entered into single line fields.
  1020. if (!multiLine && (c == '\t' || c == '\r' || c == 10))
  1021. return EditState.Continue;
  1022. // Convert carriage return and end-of-text characters to newline.
  1023. if (c == '\r' || (int)c == 3)
  1024. c = '\n';
  1025. if (IsValidChar(c))
  1026. {
  1027. Append(c);
  1028. }
  1029. if (c == 0)
  1030. {
  1031. if (compositionString.Length > 0)
  1032. {
  1033. UpdateLabel();
  1034. }
  1035. }
  1036. return EditState.Continue;
  1037. }
  1038. protected virtual bool IsValidChar(char c)
  1039. {
  1040. // Delete key on mac
  1041. if ((int)c == 127)
  1042. return false;
  1043. // Accept newline and tab
  1044. if (c == '\t' || c == '\n')
  1045. return true;
  1046. return m_TextComponent.font.HasCharacter(c);
  1047. }
  1048. /// <summary>
  1049. /// Handle the specified event.
  1050. /// </summary>
  1051. protected Event m_ProcessingEvent = new Event();
  1052. public void ProcessEvent(Event e)
  1053. {
  1054. KeyPressed(e);
  1055. }
  1056. public virtual void OnUpdateSelected(BaseEventData eventData)
  1057. {
  1058. if (!isFocused)
  1059. return;
  1060. bool consumedEvent = false;
  1061. while (Event.PopEvent(m_ProcessingEvent))
  1062. {
  1063. if (m_ProcessingEvent.rawType == EventType.KeyDown)
  1064. {
  1065. consumedEvent = true;
  1066. var shouldContinue = KeyPressed(m_ProcessingEvent);
  1067. if (shouldContinue == EditState.Finish)
  1068. {
  1069. DeactivateInputField();
  1070. break;
  1071. }
  1072. }
  1073. switch (m_ProcessingEvent.type)
  1074. {
  1075. case EventType.ValidateCommand:
  1076. case EventType.ExecuteCommand:
  1077. switch (m_ProcessingEvent.commandName)
  1078. {
  1079. case "SelectAll":
  1080. SelectAll();
  1081. consumedEvent = true;
  1082. break;
  1083. }
  1084. break;
  1085. }
  1086. }
  1087. if (consumedEvent)
  1088. UpdateLabel();
  1089. eventData.Use();
  1090. }
  1091. protected string GetSelectedString()
  1092. {
  1093. if (!hasSelection)
  1094. return "";
  1095. int startPos = caretPositionInternal;
  1096. int endPos = caretSelectPositionInternal;
  1097. // Ensure startPos is always less then endPos to make the code simpler
  1098. if (startPos > endPos)
  1099. {
  1100. int temp = startPos;
  1101. startPos = endPos;
  1102. endPos = temp;
  1103. }
  1104. return text.Substring(startPos, endPos - startPos);
  1105. }
  1106. protected int FindtNextWordBegin()
  1107. {
  1108. if (caretSelectPositionInternal + 1 >= text.Length)
  1109. return text.Length;
  1110. int spaceLoc = text.IndexOfAny(kSeparators, caretSelectPositionInternal + 1);
  1111. if (spaceLoc == -1)
  1112. spaceLoc = text.Length;
  1113. else
  1114. spaceLoc++;
  1115. return spaceLoc;
  1116. }
  1117. protected void MoveRight(bool shift, bool ctrl)
  1118. {
  1119. if (hasSelection && !shift)
  1120. {
  1121. // By convention, if we have a selection and move right without holding shift,
  1122. // we just place the cursor at the end.
  1123. caretPositionInternal = caretSelectPositionInternal = Mathf.Max(caretPositionInternal, caretSelectPositionInternal);
  1124. return;
  1125. }
  1126. int position;
  1127. if (ctrl)
  1128. position = FindtNextWordBegin();
  1129. else
  1130. position = caretSelectPositionInternal + 1;
  1131. if (shift)
  1132. caretSelectPositionInternal = position;
  1133. else
  1134. caretSelectPositionInternal = caretPositionInternal = position;
  1135. }
  1136. protected int FindtPrevWordBegin()
  1137. {
  1138. if (caretSelectPositionInternal - 2 < 0)
  1139. return 0;
  1140. int spaceLoc = text.LastIndexOfAny(kSeparators, caretSelectPositionInternal - 2);
  1141. if (spaceLoc == -1)
  1142. spaceLoc = 0;
  1143. else
  1144. spaceLoc++;
  1145. return spaceLoc;
  1146. }
  1147. protected void MoveLeft(bool shift, bool ctrl)
  1148. {
  1149. if (hasSelection && !shift)
  1150. {
  1151. // By convention, if we have a selection and move left without holding shift,
  1152. // we just place the cursor at the start.
  1153. caretPositionInternal = caretSelectPositionInternal = Mathf.Min(caretPositionInternal, caretSelectPositionInternal);
  1154. return;
  1155. }
  1156. int position;
  1157. if (ctrl)
  1158. position = FindtPrevWordBegin();
  1159. else
  1160. position = caretSelectPositionInternal - 1;
  1161. if (shift)
  1162. caretSelectPositionInternal = position;
  1163. else
  1164. caretSelectPositionInternal = caretPositionInternal = position;
  1165. }
  1166. protected virtual int DetermineCharacterLine(int charPos, TextGenerator generator)
  1167. {
  1168. for (int i = 0; i < generator.lineCount - 1; ++i)
  1169. {
  1170. if (generator.lines[i + 1].startCharIdx > charPos)
  1171. return i;
  1172. }
  1173. return generator.lineCount - 1;
  1174. }
  1175. /// <summary>
  1176. /// Use cachedInputTextGenerator as the y component for the UICharInfo is not required
  1177. /// </summary>
  1178. protected virtual int LineUpCharacterPosition(int originalPos, bool goToFirstChar)
  1179. {
  1180. if (originalPos >= cachedInputTextGenerator.characters.Count)
  1181. return 0;
  1182. UICharInfo originChar = cachedInputTextGenerator.characters[originalPos];
  1183. int originLine = DetermineCharacterLine(originalPos, cachedInputTextGenerator);
  1184. // We are on the first line return first character
  1185. if (originLine <= 0)
  1186. return goToFirstChar ? 0 : originalPos;
  1187. int endCharIdx = cachedInputTextGenerator.lines[originLine].startCharIdx - 1;
  1188. for (int i = cachedInputTextGenerator.lines[originLine - 1].startCharIdx; i < endCharIdx; ++i)
  1189. {
  1190. if (cachedInputTextGenerator.characters[i].cursorPos.x >= originChar.cursorPos.x)
  1191. return i;
  1192. }
  1193. return endCharIdx;
  1194. }
  1195. /// <summary>
  1196. /// Use cachedInputTextGenerator as the y component for the UICharInfo is not required
  1197. /// </summary>
  1198. protected virtual int LineDownCharacterPosition(int originalPos, bool goToLastChar)
  1199. {
  1200. if (originalPos >= cachedInputTextGenerator.characterCountVisible)
  1201. return text.Length;
  1202. UICharInfo originChar = cachedInputTextGenerator.characters[originalPos];
  1203. int originLine = DetermineCharacterLine(originalPos, cachedInputTextGenerator);
  1204. // We are on the last line return last character
  1205. if (originLine + 1 >= cachedInputTextGenerator.lineCount)
  1206. return goToLastChar ? text.Length : originalPos;
  1207. // Need to determine end line for next line.
  1208. int endCharIdx = GetLineEndPosition(cachedInputTextGenerator, originLine + 1);
  1209. for (int i = cachedInputTextGenerator.lines[originLine + 1].startCharIdx; i < endCharIdx; ++i)
  1210. {
  1211. if (cachedInputTextGenerator.characters[i].cursorPos.x >= originChar.cursorPos.x)
  1212. return i;
  1213. }
  1214. return endCharIdx;
  1215. }
  1216. protected void MoveDown(bool shift)
  1217. {
  1218. MoveDown(shift, true);
  1219. }
  1220. protected void MoveDown(bool shift, bool goToLastChar)
  1221. {
  1222. if (hasSelection && !shift)
  1223. {
  1224. // If we have a selection and press down without shift,
  1225. // set caret position to end of selection before we move it down.
  1226. caretPositionInternal = caretSelectPositionInternal = Mathf.Max(caretPositionInternal, caretSelectPositionInternal);
  1227. }
  1228. int position = multiLine ? LineDownCharacterPosition(caretSelectPositionInternal, goToLastChar) : text.Length;
  1229. if (shift)
  1230. caretSelectPositionInternal = position;
  1231. else
  1232. caretPositionInternal = caretSelectPositionInternal = position;
  1233. }
  1234. protected void MoveUp(bool shift)
  1235. {
  1236. MoveUp(shift, true);
  1237. }
  1238. protected void MoveUp(bool shift, bool goToFirstChar)
  1239. {
  1240. if (hasSelection && !shift)
  1241. {
  1242. // If we have a selection and press up without shift,
  1243. // set caret position to start of selection before we move it up.
  1244. caretPositionInternal = caretSelectPositionInternal = Mathf.Min(caretPositionInternal, caretSelectPositionInternal);
  1245. }
  1246. int position = multiLine ? LineUpCharacterPosition(caretSelectPositionInternal, goToFirstChar) : 0;
  1247. if (shift)
  1248. caretSelectPositionInternal = position;
  1249. else
  1250. caretSelectPositionInternal = caretPositionInternal = position;
  1251. }
  1252. protected void Delete()
  1253. {
  1254. if (m_ReadOnly)
  1255. return;
  1256. if (caretPositionInternal == caretSelectPositionInternal)
  1257. return;
  1258. if (caretPositionInternal < caretSelectPositionInternal)
  1259. {
  1260. m_Text = text.Substring(0, caretPositionInternal) + text.Substring(caretSelectPositionInternal, text.Length - caretSelectPositionInternal);
  1261. caretSelectPositionInternal = caretPositionInternal;
  1262. }
  1263. else
  1264. {
  1265. m_Text = text.Substring(0, caretSelectPositionInternal) + text.Substring(caretPositionInternal, text.Length - caretPositionInternal);
  1266. caretPositionInternal = caretSelectPositionInternal;
  1267. }
  1268. }
  1269. protected virtual void ForwardSpace()
  1270. {
  1271. if (m_ReadOnly)
  1272. return;
  1273. if (hasSelection)
  1274. {
  1275. Delete();
  1276. SendOnValueChangedAndUpdateLabel();
  1277. }
  1278. else
  1279. {
  1280. if (caretPositionInternal < text.Length)
  1281. {
  1282. m_Text = text.Remove(caretPositionInternal, 1);
  1283. SendOnValueChangedAndUpdateLabel();
  1284. }
  1285. }
  1286. }
  1287. protected virtual void Backspace()
  1288. {
  1289. if (m_ReadOnly)
  1290. return;
  1291. if (hasSelection)
  1292. {
  1293. Delete();
  1294. SendOnValueChangedAndUpdateLabel();
  1295. }
  1296. else
  1297. {
  1298. if (caretPositionInternal > 0)
  1299. {
  1300. m_Text = text.Remove(caretPositionInternal - 1, 1);
  1301. caretSelectPositionInternal = caretPositionInternal = caretPositionInternal - 1;
  1302. SendOnValueChangedAndUpdateLabel();
  1303. }
  1304. }
  1305. }
  1306. // Insert the character and update the label.
  1307. protected virtual void Insert(char c)
  1308. {
  1309. if (m_ReadOnly)
  1310. return;
  1311. string replaceString = c.ToString();
  1312. Delete();
  1313. // Can't go past the character limit
  1314. if (characterLimit > 0 && text.Length >= characterLimit)
  1315. return;
  1316. m_Text = text.Insert(m_CaretPosition, replaceString);
  1317. caretSelectPositionInternal = caretPositionInternal += replaceString.Length;
  1318. SendOnValueChanged();
  1319. }
  1320. protected void SendOnValueChangedAndUpdateLabel()
  1321. {
  1322. SendOnValueChanged();
  1323. UpdateLabel();
  1324. }
  1325. protected void SendOnValueChanged()
  1326. {
  1327. UISystemProfilerApi.AddMarker("InputField.value", this);
  1328. if (onValueChanged != null)
  1329. onValueChanged.Invoke(text);
  1330. }
  1331. /// <summary>
  1332. /// Submit the input field's text.
  1333. /// </summary>
  1334. protected void SendOnSubmit()
  1335. {
  1336. UISystemProfilerApi.AddMarker("InputField.onSubmit", this);
  1337. if (onEndEdit != null)
  1338. onEndEdit.Invoke(m_Text);
  1339. }
  1340. /// <summary>
  1341. /// Append the specified text to the end of the current.
  1342. /// </summary>
  1343. protected virtual void Append(string input)
  1344. {
  1345. if (m_ReadOnly)
  1346. return;
  1347. if (!InPlaceEditing())
  1348. return;
  1349. for (int i = 0, imax = input.Length; i < imax; ++i)
  1350. {
  1351. char c = input[i];
  1352. if (c >= ' ' || c == '\t' || c == '\r' || c == 10 || c == '\n')
  1353. {
  1354. Append(c);
  1355. }
  1356. }
  1357. }
  1358. protected virtual void Append(char input)
  1359. {
  1360. if (m_ReadOnly)
  1361. return;
  1362. if (!InPlaceEditing())
  1363. return;
  1364. // If we have an input validator, validate the input first
  1365. int insertionPoint = Math.Min(selectionFocusPosition, selectionAnchorPosition);
  1366. if (onValidateInput != null)
  1367. input = onValidateInput(text, insertionPoint, input);
  1368. else if (characterValidation != CharacterValidation.None)
  1369. input = Validate(text, insertionPoint, input);
  1370. // If the input is invalid, skip it
  1371. if (input == 0)
  1372. return;
  1373. // Append the character and update the label
  1374. Insert(input);
  1375. }
  1376. /// <summary>
  1377. /// Update the visual text Text.
  1378. /// </summary>
  1379. public void UpdateLabel()
  1380. {
  1381. if (m_TextComponent != null && m_TextComponent.font != null && !m_PreventFontCallback)
  1382. {
  1383. // TextGenerator.Populate invokes a callback that's called for anything
  1384. // that needs to be updated when the data for that font has changed.
  1385. // This makes all Text components that use that font update their vertices.
  1386. // In turn, this makes the InputField that's associated with that Text component
  1387. // update its label by calling this UpdateLabel method.
  1388. // This is a recursive call we want to prevent, since it makes the InputField
  1389. // update based on font data that didn't yet finish executing, or alternatively
  1390. // hang on infinite recursion, depending on whether the cached value is cached
  1391. // before or after the calculation.
  1392. //
  1393. // This callback also occurs when assigning text to our Text component, i.e.,
  1394. // m_TextComponent.text = processed;
  1395. m_PreventFontCallback = true;
  1396. string fullText;
  1397. if (compositionString.Length > 0)
  1398. fullText = text.Substring(0, m_CaretPosition) + compositionString + text.Substring(m_CaretPosition);
  1399. else
  1400. fullText = text;
  1401. string processed;
  1402. if (inputType == InputType.Password)
  1403. processed = new string(asteriskChar, fullText.Length);
  1404. else
  1405. processed = fullText;
  1406. bool isEmpty = string.IsNullOrEmpty(fullText);
  1407. if (m_Placeholder != null)
  1408. m_Placeholder.enabled = isEmpty;
  1409. // If not currently editing the text, set the visible range to the whole text.
  1410. // The UpdateLabel method will then truncate it to the part that fits inside the Text area.
  1411. // We can't do this when text is being edited since it would discard the current scroll,
  1412. // which is defined by means of the m_DrawStart and m_DrawEnd indices.
  1413. if (!m_AllowInput)
  1414. {
  1415. m_DrawStart = 0;
  1416. m_DrawEnd = m_Text.Length;
  1417. }
  1418. if (!isEmpty)
  1419. {
  1420. // Determine what will actually fit into the given line
  1421. Vector2 extents = m_TextComponent.rectTransform.rect.size;
  1422. var settings = m_TextComponent.GetGenerationSettings(extents);
  1423. settings.generateOutOfBounds = true;
  1424. cachedInputTextGenerator.PopulateWithErrors(processed, settings, gameObject);
  1425. SetDrawRangeToContainCaretPosition(caretSelectPositionInternal);
  1426. processed = processed.Substring(m_DrawStart, Mathf.Min(m_DrawEnd, processed.Length) - m_DrawStart);
  1427. SetCaretVisible();
  1428. }
  1429. m_TextComponent.text = processed;
  1430. MarkGeometryAsDirty();
  1431. m_PreventFontCallback = false;
  1432. }
  1433. }
  1434. protected virtual bool IsSelectionVisible()
  1435. {
  1436. if (m_DrawStart > caretPositionInternal || m_DrawStart > caretSelectPositionInternal)
  1437. return false;
  1438. if (m_DrawEnd < caretPositionInternal || m_DrawEnd < caretSelectPositionInternal)
  1439. return false;
  1440. return true;
  1441. }
  1442. protected static int GetLineStartPosition(TextGenerator gen, int line)
  1443. {
  1444. line = Mathf.Clamp(line, 0, gen.lines.Count - 1);
  1445. return gen.lines[line].startCharIdx;
  1446. }
  1447. protected static int GetLineEndPosition(TextGenerator gen, int line)
  1448. {
  1449. line = Mathf.Max(line, 0);
  1450. if (line + 1 < gen.lines.Count)
  1451. return gen.lines[line + 1].startCharIdx - 1;
  1452. return gen.characterCountVisible;
  1453. }
  1454. protected virtual void SetDrawRangeToContainCaretPosition(int caretPos)
  1455. {
  1456. // We don't have any generated lines generation is not valid.
  1457. if (cachedInputTextGenerator.lineCount <= 0)
  1458. return;
  1459. // the extents gets modified by the pixel density, so we need to use the generated extents since that will be in the same 'space' as
  1460. // the values returned by the TextGenerator.lines[x].height for instance.
  1461. Vector2 extents = cachedInputTextGenerator.rectExtents.size;
  1462. if (multiLine)
  1463. {
  1464. var lines = cachedInputTextGenerator.lines;
  1465. int caretLine = DetermineCharacterLine(caretPos, cachedInputTextGenerator);
  1466. if (caretPos > m_DrawEnd)
  1467. {
  1468. // Caret comes after drawEnd, so we need to move drawEnd to the end of the line with the caret
  1469. m_DrawEnd = GetLineEndPosition(cachedInputTextGenerator, caretLine);
  1470. float bottomY = lines[caretLine].topY - lines[caretLine].height;
  1471. if (caretLine == lines.Count - 1)
  1472. {
  1473. // Remove interline spacing on last line.
  1474. bottomY += lines[caretLine].leading;
  1475. }
  1476. int startLine = caretLine;
  1477. while (startLine > 0)
  1478. {
  1479. float topY = lines[startLine - 1].topY;
  1480. if (topY - bottomY > extents.y)
  1481. break;
  1482. startLine--;
  1483. }
  1484. m_DrawStart = GetLineStartPosition(cachedInputTextGenerator, startLine);
  1485. }
  1486. else
  1487. {
  1488. if (caretPos < m_DrawStart)
  1489. {
  1490. // Caret comes before drawStart, so we need to move drawStart to an earlier line start that comes before caret.
  1491. m_DrawStart = GetLineStartPosition(cachedInputTextGenerator, caretLine);
  1492. }
  1493. int startLine = DetermineCharacterLine(m_DrawStart, cachedInputTextGenerator);
  1494. int endLine = startLine;
  1495. float topY = lines[startLine].topY;
  1496. float bottomY = lines[endLine].topY - lines[endLine].height;
  1497. if (endLine == lines.Count - 1)
  1498. {
  1499. // Remove interline spacing on last line.
  1500. bottomY += lines[endLine].leading;
  1501. }
  1502. while (endLine < lines.Count - 1)
  1503. {
  1504. bottomY = lines[endLine + 1].topY - lines[endLine + 1].height;
  1505. if (endLine + 1 == lines.Count - 1)
  1506. {
  1507. // Remove interline spacing on last line.
  1508. bottomY += lines[endLine + 1].leading;
  1509. }
  1510. if (topY - bottomY > extents.y)
  1511. break;
  1512. ++endLine;
  1513. }
  1514. m_DrawEnd = GetLineEndPosition(cachedInputTextGenerator, endLine);
  1515. while (startLine > 0)
  1516. {
  1517. topY = lines[startLine - 1].topY;
  1518. if (topY - bottomY > extents.y)
  1519. break;
  1520. startLine--;
  1521. }
  1522. m_DrawStart = GetLineStartPosition(cachedInputTextGenerator, startLine);
  1523. }
  1524. }
  1525. else
  1526. {
  1527. var characters = cachedInputTextGenerator.characters;
  1528. if (m_DrawEnd > cachedInputTextGenerator.characterCountVisible)
  1529. m_DrawEnd = cachedInputTextGenerator.characterCountVisible;
  1530. float width = 0.0f;
  1531. if (caretPos > m_DrawEnd || (caretPos == m_DrawEnd && m_DrawStart > 0))
  1532. {
  1533. // fit characters from the caretPos leftward
  1534. m_DrawEnd = caretPos;
  1535. for (m_DrawStart = m_DrawEnd - 1; m_DrawStart >= 0; --m_DrawStart)
  1536. {
  1537. if (width + characters[m_DrawStart].charWidth > extents.x)
  1538. break;
  1539. width += characters[m_DrawStart].charWidth;
  1540. }
  1541. ++m_DrawStart; // move right one to the last character we could fit on the left
  1542. }
  1543. else
  1544. {
  1545. if (caretPos < m_DrawStart)
  1546. m_DrawStart = caretPos;
  1547. m_DrawEnd = m_DrawStart;
  1548. }
  1549. // fit characters rightward
  1550. for (; m_DrawEnd < cachedInputTextGenerator.characterCountVisible; ++m_DrawEnd)
  1551. {
  1552. width += characters[m_DrawEnd].charWidth;
  1553. if (width > extents.x)
  1554. break;
  1555. }
  1556. }
  1557. }
  1558. public void ForceLabelUpdate()
  1559. {
  1560. UpdateLabel();
  1561. }
  1562. protected virtual void MarkGeometryAsDirty()
  1563. {
  1564. #if UNITY_EDITOR
  1565. if (!Application.isPlaying || UnityEditor.PrefabUtility.GetPrefabObject(gameObject) != null)
  1566. return;
  1567. #endif
  1568. CanvasUpdateRegistry.RegisterCanvasElementForGraphicRebuild(this);
  1569. }
  1570. public virtual void Rebuild(CanvasUpdate update)
  1571. {
  1572. switch (update)
  1573. {
  1574. case CanvasUpdate.LatePreRender:
  1575. UpdateGeometry();
  1576. break;
  1577. }
  1578. }
  1579. public virtual void LayoutComplete()
  1580. { }
  1581. public virtual void GraphicUpdateComplete()
  1582. { }
  1583. protected virtual void UpdateGeometry()
  1584. {
  1585. #if UNITY_EDITOR
  1586. if (!Application.isPlaying)
  1587. return;
  1588. #endif
  1589. // No need to draw a cursor on mobile as its handled by the devices keyboard.
  1590. if (!shouldHideMobileInput)
  1591. return;
  1592. if (m_CachedInputRenderer == null && m_TextComponent != null)
  1593. {
  1594. GameObject go = new GameObject(transform.name + " Input Caret", typeof(RectTransform), typeof(CanvasRenderer));
  1595. go.hideFlags = HideFlags.DontSave;
  1596. go.transform.SetParent(m_TextComponent.transform.parent);
  1597. go.transform.SetAsFirstSibling();
  1598. go.layer = gameObject.layer;
  1599. caretRectTrans = go.GetComponent<RectTransform>();
  1600. m_CachedInputRenderer = go.GetComponent<CanvasRenderer>();
  1601. m_CachedInputRenderer.SetMaterial(m_TextComponent.GetModifiedMaterial(Graphic.defaultGraphicMaterial), Texture2D.whiteTexture);
  1602. // Needed as if any layout is present we want the caret to always be the same as the text area.
  1603. go.AddComponent<LayoutElement>().ignoreLayout = true;
  1604. AssignPositioningIfNeeded();
  1605. }
  1606. if (m_CachedInputRenderer == null)
  1607. return;
  1608. OnFillVBO(mesh);
  1609. m_CachedInputRenderer.SetMesh(mesh);
  1610. }
  1611. protected void AssignPositioningIfNeeded()
  1612. {
  1613. if (m_TextComponent != null && caretRectTrans != null &&
  1614. (caretRectTrans.localPosition != m_TextComponent.rectTransform.localPosition ||
  1615. caretRectTrans.localRotation != m_TextComponent.rectTransform.localRotation ||
  1616. caretRectTrans.localScale != m_TextComponent.rectTransform.localScale ||
  1617. caretRectTrans.anchorMin != m_TextComponent.rectTransform.anchorMin ||
  1618. caretRectTrans.anchorMax != m_TextComponent.rectTransform.anchorMax ||
  1619. caretRectTrans.anchoredPosition != m_TextComponent.rectTransform.anchoredPosition ||
  1620. caretRectTrans.sizeDelta != m_TextComponent.rectTransform.sizeDelta ||
  1621. caretRectTrans.pivot != m_TextComponent.rectTransform.pivot))
  1622. {
  1623. caretRectTrans.localPosition = m_TextComponent.rectTransform.localPosition;
  1624. caretRectTrans.localRotation = m_TextComponent.rectTransform.localRotation;
  1625. caretRectTrans.localScale = m_TextComponent.rectTransform.localScale;
  1626. caretRectTrans.anchorMin = m_TextComponent.rectTransform.anchorMin;
  1627. caretRectTrans.anchorMax = m_TextComponent.rectTransform.anchorMax;
  1628. caretRectTrans.anchoredPosition = m_TextComponent.rectTransform.anchoredPosition;
  1629. caretRectTrans.sizeDelta = m_TextComponent.rectTransform.sizeDelta;
  1630. caretRectTrans.pivot = m_TextComponent.rectTransform.pivot;
  1631. }
  1632. }
  1633. protected void OnFillVBO(Mesh vbo)
  1634. {
  1635. using (var helper = new VertexHelper())
  1636. {
  1637. if (!isFocused)
  1638. {
  1639. helper.FillMesh(vbo);
  1640. return;
  1641. }
  1642. Vector2 roundingOffset = m_TextComponent.PixelAdjustPoint(Vector2.zero);
  1643. if (!hasSelection)
  1644. GenerateCaret(helper, roundingOffset);
  1645. else
  1646. GenerateHightlight(helper, roundingOffset);
  1647. helper.FillMesh(vbo);
  1648. }
  1649. }
  1650. protected void GenerateCaret(VertexHelper vbo, Vector2 roundingOffset)
  1651. {
  1652. if (m_TextComponent == null || m_TextComponent.canvas == null)
  1653. return;
  1654. if (!m_CaretVisible)
  1655. return;
  1656. if (m_CursorVerts == null)
  1657. {
  1658. CreateCursorVerts();
  1659. }
  1660. float width = m_CaretWidth;
  1661. int adjustedPos = Mathf.Max(0, caretPositionInternal - m_DrawStart);
  1662. TextGenerator gen = m_TextComponent.cachedTextGenerator;
  1663. if (gen == null)
  1664. return;
  1665. if (gen.lineCount == 0)
  1666. return;
  1667. Vector2 startPosition = Vector2.zero;
  1668. // Calculate startPosition
  1669. if (adjustedPos < gen.characters.Count)
  1670. {
  1671. UICharInfo cursorChar = gen.characters[adjustedPos];
  1672. startPosition.x = cursorChar.cursorPos.x;
  1673. }
  1674. startPosition.x /= m_TextComponent.pixelsPerUnit;
  1675. // TODO: Only clamp when Text uses horizontal word wrap.
  1676. if (startPosition.x > m_TextComponent.rectTransform.rect.xMax)
  1677. startPosition.x = m_TextComponent.rectTransform.rect.xMax;
  1678. int characterLine = DetermineCharacterLine(adjustedPos, gen);
  1679. startPosition.y = gen.lines[characterLine].topY / m_TextComponent.pixelsPerUnit;
  1680. float height = gen.lines[characterLine].height / m_TextComponent.pixelsPerUnit;
  1681. for (int i = 0; i < m_CursorVerts.Length; i++)
  1682. m_CursorVerts[i].color = caretColor;
  1683. m_CursorVerts[0].position = new Vector3(startPosition.x, startPosition.y - height, 0.0f);
  1684. m_CursorVerts[1].position = new Vector3(startPosition.x + width, startPosition.y - height, 0.0f);
  1685. m_CursorVerts[2].position = new Vector3(startPosition.x + width, startPosition.y, 0.0f);
  1686. m_CursorVerts[3].position = new Vector3(startPosition.x, startPosition.y, 0.0f);
  1687. if (roundingOffset != Vector2.zero)
  1688. {
  1689. for (int i = 0; i < m_CursorVerts.Length; i++)
  1690. {
  1691. UIVertex uiv = m_CursorVerts[i];
  1692. uiv.position.x += roundingOffset.x;
  1693. uiv.position.y += roundingOffset.y;
  1694. }
  1695. }
  1696. vbo.AddUIVertexQuad(m_CursorVerts);
  1697. int screenHeight = Screen.height;
  1698. // Multiple display support only when not the main display. For display 0 the reported
  1699. // resolution is always the desktops resolution since its part of the display API,
  1700. // so we use the standard none multiple display method. (case 741751)
  1701. int displayIndex = m_TextComponent.canvas.targetDisplay;
  1702. if (displayIndex > 0 && displayIndex < Display.displays.Length)
  1703. screenHeight = Display.displays[displayIndex].renderingHeight;
  1704. startPosition.y = screenHeight - startPosition.y;
  1705. input.compositionCursorPos = startPosition;
  1706. }
  1707. protected void CreateCursorVerts()
  1708. {
  1709. m_CursorVerts = new UIVertex[4];
  1710. for (int i = 0; i < m_CursorVerts.Length; i++)
  1711. {
  1712. m_CursorVerts[i] = UIVertex.simpleVert;
  1713. m_CursorVerts[i].uv0 = Vector2.zero;
  1714. }
  1715. }
  1716. private void GenerateHightlight(VertexHelper vbo, Vector2 roundingOffset)
  1717. {
  1718. int startChar = Mathf.Max(0, caretPositionInternal - m_DrawStart);
  1719. int endChar = Mathf.Max(0, caretSelectPositionInternal - m_DrawStart);
  1720. // Ensure pos is always less then selPos to make the code simpler
  1721. if (startChar > endChar)
  1722. {
  1723. int temp = startChar;
  1724. startChar = endChar;
  1725. endChar = temp;
  1726. }
  1727. endChar -= 1;
  1728. TextGenerator gen = m_TextComponent.cachedTextGenerator;
  1729. if (gen.lineCount <= 0)
  1730. return;
  1731. int currentLineIndex = DetermineCharacterLine(startChar, gen);
  1732. int lastCharInLineIndex = GetLineEndPosition(gen, currentLineIndex);
  1733. UIVertex vert = UIVertex.simpleVert;
  1734. vert.uv0 = Vector2.zero;
  1735. vert.color = selectionColor;
  1736. int currentChar = startChar;
  1737. while (currentChar <= endChar && currentChar < gen.characterCount)
  1738. {
  1739. if (currentChar == lastCharInLineIndex || currentChar == endChar)
  1740. {
  1741. UICharInfo startCharInfo = gen.characters[startChar];
  1742. UICharInfo endCharInfo = gen.characters[currentChar];
  1743. Vector2 startPosition = new Vector2(startCharInfo.cursorPos.x / m_TextComponent.pixelsPerUnit, gen.lines[currentLineIndex].topY / m_TextComponent.pixelsPerUnit);
  1744. Vector2 endPosition = new Vector2((endCharInfo.cursorPos.x + endCharInfo.charWidth) / m_TextComponent.pixelsPerUnit, startPosition.y - gen.lines[currentLineIndex].height / m_TextComponent.pixelsPerUnit);
  1745. // Checking xMin as well due to text generator not setting position if char is not rendered.
  1746. if (endPosition.x > m_TextComponent.rectTransform.rect.xMax || endPosition.x < m_TextComponent.rectTransform.rect.xMin)
  1747. endPosition.x = m_TextComponent.rectTransform.rect.xMax;
  1748. var startIndex = vbo.currentVertCount;
  1749. vert.position = new Vector3(startPosition.x, endPosition.y, 0.0f) + (Vector3)roundingOffset;
  1750. vbo.AddVert(vert);
  1751. vert.position = new Vector3(endPosition.x, endPosition.y, 0.0f) + (Vector3)roundingOffset;
  1752. vbo.AddVert(vert);
  1753. vert.position = new Vector3(endPosition.x, startPosition.y, 0.0f) + (Vector3)roundingOffset;
  1754. vbo.AddVert(vert);
  1755. vert.position = new Vector3(startPosition.x, startPosition.y, 0.0f) + (Vector3)roundingOffset;
  1756. vbo.AddVert(vert);
  1757. vbo.AddTriangle(startIndex, startIndex + 1, startIndex + 2);
  1758. vbo.AddTriangle(startIndex + 2, startIndex + 3, startIndex + 0);
  1759. startChar = currentChar + 1;
  1760. currentLineIndex++;
  1761. lastCharInLineIndex = GetLineEndPosition(gen, currentLineIndex);
  1762. }
  1763. currentChar++;
  1764. }
  1765. }
  1766. /// <summary>
  1767. /// Validate the specified input.
  1768. /// </summary>
  1769. protected virtual char Validate(string text, int pos, char ch)
  1770. {
  1771. // Validation is disabled
  1772. if (characterValidation == CharacterValidation.None || !enabled)
  1773. return ch;
  1774. if (characterValidation == CharacterValidation.Integer || characterValidation == CharacterValidation.Decimal)
  1775. {
  1776. // Integer and decimal
  1777. bool cursorBeforeDash = (pos == 0 && text.Length > 0 && text[0] == '-');
  1778. bool dashInSelection = text.Length > 0 && text[0] == '-' && ((caretPositionInternal == 0 && caretSelectPositionInternal > 0) || (caretSelectPositionInternal == 0 && caretPositionInternal > 0));
  1779. bool selectionAtStart = caretPositionInternal == 0 || caretSelectPositionInternal == 0;
  1780. if (!cursorBeforeDash || dashInSelection)
  1781. {
  1782. if (ch >= '0' && ch <= '9') return ch;
  1783. if (ch == '-' && (pos == 0 || selectionAtStart)) return ch;
  1784. if (ch == '.' && characterValidation == CharacterValidation.Decimal && !text.Contains(".")) return ch;
  1785. }
  1786. }
  1787. else if (characterValidation == CharacterValidation.Alphanumeric)
  1788. {
  1789. // All alphanumeric characters
  1790. if (ch >= 'A' && ch <= 'Z') return ch;
  1791. if (ch >= 'a' && ch <= 'z') return ch;
  1792. if (ch >= '0' && ch <= '9') return ch;
  1793. }
  1794. else if (characterValidation == CharacterValidation.Name)
  1795. {
  1796. // FIXME: some actions still lead to invalid input:
  1797. // - Hitting delete in front of an uppercase letter
  1798. // - Selecting an uppercase letter and deleting it
  1799. // - Typing some text, hitting Home and typing more text (we then have an uppercase letter in the middle of a word)
  1800. // - Typing some text, hitting Home and typing a space (we then have a leading space)
  1801. // - Erasing a space between two words (we then have an uppercase letter in the middle of a word)
  1802. // - We accept a trailing space
  1803. // - We accept the insertion of a space between two lowercase letters.
  1804. // - Typing text in front of an existing uppercase letter
  1805. // - ... and certainly more
  1806. //
  1807. // The rule we try to implement are too complex for this kind of verification.
  1808. if (char.IsLetter(ch))
  1809. {
  1810. // Character following a space should be in uppercase.
  1811. if (char.IsLower(ch) && ((pos == 0) || (text[pos - 1] == ' ')))
  1812. {
  1813. return char.ToUpper(ch);
  1814. }
  1815. // Character not following a space or an apostrophe should be in lowercase.
  1816. if (char.IsUpper(ch) && (pos > 0) && (text[pos - 1] != ' ') && (text[pos - 1] != '\''))
  1817. {
  1818. return char.ToLower(ch);
  1819. }
  1820. return ch;
  1821. }
  1822. if (ch == '\'')
  1823. {
  1824. // Don't allow more than one apostrophe
  1825. if (!text.Contains("'"))
  1826. // Don't allow consecutive spaces and apostrophes.
  1827. if (!(((pos > 0) && ((text[pos - 1] == ' ') || (text[pos - 1] == '\''))) ||
  1828. ((pos < text.Length) && ((text[pos] == ' ') || (text[pos] == '\'')))))
  1829. return ch;
  1830. }
  1831. if (ch == ' ')
  1832. {
  1833. // Don't allow consecutive spaces and apostrophes.
  1834. if (!(((pos > 0) && ((text[pos - 1] == ' ') || (text[pos - 1] == '\''))) ||
  1835. ((pos < text.Length) && ((text[pos] == ' ') || (text[pos] == '\'')))))
  1836. return ch;
  1837. }
  1838. }
  1839. else if (characterValidation == CharacterValidation.EmailAddress)
  1840. {
  1841. // From StackOverflow about allowed characters in email addresses:
  1842. // Uppercase and lowercase English letters (a-z, A-Z)
  1843. // Digits 0 to 9
  1844. // Characters ! # $ % & ' * + - / = ? ^ _ ` { | } ~
  1845. // Character . (dot, period, full stop) provided that it is not the first or last character,
  1846. // and provided also that it does not appear two or more times consecutively.
  1847. if (ch >= 'A' && ch <= 'Z') return ch;
  1848. if (ch >= 'a' && ch <= 'z') return ch;
  1849. if (ch >= '0' && ch <= '9') return ch;
  1850. if (ch == '@' && text.IndexOf('@') == -1) return ch;
  1851. if (kEmailSpecialCharacters.IndexOf(ch) != -1) return ch;
  1852. if (ch == '.')
  1853. {
  1854. char lastChar = (text.Length > 0) ? text[Mathf.Clamp(pos, 0, text.Length - 1)] : ' ';
  1855. char nextChar = (text.Length > 0) ? text[Mathf.Clamp(pos + 1, 0, text.Length - 1)] : '\n';
  1856. if (lastChar != '.' && nextChar != '.')
  1857. return ch;
  1858. }
  1859. }
  1860. return (char)0;
  1861. }
  1862. public virtual void ActivateInputField()
  1863. {
  1864. if (m_TextComponent == null || m_TextComponent.font == null || !IsActive() || !IsInteractable())
  1865. return;
  1866. if (isFocused)
  1867. {
  1868. if (m_Keyboard != null && !m_Keyboard.active)
  1869. {
  1870. m_Keyboard.active = true;
  1871. m_Keyboard.text = m_Text;
  1872. }
  1873. }
  1874. m_ShouldActivateNextUpdate = true;
  1875. }
  1876. protected virtual void ActivateInputFieldInternal()
  1877. {
  1878. if (EventSystem.current == null)
  1879. return;
  1880. if (EventSystem.current.currentSelectedGameObject != gameObject)
  1881. EventSystem.current.SetSelectedGameObject(gameObject);
  1882. if (true) //(TouchScreenKeyboard.isSupported)
  1883. {
  1884. if (input.touchSupported)
  1885. {
  1886. TouchScreenKeyboard.hideInput = shouldHideMobileInput;
  1887. }
  1888. //TODO
  1889. Vector3 worldPosition = Vector3.zero;
  1890. Quaternion worldRotation = Quaternion.identity;
  1891. Vector3 localScale = Vector3.one;
  1892. GetKeyboardTransform(ref worldPosition, ref worldRotation, ref localScale);
  1893. m_Keyboard = SCKeyboardBase.Open(m_SCKeyboardEnum, m_Text, keyboardType, this.transform, worldPosition, worldRotation, localScale);
  1894. m_Keyboard.SetTextOnOpen(text);
  1895. /*(inputType == InputType.Password) ?
  1896. TouchScreenKeyboard.Open(m_Text, keyboardType, false, multiLine, true) :
  1897. TouchScreenKeyboard.Open(m_Text, keyboardType, inputType == InputType.AutoCorrect, multiLine);*/
  1898. // Mimics OnFocus but as mobile doesn't properly support select all
  1899. // just set it to the end of the text (where it would move when typing starts)
  1900. MoveTextEnd(false);
  1901. }
  1902. else
  1903. {
  1904. input.imeCompositionMode = IMECompositionMode.On;
  1905. OnFocus();
  1906. }
  1907. m_AllowInput = true;
  1908. m_OriginalText = text;
  1909. m_WasCanceled = false;
  1910. SetCaretVisible();
  1911. UpdateLabel();
  1912. }
  1913. public override void OnSelect(BaseEventData eventData)
  1914. {
  1915. base.OnSelect(eventData);
  1916. if (shouldActivateOnSelect)
  1917. ActivateInputField();
  1918. }
  1919. public virtual void OnPointerClick(PointerEventData eventData)
  1920. {
  1921. if (eventData.button > PointerEventData.InputButton.Left)
  1922. return;
  1923. ActivateInputField();
  1924. }
  1925. public virtual void DeactivateInputField()
  1926. {
  1927. // Not activated do nothing.
  1928. if (!m_AllowInput)
  1929. return;
  1930. m_HasDoneFocusTransition = false;
  1931. m_AllowInput = false;
  1932. if (m_Placeholder != null)
  1933. m_Placeholder.enabled = string.IsNullOrEmpty(m_Text);
  1934. if (m_TextComponent != null && IsInteractable())
  1935. {
  1936. if (m_WasCanceled)
  1937. text = m_OriginalText;
  1938. if (m_Keyboard != null)
  1939. {
  1940. m_Keyboard.active = false;
  1941. m_Keyboard = null;
  1942. }
  1943. m_CaretPosition = m_CaretSelectPosition = 0;
  1944. SendOnSubmit();
  1945. input.imeCompositionMode = IMECompositionMode.Auto;
  1946. }
  1947. MarkGeometryAsDirty();
  1948. }
  1949. public override void OnDeselect(BaseEventData eventData)
  1950. {
  1951. Debug.Log("Deselect");
  1952. DeactivateInputField();
  1953. base.OnDeselect(eventData);
  1954. }
  1955. public virtual void OnSubmit(BaseEventData eventData)
  1956. {
  1957. if (!IsActive() || !IsInteractable())
  1958. return;
  1959. if (!isFocused)
  1960. m_ShouldActivateNextUpdate = true;
  1961. }
  1962. protected void EnforceContentType()
  1963. {
  1964. switch (contentType)
  1965. {
  1966. case ContentType.Standard:
  1967. {
  1968. // Don't enforce line type for this content type.
  1969. m_InputType = InputType.Standard;
  1970. m_KeyboardType = TouchScreenKeyboardType.Default;
  1971. m_CharacterValidation = CharacterValidation.None;
  1972. break;
  1973. }
  1974. case ContentType.Autocorrected:
  1975. {
  1976. // Don't enforce line type for this content type.
  1977. m_InputType = InputType.AutoCorrect;
  1978. m_KeyboardType = TouchScreenKeyboardType.Default;
  1979. m_CharacterValidation = CharacterValidation.None;
  1980. break;
  1981. }
  1982. case ContentType.IntegerNumber:
  1983. {
  1984. m_LineType = LineType.SingleLine;
  1985. m_InputType = InputType.Standard;
  1986. m_KeyboardType = TouchScreenKeyboardType.NumberPad;
  1987. m_CharacterValidation = CharacterValidation.Integer;
  1988. break;
  1989. }
  1990. case ContentType.DecimalNumber:
  1991. {
  1992. m_LineType = LineType.SingleLine;
  1993. m_InputType = InputType.Standard;
  1994. m_KeyboardType = TouchScreenKeyboardType.NumbersAndPunctuation;
  1995. m_CharacterValidation = CharacterValidation.Decimal;
  1996. break;
  1997. }
  1998. case ContentType.Alphanumeric:
  1999. {
  2000. m_LineType = LineType.SingleLine;
  2001. m_InputType = InputType.Standard;
  2002. m_KeyboardType = TouchScreenKeyboardType.ASCIICapable;
  2003. m_CharacterValidation = CharacterValidation.Alphanumeric;
  2004. break;
  2005. }
  2006. case ContentType.Name:
  2007. {
  2008. m_LineType = LineType.SingleLine;
  2009. m_InputType = InputType.Standard;
  2010. m_KeyboardType = TouchScreenKeyboardType.NamePhonePad;
  2011. m_CharacterValidation = CharacterValidation.Name;
  2012. break;
  2013. }
  2014. case ContentType.EmailAddress:
  2015. {
  2016. m_LineType = LineType.SingleLine;
  2017. m_InputType = InputType.Standard;
  2018. m_KeyboardType = TouchScreenKeyboardType.EmailAddress;
  2019. m_CharacterValidation = CharacterValidation.EmailAddress;
  2020. break;
  2021. }
  2022. case ContentType.Password:
  2023. {
  2024. m_LineType = LineType.SingleLine;
  2025. m_InputType = InputType.Password;
  2026. m_KeyboardType = TouchScreenKeyboardType.Default;
  2027. m_CharacterValidation = CharacterValidation.None;
  2028. break;
  2029. }
  2030. case ContentType.Pin:
  2031. {
  2032. m_LineType = LineType.SingleLine;
  2033. m_InputType = InputType.Password;
  2034. m_KeyboardType = TouchScreenKeyboardType.NumberPad;
  2035. m_CharacterValidation = CharacterValidation.Integer;
  2036. break;
  2037. }
  2038. default:
  2039. {
  2040. // Includes Custom type. Nothing should be enforced.
  2041. break;
  2042. }
  2043. }
  2044. EnforceTextHOverflow();
  2045. }
  2046. protected void EnforceTextHOverflow()
  2047. {
  2048. if (m_TextComponent != null)
  2049. if (multiLine)
  2050. m_TextComponent.horizontalOverflow = HorizontalWrapMode.Wrap;
  2051. else
  2052. m_TextComponent.horizontalOverflow = HorizontalWrapMode.Overflow;
  2053. }
  2054. protected void SetToCustomIfContentTypeIsNot(params ContentType[] allowedContentTypes)
  2055. {
  2056. if (contentType == ContentType.Custom)
  2057. return;
  2058. for (int i = 0; i < allowedContentTypes.Length; i++)
  2059. if (contentType == allowedContentTypes[i])
  2060. return;
  2061. contentType = ContentType.Custom;
  2062. }
  2063. protected void SetToCustom()
  2064. {
  2065. if (contentType == ContentType.Custom)
  2066. return;
  2067. contentType = ContentType.Custom;
  2068. }
  2069. protected override void DoStateTransition(SelectionState state, bool instant)
  2070. {
  2071. if (m_HasDoneFocusTransition)
  2072. state = SelectionState.Highlighted;
  2073. else if (state == SelectionState.Pressed)
  2074. m_HasDoneFocusTransition = true;
  2075. base.DoStateTransition(state, instant);
  2076. }
  2077. public virtual void CalculateLayoutInputHorizontal() { }
  2078. public virtual void CalculateLayoutInputVertical() { }
  2079. public virtual float minWidth { get { return 0; } }
  2080. public virtual float preferredWidth
  2081. {
  2082. get
  2083. {
  2084. if (textComponent == null)
  2085. return 0;
  2086. var settings = textComponent.GetGenerationSettings(Vector2.zero);
  2087. return textComponent.cachedTextGeneratorForLayout.GetPreferredWidth(m_Text, settings) / textComponent.pixelsPerUnit;
  2088. }
  2089. }
  2090. public virtual float flexibleWidth { get { return -1; } }
  2091. public virtual float minHeight { get { return 0; } }
  2092. public virtual float preferredHeight
  2093. {
  2094. get
  2095. {
  2096. if (textComponent == null)
  2097. return 0;
  2098. var settings = textComponent.GetGenerationSettings(new Vector2(textComponent.rectTransform.rect.size.x, 0.0f));
  2099. return textComponent.cachedTextGeneratorForLayout.GetPreferredHeight(m_Text, settings) / textComponent.pixelsPerUnit;
  2100. }
  2101. }
  2102. public virtual float flexibleHeight { get { return -1; } }
  2103. public virtual int layoutPriority { get { return 1; } }
  2104. public void SetKeyboardTransform(Vector3 localPosition, Quaternion localRotation, Vector3 localScale)
  2105. {
  2106. m_UseCustomTransform = true;
  2107. m_CustomPosition = localPosition;
  2108. m_CustomRotation = localRotation.eulerAngles;
  2109. m_CustomLocalScale = localScale;
  2110. Vector3 worldPosition = Vector3.zero;
  2111. Quaternion worldRotation = Quaternion.identity;
  2112. Vector3 scale = Vector3.one;
  2113. GetKeyboardTransform(ref worldPosition, ref worldRotation, ref scale);
  2114. ResetKeyboardTransform(worldPosition, worldRotation, scale);
  2115. }
  2116. public void UseDefaultKeyboardTransform()
  2117. {
  2118. m_UseCustomTransform = false;
  2119. Vector3 worldPosition = Vector3.zero;
  2120. Quaternion worldRotation = Quaternion.identity;
  2121. Vector3 localScale = Vector3.one;
  2122. GetKeyboardTransform(ref worldPosition, ref worldRotation, ref localScale);
  2123. ResetKeyboardTransform(worldPosition, worldRotation, localScale);
  2124. }
  2125. private void ResetKeyboardTransform(Vector3 position, Quaternion rotation, Vector3 localScale)
  2126. {
  2127. if (m_Keyboard != null && m_Keyboard.active)
  2128. {
  2129. m_Keyboard.SetKeyboardTransform(position, rotation, localScale);
  2130. }
  2131. }
  2132. private void GetKeyboardTransform(ref Vector3 worldPosition, ref Quaternion worldRotation, ref Vector3 localScale)
  2133. {
  2134. Transform head = Camera.main.transform.transform;
  2135. //float inputFieldDistance = Vector3.Distance(this.transform.position, head.transform.position) * 0.5f;
  2136. worldPosition = m_UseCustomTransform ? m_CustomPosition : head.transform.position + new Vector3(head.forward.normalized.x, (head.forward.normalized.y - 0.25f), head.forward.normalized.z) * 0.5f;//new Vector3(0f, 0f, -9f);
  2137. worldRotation = m_UseCustomTransform ? Quaternion.Euler(m_CustomRotation) : Quaternion.Euler(20f, head.rotation.eulerAngles.y, 0f);
  2138. localScale = m_UseCustomTransform ? m_CustomLocalScale : Vector3.one * 0.3f;
  2139. }
  2140. }
  2141. }