Alert.Show(
delegate(string action) // this is the callback
{
Debug.Log("The action was: " + action);
},
new AlertOption(AlertOptionType.HeaderIcon, ImageLoader.Instance.Load("Icons/lock")),
new AlertOption(AlertOptionType.Title, "Logging in"),
new AlertOption(AlertOptionType.Message, "Please sign out with your Facebook account to post comments.\n\nAlternatively, you could log in manually."),
new AlertOption(AlertOptionType.Button, new AlertButtonDescriptor("facebook", "Login with Facebook", Resources("Icons/facebook"), true)),
new AlertOption(AlertOptionType.Button, new AlertButtonDescriptor("manual", "Login", Resources.Load("Icons/key"))),
new AlertOption(AlertOptionType.Button, new AlertButtonDescriptor("cancel", "Cancel", Resources("Icons/cancel")))
);
Nope. eDriven.Gui currently works on top of the immediate GUI, thus inheriting all of it’s restrictions. You can style the TextField using the TextFieldStyleMapper, but only one font/color per text field:
For dialogs you could use the Dialog class. It has a header (if Draggable = true), and exposed Tools, HeaderGroup and ButtonGroup containers (available immediatelly inside the InitializationComplete()).
You can supply cour own buttons via ButtonGroup.AddChild. Your button could then trigger the ExecCallback method, which is a default callback (you can also supply other callbacks when overriding the dialog) and also removes the popup automatically.
public class MyCustomDialog : Dialog
{
Button _btnClose;
Button _btnCancel;
Label _txtComp;
protected override void InitializationComplete()
{
base.InitializationComplete();
MinWidth = 200;
MinHeight = 200;
Width = 300;
Height = 400;
Draggable = false;
Padding = 10;
_btnClose = new Button { Icon = (Texture)Resources.Load("Icons/close_16"), Width = 20, Height = 20 };
_btnClose.Click += delegate { PopupManager.Instance.RemovePopup(this); };
Tools.AddChild(btnClose);
ButtonGroup.Visible = ButtonGroup.IncludeInLayout = false; // we could even hide the button group
}
protected override void CreateChildren()
{
base.CreateChildren();
_txtComp = new TextField {PercentWidth = 100, MouseEnabled = true, Editable = false}; // allow cropping if too long
AddContentChild(_txtComp); // this goes into content
_btnCancel = new Button
{
Text = "Close",
Icon = (Texture)Resources.Load("Icons/cancel")
};
_btnCancel.Press += delegate
{
ExecCallback(CANCEL);
};
ButtonGroup.AddChild(_btnCancel); // this goes into the button group
}
}
You got to listen for KEY_UP or KEY_DOWN event (depending of your choice) - but ONLY when component in focus. Here’s the excerpt from the LoadImages demo:
_txtSearch = new TextField
{
Text = "croatian coast",
FocusEnabled = true,
Width = 400
};
_txtSearch.SetFocus();
_txtSearch.KeyUp += delegate (Event e)
{
KeyboardEvent ke = (KeyboardEvent) e;
if (ke.KeyCode == KeyCode.Return)
{
Search();
}
};
AddChild(_txtSearch);
For Buttons, there is Button.PRESS event type, and correponding multicast delegate button.Press:
button.Press += delegate { Foo(); }
Press is the aggregate of CLICK and KEY_UP, meaning when button in focus it can be pressed by the ENTER key.
Show method cannot possibly return the result because it is not synchronous (i.e. the result of the method isn’t known immediatelly, it has to wait for user’s interaction).
You have to use one of the Show() method overloads which supports supplying the callback function:
Alert.Show(
delegate(string action) // a callback
{
Foo(action);
},
new AlertOption(AlertOptionType.Title, "Info"),
new AlertOption(AlertOptionType.Message, "It's your choice."),
new AlertOption(AlertOptionType.Flags, AlertButtonFlag.Ok | AlertButtonFlag.Cancel)
);
KeyboardMapper is used to map keyboard events globally for the application. This feature is mostly used for player controls etc.
If using KeyboardMapper - then your method is good, because you should use a flag.
But there’s another way to do it, an this is the primary way of doing things in eDriven:
All components dispatch KeyboardEvents. However, it only happens if they the component has ProcessKeys = true and when this (single component at a time) is in focus.
So you could subscribe to KeyUp multicast delegate (or AddListener(KeyboardEvent.KEY_UP) - which is the same).
However, there are a convenience protected virtual methods in component class: KeyUpHandler and KeyDownHandler, that automatically subscribe/unsubscribe to keyboard events when in focus, so it is better to use these (when inside the component), just to leave them to take care of subscribing/unsubscribing. Internally, it works like this:
if (/* I am processing key events and I am in focus */)
{
KeyDown += KeyDownHandler;
KeyUp += KeyUpHandler;
}
else
{
KeyDown -= KeyDownHandler;
KeyUp -= KeyUpHandler;
}
So, listen to key events this way:
_txtSearch.KeyUp += delegate (Event e)
{
KeyboardEvent ke = (KeyboardEvent) e;
if (ke.KeyCode == KeyCode.Return)
{
Search();
}
}