UnigineEditor
Interface Overview
Assets Workflow
Settings and Preferences
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Landscape Tool
Using Editor Tools for Specific Tasks
FAQ
Программирование
Fundamentals
Setting Up Development Environment
Usage Examples
UnigineScript
C++
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine and Tools
GUI
Double Precision Coordinates
API
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
CIGI Client Plugin
Rendering-Related Classes

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#

WidgetDialog Class Usage Example#

C# Side#

Let's see how the example works:

Source code (C#)
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 int init()
		{	// get gui
			Gui gui = Gui.get();
			
			// 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.getWidget(), Gui.ALIGN_EXPAND);

			button_1 = new WidgetButton(gui, "File");
			button_1.addCallback(Gui.CLICKED, () => button_file_clicked("DialogFile", "./"));
			window.addChild(button_1.getWidget(), 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.getWidget(), 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.getWidget(), Gui.ALIGN_EXPAND);

			// arrange window
			window.arrange();
			gui.addChild(window.getWidget(), Gui.ALIGN_OVERLAP | Gui.ALIGN_CENTER);

			return 1;
		}

		public override int 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 1;
		}
		
		/*
		 * Dialog methods for each dialog window
		 */
		private void dialog_ok_clicked(WidgetDialog dialog, int type)
		{
			Log.message("{0} ok clicked\n", dialog.getText());
			if (type == 1)
				Log.message("{0}\n", WidgetDialogFile.cast(dialog).getFile());
			if (type == 2)
				Log.message("{0}\n", WidgetDialogColor.cast(dialog).getWebColor());
			Gui.get().removeChild(dialog.getWidget());
		}

		private void dialog_cancel_clicked(Widget widget, WidgetDialog dialog)
		{
			Log.message("{0} cancel clicked\n", dialog.getText());
			Gui.get().removeChild(dialog.getWidget());
		}

		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));
			Gui.get().addChild(dialog.getWidget(), Gui.ALIGN_OVERLAP | Gui.ALIGN_CENTER);
			dialog.setPermanentFocus();
		}
		
		/*
		 * Callbacks for buttons
		 */
		private void button_message_clicked(string str, string message)
		{
			dialog_message = new WidgetDialogMessage(Gui.get(), str);
			dialog_message.setMessageText(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.setPath(path);
			dialog_show(dialog_file.getWidgetDialog(), 1);
		}

		private void button_color_clicked(string str, vec4 color)
		{
			dialog_color = new WidgetDialogColor(Gui.get(), str);
			dialog_color.setColor(color);
			dialog_show(dialog_color.getWidgetDialog(), 2);
		}

		private void button_image_clicked(string str, string name)
		{
			dialog_image = new WidgetDialogImage(Gui.get(), str);
			dialog_image.setTexture(name);
			dialog_show(dialog_image.getWidgetDialog(), 3);
		}
	}

	[STAThread]
	static void Main(string[] args)
	{
		Wrapper.init();

		// 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.
    Notice
    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:
    Source code (C#)
    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.
    Warning
    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: 27.12.2018