1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402 |
- //#define TMP_DEBUG_MODE
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Threading;
- using System.Text;
- using System.Text.RegularExpressions;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Events;
- using UnityEngine.EventSystems;
- using UnityEngine.Serialization;
- namespace TMPro
- {
- /// <summary>
- /// Editable text input field.
- /// </summary>
- [AddComponentMenu("UI/TextMeshPro - Input Field", 11)]
- public class TMP_InputField : Selectable,
- IUpdateSelectedHandler,
- IBeginDragHandler,
- IDragHandler,
- IEndDragHandler,
- IPointerClickHandler,
- ISubmitHandler,
- ICanvasElement,
- ILayoutElement,
- IScrollHandler
- {
- // Setting the content type acts as a shortcut for setting a combination of InputType, CharacterValidation, LineType, and TouchScreenKeyboardType
- public enum ContentType
- {
- Standard,
- Autocorrected,
- IntegerNumber,
- DecimalNumber,
- Alphanumeric,
- Name,
- EmailAddress,
- Password,
- Pin,
- Custom
- }
- public enum InputType
- {
- Standard,
- AutoCorrect,
- Password,
- }
- public enum CharacterValidation
- {
- None,
- Digit,
- Integer,
- Decimal,
- Alphanumeric,
- Name,
- Regex,
- EmailAddress,
- CustomValidator
- }
- public enum LineType
- {
- SingleLine,
- MultiLineSubmit,
- MultiLineNewline
- }
- public delegate char OnValidateInput(string text, int charIndex, char addedChar);
- [Serializable]
- public class SubmitEvent : UnityEvent<string> { }
- [Serializable]
- public class OnChangeEvent : UnityEvent<string> { }
- [Serializable]
- public class SelectionEvent : UnityEvent<string> { }
- [Serializable]
- public class TextSelectionEvent : UnityEvent<string, int, int> { }
- [Serializable]
- public class TouchScreenKeyboardEvent : UnityEvent<TouchScreenKeyboard.Status> { }
- protected TouchScreenKeyboard m_SoftKeyboard;
- static private readonly char[] kSeparators = { ' ', '.', ',', '\t', '\r', '\n' };
- #region Exposed properties
- /// <summary>
- /// Text Text used to display the input's value.
- /// </summary>
- protected RectTransform m_RectTransform;
- [SerializeField]
- protected RectTransform m_TextViewport;
- protected RectMask2D m_TextComponentRectMask;
- protected RectMask2D m_TextViewportRectMask;
- private Rect m_CachedViewportRect;
- [SerializeField]
- protected TMP_Text m_TextComponent;
- protected RectTransform m_TextComponentRectTransform;
- [SerializeField]
- protected Graphic m_Placeholder;
- [SerializeField]
- protected Scrollbar m_VerticalScrollbar;
- [SerializeField]
- protected TMP_ScrollbarEventHandler m_VerticalScrollbarEventHandler;
- //private bool m_ForceDeactivation;
- private bool m_IsDrivenByLayoutComponents = false;
- [SerializeField]
- private LayoutGroup m_LayoutGroup;
- /// <summary>
- /// Used to keep track of scroll position
- /// </summary>
- private float m_ScrollPosition;
- /// <summary>
- ///
- /// </summary>
- [SerializeField]
- protected float m_ScrollSensitivity = 1.0f;
- //[SerializeField]
- //protected TMP_Text m_PlaceholderTextComponent;
- [SerializeField]
- private ContentType m_ContentType = ContentType.Standard;
- /// <summary>
- /// Type of data expected by the input field.
- /// </summary>
- [SerializeField]
- private InputType m_InputType = InputType.Standard;
- /// <summary>
- /// The character used to hide text in password field.
- /// </summary>
- [SerializeField]
- private char m_AsteriskChar = '*';
- /// <summary>
- /// Keyboard type applies to mobile keyboards that get shown.
- /// </summary>
- [SerializeField]
- private TouchScreenKeyboardType m_KeyboardType = TouchScreenKeyboardType.Default;
- [SerializeField]
- private LineType m_LineType = LineType.SingleLine;
- /// <summary>
- /// Should hide mobile input field part of the virtual keyboard.
- /// </summary>
- [SerializeField]
- private bool m_HideMobileInput = false;
- /// <summary>
- /// Should hide soft / virtual keyboard.
- /// </summary>
- [SerializeField]
- private bool m_HideSoftKeyboard = false;
- /// <summary>
- /// What kind of validation to use with the input field's data.
- /// </summary>
- [SerializeField]
- private CharacterValidation m_CharacterValidation = CharacterValidation.None;
- /// <summary>
- /// The Regex expression used for validating the text input.
- /// </summary>
- [SerializeField]
- private string m_RegexValue = string.Empty;
- /// <summary>
- /// The point sized used by the placeholder and input text object.
- /// </summary>
- [SerializeField]
- private float m_GlobalPointSize = 14;
- /// <summary>
- /// Maximum number of characters allowed before input no longer works.
- /// </summary>
- [SerializeField]
- private int m_CharacterLimit = 0;
- /// <summary>
- /// Event delegates triggered when the input field submits its data.
- /// </summary>
- [SerializeField]
- private SubmitEvent m_OnEndEdit = new SubmitEvent();
- /// <summary>
- /// Event delegates triggered when the input field submits its data.
- /// </summary>
- [SerializeField]
- private SubmitEvent m_OnSubmit = new SubmitEvent();
- /// <summary>
- /// Event delegates triggered when the input field is focused.
- /// </summary>
- [SerializeField]
- private SelectionEvent m_OnSelect = new SelectionEvent();
- /// <summary>
- /// Event delegates triggered when the input field focus is lost.
- /// </summary>
- [SerializeField]
- private SelectionEvent m_OnDeselect = new SelectionEvent();
- /// <summary>
- /// Event delegates triggered when the text is selected / highlighted.
- /// </summary>
- [SerializeField]
- private TextSelectionEvent m_OnTextSelection = new TextSelectionEvent();
- /// <summary>
- /// Event delegates triggered when text is no longer select / highlighted.
- /// </summary>
- [SerializeField]
- private TextSelectionEvent m_OnEndTextSelection = new TextSelectionEvent();
- /// <summary>
- /// Event delegates triggered when the input field changes its data.
- /// </summary>
- [SerializeField]
- private OnChangeEvent m_OnValueChanged = new OnChangeEvent();
- /// <summary>
- /// Event delegates triggered when the status of the TouchScreenKeyboard changes.
- /// </summary>
- [SerializeField]
- private TouchScreenKeyboardEvent m_OnTouchScreenKeyboardStatusChanged = new TouchScreenKeyboardEvent();
- /// <summary>
- /// Custom validation callback.
- /// </summary>
- [SerializeField]
- private OnValidateInput m_OnValidateInput;
- [SerializeField]
- private Color m_CaretColor = new Color(50f / 255f, 50f / 255f, 50f / 255f, 1f);
- [SerializeField]
- private bool m_CustomCaretColor = false;
- [SerializeField]
- private Color m_SelectionColor = new Color(168f / 255f, 206f / 255f, 255f / 255f, 192f / 255f);
- /// <summary>
- /// Input field's value.
- /// </summary>
- [SerializeField]
- [TextArea(5, 10)]
- protected string m_Text = string.Empty;
- [SerializeField]
- [Range(0f, 4f)]
- private float m_CaretBlinkRate = 0.85f;
- [SerializeField]
- [Range(1, 5)]
- private int m_CaretWidth = 1;
- [SerializeField]
- private bool m_ReadOnly = false;
- [SerializeField]
- private bool m_RichText = true;
- #endregion
- protected int m_StringPosition = 0;
- protected int m_StringSelectPosition = 0;
- protected int m_CaretPosition = 0;
- protected int m_CaretSelectPosition = 0;
- private RectTransform caretRectTrans = null;
- protected UIVertex[] m_CursorVerts = null;
- private CanvasRenderer m_CachedInputRenderer;
- private Vector2 m_LastPosition;
- [NonSerialized]
- protected Mesh m_Mesh;
- private bool m_AllowInput = false;
- //bool m_HasLostFocus = false;
- private bool m_ShouldActivateNextUpdate = false;
- private bool m_UpdateDrag = false;
- private bool m_DragPositionOutOfBounds = false;
- private const float kHScrollSpeed = 0.05f;
- private const float kVScrollSpeed = 0.10f;
- protected bool m_CaretVisible;
- private Coroutine m_BlinkCoroutine = null;
- private float m_BlinkStartTime = 0.0f;
- private Coroutine m_DragCoroutine = null;
- private string m_OriginalText = "";
- private bool m_WasCanceled = false;
- private bool m_HasDoneFocusTransition = false;
- private WaitForSecondsRealtime m_WaitForSecondsRealtime;
- private bool m_PreventCallback = false;
- private bool m_TouchKeyboardAllowsInPlaceEditing = false;
- private bool m_IsTextComponentUpdateRequired = false;
- private bool m_IsScrollbarUpdateRequired = false;
- private bool m_IsUpdatingScrollbarValues = false;
- private bool m_isLastKeyBackspace = false;
- private float m_PointerDownClickStartTime;
- private float m_KeyDownStartTime;
- private float m_DoubleClickDelay = 0.5f;
- // Doesn't include dot and @ on purpose! See usage for details.
- const string kEmailSpecialCharacters = "!#$%&'*+-/=?^_`{|}~";
- private BaseInput inputSystem
- {
- get
- {
- if (EventSystem.current && EventSystem.current.currentInputModule)
- return EventSystem.current.currentInputModule.input;
- return null;
- }
- }
- private string compositionString
- {
- get { return inputSystem != null ? inputSystem.compositionString : Input.compositionString; }
- }
- private bool m_IsCompositionActive = false;
- private bool m_ShouldUpdateIMEWindowPosition = false;
- private int m_PreviousIMEInsertionLine = 0;
- private int compositionLength
- {
- get
- {
- if (m_ReadOnly)
- return 0;
- return compositionString.Length;
- }
- }
- protected TMP_InputField()
- {
- SetTextComponentWrapMode();
- }
- protected Mesh mesh
- {
- get
- {
- if (m_Mesh == null)
- m_Mesh = new Mesh();
- return m_Mesh;
- }
- }
- /// <summary>
- /// Should the mobile keyboard input be hidden.
- /// </summary>
- public bool shouldHideMobileInput
- {
- get
- {
- switch (Application.platform)
- {
- case RuntimePlatform.Android:
- case RuntimePlatform.IPhonePlayer:
- case RuntimePlatform.tvOS:
- return m_HideMobileInput;
- default:
- return true;
- }
- }
- set
- {
- switch(Application.platform)
- {
- case RuntimePlatform.Android:
- case RuntimePlatform.IPhonePlayer:
- case RuntimePlatform.tvOS:
- SetPropertyUtility.SetStruct(ref m_HideMobileInput, value);
- break;
- default:
- m_HideMobileInput = true;
- break;
- }
- }
- }
- public bool shouldHideSoftKeyboard
- {
- get
- {
- switch (Application.platform)
- {
- case RuntimePlatform.Android:
- case RuntimePlatform.IPhonePlayer:
- case RuntimePlatform.tvOS:
- case RuntimePlatform.WSAPlayerX86:
- case RuntimePlatform.WSAPlayerX64:
- case RuntimePlatform.WSAPlayerARM:
- #if UNITY_2019_3_OR_NEWER
- case RuntimePlatform.Stadia:
- #endif
- case RuntimePlatform.Switch:
- return m_HideSoftKeyboard;
- default:
- return true;
- }
- }
- set
- {
- switch (Application.platform)
- {
- case RuntimePlatform.Android:
- case RuntimePlatform.IPhonePlayer:
- case RuntimePlatform.tvOS:
- case RuntimePlatform.WSAPlayerX86:
- case RuntimePlatform.WSAPlayerX64:
- case RuntimePlatform.WSAPlayerARM:
- #if UNITY_2019_3_OR_NEWER
- case RuntimePlatform.Stadia:
- #endif
- case RuntimePlatform.Switch:
- SetPropertyUtility.SetStruct(ref m_HideSoftKeyboard, value);
- break;
- default:
- m_HideSoftKeyboard = true;
- break;
- }
- if (m_HideSoftKeyboard == true && m_SoftKeyboard != null && TouchScreenKeyboard.isSupported && m_SoftKeyboard.active)
- {
- m_SoftKeyboard.active = false;
- m_SoftKeyboard = null;
- }
- }
- }
- private bool isKeyboardUsingEvents()
- {
- switch (Application.platform)
- {
- case RuntimePlatform.Android:
- case RuntimePlatform.IPhonePlayer:
- case RuntimePlatform.tvOS:
- case RuntimePlatform.Switch:
- return false;
- default:
- return true;
- }
- }
- /// <summary>
- /// Input field's current text value. This is not necessarily the same as what is visible on screen.
- /// </summary>
- /// <remarks>
- /// Note that null is invalid value for InputField.text.
- /// </remarks>
- /// <example>
- /// <code>
- /// using UnityEngine;
- /// using System.Collections;
- /// using UnityEngine.UI; // Required when Using UI elements.
- ///
- /// public class Example : MonoBehaviour
- /// {
- /// public InputField mainInputField;
- ///
- /// public void Start()
- /// {
- /// mainInputField.text = "Enter Text Here...";
- /// }
- /// }
- /// </code>
- /// </example>
- public string text
- {
- get
- {
- return m_Text;
- }
- set
- {
- SetText(value);
- }
- }
- /// <summary>
- /// Set Input field's current text value without invoke onValueChanged. This is not necessarily the same as what is visible on screen.
- /// </summary>
- public void SetTextWithoutNotify(string input)
- {
- SetText(input, false);
- }
- void SetText(string value, bool sendCallback = true)
- {
- if (this.text == value)
- return;
- if (value == null)
- value = "";
- value = value.Replace("\0", string.Empty); // remove embedded nulls
- m_Text = value;
- /*
- if (m_LineType == LineType.SingleLine)
- value = value.Replace("\n", "").Replace("\t", "");
- // If we have an input validator, validate the input and apply the character limit at the same time.
- if (onValidateInput != null || characterValidation != CharacterValidation.None)
- {
- m_Text = "";
- OnValidateInput validatorMethod = onValidateInput ?? Validate;
- m_CaretPosition = m_CaretSelectPosition = value.Length;
- int charactersToCheck = characterLimit > 0 ? Math.Min(characterLimit, value.Length) : value.Length;
- for (int i = 0; i < charactersToCheck; ++i)
- {
- char c = validatorMethod(m_Text, m_Text.Length, value[i]);
- if (c != 0)
- m_Text += c;
- }
- }
- else
- {
- m_Text = characterLimit > 0 && value.Length > characterLimit ? value.Substring(0, characterLimit) : value;
- }
- */
- #if UNITY_EDITOR
- if (!Application.isPlaying)
- {
- SendOnValueChangedAndUpdateLabel();
- return;
- }
- #endif
- if (m_SoftKeyboard != null)
- m_SoftKeyboard.text = m_Text;
- if (m_StringPosition > m_Text.Length)
- m_StringPosition = m_StringSelectPosition = m_Text.Length;
- else if (m_StringSelectPosition > m_Text.Length)
- m_StringSelectPosition = m_Text.Length;
- // Set RectTransform relative position to top of viewport.
- AdjustTextPositionRelativeToViewport(0);
- m_forceRectTransformAdjustment = true;
- m_IsTextComponentUpdateRequired = true;
- UpdateLabel();
- if (sendCallback)
- SendOnValueChanged();
- }
- public bool isFocused
- {
- get { return m_AllowInput; }
- }
- public float caretBlinkRate
- {
- get { return m_CaretBlinkRate; }
- set
- {
- if (SetPropertyUtility.SetStruct(ref m_CaretBlinkRate, value))
- {
- if (m_AllowInput)
- SetCaretActive();
- }
- }
- }
- public int caretWidth { get { return m_CaretWidth; } set { if (SetPropertyUtility.SetStruct(ref m_CaretWidth, value)) MarkGeometryAsDirty(); } }
- public RectTransform textViewport { get { return m_TextViewport; } set { SetPropertyUtility.SetClass(ref m_TextViewport, value); } }
- public TMP_Text textComponent
- {
- get { return m_TextComponent; }
- set
- {
- if (SetPropertyUtility.SetClass(ref m_TextComponent, value))
- {
- SetTextComponentWrapMode();
- }
- }
- }
- //public TMP_Text placeholderTextComponent { get { return m_PlaceholderTextComponent; } set { SetPropertyUtility.SetClass(ref m_PlaceholderTextComponent, value); } }
- public Graphic placeholder { get { return m_Placeholder; } set { SetPropertyUtility.SetClass(ref m_Placeholder, value); } }
- public Scrollbar verticalScrollbar
- {
- get { return m_VerticalScrollbar; }
- set
- {
- if (m_VerticalScrollbar != null)
- m_VerticalScrollbar.onValueChanged.RemoveListener(OnScrollbarValueChange);
- SetPropertyUtility.SetClass(ref m_VerticalScrollbar, value);
- if (m_VerticalScrollbar)
- {
- m_VerticalScrollbar.onValueChanged.AddListener(OnScrollbarValueChange);
- }
- }
- }
- public float scrollSensitivity { get { return m_ScrollSensitivity; } set { if (SetPropertyUtility.SetStruct(ref m_ScrollSensitivity, value)) MarkGeometryAsDirty(); } }
- public Color caretColor { get { return customCaretColor ? m_CaretColor : textComponent.color; } set { if (SetPropertyUtility.SetColor(ref m_CaretColor, value)) MarkGeometryAsDirty(); } }
- public bool customCaretColor { get { return m_CustomCaretColor; } set { if (m_CustomCaretColor != value) { m_CustomCaretColor = value; MarkGeometryAsDirty(); } } }
- public Color selectionColor { get { return m_SelectionColor; } set { if (SetPropertyUtility.SetColor(ref m_SelectionColor, value)) MarkGeometryAsDirty(); } }
- public SubmitEvent onEndEdit { get { return m_OnEndEdit; } set { SetPropertyUtility.SetClass(ref m_OnEndEdit, value); } }
- public SubmitEvent onSubmit { get { return m_OnSubmit; } set { SetPropertyUtility.SetClass(ref m_OnSubmit, value); } }
- public SelectionEvent onSelect { get { return m_OnSelect; } set { SetPropertyUtility.SetClass(ref m_OnSelect, value); } }
- public SelectionEvent onDeselect { get { return m_OnDeselect; } set { SetPropertyUtility.SetClass(ref m_OnDeselect, value); } }
- public TextSelectionEvent onTextSelection { get { return m_OnTextSelection; } set { SetPropertyUtility.SetClass(ref m_OnTextSelection, value); } }
- public TextSelectionEvent onEndTextSelection { get { return m_OnEndTextSelection; } set { SetPropertyUtility.SetClass(ref m_OnEndTextSelection, value); } }
- public OnChangeEvent onValueChanged { get { return m_OnValueChanged; } set { SetPropertyUtility.SetClass(ref m_OnValueChanged, value); } }
- public TouchScreenKeyboardEvent onTouchScreenKeyboardStatusChanged { get { return m_OnTouchScreenKeyboardStatusChanged; } set { SetPropertyUtility.SetClass(ref m_OnTouchScreenKeyboardStatusChanged, value); } }
- public OnValidateInput onValidateInput { get { return m_OnValidateInput; } set { SetPropertyUtility.SetClass(ref m_OnValidateInput, value); } }
- public int characterLimit
- {
- get { return m_CharacterLimit; }
- set
- {
- if (SetPropertyUtility.SetStruct(ref m_CharacterLimit, Math.Max(0, value)))
- {
- UpdateLabel();
- if (m_SoftKeyboard != null)
- m_SoftKeyboard.characterLimit = value;
- }
- }
- }
- //public bool isInteractableControl { set { if ( } }
- /// <summary>
- /// Set the point size on both Placeholder and Input text object.
- /// </summary>
- public float pointSize
- {
- get { return m_GlobalPointSize; }
- set
- {
- if (SetPropertyUtility.SetStruct(ref m_GlobalPointSize, Math.Max(0, value)))
- {
- SetGlobalPointSize(m_GlobalPointSize);
- UpdateLabel();
- }
- }
- }
- /// <summary>
- /// Sets the Font Asset on both Placeholder and Input child objects.
- /// </summary>
- public TMP_FontAsset fontAsset
- {
- get { return m_GlobalFontAsset; }
- set
- {
- if (SetPropertyUtility.SetClass(ref m_GlobalFontAsset, value))
- {
- SetGlobalFontAsset(m_GlobalFontAsset);
- UpdateLabel();
- }
- }
- }
- [SerializeField]
- protected TMP_FontAsset m_GlobalFontAsset;
- /// <summary>
- /// Determines if the whole text will be selected when focused.
- /// </summary>
- public bool onFocusSelectAll
- {
- get { return m_OnFocusSelectAll; }
- set { m_OnFocusSelectAll = value; }
- }
- [SerializeField]
- protected bool m_OnFocusSelectAll = true;
- protected bool m_isSelectAll;
- /// <summary>
- /// Determines if the text and caret position as well as selection will be reset when the input field is deactivated.
- /// </summary>
- public bool resetOnDeActivation
- {
- get { return m_ResetOnDeActivation; }
- set { m_ResetOnDeActivation = value; }
- }
- [SerializeField]
- protected bool m_ResetOnDeActivation = true;
- private bool m_SelectionStillActive = false;
- private bool m_ReleaseSelection = false;
- private GameObject m_PreviouslySelectedObject;
- /// <summary>
- /// Controls whether the original text is restored when pressing "ESC".
- /// </summary>
- public bool restoreOriginalTextOnEscape
- {
- get { return m_RestoreOriginalTextOnEscape; }
- set { m_RestoreOriginalTextOnEscape = value; }
- }
- [SerializeField]
- private bool m_RestoreOriginalTextOnEscape = true;
- /// <summary>
- /// Is Rich Text editing allowed?
- /// </summary>
- public bool isRichTextEditingAllowed
- {
- get { return m_isRichTextEditingAllowed; }
- set { m_isRichTextEditingAllowed = value; }
- }
- [SerializeField]
- protected bool m_isRichTextEditingAllowed = false;
- // Content Type related
- public ContentType contentType { get { return m_ContentType; } set { if (SetPropertyUtility.SetStruct(ref m_ContentType, value)) EnforceContentType(); } }
- public LineType lineType
- {
- get { return m_LineType; }
- set
- {
- if (SetPropertyUtility.SetStruct(ref m_LineType, value))
- {
- SetToCustomIfContentTypeIsNot(ContentType.Standard, ContentType.Autocorrected);
- SetTextComponentWrapMode();
- }
- }
- }
- /// <summary>
- /// Limits the number of lines of text in the Input Field.
- /// </summary>
- public int lineLimit
- {
- get { return m_LineLimit; }
- set
- {
- if (m_LineType == LineType.SingleLine)
- m_LineLimit = 1;
- else
- SetPropertyUtility.SetStruct(ref m_LineLimit, value);
- }
- }
- [SerializeField]
- protected int m_LineLimit = 0;
- public InputType inputType { get { return m_InputType; } set { if (SetPropertyUtility.SetStruct(ref m_InputType, value)) SetToCustom(); } }
- public TouchScreenKeyboardType keyboardType
- {
- get { return m_KeyboardType; }
- set
- {
- if (SetPropertyUtility.SetStruct(ref m_KeyboardType, value))
- SetToCustom();
- }
- }
- public CharacterValidation characterValidation { get { return m_CharacterValidation; } set { if (SetPropertyUtility.SetStruct(ref m_CharacterValidation, value)) SetToCustom(); } }
- /// <summary>
- /// Sets the Input Validation to use a Custom Input Validation script.
- /// </summary>
- public TMP_InputValidator inputValidator
- {
- get { return m_InputValidator; }
- set { if (SetPropertyUtility.SetClass(ref m_InputValidator, value)) SetToCustom(CharacterValidation.CustomValidator); }
- }
- [SerializeField]
- protected TMP_InputValidator m_InputValidator = null;
- public bool readOnly { get { return m_ReadOnly; } set { m_ReadOnly = value; } }
- public bool richText { get { return m_RichText; } set { m_RichText = value; SetTextComponentRichTextMode(); } }
- // Derived property
- public bool multiLine { get { return m_LineType == LineType.MultiLineNewline || lineType == LineType.MultiLineSubmit; } }
- // Not shown in Inspector.
- public char asteriskChar { get { return m_AsteriskChar; } set { if (SetPropertyUtility.SetStruct(ref m_AsteriskChar, value)) UpdateLabel(); } }
- public bool wasCanceled { get { return m_WasCanceled; } }
- protected void ClampStringPos(ref int pos)
- {
- if (pos < 0)
- pos = 0;
- else if (pos > text.Length)
- pos = text.Length;
- }
- protected void ClampCaretPos(ref int pos)
- {
- if (pos < 0)
- pos = 0;
- else if (pos > m_TextComponent.textInfo.characterCount - 1)
- pos = m_TextComponent.textInfo.characterCount - 1;
- }
- /// <summary>
- /// Current position of the cursor.
- /// Getters are public Setters are protected
- /// </summary>
- protected int caretPositionInternal { get { return m_CaretPosition + compositionLength; } set { m_CaretPosition = value; ClampCaretPos(ref m_CaretPosition); } }
- protected int stringPositionInternal { get { return m_StringPosition + compositionLength; } set { m_StringPosition = value; ClampStringPos(ref m_StringPosition); } }
- protected int caretSelectPositionInternal { get { return m_CaretSelectPosition + compositionLength; } set { m_CaretSelectPosition = value; ClampCaretPos(ref m_CaretSelectPosition); } }
- protected int stringSelectPositionInternal { get { return m_StringSelectPosition + compositionLength; } set { m_StringSelectPosition = value; ClampStringPos(ref m_StringSelectPosition); } }
- private bool hasSelection { get { return stringPositionInternal != stringSelectPositionInternal; } }
- private bool m_isSelected;
- private bool m_IsStringPositionDirty;
- private bool m_IsCaretPositionDirty;
- private bool m_forceRectTransformAdjustment;
- /// <summary>
- /// Get: Returns the focus position as thats the position that moves around even during selection.
- /// Set: Set both the anchor and focus position such that a selection doesn't happen
- /// </summary>
- public int caretPosition
- {
- get { return caretSelectPositionInternal; }
- set { selectionAnchorPosition = value; selectionFocusPosition = value; m_IsStringPositionDirty = true; }
- }
- /// <summary>
- /// Get: Returns the fixed position of selection
- /// Set: If compositionString is 0 set the fixed position
- /// </summary>
- public int selectionAnchorPosition
- {
- get
- {
- return caretPositionInternal;
- }
- set
- {
- if (compositionLength != 0)
- return;
- caretPositionInternal = value;
- m_IsStringPositionDirty = true;
- }
- }
- /// <summary>
- /// Get: Returns the variable position of selection
- /// Set: If compositionString is 0 set the variable position
- /// </summary>
- public int selectionFocusPosition
- {
- get
- {
- return caretSelectPositionInternal;
- }
- set
- {
- if (compositionLength != 0)
- return;
- caretSelectPositionInternal = value;
- m_IsStringPositionDirty = true;
- }
- }
- /// <summary>
- ///
- /// </summary>
- public int stringPosition
- {
- get { return stringSelectPositionInternal; }
- set { selectionStringAnchorPosition = value; selectionStringFocusPosition = value; m_IsCaretPositionDirty = true; }
- }
- /// <summary>
- /// The fixed position of the selection in the raw string which may contains rich text.
- /// </summary>
- public int selectionStringAnchorPosition
- {
- get
- {
- return stringPositionInternal;
- }
- set
- {
- if (compositionLength != 0)
- return;
- stringPositionInternal = value;
- m_IsCaretPositionDirty = true;
- }
- }
- /// <summary>
- /// The variable position of the selection in the raw string which may contains rich text.
- /// </summary>
- public int selectionStringFocusPosition
- {
- get
- {
- return stringSelectPositionInternal;
- }
- set
- {
- if (compositionLength != 0)
- return;
- stringSelectPositionInternal = value;
- m_IsCaretPositionDirty = true;
- }
- }
- #if UNITY_EDITOR
- // Remember: This is NOT related to text validation!
- // This is Unity's own OnValidate method which is invoked when changing values in the Inspector.
- protected override void OnValidate()
- {
- base.OnValidate();
- EnforceContentType();
- m_CharacterLimit = Math.Max(0, m_CharacterLimit);
- //This can be invoked before OnEnabled is called. So we shouldn't be accessing other objects, before OnEnable is called.
- if (!IsActive())
- return;
- SetTextComponentRichTextMode();
- UpdateLabel();
- if (m_AllowInput)
- SetCaretActive();
- }
- #endif // if UNITY_EDITOR
- protected override void OnEnable()
- {
- //Debug.Log("*** OnEnable() *** - " + this.name);
- base.OnEnable();
- if (m_Text == null)
- m_Text = string.Empty;
- // Check if Input Field is driven by any layout components
- ILayoutController layoutController = GetComponent<ILayoutController>();
- if (layoutController != null)
- {
- m_IsDrivenByLayoutComponents = true;
- m_LayoutGroup = GetComponent<LayoutGroup>();
- }
- else
- m_IsDrivenByLayoutComponents = false;
- if (Application.isPlaying)
- {
- if (m_CachedInputRenderer == null && m_TextComponent != null)
- {
- GameObject go = new GameObject("Caret", typeof(TMP_SelectionCaret));
- go.hideFlags = HideFlags.DontSave;
- go.transform.SetParent(m_TextComponent.transform.parent);
- go.transform.SetAsFirstSibling();
- go.layer = gameObject.layer;
- caretRectTrans = go.GetComponent<RectTransform>();
- m_CachedInputRenderer = go.GetComponent<CanvasRenderer>();
- m_CachedInputRenderer.SetMaterial(Graphic.defaultGraphicMaterial, Texture2D.whiteTexture);
- // Needed as if any layout is present we want the caret to always be the same as the text area.
- go.AddComponent<LayoutElement>().ignoreLayout = true;
- AssignPositioningIfNeeded();
- }
- }
- m_RectTransform = GetComponent<RectTransform>();
- // Get a reference to the RectMask 2D on the Viewport Text Area object.
- if (m_TextViewport != null)
- {
- m_TextViewportRectMask = m_TextViewport.GetComponent<RectMask2D>();
- UpdateMaskRegions();
- }
- // If we have a cached renderer then we had OnDisable called so just restore the material.
- if (m_CachedInputRenderer != null)
- m_CachedInputRenderer.SetMaterial(Graphic.defaultGraphicMaterial, Texture2D.whiteTexture);
- if (m_TextComponent != null)
- {
- m_TextComponent.RegisterDirtyVerticesCallback(MarkGeometryAsDirty);
- m_TextComponent.RegisterDirtyVerticesCallback(UpdateLabel);
- // Cache reference to Vertical Scrollbar RectTransform and add listener.
- if (m_VerticalScrollbar != null)
- {
- m_VerticalScrollbar.onValueChanged.AddListener(OnScrollbarValueChange);
- }
- UpdateLabel();
- }
- // Subscribe to event fired when text object has been regenerated.
- TMPro_EventManager.TEXT_CHANGED_EVENT.Add(ON_TEXT_CHANGED);
- }
- protected override void OnDisable()
- {
- // the coroutine will be terminated, so this will ensure it restarts when we are next activated
- m_BlinkCoroutine = null;
- DeactivateInputField();
- if (m_TextComponent != null)
- {
- m_TextComponent.UnregisterDirtyVerticesCallback(MarkGeometryAsDirty);
- m_TextComponent.UnregisterDirtyVerticesCallback(UpdateLabel);
- if (m_VerticalScrollbar != null)
- m_VerticalScrollbar.onValueChanged.RemoveListener(OnScrollbarValueChange);
- }
- CanvasUpdateRegistry.UnRegisterCanvasElementForRebuild(this);
- // Clear needs to be called otherwise sync never happens as the object is disabled.
- if (m_CachedInputRenderer != null)
- m_CachedInputRenderer.Clear();
- if (m_Mesh != null)
- DestroyImmediate(m_Mesh);
- m_Mesh = null;
- // Unsubscribe to event triggered when text object has been regenerated
- TMPro_EventManager.TEXT_CHANGED_EVENT.Remove(ON_TEXT_CHANGED);
- base.OnDisable();
- }
- /// <summary>
- /// Method used to update the tracking of the caret position when the text object has been regenerated.
- /// </summary>
- /// <param name="obj"></param>
- private void ON_TEXT_CHANGED(UnityEngine.Object obj)
- {
- if (obj == m_TextComponent && Application.isPlaying && compositionLength == 0)
- {
- caretPositionInternal = GetCaretPositionFromStringIndex(stringPositionInternal);
- caretSelectPositionInternal = GetCaretPositionFromStringIndex(stringSelectPositionInternal);
- #if TMP_DEBUG_MODE
- Debug.Log("Caret Position: " + caretPositionInternal + " Selection Position: " + caretSelectPositionInternal + " String Position: " + stringPositionInternal + " String Select Position: " + stringSelectPositionInternal);
- #endif
- }
- }
- IEnumerator CaretBlink()
- {
- // Always ensure caret is initially visible since it can otherwise be confusing for a moment.
- m_CaretVisible = true;
- yield return null;
- while ((isFocused || m_SelectionStillActive) && m_CaretBlinkRate > 0)
- {
- // the blink rate is expressed as a frequency
- float blinkPeriod = 1f / m_CaretBlinkRate;
- // the caret should be ON if we are in the first half of the blink period
- bool blinkState = (Time.unscaledTime - m_BlinkStartTime) % blinkPeriod < blinkPeriod / 2;
- if (m_CaretVisible != blinkState)
- {
- m_CaretVisible = blinkState;
- if (!hasSelection)
- MarkGeometryAsDirty();
- }
- // Then wait again.
- yield return null;
- }
- m_BlinkCoroutine = null;
- }
- void SetCaretVisible()
- {
- if (!m_AllowInput)
- return;
- m_CaretVisible = true;
- m_BlinkStartTime = Time.unscaledTime;
- SetCaretActive();
- }
- // SetCaretActive will not set the caret immediately visible - it will wait for the next time to blink.
- // However, it will handle things correctly if the blink speed changed from zero to non-zero or non-zero to zero.
- void SetCaretActive()
- {
- if (!m_AllowInput)
- return;
- if (m_CaretBlinkRate > 0.0f)
- {
- if (m_BlinkCoroutine == null)
- m_BlinkCoroutine = StartCoroutine(CaretBlink());
- }
- else
- {
- m_CaretVisible = true;
- }
- }
- protected void OnFocus()
- {
- if (m_OnFocusSelectAll)
- SelectAll();
- }
- protected void SelectAll()
- {
- m_isSelectAll = true;
- stringPositionInternal = text.Length;
- stringSelectPositionInternal = 0;
- }
- /// <summary>
- /// Move to the end of the text.
- /// </summary>
- /// <param name="shift"></param>
- public void MoveTextEnd(bool shift)
- {
- if (m_isRichTextEditingAllowed)
- {
- int position = text.Length;
- if (shift)
- {
- stringSelectPositionInternal = position;
- }
- else
- {
- stringPositionInternal = position;
- stringSelectPositionInternal = stringPositionInternal;
- }
- }
- else
- {
- int position = m_TextComponent.textInfo.characterCount - 1;
- if (shift)
- {
- caretSelectPositionInternal = position;
- stringSelectPositionInternal = GetStringIndexFromCaretPosition(position);
- }
- else
- {
- caretPositionInternal = caretSelectPositionInternal = position;
- stringSelectPositionInternal = stringPositionInternal = GetStringIndexFromCaretPosition(position);
- }
- }
- UpdateLabel();
- }
- /// <summary>
- /// Move to the start of the text.
- /// </summary>
- /// <param name="shift"></param>
- public void MoveTextStart(bool shift)
- {
- if (m_isRichTextEditingAllowed)
- {
- int position = 0;
- if (shift)
- {
- stringSelectPositionInternal = position;
- }
- else
- {
- stringPositionInternal = position;
- stringSelectPositionInternal = stringPositionInternal;
- }
- }
- else
- {
- int position = 0;
- if (shift)
- {
- caretSelectPositionInternal = position;
- stringSelectPositionInternal = GetStringIndexFromCaretPosition(position);
- }
- else
- {
- caretPositionInternal = caretSelectPositionInternal = position;
- stringSelectPositionInternal = stringPositionInternal = GetStringIndexFromCaretPosition(position);
- }
- }
- UpdateLabel();
- }
- /// <summary>
- /// Move to the end of the current line of text.
- /// </summary>
- /// <param name="shift"></param>
- public void MoveToEndOfLine(bool shift, bool ctrl)
- {
- // Get the line the caret is currently located on.
- int currentLine = m_TextComponent.textInfo.characterInfo[caretPositionInternal].lineNumber;
- // Get the last character of the given line.
- int characterIndex = ctrl == true ? m_TextComponent.textInfo.characterCount - 1 : m_TextComponent.textInfo.lineInfo[currentLine].lastCharacterIndex;
- int position = m_TextComponent.textInfo.characterInfo[characterIndex].index;
- if (shift)
- {
- stringSelectPositionInternal = position;
- caretSelectPositionInternal = characterIndex;
- }
- else
- {
- stringPositionInternal = position;
- stringSelectPositionInternal = stringPositionInternal;
- caretSelectPositionInternal = caretPositionInternal = characterIndex;
- }
- UpdateLabel();
- }
- /// <summary>
- /// Move to the start of the current line of text.
- /// </summary>
- /// <param name="shift"></param>
- public void MoveToStartOfLine(bool shift, bool ctrl)
- {
- // Get the line the caret is currently located on.
- int currentLine = m_TextComponent.textInfo.characterInfo[caretPositionInternal].lineNumber;
- // Get the first character of the given line.
- int characterIndex = ctrl == true ? 0 : m_TextComponent.textInfo.lineInfo[currentLine].firstCharacterIndex;
- int position = 0;
- if (characterIndex > 0)
- position = m_TextComponent.textInfo.characterInfo[characterIndex - 1].index + m_TextComponent.textInfo.characterInfo[characterIndex - 1].stringLength;
- if (shift)
- {
- stringSelectPositionInternal = position;
- caretSelectPositionInternal = characterIndex;
- }
- else
- {
- stringPositionInternal = position;
- stringSelectPositionInternal = stringPositionInternal;
- caretSelectPositionInternal = caretPositionInternal = characterIndex;
- }
- UpdateLabel();
- }
- static string clipboard
- {
- get
- {
- return GUIUtility.systemCopyBuffer;
- }
- set
- {
- GUIUtility.systemCopyBuffer = value;
- }
- }
- private bool InPlaceEditing()
- {
- if (m_TouchKeyboardAllowsInPlaceEditing || (TouchScreenKeyboard.isSupported && (Application.platform == RuntimePlatform.WSAPlayerX86 || Application.platform == RuntimePlatform.WSAPlayerX64 || Application.platform == RuntimePlatform.WSAPlayerARM)))
- return true;
- if (TouchScreenKeyboard.isSupported && shouldHideSoftKeyboard)
- return true;
- if (TouchScreenKeyboard.isSupported && shouldHideSoftKeyboard == false && shouldHideMobileInput == false)
- return false;
- return true;
- }
- void UpdateStringPositionFromKeyboard()
- {
- // TODO: Might want to add null check here.
- var selectionRange = m_SoftKeyboard.selection;
- //if (selectionRange.start == 0 && selectionRange.length == 0)
- // return;
- var selectionStart = selectionRange.start;
- var selectionEnd = selectionRange.end;
- var stringPositionChanged = false;
- if (stringPositionInternal != selectionStart)
- {
- stringPositionChanged = true;
- stringPositionInternal = selectionStart;
- caretPositionInternal = GetCaretPositionFromStringIndex(stringPositionInternal);
- }
- if (stringSelectPositionInternal != selectionEnd)
- {
- stringSelectPositionInternal = selectionEnd;
- stringPositionChanged = true;
- caretSelectPositionInternal = GetCaretPositionFromStringIndex(stringSelectPositionInternal);
- }
- if (stringPositionChanged)
- {
- m_BlinkStartTime = Time.unscaledTime;
- UpdateLabel();
- }
- }
- /// <summary>
- /// Update the text based on input.
- /// </summary>
- // TODO: Make LateUpdate a coroutine instead. Allows us to control the update to only be when the field is active.
- protected virtual void LateUpdate()
- {
- // Only activate if we are not already activated.
- if (m_ShouldActivateNextUpdate)
- {
- if (!isFocused)
- {
- ActivateInputFieldInternal();
- m_ShouldActivateNextUpdate = false;
- return;
- }
- // Reset as we are already activated.
- m_ShouldActivateNextUpdate = false;
- }
- // Update Scrollbar if needed
- if (m_IsScrollbarUpdateRequired)
- {
- UpdateScrollbar();
- m_IsScrollbarUpdateRequired = false;
- }
- // Handle double click to reset / deselect Input Field when ResetOnActivation is false.
- if (!isFocused && m_SelectionStillActive)
- {
- GameObject selectedObject = EventSystem.current != null ? EventSystem.current.currentSelectedGameObject : null;
- if (selectedObject == null && m_ResetOnDeActivation)
- {
- ReleaseSelection();
- return;
- }
- if (selectedObject != null && selectedObject != this.gameObject)
- {
- if (selectedObject == m_PreviouslySelectedObject)
- return;
- m_PreviouslySelectedObject = selectedObject;
- // Special handling for Vertical Scrollbar
- if (m_VerticalScrollbar && selectedObject == m_VerticalScrollbar.gameObject)
- {
- // Do not release selection
- return;
- }
- // Release selection for all objects when ResetOnDeActivation is true
- if (m_ResetOnDeActivation)
- {
- ReleaseSelection();
- return;
- }
- // Release current selection of selected object is another Input Field
- if (selectedObject.GetComponent<TMP_InputField>() != null)
- ReleaseSelection();
- return;
- }
- #if ENABLE_INPUT_SYSTEM
- if (m_ProcessingEvent != null && m_ProcessingEvent.rawType == EventType.MouseDown && m_ProcessingEvent.button == 0)
- {
- // Check for Double Click
- bool isDoubleClick = false;
- float timeStamp = Time.unscaledTime;
- if (m_KeyDownStartTime + m_DoubleClickDelay > timeStamp)
- isDoubleClick = true;
- m_KeyDownStartTime = timeStamp;
- if (isDoubleClick)
- {
- //m_StringPosition = m_StringSelectPosition = 0;
- //m_CaretPosition = m_CaretSelectPosition = 0;
- //m_TextComponent.rectTransform.localPosition = m_DefaultTransformPosition;
- //if (caretRectTrans != null)
- // caretRectTrans.localPosition = Vector3.zero;
- ReleaseSelection();
- return;
- }
- }
- #else
- if (Input.GetKeyDown(KeyCode.Mouse0))
- {
- // Check for Double Click
- bool isDoubleClick = false;
- float timeStamp = Time.unscaledTime;
- if (m_KeyDownStartTime + m_DoubleClickDelay > timeStamp)
- isDoubleClick = true;
- m_KeyDownStartTime = timeStamp;
- if (isDoubleClick)
- {
- //m_StringPosition = m_StringSelectPosition = 0;
- //m_CaretPosition = m_CaretSelectPosition = 0;
- //m_TextComponent.rectTransform.localPosition = m_DefaultTransformPosition;
- //if (caretRectTrans != null)
- // caretRectTrans.localPosition = Vector3.zero;
- ReleaseSelection();
- return;
- }
- }
- #endif
- }
- UpdateMaskRegions();
- if (InPlaceEditing() && isKeyboardUsingEvents() || !isFocused)
- {
- return;
- }
- AssignPositioningIfNeeded();
- if (m_SoftKeyboard == null || m_SoftKeyboard.status != TouchScreenKeyboard.Status.Visible)
- {
- if (m_SoftKeyboard != null)
- {
- if (!m_ReadOnly)
- text = m_SoftKeyboard.text;
- if (m_SoftKeyboard.status == TouchScreenKeyboard.Status.LostFocus)
- SendTouchScreenKeyboardStatusChanged();
- if (m_SoftKeyboard.status == TouchScreenKeyboard.Status.Canceled)
- {
- m_ReleaseSelection = true;
- m_WasCanceled = true;
- SendTouchScreenKeyboardStatusChanged();
- }
- if (m_SoftKeyboard.status == TouchScreenKeyboard.Status.Done)
- {
- m_ReleaseSelection = true;
- OnSubmit(null);
- SendTouchScreenKeyboardStatusChanged();
- }
- }
- OnDeselect(null);
- return;
- }
- string val = m_SoftKeyboard.text;
- if (m_Text != val)
- {
- if (m_ReadOnly)
- {
- m_SoftKeyboard.text = m_Text;
- }
- else
- {
- m_Text = "";
- for (int i = 0; i < val.Length; ++i)
- {
- char c = val[i];
- if (c == '\r' || (int)c == 3)
- c = '\n';
- if (onValidateInput != null)
- c = onValidateInput(m_Text, m_Text.Length, c);
- else if (characterValidation != CharacterValidation.None)
- c = Validate(m_Text, m_Text.Length, c);
- if (lineType == LineType.MultiLineSubmit && c == '\n')
- {
- m_SoftKeyboard.text = m_Text;
- OnSubmit(null);
- OnDeselect(null);
- return;
- }
- if (c != 0)
- m_Text += c;
- }
- if (characterLimit > 0 && m_Text.Length > characterLimit)
- m_Text = m_Text.Substring(0, characterLimit);
- UpdateStringPositionFromKeyboard();
- // Set keyboard text before updating label, as we might have changed it with validation
- // and update label will take the old value from keyboard if we don't change it here
- if (m_Text != val)
- m_SoftKeyboard.text = m_Text;
- SendOnValueChangedAndUpdateLabel();
- }
- }
- else if (m_HideMobileInput && Application.platform == RuntimePlatform.Android)
- {
- UpdateStringPositionFromKeyboard();
- }
- //else if (m_HideMobileInput) // m_Keyboard.canSetSelection
- //{
- // int length = stringPositionInternal < stringSelectPositionInternal ? stringSelectPositionInternal - stringPositionInternal : stringPositionInternal - stringSelectPositionInternal;
- // m_SoftKeyboard.selection = new RangeInt(stringPositionInternal < stringSelectPositionInternal ? stringPositionInternal : stringSelectPositionInternal, length);
- //}
- //else if (!m_HideMobileInput) // m_Keyboard.canGetSelection)
- //{
- // UpdateStringPositionFromKeyboard();
- //}
- if (m_SoftKeyboard.status != TouchScreenKeyboard.Status.Visible)
- {
- if (m_SoftKeyboard.status == TouchScreenKeyboard.Status.Canceled)
- m_WasCanceled = true;
- OnDeselect(null);
- }
- }
- private bool MayDrag(PointerEventData eventData)
- {
- return IsActive() &&
- IsInteractable() &&
- eventData.button == PointerEventData.InputButton.Left &&
- m_TextComponent != null &&
- (m_SoftKeyboard == null || shouldHideSoftKeyboard || shouldHideMobileInput);
- }
- public virtual void OnBeginDrag(PointerEventData eventData)
- {
- if (!MayDrag(eventData))
- return;
- m_UpdateDrag = true;
- }
- public virtual void OnDrag(PointerEventData eventData)
- {
- if (!MayDrag(eventData))
- return;
- CaretPosition insertionSide;
- int insertionIndex = TMP_TextUtilities.GetCursorIndexFromPosition(m_TextComponent, eventData.position, eventData.pressEventCamera, out insertionSide);
- if (m_isRichTextEditingAllowed)
- {
- if (insertionSide == CaretPosition.Left)
- {
- stringSelectPositionInternal = m_TextComponent.textInfo.characterInfo[insertionIndex].index;
- }
- else if (insertionSide == CaretPosition.Right)
- {
- stringSelectPositionInternal = m_TextComponent.textInfo.characterInfo[insertionIndex].index + m_TextComponent.textInfo.characterInfo[insertionIndex].stringLength;
- }
- }
- else
- {
- if (insertionSide == CaretPosition.Left)
- {
- stringSelectPositionInternal = insertionIndex == 0
- ? m_TextComponent.textInfo.characterInfo[0].index
- : m_TextComponent.textInfo.characterInfo[insertionIndex - 1].index + m_TextComponent.textInfo.characterInfo[insertionIndex - 1].stringLength;
- }
- else if (insertionSide == CaretPosition.Right)
- {
- stringSelectPositionInternal = m_TextComponent.textInfo.characterInfo[insertionIndex].index + m_TextComponent.textInfo.characterInfo[insertionIndex].stringLength;
- }
- }
- caretSelectPositionInternal = GetCaretPositionFromStringIndex(stringSelectPositionInternal);
- MarkGeometryAsDirty();
- m_DragPositionOutOfBounds = !RectTransformUtility.RectangleContainsScreenPoint(textViewport, eventData.position, eventData.pressEventCamera);
- if (m_DragPositionOutOfBounds && m_DragCoroutine == null)
- m_DragCoroutine = StartCoroutine(MouseDragOutsideRect(eventData));
- eventData.Use();
- #if TMP_DEBUG_MODE
- Debug.Log("Caret Position: " + caretPositionInternal + " Selection Position: " + caretSelectPositionInternal + " String Position: " + stringPositionInternal + " String Select Position: " + stringSelectPositionInternal);
- #endif
- }
- IEnumerator MouseDragOutsideRect(PointerEventData eventData)
- {
- while (m_UpdateDrag && m_DragPositionOutOfBounds)
- {
- Vector2 localMousePos;
- RectTransformUtility.ScreenPointToLocalPointInRectangle(textViewport, eventData.position, eventData.pressEventCamera, out localMousePos);
- Rect rect = textViewport.rect;
- if (multiLine)
- {
- if (localMousePos.y > rect.yMax)
- MoveUp(true, true);
- else if (localMousePos.y < rect.yMin)
- MoveDown(true, true);
- }
- else
- {
- if (localMousePos.x < rect.xMin)
- MoveLeft(true, false);
- else if (localMousePos.x > rect.xMax)
- MoveRight(true, false);
- }
- UpdateLabel();
- float delay = multiLine ? kVScrollSpeed : kHScrollSpeed;
- if (m_WaitForSecondsRealtime == null)
- m_WaitForSecondsRealtime = new WaitForSecondsRealtime(delay);
- else
- m_WaitForSecondsRealtime.waitTime = delay;
- yield return m_WaitForSecondsRealtime;
- }
- m_DragCoroutine = null;
- }
- public virtual void OnEndDrag(PointerEventData eventData)
- {
- if (!MayDrag(eventData))
- return;
- m_UpdateDrag = false;
- }
- public override void OnPointerDown(PointerEventData eventData)
- {
- if (!MayDrag(eventData))
- return;
- EventSystem.current.SetSelectedGameObject(gameObject, eventData);
- bool hadFocusBefore = m_AllowInput;
- base.OnPointerDown(eventData);
- if (InPlaceEditing() == false)
- {
- if (m_SoftKeyboard == null || !m_SoftKeyboard.active)
- {
- OnSelect(eventData);
- return;
- }
- }
- #if ENABLE_INPUT_SYSTEM
- Event.PopEvent(m_ProcessingEvent);
- bool shift = m_ProcessingEvent != null && (m_ProcessingEvent.modifiers & EventModifiers.Shift) != 0;
- #else
- bool shift = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
- #endif
- // Check for Double Click
- bool isDoubleClick = false;
- float timeStamp = Time.unscaledTime;
- if (m_PointerDownClickStartTime + m_DoubleClickDelay > timeStamp)
- isDoubleClick = true;
- m_PointerDownClickStartTime = timeStamp;
- // Only set caret position if we didn't just get focus now.
- // Otherwise it will overwrite the select all on focus.
- if (hadFocusBefore || !m_OnFocusSelectAll)
- {
- CaretPosition insertionSide;
- int insertionIndex = TMP_TextUtilities.GetCursorIndexFromPosition(m_TextComponent, eventData.position, eventData.pressEventCamera, out insertionSide);
- if (shift)
- {
- if (m_isRichTextEditingAllowed)
- {
- if (insertionSide == CaretPosition.Left)
- {
- stringSelectPositionInternal = m_TextComponent.textInfo.characterInfo[insertionIndex].index;
- }
- else if (insertionSide == CaretPosition.Right)
- {
- stringSelectPositionInternal = m_TextComponent.textInfo.characterInfo[insertionIndex].index + m_TextComponent.textInfo.characterInfo[insertionIndex].stringLength;
- }
- }
- else
- {
- if (insertionSide == CaretPosition.Left)
- {
- stringSelectPositionInternal = insertionIndex == 0
- ? m_TextComponent.textInfo.characterInfo[0].index
- : m_TextComponent.textInfo.characterInfo[insertionIndex - 1].index + m_TextComponent.textInfo.characterInfo[insertionIndex - 1].stringLength;
- }
- else if (insertionSide == CaretPosition.Right)
- {
- stringSelectPositionInternal = m_TextComponent.textInfo.characterInfo[insertionIndex].index + m_TextComponent.textInfo.characterInfo[insertionIndex].stringLength;
- }
- }
- }
- else
- {
- if (m_isRichTextEditingAllowed)
- {
- if (insertionSide == CaretPosition.Left)
- {
- stringPositionInternal = stringSelectPositionInternal = m_TextComponent.textInfo.characterInfo[insertionIndex].index;
- }
- else if (insertionSide == CaretPosition.Right)
- {
- stringPositionInternal = stringSelectPositionInternal = m_TextComponent.textInfo.characterInfo[insertionIndex].index + m_TextComponent.textInfo.characterInfo[insertionIndex].stringLength;
- }
- }
- else
- {
- if (insertionSide == CaretPosition.Left)
- {
- stringPositionInternal = stringSelectPositionInternal = insertionIndex == 0
- ? m_TextComponent.textInfo.characterInfo[0].index
- : m_TextComponent.textInfo.characterInfo[insertionIndex - 1].index + m_TextComponent.textInfo.characterInfo[insertionIndex - 1].stringLength;
- }
- else if (insertionSide == CaretPosition.Right)
- {
- stringPositionInternal = stringSelectPositionInternal = m_TextComponent.textInfo.characterInfo[insertionIndex].index + m_TextComponent.textInfo.characterInfo[insertionIndex].stringLength;
- }
- }
- }
- if (isDoubleClick)
- {
- int wordIndex = TMP_TextUtilities.FindIntersectingWord(m_TextComponent, eventData.position, eventData.pressEventCamera);
- if (wordIndex != -1)
- {
- // TODO: Should behavior be different if rich text editing is enabled or not?
- // Select current word
- caretPositionInternal = m_TextComponent.textInfo.wordInfo[wordIndex].firstCharacterIndex;
- caretSelectPositionInternal = m_TextComponent.textInfo.wordInfo[wordIndex].lastCharacterIndex + 1;
- stringPositionInternal = m_TextComponent.textInfo.characterInfo[caretPositionInternal].index;
- stringSelectPositionInternal = m_TextComponent.textInfo.characterInfo[caretSelectPositionInternal - 1].index + m_TextComponent.textInfo.characterInfo[caretSelectPositionInternal - 1].stringLength;
- }
- else
- {
- // Select current character
- caretPositionInternal = insertionIndex;
- caretSelectPositionInternal = caretPositionInternal + 1;
- stringPositionInternal = m_TextComponent.textInfo.characterInfo[insertionIndex].index;
- stringSelectPositionInternal = stringPositionInternal + m_TextComponent.textInfo.characterInfo[insertionIndex].stringLength;
- }
- }
- else
- {
- caretPositionInternal = caretSelectPositionInternal = GetCaretPositionFromStringIndex(stringPositionInternal);
- }
- m_isSelectAll = false;
- }
- UpdateLabel();
- eventData.Use();
- #if TMP_DEBUG_MODE
- Debug.Log("Caret Position: " + caretPositionInternal + " Selection Position: " + caretSelectPositionInternal + " String Position: " + stringPositionInternal + " String Select Position: " + stringSelectPositionInternal);
- #endif
- }
- protected enum EditState
- {
- Continue,
- Finish
- }
- protected EditState KeyPressed(Event evt)
- {
- var currentEventModifiers = evt.modifiers;
- bool ctrl = SystemInfo.operatingSystemFamily == OperatingSystemFamily.MacOSX ? (currentEventModifiers & EventModifiers.Command) != 0 : (currentEventModifiers & EventModifiers.Control) != 0;
- bool shift = (currentEventModifiers & EventModifiers.Shift) != 0;
- bool alt = (currentEventModifiers & EventModifiers.Alt) != 0;
- bool ctrlOnly = ctrl && !alt && !shift;
- switch (evt.keyCode)
- {
- case KeyCode.Backspace:
- {
- Backspace();
- return EditState.Continue;
- }
- case KeyCode.Delete:
- {
- DeleteKey();
- return EditState.Continue;
- }
- case KeyCode.Home:
- {
- MoveToStartOfLine(shift, ctrl);
- return EditState.Continue;
- }
- case KeyCode.End:
- {
- MoveToEndOfLine(shift, ctrl);
- return EditState.Continue;
- }
- // Select All
- case KeyCode.A:
- {
- if (ctrlOnly)
- {
- SelectAll();
- return EditState.Continue;
- }
- break;
- }
- // Copy
- case KeyCode.C:
- {
- if (ctrlOnly)
- {
- if (inputType != InputType.Password)
- clipboard = GetSelectedString();
- else
- clipboard = "";
- return EditState.Continue;
- }
- break;
- }
- // Paste
- case KeyCode.V:
- {
- if (ctrlOnly)
- {
- Append(clipboard);
- return EditState.Continue;
- }
- break;
- }
- // Cut
- case KeyCode.X:
- {
- if (ctrlOnly)
- {
- if (inputType != InputType.Password)
- clipboard = GetSelectedString();
- else
- clipboard = "";
- Delete();
- UpdateTouchKeyboardFromEditChanges();
- SendOnValueChangedAndUpdateLabel();
- return EditState.Continue;
- }
- break;
- }
- case KeyCode.LeftArrow:
- {
- MoveLeft(shift, ctrl);
- return EditState.Continue;
- }
- case KeyCode.RightArrow:
- {
- MoveRight(shift, ctrl);
- return EditState.Continue;
- }
- case KeyCode.UpArrow:
- {
- MoveUp(shift);
- return EditState.Continue;
- }
- case KeyCode.DownArrow:
- {
- MoveDown(shift);
- return EditState.Continue;
- }
- case KeyCode.PageUp:
- {
- MovePageUp(shift);
- return EditState.Continue;
- }
- case KeyCode.PageDown:
- {
- MovePageDown(shift);
- return EditState.Continue;
- }
- // Submit
- case KeyCode.Return:
- case KeyCode.KeypadEnter:
- {
- if (lineType != LineType.MultiLineNewline)
- {
- m_ReleaseSelection = true;
- return EditState.Finish;
- }
- break;
- }
- case KeyCode.Escape:
- {
- m_ReleaseSelection = true;
- m_WasCanceled = true;
- return EditState.Finish;
- }
- }
- char c = evt.character;
- // Don't allow return chars or tabulator key to be entered into single line fields.
- if (!multiLine && (c == '\t' || c == '\r' || c == 10))
- return EditState.Continue;
- // Convert carriage return and end-of-text characters to newline.
- if (c == '\r' || (int)c == 3)
- c = '\n';
- // Convert Shift Enter to Vertical tab
- if (shift && c == '\n')
- c = '\v';
- if (IsValidChar(c))
- {
- Append(c);
- }
- if (c == 0)
- {
- if (compositionLength > 0)
- {
- UpdateLabel();
- }
- }
- return EditState.Continue;
- }
- protected virtual bool IsValidChar(char c)
- {
- // Null character
- if (c == 0)
- return false;
- // Delete key on mac
- if (c == 127)
- return false;
- // Accept newline and tab
- if (c == '\t' || c == '\n')
- return true;
- return true;
- // With the addition of Dynamic support, I think this will best be handled by the text component.
- //return m_TextComponent.font.HasCharacter(c, true);
- }
- /// <summary>
- /// Handle the specified event.
- /// </summary>
- private Event m_ProcessingEvent = new Event();
- public void ProcessEvent(Event e)
- {
- KeyPressed(e);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="eventData"></param>
- public virtual void OnUpdateSelected(BaseEventData eventData)
- {
- if (!isFocused)
- return;
- bool consumedEvent = false;
- EditState shouldContinue;
- while (Event.PopEvent(m_ProcessingEvent))
- {
- //Debug.Log("Event: " + m_ProcessingEvent.ToString() + " IsCompositionActive= " + m_IsCompositionActive + " Composition Length: " + compositionLength);
- switch (m_ProcessingEvent.rawType)
- {
- case EventType.KeyUp:
- // TODO: Figure out way to handle navigation during IME Composition.
- break;
- case EventType.KeyDown:
- consumedEvent = true;
- // Special handling on OSX which produces more events which need to be suppressed.
- if (m_IsCompositionActive && compositionLength == 0)
- {
- //if (m_ProcessingEvent.keyCode == KeyCode.Backspace && m_ProcessingEvent.modifiers == EventModifiers.None)
- //{
- // int eventCount = Event.GetEventCount();
- // // Suppress all subsequent events
- // for (int i = 0; i < eventCount; i++)
- // Event.PopEvent(m_ProcessingEvent);
- // break;
- //}
- // Suppress other events related to navigation or termination of composition sequence.
- if (m_ProcessingEvent.character == 0 && m_ProcessingEvent.modifiers == EventModifiers.None)
- break;
- }
- shouldContinue = KeyPressed(m_ProcessingEvent);
- if (shouldContinue == EditState.Finish)
- {
- SendOnSubmit();
- DeactivateInputField();
- break;
- }
- m_IsTextComponentUpdateRequired = true;
- UpdateLabel();
- break;
- case EventType.ValidateCommand:
- case EventType.ExecuteCommand:
- switch (m_ProcessingEvent.commandName)
- {
- case "SelectAll":
- SelectAll();
- consumedEvent = true;
- break;
- }
- break;
- }
- }
- if (consumedEvent)
- UpdateLabel();
- eventData.Use();
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="eventData"></param>
- public virtual void OnScroll(PointerEventData eventData)
- {
- // Return if Single Line
- if (m_LineType == LineType.SingleLine)
- return;
- if (m_TextComponent.preferredHeight < m_TextViewport.rect.height)
- return;
- float scrollDirection = -eventData.scrollDelta.y;
- m_ScrollPosition = m_ScrollPosition + (1f / m_TextComponent.textInfo.lineCount) * scrollDirection * m_ScrollSensitivity;
- m_ScrollPosition = Mathf.Clamp01(m_ScrollPosition);
- AdjustTextPositionRelativeToViewport(m_ScrollPosition);
- if (m_VerticalScrollbar)
- {
- m_IsUpdatingScrollbarValues = true;
- m_VerticalScrollbar.value = m_ScrollPosition;
- }
- //Debug.Log("Scroll Position:" + m_ScrollPosition);
- }
- private string GetSelectedString()
- {
- if (!hasSelection)
- return "";
- int startPos = stringPositionInternal;
- int endPos = stringSelectPositionInternal;
- // Ensure pos is always less then selPos to make the code simpler
- if (startPos > endPos)
- {
- int temp = startPos;
- startPos = endPos;
- endPos = temp;
- }
- //for (int i = m_CaretPosition; i < m_CaretSelectPosition; i++)
- //{
- // Debug.Log("Character [" + m_TextComponent.textInfo.characterInfo[i].character + "] using Style [" + m_TextComponent.textInfo.characterInfo[i].style + "] has been selected.");
- //}
- return text.Substring(startPos, endPos - startPos);
- }
- private int FindNextWordBegin()
- {
- if (stringSelectPositionInternal + 1 >= text.Length)
- return text.Length;
- int spaceLoc = text.IndexOfAny(kSeparators, stringSelectPositionInternal + 1);
- if (spaceLoc == -1)
- spaceLoc = text.Length;
- else
- spaceLoc++;
- return spaceLoc;
- }
- private void MoveRight(bool shift, bool ctrl)
- {
- if (hasSelection && !shift)
- {
- // By convention, if we have a selection and move right without holding shift,
- // we just place the cursor at the end.
- stringPositionInternal = stringSelectPositionInternal = Mathf.Max(stringPositionInternal, stringSelectPositionInternal);
- caretPositionInternal = caretSelectPositionInternal = GetCaretPositionFromStringIndex(stringSelectPositionInternal);
- #if TMP_DEBUG_MODE
- Debug.Log("Caret Position: " + caretPositionInternal + " Selection Position: " + caretSelectPositionInternal + " String Position: " + stringPositionInternal + " String Select Position: " + stringSelectPositionInternal);
- #endif
- return;
- }
- int position;
- if (ctrl)
- position = FindNextWordBegin();
- else
- {
- if (m_isRichTextEditingAllowed)
- {
- // Special handling for Surrogate pairs and Diacritical marks.
- if (stringSelectPositionInternal < text.Length && char.IsHighSurrogate(text[stringSelectPositionInternal]))
- position = stringSelectPositionInternal + 2;
- else
- position = stringSelectPositionInternal + 1;
- }
- else
- {
- position = m_TextComponent.textInfo.characterInfo[caretSelectPositionInternal].index + m_TextComponent.textInfo.characterInfo[caretSelectPositionInternal].stringLength;
- }
- }
- if (shift)
- {
- stringSelectPositionInternal = position;
- caretSelectPositionInternal = GetCaretPositionFromStringIndex(stringSelectPositionInternal);
- }
- else
- {
- stringSelectPositionInternal = stringPositionInternal = position;
- // Only increase caret position as we cross character boundary.
- if (stringPositionInternal >= m_TextComponent.textInfo.characterInfo[caretPositionInternal].index + m_TextComponent.textInfo.characterInfo[caretPositionInternal].stringLength)
- caretSelectPositionInternal = caretPositionInternal = GetCaretPositionFromStringIndex(stringSelectPositionInternal);
- }
- #if TMP_DEBUG_MODE
- Debug.Log("Caret Position: " + caretPositionInternal + " Selection Position: " + caretSelectPositionInternal + " String Position: " + stringPositionInternal + " String Select Position: " + stringSelectPositionInternal);
- #endif
- }
- private int FindPrevWordBegin()
- {
- if (stringSelectPositionInternal - 2 < 0)
- return 0;
- int spaceLoc = text.LastIndexOfAny(kSeparators, stringSelectPositionInternal - 2);
- if (spaceLoc == -1)
- spaceLoc = 0;
- else
- spaceLoc++;
- return spaceLoc;
- }
- private void MoveLeft(bool shift, bool ctrl)
- {
- if (hasSelection && !shift)
- {
- // By convention, if we have a selection and move left without holding shift,
- // we just place the cursor at the start.
- stringPositionInternal = stringSelectPositionInternal = Mathf.Min(stringPositionInternal, stringSelectPositionInternal);
- caretPositionInternal = caretSelectPositionInternal = GetCaretPositionFromStringIndex(stringSelectPositionInternal);
- #if TMP_DEBUG_MODE
- Debug.Log("Caret Position: " + caretPositionInternal + " Selection Position: " + caretSelectPositionInternal + " String Position: " + stringPositionInternal + " String Select Position: " + stringSelectPositionInternal);
- #endif
- return;
- }
- int position;
- if (ctrl)
- position = FindPrevWordBegin();
- else
- {
- if (m_isRichTextEditingAllowed)
- {
- // Special handling for Surrogate pairs and Diacritical marks.
- if (stringSelectPositionInternal > 0 && char.IsLowSurrogate(text[stringSelectPositionInternal - 1]))
- position = stringSelectPositionInternal - 2;
- else
- position = stringSelectPositionInternal - 1;
- }
- else
- {
- //position = GetStringIndexFromCaretPosition(caretSelectPositionInternal - 1);
- position = caretSelectPositionInternal < 2
- ? m_TextComponent.textInfo.characterInfo[0].index
- : m_TextComponent.textInfo.characterInfo[caretSelectPositionInternal - 2].index + m_TextComponent.textInfo.characterInfo[caretSelectPositionInternal - 2].stringLength;
- }
- }
- if (shift)
- {
- stringSelectPositionInternal = position;
- caretSelectPositionInternal = GetCaretPositionFromStringIndex(stringSelectPositionInternal);
- }
- else
- {
- stringSelectPositionInternal = stringPositionInternal = position;
- // Only decrease caret position as we cross character boundary.
- if (caretPositionInternal > 0 && stringPositionInternal <= m_TextComponent.textInfo.characterInfo[caretPositionInternal - 1].index)
- caretSelectPositionInternal = caretPositionInternal = GetCaretPositionFromStringIndex(stringSelectPositionInternal);
- }
- #if TMP_DEBUG_MODE
- Debug.Log("Caret Position: " + caretPositionInternal + " Selection Position: " + caretSelectPositionInternal + " String Position: " + stringPositionInternal + " String Select Position: " + stringSelectPositionInternal);
- #endif
- }
- private int LineUpCharacterPosition(int originalPos, bool goToFirstChar)
- {
- if (originalPos >= m_TextComponent.textInfo.characterCount)
- originalPos -= 1;
- TMP_CharacterInfo originChar = m_TextComponent.textInfo.characterInfo[originalPos];
- int originLine = originChar.lineNumber;
- // We are on the first line return first character
- if (originLine - 1 < 0)
- return goToFirstChar ? 0 : originalPos;
- int endCharIdx = m_TextComponent.textInfo.lineInfo[originLine].firstCharacterIndex - 1;
- int closest = -1;
- float distance = TMP_Math.FLOAT_MAX;
- float range = 0;
- for (int i = m_TextComponent.textInfo.lineInfo[originLine - 1].firstCharacterIndex; i < endCharIdx; ++i)
- {
- TMP_CharacterInfo currentChar = m_TextComponent.textInfo.characterInfo[i];
- float d = originChar.origin - currentChar.origin;
- float r = d / (currentChar.xAdvance - currentChar.origin);
- if (r >= 0 && r <= 1)
- {
- if (r < 0.5f)
- return i;
- else
- return i + 1;
- }
- d = Mathf.Abs(d);
- if (d < distance)
- {
- closest = i;
- distance = d;
- range = r;
- }
- }
- if (closest == -1) return endCharIdx;
- //Debug.Log("Returning nearest character with Range = " + range);
- if (range < 0.5f)
- return closest;
- else
- return closest + 1;
- }
- private int LineDownCharacterPosition(int originalPos, bool goToLastChar)
- {
- if (originalPos >= m_TextComponent.textInfo.characterCount)
- return m_TextComponent.textInfo.characterCount - 1; // text.Length;
- TMP_CharacterInfo originChar = m_TextComponent.textInfo.characterInfo[originalPos];
- int originLine = originChar.lineNumber;
- //// We are on the last line return last character
- if (originLine + 1 >= m_TextComponent.textInfo.lineCount)
- return goToLastChar ? m_TextComponent.textInfo.characterCount - 1 : originalPos;
- // Need to determine end line for next line.
- int endCharIdx = m_TextComponent.textInfo.lineInfo[originLine + 1].lastCharacterIndex;
- int closest = -1;
- float distance = TMP_Math.FLOAT_MAX;
- float range = 0;
- for (int i = m_TextComponent.textInfo.lineInfo[originLine + 1].firstCharacterIndex; i < endCharIdx; ++i)
- {
- TMP_CharacterInfo currentChar = m_TextComponent.textInfo.characterInfo[i];
- float d = originChar.origin - currentChar.origin;
- float r = d / (currentChar.xAdvance - currentChar.origin);
- if (r >= 0 && r <= 1)
- {
- if (r < 0.5f)
- return i;
- else
- return i + 1;
- }
- d = Mathf.Abs(d);
- if (d < distance)
- {
- closest = i;
- distance = d;
- range = r;
- }
- }
- if (closest == -1) return endCharIdx;
- //Debug.Log("Returning nearest character with Range = " + range);
- if (range < 0.5f)
- return closest;
- else
- return closest + 1;
- }
- private int PageUpCharacterPosition(int originalPos, bool goToFirstChar)
- {
- if (originalPos >= m_TextComponent.textInfo.characterCount)
- originalPos -= 1;
- TMP_CharacterInfo originChar = m_TextComponent.textInfo.characterInfo[originalPos];
- int originLine = originChar.lineNumber;
- // We are on the first line return first character
- if (originLine - 1 < 0)
- return goToFirstChar ? 0 : originalPos;
- float viewportHeight = m_TextViewport.rect.height;
- int newLine = originLine - 1;
- // Iterate through each subsequent line to find the first baseline that is not visible in the viewport.
- for (; newLine > 0; newLine--)
- {
- if (m_TextComponent.textInfo.lineInfo[newLine].baseline > m_TextComponent.textInfo.lineInfo[originLine].baseline + viewportHeight)
- break;
- }
- int endCharIdx = m_TextComponent.textInfo.lineInfo[newLine].lastCharacterIndex;
- int closest = -1;
- float distance = TMP_Math.FLOAT_MAX;
- float range = 0;
- for (int i = m_TextComponent.textInfo.lineInfo[newLine].firstCharacterIndex; i < endCharIdx; ++i)
- {
- TMP_CharacterInfo currentChar = m_TextComponent.textInfo.characterInfo[i];
- float d = originChar.origin - currentChar.origin;
- float r = d / (currentChar.xAdvance - currentChar.origin);
- if (r >= 0 && r <= 1)
- {
- if (r < 0.5f)
- return i;
- else
- return i + 1;
- }
- d = Mathf.Abs(d);
- if (d < distance)
- {
- closest = i;
- distance = d;
- range = r;
- }
- }
- if (closest == -1) return endCharIdx;
- //Debug.Log("Returning nearest character with Range = " + range);
- if (range < 0.5f)
- return closest;
- else
- return closest + 1;
- }
- private int PageDownCharacterPosition(int originalPos, bool goToLastChar)
- {
- if (originalPos >= m_TextComponent.textInfo.characterCount)
- return m_TextComponent.textInfo.characterCount - 1;
- TMP_CharacterInfo originChar = m_TextComponent.textInfo.characterInfo[originalPos];
- int originLine = originChar.lineNumber;
- // We are on the last line return last character
- if (originLine + 1 >= m_TextComponent.textInfo.lineCount)
- return goToLastChar ? m_TextComponent.textInfo.characterCount - 1 : originalPos;
- float viewportHeight = m_TextViewport.rect.height;
- int newLine = originLine + 1;
- // Iterate through each subsequent line to find the first baseline that is not visible in the viewport.
- for (; newLine < m_TextComponent.textInfo.lineCount - 1; newLine++)
- {
- if (m_TextComponent.textInfo.lineInfo[newLine].baseline < m_TextComponent.textInfo.lineInfo[originLine].baseline - viewportHeight)
- break;
- }
- // Need to determine end line for next line.
- int endCharIdx = m_TextComponent.textInfo.lineInfo[newLine].lastCharacterIndex;
- int closest = -1;
- float distance = TMP_Math.FLOAT_MAX;
- float range = 0;
- for (int i = m_TextComponent.textInfo.lineInfo[newLine].firstCharacterIndex; i < endCharIdx; ++i)
- {
- TMP_CharacterInfo currentChar = m_TextComponent.textInfo.characterInfo[i];
- float d = originChar.origin - currentChar.origin;
- float r = d / (currentChar.xAdvance - currentChar.origin);
- if (r >= 0 && r <= 1)
- {
- if (r < 0.5f)
- return i;
- else
- return i + 1;
- }
- d = Mathf.Abs(d);
- if (d < distance)
- {
- closest = i;
- distance = d;
- range = r;
- }
- }
- if (closest == -1) return endCharIdx;
- if (range < 0.5f)
- return closest;
- else
- return closest + 1;
- }
- private void MoveDown(bool shift)
- {
- MoveDown(shift, true);
- }
- private void MoveDown(bool shift, bool goToLastChar)
- {
- if (hasSelection && !shift)
- {
- // If we have a selection and press down without shift,
- // set caret to end of selection before we move it down.
- caretPositionInternal = caretSelectPositionInternal = Mathf.Max(caretPositionInternal, caretSelectPositionInternal);
- }
- int position = multiLine ? LineDownCharacterPosition(caretSelectPositionInternal, goToLastChar) : m_TextComponent.textInfo.characterCount - 1; // text.Length;
- if (shift)
- {
- caretSelectPositionInternal = position;
- stringSelectPositionInternal = GetStringIndexFromCaretPosition(caretSelectPositionInternal);
- }
- else
- {
- caretSelectPositionInternal = caretPositionInternal = position;
- stringSelectPositionInternal = stringPositionInternal = GetStringIndexFromCaretPosition(caretSelectPositionInternal);
- }
- #if TMP_DEBUG_MODE
- Debug.Log("Caret Position: " + caretPositionInternal + " Selection Position: " + caretSelectPositionInternal + " String Position: " + stringPositionInternal + " String Select Position: " + stringSelectPositionInternal);
- #endif
- }
- private void MoveUp(bool shift)
- {
- MoveUp(shift, true);
- }
- private void MoveUp(bool shift, bool goToFirstChar)
- {
- if (hasSelection && !shift)
- {
- // If we have a selection and press up without shift,
- // set caret position to start of selection before we move it up.
- caretPositionInternal = caretSelectPositionInternal = Mathf.Min(caretPositionInternal, caretSelectPositionInternal);
- }
- int position = multiLine ? LineUpCharacterPosition(caretSelectPositionInternal, goToFirstChar) : 0;
- if (shift)
- {
- caretSelectPositionInternal = position;
- stringSelectPositionInternal = GetStringIndexFromCaretPosition(caretSelectPositionInternal);
- }
- else
- {
- caretSelectPositionInternal = caretPositionInternal = position;
- stringSelectPositionInternal = stringPositionInternal = GetStringIndexFromCaretPosition(caretSelectPositionInternal);
- }
- #if TMP_DEBUG_MODE
- Debug.Log("Caret Position: " + caretPositionInternal + " Selection Position: " + caretSelectPositionInternal + " String Position: " + stringPositionInternal + " String Select Position: " + stringSelectPositionInternal);
- #endif
- }
- private void MovePageUp(bool shift)
- {
- MovePageUp(shift, true);
- }
- private void MovePageUp(bool shift, bool goToFirstChar)
- {
- if (hasSelection && !shift)
- {
- // If we have a selection and press up without shift,
- // set caret position to start of selection before we move it up.
- caretPositionInternal = caretSelectPositionInternal = Mathf.Min(caretPositionInternal, caretSelectPositionInternal);
- }
- int position = multiLine ? PageUpCharacterPosition(caretSelectPositionInternal, goToFirstChar) : 0;
- if (shift)
- {
- caretSelectPositionInternal = position;
- stringSelectPositionInternal = GetStringIndexFromCaretPosition(caretSelectPositionInternal);
- }
- else
- {
- caretSelectPositionInternal = caretPositionInternal = position;
- stringSelectPositionInternal = stringPositionInternal = GetStringIndexFromCaretPosition(caretSelectPositionInternal);
- }
- // Scroll to top of viewport
- //int currentLine = m_TextComponent.textInfo.characterInfo[position].lineNumber;
- //float lineAscender = m_TextComponent.textInfo.lineInfo[currentLine].ascender;
- // Adjust text area up or down if not in single line mode.
- if (m_LineType != LineType.SingleLine)
- {
- float offset = m_TextViewport.rect.height; // m_TextViewport.rect.yMax - (m_TextComponent.rectTransform.anchoredPosition.y + lineAscender);
- float topTextBounds = m_TextComponent.rectTransform.position.y + m_TextComponent.textBounds.max.y;
- float topViewportBounds = m_TextViewport.position.y + m_TextViewport.rect.yMax;
- offset = topViewportBounds > topTextBounds + offset ? offset : topViewportBounds - topTextBounds;
- m_TextComponent.rectTransform.anchoredPosition += new Vector2(0, offset);
- AssignPositioningIfNeeded();
- m_IsScrollbarUpdateRequired = true;
- }
- #if TMP_DEBUG_MODE
- Debug.Log("Caret Position: " + caretPositionInternal + " Selection Position: " + caretSelectPositionInternal + " String Position: " + stringPositionInternal + " String Select Position: " + stringSelectPositionInternal);
- #endif
- }
- private void MovePageDown(bool shift)
- {
- MovePageDown(shift, true);
- }
- private void MovePageDown(bool shift, bool goToLastChar)
- {
- if (hasSelection && !shift)
- {
- // If we have a selection and press down without shift,
- // set caret to end of selection before we move it down.
- caretPositionInternal = caretSelectPositionInternal = Mathf.Max(caretPositionInternal, caretSelectPositionInternal);
- }
- int position = multiLine ? PageDownCharacterPosition(caretSelectPositionInternal, goToLastChar) : m_TextComponent.textInfo.characterCount - 1;
- if (shift)
- {
- caretSelectPositionInternal = position;
- stringSelectPositionInternal = GetStringIndexFromCaretPosition(caretSelectPositionInternal);
- }
- else
- {
- caretSelectPositionInternal = caretPositionInternal = position;
- stringSelectPositionInternal = stringPositionInternal = GetStringIndexFromCaretPosition(caretSelectPositionInternal);
- }
- // Scroll to top of viewport
- //int currentLine = m_TextComponent.textInfo.characterInfo[position].lineNumber;
- //float lineAscender = m_TextComponent.textInfo.lineInfo[currentLine].ascender;
- // Adjust text area up or down if not in single line mode.
- if (m_LineType != LineType.SingleLine)
- {
- float offset = m_TextViewport.rect.height; // m_TextViewport.rect.yMax - (m_TextComponent.rectTransform.anchoredPosition.y + lineAscender);
- float bottomTextBounds = m_TextComponent.rectTransform.position.y + m_TextComponent.textBounds.min.y;
- float bottomViewportBounds = m_TextViewport.position.y + m_TextViewport.rect.yMin;
- offset = bottomViewportBounds > bottomTextBounds + offset ? offset : bottomViewportBounds - bottomTextBounds;
- m_TextComponent.rectTransform.anchoredPosition += new Vector2(0, offset);
- AssignPositioningIfNeeded();
- m_IsScrollbarUpdateRequired = true;
- }
- #if TMP_DEBUG_MODE
- Debug.Log("Caret Position: " + caretPositionInternal + " Selection Position: " + caretSelectPositionInternal + " String Position: " + stringPositionInternal + " String Select Position: " + stringSelectPositionInternal);
- #endif
- }
- private void Delete()
- {
- if (m_ReadOnly)
- return;
- if (m_StringPosition == m_StringSelectPosition)
- return;
- if (m_isRichTextEditingAllowed || m_isSelectAll)
- {
- // Handling of Delete when Rich Text is allowed.
- if (m_StringPosition < m_StringSelectPosition)
- {
- m_Text = text.Remove(m_StringPosition, m_StringSelectPosition - m_StringPosition);
- m_StringSelectPosition = m_StringPosition;
- }
- else
- {
- m_Text = text.Remove(m_StringSelectPosition, m_StringPosition - m_StringSelectPosition);
- m_StringPosition = m_StringSelectPosition;
- }
- m_isSelectAll = false;
- }
- else
- {
- if (m_CaretPosition < m_CaretSelectPosition)
- {
- m_StringPosition = m_TextComponent.textInfo.characterInfo[m_CaretPosition].index;
- m_StringSelectPosition = m_TextComponent.textInfo.characterInfo[m_CaretSelectPosition - 1].index + m_TextComponent.textInfo.characterInfo[m_CaretSelectPosition - 1].stringLength;
- m_Text = text.Remove(m_StringPosition, m_StringSelectPosition - m_StringPosition);
- m_StringSelectPosition = m_StringPosition;
- m_CaretSelectPosition = m_CaretPosition;
- }
- else
- {
- m_StringPosition = m_TextComponent.textInfo.characterInfo[m_CaretPosition - 1].index + m_TextComponent.textInfo.characterInfo[m_CaretPosition - 1].stringLength;
- m_StringSelectPosition = m_TextComponent.textInfo.characterInfo[m_CaretSelectPosition].index;
- m_Text = text.Remove(m_StringSelectPosition, m_StringPosition - m_StringSelectPosition);
- m_StringPosition = m_StringSelectPosition;
- m_CaretPosition = m_CaretSelectPosition;
- }
- }
- #if TMP_DEBUG_MODE
- Debug.Log("Caret Position: " + caretPositionInternal + " Selection Position: " + caretSelectPositionInternal + " String Position: " + stringPositionInternal + " String Select Position: " + stringSelectPositionInternal);
- #endif
- }
- /// <summary>
- /// Handling of DEL key
- /// </summary>
- private void DeleteKey()
- {
- if (m_ReadOnly)
- return;
- if (hasSelection)
- {
- m_isLastKeyBackspace = true;
- Delete();
- UpdateTouchKeyboardFromEditChanges();
- SendOnValueChangedAndUpdateLabel();
- }
- else
- {
- if (m_isRichTextEditingAllowed)
- {
- if (stringPositionInternal < text.Length)
- {
- // Special handling for Surrogate Pairs
- if (char.IsHighSurrogate(text[stringPositionInternal]))
- m_Text = text.Remove(stringPositionInternal, 2);
- else
- m_Text = text.Remove(stringPositionInternal, 1);
- m_isLastKeyBackspace = true;
- UpdateTouchKeyboardFromEditChanges();
- SendOnValueChangedAndUpdateLabel();
- }
- }
- else
- {
- if (caretPositionInternal < m_TextComponent.textInfo.characterCount - 1)
- {
- int numberOfCharactersToRemove = m_TextComponent.textInfo.characterInfo[caretPositionInternal].stringLength;
- // Adjust string position to skip any potential rich text tags.
- int nextCharacterStringPosition = m_TextComponent.textInfo.characterInfo[caretPositionInternal].index;
- m_Text = text.Remove(nextCharacterStringPosition, numberOfCharactersToRemove);
- m_isLastKeyBackspace = true;
- SendOnValueChangedAndUpdateLabel();
- }
- }
- }
- #if TMP_DEBUG_MODE
- Debug.Log("Caret Position: " + caretPositionInternal + " Selection Position: " + caretSelectPositionInternal + " String Position: " + stringPositionInternal + " String Select Position: " + stringSelectPositionInternal);
- #endif
- }
- /// <summary>
- /// Handling of Backspace key
- /// </summary>
- private void Backspace()
- {
- if (m_ReadOnly)
- return;
- if (hasSelection)
- {
- m_isLastKeyBackspace = true;
- Delete();
- UpdateTouchKeyboardFromEditChanges();
- SendOnValueChangedAndUpdateLabel();
- }
- else
- {
- if (m_isRichTextEditingAllowed)
- {
- if (stringPositionInternal > 0)
- {
- int numberOfCharactersToRemove = 1;
- // Special handling for Surrogate pairs and Diacritical marks
- if (char.IsLowSurrogate(text[stringPositionInternal - 1]))
- numberOfCharactersToRemove = 2;
- stringSelectPositionInternal = stringPositionInternal = stringPositionInternal - numberOfCharactersToRemove;
- m_Text = text.Remove(stringPositionInternal, numberOfCharactersToRemove);
- caretSelectPositionInternal = caretPositionInternal = caretPositionInternal - 1;
- m_isLastKeyBackspace = true;
- UpdateTouchKeyboardFromEditChanges();
- SendOnValueChangedAndUpdateLabel();
- }
- }
- else
- {
- if (caretPositionInternal > 0)
- {
- int numberOfCharactersToRemove = m_TextComponent.textInfo.characterInfo[caretPositionInternal - 1].stringLength;
- // Delete the previous character
- m_Text = text.Remove(m_TextComponent.textInfo.characterInfo[caretPositionInternal - 1].index, numberOfCharactersToRemove);
- // Get new adjusted string position
- stringSelectPositionInternal = stringPositionInternal = caretPositionInternal < 2
- ? m_TextComponent.textInfo.characterInfo[0].index
- : m_TextComponent.textInfo.characterInfo[caretPositionInternal - 2].index + m_TextComponent.textInfo.characterInfo[caretPositionInternal - 2].stringLength;
- caretSelectPositionInternal = caretPositionInternal = caretPositionInternal - 1;
- }
- m_isLastKeyBackspace = true;
- UpdateTouchKeyboardFromEditChanges();
- SendOnValueChangedAndUpdateLabel();
- }
- }
- #if TMP_DEBUG_MODE
- Debug.Log("Caret Position: " + caretPositionInternal + " Selection Position: " + caretSelectPositionInternal + " String Position: " + stringPositionInternal + " String Select Position: " + stringSelectPositionInternal);
- #endif
- }
- /// <summary>
- /// Append the specified text to the end of the current.
- /// </summary>
- protected virtual void Append(string input)
- {
- if (m_ReadOnly)
- return;
- if (InPlaceEditing() == false)
- return;
- for (int i = 0, imax = input.Length; i < imax; ++i)
- {
- char c = input[i];
- if (c >= ' ' || c == '\t' || c == '\r' || c == 10 || c == '\n')
- {
- Append(c);
- }
- }
- }
- protected virtual void Append(char input)
- {
- if (m_ReadOnly)
- return;
- if (InPlaceEditing() == false)
- return;
- // If we have an input validator, validate the input first
- if (onValidateInput != null)
- input = onValidateInput(text, stringPositionInternal, input);
- else if (characterValidation == CharacterValidation.CustomValidator)
- {
- input = Validate(text, stringPositionInternal, input);
- if (input == 0) return;
- SendOnValueChanged();
- UpdateLabel();
- return;
- }
- else if (characterValidation != CharacterValidation.None)
- input = Validate(text, stringPositionInternal, input);
- // If the input is invalid, skip it
- if (input == 0)
- return;
- // Append the character and update the label
- Insert(input);
- }
- // Insert the character and update the label.
- private void Insert(char c)
- {
- if (m_ReadOnly)
- return;
- //Debug.Log("Inserting character " + m_IsCompositionActive);
- string replaceString = c.ToString();
- Delete();
- // Can't go past the character limit
- if (characterLimit > 0 && text.Length >= characterLimit)
- return;
- m_Text = text.Insert(m_StringPosition, replaceString);
- if (!char.IsHighSurrogate(c))
- m_CaretSelectPosition = m_CaretPosition += 1;
- m_StringSelectPosition = m_StringPosition += 1;
- UpdateTouchKeyboardFromEditChanges();
- SendOnValueChanged();
- #if TMP_DEBUG_MODE
- Debug.Log("Caret Position: " + caretPositionInternal + " Selection Position: " + caretSelectPositionInternal + " String Position: " + stringPositionInternal + " String Select Position: " + stringSelectPositionInternal);
- #endif
- }
- private void UpdateTouchKeyboardFromEditChanges()
- {
- // Update the TouchKeyboard's text from edit changes
- // if in-place editing is allowed
- if (m_SoftKeyboard != null && InPlaceEditing())
- {
- m_SoftKeyboard.text = m_Text;
- }
- }
- private void SendOnValueChangedAndUpdateLabel()
- {
- UpdateLabel();
- SendOnValueChanged();
- }
- private void SendOnValueChanged()
- {
- if (onValueChanged != null)
- onValueChanged.Invoke(text);
- }
- /// <summary>
- /// Submit the input field's text.
- /// </summary>
- protected void SendOnEndEdit()
- {
- if (onEndEdit != null)
- onEndEdit.Invoke(m_Text);
- }
- protected void SendOnSubmit()
- {
- if (onSubmit != null)
- onSubmit.Invoke(m_Text);
- }
- protected void SendOnFocus()
- {
- if (onSelect != null)
- onSelect.Invoke(m_Text);
- }
- protected void SendOnFocusLost()
- {
- if (onDeselect != null)
- onDeselect.Invoke(m_Text);
- }
- protected void SendOnTextSelection()
- {
- m_isSelected = true;
- if (onTextSelection != null)
- onTextSelection.Invoke(m_Text, stringPositionInternal, stringSelectPositionInternal);
- }
- protected void SendOnEndTextSelection()
- {
- if (!m_isSelected) return;
- if (onEndTextSelection != null)
- onEndTextSelection.Invoke(m_Text, stringPositionInternal, stringSelectPositionInternal);
- m_isSelected = false;
- }
- protected void SendTouchScreenKeyboardStatusChanged()
- {
- if (onTouchScreenKeyboardStatusChanged != null)
- onTouchScreenKeyboardStatusChanged.Invoke(m_SoftKeyboard.status);
- }
- /// <summary>
- /// Update the visual text Text.
- /// </summary>
- protected void UpdateLabel()
- {
- if (m_TextComponent != null && m_TextComponent.font != null && m_PreventCallback == false)
- {
- // Prevent callback from the text component as we assign new text. This is to prevent a recursive call.
- m_PreventCallback = true;
- string fullText;
- if (compositionLength > 0 && m_ReadOnly == false)
- {
- //Input.imeCompositionMode = IMECompositionMode.On;
- // Handle selections
- Delete();
- if (m_RichText)
- fullText = text.Substring(0, m_StringPosition) + "<u>" + compositionString + "</u>" + text.Substring(m_StringPosition);
- else
- fullText = text.Substring(0, m_StringPosition) + compositionString + text.Substring(m_StringPosition);
- m_IsCompositionActive = true;
- //Debug.Log("[" + Time.frameCount + "] Handling IME Input");
- }
- else
- {
- fullText = text;
- m_IsCompositionActive = false;
- m_ShouldUpdateIMEWindowPosition = true;
- }
- //Debug.Log("Handling IME Input... [" + compositionString + "] of length [" + compositionLength + "] at StringPosition [" + m_StringPosition + "] IsActive [" + m_IsCompositionActive + "]");
- string processed;
- if (inputType == InputType.Password)
- processed = new string(asteriskChar, fullText.Length);
- else
- processed = fullText;
- bool isEmpty = string.IsNullOrEmpty(fullText);
- if (m_Placeholder != null)
- m_Placeholder.enabled = isEmpty;
- if (!isEmpty && m_ReadOnly == false)
- {
- SetCaretVisible();
- }
- m_TextComponent.text = processed + "\u200B"; // Extra space is added for Caret tracking.
- // Rebuild layout if using Layout components.
- if (m_IsDrivenByLayoutComponents)
- LayoutRebuilder.MarkLayoutForRebuild(m_RectTransform);
- // Special handling to limit the number of lines of text in the Input Field.
- if (m_LineLimit > 0)
- {
- m_TextComponent.ForceMeshUpdate();
- // Check if text exceeds maximum number of lines.
- if (m_TextComponent.textInfo.lineCount > m_LineLimit)
- {
- int lastValidCharacterIndex = m_TextComponent.textInfo.lineInfo[m_LineLimit - 1].lastCharacterIndex;
- int characterStringIndex = m_TextComponent.textInfo.characterInfo[lastValidCharacterIndex].index + m_TextComponent.textInfo.characterInfo[lastValidCharacterIndex].stringLength;
- text = processed.Remove(characterStringIndex, processed.Length - characterStringIndex);
- m_TextComponent.text = text + "\u200B";
- }
- }
- if (m_IsTextComponentUpdateRequired)
- {
- m_IsTextComponentUpdateRequired = false;
- m_TextComponent.ForceMeshUpdate();
- }
- MarkGeometryAsDirty();
- // Scrollbar should be updated.
- m_IsScrollbarUpdateRequired = true;
- m_PreventCallback = false;
- }
- }
- void UpdateScrollbar()
- {
- // Update Scrollbar
- if (m_VerticalScrollbar)
- {
- float size = m_TextViewport.rect.height / m_TextComponent.preferredHeight;
- m_IsUpdatingScrollbarValues = true;
- m_VerticalScrollbar.size = size;
- m_ScrollPosition = m_VerticalScrollbar.value = m_TextComponent.rectTransform.anchoredPosition.y / (m_TextComponent.preferredHeight - m_TextViewport.rect.height);
- //m_VerticalScrollbar.numberOfSteps = (int)(m_TextComponent.textInfo.lineCount / 0.25f); // Replace by scroll sensitivity.
- //Debug.Log("Updating Scrollbar... Value: " + m_VerticalScrollbar.value);
- }
- }
- /// <summary>
- /// Function to update the vertical position of the text container when OnValueChanged event is received from the Scrollbar.
- /// </summary>
- /// <param name="value"></param>
- void OnScrollbarValueChange(float value)
- {
- if (m_IsUpdatingScrollbarValues) { m_IsUpdatingScrollbarValues = false; return; }
- if (value < 0 || value > 1) return;
- AdjustTextPositionRelativeToViewport(value);
- m_ScrollPosition = value;
- //Debug.Log("Scrollbar value is: " + value + " Transform POS: " + m_TextComponent.rectTransform.anchoredPosition);
- }
- void UpdateMaskRegions()
- {
- // TODO: Figure out a better way to handle adding an offset to the masking region
- // This region is defined by the RectTransform of the GameObject that contains the RectMask2D component.
- /*
- // Update Masking Region
- if (m_TextViewportRectMask != null)
- {
- Rect viewportRect = m_TextViewportRectMask.canvasRect;
- if (viewportRect != m_CachedViewportRect)
- {
- m_CachedViewportRect = viewportRect;
- viewportRect.min -= m_TextViewport.offsetMin * 0.5f;
- viewportRect.max -= m_TextViewport.offsetMax * 0.5f;
- if (m_CachedInputRenderer != null)
- m_CachedInputRenderer.EnableRectClipping(viewportRect);
- if (m_TextComponent.canvasRenderer != null)
- m_TextComponent.canvasRenderer.EnableRectClipping(viewportRect);
- if (m_Placeholder != null && m_Placeholder.enabled)
- m_Placeholder.canvasRenderer.EnableRectClipping(viewportRect);
- }
- }
- */
- }
- /// <summary>
- /// Adjusts the relative position of the body of the text relative to the viewport.
- /// </summary>
- /// <param name="relativePosition"></param>
- void AdjustTextPositionRelativeToViewport (float relativePosition)
- {
- if (m_TextViewport == null)
- return;
- TMP_TextInfo textInfo = m_TextComponent.textInfo;
- // Check to make sure we have valid data and lines to query.
- if (textInfo == null || textInfo.lineInfo == null || textInfo.lineCount == 0 || textInfo.lineCount > textInfo.lineInfo.Length) return;
- m_TextComponent.rectTransform.anchoredPosition = new Vector2(m_TextComponent.rectTransform.anchoredPosition.x, (m_TextComponent.preferredHeight - m_TextViewport.rect.height) * relativePosition);
- AssignPositioningIfNeeded();
- //Debug.Log("Text height: " + m_TextComponent.preferredHeight + " Viewport height: " + m_TextViewport.rect.height + " Adjusted RectTransform anchordedPosition:" + m_TextComponent.rectTransform.anchoredPosition + " Text Bounds: " + m_TextComponent.bounds.ToString("f3"));
- }
- private int GetCaretPositionFromStringIndex(int stringIndex)
- {
- int count = m_TextComponent.textInfo.characterCount;
- for (int i = 0; i < count; i++)
- {
- if (m_TextComponent.textInfo.characterInfo[i].index >= stringIndex)
- return i;
- }
- return count;
- }
- /// <summary>
- /// Returns / places the caret before the given character at the string index.
- /// </summary>
- /// <param name="stringIndex"></param>
- /// <returns></returns>
- private int GetMinCaretPositionFromStringIndex(int stringIndex)
- {
- int count = m_TextComponent.textInfo.characterCount;
- for (int i = 0; i < count; i++)
- {
- if (stringIndex < m_TextComponent.textInfo.characterInfo[i].index + m_TextComponent.textInfo.characterInfo[i].stringLength)
- return i;
- }
- return count;
- }
- /// <summary>
- /// Returns / places the caret after the given character at the string index.
- /// </summary>
- /// <param name="stringIndex"></param>
- /// <returns></returns>
- private int GetMaxCaretPositionFromStringIndex(int stringIndex)
- {
- int count = m_TextComponent.textInfo.characterCount;
- for (int i = 0; i < count; i++)
- {
- if (m_TextComponent.textInfo.characterInfo[i].index >= stringIndex)
- return i;
- }
- return count;
- }
- private int GetStringIndexFromCaretPosition(int caretPosition)
- {
- // Clamp values between 0 and character count.
- ClampCaretPos(ref caretPosition);
- return m_TextComponent.textInfo.characterInfo[caretPosition].index;
- }
- public void ForceLabelUpdate()
- {
- UpdateLabel();
- }
- private void MarkGeometryAsDirty()
- {
- #if UNITY_EDITOR
- if (!Application.isPlaying || UnityEditor.PrefabUtility.IsPartOfPrefabAsset(this))
- return;
- #endif
- CanvasUpdateRegistry.RegisterCanvasElementForGraphicRebuild(this);
- }
- public virtual void Rebuild(CanvasUpdate update)
- {
- switch (update)
- {
- case CanvasUpdate.LatePreRender:
- UpdateGeometry();
- break;
- }
- }
- public virtual void LayoutComplete()
- { }
- public virtual void GraphicUpdateComplete()
- { }
- private void UpdateGeometry()
- {
- #if UNITY_EDITOR
- if (!Application.isPlaying)
- return;
- #endif
- // No need to draw a cursor on mobile as its handled by the devices keyboard.
- if (InPlaceEditing() == false)
- return;
- if (m_CachedInputRenderer == null)
- return;
- OnFillVBO(mesh);
- m_CachedInputRenderer.SetMesh(mesh);
- }
- /// <summary>
- /// Method to keep the Caret RectTransform properties in sync with the text object's RectTransform
- /// </summary>
- private void AssignPositioningIfNeeded()
- {
- if (m_TextComponent != null && caretRectTrans != null &&
- (caretRectTrans.localPosition != m_TextComponent.rectTransform.localPosition ||
- caretRectTrans.localRotation != m_TextComponent.rectTransform.localRotation ||
- caretRectTrans.localScale != m_TextComponent.rectTransform.localScale ||
- caretRectTrans.anchorMin != m_TextComponent.rectTransform.anchorMin ||
- caretRectTrans.anchorMax != m_TextComponent.rectTransform.anchorMax ||
- caretRectTrans.anchoredPosition != m_TextComponent.rectTransform.anchoredPosition ||
- caretRectTrans.sizeDelta != m_TextComponent.rectTransform.sizeDelta ||
- caretRectTrans.pivot != m_TextComponent.rectTransform.pivot))
- {
- caretRectTrans.localPosition = m_TextComponent.rectTransform.localPosition;
- caretRectTrans.localRotation = m_TextComponent.rectTransform.localRotation;
- caretRectTrans.localScale = m_TextComponent.rectTransform.localScale;
- caretRectTrans.anchorMin = m_TextComponent.rectTransform.anchorMin;
- caretRectTrans.anchorMax = m_TextComponent.rectTransform.anchorMax;
- caretRectTrans.anchoredPosition = m_TextComponent.rectTransform.anchoredPosition;
- caretRectTrans.sizeDelta = m_TextComponent.rectTransform.sizeDelta;
- caretRectTrans.pivot = m_TextComponent.rectTransform.pivot;
- }
- }
- private void OnFillVBO(Mesh vbo)
- {
- using (var helper = new VertexHelper())
- {
- if (!isFocused && !m_SelectionStillActive)
- {
- helper.FillMesh(vbo);
- return;
- }
- if (m_IsStringPositionDirty)
- {
- stringPositionInternal = GetStringIndexFromCaretPosition(m_CaretPosition);
- stringSelectPositionInternal = GetStringIndexFromCaretPosition(m_CaretSelectPosition);
- m_IsStringPositionDirty = false;
- }
- if (m_IsCaretPositionDirty)
- {
- caretPositionInternal = GetCaretPositionFromStringIndex(stringPositionInternal);
- caretSelectPositionInternal = GetCaretPositionFromStringIndex(stringSelectPositionInternal);
- m_IsCaretPositionDirty = false;
- }
- if (!hasSelection && !m_ReadOnly)
- {
- GenerateCaret(helper, Vector2.zero);
- SendOnEndTextSelection();
- }
- else
- {
- GenerateHightlight(helper, Vector2.zero);
- SendOnTextSelection();
- }
- helper.FillMesh(vbo);
- }
- }
- private void GenerateCaret(VertexHelper vbo, Vector2 roundingOffset)
- {
- if (m_CaretVisible == false)
- return;
- if (m_CursorVerts == null)
- {
- CreateCursorVerts();
- }
- float width = m_CaretWidth;
- // TODO: Optimize to only update the caret position when needed.
- Vector2 startPosition = Vector2.zero;
- float height = 0;
- TMP_CharacterInfo currentCharacter;
- // Make sure caret position does not exceed characterInfo array size.
- if (caretPositionInternal >= m_TextComponent.textInfo.characterInfo.Length)
- return;
- int currentLine = m_TextComponent.textInfo.characterInfo[caretPositionInternal].lineNumber;
- // Caret is positioned at the origin for the first character of each lines and at the advance for subsequent characters.
- if (caretPositionInternal == m_TextComponent.textInfo.lineInfo[currentLine].firstCharacterIndex)
- {
- currentCharacter = m_TextComponent.textInfo.characterInfo[caretPositionInternal];
- height = currentCharacter.ascender - currentCharacter.descender;
- if (m_TextComponent.verticalAlignment == VerticalAlignmentOptions.Geometry)
- startPosition = new Vector2(currentCharacter.origin, 0 - height / 2);
- else
- startPosition = new Vector2(currentCharacter.origin, currentCharacter.descender);
- }
- else
- {
- currentCharacter = m_TextComponent.textInfo.characterInfo[caretPositionInternal - 1];
- height = currentCharacter.ascender - currentCharacter.descender;
- if (m_TextComponent.verticalAlignment == VerticalAlignmentOptions.Geometry)
- startPosition = new Vector2(currentCharacter.xAdvance, 0 - height / 2);
- else
- startPosition = new Vector2(currentCharacter.xAdvance, currentCharacter.descender);
- }
- if (m_SoftKeyboard != null)
- {
- int selectionStart = m_StringPosition;
- int softKeyboardStringLength = m_SoftKeyboard.text == null ? 0 : m_SoftKeyboard.text.Length;
- if (selectionStart < 0)
- selectionStart = 0;
- if (selectionStart > softKeyboardStringLength)
- selectionStart = softKeyboardStringLength;
- m_SoftKeyboard.selection = new RangeInt(selectionStart, 0);
- }
- // Adjust the position of the RectTransform based on the caret position in the viewport (only if we have focus).
- if (isFocused && startPosition != m_LastPosition || m_forceRectTransformAdjustment || m_isLastKeyBackspace)
- AdjustRectTransformRelativeToViewport(startPosition, height, currentCharacter.isVisible);
- m_LastPosition = startPosition;
- // Clamp Caret height
- float top = startPosition.y + height;
- float bottom = top - height;
- // Minor tweak to address caret potentially being too thin based on canvas scaler values.
- float scale = m_TextComponent.canvas.scaleFactor;
- m_CursorVerts[0].position = new Vector3(startPosition.x, bottom, 0.0f);
- m_CursorVerts[1].position = new Vector3(startPosition.x, top, 0.0f);
- m_CursorVerts[2].position = new Vector3(startPosition.x + width, top, 0.0f);
- m_CursorVerts[3].position = new Vector3(startPosition.x + width, bottom, 0.0f);
- // Set Vertex Color for the caret color.
- m_CursorVerts[0].color = caretColor;
- m_CursorVerts[1].color = caretColor;
- m_CursorVerts[2].color = caretColor;
- m_CursorVerts[3].color = caretColor;
- vbo.AddUIVertexQuad(m_CursorVerts);
- // Update position of IME window when necessary.
- if (m_ShouldUpdateIMEWindowPosition || currentLine != m_PreviousIMEInsertionLine)
- {
- m_ShouldUpdateIMEWindowPosition = false;
- m_PreviousIMEInsertionLine = currentLine;
- // Calculate position of IME Window in screen space.
- Camera cameraRef;
- if (m_TextComponent.canvas.renderMode == RenderMode.ScreenSpaceOverlay)
- cameraRef = null;
- else
- cameraRef = m_TextComponent.canvas.worldCamera;
- Vector3 cursorPosition = m_CachedInputRenderer.gameObject.transform.TransformPoint(m_CursorVerts[0].position);
- Vector2 screenPosition = RectTransformUtility.WorldToScreenPoint(cameraRef, cursorPosition);
- screenPosition.y = Screen.height - screenPosition.y;
- if (inputSystem != null)
- inputSystem.compositionCursorPos = screenPosition;
- //Debug.Log("[" + Time.frameCount + "] Updating IME Window position(" + screenPosition + ") with Composition Length: " + compositionLength);
- }
- //#if TMP_DEBUG_MODE
- //Debug.Log("Caret position updated at frame: " + Time.frameCount);
- //#endif
- }
- private void CreateCursorVerts()
- {
- m_CursorVerts = new UIVertex[4];
- for (int i = 0; i < m_CursorVerts.Length; i++)
- {
- m_CursorVerts[i] = UIVertex.simpleVert;
- m_CursorVerts[i].uv0 = Vector2.zero;
- }
- }
- private void GenerateHightlight(VertexHelper vbo, Vector2 roundingOffset)
- {
- // Update Masking Region
- UpdateMaskRegions();
- // Make sure caret position does not exceed characterInfo array size.
- //if (caretSelectPositionInternal >= m_TextComponent.textInfo.characterInfo.Length)
- // return;
- TMP_TextInfo textInfo = m_TextComponent.textInfo;
- m_CaretPosition = GetCaretPositionFromStringIndex(stringPositionInternal);
- m_CaretSelectPosition = GetCaretPositionFromStringIndex(stringSelectPositionInternal);
- if (m_SoftKeyboard != null)
- {
- int stringPosition = m_CaretPosition < m_CaretSelectPosition ? textInfo.characterInfo[m_CaretPosition].index : textInfo.characterInfo[m_CaretSelectPosition].index;
- int length = m_CaretPosition < m_CaretSelectPosition ? stringSelectPositionInternal - stringPosition : stringPositionInternal - stringPosition;
- m_SoftKeyboard.selection = new RangeInt(stringPosition, length);
- }
- // Adjust text RectTranform position to make sure it is visible in viewport.
- Vector2 caretPosition;
- float height = 0;
- if (m_CaretSelectPosition < textInfo.characterCount)
- {
- caretPosition = new Vector2(textInfo.characterInfo[m_CaretSelectPosition].origin, textInfo.characterInfo[m_CaretSelectPosition].descender);
- height = textInfo.characterInfo[m_CaretSelectPosition].ascender - textInfo.characterInfo[m_CaretSelectPosition].descender;
- }
- else
- {
- caretPosition = new Vector2(textInfo.characterInfo[m_CaretSelectPosition - 1].xAdvance, textInfo.characterInfo[m_CaretSelectPosition - 1].descender);
- height = textInfo.characterInfo[m_CaretSelectPosition - 1].ascender - textInfo.characterInfo[m_CaretSelectPosition - 1].descender;
- }
- // TODO: Don't adjust the position of the RectTransform if Reset On Deactivation is disabled
- // and we just selected the Input Field again.
- AdjustRectTransformRelativeToViewport(caretPosition, height, true);
- int startChar = Mathf.Max(0, m_CaretPosition);
- int endChar = Mathf.Max(0, m_CaretSelectPosition);
- // Ensure pos is always less then selPos to make the code simpler
- if (startChar > endChar)
- {
- int temp = startChar;
- startChar = endChar;
- endChar = temp;
- }
- endChar -= 1;
- //Debug.Log("Updating Highlight... Caret Position: " + startChar + " Caret Select POS: " + endChar);
- int currentLineIndex = textInfo.characterInfo[startChar].lineNumber;
- int nextLineStartIdx = textInfo.lineInfo[currentLineIndex].lastCharacterIndex;
- UIVertex vert = UIVertex.simpleVert;
- vert.uv0 = Vector2.zero;
- vert.color = selectionColor;
- int currentChar = startChar;
- while (currentChar <= endChar && currentChar < textInfo.characterCount)
- {
- if (currentChar == nextLineStartIdx || currentChar == endChar)
- {
- TMP_CharacterInfo startCharInfo = textInfo.characterInfo[startChar];
- TMP_CharacterInfo endCharInfo = textInfo.characterInfo[currentChar];
- // Extra check to handle Carriage Return
- if (currentChar > 0 && endCharInfo.character == 10 && textInfo.characterInfo[currentChar - 1].character == 13)
- endCharInfo = textInfo.characterInfo[currentChar - 1];
- Vector2 startPosition = new Vector2(startCharInfo.origin, textInfo.lineInfo[currentLineIndex].ascender);
- Vector2 endPosition = new Vector2(endCharInfo.xAdvance, textInfo.lineInfo[currentLineIndex].descender);
- var startIndex = vbo.currentVertCount;
- vert.position = new Vector3(startPosition.x, endPosition.y, 0.0f);
- vbo.AddVert(vert);
- vert.position = new Vector3(endPosition.x, endPosition.y, 0.0f);
- vbo.AddVert(vert);
- vert.position = new Vector3(endPosition.x, startPosition.y, 0.0f);
- vbo.AddVert(vert);
- vert.position = new Vector3(startPosition.x, startPosition.y, 0.0f);
- vbo.AddVert(vert);
- vbo.AddTriangle(startIndex, startIndex + 1, startIndex + 2);
- vbo.AddTriangle(startIndex + 2, startIndex + 3, startIndex + 0);
- startChar = currentChar + 1;
- currentLineIndex++;
- if (currentLineIndex < textInfo.lineCount)
- nextLineStartIdx = textInfo.lineInfo[currentLineIndex].lastCharacterIndex;
- }
- currentChar++;
- }
- // Scrollbar should be updated.
- m_IsScrollbarUpdateRequired = true;
- //#if TMP_DEBUG_MODE
- // Debug.Log("Text selection updated at frame: " + Time.frameCount);
- //#endif
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="startPosition"></param>
- /// <param name="height"></param>
- /// <param name="isCharVisible"></param>
- private void AdjustRectTransformRelativeToViewport(Vector2 startPosition, float height, bool isCharVisible)
- {
- //Debug.Log("Adjusting transform position relative to viewport.");
- if (m_TextViewport == null)
- return;
- Vector3 localPosition = transform.localPosition;
- Vector3 textComponentLocalPosition = m_TextComponent.rectTransform.localPosition;
- Vector3 textViewportLocalPosition = m_TextViewport.localPosition;
- Rect textViewportRect = m_TextViewport.rect;
- Vector2 caretPosition = new Vector2(startPosition.x + textComponentLocalPosition.x + textViewportLocalPosition.x + localPosition.x, startPosition.y + textComponentLocalPosition.y + textViewportLocalPosition.y + localPosition.y);
- Rect viewportWSRect = new Rect(localPosition.x + textViewportLocalPosition.x + textViewportRect.x, localPosition.y + textViewportLocalPosition.y + textViewportRect.y, textViewportRect.width, textViewportRect.height);
- // Adjust the position of the RectTransform based on the caret position in the viewport.
- float rightOffset = viewportWSRect.xMax - (caretPosition.x + m_TextComponent.margin.z + m_CaretWidth);
- if (rightOffset < 0f)
- {
- if (!multiLine || (multiLine && isCharVisible))
- {
- //Debug.Log("Shifting text to the LEFT by " + rightOffset.ToString("f3"));
- m_TextComponent.rectTransform.anchoredPosition += new Vector2(rightOffset, 0);
- AssignPositioningIfNeeded();
- }
- }
- float leftOffset = (caretPosition.x - m_TextComponent.margin.x) - viewportWSRect.xMin;
- if (leftOffset < 0f)
- {
- //Debug.Log("Shifting text to the RIGHT by " + leftOffset.ToString("f3"));
- m_TextComponent.rectTransform.anchoredPosition += new Vector2(-leftOffset, 0);
- AssignPositioningIfNeeded();
- }
- // Adjust text area up or down if not in single line mode.
- if (m_LineType != LineType.SingleLine)
- {
- float topOffset = viewportWSRect.yMax - (caretPosition.y + height);
- if (topOffset < -0.0001f)
- {
- //Debug.Log("Shifting text to Up " + topOffset.ToString("f3"));
- m_TextComponent.rectTransform.anchoredPosition += new Vector2(0, topOffset);
- AssignPositioningIfNeeded();
- m_IsScrollbarUpdateRequired = true;
- }
- float bottomOffset = caretPosition.y - viewportWSRect.yMin;
- if (bottomOffset < 0f)
- {
- //Debug.Log("Shifting text to Down " + bottomOffset.ToString("f3"));
- m_TextComponent.rectTransform.anchoredPosition -= new Vector2(0, bottomOffset);
- AssignPositioningIfNeeded();
- m_IsScrollbarUpdateRequired = true;
- }
- }
- // Special handling of backspace
- if (m_isLastKeyBackspace)
- {
- float anchoredPositionX = m_TextComponent.rectTransform.anchoredPosition.x;
- float firstCharPosition = localPosition.x + textViewportLocalPosition.x + textComponentLocalPosition.x + m_TextComponent.textInfo.characterInfo[0].origin - m_TextComponent.margin.x;
- float lastCharPosition = localPosition.x + textViewportLocalPosition.x + textComponentLocalPosition.x + m_TextComponent.textInfo.characterInfo[m_TextComponent.textInfo.characterCount - 1].origin + m_TextComponent.margin.z + m_CaretWidth;
- if (anchoredPositionX > 0.0001f && firstCharPosition > viewportWSRect.xMin)
- {
- float offset = viewportWSRect.xMin - firstCharPosition;
- if (anchoredPositionX < -offset)
- offset = -anchoredPositionX;
- m_TextComponent.rectTransform.anchoredPosition += new Vector2(offset, 0);
- AssignPositioningIfNeeded();
- }
- else if (anchoredPositionX < -0.0001f && lastCharPosition < viewportWSRect.xMax)
- {
- float offset = viewportWSRect.xMax - lastCharPosition;
- if (-anchoredPositionX < offset)
- offset = -anchoredPositionX;
- m_TextComponent.rectTransform.anchoredPosition += new Vector2(offset, 0);
- AssignPositioningIfNeeded();
- }
- m_isLastKeyBackspace = false;
- }
- m_forceRectTransformAdjustment = false;
- }
- /// <summary>
- /// Validate the specified input.
- /// </summary>
- protected char Validate(string text, int pos, char ch)
- {
- // Validation is disabled
- if (characterValidation == CharacterValidation.None || !enabled)
- return ch;
- if (characterValidation == CharacterValidation.Integer || characterValidation == CharacterValidation.Decimal)
- {
- // Integer and decimal
- bool cursorBeforeDash = (pos == 0 && text.Length > 0 && text[0] == '-');
- bool selectionAtStart = stringPositionInternal == 0 || stringSelectPositionInternal == 0;
- if (!cursorBeforeDash)
- {
- if (ch >= '0' && ch <= '9') return ch;
- if (ch == '-' && (pos == 0 || selectionAtStart)) return ch;
- var separator = Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator;
- if (ch == Convert.ToChar(separator) && characterValidation == CharacterValidation.Decimal && !text.Contains(separator)) return ch;
- }
- }
- else if (characterValidation == CharacterValidation.Digit)
- {
- if (ch >= '0' && ch <= '9') return ch;
- }
- else if (characterValidation == CharacterValidation.Alphanumeric)
- {
- // All alphanumeric characters
- if (ch >= 'A' && ch <= 'Z') return ch;
- if (ch >= 'a' && ch <= 'z') return ch;
- if (ch >= '0' && ch <= '9') return ch;
- }
- else if (characterValidation == CharacterValidation.Name)
- {
- char lastChar = (text.Length > 0) ? text[Mathf.Clamp(pos, 0, text.Length - 1)] : ' ';
- char nextChar = (text.Length > 0) ? text[Mathf.Clamp(pos + 1, 0, text.Length - 1)] : '\n';
- if (char.IsLetter(ch))
- {
- // Space followed by a letter -- make sure it's capitalized
- if (char.IsLower(ch) && lastChar == ' ')
- return char.ToUpper(ch);
- // Uppercase letters are only allowed after spaces (and apostrophes)
- if (char.IsUpper(ch) && lastChar != ' ' && lastChar != '\'')
- return char.ToLower(ch);
- // If character was already in correct case, return it as-is.
- // Also, letters that are neither upper nor lower case are always allowed.
- return ch;
- }
- else if (ch == '\'')
- {
- // Don't allow more than one apostrophe
- if (lastChar != ' ' && lastChar != '\'' && nextChar != '\'' && !text.Contains("'"))
- return ch;
- }
- else if (ch == ' ')
- {
- // Don't allow more than one space in a row
- if (lastChar != ' ' && lastChar != '\'' && nextChar != ' ' && nextChar != '\'')
- return ch;
- }
- }
- else if (characterValidation == CharacterValidation.EmailAddress)
- {
- // From StackOverflow about allowed characters in email addresses:
- // Uppercase and lowercase English letters (a-z, A-Z)
- // Digits 0 to 9
- // Characters ! # $ % & ' * + - / = ? ^ _ ` { | } ~
- // Character . (dot, period, full stop) provided that it is not the first or last character,
- // and provided also that it does not appear two or more times consecutively.
- if (ch >= 'A' && ch <= 'Z') return ch;
- if (ch >= 'a' && ch <= 'z') return ch;
- if (ch >= '0' && ch <= '9') return ch;
- if (ch == '@' && text.IndexOf('@') == -1) return ch;
- if (kEmailSpecialCharacters.IndexOf(ch) != -1) return ch;
- if (ch == '.')
- {
- char lastChar = (text.Length > 0) ? text[Mathf.Clamp(pos, 0, text.Length - 1)] : ' ';
- char nextChar = (text.Length > 0) ? text[Mathf.Clamp(pos + 1, 0, text.Length - 1)] : '\n';
- if (lastChar != '.' && nextChar != '.')
- return ch;
- }
- }
- else if (characterValidation == CharacterValidation.Regex)
- {
- // Regex expression
- if (Regex.IsMatch(ch.ToString(), m_RegexValue))
- {
- return ch;
- }
- }
- else if (characterValidation == CharacterValidation.CustomValidator)
- {
- if (m_InputValidator != null)
- {
- char c = m_InputValidator.Validate(ref text, ref pos, ch);
- m_Text = text;
- stringSelectPositionInternal = stringPositionInternal = pos;
- return c;
- }
- }
- return (char)0;
- }
- public void ActivateInputField()
- {
- if (m_TextComponent == null || m_TextComponent.font == null || !IsActive() || !IsInteractable())
- return;
- if (isFocused)
- {
- if (m_SoftKeyboard != null && !m_SoftKeyboard.active)
- {
- m_SoftKeyboard.active = true;
- m_SoftKeyboard.text = m_Text;
- }
- }
- m_ShouldActivateNextUpdate = true;
- }
- private void ActivateInputFieldInternal()
- {
- if (EventSystem.current == null)
- return;
- if (EventSystem.current.currentSelectedGameObject != gameObject)
- EventSystem.current.SetSelectedGameObject(gameObject);
- if (TouchScreenKeyboard.isSupported && shouldHideSoftKeyboard == false)
- {
- if (inputSystem != null && inputSystem.touchSupported)
- {
- TouchScreenKeyboard.hideInput = shouldHideMobileInput;
- }
- if (shouldHideSoftKeyboard == false && m_ReadOnly == false)
- {
- m_SoftKeyboard = (inputType == InputType.Password) ?
- TouchScreenKeyboard.Open(m_Text, keyboardType, false, multiLine, true, false, "", characterLimit) :
- TouchScreenKeyboard.Open(m_Text, keyboardType, inputType == InputType.AutoCorrect, multiLine, false, false, "", characterLimit);
- OnFocus();
- // Opening the soft keyboard sets its selection to the end of the text.
- // As such, we set the selection to match the Input Field's internal selection.
- if (m_SoftKeyboard != null)
- {
- int length = stringPositionInternal < stringSelectPositionInternal ? stringSelectPositionInternal - stringPositionInternal : stringPositionInternal - stringSelectPositionInternal;
- m_SoftKeyboard.selection = new RangeInt(stringPositionInternal < stringSelectPositionInternal ? stringPositionInternal : stringSelectPositionInternal, length);
- }
- //}
- }
- // Cache the value of isInPlaceEditingAllowed, because on UWP this involves calling into native code
- // The value only needs to be updated once when the TouchKeyboard is opened.
- #if UNITY_2019_1_OR_NEWER
- m_TouchKeyboardAllowsInPlaceEditing = TouchScreenKeyboard.isInPlaceEditingAllowed;
- #endif
- }
- else
- {
- if (!TouchScreenKeyboard.isSupported && m_ReadOnly == false && inputSystem != null)
- inputSystem.imeCompositionMode = IMECompositionMode.On;
- OnFocus();
- }
- m_AllowInput = true;
- m_OriginalText = text;
- m_WasCanceled = false;
- SetCaretVisible();
- UpdateLabel();
- }
- public override void OnSelect(BaseEventData eventData)
- {
- //Debug.Log("OnSelect()");
- base.OnSelect(eventData);
- SendOnFocus();
- ActivateInputField();
- }
- public virtual void OnPointerClick(PointerEventData eventData)
- {
- //Debug.Log("Pointer Click Event...");
- if (eventData.button != PointerEventData.InputButton.Left)
- return;
- ActivateInputField();
- }
- public void OnControlClick()
- {
- //Debug.Log("Input Field control click...");
- }
- public void ReleaseSelection()
- {
- m_SelectionStillActive = false;
- m_ReleaseSelection = false;
- m_PreviouslySelectedObject = null;
- MarkGeometryAsDirty();
- SendOnEndEdit();
- SendOnEndTextSelection();
- }
- public void DeactivateInputField(bool clearSelection = false)
- {
- //Debug.Log("Deactivate Input Field...");
- // Not activated do nothing.
- if (!m_AllowInput)
- return;
- m_HasDoneFocusTransition = false;
- m_AllowInput = false;
- if (m_Placeholder != null)
- m_Placeholder.enabled = string.IsNullOrEmpty(m_Text);
- if (m_TextComponent != null && IsInteractable())
- {
- if (m_WasCanceled && m_RestoreOriginalTextOnEscape)
- text = m_OriginalText;
- if (m_SoftKeyboard != null)
- {
- m_SoftKeyboard.active = false;
- m_SoftKeyboard = null;
- }
- m_SelectionStillActive = true;
- if (m_ResetOnDeActivation || m_ReleaseSelection)
- {
- //m_StringPosition = m_StringSelectPosition = 0;
- //m_CaretPosition = m_CaretSelectPosition = 0;
- //m_TextComponent.rectTransform.localPosition = m_DefaultTransformPosition;
- if (m_VerticalScrollbar == null)
- ReleaseSelection();
- }
- if (inputSystem != null)
- inputSystem.imeCompositionMode = IMECompositionMode.Auto;
- }
- MarkGeometryAsDirty();
- // Scrollbar should be updated.
- m_IsScrollbarUpdateRequired = true;
- }
- public override void OnDeselect(BaseEventData eventData)
- {
- DeactivateInputField();
- base.OnDeselect(eventData);
- SendOnFocusLost();
- }
- public virtual void OnSubmit(BaseEventData eventData)
- {
- //Debug.Log("OnSubmit()");
- if (!IsActive() || !IsInteractable())
- return;
- if (!isFocused)
- m_ShouldActivateNextUpdate = true;
- SendOnSubmit();
- }
- //public virtual void OnLostFocus(BaseEventData eventData)
- //{
- // if (!IsActive() || !IsInteractable())
- // return;
- //}
- private void EnforceContentType()
- {
- switch (contentType)
- {
- case ContentType.Standard:
- {
- // Don't enforce line type for this content type.
- m_InputType = InputType.Standard;
- m_KeyboardType = TouchScreenKeyboardType.Default;
- m_CharacterValidation = CharacterValidation.None;
- break;
- }
- case ContentType.Autocorrected:
- {
- // Don't enforce line type for this content type.
- m_InputType = InputType.AutoCorrect;
- m_KeyboardType = TouchScreenKeyboardType.Default;
- m_CharacterValidation = CharacterValidation.None;
- break;
- }
- case ContentType.IntegerNumber:
- {
- m_LineType = LineType.SingleLine;
- m_InputType = InputType.Standard;
- m_KeyboardType = TouchScreenKeyboardType.NumberPad;
- m_CharacterValidation = CharacterValidation.Integer;
- break;
- }
- case ContentType.DecimalNumber:
- {
- m_LineType = LineType.SingleLine;
- m_InputType = InputType.Standard;
- m_KeyboardType = TouchScreenKeyboardType.NumbersAndPunctuation;
- m_CharacterValidation = CharacterValidation.Decimal;
- break;
- }
- case ContentType.Alphanumeric:
- {
- m_LineType = LineType.SingleLine;
- m_InputType = InputType.Standard;
- m_KeyboardType = TouchScreenKeyboardType.ASCIICapable;
- m_CharacterValidation = CharacterValidation.Alphanumeric;
- break;
- }
- case ContentType.Name:
- {
- m_LineType = LineType.SingleLine;
- m_InputType = InputType.Standard;
- m_KeyboardType = TouchScreenKeyboardType.Default;
- m_CharacterValidation = CharacterValidation.Name;
- break;
- }
- case ContentType.EmailAddress:
- {
- m_LineType = LineType.SingleLine;
- m_InputType = InputType.Standard;
- m_KeyboardType = TouchScreenKeyboardType.EmailAddress;
- m_CharacterValidation = CharacterValidation.EmailAddress;
- break;
- }
- case ContentType.Password:
- {
- m_LineType = LineType.SingleLine;
- m_InputType = InputType.Password;
- m_KeyboardType = TouchScreenKeyboardType.Default;
- m_CharacterValidation = CharacterValidation.None;
- break;
- }
- case ContentType.Pin:
- {
- m_LineType = LineType.SingleLine;
- m_InputType = InputType.Password;
- m_KeyboardType = TouchScreenKeyboardType.NumberPad;
- m_CharacterValidation = CharacterValidation.Digit;
- break;
- }
- default:
- {
- // Includes Custom type. Nothing should be enforced.
- break;
- }
- }
- SetTextComponentWrapMode();
- }
- void SetTextComponentWrapMode()
- {
- if (m_TextComponent == null)
- return;
- if (multiLine)
- m_TextComponent.enableWordWrapping = true;
- else
- m_TextComponent.enableWordWrapping = false;
- }
- // Control Rich Text option on the text component.
- void SetTextComponentRichTextMode()
- {
- if (m_TextComponent == null)
- return;
- m_TextComponent.richText = m_RichText;
- }
- void SetToCustomIfContentTypeIsNot(params ContentType[] allowedContentTypes)
- {
- if (contentType == ContentType.Custom)
- return;
- for (int i = 0; i < allowedContentTypes.Length; i++)
- if (contentType == allowedContentTypes[i])
- return;
- contentType = ContentType.Custom;
- }
- void SetToCustom()
- {
- if (contentType == ContentType.Custom)
- return;
- contentType = ContentType.Custom;
- }
- void SetToCustom(CharacterValidation characterValidation)
- {
- if (contentType == ContentType.Custom)
- {
- characterValidation = CharacterValidation.CustomValidator;
- return;
- }
- contentType = ContentType.Custom;
- characterValidation = CharacterValidation.CustomValidator;
- }
- protected override void DoStateTransition(SelectionState state, bool instant)
- {
- if (m_HasDoneFocusTransition)
- state = SelectionState.Selected;
- else if (state == SelectionState.Pressed)
- m_HasDoneFocusTransition = true;
- base.DoStateTransition(state, instant);
- }
- /// <summary>
- /// See ILayoutElement.CalculateLayoutInputHorizontal.
- /// </summary>
- public virtual void CalculateLayoutInputHorizontal()
- { }
- /// <summary>
- /// See ILayoutElement.CalculateLayoutInputVertical.
- /// </summary>
- public virtual void CalculateLayoutInputVertical()
- { }
- /// <summary>
- /// See ILayoutElement.minWidth.
- /// </summary>
- public virtual float minWidth { get { return 0; } }
- /// <summary>
- /// Get the displayed with of all input characters.
- /// </summary>
- public virtual float preferredWidth
- {
- get
- {
- if (textComponent == null)
- return 0;
- float horizontalPadding = 0;
- if (m_LayoutGroup != null)
- horizontalPadding = m_LayoutGroup.padding.horizontal;
- if (m_TextViewport != null)
- horizontalPadding += m_TextViewport.offsetMin.x - m_TextViewport.offsetMax.x;
- return m_TextComponent.preferredWidth + horizontalPadding; // Should add some extra padding for caret
- }
- }
- /// <summary>
- /// See ILayoutElement.flexibleWidth.
- /// </summary>
- public virtual float flexibleWidth { get { return -1; } }
- /// <summary>
- /// See ILayoutElement.minHeight.
- /// </summary>
- public virtual float minHeight { get { return 0; } }
- /// <summary>
- /// Get the height of all the text if constrained to the height of the RectTransform.
- /// </summary>
- public virtual float preferredHeight
- {
- get
- {
- if (textComponent == null)
- return 0;
- float verticalPadding = 0;
- if (m_LayoutGroup != null)
- verticalPadding = m_LayoutGroup.padding.vertical;
- if (m_TextViewport != null)
- verticalPadding += m_TextViewport.offsetMin.y - m_TextViewport.offsetMax.y;
- return m_TextComponent.preferredHeight + verticalPadding;
- }
- }
- /// <summary>
- /// See ILayoutElement.flexibleHeight.
- /// </summary>
- public virtual float flexibleHeight { get { return -1; } }
- /// <summary>
- /// See ILayoutElement.layoutPriority.
- /// </summary>
- public virtual int layoutPriority { get { return 1; } }
- /// <summary>
- /// Function to conveniently set the point size of both Placeholder and Input Field text object.
- /// </summary>
- /// <param name="pointSize"></param>
- public void SetGlobalPointSize(float pointSize)
- {
- TMP_Text placeholderTextComponent = m_Placeholder as TMP_Text;
- if (placeholderTextComponent != null) placeholderTextComponent.fontSize = pointSize;
- textComponent.fontSize = pointSize;
- }
- /// <summary>
- /// Function to conveniently set the Font Asset of both Placeholder and Input Field text object.
- /// </summary>
- /// <param name="fontAsset"></param>
- public void SetGlobalFontAsset(TMP_FontAsset fontAsset)
- {
- TMP_Text placeholderTextComponent = m_Placeholder as TMP_Text;
- if (placeholderTextComponent != null) placeholderTextComponent.font = fontAsset;
- textComponent.font = fontAsset;
- }
- }
- static class SetPropertyUtility
- {
- public static bool SetColor(ref Color currentValue, Color newValue)
- {
- if (currentValue.r == newValue.r && currentValue.g == newValue.g && currentValue.b == newValue.b && currentValue.a == newValue.a)
- return false;
- currentValue = newValue;
- return true;
- }
- public static bool SetEquatableStruct<T>(ref T currentValue, T newValue) where T : IEquatable<T>
- {
- if (currentValue.Equals(newValue))
- return false;
- currentValue = newValue;
- return true;
- }
- public static bool SetStruct<T>(ref T currentValue, T newValue) where T : struct
- {
- if (currentValue.Equals(newValue))
- return false;
- currentValue = newValue;
- return true;
- }
- public static bool SetClass<T>(ref T currentValue, T newValue) where T : class
- {
- if ((currentValue == null && newValue == null) || (currentValue != null && currentValue.Equals(newValue)))
- return false;
- currentValue = newValue;
- return true;
- }
- }
- }
|