Search code examples
dialogversiondm-script

Closing a modal dialog in GMS 2.x and GMS 1.x?


I have a dialog that needs 3 options, which I have implemented as buttons. It would be best served by a modal dialog. I have code like this:

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")

This works fine in DM2. In DM1, however, I get an error: script objects have no close method.

Instead, I thought I'd try to close the window. Replace self.close above with:

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

This crashes both DM1 and DM2. Is there a better way? Do a modal dialog with radio buttons instead?


Solution

  • Presumably your goal is to have a modal 'choice' of multiple actions. A code which would do that would be

    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() )
    }
    

    RadioDialog