lastMorphTexSelCaller = ""

function MorphingEditorWidgetFunc(message)
	local 
		lEditor, lTexSel, lSaveAs, lLoad, lSetXml = 
		GUI:getLayer("MorphingEditorLayer"), GUI:getLayer("MorphTexSelectionLayer"), GUI:getLayer("MorphSaveAsDlgLayer"), GUI:getLayer("MorphLoadDlgLayer"), GUI:getLayer("MorphSetXmlDBLayer")
	local wEditor, wGrid1, wGrid2 = lEditor:getWidget("MorphingEditor"), lEditor:getWidget("MorphingGrid1"), lEditor:getWidget("MorphingGrid2") 
	
	local function SelectTexture(target_grid)
		lastMorphTexSelCaller = target_grid
		wEditor:AcceptMessage(Message("set focus",target_grid))
		wEditor:AcceptMessage(Message("move tex selector",target_grid))
		Screen:pushLayer(lTexSel)
	end

	if message:is("escape") or message:is("Exit","press") then
		wEditor:AcceptMessage(Message("escape"))
	elseif message:is("exit morphing editor") then 
		-- сюда можно попасть через обработчик "escape" в С++-коде
		Screen:Clear()
		Screen:pushLayer(GUI:getLayer("MainMenu"))
	elseif (message:is("add nodes") or message:is("AddNodes","press")) then
		wEditor:AcceptMessage(Message("grid add nodes"))
	elseif (message:is("del nodes") or message:is("DelNodes","press")) then
		wEditor:AcceptMessage(Message("grid del nodes"))
	elseif (message:is("connect nodes") or message:is("AddEdges","press")) then
		wEditor:AcceptMessage(Message("grid connect nodes"))
	elseif (message:is("disconnect nodes") or message:is("DelEdges","press")) then
		wEditor:AcceptMessage(Message("grid disconnect nodes"))
	elseif (message:is("auto edges") or message:is("AutoEdges","press")) then
		wEditor:AcceptMessage(Message("grid auto edges"))
	elseif (message:is("move nodes") or message:is("MoveNodes","press")) then
		wEditor:AcceptMessage(Message("grid move nodes"))
	elseif (message:is("duplicate grid") or message:is("DupGrid","press")) then
		wEditor:AcceptMessage(Message("duplicate grid"))
	elseif (message:is("SelectTexture1","press")) then
		SelectTexture("MorphingGrid1")
	elseif (message:is("SelectTexture2","press")) then
		SelectTexture("MorphingGrid2")
	elseif (message:is("switch test morpher") or message:is("Play","press")) then
		wEditor:AcceptMessage(Message("switch test morpher"))
	elseif (message:is("save") or message:is("Save","press")) then
		if wEditor:QueryState(Message("can play")):getIntegerParam() == 1 then
			Screen:pushLayer(lSaveAs)
		end
	elseif (message:is("load") or message:is("Load","press")) then
		wEditor:AcceptMessage(Message("init load dlg"))
		Screen:pushLayer(lLoad)
	elseif (message:is("clear") or message:is("Clear","press")) then
		wEditor:AcceptMessage(Message("clear"))
	elseif (message:is("set xmldb") or message:is("SetXmlDB","press")) then
		Screen:pushLayer(lSetXml)
		messageManager:putMessage(Message("Refresh name"))
	elseif (message:is("NodeColor0","press")) then
		wEditor:AcceptMessage(Message("node color",0x00FF00))
	elseif (message:is("NodeColor1","press")) then
		wEditor:AcceptMessage(Message("node color",0x00C0FF))
	elseif (message:is("NodeColor2","press")) then
		wEditor:AcceptMessage(Message("node color",0xFFD200))
	elseif (message:is("IncLifetime","press")) then
		wEditor:AcceptMessage(Message("lifetime","inc"))
	elseif (message:is("DecLifetime","press")) then
		wEditor:AcceptMessage(Message("lifetime","dec"))
	elseif (message:is("SrcFading","Change")) then
		wEditor:AcceptMessage(Message("src fading state",lEditor:getWidget("SrcFading"):QueryState(Message("GetState")):getData()))
	elseif (message:is("FlipH","Change")) then
		wEditor:AcceptMessage(Message("fliph state",lEditor:getWidget("FlipH"):QueryState(Message("GetState")):getData()))
	elseif (message:is("FlipV","Change")) then
		wEditor:AcceptMessage(Message("flipv state",lEditor:getWidget("FlipV"):QueryState(Message("GetState")):getData()))
	elseif (message:is("TimeSmoothing","Change")) then
		wEditor:AcceptMessage(Message("timesmoothing state",lEditor:getWidget("TimeSmoothing"):QueryState(Message("GetState")):getData()))
	elseif (message:is("f1 pressed")) then
		morphMap:Reload("MorphersDB.xml") -- а вдруг что-то изменилось
		Screen:Clear()
		Screen:pushLayer(GUI:getLayer("BookLayer"))
		GUI:getLayer("BookLayer"):getWidget("BookWidget"):AcceptMessage(Message("ReloadMorphers"))
	elseif message:is("zoom in") or message:is("zoom out")
		or message:is("cliprect left") or message:is("cliprect right")
		or message:is("cliprect down") or message:is("cliprect up") then
		wEditor:AcceptMessage(message)
	end
end

function MorphTexSelectionFunc(message)
	local texList = GUI:getLayer("MorphTexSelectionLayer"):getWidget("Textures")
	if message:is("Close","press") then
		Screen:popLayer()
	elseif message:is("ScrollUp","press") or message:is("UpArrow") then
		texList:AcceptMessage(Message("ScrollUp"))
	elseif message:is("ScrollDown","press") or message:is("DownArrow") then
		texList:AcceptMessage(Message("ScrollDown"))
	elseif message:is("Ok", "press") then
		local tex_id = texList:QueryState(Message("CurrentItem")):getData()
		if tex_id ~= "" then
			GUI:getLayer("MorphingEditorLayer"):getWidget(lastMorphTexSelCaller):AcceptMessage(Message("change texture",tex_id))
			Screen:popLayer()
		end
	end
end

function MorphSaveAsDlgFunc(message)
	local namebox = GUI:getLayer("MorphSaveAsDlgLayer"):getWidget("Name")
	if message:is("Close","press") then
		Screen:popLayer()
	elseif message:is("Ok", "press") then
		local name = namebox:QueryState(Message("Text")):getData()
		if name ~= "" then
			GUI:getLayer("MorphingEditorLayer"):getWidget("MorphingEditor"):AcceptMessage(Message("save",name))
			Screen:popLayer()
		end
	end
end

function MorphSetXmlDBFunc(message)
	local namebox = GUI:getLayer("MorphSetXmlDBLayer"):getWidget("Name")
	if message:is("Refresh name") then
		namebox:AcceptMessage(Message(
			"Set",
			GUI:getLayer("MorphingEditorLayer"):getWidget("MorphingEditor"):QueryState(Message("current xmldb")):getData()
		))
	elseif message:is("Close","press") then
		Screen:popLayer()
	elseif message:is("Ok", "press") then
		local name = namebox:QueryState(Message("Text")):getData()
		if name ~= "" then
			GUI:getLayer("MorphingEditorLayer"):getWidget("MorphingEditor"):AcceptMessage(Message("set xmldb", name))
			Screen:popLayer()
		end
	end
end

function MorphOpenDlgFunc(message)
	local morphList = GUI:getLayer("MorphLoadDlgLayer"):getWidget("Morphers")
	if message:is("Close","press") then
		Screen:popLayer()
	elseif message:is("ScrollUp","press") or message:is("UpArrow") then
		morphList:AcceptMessage(Message("ScrollUp"))
	elseif message:is("ScrollDown","press") or message:is("DownArrow") then
		morphList:AcceptMessage(Message("ScrollDown"))
	elseif message:is("Ok", "press") then
		local morpher = morphList:QueryState(Message("CurrentItem")):getData()
		if morpher ~= "" then
			GUI:getLayer("MorphingEditorLayer"):getWidget("MorphingEditor"):AcceptMessage(Message("load",morpher))
			GUI:getLayer("MorphSaveAsDlgLayer"):getWidget("Name"):AcceptMessage(Message("Set",morpher))
			Screen:popLayer()
		end
	end
end
