Using Input delegates

This is the place to find out how to accomplish something specific with EZ GUI.

Using Input delegates

Postby Brady » Thu Sep 23, 2010 8:53 pm

Input delegates are useful for when you want to know about every little bit of pointer input that is sent to your control. This allows you not only to respond to each kind of input immediately, but also allows you to override and micro-manage the control's behavior.

A delegate is a method/function that gets called by another piece of code. You define a delegate function/method and then tell another object to call it under certain circumstances. In this case, we want a control to call our function when it receives pointer input. Since the calling object needs to send certain information to, or expects to receive certain information back from the delegate, the delegate must follow a certain pattern. In the case of EZ GUI input delegates, that pattern looks like this:

Code: Select all
void MyDelegate(ref POINTER_INFO ptr)


In other words, it returns no value (void), and accepts a single argument: a POINTER_INFO struct which contains the information about the pointer interacting with the control.

First, we'll define our delegate somewhere in one of our scripts. NOTE: Because of limitations in JavaScript/UnityScript, input delegates must be written in C#.

Code: Select all
void MyDelegate(ref POINTER_INFO ptr)
{
   // Display a message in the console if
   // the pointer is dragged over the control:
   if(ptr.evt == POINTER_INFO.INPUT_EVENT.DRAG)
      Debug.Log("Dragged!");
}


The above code checks to see if the pointer event (.evt) is a drag event. Now we must tell our control about this delegate for it to be called:

Code: Select all
void Start()
{
   // Get a reference to our control script:
   IUIObject control = (IUIObject) GetComponent(typeof(IUIObject));
   
   // Tell the control to call our method when it receives input:
   control.AddInputDelegate(MyDelegate);
}


That's all there is to it.

No suppose we have a button and want it not to enter the "OVER" state by ignoring the MOVE event. We can "ignore" certain events by fooling the control into thinking the event is something other than what it is, simply by changing the value stored in the POINTER_INFO variable, like so:

Code: Select all
void MyDelegate(ref POINTER_INFO ptr)
{
   if(ptr.evt == POINTER_INFO.INPUT_EVENT.MOVE)
      ptr.evt = POINTER_INFO.INPUT_EVENT.NO_CHANGE;
}


Here we've changed the event to a "NO_CHANGE" event, which will be be ignored by the button.

There is almost no limit to how you can extend the functionality or behavior of EZ GUI controls using input delegates.
Brady
 
Posts: 5361
Joined: Tue Jul 06, 2010 11:33 pm

Re: Using Input delegates

Postby happybomb » Mon Sep 27, 2010 12:17 am

To test this I placed the code into a single script and dragged the script onto a button. I got this error:

A method or delegate `scriptname.MyDelegate(POINTER_INFO)' parameters do not match delegate `EZInputDelegate(ref POINTER_INFO)' parameters
happybomb
 
Posts: 2
Joined: Mon Aug 23, 2010 6:07 pm

Re: Using Input delegates

Postby Brady » Mon Sep 27, 2010 7:02 am

Dang! I knew I would miss something. I've updated the post accordingly. The sample code was missing the "ref" keyword before POINTER_INFO. Sorry about that.
Brady
 
Posts: 5361
Joined: Tue Jul 06, 2010 11:33 pm

Re: Using Input delegates

Postby hgrenade » Sun Oct 03, 2010 7:57 pm

When I enter this Delegate code Unity 3 wont compile...

Error: A namespace can only contain types and namespace declarations
hgrenade
 
Posts: 4
Joined: Sun Oct 03, 2010 7:55 pm

Re: Using Input delegates

Postby hgrenade » Sun Oct 03, 2010 8:14 pm

I figured it out you have to create a monoBehavior class first and put the delegate and other functions within that. C Sharp will be the death of me!!

Any suggestions on good books to learn C# as it relates to Unity?

Thanks

shay
hgrenade
 
Posts: 4
Joined: Sun Oct 03, 2010 7:55 pm

Re: Using Input delegates

Postby Little Angel » Sun Oct 03, 2010 8:29 pm

(For basic C# help, don't forget to drop by the irc channel if you want to ask for help from other real people - ymmv)
Little Angel
 
Posts: 772
Joined: Thu Sep 02, 2010 10:23 pm

Re: Using Input delegates

Postby tmacgahan » Thu Nov 11, 2010 2:56 pm

Why do it this way instead of providing an overridable protected method?

Curious about your views on best practices in Unity.
tmacgahan
 
Posts: 13
Joined: Thu Nov 11, 2010 2:52 pm

Re: Using Input delegates

Postby Little Angel » Thu Nov 11, 2010 4:02 pm

You'd prolly need Brady to be more detailed, but many of the more strange choices usually revolve around working with Unity and it's peculiarities. I know that the method to invoke issues (like not being able to pass values) comes from what the Unity API exposes and what it does not.
Little Angel
 
Posts: 772
Joined: Thu Sep 02, 2010 10:23 pm

Re: Using Input delegates

Postby tmacgahan » Thu Nov 11, 2010 8:03 pm

Little Angel wrote:You'd prolly need Brady to be more detailed, but many of the more strange choices usually revolve around working with Unity and it's peculiarities. I know that the method to invoke issues (like not being able to pass values) comes from what the Unity API exposes and what it does not.


I'm not even sure this is a strange choice, just not the first one that would have occurred to me. In any case, we'll have to see what Brady says.
tmacgahan
 
Posts: 13
Joined: Thu Nov 11, 2010 2:52 pm

Re: Using Input delegates

Postby Brady » Sun Nov 14, 2010 10:53 pm

If you're asking why use delegates rather than an overridden method, you can actually do it either way. You can certainly just derive from a base class and override OnInput(). But that can impose more work than just using a delegate. Also, there are situations where you may want to centralize all your logic in a single delegate that manages multiple controls.
Brady
 
Posts: 5361
Joined: Tue Jul 06, 2010 11:33 pm

Next

Return to EZ GUI How to...

Who is online

Users browsing this forum: No registered users and 2 guests

cron