-- ************************************************** -- Provide Moho with the name of this script object -- ************************************************** ScriptName = "MS_Spiral" -- ************************************************** -- General information about this script -- ************************************************** MS_Spiral = {} function MS_Spiral:Name() return "Spiral" end function MS_Spiral:Version() return "1.0" end function MS_Spiral:Description() return "Draw a Spiral" end function MS_Spiral:Creator() return "Myles Strous" end function MS_Spiral:UILabel() return("Spiral") end -- ************************************************** -- Recurring values -- ************************************************** MS_Spiral.startVec = LM.Vector2:new_local() MS_Spiral.numTurns = 3 MS_Spiral.ptsPerTurn = 8 -- ************************************************** -- The guts of this script -- ************************************************** function MS_Spiral:IsEnabled(moho) local mesh = moho:Mesh() if (mesh == nil) then return false end if (moho.layer:CurrentAction() ~= "") then return false -- creating new objects in the middle of an action can lead to unexpected results end return true end function MS_Spiral:OnMouseDown(moho, mouseEvent) local mesh = moho:Mesh() if (mesh == nil) then return end moho.document:PrepUndo(moho.layer) moho.document:SetDirty() local n = mesh:CountPoints() mesh:SelectNone() self.startVec:Set(mouseEvent.vec) if (moho.gridOn) then moho:SnapToGrid(self.startVec) end mesh:AddLonePoint(self.startVec, moho.frame) for i = 2,self.numTurns*self.ptsPerTurn do mesh:AppendPoint(self.startVec, moho.frame) end mesh:SelectConnected() moho:CreateShape(false) mouseEvent.view:DrawMe() end function MS_Spiral:OnMouseMoved(moho, mouseEvent) local mesh = moho:Mesh() if (mesh == nil) then return end local vec = LM.Vector2:new_local() vec:Set(mouseEvent.vec) if (moho.gridOn) then moho:SnapToGrid(vec) end local x1 = self.startVec.x local x2 = vec.x local y1 = self.startVec.y local y2 = vec.y local n = mesh:CountPoints() local xrad = x2-x1 local yrad = y2-y1 local rad = math.sqrt(xrad*xrad + yrad*yrad) local angle = math.pi / 2 local dAngle = (2 * math.pi) / self.ptsPerTurn local n = mesh:CountPoints() local v = LM.Vector2:new_local() local ptsInSpiral = self.numTurns*self.ptsPerTurn for i = 1, ptsInSpiral do local ptrad = rad * (i / ptsInSpiral) local pt = mesh:Point(n - (ptsInSpiral - i) -1) local dx = ptrad * math.cos(angle) local dy = ptrad * math.sin(angle) pt.fPos.x = x1 + dx pt.fPos.y = y1 + dy angle = angle + dAngle end moho:AddPointKeyframe(moho.frame) mouseEvent.view:DrawMe() end function MS_Spiral:OnMouseUp(moho, mouseEvent) local mesh = moho:Mesh() if (mesh == nil) then return end moho:AddPointKeyframe(moho.frame) mouseEvent.view:DrawMe() moho:NewKeyframe(CHANNEL_POINT) moho:NewKeyframe(CHANNEL_CURVE) moho:UpdateSelectedChannels() end function MS_Spiral:OnKeyDown(moho, keyEvent) LM_SelectPoints:OnKeyDown(moho, keyEvent) end -- ************************************************** -- Tool options - create and respond to tool's UI -- ************************************************** MS_Spiral.DUMMY = MOHO.MSG_BASE MS_Spiral.CHANGE = MOHO.MSG_BASE + 1 MS_Spiral.RESET = MOHO.MSG_BASE + 2 function MS_Spiral:DoLayout(moho, layout) layout:AddChild(LM.GUI.StaticText("Number of turns")) self.numOfTurns = LM.GUI.TextControl(0, "3", self.CHANGE, LM.GUI.FIELD_UINT) self.numOfTurns:SetWheelInc(1) layout:AddChild(self.numOfTurns) layout:AddChild(LM.GUI.StaticText("Points per turn")) self.pointsPerTurn = LM.GUI.TextControl(0, "8", self.CHANGE, LM.GUI.FIELD_UINT) self.pointsPerTurn:SetWheelInc(1) layout:AddChild(self.pointsPerTurn) layout:AddChild(LM.GUI.Button("Reset", self.RESET)) end function MS_Spiral:UpdateWidgets(moho) local mesh = moho:Mesh() if (mesh == nil) then return end self.numOfTurns:SetValue(self.numTurns) self.pointsPerTurn:SetValue(self.ptsPerTurn) end function MS_Spiral:HandleMessage(moho, view, msg) local mesh = moho:Mesh() if (mesh == nil) then return end if (msg == self.RESET) then self.numTurns = 3 self.ptsPerTurn = 8 self:UpdateWidgets(moho) moho:UpdateUI() elseif (msg == self.CHANGE) then self.numTurns = self.numOfTurns:IntValue() self.ptsPerTurn = self.pointsPerTurn:IntValue() if (self.numTurns < 1) then self.numTurns = 1 end if (self.ptsPerTurn < 3) then self.ptsPerTurn = 3 end self:UpdateWidgets(moho) moho:UpdateUI() end end