Author Topic: [SOLVED] Intercept OnSelect(false) call in UIInput to inject code?  (Read 2613 times)

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
[SOLVED] Intercept OnSelect(false) call in UIInput to inject code?
« on: September 05, 2013, 05:01:46 AM »
Hello, I'm working on a file manager. One of the rules is that you can't have duplicate names. I have written the code to give me a unique name just fine. And I'm using it:

1- each time I create a new folder/file.
2- after I submit the changes to a file/folder's name (inside OnSubmit(string name))

Problem is, you see whenever I create a new folder, I give my input 'selected = true' so that I can name a folder right after I create it, BUT if I deselect the input (click elsewhere), OnSubmit doesn't seem to trigger, instead OnSelect(false) is what's being called, and then I can end up with multiple folders having the same name (due to mDefaultText). (See attachment) So I should also inject my code in there as well, specifically:

  1. if (string.IsNullOrEmpty(mText))
  2. {
  3.         label.text = mDefaultText;
  4.         // TODO: INJECT
  5.         //
  6.          here...
  7.         //
  8.         label.color = mDefaultColor;
  9.         if (isPassword) label.password = false;
  10. }
  11. else label.text = mText;
  12.  

I tried inserting a call to 'onSubmit(label.text);' because I do have my code there - but I got an exception onSubmit is null :(

Can I assign onSubmit somehow? Or do I have to make my own custom inherited UIInput and then override OnSelect?

Thanks :D


And here's my code if somebody wants to benefit :D
(I give it the names of all the contents of the current folder, and the name I'm trying to name a certain file/folder with and it gives me back a unique name for that file/folder, in the fashion of file_N, file_N+1, ... file_N+n. Just like MS Windows)
  1. public static string GetUniqueName(List<string> names, string name)
  2. {
  3.         int index = MiscOps.LinearSearch(names.ToArray(), name);
  4.         if (index == -1)
  5.                 return name;
  6.  
  7.         if (!name.Contains("_"))
  8.                 return GetUniqueName(names, name + "_0");
  9.  
  10.         short n;
  11.         string postfix = name.Substring(name.LastIndexOf('_') + 1);
  12.         bool isNum = Int16.TryParse(postfix, out n);
  13.  
  14.         if (!isNum)
  15.                 return GetUniqueName(names, name + "_0");
  16.  
  17.         int postfixNum = Convert.ToInt16(postfix);
  18.         postfixNum++;
  19.         name = name.Substring(0, name.LastIndexOf('_') + 1);
  20.         name += postfixNum.ToString();
  21.         return GetUniqueName(names, name);
  22. }
  23.  
« Last Edit: September 11, 2013, 08:34:18 AM by vexe »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Intercept OnSelect(false) call in UIInput to inject code?
« Reply #1 on: September 05, 2013, 08:52:48 PM »
OnSelect(false) means the input process is getting canceled. If you don't want this behaviour you need to handle this logic yourself. The same thing happens when you hit Escape.

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Re: Intercept OnSelect(false) call in UIInput to inject code?
« Reply #2 on: September 05, 2013, 09:30:26 PM »
Thanks. I know that OnSelect(false) cancels input. I was wondering if I could stick my code in without having to inherit and override the behavior. If I could just insert that onSubmit call right after setting the text to default, I wouldn't need to override anything. As I said the problem is onSubmit was null at that point. Any ideas?

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Re: Intercept OnSelect(false) call in UIInput to inject code?
« Reply #3 on: September 05, 2013, 09:56:05 PM »
OK, I've found a way, it did the job. What you think?
  1. if (string.IsNullOrEmpty(mText)) {
  2.         label.text = mDefaultText;
  3.         // TODO: HACK
  4.         //
  5.         eventReceiver.SendMessage(functionName, label.text, SendMessageOptions.DontRequireReceiver);
  6.         selected = false;
  7.         //
  8.         label.color = mDefaultColor;
  9.         if (isPassword) label.password = false;
  10. }
  11.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Intercept OnSelect(false) call in UIInput to inject code?
« Reply #4 on: September 07, 2013, 01:01:34 AM »
Don't do that. Implement OnSelect(bool isSelected), and do your logic inside if (!isSelected) { ... }