Godot简易模态提示框

// Tips dialog
// Created by nwhasd
// 2021/8/28
// MIT license 

using Godot;

// 模态提示框
public class AcceptDialogEx : AcceptDialog
{
    // 显示提示
    public static void ShowTips(Node parent, string tips)
    {
        AcceptDialogEx dlg = new AcceptDialogEx();
        dlg.SetTips(parent, tips);
    }

    // 设置提示
    private void SetTips(Node parent, string tips)
    {
        if (null == parent)
            return;

        parent.AddChild(this);
        DialogText = tips;
        WindowTitle = "提示";
        GetOk().Text = "确定";
        PopupExclusive = true;
        PopupCentered();
        Connect("popup_hide", this, nameof(OnClose));
    }

    // 关闭对话框
    private void OnClose()
    {
        Node parent = GetParent();
        if (null != parent)
            parent.RemoveChild(this);

        QueueFree();
    }
}

使用:

using Godot;

public class MainScene : Node
{
    public override void _Ready()
    {
        GetNode("Button").Connect("pressed", this, "OnClick");
    }

    void OnClick()
    {
        AcceptDialogEx.ShowTips(this, "你好!");
    }
}

猜你喜欢

转载自blog.csdn.net/u013404885/article/details/119969413
今日推荐