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 is also explains how to pass a user data in a callback
See also
- An example can be found in the <UnigineSDK>/source/csharp/samples/Api/Widgets/WidgetDialog/ directory.
- The C++ API Unigine::WidgetDialog class and its UnigineScript analog which have the same methods and behavior as in the C# API.
- Articles on UI containers and UI Widgets
WidgetDialog Class Usage Example
C# Side
Let's see how works the example:
using System;
using Unigine;
/*
*/
class UnigineApp {
/*
* Instances for each type of WidgetDialog window
*/
private static WidgetDialogMessage dialog_message;
private static WidgetDialogFile dialog_file;
private static WidgetDialogColor dialog_color;
private static WidgetDialogImage dialog_image;
/*
* Dialog methods for each dialog window
*/
private static void dialog_ok_clicked(int type) {
WidgetDialog dialog = null;
if(type == 0) dialog = dialog_message.getWidgetDialog();
if(type == 1) dialog = dialog_file.getWidgetDialog();
if(type == 2) dialog = dialog_color.getWidgetDialog();
if(type == 3) dialog = dialog_image.getWidgetDialog();
Log.message("{0} ok clicked\n",dialog.getText());
if(type == 1) Log.message("{0}\n",WidgetDialogFile.create(dialog).getFile());
if(type == 2) Log.message("{0}\n",WidgetDialogColor.create(dialog).getWebColor());
Gui.get().removeChild(dialog.getWidget());
}
private static void dialog_cancel_clicked(int type) {
WidgetDialog dialog = null;
if(type == 0) dialog = dialog_message.getWidgetDialog();
if(type == 1) dialog = dialog_file.getWidgetDialog();
if(type == 2) dialog = dialog_color.getWidgetDialog();
if(type == 3) dialog = dialog_image.getWidgetDialog();
Log.message("{0} cancel clicked\n",dialog.getText());
Gui.get().removeChild(dialog.getWidget());
}
private static void dialog_show(WidgetDialog dialog,int type) {
dialog.getOkButton().setCallback0(Gui.CLICKED,new Widget.Callback0i(dialog_ok_clicked),type);
dialog.getCancelButton().setCallback0(Gui.CLICKED,new Widget.Callback0i(dialog_cancel_clicked),type);
Gui.get().addChild(dialog.getWidget(),Gui.ALIGN_OVERLAP | Gui.ALIGN_CENTER);
dialog.setPermanentFocus();
}
/*
* Callbacks for buttons
*/
private static void button_message_clicked() {
WidgetDialogMessage dialog = WidgetDialogMessage.create(Gui.get(),"DialogMessage");
dialog.setMessageText("Message");
dialog_show(dialog.getWidgetDialog(),0);
dialog_message = dialog;
}
private static void button_file_clicked() {
WidgetDialogFile dialog = WidgetDialogFile.create(Gui.get(),"DialogPath");
dialog.setPath("./");
dialog_show(dialog.getWidgetDialog(),1);
dialog_file = dialog;
}
private static void button_color_clicked() {
WidgetDialogColor dialog = WidgetDialogColor.create(Gui.get(),"DialogColor");
dialog.setColor(new vec4(1.0f));
dialog_show(dialog.getWidgetDialog(),2);
dialog_color = dialog;
}
private static void button_image_clicked() {
WidgetDialogImage dialog = WidgetDialogImage.create(Gui.get(),"DialogImage");
dialog.setTexture("data/widget_dialog.png");
dialog_show(dialog.getWidgetDialog(),3);
dialog_image = dialog;
}
/*
*/
[STAThread]
static void Main(string[] args) {
Wrapper.init();
// initialize engine
Engine engine = Engine.init(Engine.VERSION,args);
// get gui
Gui gui = Gui.get();
// create the main window
WidgetWindow window = WidgetWindow.create(gui,"Dialogs",4,4);
// create buttons
WidgetButton button_0 = WidgetButton.create(gui,"Message");
button_0.setCallback0(Gui.CLICKED,new Widget.Callback0(button_message_clicked));
window.addChild(button_0.getWidget(),Gui.ALIGN_EXPAND);
WidgetButton button_1 = WidgetButton.create(gui,"File");
button_1.setCallback0(Gui.CLICKED,new Widget.Callback0(button_file_clicked));
window.addChild(button_1.getWidget(),Gui.ALIGN_EXPAND);
WidgetButton button_2 = WidgetButton.create(gui,"Color");
button_2.setCallback0(Gui.CLICKED,new Widget.Callback0(button_color_clicked));
window.addChild(button_2.getWidget(),Gui.ALIGN_EXPAND);
WidgetButton button_3 = WidgetButton.create(gui,"Image");
button_3.setCallback0(Gui.CLICKED,new Widget.Callback0(button_image_clicked));
window.addChild(button_3.getWidget(),Gui.ALIGN_EXPAND);
// arrange window
window.arrange();
gui.addChild(window.getWidget(),Gui.ALIGN_OVERLAP | Gui.ALIGN_CENTER);
// enter main loop
engine.main();
// 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();
gui.clearPtr();
// shutdown engine
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 on it.
- We set a callback for each button, which will be performed 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 receive an 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
When you run the application, you see the following:
Last update: 2018-04-26
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)