SCInputField.cs 92 KB

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