SCInputField.cs 93 KB

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