I tried to fix Fazek's Line Replace tool for v10

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

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
Breinmeester
Posts: 303
Joined: Thu May 13, 2010 2:01 pm

I tried to fix Fazek's Line Replace tool for v10

Post by Breinmeester »

...but failed. Did anyone else have better luck (or skills)???
User avatar
synthsin75
Posts: 9934
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: I tried to fix Fazek's Line Replace tool for v10

Post by synthsin75 »

Anywhere you find:
table.getn(*name of table*)
Example: table.getn(tbl)

You need to replace it with:
#*name of table*
Example: #tbl


string.gfind was changed to string.gmatch
Breinmeester
Posts: 303
Joined: Thu May 13, 2010 2:01 pm

Re: I tried to fix Fazek's Line Replace tool for v10

Post by Breinmeester »

Yeah, i found out about that and changed all the getn() to #. But then the preview still didn't work. So i tried editing in the DrawMe(moho, view) of the Freehand tool, but that didn't work. And also some of the interpolations between the points went wonky where they didn't used to. I only gave it about half an hour to an hour, so i didn't really dive in yet, but i'm also not the best coder on the forums, so i was hoping someone had fixed it already.
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Re: I tried to fix Fazek's Line Replace tool for v10

Post by heyvern »

I thought I had all of Fazek's tools but can't find this one. I have a huge folder with a bazillion on his scripts but nothing called "replace line".

Can you tell me exactly what the name is? It could be it's called something I am not looking for correctly. ;)
Or if you could post it or point me to a download.

hopefully I should be able to fix it without too much trouble.

NEVER MIND!!! I found it! :)
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Re: I tried to fix Fazek's Line Replace tool for v10

Post by heyvern »

It works fine for me after the changes. No errors.

Can you explain what you mean about interpolations going wonky and the preview not working?

I never used this tool before so don't know exactly how it is suppose to work.

Below is the corrected code. You can just copy and paste it.

Code: Select all

- **************************************************
-- Provide Moho with the name of this script object
-- **************************************************

ScriptName = "FA_ReplaceLine"

-- **************************************************
-- General information about this script
-- **************************************************

FA_ReplaceLine = {}

function FA_ReplaceLine:Name()
	return "Replace Line"
end

function FA_ReplaceLine:Version()
	return "5.0"
end

function FA_ReplaceLine:Description()
	return "Replace the line of selected points with a mouse stroke (hold <shift> to add selection)"
end

function FA_ReplaceLine:Creator()
	return "Lost Marble, Modified by Fazek, from Heyvern's idea"
end

function FA_ReplaceLine:UILabel()
	return("Replace Line")
end

function FA_ReplaceLine:LoadPrefs(prefs)
	self.widthMode = prefs:GetInt("FA_ReplaceLine.widthMode", 0)
	self.minWidth = prefs:GetFloat("FA_ReplaceLine.minWidth", 1)
	self.maxWidth = prefs:GetFloat("FA_ReplaceLine.maxWidth", 8)
	self.keepEnds = prefs:GetBool("FA_ReplaceLine.keepEnds", false)
	self.widthOnly = prefs:GetBool("FA_ReplaceLine.widthOnly", false)
end

function FA_ReplaceLine:SavePrefs(prefs)
	prefs:SetInt("FA_ReplaceLine.widthMode", self.widthMode)
	prefs:SetFloat("FA_ReplaceLine.minWidth", self.minWidth)
	prefs:SetFloat("FA_ReplaceLine.maxWidth", self.maxWidth)
	prefs:SetBool("FA_ReplaceLine.keepEnds", self.keepEnds)
	prefs:SetBool("FA_ReplaceLine.widthOnly", self.widthOnly)
end

-- **************************************************
-- Recurring values
-- **************************************************

FA_ReplaceLine.delayed= false
FA_ReplaceLine.firstVecX= 0
FA_ReplaceLine.firstVecY= 0
FA_ReplaceLine.lastVecX= 0
FA_ReplaceLine.lastVecY= 0
FA_ReplaceLine.lastDrawX= 0
FA_ReplaceLine.lastDrawY= 0
FA_ReplaceLine.pointTab= nil
FA_ReplaceLine.selID= -1

FA_ReplaceLine.widthMode = 0 -- 0: none, 1: pen pressure, 2: random, 3: constant
FA_ReplaceLine.minWidth = 1
FA_ReplaceLine.maxWidth = 8
FA_ReplaceLine.keepEnds = false
FA_ReplaceLine.widthOnly = false

-- **************************************************
-- The guts of this script
-- **************************************************

function FA_ReplaceLine:IsEnabled(moho)
	if (moho:CountPoints() > 0) then
		return true
	end
	return false
end

function FA_ReplaceLine:OnMouseDown(moho, mouseEvent)
	local mesh = moho:Mesh()
	if (mesh == nil) then return end

	self.delayed= true
	local ptID= mouseEvent.view:PickPoint(mouseEvent.pt)

	if (mouseEvent.shiftKey or (moho:CountSelectedPoints() < 1 and ptID < 0)) then
		FA_SharedUtils.selectionMode= 0
		FA_SharedUtils:MouseDown(moho, mouseEvent)
		return
	end

	if (ptID >= 0) then
		--translate point function, selection not changed!

		moho.document:PrepUndo(moho.layer)
		moho.document:SetDirty()
		local pt= mesh:Point(ptID)
		pt.fSelected= true
		pt.fTempPos:Set(pt.fPos)

		--moho:AddPointKeyframe(moho.frame, nil, true)						-- trying to fix AS9
		--moho:NewKeyframe(CHANNEL_POINT)


	end
	self.selID= ptID
	self.previewWidth = moho:PixelToDoc(1)
	FA_SharedUtils.selectionMode= -1
	mouseEvent.view:DrawMe()
	return
end

function FA_ReplaceLine:OnMouseMoved(moho, mouseEvent)

	if (FA_SharedUtils.selectionMode >= 0) then
		FA_SharedUtils:MouseMoved(moho,mouseEvent)
		return
	end

	local mesh = moho:Mesh()
	if (mesh == nil) then return end

	local ptWidth
	local wm= self.widthMode

	if (self.delayed) then

		if (self.selID > -1) then
	--translate point mode
			local pt= mesh:Point(self.selID)
			local vec= mouseEvent.vec - mouseEvent.startVec + pt.fTempPos
			mesh:Point(self.selID).fPos:Set(vec)
			mouseEvent.view:DrawMe()
			return
		end
		local x= mouseEvent.startPt.x - mouseEvent.pt.x
		local y= mouseEvent.startPt.y - mouseEvent.pt.y

		if ((x*x + y*y) < 9) then return end

		self.lastVecX= mouseEvent.startVec.x
		self.lastVecY= mouseEvent.startVec.y
		self.firstVecX= self.lastVecX
		self.firstVecY= self.lastVecY
		self.lastDrawX= mouseEvent.startPt.x
		self.lastDrawY= mouseEvent.startPt.y
		self.delayed= false
		self.pointTab= {}

		if (wm > 0) then
			ptWidth= 0.0

			if (wm == 1) then

				if (mouseEvent.penPressure > 0) then
					ptWidth= mouseEvent.penPressure
				end
			elseif (wm == 2) then
				ptWidth= math.random()
			end
			ptWidth= LM.Lerp(ptWidth,self.minWidth,self.maxWidth)
			ptWidth= moho:PixelToDoc(ptWidth)

			table.insert(self.pointTab,{ self.lastVecX, self.lastVecY,ptWidth })
		else
			table.insert(self.pointTab,{ self.lastVecX, self.lastVecY })
		end
	end
	local x= mouseEvent.pt.x - self.lastDrawX
	local y= mouseEvent.pt.y - self.lastDrawY

	if ((x*x + y*y) >= 9) then
		self.lastVecX= mouseEvent.vec.x
		self.lastVecY= mouseEvent.vec.y

		if (wm > 0) then
			ptWidth= 0.0

			if (wm == 1) then

				if (mouseEvent.penPressure > 0) then
					ptWidth= mouseEvent.penPressure
				end
			elseif (wm == 2) then
				ptWidth= math.random()
			end
			ptWidth= LM.Lerp(ptWidth,self.minWidth,self.maxWidth)
			ptWidth= moho:PixelToDoc(ptWidth)
			table.insert(self.pointTab,{ self.lastVecX, self.lastVecY,ptWidth })
			self.previewWidth= ptWidth
		else
			table.insert(self.pointTab,{ self.lastVecX, self.lastVecY })
		end
	end

-- preview

	local gfx = mouseEvent.view:Graphics()
	x= (gfx:Height() / moho.document:Height()) * 0.85
	x= moho:DocToPixel(self.previewWidth) * gfx:CurrentScale(false) * x

	gfx:SetSmoothing(true)
	gfx:Push()
	local m = gfx:CurrentTransform()
	m:Invert()
	gfx:ApplyMatrix(m)
	gfx:SetColor(255, 0, 0)
	gfx:SetPenWidth(x)
	gfx:MoveTo(self.lastDrawX, self.lastDrawY)
	gfx:LineTo(mouseEvent.pt.x, mouseEvent.pt.y)
	gfx:Pop()
	gfx:SetSmoothing(false)

	mouseEvent.view:RefreshView()

	self.lastDrawX = mouseEvent.pt.x
	self.lastDrawY = mouseEvent.pt.y
end

function FA_ReplaceLine:OnMouseUp(moho, mouseEvent)

	if (FA_SharedUtils.selectionMode >= 0) then
		FA_SharedUtils:MouseUp(moho,mouseEvent)
		return
	end
	local mesh = moho:Mesh()
	if (mesh == nil) then return end

	moho.document:PrepUndo(moho.layer)
	moho.document:SetDirty()

	if (self.delayed) then

		if (self.selID > -1) then
	--translate point mode
			moho:AddPointKeyframe(moho.frame, nil, true)						-- fix for AS9 MKE
			moho:NewKeyframe(CHANNEL_POINT)
		else
			mesh:SelectNone()
		end
		mouseEvent.view:DrawMe()
		return
	end
	self.lastVecX= mouseEvent.vec.x
	self.lastVecY= mouseEvent.vec.y
	local wm= self.widthMode

	if (wm > 0) then
		local ptWidth= 0.0

		if (wm == 1) then

			if (mouseEvent.penPressure > 0) then
				ptWidth= mouseEvent.penPressure
			end
		elseif (wm == 2) then
			ptWidth= math.random()
		end
		ptWidth= LM.Lerp(ptWidth,self.minWidth,self.maxWidth)
		ptWidth= moho:PixelToDoc(ptWidth)
		table.insert(self.pointTab,{ self.lastVecX, self.lastVecY,ptWidth })
		self.previewWidth= ptWidth
	else
		table.insert(self.pointTab,{ self.lastVecX, self.lastVecY })
	end

-- count the summed distance between the selected points

	local selLine= {}

	if (self:GetPointsLine(moho, mesh, selLine)) then
		-- closed curve

		table.insert(selLine,selLine[1])
	end
	local ptSum= 0
	local pt
	local x,y,x0,y0,x1,y1,sx,sy
	local n= #selLine

	x0= selLine[1][1]
	y0= selLine[1][2]

	for i= 2, n do
		sx= selLine[i][1]
		sy= selLine[i][2]
		x= sx - x0
		y= sy - y0
		ptSum= ptSum + math.sqrt(x*x + y*y)
		x0= sx
		y0= sy
	end

-- count the length of the new line

	local nSum= 0
	x1= self.pointTab[1][1]
	y1= self.pointTab[1][2]
	local pN= #self.pointTab

	-- pN is at least 2

	for i= 2, pN do
		x= self.pointTab[i][1] - x1
		y= self.pointTab[i][2] - y1

		nSum= nSum + math.sqrt(x*x + y*y)
		x1= x1 + x
		y1= y1 + y
	end

-- interpolate

	local tab1= {}
	local tab2= {}
	local err1,err2

	x1= self.pointTab[1][1]
	y1= self.pointTab[1][2]
	x0= selLine[1][1]
	y0= selLine[1][2]

	if (wm > 0) then
		table.insert(tab1,{x1,y1,self.pointTab[1][3]})
	else
		table.insert(tab1,{x1,y1})
	end
	x= x1 - x0
	y= y1 - y0
	err1= x*x + y*y
	err2= err1 + 1

	local ps, ns, q, next

	if (ptSum > 0) then

	--straight order

		ps= 0
		ns= 0
		q= 2	--index 2 always exists

		nSum= nSum / ptSum

		for i= 2, n - 1 do
			sx= selLine[i][1]
			sy= selLine[i][2]
			x= sx - x0
			y= sy - y0
			ps= ps + math.sqrt(x*x + y*y)
			x0= sx
			y0= sy
			ptSum= ps * nSum

			while (true) do
				x= self.pointTab[q][1] - x1
				y= self.pointTab[q][2] - y1
				next= ns + math.sqrt(x*x + y*y)

				if (next >= ptSum or q >= pN) then
					break
				end
				q= q + 1
				x1= x1 + x
				y1= y1 + y
				ns= next
			end

		--linear interpolation

			if (wm > 0) then
				local w= self.pointTab[q - 1][3]

				if (next > ns) then
					next= (ptSum - ns) / (next - ns)
					x= x1 + x*next
					y= y1 + y*next
					w= w + (self.pointTab[q][3] - w)*next
				else
					x= x1
					y= y1
				end
				table.insert(tab1,{x,y,w})
			else

				if (next > ns) then
					next= (ptSum - ns) / (next - ns)
					x= x1 + x*next
					y= y1 + y*next
				else
					x= x1
					y= y1
				end
				table.insert(tab1,{x,y})
			end
			x= x - sx
			y= y - sy
			err1= err1 + x*x + y*y
		end
		sx= self.pointTab[pN][1]
		sy= self.pointTab[pN][2]
		x= sx - selLine[n][1]
		y= sy - selLine[n][2]
		err1= err1 + x*x + y*y

		if (wm > 0) then
			table.insert(tab1,{sx,sy,self.pointTab[pN][3]})
		else
			table.insert(tab1,{sx,sy})
		end

	--reverse order

		x1= self.pointTab[1][1]
		y1= self.pointTab[1][2]
		x0= selLine[n][1]
		y0= selLine[n][2]

		if (wm > 0) then
			table.insert(tab2,{x1,y1,self.pointTab[1][3]})
		else
			table.insert(tab2,{x1,y1})
		end
		x= x1 - x0
		y= y1 - y0
		err2= x*x + y*y

		ps= 0
		ns= 0
		q= 2	--index 2 always exists

		for i= n - 1, 2, -1 do
			sx= selLine[i][1]
			sy= selLine[i][2]
			x= sx - x0
			y= sy - y0
			ps= ps + math.sqrt(x*x + y*y)
			x0= sx
			y0= sy
			ptSum= ps * nSum

			while (true) do
				x= self.pointTab[q][1] - x1
				y= self.pointTab[q][2] - y1
				next= ns + math.sqrt(x*x + y*y)

				if (next >= ptSum or q >= pN) then
					break
				end
				q= q + 1
				x1= x1 + x
				y1= y1 + y
				ns= next
			end

		--linear interpolation

			if (wm > 0) then
				local w= self.pointTab[q - 1][3]

				if (next > ns) then
					next= (ptSum - ns) / (next - ns)
					x= x1 + x*next
					y= y1 + y*next
					w= w + (self.pointTab[q][3] - w)*next
				else
					x= x1
					y= y1
				end
				table.insert(tab2,{x,y,w})
			else
				if (next > ns) then
					next= (ptSum - ns) / (next - ns)
					x= x1 + x*next
					y= y1 + y*next
				else
					x= x1
					y= y1
				end
				table.insert(tab2,{x,y})
			end
			x= x - sx
			y= y - sy
			err2= err2 + x*x + y*y
		end
		sx= self.pointTab[pN][1]
		sy= self.pointTab[pN][2]
		x= sx - selLine[1][1]
		y= sy - selLine[1][2]
		err2= err2 + x*x + y*y

		if (wm > 0) then
			table.insert(tab2,{sx,sy,self.pointTab[pN][3]})
		else
			table.insert(tab2,{sx,sy})
		end
	end

	--which order is better?

	if (err1 < err2) then
	--straight order
		x= 1
		y= 1
	else
	--reverse order
		tab1= tab2
		x= n
		y= -1
	end

	if (wm > 0) then
		pt= mesh:Point(selLine[x][3])

		if (self.widthOnly) then
--change only the width, not position
			if (not self.keepEnds) then
				pt.fWidth:SetValue(moho.frame, tab1[1][3])
			end

			for j= 2, n - 1 do
				x= x + y
				pt= mesh:Point(selLine[x][3])
				pt.fWidth:SetValue(moho.frame, tab1[j][3])
			end
			x= x + y

			pt= mesh:Point(selLine[x][3])
		else
--width and position
			pt.fPos.x= tab1[1][1]
			pt.fPos.y= tab1[1][2]

			if (not self.keepEnds) then
				pt.fWidth:SetValue(moho.frame, tab1[1][3])
			end

			for j= 2, n - 1 do
				x= x + y
				pt= mesh:Point(selLine[x][3])
				pt.fPos.x= tab1[j][1]
				pt.fPos.y= tab1[j][2]
				pt.fWidth:SetValue(moho.frame, tab1[j][3])
			end
			x= x + y

			pt= mesh:Point(selLine[x][3])
			pt.fPos.x= tab1[n][1]
			pt.fPos.y= tab1[n][2]
		end

		if (not self.keepEnds) then
			pt.fWidth:SetValue(moho.frame, tab1[n][3])
		end

	--	moho:AddPointKeyframe(moho.frame)						-- trying to fix AS9
	--	moho:NewKeyframe(CHANNEL_POINT)

	else
--position only
		for j= 1, n do
			pt= mesh:Point(selLine[x][3])
			pt.fPos.x= tab1[j][1]
			pt.fPos.y= tab1[j][2]
			x= x + y

	--	moho:AddPointKeyframe(moho.frame)						-- trying to fix AS9
	--	moho:NewKeyframe(CHANNEL_POINT)

		end
	end
	self.pointTab= nil

		mouseEvent.view:RefreshView()

		moho:AddPointKeyframe(moho.frame, nil, true)						-- trying to fix AS9
		moho:NewKeyframe(CHANNEL_POINT)
																			-- the selected frame works alright.
																			-- but all points on all other selected frames are offset - equally.  Why?
	mouseEvent.view:DrawMe()
end

function FA_ReplaceLine:GetPointsLine(moho, mesh, selLine)

-- parse the points and create a continuous line from them

	-- indexing itself

	local cveParts= {}
	local tab,tN,wN, pt
	local full= false

	for i= 0, mesh:CountPoints() - 1 do
		mesh:Point(i).fTempPos.x= i
		mesh:Point(i).fTempPos.y= 0
	end

	for i= 0, mesh:CountCurves() - 1 do
		local cve= mesh:Curve(i)

		-- mark the internal curve points to avoid point duplications

		tN= 0

		for j= 0, cve:CountPoints() do
			if (cve:Point(j).fSelected) then
				tN= tN + 1
			end
		end

		if (cve.fClosed) then

			for j= 0, cve:CountPoints() - 1 do
				pt= cve:Point(j)

				if (pt.fSelected) then
					pt.fTempPos.y= tN
				end
			end
		else
			for j= 1, cve:CountPoints() - 2 do
				pt= cve:Point(j)

				if (pt.fSelected and pt.fTempPos.y < tN) then
					pt.fTempPos.y= tN
				end
			end
		end
	end

	local x,y,err, bestErr, best

	for i= 0, mesh:CountCurves() - 1 do
		local cve= mesh:Curve(i)
		tab= nil
		tN= 0

		for j= 0, cve:CountPoints() do
			if (cve:Point(j).fSelected) then
				tN= tN + 1
			end
		end

		wN= cve:CountPoints() - 1

		if (cve.fClosed) then
--roundover?
			local min= -1

			for j= wN, 0, -1 do

				if (not cve:Point(j).fSelected) then
					min= j
					break
				end
			end

			if (min < 0) then

				--full roundover, find the closest point to the start

				best= nil

				for j= 0, wN do
					pt= cve:Point(j)

					if (pt.fSelected) then
						x= pt.fPos.x - self.firstVecX
						y= pt.fPos.y - self.firstVecY
						err= x*x + y*y

						if (not best or err < bestErr) then
							bestErr= err
							best= j
						end
					end
				end

				if (best) then
					min= best - 1
					full= true
				else
					min= -1
				end
			end

			for j= min + 1, wN do
				pt= cve:Point(j)

				if (pt.fSelected and tN >= pt.fTempPos.y) then

					if (not tab) then
						tab= {}
					end
					table.insert(tab,{ pt.fPos.x,pt.fPos.y,pt.fTempPos.x })
				end
			end

			for j= 0, min do
				pt= cve:Point(j)

				if (pt.fSelected and tN >= pt.fTempPos.y) then

					if (not tab) then
						tab= {}
					end
					table.insert(tab,{ pt.fPos.x,pt.fPos.y,pt.fTempPos.x })
				end
			end
		else
			for j= 0, wN do
				pt= cve:Point(j)

				if (pt.fSelected and (pt.fTempPos.y == 0 or (j ~= 0 and j ~= wN and tN >= pt.fTempPos.y))) then

					if (not tab) then
						tab= {}
					end
					table.insert(tab,{ pt.fPos.x,pt.fPos.y,pt.fTempPos.x })
				end
			end
		end

		if (tab) then
			table.insert(cveParts,tab)
		end
	end

	local n= #cveParts

	if (n < 1) then return full end

	-- ordering the connecting lines

	for i= 2, n do
		local bestInv= false
		local wtab= cveParts[i]
		wN= #wtab
		bestErr= 1
		best= nil

		for j= 1, i - 1 do
			tab= cveParts[j]
			tN= #tab

			if (tab[1][3] == wtab[wN][3]) then
				best= j
				bestInv= false
				bestErr= 0
			elseif (tab[tN][3] == wtab[1][3]) then
				best= j + 1
				bestInv= false
				bestErr= 0
			elseif (tab[1][3] == wtab[1][3]) then
				best= j
				bestInv= true
				bestErr= 0
			elseif (tab[tN][3] == wtab[wN][3]) then
				best= j + 1
				bestInv= true
				bestErr= 0
			elseif (bestErr > 0) then
				x= tab[1][1] - wtab[wN][1]
				y= tab[1][2] - wtab[wN][2]
				err= x*x + y*y

				if (not best or err < bestErr) then
					best= j
					bestInv= false
					bestErr= err
				end
				x= tab[tN][1] - wtab[1][1]
				y= tab[tN][2] - wtab[1][2]
				err= x*x + y*y

				if (err < bestErr) then
					best= j + 1
					bestInv= false
					bestErr= err
				end
				x= tab[1][1] - wtab[1][1]
				y= tab[1][2] - wtab[1][2]
				err= x*x + y*y

				if (not best or err < bestErr) then
					best= j
					bestInv= true
					bestErr= err
				end
				x= tab[tN][1] - wtab[wN][1]
				y= tab[tN][2] - wtab[wN][2]
				err= x*x + y*y

				if (err < bestErr) then
					best= j + 1
					bestInv= true
					bestErr= err
				end
			end
		end

		if (best) then

			if (bestInv) then

				--reverse order

				for i= 1, wN / 2 do
					local t= wtab[i]
					wtab[i] = wtab[wN - i + 1]
					wtab[wN - i + 1]= t
				end
			end
			table.remove(cveParts,i)
			table.insert(cveParts,best,wtab)
		end
	end

	-- build the table

	for i= 1, n do
		tab= cveParts[i]
		tN= #tab

		if (tab[1][3] == err) then
			for j= 2, tN do
				table.insert(selLine,tab[j])
			end
		else
			for j= 1, tN do
				table.insert(selLine,tab[j])
			end
		end
		err= tab[tN][3]
	end
	return full
end

-- **************************************************
-- Replace Line dialog
-- **************************************************

local FA_ReplaceLineDialog = {}

function FA_ReplaceLineDialog:new()
	local dlg = LM.GUI.SimpleDialog("Line Options", FA_ReplaceLineDialog)
	local l = dlg:GetLayout()

	l:AddChild(LM.GUI.StaticText("Variable line width:"), LM.GUI.ALIGN_LEFT)
	l:Indent()

	dlg.lineWidthNone = LM.GUI.RadioButton("Keep original", FA_ReplaceLine.PRESSURE)
	l:AddChild(dlg.lineWidthNone, LM.GUI.ALIGN_LEFT)
	dlg.lineWidthPressure = LM.GUI.RadioButton("Use pen pressure", FA_ReplaceLine.PRESSURE)
	l:AddChild(dlg.lineWidthPressure, LM.GUI.ALIGN_LEFT)
	dlg.lineWidthRandom = LM.GUI.RadioButton("Random", FA_ReplaceLine.PRESSURE)
	l:AddChild(dlg.lineWidthRandom, LM.GUI.ALIGN_LEFT)
	dlg.lineWidthFixed = LM.GUI.RadioButton("Fixed", FA_ReplaceLine.PRESSURE)
	l:AddChild(dlg.lineWidthFixed, LM.GUI.ALIGN_LEFT)

	l:PushH()
		l:Unindent()
		l:PushV()
			l:AddChild(LM.GUI.StaticText(FA_SharedUtils.txMinWidth), LM.GUI.ALIGN_LEFT)
			l:AddChild(LM.GUI.StaticText(FA_SharedUtils.txMaxWidth), LM.GUI.ALIGN_LEFT)
		l:Pop()

		l:PushV()
			dlg.minText = LM.GUI.TextControl(0, "00.000", 0, LM.GUI.FIELD_UFLOAT)
			dlg.minText:SetWheelInc(1)
			l:AddChild(dlg.minText, LM.GUI.ALIGN_LEFT)
			dlg.maxText = LM.GUI.TextControl(0, "00.000", 0, LM.GUI.FIELD_UFLOAT)
			dlg.maxText:SetWheelInc(1)
			l:AddChild(dlg.maxText, LM.GUI.ALIGN_LEFT)
		l:Pop()
	l:Pop()

	dlg.keepEndsCheck = LM.GUI.CheckBox("Keep width of ends")
	l:AddChild(dlg.keepEndsCheck, LM.GUI.ALIGN_LEFT)

	l:AddChild(LM.GUI.Button(FA_SharedUtils.txReset, FA_ReplaceLine.RESET))
	return dlg
end

function FA_ReplaceLineDialog:UpdateWidgets()

	if (FA_ReplaceLine.widthMode == 1) then
		self.lineWidthPressure:SetValue(true)
	elseif (FA_ReplaceLine.widthMode == 2) then
		self.lineWidthRandom:SetValue(true)
	elseif (FA_ReplaceLine.widthMode == 3) then
		self.lineWidthFixed:SetValue(true)
	else
		self.lineWidthNone:SetValue(true)
	end
	self.minText:SetValue(FA_ReplaceLine.minWidth)
	self.maxText:SetValue(FA_ReplaceLine.maxWidth)
	self.keepEndsCheck:SetValue(FA_ReplaceLine.keepEnds)
	self.minText:Enable(FA_ReplaceLine.widthMode > 0)
	self.maxText:Enable(FA_ReplaceLine.widthMode ~= 0 and FA_ReplaceLine.widthMode ~= 3)
	self.keepEndsCheck:Enable(FA_ReplaceLine.widthMode > 0)
	FA_ReplaceLine.widthOnlyCheck:Enable(FA_ReplaceLine.widthMode > 0)
end

function FA_ReplaceLineDialog:OnValidate()
	local b = true
	if (not self:Validate(self.minText, 0, 32)) then
		b = false
	end
	if (not self:Validate(self.maxText, self.minText:FloatValue(), 32)) then
		b = false
	end
	return b
end

function FA_ReplaceLineDialog:OnOK()

	if (self.lineWidthPressure:Value()) then
		FA_ReplaceLine.widthMode = 1
	elseif (self.lineWidthRandom:Value()) then
		FA_ReplaceLine.widthMode = 2
	elseif (self.lineWidthFixed:Value()) then
		FA_ReplaceLine.widthMode = 3
	else
		FA_ReplaceLine.widthMode = 0
	end
	FA_ReplaceLine.minWidth = self.minText:FloatValue()
	FA_ReplaceLine.maxWidth = self.maxText:FloatValue()
	FA_ReplaceLine.keepEnds = self.keepEndsCheck:Value()
end

function FA_ReplaceLineDialog:HandleMessage(msg)
	if (msg == FA_ReplaceLine.PRESSURE) then
		local en= (not self.lineWidthNone:Value())

		self.minText:Enable(en)
		self.maxText:Enable(not (self.lineWidthNone:Value() or self.lineWidthFixed:Value()))
		self.keepEndsCheck:Enable(en)
		FA_ReplaceLine.widthOnlyCheck:Enable(en)
	elseif (msg == FA_ReplaceLine.RESET) then
		FA_ReplaceLine.widthMode = 0
		FA_ReplaceLine.minWidth = 1
		FA_ReplaceLine.maxWidth = 8
		FA_ReplaceLine.keepEnds = false
		self:UpdateWidgets()
	end
end

-- **************************************************
-- Tool options - create and respond to tool's UI
-- **************************************************

FA_ReplaceLine.DUMMY = MOHO.MSG_BASE
FA_ReplaceLine.PRESSURE = MOHO.MSG_BASE + 1
FA_ReplaceLine.RESET = MOHO.MSG_BASE + 2
FA_ReplaceLine.WIDTHONLY = MOHO.MSG_BASE + 3
FA_ReplaceLine.SELECTITEM = MOHO.MSG_BASE + 4

function FA_ReplaceLine:DoLayout(moho, layout)
	self.menu = LM.GUI.Menu(FA_SharedUtils.txSelectGroup)

	self.popup = LM.GUI.PopupMenu(128, false)
	self.popup:SetMenu(self.menu)
	layout:AddChild(self.popup)

	self.dlog = FA_ReplaceLineDialog:new()
	self.dlpopup = LM.GUI.PopupDialog("Line Options")
	self.dlpopup:SetDialog(self.dlog)
	layout:AddChild(self.dlpopup)

	self.widthOnlyCheck = LM.GUI.CheckBox("Line width only",self.WIDTHONLY)
	self.widthOnlyCheck:SetValue(self.widthOnly)
	self.widthOnlyCheck:Enable(self.widthMode > 0)
	layout:AddChild(self.widthOnlyCheck, LM.GUI.ALIGN_LEFT)
end

function FA_ReplaceLine:UpdateWidgets(moho)
	local mesh = moho:Mesh()
	if (mesh == nil) then return end

	MOHO.BuildGroupMenu(self.menu, mesh, self.SELECTITEM, self.DUMMY)
end

function FA_ReplaceLine:HandleMessage(moho, view, msg)
	local mesh = moho:Mesh()
	if (mesh == nil) then
		return
	end

	if (msg >= self.SELECTITEM) then
		FA_SharedUtils:SelectItem(moho, mesh, msg - self.SELECTITEM)
	elseif (msg == self.WIDTHONLY) then
		self.widthOnly= self.widthOnlyCheck:Value()
	end
end-
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Re: I tried to fix Fazek's Line Replace tool for v10

Post by heyvern »

Okay I see.

There is "preview" code in there that would show the "line" as you draw. This might be a bit more tricky to fix but will look into it.

There is the "fa_sharedutils" script that might have this stuff but just guessing.
benny666
Posts: 29
Joined: Mon Jun 29, 2009 4:52 pm
Location: Italy

Re: I tried to fix Fazek's Line Replace tool for v10

Post by benny666 »

OT: any idea to fix "nudge keys tool" script?
Post Reply