Flexible load of layers

This should do the loading of layer presets. It doesn’t copy the state of the parameters though.

-- load handling
-- selectedPath and selectedPreset should not appear in the undo history,
--  so we make them non-persistent, but save and restore them explicitly with
--  onSave and onLoad
defineParameter{name = "selectedPath", default = "", onChanged = function() onSelectPath() end, persistent = false, automatable = false}
defineParameter{name = "selectedPreset", default = "", persistent = false, automatable = false}

function onSave()
	return { selectedPath = selectedPath, selectedPreset = selectedPreset }
end

function onLoad(data)
	if type(data) == "table" then
		-- beware of old tables still filled with other entries
		if data.selectedPath then
			selectedPath = data.selectedPath
		end
		if data.selectedPreset then
			selectedPreset = data.selectedPreset
		end
	end
end

function onUndo()
	-- print("onUndo: updatePreset = ", undoInfo.updatePreset)
	if undoInfo.updatePreset then
		local layer = this.parent:getLayer()
		if layer then
			selectedPreset = layer.name
		end
	end
end

-- pseudo parameter to add a callback into the undo group for replacing the sample layer
defineParameter{name = "undoInfo", default = {}, onChanged = onUndo, automatable = false}


function loadFinished(currentProgressInfo)
	if currentProgressInfo.root then
		startUndoBlock("Load Layer " .. currentProgressInfo.root.name)

		undoInfo = { updatePreset = true }
		this:setParameter("undoInfo", { updatePreset = false }, true)
		
		local layer = this.parent:getLayer()
		
		this.parent:appendLayer(currentProgressInfo.root)

		if layer then
			this.parent:removeLayer(layer)			
		end

		undoInfo = { updatePreset = false }
		this:setParameter("undoInfo", { updatePreset = true }, true)

		endUndoBlock()
	end
end

function onSelectPath()
	loadPresetAsync(selectedPath, loadFinished)
end

Sample Selector.zip (13.8 KB)