1

我有一个需要 3 个选项的对话框,我已将其实现为按钮。最好使用模态对话框。我有这样的代码:

class testDialog : uiframe
{
    void OnOne( object self )
    {
        Result( "Doing one\n" )
        self.close()
    }
    void OnTwo( object self )
    {
        Result( "Two.\n" )
        self.close()
    }
    void OnThree( object self )
    {
        Result( "Three.\n" )
        self.close()
    }
}

void ThreeButtonDialog(String description)
{
    TagGroup dialog_items
    TagGroup dialog_tags = DLGCreateDialog( "Test Dialog", dialog_items )
    dialog_items.DLGAddElement( DLGCreateLabel( description ).DLGAnchor( "North" ) ).dlgexternalpadding(5,5)
    TagGroup button_items
    TagGroup button_fields = DLGCreateGroup( button_items )
    DLGLayout( button_fields, DLGCreateTableLayout( 3, 1, 0 ) )
    TagGroup one_button = DLGCreatePushButton("Option1", "OnOne")
    TagGroup two_button = DLGCreatePushButton("Option2", "OnTwo")
    TagGroup three_button = DLGCreatePushButton("Option3", "OnThree")
    button_items.DLGAddElement(one_button)
    button_items.DLGAddElement(two_button)
    button_items.DLGAddElement(three_button)
    dialog_items.DLGAddElement( button_fields )

    Object dialog = alloc( testDialog ).init(dialog_tags)
    dialog.Display("Test...")
    DocumentWindow dialogwin=getdocumentwindow(0)
    WindowSetFrameposition(dialogwin, 300, 200)
}

ThreeButtonDialog("test")

这在 DM2 中运行良好。然而,在 DM1 中,我得到一个错误:脚本对象没有关闭方法。

相反,我想我会尝试关闭窗口。将上面的 self.close 替换为:

DocumentWindow dialogwin=getdocumentwindow(0)
dialogwin.WindowClose(0)

这会导致 DM1 和 DM2 崩溃。有没有更好的办法?用单选按钮做一个模态对话框吗?

4

3 回答 3

1

对于 GMS 1.x,从 UIframe 关闭对话框的正确方法是使用

self.GetFrameWindow().WindowClose(0)
而不是
self.close(0)
在上面的代码中。

但是,这会使 GMS 2+ 中的 DM 崩溃。

由于这个问题,在 GMS 2 开发期间的某个时间点, UIframe 对象的方法close( object self )被添加到脚本语言中。(窗口管理在 GMS 1 和 GMS 2 之间发生了变化。)

于 2015-02-28T13:14:48.643 回答
1

大概您的目标是对多个动作进行模态“选择”。可以做到这一点的代码是

class CThreeButtonDialog:UIFrame
{
    TagGroup DLG,DLGitems
    TagGroup radio,radioItems

    object Init( object self, string title, string prompt, string s1, string s2, string s3 )
    {

        DLG = DLGCreateDialog(title,DLGitems)
        DLGitems.DLGAddElement( DLGCreateLabel(prompt) )
        radio = DLGCreateRadioList( radioItems, 1 )
        radioItems.DLGAddRadioItem(s1,1)
        radioItems.DLGAddRadioItem(s2,2)
        radioItems.DLGAddRadioItem(s3,3)
        DLGitems.DLGAddElement(radio)
        return self.super.init(DLG)
    }

    number GetChoice( object self )
    {
        return radio.DLGGetValue()
    }
}


{
    object myChoice = Alloc(CThreeButtonDialog).Init("Choose","Chose your action","One","Two","Three")
    myChoice.Pose()
    OKDialog( "Chosen action:" + myChoice.GetChoice() )
}

电台对话

于 2015-02-28T13:35:34.227 回答
0

如果您的主脚本实际上是在后台线程上运行的,那么还可以选择使用信号并等待该信号,如下面的脚本:

// $BACKGROUND$
Class CModal3Options : UIFrame
{
    object waitSignal 
    TagGroup DLG,DLGitems
    number choice

    object Init(object self, string title, string prompt, string s1, string s2, string s3 )
    {
        choice = 0
        DLG = DLGCreateDialog(title,DLGitems)
        DLGitems.DLGAddElement( DLGCreateLabel(prompt) )
        TagGroup but1 = DLGCreatePushButton( s1, "Action1" );
        TagGroup but2 = DLGCreatePushButton( s2, "Action2" );
        TagGroup but3 = DLGCreatePushButton( s3, "Action3" );
        DLGitems.DLGAddElement( DLGGroupItems(but1,but2,but3).DLGTableLayout(3,1,0) )
        self.super.init(DLG)
        waitSignal = NewSignal(0)    
        return self
    }
    void Action1(object self) { choice=1;waitSignal.SetSignal(); }
    void Action2(object self) { choice=2;waitSignal.SetSignal(); }
    void Action3(object self) { choice=3;waitSignal.SetSignal(); }
    number GetChoice(object self) { return choice; }

    number PoseForTime(object self, number timeOutSec )
    {
        self.Display("Test")
        waitSignal.WaitOnSignal(timeOutSec,NULL)
        self.close()
        return choice
    }
}

{
    object dlg = Alloc(CModal3Options).Init("title","Prompt text","one","two","three")
    number choice = dlg.PoseForTime(2)

    if ( 0 == choice )
    {
        OKDialog("Timeout")
    }
    else
        OKDialog("Your choice: "+choice)
}
于 2015-07-02T11:20:42.337 回答