Multiple float dialogs

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
User avatar
MehdiZangenehBar
Posts: 114
Joined: Wed Feb 07, 2024 7:17 pm
Contact:

Multiple float dialogs

Post by MehdiZangenehBar »

When I click on dialog with label "1" , all other dialogs will be closed, how we can prevent this?

Code: Select all

ScriptName = 'Test_Script'

Test_Script = {}

function Test_Script:OnMouseDown(moho, mouseEvent)
end

function Test_Script:DoLayout(moho, layout)
	for i = 1 , 5 do
		local simple_dialog = LM.GUI.SimpleDialog(tostring(i), {})
		simple_dialog:GetLayout():AddChild(LM.GUI.Button('Button'))
		simple_dialog:DoModeless()
	end
end
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Multiple float dialogs

Post by synthsin75 »

You shouldn't be using doLayout to make modeless dialogs. You can use a button in the tool options (doLayout) instead.
User avatar
MehdiZangenehBar
Posts: 114
Joined: Wed Feb 07, 2024 7:17 pm
Contact:

Re: Multiple float dialogs

Post by MehdiZangenehBar »

synthsin75 wrote: Mon Jun 10, 2024 12:47 am You shouldn't be using doLayout to make modeless dialogs. You can use a button in the tool options (doLayout) instead.
Well, your answer will not explain the problem, I have to create dialogs using script not any button.
User avatar
MehdiZangenehBar
Posts: 114
Joined: Wed Feb 07, 2024 7:17 pm
Contact:

Re: Multiple float dialogs

Post by MehdiZangenehBar »

funny is that if I close any dialog, it will close next created dialogs.
1 will close 1 .. 5
2 will close 2 .. 5
3 will close 3 .. 5
4 will close 4 .. 5
5 will close 5
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Multiple float dialogs

Post by synthsin75 »

If you invoke modeless dialogs from DoLayout, you're asking for instability and crashes. That instance of DoLayout ceases to exist once the tool is changed or entering/leaving frame zero, leaving those dialogs orphaned.
You'd better off using the Run function of a button script to invoke them. But you're welcome to keep testing and prove this for yourself.
User avatar
MehdiZangenehBar
Posts: 114
Joined: Wed Feb 07, 2024 7:17 pm
Contact:

Re: Multiple float dialogs

Post by MehdiZangenehBar »

In my case that's exactly what I want, close ALL created float dialogs and reopen them on DoLayout. Please take a look at this example:

Code: Select all

ScriptName = 'Test_Script'

Test_Script = {}

Test_Script.first_float_dialog = nil

function Test_Script:OnMouseDown(moho, mouseEvent)
end

function Test_Script:DoLayout(moho, simple_dialog_layout)
	if Test_Script.first_float_dialog ~= nil then
		Test_Script.first_float_dialog.image_text_list:SetSelItem(true)
		Test_Script.first_float_dialog = nil
	end
	for i = 1 , 5 do
		local simple_dialog = LM.GUI.SimpleDialog(tostring(i), {})
		local simple_dialog_layout = simple_dialog:GetLayout()
		simple_dialog.image_text_list = LM.GUI.ImageTextList(100, 100, LM.GUI.MSG_OK)
		simple_dialog_layout:AddChild(simple_dialog.image_text_list)
		simple_dialog:DoModeless()
		if Test_Script.first_float_dialog == nil then Test_Script.first_float_dialog = simple_dialog end
	end
end
This code works perfect, but the crash will accure if we close one of the float dialogs by hitting the X button.
I will consider your advice and caution about the DoLayout, but really there is not ANY functionality to handle this? maybe try catch? or any other tip or trick to prevent the crash?
User avatar
MehdiZangenehBar
Posts: 114
Joined: Wed Feb 07, 2024 7:17 pm
Contact:

Re: Multiple float dialogs

Post by MehdiZangenehBar »

I just tested with pcall, it seems it will not crash, would you please check this out on your system:

Code: Select all

ScriptName = 'Test_Script'

Test_Script = {}

Test_Script.first_float_dialog = nil

function Test_Script:OnMouseDown(moho, mouseEvent)
end

function Test_Script:DoLayout(moho, simple_dialog_layout)
	if Test_Script.first_float_dialog ~= nil then
		pcall
		(
			function ()
				Test_Script.first_float_dialog.image_text_list:SetSelItem(Test_Script.first_float_dialog.image_text_list:GetItem(0), false)
			end
		)
		Test_Script.first_float_dialog = nil
	end
	for i = 1 , 5 do
		local simple_dialog = LM.GUI.SimpleDialog(tostring(i), {})
		local simple_dialog_layout = simple_dialog:GetLayout()
		simple_dialog.image_text_list = LM.GUI.ImageTextList(100, 100, LM.GUI.MSG_CANCEL)
		simple_dialog.image_text_list:AddItem("", false)
		simple_dialog_layout:AddChild(simple_dialog.image_text_list, LM.GUI.ALIGN_LEFT, 0)
		simple_dialog:DoModeless()
		if Test_Script.first_float_dialog == nil then Test_Script.first_float_dialog = simple_dialog end
	end
end
User avatar
MehdiZangenehBar
Posts: 114
Joined: Wed Feb 07, 2024 7:17 pm
Contact:

Re: Multiple float dialogs

Post by MehdiZangenehBar »

OK, you right!
it will crash randomly...
I should change my method to the button.
Thanks for your explantion.
Post Reply