问题
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?
回答1:
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() )
}
回答2:
For GMS 1.x the proper way to close a dialog from a UIframe would be to use
self.GetFrameWindow().WindowClose(0)
instead of self.close(0)
in your code above.
However, this will crash DM in GMS 2+.
The UIframe object's method close( object self )
was added to the scripting language at some point during the GMS 2 development because of this issue. (Window management changed between GMS 1 and GMS 2.)
回答3:
If your main script is actually running on a background-thread, then there is also the option to use a signal and wait for that signal like in the following script:
// $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)
}
来源:https://stackoverflow.com/questions/28776137/closing-a-modal-dialog-in-gms-2-x-and-gms-1-x