This seems to fix the problem.
Code: Select all
-- **************************************************
-- Provide Moho with the name of this script object
-- **************************************************
ScriptName = "LM_AddPoint"
-- **************************************************
-- General information about this script
-- **************************************************
LM_AddPoint = {}
LM_AddPoint.BASE_STR = 2020
LM_AddPoint.STR_LABEL = LM_AddPoint.BASE_STR
LM_AddPoint.STR_DESC = LM_AddPoint.BASE_STR + 1
LM_AddPoint.STR_SELECT = LM_AddPoint.BASE_STR + 2
LM_AddPoint.STR_SHARPCORNERS = LM_AddPoint.BASE_STR + 3
function LM_AddPoint:Name()
return "Add Point"
end
function LM_AddPoint:Version()
return "6.0"
end
function LM_AddPoint:IsBeginnerScript()
return true
end
function LM_AddPoint:Description()
return string.format(
MOHO.Localize("/Scripts/Tool/AddPoint/Description=Add a point (press <%s> to weld, hold <alt> to disable auto-welding, <ctrl/cmd> to reverse sharp corners)"),
MOHO.BindingKeyName()
)
end
function LM_AddPoint:BeginnerDescription()
return MOHO.Localize("/Scripts/Tool/AddPoint/BeginnerDescription=Click and drag to create a new curve segment. Click on an existing curve to add an additional point. Drag a curve endpoint on top of another point and weld them together to create a closed shape with 'Auto Weld' enabled.")
end
function LM_AddPoint:BeginnerDisabledDescription()
return MOHO.Localize("/Scripts/Tool/AddPoint/BeginnerDisabledDescription=A vector layer needs to be selected in the Layers window, and you need to be on Frame 0 in the Timeline to use this tool.")
end
function LM_AddPoint:Creator()
return "Lost Marble LLC, modified by J.Wesley Fowler (synthsin75)"
end
function LM_AddPoint:UILabel()
return(MOHO.Localize("/Scripts/Tool/AddPoint/AddPoint=Add Point"))
end
function LM_AddPoint:LoadPrefs(prefs)
self.sharpCorners = prefs:GetBool("LM_AddPoint.sharpCorners", false)
end
function LM_AddPoint:SavePrefs(prefs)
prefs:SetBool("LM_AddPoint.sharpCorners", self.sharpCorners)
end
function LM_AddPoint:ResetPrefs()
self.sharpCorners = false
LM_TransformPoints.autoWeld = false
LM_TransformPoints.autoWeldRadius = 12
LM_TransformPoints.autoFill = true
end
function LM_AddPoint:NonDragMouseMove()
return true -- Call MouseMoved() even if the mouse button is not down
end
-- **************************************************
-- Recurring values
-- **************************************************
LM_AddPoint.numSel = 0
LM_AddPoint.selID = 0
LM_AddPoint.startWeldVec = LM.Vector2:new_local()
LM_AddPoint.startWeldVec:Set(-10000000, -10000000)
LM_AddPoint.startWeldToPoint = true
-- **************************************************
-- The guts of this script
-- **************************************************
function LM_AddPoint:IsEnabled(moho)
if (moho:DisableDrawingTools()) then
return false
end
local mesh = moho:DrawingMesh()
if (mesh == nil) then
return false
end
if (moho.drawingLayer:CurrentAction() ~= "") then
return false -- creating new objects in the middle of an action can lead to unexpected results
end
return true
end
function LM_AddPoint:IsRelevant(moho)
if (moho:DisableDrawingTools()) then
return false
end
local mesh = moho:DrawingMesh()
if (mesh == nil) then
return false
end
return true
end
function LM_AddPoint:OnInputDeviceEvent(moho, deviceEvent)
return LM_TransformPoints:OnInputDeviceEvent(moho, deviceEvent)
end
function LM_AddPoint:OnMouseDown(moho, mouseEvent)
local mesh = moho:DrawingMesh()
if (mesh == nil) then
return
end
self.startWeldVec:Set(-10000000, -10000000)
moho.document:PrepUndo(moho.drawingLayer)
moho.document:SetDirty()
self.fixedHandles = LM_TransformPoints.fixedHandles
local closestID = -1
local closest = 1e6
local curveID = -1
local segID = -1
local sharpCorners = self.sharpCorners
if (mouseEvent.ctrlKey) then
sharpCorners = not sharpCorners
end
if (mesh:ContinuousTriangulation()) then
sharpCorners = true
end
mesh:SelectNone()
if (not(mouseEvent.altKey)) then
-- find the closest point for auto-welding the new point to
local testVec1 = LM.Vector2:new_local()
testVec1:Set(mouseEvent.drawingVec)
closestID = mesh:ClosestPoint(testVec1)
if (closestID >= 0) then
local testVec2 = LM.Vector2:new_local()
testVec2:Set(mesh:Point(closestID).fPos)
-- convert the two chosen points to pixel coordinates
-- this is to make sure that they are actually close enough in screen space
local p1 = LM.Point:new_local()
local p2 = LM.Point:new_local()
local m = LM.Matrix:new_local()
moho.drawingLayer:GetFullTransform(moho.frame, m, moho.document)
m:Transform(testVec1)
m:Transform(testVec2)
mouseEvent.view:Graphics():WorldToScreen(testVec1, p1)
mouseEvent.view:Graphics():WorldToScreen(testVec2, p2)
p1.x = p1.x - p2.x
p1.y = p1.y - p2.y
closest = (p1.x * p1.x) + (p1.y * p1.y)
end
end
self.secondPtID = -1
if (closest < LM_TransformPoints.autoWeldRadius * LM_TransformPoints.autoWeldRadius and not(mouseEvent.altKey)) then -- add as an extension to an existing point
LM_TransformPoints.fixedHandles = false
if (mesh:Point(closestID):IsEndpoint()) then -- extend this curve
mesh:AddPoint(mouseEvent.drawingVec, closestID, moho.drawingLayerFrame)
else -- start a new curve that is connected to this point
mesh:AddPoint(mouseEvent.drawingVec, -1, moho.drawingLayerFrame)
if (sharpCorners) then
mesh:Point(mesh:CountPoints() - 1):SetCurvature(MOHO.PEAKED, moho.drawingLayerFrame)
mesh:Point(mesh:CountPoints() - 2):SetCurvature(MOHO.PEAKED, moho.drawingLayerFrame)
mesh:Point(closestID):SetCurvature(MOHO.PEAKED, moho.drawingLayerFrame)
end
mesh:WeldPoints(mesh:CountPoints() - 2, closestID, moho.drawingLayerFrame)
end
if (sharpCorners) then
mesh:Point(mesh:CountPoints() - 1):SetCurvature(MOHO.PEAKED, moho.drawingLayerFrame)
end
else
local curveID = -1
local segID = -1
if (not mouseEvent.altKey) then
local pickWidth = 3
while (pickWidth < LM_TransformPoints.autoWeldRadius) do
curveID = -1
segID = -1
curveID, segID = mouseEvent.view:PickEdge(mouseEvent.pt, curveID, segID, pickWidth)
if (curveID >= 0) then
break
end
pickWidth = pickWidth + 3
end
end
if (curveID >= 0) then -- add a point in the middle of some curve
if (moho.gridOn) then
moho:SnapToGrid(mouseEvent.drawingStartVec)
moho:SnapToGrid(mouseEvent.drawingVec)
end
local v = mesh:Curve(curveID):ClosestPointOnSegment(segID, mouseEvent.drawingVec, true, true)
mesh:AddPoint(v, curveID, segID, moho.drawingLayerFrame, LM_TransformPoints:UseFixedHandles(moho))
--syn\/
mesh:Curve(curveID):SetWeight(mesh:CountPoints()-1, 0, moho.drawingLayerFrame, true)
--syn/\
else -- add a new curve segment
LM_TransformPoints.fixedHandles = false
if (moho.gridOn) then
moho:SnapToGrid(mouseEvent.drawingStartVec)
moho:SnapToGrid(mouseEvent.drawingVec)
end
mesh:AddPoint(mouseEvent.drawingVec, -1, moho.drawingLayerFrame)
if (sharpCorners) then
mesh:Point(mesh:CountPoints() - 1):SetCurvature(MOHO.PEAKED, moho.drawingLayerFrame)
mesh:Point(mesh:CountPoints() - 2):SetCurvature(MOHO.PEAKED, moho.drawingLayerFrame)
end
self.secondPtID = mesh:CountPoints() - 2
end
if (sharpCorners) then
mesh:Point(mesh:CountPoints() - 1):SetCurvature(MOHO.PEAKED, moho.drawingLayerFrame)
end
end
LM_TransformPoints.selID = mesh:CountPoints() - 1
mouseEvent.ctrlKey = false
LM_TransformPoints:OnMouseDown(moho, mouseEvent)
moho:AddPointKeyframe(moho.drawingFrame)
mouseEvent.view:DrawMe()
end
function LM_AddPoint:OnMouseMoved(moho, mouseEvent)
local mesh = moho:DrawingMesh()
if (mesh == nil) then
return
end
if (mesh:CountPoints() < 1) then
return
end
if (not LM_TransformPoints.dragging) then
if (mouseEvent.altKey) then
if (self.startWeldVec.x > -1000000) then
self.startWeldVec:Set(-10000000, -10000000)
mouseEvent.view:DrawMe()
end
return
end
-- Display the upcoming weld points for the user's benefit
local m = LM.Matrix:new_local()
local testVec1 = LM.Vector2:new_local()
local testVec2 = LM.Vector2:new_local()
local p1 = LM.Point:new_local()
local p2 = LM.Point:new_local()
local dist = 0
local curveID = -1
local segID = -1
local pickWidth = 3
local id = mesh:ClosestPoint(mouseEvent.drawingVec)
local pt
self.startWeldVec:Set(-10000000, -10000000)
if (id >= 0) then
pt = mesh:Point(id)
testVec1:Set(mouseEvent.drawingVec)
testVec2:Set(pt.fPos)
moho.drawingLayer:GetFullTransform(moho.frame, m, moho.document)
m:Transform(testVec1)
moho.view:Graphics():WorldToScreen(testVec1, p1)
m:Transform(testVec2)
moho.view:Graphics():WorldToScreen(testVec2, p2)
p2.x = p1.x - p2.x
p2.y = p1.y - p2.y
dist = (p2.x * p2.x) + (p2.y * p2.y)
if (dist < LM_TransformPoints.autoWeldRadius * LM_TransformPoints.autoWeldRadius) then
self.startWeldToPoint = true
self.startWeldVec:Set(pt.fPos)
mouseEvent.view:DrawMe()
return
end
end
while (pickWidth < LM_TransformPoints.autoWeldRadius) do
curveID, segID = moho.view:PickEdge(p1, curveID, segID, pickWidth)
if (curveID >= 0) then -- add a point in the middle of some curve
self.startWeldToPoint = false
self.startWeldVec:Set(mesh:Curve(curveID):ClosestPointOnSegment(segID, mouseEvent.drawingVec, true, true))
break
end
pickWidth = pickWidth + 3
end
mouseEvent.view:DrawMe()
return
end
if (LM_TransformPoints.dragging) then
if (LM_TransformPoints.selID >= 0) then
if (mouseEvent.ctrlKey) then
if (self.sharpCorners and not mesh:ContinuousTriangulation()) then
mesh:Point(LM_TransformPoints.selID):SetCurvature(MOHO.SMOOTH, 0)
else
mesh:Point(LM_TransformPoints.selID):SetCurvature(MOHO.PEAKED, moho.drawingLayerFrame)
end
else
if (self.sharpCorners or mesh:ContinuousTriangulation()) then
mesh:Point(LM_TransformPoints.selID):SetCurvature(MOHO.PEAKED, moho.drawingLayerFrame)
else
mesh:Point(LM_TransformPoints.selID):SetCurvature(MOHO.SMOOTH, 0)
end
end
end
end
mouseEvent.ctrlKey = false
LM_TransformPoints:OnMouseMoved(moho, mouseEvent)
end
function LM_AddPoint:OnMouseUp(moho, mouseEvent)
local mesh = moho:DrawingMesh()
if (mesh == nil) then
return
end
local ptID = LM_TransformPoints.selID
self.startWeldVec:Set(-10000000, -10000000)
mouseEvent.ctrlKey = false
LM_TransformPoints:OnMouseUp(moho, mouseEvent)
LM_TransformPoints.fixedHandles = self.fixedHandles
local doUpdateUI = false
if (moho.drawingLayerFrame > 0) then
if (ptID >= 0 and ptID < mesh:CountPoints()) then
local point = mesh:Point(ptID)
point.fAnimPos:SetValue(0, point.fAnimPos.value) point.fAnimPos:DeleteKey(moho.drawingLayerFrame)
point.fWidth:SetValue(0, point.fWidth.value) point.fWidth:DeleteKey(moho.drawingLayerFrame)
point.fColor:SetValue(0, point.fColor.value) point.fColor:DeleteKey(moho.drawingLayerFrame)
point.fColorStrength:SetValue(0, point.fColorStrength.value) point.fColorStrength:DeleteKey(moho.drawingLayerFrame)
local numCurves = point:CountCurves()
for i = 0, point:CountCurves() - 1 do
local curve = nil
local ptPos = -1
curve, ptPos = point:Curve(i, ptPos)
curve:DeleteCurvatureKey(ptPos, moho.drawingLayerFrame)
end
doUpdateUI = true
end
if (self.secondPtID >= 0 and self.secondPtID < mesh:CountPoints()) then
local point = mesh:Point(self.secondPtID)
point.fAnimPos:SetValue(0, point.fAnimPos.value) point.fAnimPos:DeleteKey(moho.drawingLayerFrame)
point.fWidth:SetValue(0, point.fWidth.value) point.fWidth:DeleteKey(moho.drawingLayerFrame)
point.fColor:SetValue(0, point.fColor.value) point.fColor:DeleteKey(moho.drawingLayerFrame)
point.fColorStrength:SetValue(0, point.fColorStrength.value) point.fColorStrength:DeleteKey(moho.drawingLayerFrame)
local numCurves = point:CountCurves()
for i = 0, point:CountCurves() - 1 do
local curve = nil
local ptPos = -1
curve, ptPos = point:Curve(i, ptPos)
curve:DeleteCurvatureKey(ptPos, moho.drawingLayerFrame)
end
doUpdateUI = true
end
end
if (ptID >= 0 and ptID < mesh:CountPoints()) then
mesh:MakePointConsistentWithNeighbors(ptID, moho.drawingLayerFrame)
self.secondPtID = -1
doUpdateUI = true
end
if (doUpdateUI) then
moho:UpdateUI()
end
end
function LM_AddPoint:OnKeyDown(moho, keyEvent)
LM_TransformPoints:OnKeyDown(moho, keyEvent)
end
function LM_AddPoint:DrawMe(moho, view)
if (LM_TransformPoints.fillingShape) then
local g = view:Graphics()
local matrix = LM.Matrix:new_local()
moho.drawingLayer:GetFullTransform(moho.frame, matrix, moho.document)
g:Push()
g:ApplyMatrix(matrix)
view:DrawPreviewShape()
g:Pop()
elseif (not LM_TransformPoints.dragging) then
local g = view:Graphics()
local matrix = LM.Matrix:new_local()
moho.drawingLayer:GetFullTransform(moho.frame, matrix, moho.document)
g:Push()
g:ApplyMatrix(matrix)
if (self.startWeldToPoint) then
g:SetColor(0, 255, 0, 128)
else
g:SetColor(255, 0, 0, 128)
end
g:SetSmoothing(true)
g:SetBezierTolerance(2)
g:FillCircle(self.startWeldVec, moho:PixelToDoc(LM_TransformPoints.autoWeldRadius) / g:CurrentScale(false))
g:SetSmoothing(false)
g:Pop()
-- Draw bezier handles
if (LM_TransformPoints.showHandles) then
local mesh = moho:DrawingMesh()
if (mesh == nil) then
return
end
g:Push()
g:ApplyMatrix(matrix)
g:SetSmoothing(true)
local meshLayer = moho:LayerAsVector(moho.drawingLayer)
local selectedOnly = false
--[[if (self.isDragging) then
if (self.numSel > 1) then
selectedOnly = true
end
elseif (moho:CountSelectedPoints() > 1) then
selectedOnly = true
end]]
if (moho:CountSelectedPoints() < 2) then
meshLayer:DrawHandles(moho.document, g, selectedOnly)
end
g:Pop()
end
return
end
LM_TransformPoints:DrawMe(moho, view)
end
-- **************************************************
-- Tool options - create and respond to tool's UI
-- **************************************************
LM_AddPoint.DUMMY = MOHO.MSG_BASE
LM_AddPoint.AUTOWELD = MOHO.MSG_BASE + 1
LM_AddPoint.AUTOFILL = MOHO.MSG_BASE + 2
LM_AddPoint.AUTOSTROKE = MOHO.MSG_BASE + 3
LM_AddPoint.SHARPCORNERS = MOHO.MSG_BASE + 4
LM_AddPoint.SHOWHANDLES = MOHO.MSG_BASE + 5
LM_AddPoint.FIXEDHANDLES = MOHO.MSG_BASE + 6
LM_AddPoint.SELECTITEM = MOHO.MSG_BASE + 7
LM_AddPoint.sharpCorners = false
function LM_AddPoint:DoLayout(moho, layout)
self.menu = LM.GUI.Menu(MOHO.Localize("/Scripts/Tool/AddPoint/SelectGroup=Select Group"))
self.popup = LM.GUI.PopupMenu(128, false)
self.popup:SetMenu(self.menu)
layout:AddChild(self.popup)
self.autoWeldCheck = LM.GUI.CheckBox(MOHO.Localize("/Scripts/Tool/AddPoint/AutoWeld=Auto-weld"), self.AUTOWELD)
layout:AddChild(self.autoWeldCheck)
self.autoFillCheck = LM.GUI.CheckBox(MOHO.Localize("/Scripts/Tool/AddPoint/AutoFill=Auto-fill"), self.AUTOFILL)
layout:AddChild(self.autoFillCheck)
self.autoStrokeCheck = LM.GUI.CheckBox(MOHO.Localize("/Scripts/Tool/AddPoint/AutoStroke=Auto-stroke"), self.AUTOSTROKE)
layout:AddChild(self.autoStrokeCheck)
self.sharpCornersCheck = LM.GUI.CheckBox(MOHO.Localize("/Scripts/Tool/AddPoint/SharpCorners=Sharp corners"), self.SHARPCORNERS)
layout:AddChild(self.sharpCornersCheck)
if (MOHO.IsMohoPro()) then
self.showHandleCheck = LM.GUI.ImageButton("ScriptResources/show_handles", MOHO.Localize("/Scripts/Tool/TransformPoints/ShowBezierHandles=Show Bezier Handles"), true, self.SHOWHANDLES, true)
layout:AddChild(self.showHandleCheck)
self.fixedHandleCheck = LM.GUI.ImageButton("ScriptResources/fixed_handles", MOHO.Localize("/Scripts/Tool/TransformPoints/FixedBezierHandles=Fixed Bezier Handles"), true, self.FIXEDHANDLES, true)
layout:AddChild(self.fixedHandleCheck)
end
end
function LM_AddPoint:UpdateWidgets(moho)
if (moho:CurrentTool() ~= "LM_AddPoint") 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.autoWeldCheck:SetValue(LM_TransformPoints.autoWeld)
self.autoFillCheck:SetValue(LM_TransformPoints.autoFill)
self.autoStrokeCheck:SetValue(LM_TransformPoints.autoStroke)
self.sharpCornersCheck:SetValue(LM_AddPoint.sharpCorners)
if (MOHO.IsMohoPro()) then
self.showHandleCheck:SetValue(LM_TransformPoints.showHandles)
self.fixedHandleCheck:SetValue(LM_TransformPoints.fixedHandles)
self.fixedHandleCheck:Enable(LM_TransformPoints.showHandles)
end
end
function LM_AddPoint:HandleMessage(moho, view, msg)
local mesh = moho:DrawingMesh()
if (mesh == nil) then
return
end
if (msg == self.AUTOWELD) then
LM_TransformPoints.autoWeld = self.autoWeldCheck:Value()
elseif (msg == self.AUTOFILL) then
LM_TransformPoints.autoFill = self.autoFillCheck:Value()
elseif (msg == self.AUTOSTROKE) then
LM_TransformPoints.autoStroke = self.autoStrokeCheck:Value()
elseif (msg == self.SHARPCORNERS) then
LM_AddPoint.sharpCorners = self.sharpCornersCheck:Value()
elseif (msg == self.SHOWHANDLES) then
if (MOHO.IsMohoPro()) then
LM_TransformPoints.showHandles = self.showHandleCheck:Value()
moho:UpdateUI()
end
elseif (msg == self.FIXEDHANDLES) then
if (MOHO.IsMohoPro()) then
LM_TransformPoints.fixedHandles = self.fixedHandleCheck:Value()
end
elseif (msg >= self.SELECTITEM) then
mesh:SelectNone()
local i = msg - self.SELECTITEM
local name = mesh:Group(i):Name()
mesh:SelectGroup(name)
moho:UpdateUI()
end
end