Event handling in DF-Gui

In DFGUI, there are a wide variety of events which can be used to add functionality to a UI. Most of these should be familiar to GUI programmers - Click, MouseDown, MouseUp, KeyDown, TextSubmitted, etc.
DFGUI additionally has the concept of “event bubbling”. When a control fires an event, this event can be handled by a script on the control. If the event is not consumed by a script on the control (by calling the Use() method of the passed EventArgs), it is forwarded to the parent of the control, all the way up the hierarchy.

There are three main ways to handle events. You can either use an Event Binding to bind the control event to a method in your script, you can handle them SendMessage-style (with a specially named method - pattern is On[EventName], so for instance OnClick will be called upon the Click event), or you can subscribe to the event with standard .NET event handlers (such as “control.Click += control_OnClick”).

So, there are 3 ways to add Event Handler to control, such as a button control:

1. create new script with handler function (such as OnClick), and binding the script to the control,.there are 2 way to bind, one is dragging the script from the Asset window to the Hierarchy Window, two is and new script as a component.

2. Add Df Event Binding to the control,

Event Binding
To add an event binding to a control, right click the control and choose Add Binding → Event Binding.
You can assign the Source and Target components the same way you would a Property Binding component. Choose the event of the Data Source, and the function to call on the Data Target. Keep in mind that the target function must either be parameterless, or match the signature of the bound event. IEnumerators are supported as target functions, in which case the function is started as a coroutine.


3. delegate

when creating a control dynamic , delegating is a good choice,

var button = target.AddControl<dfButton>();

// Set whatever properties the control needs after creation
button.NormalBackgroundColor = colors[ colorNum % colors.Length ];
button.BackgroundSprite = "button-normal";
button.Text = string.Format( "Button {0}", button.ZOrder );
button.Anchor = dfAnchorStyle.Left | dfAnchorStyle.Right;
button.Width = target.Width - target.ScrollPadding.horizontal;

//event handler
button.Click += ButtonClick;


void ButtonClick(dfControl control, dfMouseEventArgs mouseEvent)
{
	Debug.Log(((dfButton)control).Text + " clicked");
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值