【问题标题】:How to add content to a visible dialog如何将内容添加到可见对话框
【发布时间】:2020-04-14 13:32:46
【问题描述】:

当用户点击 dm 脚本中的按钮时,如何将元素添加到可见对话框?


我的目标是一个输入对话框,允许用户选择他想要处理的图像。这可以是多个图像。因此,我想设计一个提供 add 按钮的对话框。单击该按钮会添加一个选择框 (DLGCreateImagePopup()) 用于选择图像。

我的问题是我没有找到更新对话框 UI 的方法。它不会重绘内容。唯一接近我的问题的是这篇关于how to enable and disable an element 的帖子。该帖子建议使用UIFrame.close(),后跟UIFrame.display(),但即将出现的对话框不再是模态的。将UIFrame.display() 更改为UIFrame.pose(),对话框就会消失。当尝试再次执行脚本时,错误 Class already declared: 'TestDialog' 出现。那我得重启GMS了。

以下脚本会创建如图所示的对话框。单击“添加”时,“按下按钮”行。应该会出现,但什么也没发生。

TagGroup dialog_items;
TagGroup dialog_tags = DLGCreateDialog("Test dialog", dialog_items);

TagGroup group = DLGCreateGroup();
group.DLGIdentifier("group");
dialog_items.DLGAddElement(group);

TagGroup label = DLGCreateLabel("Press the 'Add' button.");
group.DLGAddElement(label);

TagGroup add = DLGCreatePushButton("Add", "addButtonPressed");
group.DLGAddElement(add);

class TestDialog : UIFrame{
    void addButtonPressed(object self){
        TagGroup g = self.LookUpElement("group");
        TagGroup l = DLGCreateLabel("Button pressed.");
        g.DLGAddElement(l);

        self.ValidateView(); // <- does nothing

        // self.close();
        // self.display(""); // <- doesn't show as modal

        // self.close();
        // self.pose(); // <- doesn't show up, forces to restart GMS
    }
}

Object dialog = alloc(TestDialog).init(dialog_tags);

dialog.Pose();

【问题讨论】:

    标签: user-interface dialog updates dm-script


    【解决方案1】:

    DM 脚本中的脚本对话框模型非常有限,不支持在显示期间添加或删除项目(从 GMS 3.4 开始)。

    也许可以通过大量努力来解决这个问题,但总体而言,花费太多时间来使脚本对话框“漂亮”通常是不值得的。

    最好的办法是利用现有项目的 shown 属性来显示或隐藏它们。

    查看示例:

    class CElementHideTest : UIframe
    {
        TagGroup BuildDialog(object self)
        {
            TagGroup dlg,dlgItems
            dlg = DLGCreateDialog("test",dlgitems)
    
            TagGroup group = DLGCreateGroup().DLGIdentifier("group")
            dlgitems.DLGAddElement(group)
    
            TagGroup label = DLGCreateLabel("Toggle tests")
            group.DLGAddElement(label)
    
            TagGroup toggleEnabledButton = DLGCreatePushButton("Toggle Enabled", "toggleEnabled")
            group.DLGAddElement(toggleEnabledButton)
    
            TagGroup toggleShownButton = DLGCreatePushButton("Toggle Shown", "toggleShown")
            group.DLGAddElement(toggleShownButton)
    
            TagGroup field1 = DLGCreateIntegerField(5,5).DLGIdentifier("field")
            dlgitems.DLGAddElement(field1)
    
            return dlg
        }
    
        void toggleEnabled(object self)
        {
            number is = self.GetElementIsEnabled("field")
            self.SetElementIsEnabled("field",!is)
        }
    
        void toggleShown(object self)
        {
            number is = self.GetElementIsShown("field")
            self.SetElementIsShown("field",!is)
        }
    
        object Init(object self)
        {
            return self.Init(self.BuildDialog())
        }
    }
    
    Alloc(CElementHideTest).init().Pose()
    

    虽然可以在显示时调整对话框窗口的大小,但这对于模态对话框没有多大用处,因为 OK |取消按钮在启动时是固定的。因此,您只能创建一个“丑陋”的对话框,其中包含大量空白区域,其中将出现项目。

    但是,如果您的脚本在后台线程上运行,那么您可以创建自己的模式对话框,如下例所示。这将允许您在显示项目时使用窗口调整大小并缩小/扩展对话框。

    请注意,这不能在主线程中的脚本上运行,因为对话框显示代码在主线程上运行。因此,等待对话框会阻止对话框正确显示。

    Class CScriptModalDialog : UIFrame
    {
        object contSignal
    
        TagGroup BuildDialog(object self)
        {
            TagGroup dlg,dlgItems
            dlg = DLGCreateDialog("test",dlgitems)
    
            TagGroup group = DLGCreateGroup().DLGIdentifier("group")
            dlgitems.DLGAddElement(group)
    
            TagGroup label = DLGCreateLabel("Display as modal dialog")
            group.DLGAddElement(label)
    
            TagGroup toggleEnabledButton = DLGCreatePushButton("Continue", "ContinuePressed")
            group.DLGAddElement(toggleEnabledButton)
    
            contSignal = NewSignal(0)
    
            return dlg
        }
    
        void ContinuePressed(object self)
        {
            contSignal.SetSignal()
        }
    
        number PoseScriptDlg(object self, number timeOutSec )
        {
            self.Init(self.BuildDialog())
            self.Display("Script dialog")
            object cancelSignal = NewCancelSignal()
            number success = contSignal.WaitOnSignal(timeOutSec,cancelSignal)   // Could also use Infinity() as timeout
            self.Close()
            return success
        }
    
        number WaitOnOK(object self)
        {
            object cancelSignal = NewCancelSignal()
            return contSignal.WaitOnSignal(1,cancelSignal)
        }
    }
    
    class CMain
    {
        object continueDlg
        CMain(object self) { continueDlg=Alloc(CScriptModalDialog); }
    
        void RunMethod(object self)
        {
            ClearResults()
            Result("Waiting on user for 3 sec...\n")
            if ( continueDlg.PoseScriptDlg(3) )
                Result("Continue\n")
            else
                Result("TimeOut\n")
        }
    }
    
    Alloc(CMain).StartThread("RunMethod")
    

    【讨论】:

    • 感谢您的回答。然后,我将使用您提出的解决方案。
    【解决方案2】:

    不是对您问题的直接回答,也可能不是您想要的,而是作为一个创意者: 如果您只对选择最多四张图片感兴趣,也可以使用现有的Get...Images() 命令获得一些创意,例如 f.e:

    image img1,img2,img3,img4
    if (GetFourlabeledImagesWithPrompt( "Select up to 4 images.\nDouble selected images will be used once.","Titel", "first:",img1, "second:",img2,"third:",img3,"fourth:",img4))
    {
        // Make list of used ID's removing doubles
        taggroup list = NewTagGroup()
        list.TagGroupSetTagAsBoolean( img1.ImageGetLabel(), 1 )
        list.TagGroupSetTagAsBoolean( img2.ImageGetLabel(), 1 )
        list.TagGroupSetTagAsBoolean( img3.ImageGetLabel(), 1 )
        list.TagGroupSetTagAsBoolean( img4.ImageGetLabel(), 1 )
    
        number nUsed = list.TagGroupCountTags()
        Result("\n Unique images chosen: " + nUsed)
        for( number i=0; i<nUsed; i++)
        {
            image img := FindImageByLabel( list.TagGroupGetTagLabel(i) )
            if ( img.ImageIsValid() )
            {
                Result("\n\t Image #"+i+": <"+img.ImageGetName()+">" )
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多