This page has been translated automatically.
视频教程
界面
要领
高级
实用建议
UnigineEditor
界面概述
资产工作流程
设置和首选项
项目开发
调整节点参数
Setting Up Materials
Setting Up Properties
照明
Landscape Tool
Sandworm
使用编辑器工具执行特定任务
Extending Editor Functionality
嵌入式节点类型
Nodes
Objects
Effects
Decals
光源
Geodetics
World Objects
Sound Objects
Pathfinding Objects
Players
编程
基本原理
搭建开发环境
Usage Examples
UnigineScript
C++
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine Tools
GUI
双精度坐标
应用程序接口
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 Optimization
Materials
Art Samples
Tutorials
注意! 这个版本的文档是过时的,因为它描述了一个较老的SDK版本!请切换到最新SDK版本的文档。
注意! 这个版本的文档描述了一个不再受支持的旧SDK版本!请升级到最新的SDK版本。

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: 2021-04-29
Build: ()