问题
I creat a dialog with DLGCreateImagePopup to select a opened image. But after I select the image, I cannot do anything for that image. How to get the image ID or name of the selected image?
回答1:
If you want to do it as a posed dialog (i.e. get the image after clicking the OK button), you could do it as follows:
Class CMyDLG : UIframe
{
TagGroup DLG,DLGItems,imgPop
object Init(object self)
{
DLG = DLGCreateDialog("Test",DLGItems)
imgPop = DLGCreateImagePopup()
DLGItems.DLGAddElement( imgPop )
return self.super.init(DLG)
}
image GetSelectedImage( object self )
{
string selectedImageLabel
imgPop.DLGGetValue(selectedImageLabel)
Result("\n" + selectedImageLabel)
// From the string, get the label
string label = selectedImageLabel.left( selectedImageLabel.find(":") )
Result("\n" + label)
// From label, return image
return FindImageByLabel(label)
}
}
// main
{
object dlg = Alloc(CMyDLG).Init()
dlg.Pose()
image selected := dlg.GetSelectedImage()
if ( selected.ImageIsValid() )
selected.SetName( "Selected" + random())
else Throw( "Error, nothing selected." )
}
If you want to do something with the image while the dialog is still displayed (modeless dialog) you'd have to attach a OnChanged-type method to the item as in:
Class CMyDLG : UIframe
{
TagGroup DLG,DLGItems,imgPop
void FieldChanged(object self, taggroup tg)
{
string selectedImageLabel
tg.DLGGetValue(selectedImageLabel)
Result("\n" + selectedImageLabel)
// From the string, get the label
string label = selectedImageLabel.left( selectedImageLabel.find(":") )
Result("\n" + label)
// From label, return image
image selected := FindImageByLabel(label)
if ( selected.ImageIsValid() )
selected.SetName( "Selected" + random())
else Throw( "Error, nothing selected." )
}
object Init(object self)
{
DLG = DLGCreateDialog("Test",DLGItems)
imgPop = DLGCreateImagePopup().DLGChangedMethod("FieldChanged")
DLGItems.DLGAddElement( imgPop )
return self.super.init(DLG)
}
}
// main
{
Alloc(CMyDLG).Init().display("myDialog")
}
Note, that when I've tested this script on the latest GMS 3, I've noticed a bug. The Changed-method (and hence the actual selection) seems to happen only when the drop-down is accessed a second time.
来源:https://stackoverflow.com/questions/58170009/how-to-select-a-opened-image-with-dlgcreateimagepopup-in-created-dialog