Author Topic: [Feature request] Password Char on UIInput (code included)  (Read 7137 times)

indiefreaks

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
[Feature request] Password Char on UIInput (code included)
« on: February 12, 2013, 06:55:22 AM »
Hi,

Since you provide a "Is Password" property on UIInput to handle password cases, I thought it would finalize the feature to add the ability to specify the character to use instead of forcing the char to '*'.
That's a really small feature request and it's also atomic enought to bring issues implementing it.

I've already made the change on my side yet I'd like to avoid that being removed every time I update my NGUI package to get all the neat additional features and bug fixes.

So here is the code I implemented myself which seems to be compliant with your approach. (Please, consider adding this ;))

In NGUI/Scripts/UI/UIInput.cs:
@line 54 right after caratChar field.

  1. [...]
  2.         /// <summary>
  3.         /// Visual carat character appended to the end of the text when typing.
  4.         /// </summary>
  5.  
  6.         public string caratChar = "|";
  7.  
  8.         /// <summary>
  9.         /// Character used to replace the text when isPassword is set to true.
  10.         /// </summary>
  11.  
  12.         public string passwordChar = "*";
  13.  
  14.         /// <summary>
  15.         /// Delegate used for validation.
  16.         /// </summary>
  17.  
  18.         public Validator validator;
  19. [...]
  20.  

@line 248, replace the existing code with this line:
  1.         if (isPassword) label.password = true; label.passwordChar = passwordChar;
  2.  

Then, we replace @line 448 in the UpdateLabel() method:

  1. [...]
  2.  
  3.         // Start with the text and append the IME composition and carat chars
  4.         string processed;
  5.         if (isPassword && selected)
  6.         {
  7.                 processed = "";
  8.                 for (int i = 0, imax = mText.Length; i < imax; ++i) processed += passwordChar;
  9.                 processed += Input.compositionString + caratChar;
  10.         }
  11.         else processed = selected ? (mText + Input.compositionString + caratChar) : mText;
  12. [...]
  13.  

Now, we need to change a few more lines in NGUI/Scripts/UI/UILabel.cs

@line 26, we add the following new field:

  1.         [HideInInspector][SerializeField] string mPasswordChar = "*";
  2.  

And @line 276, right after the password property, we add this new one:

  1.  
  2.     /// <summary>
  3.     /// The character to use to hide label content when password is set
  4.     /// </summary>
  5.  
  6.         public string passwordChar
  7.         {
  8.                 get { return mPasswordChar; }
  9.                 set
  10.                 {
  11.                         mPasswordChar = value;
  12.                         hasChanged = true;
  13.                 }
  14.         }
  15.  
  16.  

And finally, replace the previously static "*" occurrences by the mPasswordChar field value in ProcessText() method:
@line 472
  1.         for (int i = 0, imax = mProcessedText.Length - 1; i < imax; ++i) hidden += mPasswordChar;
  2.  

And finally @line 477
  1.         for (int i = 0, imax = mProcessedText.Length; i < imax; ++i) hidden += mPasswordChar;
  2.  
« Last Edit: February 12, 2013, 09:50:46 AM by ArenMook »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [Feature request] Password Char on UIInput (code included)
« Reply #1 on: February 12, 2013, 09:50:19 AM »
The problem with this approach is that it adds a new string to the labels, which is not used in 99% of the cases, resulting in extra memory for no reason. I can't add it to the NGUI because of that. I suggest you keep it a local change.

indiefreaks

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
Re: [Feature request] Password Char on UIInput (code included)
« Reply #2 on: February 12, 2013, 10:28:26 AM »
I understand such requirement especially for mobile devices.

I can live with a local implementation but what would be your best advice and recommendation to create my own custom controls while keeping the benefits of your code?

Inheritance isn't a viable option as you do not expose any virtual implementation on the processing of text for either UIInput or UILabel.
The sole way I can see would be to copy/paste your UIInput and UILabel scripts and make my changes to it and then use a Diff tool each release you make to apply your own fixes and features to them.

Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [Feature request] Password Char on UIInput (code included)
« Reply #3 on: February 12, 2013, 10:30:42 AM »
If you had the pro version you'd simply be able to pull the latest changes and have them be auto-merged for you. Without Pro, yeah manual is your best approach.

indiefreaks

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
Re: [Feature request] Password Char on UIInput (code included)
« Reply #4 on: February 12, 2013, 10:35:23 AM »
I don't know if I have the pro edition...  ???
I did pay for the NGUI version on the Asset Store thow ;)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [Feature request] Password Char on UIInput (code included)
« Reply #5 on: February 12, 2013, 10:56:24 AM »
Then no, you don't. :P Professional version is available only via PayPal and comes with the Git repository access.

indiefreaks

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
Re: [Feature request] Password Char on UIInput (code included)
« Reply #6 on: February 12, 2013, 11:42:21 AM »
Hmmm, I didn't know about this option... Is there a way for me to upgrade to this version? What would be the additional cost if possible?...

indiefreaks

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
Re: [Feature request] Password Char on UIInput (code included)
« Reply #7 on: February 12, 2013, 11:46:37 AM »
No need to reply, I just spent the required time to read through the forum and the product detail page on this same website ;)

Thanks