Select points AND shapes: LM_select_points.lua tweak

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

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
Ophiuchus
Posts: 4
Joined: Thu Mar 14, 2024 3:08 am

Select points AND shapes: LM_select_points.lua tweak

Post by Ophiuchus »

I made a small modification to the Select Points tool so that it also selects the shapes associated with those selected points.
Shapes are only selected if all their points are selected.
I hope you find it useful:

Code: Select all

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

ScriptName = "LM_SelectPoints"

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

LM_SelectPoints = {}

LM_SelectPoints.BASE_STR = 2310

function LM_SelectPoints:Name()
	return "Select Points"
end

function LM_SelectPoints:Version()
	return "6.0"
end

function LM_SelectPoints:IsBeginnerScript()
	return true
end

function LM_SelectPoints:Description()
	return MOHO.Localize("/Scripts/Tool/SelectPoints/Description=Select/deselect points (hold <shift> to add to selection, <alt> to remove from selection, <ctrl/cmd> to toggle lasso mode)")
end

function LM_SelectPoints:BeginnerDescription()
	return MOHO.Localize("/Scripts/Tool/SelectPoints/BeginnerDescription=To select points, drag a rectangle around them. You can also click on a single point to select it. Click on a curve to select the entire curve or click on a filled-in area to select an entire object.")
end

function LM_SelectPoints:BeginnerDisabledDescription()
	return MOHO.Localize("/Scripts/Tool/SelectPoints/BeginnerDisabledDescription=A vector layer needs to be selected in the Layers window to use this tool.")
end

function LM_SelectPoints:Creator()
	return "Lost Marble LLC"
end

function LM_SelectPoints:UILabel()
	return MOHO.Localize("/Scripts/Tool/SelectPoints/SelectPoints=Select Points")
end

function LM_SelectPoints:LoadPrefs(prefs)
	self.lassoMode = prefs:GetBool("LM_SelectPoints.lassoMode", false)
	self.simplifyValue = prefs:GetInt("LM_SelectPoints.simplifyValue", 25)
end

function LM_SelectPoints:SavePrefs(prefs)
	prefs:SetBool("LM_SelectPoints.lassoMode", self.lassoMode)
	prefs:SetInt("LM_SelectPoints.simplifyValue", self.simplifyValue)
end

function LM_SelectPoints:ResetPrefs()
	self.lassoMode = false
	self.simplifyValue = 25
end

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

LM_SelectPoints.SELMODE_GROUP_PTS = 0
LM_SelectPoints.SELMODE_PT = 1
LM_SelectPoints.SELMODE_EDGE = 2
LM_SelectPoints.SELMODE_SHAPE = 3
LM_SelectPoints.selMode = MOHO_SELMODE_GROUP_PTS
LM_SelectPoints.selRect = LM.Rect:new_local()
LM_SelectPoints.previousX = 0
LM_SelectPoints.previousY = 0
LM_SelectPoints.allowShapePicking = true
LM_SelectPoints.ctrlKeySelection = false
LM_SelectPoints.isMouseDragging = false
LM_SelectPoints.simplifyValue = 25

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

function LM_SelectPoints:IsEnabled(moho)
	if (moho.drawingLayer:LayerType() ~= MOHO.LT_VECTOR) then
		return false
	else
		return true
	end
end

function LM_SelectPoints:IsRelevant(moho)
	local mesh = moho:DrawingMesh()
	if (mesh == nil) then
		return false
	end
	return true
end

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

	self.isMouseDragging = true
	local lassoMode = false
	self.ctrlKeySelection = false
	if (self.lassoMode) then
		if (not(mouseEvent.ctrlKey)) then
			lassoMode = true
		else
			self.ctrlKeySelection = true
		end
	else
		if (mouseEvent.ctrlKey) then
			lassoMode = true
			self.ctrlKeySelection = true
		end
	end

	self.selMode = self.SELMODE_GROUP_PTS
	if (not mouseEvent.shiftKey and not mouseEvent.altKey) then
		mesh:SelectNone()
	end

	if (self.selMode == self.SELMODE_GROUP_PTS) then
		if (lassoMode) then
			self.lassoList = { { mouseEvent.startPt.x, mouseEvent.startPt.y } }
			self.previousX = mouseEvent.startPt.x
			self.previousY = mouseEvent.startPt.y
		else
			self.selRect.left = mouseEvent.startPt.x
			self.selRect.top = mouseEvent.startPt.y
			self.selRect.right = mouseEvent.pt.x
			self.selRect.bottom = mouseEvent.pt.y
			mouseEvent.view:Graphics():SelectionRect(self.selRect)
		end
	end
	mouseEvent.view:DrawMe()
end

function LM_SelectPoints:OnMouseMoved(moho, mouseEvent)
	local mesh = moho:DrawingMesh()
	if (mesh == nil) then
		return
	end

	local lassoMode = false
	if (self.lassoMode) then
		if (not(self.ctrlKeySelection)) then
			lassoMode = true
		end
	else
		if (self.ctrlKeySelection) then
			lassoMode = true
		end
	end

	if (self.selMode == self.SELMODE_GROUP_PTS) then
		if (lassoMode) then
			local g = mouseEvent.view:Graphics()

			g:SetSmoothing(true)
			g:Push()
			local m = g:CurrentTransform()
			m:Invert()
			g:ApplyMatrix(m)
			g:SetColor(MOHO.MohoGlobals.SelCol)
			g:MoveTo(self.previousX, self.previousY)
			g:LineTo(mouseEvent.pt.x, mouseEvent.pt.y)
			g:Pop()
			g:SetSmoothing(false)
			mouseEvent.view:RefreshView()

			table.insert(self.lassoList, { mouseEvent.pt.x, mouseEvent.pt.y })
			self.previousX = mouseEvent.pt.x
			self.previousY = mouseEvent.pt.y
		else
			mouseEvent.view:Graphics():SelectionRect(self.selRect)
			self.selRect.right = mouseEvent.pt.x
			self.selRect.bottom = mouseEvent.pt.y
			mouseEvent.view:Graphics():SelectionRect(self.selRect)
			mouseEvent.view:RefreshView()
		end
		mouseEvent.view:DrawMe()
	end
end

function LM_SelectPoints:OnMouseUp(moho, mouseEvent)
	self.allowShapePicking = true
	local mesh = moho:DrawingMesh()
	if (mesh == nil) then
		return
	end

	local lassoMode = false
	if (self.lassoMode) then
		if (not(self.ctrlKeySelection)) then
			lassoMode = true
		end
	else
		if (self.ctrlKeySelection) then
			lassoMode = true
		end
	end

	local mouseDist = math.abs(mouseEvent.pt.x - mouseEvent.startPt.x) + math.abs(mouseEvent.pt.y - mouseEvent.startPt.y)
	if (mouseDist < 8) then
		-- Single-click selection - select a shape or curve under the mouse
		self:SingleClickSelect(moho, mouseEvent, false, true)
	elseif (self.selMode == self.SELMODE_GROUP_PTS and lassoMode) then
		-- draw the finalized lasso outline
		local g = mouseEvent.view:Graphics()

		g:SetSmoothing(true)
		g:Push()
		local m = g:CurrentTransform()
		m:Invert()
		g:ApplyMatrix(m)
		g:SetColor(MOHO.MohoGlobals.SelCol)
		g:MoveTo(self.previousX, self.previousY)
		g:LineTo(mouseEvent.startPt.x, mouseEvent.startPt.y)
		g:Pop()
		g:SetSmoothing(false)
		mouseEvent.view:RefreshView()
		LM.Snooze(100)
	end

	self.isMouseDragging = false

	if (self.selMode == self.SELMODE_GROUP_PTS and mesh:CountPoints() > 0) then
		if (lassoMode) then
			local g = mouseEvent.view:Graphics()

			-- draw the lasso shape, and do hit testing
			-- 1 - draw the lasso shape
			local end1 = LM.Vector2:new_local()
			local end2 = LM.Vector2:new_local()
			g:Clear(0, 0, 0, 0)
			g:Push()
			local m = g:CurrentTransform()
			m:Invert()
			g:ApplyMatrix(m)
			g:SetColor(255, 255, 255, 255)
			g:BeginShape()
			for i = 1, #self.lassoList - 1 do
				end1:Set(self.lassoList[i][1], self.lassoList[i][2])
				end2:Set(self.lassoList[i + 1][1], self.lassoList[i + 1][2])
				g:AddLine(end1, end2)
			end
			end1:Set(self.lassoList[#self.lassoList][1], self.lassoList[#self.lassoList][2])
			end2:Set(self.lassoList[1][1], self.lassoList[1][2])
			g:AddLine(end1, end2)
			g:EndShape()
			g:Pop()
-- test code to view the lasso's shape
--mouseEvent.view:RefreshView()
--LM.Snooze(1000)

			-- 2 - do hit testing on the lasso shape
			local v = LM.Vector2:new_local()
			local screenPt = LM.Point:new_local()
			local m = LM.Matrix:new_local()

			moho.drawingLayer:GetFullTransform(moho.frame, m, moho.document)
			for i = 0, mesh:CountPoints() - 1 do
				local pt = mesh:Point(i)
				if (not pt.fHidden) then
					v:Set(pt.fPos)
					m:Transform(v)
					g:WorldToScreen(v, screenPt)
					if (g:IsFullWhite(screenPt)) then
						if (mouseEvent.altKey) then
							pt.fSelected = false
						else
							pt.fSelected = true
						end
					end
				end
			end
		else
			local v = LM.Vector2:new_local()
			local screenPt = LM.Point:new_local()
			local m = LM.Matrix:new_local()

			self.selRect:Normalize()
			moho.drawingLayer:GetFullTransform(moho.frame, m, moho.document)
			for i = 0, mesh:CountPoints() - 1 do
				local pt = mesh:Point(i)
				if (not pt.fHidden) then
					v:Set(pt.fPos)
					m:Transform(v)
					mouseEvent.view:Graphics():WorldToScreen(v, screenPt)
					if (self.selRect:Contains(screenPt)) then
						if (mouseEvent.altKey) then
							pt.fSelected = false
						else
							pt.fSelected = true
						end
					end
				end
			end
		end
	end
	self.lassoList = nil
	-- select shapes whose points are all selected
for i = 0, mesh:CountShapes() - 1 do
	local shape = mesh:Shape(i)
	if (shape:AllPointsSelected()) then
		shape.fSelected = true
	end
end										  
 	moho:UpdateSelectedChannels()
end

function LM_SelectPoints:OnKeyDown(moho, keyEvent)
	local mesh = moho:DrawingMesh()
	if (mesh == nil) then
		return
	end

	if ((keyEvent.keyCode == LM.GUI.KEY_DELETE) or (keyEvent.keyCode == LM.GUI.KEY_BACKSPACE)) then
		if (not keyEvent.view:IsMouseDragging()) then
			moho.document:PrepUndo(moho.drawingLayer)
			moho.document:SetDirty()
			MOHO.DeleteSelectedPoints(mesh)
			moho:UpdateUI()
			keyEvent.view:DrawMe()
		end
	end
end

function LM_SelectPoints:DrawMe(moho, view)
	if (self.isMouseDragging and self.selMode == self.SELMODE_GROUP_PTS) then
		local g = view:Graphics()
		local lassoMode = false
		if (self.lassoMode) then
			if (not(self.ctrlKeySelection)) then
				lassoMode = true
			end
		else
			if (self.ctrlKeySelection) then
				lassoMode = true
			end
		end

		if (lassoMode) then
			g:SetSmoothing(true)
			g:Push()
			local m = g:CurrentTransform()
			m:Invert()
			g:ApplyMatrix(m)
			g:SetColor(MOHO.MohoGlobals.SelCol)
			g:MoveTo(self.lassoList[1][1], self.lassoList[1][2])
			for i = 2, #self.lassoList do
				g:LineTo(self.lassoList[i][1], self.lassoList[i][2])
			end
			g:Pop()
			g:SetSmoothing(false)
		else
			g:SelectionRect(self.selRect)
		end
	end
end

function LM_SelectPoints:OnInputDeviceEvent(moho, deviceEvent)
	return LM_TransformPoints:OnInputDeviceEvent(moho, deviceEvent)
end

function LM_SelectPoints:SingleClickSelect(moho, mouseEvent, tryToPreserveSelection, allowEmptySelection)
	-- if tryToPreserveSelection is true, then don't change the selection if some points will remain selected
	local mesh = moho:DrawingMesh()
	if (mesh == nil) then
		return
	end

	if (tryToPreserveSelection and moho:CountSelectedPoints() < 2) then
		mesh:SelectNone()
		moho:CountSelectedPoints(true)
	end
	if (not tryToPreserveSelection and not mouseEvent.shiftKey and not mouseEvent.altKey) then
		mesh:SelectNone()
	end

	local i = mouseEvent.view:PickPoint(mouseEvent.pt)
	if (i >= 0) then --pick a point
		self.selMode = self.SELMODE_PT
		if (tryToPreserveSelection) then
			if (not mesh:Point(i).fSelected) then
				mesh:SelectNone()
			end
		end
		if (mouseEvent.altKey) then
			mesh:Point(i).fSelected = false
		else
			mesh:Point(i).fSelected = true
		end
	else
		if (tryToPreserveSelection) then
			for i = 0, mesh:CountPoints() - 1 do
				local point = mesh:Point(i)
				point.fPrevSelected = point.fSelected
				point.fSelected = false
			end
		end
		local curveID = -1
		local segID = -1
		curveID, segID = mouseEvent.view:PickEdge(mouseEvent.pt, curveID, segID)
		if (curveID >= 0 and segID >= 0) then -- an edge was clicked on
			self.selMode = SELMODE_EDGE
			local curve = mesh:Curve(curveID)

			-- new method - select the points in the curve that was clicked on
			if (mouseEvent.altKey) then
				local isCurveSelected = true
				for i = 0, curve:CountPoints() - 1 do
					if (not curve:Point(i).fSelected) then
						isCurveSelected = false
						break
					end
				end
				for i = 0, curve:CountPoints() - 1 do
					local point = curve:Point(i)
					if ((not isCurveSelected) or (point:CountCurves() < 2)) then
						point.fSelected = false
						if (point.fHidden) then
							point.fSelected = false
						end
					end
				end
			else
				for i = 0, curve:CountPoints() - 1 do
					local point = curve:Point(i)
					if (not point.fHidden) then
						point.fSelected = true
					end
				end
			end

			-- old method - select all points connected to the curve that was clicked on
--[[
			-- take current selection and mark it special
			for i = 0, mesh:CountPoints() - 1 do
				local point = mesh:Point(i)
				if (point.fSelected) then
					point.fSelected = false
					point.fPrevSelected = true
				end
			end
			-- select everything connected to the picked edge
			local curve = mesh:Curve(curveID)
			curve:Point(0).fSelected = true
			mesh:SelectConnected()
			-- turn the original selection back on as well
			for i = 0, mesh:CountPoints() - 1 do
				local point = mesh:Point(i)
				if (point.fPrevSelected) then
					point.fSelected = true
					point.fPrevSelected = false
				end
			end
]]
		elseif (self.allowShapePicking) then -- try to pick a shape
			local shapeID = mouseEvent.view:PickShape(mouseEvent.pt)
			if (shapeID >= 0) then
				self.selMode = SELMODE_SHAPE
--[[				-- take current selection and mark it special
				for i = 0, mesh:CountPoints() - 1 do
					local point = mesh:Point(i)
					if (point.fSelected) then
						point.fSelected = false
						point.fPrevSelected = true
					end
				end]]
				-- select (or deselect) everything connected to the shape
				local shape = mesh:Shape(shapeID)
				for i = 0, shape:CountEdges() - 1 do
					curveID, segID = shape:GetEdge(i, curveID, segID)
					local curve = mesh:Curve(curveID)
					if (mouseEvent.altKey) then
						curve:Point(segID).fSelected = false
						curve:Point(segID + 1).fSelected = false
					else
						curve:Point(segID).fSelected = true
						curve:Point(segID + 1).fSelected = true
					end
				end
--				mesh:SelectConnected()
--[[				-- turn the original selection back on as well
				for i = 0, mesh:CountPoints() - 1 do
					local point = mesh:Point(i)
					if (point.fPrevSelected) then
						point.fSelected = true
						point.fPrevSelected = false
					end
				end]]
			end
		end

		if (not allowEmptySelection) then
			local numSel = moho:CountSelectedPoints(true)
			if (numSel < 1) then
				for i = 0, mesh:CountPoints() - 1 do
					local point = mesh:Point(i)
					point.fSelected = point.fPrevSelected
					point.fPrevSelected = false
				end
			end
		end

		if (tryToPreserveSelection) then
			local preserveSelection = false
			-- pass 1 - check if any of the selection is still selected
			for i = 0, mesh:CountPoints() - 1 do
				local point = mesh:Point(i)
				if (point.fPrevSelected and point.fSelected) then
					preserveSelection = true
				end
			end
			-- pass 2 - preserve the selection
			if (preserveSelection) then
				for i = 0, mesh:CountPoints() - 1 do
					local point = mesh:Point(i)
					point.fSelected = point.fPrevSelected
					point.fPrevSelected = false
				end
			else
				for i = 0, mesh:CountPoints() - 1 do
					local point = mesh:Point(i)
					point.fPrevSelected = false
				end
			end
		end
	end
	moho:UpdateSelectedChannels()
end

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

LM_SelectPoints.CREATE = MOHO.MSG_BASE
LM_SelectPoints.DELETE = MOHO.MSG_BASE + 1
LM_SelectPoints.LASSO = MOHO.MSG_BASE + 2
LM_SelectPoints.FLIP_H = MOHO.MSG_BASE + 3
LM_SelectPoints.FLIP_V = MOHO.MSG_BASE + 4
LM_SelectPoints.DUMMY = MOHO.MSG_BASE + 5
LM_SelectPoints.WELDOVERLAPS = MOHO.MSG_BASE + 6
LM_SelectPoints.SIMPLIFYCURVE = MOHO.MSG_BASE + 7
LM_SelectPoints.CHANGE_SIMPLIFY = MOHO.MSG_BASE + 8
LM_SelectPoints.SPLIT = MOHO.MSG_BASE + 9
LM_SelectPoints.SELECTITEM = MOHO.MSG_BASE + 10
LM_SelectPoints.lassoMode = false

function LM_SelectPoints:DoLayout(moho, layout)
	self.menu = LM.GUI.Menu(MOHO.Localize("/Scripts/Tool/SelectPoints/SelectGroup=Select Group"))

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

	self.groupName = LM.GUI.TextControl(0, "Room For a Big Long Name", 0, LM.GUI.FIELD_TEXT)
	self.groupName:SetValue("")
	layout:AddChild(self.groupName)

	layout:AddChild(LM.GUI.Button(MOHO.Localize("/Scripts/Tool/SelectPoints/Create=Create"), self.CREATE))
	layout:AddChild(LM.GUI.Button(MOHO.Localize("/Scripts/Tool/SelectPoints/Delete=Delete"), self.DELETE))
	self.weldBut = LM.GUI.Button(MOHO.Localize("/Scripts/Tool/SelectPoints/WeldCrossings=Weld Crossings"), self.WELDOVERLAPS)
	self.weldBut:SetToolTip(MOHO.Localize("/Scripts/Tool/SelectPoints/WeldSelectedCurvesWhereTheyCrossOtherCurves=Weld selected curves where they cross other curves"))
	layout:AddChild(self.weldBut)

	self.simplifyBut = LM.GUI.Button(MOHO.Localize("/Scripts/Tool/SelectPoints/Simplify=Simplify:"), self.SIMPLIFYCURVE)
	self.simplifyBut:SetToolTip(MOHO.Localize("/Scripts/Tool/SelectPoints/SimplifySelectedCurves=Simplify selected curves"))
	layout:AddChild(self.simplifyBut)
	self.simplifyText = LM.GUI.TextControl(0, "0000", self.CHANGE_SIMPLIFY, LM.GUI.FIELD_UINT)
	self.simplifyText:SetPercentageMode(true)
	layout:AddChild(self.simplifyText)

	self.splitBut = LM.GUI.Button(MOHO.Localize("/Scripts/Tool/SelectPoints/Split=Split"), self.SPLIT)
	self.splitBut:SetToolTip(MOHO.Localize("/Scripts/Tool/SelectPoints/AddAPointInTheMiddleOfSelectedEdges=Add a point in the middle of selected edges"))
	layout:AddChild(self.splitBut)

	self.lassoCheck = LM.GUI.CheckBox(MOHO.Localize("/Scripts/Tool/SelectPoints/LassoMode=Lasso mode"), self.LASSO)
	layout:AddChild(self.lassoCheck)

	self.flipH = LM.GUI.ImageButton("ScriptResources/flip_points_h", MOHO.Localize("/Scripts/Tool/SelectPoints/FlipH=Flip Horizontally"), false, self.FLIP_H, true)
	layout:AddChild(self.flipH)

	self.flipV = LM.GUI.ImageButton("ScriptResources/flip_points_v", MOHO.Localize("/Scripts/Tool/SelectPoints/FlipV=Flip Vertically"), false, self.FLIP_V, true)
	layout:AddChild(self.flipV)
end

function LM_SelectPoints:UpdateWidgets(moho)
	if (moho:CurrentTool() ~= "LM_SelectPoints") then
		return -- this could get called when doing a double-tap on a multitouch Wacom device with a different active tool
	end

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

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

	self.lassoCheck:SetValue(self.lassoMode)
	self.simplifyText:SetValue(self.simplifyValue)
	if (moho:CountSelectedPoints() > 1) then
		self.weldBut:Enable(true)
		self.simplifyBut:Enable(self.simplifyValue > 0)
		self.splitBut:Enable(true)
		self.flipH:Enable(true)
		self.flipV:Enable(true)
	else
		self.simplifyBut:Enable(false)
		self.weldBut:Enable(false)
		self.splitBut:Enable(false)
		self.flipH:Enable(false)
		self.flipV:Enable(false)
	end
end

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

	if (msg == self.CREATE) then
		local name = self.groupName:Value()

		if (string.len(name) == 0) then
			LM.GUI.Alert(LM.GUI.ALERT_INFO, MOHO.Localize("/Scripts/Tool/SelectPoints/NameGroup=To create a group, you must first name it."))
		elseif (moho:CountSelectedPoints() < 1) then
			LM.GUI.Alert(LM.GUI.ALERT_INFO, MOHO.Localize("/Scripts/Tool/SelectPoints/CantCreateGroup=Can't create group - no points are selected."))
		else
			moho.document:PrepUndo(moho.drawingLayer)
			moho.document:SetDirty()
			mesh:AddGroup(name)
			moho:UpdateUI()
		end
	elseif (msg == self.DELETE) then
		local name = self.groupName:Value()
		moho.document:PrepUndo(moho.drawingLayer)
		moho.document:SetDirty()
		mesh:DeleteGroup(name)
		self.groupName:SetValue("")
		moho:UpdateUI()
	elseif (msg == self.LASSO) then
		self.lassoMode = self.lassoCheck:Value()
	elseif (msg == self.FLIP_H) then
		moho.document:PrepUndo(moho.drawingLayer)
		moho.document:SetDirty()
	
		local center = mesh:SelectedCenter()
	
		for i = 0, mesh:CountPoints() - 1 do
			local pt = mesh:Point(i)
			if (pt.fSelected) then
				local f = center.x - pt.fPos.x
				pt.fPos.x = center.x + f
				pt:FlipControlHandles(moho.drawingLayerFrame)
			end
		end
	
		moho:AddPointKeyframe(moho.drawingFrame)
		moho:NewKeyframe(CHANNEL_POINT)
	elseif (msg == self.FLIP_V) then
		moho.document:PrepUndo(moho.drawingLayer)
		moho.document:SetDirty()
	
		local center = mesh:SelectedCenter()
	
		for i = 0, mesh:CountPoints() - 1 do
			local pt = mesh:Point(i)
			if (pt.fSelected) then
				local f = center.y - pt.fPos.y
				pt.fPos.y = center.y + f
				pt:FlipControlHandles(moho.drawingLayerFrame)
			end
		end
	
		moho:AddPointKeyframe(moho.drawingFrame)
		moho:NewKeyframe(CHANNEL_POINT)
	elseif (msg == self.WELDOVERLAPS) then
		local didPrepUndo = false
		for i = 0, mesh:CountCurves() - 1 do
			local curve = mesh:Curve(i)
			if (curve:IsSelected()) then
				if (not didPrepUndo) then
					moho.document:PrepUndo(moho.drawingLayer)
					moho.document:SetDirty()
					didPrepUndo = true
				end
				local awr = LM_TransformPoints.autoWeldRadius
				LM_TransformPoints.autoWeldRadius = 3
				MOHO.WeldFullCurve(moho, moho.drawingLayer, mesh, i, moho.view)
				LM_TransformPoints.autoWeldRadius = awr
			end
		end
	elseif (msg == self.SIMPLIFYCURVE) then
		local didPrepUndo = false
		local epsilon = MOHO.SimplifyEpsilon(self.simplifyValue)
		for i = 0, mesh:CountCurves() - 1 do
			local curve = mesh:Curve(i)
			if (curve:IsSelected() or curve:IsPartiallySelected()) then
				if (not didPrepUndo) then
					moho.document:PrepUndo(moho.drawingLayer)
					moho.document:SetDirty()
					didPrepUndo = true
				end
--local beforePts = mesh:Curve(curveID):CountPoints()
				mesh:AdvancedCurveSimplification(i, moho.drawingLayerFrame, 0.0, epsilon, 0.01) -- 0.001, 0.0001, 0.01
--print(beforePts .. "/" .. mesh:Curve(curveID):CountPoints())
--				MOHO.ReduceCurve(moho, mesh, i)
			end
		end
		moho:UpdateUI()
	elseif (msg == self.CHANGE_SIMPLIFY) then
		self.simplifyValue = LM.Clamp(self.simplifyText:IntValue(), 0, 100)
		self:UpdateWidgets(moho)
	elseif (msg == self.SPLIT) then
		local selectedEdge = false
		for i = 0, mesh:CountCurves() - 1 do
			local curve = mesh:Curve(i)
			if (curve:IsSelected() or curve:IsPartiallySelected()) then
				selectedEdge = true
			end
		end
		if (selectedEdge) then
			moho.document:PrepUndo(moho.drawingLayer)
			moho.document:SetDirty()

			MOHO:SplitSelectedSegments(mesh, 1, moho.drawingLayerFrame)
		end
		moho:UpdateUI()
	elseif (msg >= self.SELECTITEM) then
		mesh:SelectNone()
		local i = msg - self.SELECTITEM
		local name = mesh:Group(i):Name()
		mesh:SelectGroup(name)
		self.groupName:SetValue(name)
		moho:UpdateUI()
	end
end
Post Reply