This page has been translated automatically.
Video Tutorials
Interface
Essentials
Advanced
How To
Professional (SIM)
UnigineEditor
Interface Overview
Assets Workflow
Settings and Preferences
Working With Projects
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Lighting
Landscape Tool
Sandworm
Using Editor Tools for Specific Tasks
Extending Editor Functionality
Built-in Node Types
Nodes
Objects
Effects
Decals
Light Sources
Geodetics
World Nodes
Sound Objects
Pathfinding Objects
Players
Programming
Fundamentals
Setting Up Development Environment
Usage Examples
C++
UnigineScript
UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
Materials and Shaders
Rebuilding the Engine 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
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
Content Creation
Content Optimization
Materials
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials
Warning! This version of documentation is OUTDATED, as it describes an older SDK version! Please switch to the documentation for the latest SDK version.
Warning! This version of documentation describes an old SDK version which is no longer supported! Please upgrade to the latest SDK version.

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 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.
    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: 2022-03-10
Build: ()