Widget Dialog
This article describes the sample located in the <UnigineSDK>/source/csharp/samples/Api/Widgets/WidgetDialog/ directory. By using WidgetDialog class you can create dialog windows. The article also explains how to pass user data to a callback.
See also#
- An example can be found in the <UnigineSDK>/source/csharp/samples/Api/Widgets/WidgetDialog/ directory.
- WidgetDialog Class API reference.
- Articles on UI containers and UI Widgets
WidgetDialog Class Usage Example#
C# Side#
Let's see how the example works:
using System;
using Unigine;
class UnigineApp
{
class AppSystemLogic : SystemLogic
{
private WidgetWindow window;
private WidgetButton button_0;
private WidgetButton button_1;
private WidgetButton button_2;
private WidgetButton button_3;
/*
* Instances for each type of WidgetDialog window
*/
private WidgetDialogMessage dialog_message;
private WidgetDialogFile dialog_file;
private WidgetDialogColor dialog_color;
private WidgetDialogImage dialog_image;
public override bool Init()
{ // get gui
Gui gui = Engine.gui;
// create the main window
window = new WidgetWindow(gui, "Dialogs", 4, 4);
// create buttons
button_0 = new WidgetButton(gui, "Message");
button_0.AddCallback(Gui.CLICKED, () => button_message_clicked("DialogMessage", "Message"));
window.AddChild(button_0, Gui.ALIGN_EXPAND);
button_1 = new WidgetButton(gui, "File");
button_1.AddCallback(Gui.CLICKED, () => button_file_clicked("DialogFile", "./"));
window.AddChild(button_1, Gui.ALIGN_EXPAND);
button_2 = new WidgetButton(gui, "Color");
button_2.AddCallback(Gui.CLICKED, () => button_color_clicked("DialogColor", new vec4(1.0f)));
window.AddChild(button_2, Gui.ALIGN_EXPAND);
button_3 = new WidgetButton(gui, "Image");
button_3.AddCallback(Gui.CLICKED, () => button_image_clicked("DialogImage", "data/widget_dialog.png"));
window.AddChild(button_3, Gui.ALIGN_EXPAND);
// arrange window
window.Arrange();
gui.AddChild(window, Gui.ALIGN_OVERLAP | Gui.ALIGN_CENTER);
return true;
}
public override bool Shutdown()
{
// clear widgets
if (dialog_message != null)
dialog_message.clearPtr();
if (dialog_file != null)
dialog_file.clearPtr();
if (dialog_color != null)
dialog_color.clearPtr();
if (dialog_image != null)
dialog_image.clearPtr();
button_0.clearPtr();
button_1.clearPtr();
button_2.clearPtr();
button_3.clearPtr();
window.clearPtr();
return true;
}
/*
* Dialog methods for each dialog window
*/
private void dialog_ok_clicked(WidgetDialog dialog, int type)
{
Log.Message("{0} ok clicked\n", dialog.Text);
if (type == 1)
Log.Message("{0}\n", WidgetDialogFile.Сast(dialog).File);
if (type == 2)
Log.Message("{0}\n", WidgetDialogColor.Сast(dialog).WebColor;
Engine.gui.RemoveChild(dialog);
}
private void dialog_cancel_clicked(Widget widget, WidgetDialog dialog)
{
Log.Message("{0} cancel clicked\n", dialog.Text);
Engine.gui.RemoveChild(dialog);
}
private void dialog_show(WidgetDialog dialog, int type)
{
dialog.GetOkButton().AddCallback(Gui.CLICKED, () => dialog_ok_clicked(dialog, type));
dialog.GetCancelButton().AddCallback(Gui.CLICKED, widget => dialog_cancel_clicked(widget, dialog));
Engine.gui.AddChild(dialog, Gui.ALIGN_OVERLAP | Gui.ALIGN_CENTER);
dialog.SetPermanentFocus();
}
/*
* Callbacks for buttons
*/
private void button_message_clicked(string str, string message)
{
dialog_message = new WidgetDialogMessage(Engine.gui, str);
dialog_message.MessageText = message;
dialog_show(dialog_message.GetWidgetDialog(), 0);
}
private void button_file_clicked(string str, string path)
{
dialog_file = new WidgetDialogFile(Gui.get(), str);
dialog_file.Path = path;
dialog_show(dialog_file.getWidgetDialog(), 1);
}
private void button_color_clicked(string str, vec4 color)
{
dialog_color = new WidgetDialogColor(Engine.gui, str);
dialog_color.Color = color;
dialog_show(dialog_color.GetWidgetDialog(), 2);
}
private void button_image_clicked(string str, string name)
{
dialog_image = new WidgetDialogImage(Engine.gui, str);
dialog_image.Texture = name;
dialog_show(dialog_image.GetWidgetDialog(), 3);
}
}
[STAThread]
static void Main(string[] args)
{
// engine init
Engine engine = Engine.Init(Engine.VERSION, args);
// enter main loop
AppSystemLogic system_logic = new AppSystemLogic();
engine.Main(system_logic, null, null);
// engine shutdown
Engine.Shutdown();
}
}
In this sample we do the following:
- We create 4 different Widget Dialog windows (all these classes inherit from the WidgetDialog class) (dialog_message, dialog_file, dialog_color, dialog_image).
- In the Main() method, we create the main window and add 4 buttons to it.
- We set a callback for each button, which will be exacuted when the button is clicked (button_message_clicked(), button_file_clicked(), button_color_clicked(), button_image_clicked()).
- To show the dialog, we create one method dialog_show() which receives a WidgetDialog object and an integer variable called type (from 0 to 3) as arguments.
The callback functions can receive optional arguments of the int or IntPtr type that are used to store user data. IntPtr values can be wrapped in classes, for example:
IntPtr ptr; // create a node and then create a Unigine object Unigine.Object.create(new Node(ptr));
- In the dialog_show() method we set callbacks for Ok and Cancel buttons and pass the integer variable called type as a user data to the callback to handle specific window. Callbacks dialog_ok_clicked() and dialog_cancel_clicked() receive the type variable as an argument. According to the value of the variable the special method will be performed.
- Before Engine.Shutdown() we clear all pointers to widgets if they exist.
You should clear all the GUI objects before engine shutdown to avoid memory leaks!
Output#
As you run the application, you'll see the following:
Last update:
2021-12-13
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)