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

ScriptName = "AE_SelectShape"

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

AE_SelectShape = {}

AE_SelectShape.BASE_STR = 2320

function AE_SelectShape:Name()
	return "Select Shape"
end

function AE_SelectShape:Version()
	return "6.01.5"
end

function AE_SelectShape:IsBeginnerScript()
	return true
end

function AE_SelectShape:Description()
	return MOHO.Localize("/Scripts/Tool/SelectShape/Description=Click on a shape to select it (hold <shift> to select additional shapes, <alt> to deselect shapes, <cmd/ctrl> to invoke the Eyedropper tool)")
end

function AE_SelectShape:BeginnerDescription()
	return MOHO.Localize("/Scripts/Tool/SelectShape/BeginnerDescription=Click on an existing shape to select it. Quickly choose a Fill color from the color swatch at the bottom of the Style window (Window > Style) or right-click a color swatch to change the Stroke color.")
end

function AE_SelectShape:BeginnerDisabledDescription()
	return MOHO.Localize("/Scripts/Tool/SelectShape/BeginnerDisabledDescription=You need to create a shape first using the 'Create Shape' or 'Paint Bucket' tool before you can use this tool.")
end

function AE_SelectShape:Creator()
	return "Smith Micro Software, Inc., tweak by A.Evseeva"
end

function AE_SelectShape:UILabel()
	return(MOHO.Localize("/Scripts/Tool/SelectShape/SelectShape=Select Shape"))
end

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

AE_SelectShape.prevMousePt = LM.Point:new_local()
AE_SelectShape.prevSelID = -1
AE_SelectShape.handleRadius = 0.03
AE_SelectShape.dragMode = -1 --0:shape picker, 1:center point, 2:handle point
AE_SelectShape.eyedropperMode = false
AE_SelectShape.selShape = nil
AE_SelectShape.startVec = LM.Vector2:new_local()
AE_SelectShape.lastVec = LM.Vector2:new_local()
AE_SelectShape.effectScale = 1.0
AE_SelectShape.tracing = false

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

function AE_SelectShape:IsEnabled(moho)
	if (moho:CountShapes() > 0) then
		return true
	end
	return false
end

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

function AE_SelectShape:HideConstructionCurves(moho)
	return true
end

function AE_SelectShape:SupportsGPUMode(moho)
	if (self.eyedropperMode) then
		return false
	else
		return true
	end
end

function AE_SelectShape:OnMouseDown(moho, mouseEvent)
	self.eyedropperMode = false
	if (mouseEvent.ctrlKey) then
		mouseEvent.ctrlKey = false
		self.eyedropperMode = true
		LM_Eyedropper:OnMouseDown(moho, mouseEvent)
		return
	end

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

	self.prevMousePt:Set(mouseEvent.pt)
	self.prevSelID = mouseEvent.view:PickShape(mouseEvent.pt)
	self.selShape = nil

	self.dragMode = 0 -- shape picker (default)
	-- cycle through shapes and see if the user clicked on an effect handle
	local g = mouseEvent.view:Graphics()
	local matrix = LM.Matrix:new_local()
	moho.drawingLayer:GetFullTransform(moho.frame, matrix, moho.document)
	g:Push()
	g:ApplyMatrix(matrix)
	local graphicsScale = g:CurrentScale(false)
	g:Pop()

	for i = 0, mesh:CountShapes() - 1 do
		local shape = mesh:Shape(i)
		if (shape:HasPositionDependentStyles()) then
			local center = shape:EffectHandle1()
			local handleLocation = shape:EffectHandle2()

			self.effectScale = shape.fEffectScale.value
			if ((center - mouseEvent.drawingVec):Mag() < self.handleRadius / graphicsScale) then
				self.dragMode = 1
				self.selShape = shape
				self.startVec:Set(shape.fEffectOffset.value)
				break
			elseif ((handleLocation - mouseEvent.drawingVec):Mag() < self.handleRadius / graphicsScale) then
				self.dragMode = 2
				self.selShape = shape
				self.startVec:Set(handleLocation)
				self.lastVec:Set(mouseEvent.drawingVec)
				break
			end
		end
	end

	if (self.dragMode == 0) then -- shape picker
		if (not mouseEvent.shiftKey and not mouseEvent.altKey) then
			for i = 0, mesh:CountShapes() - 1 do
				mesh:Shape(i).fSelected = false
			end
		end
		if (self.prevSelID >= 0) then
			if (mouseEvent.altKey) then
				mesh:Shape(self.prevSelID).fSelected = false
			else
				mesh:Shape(self.prevSelID).fSelected = true
			end
		end
		moho:UpdateSelectedChannels()
	elseif (self.dragMode == 1 or self.dragMode == 2) then -- move an effect handle
		moho.document:PrepUndo(moho.drawingLayer)
		moho.document:SetDirty()
	end

	mouseEvent.view:DrawMe()
end

function AE_SelectShape:OnMouseMoved(moho, mouseEvent)
	if (self.eyedropperMode) then
		mouseEvent.ctrlKey = false
		LM_Eyedropper:OnMouseMoved(moho, mouseEvent)
		return
	end

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

	self.prevMousePt:Set(mouseEvent.pt)
	self.prevSelID = mouseEvent.view:PickShape(mouseEvent.pt)

	if (self.dragMode == 0) then -- shape picker
		if (not mouseEvent.shiftKey and not mouseEvent.altKey) then
			for i = 0, mesh:CountShapes() - 1 do
				mesh:Shape(i).fSelected = false
			end
		end
		if (self.prevSelID >= 0) then
			if (mouseEvent.altKey) then
				mesh:Shape(self.prevSelID).fSelected = false
			else
				mesh:Shape(self.prevSelID).fSelected = true
			end
		end
		moho:UpdateSelectedChannels()
	elseif (self.dragMode == 1 or self.dragMode == 2) then -- move an effect handle
		if (self.selShape ~= nil) then
			if (self.dragMode == 1) then
				self.selShape.fEffectOffset:SetValue(moho.drawingLayerFrame, self.startVec + (mouseEvent.drawingVec - mouseEvent.drawingStartVec))
			else
				local center = self.selShape:EffectHandle1()
				local handleLocation = self.selShape:EffectHandle2()
				local min = LM.Vector2:new_local()
				local max = LM.Vector2:new_local()
				self.selShape:ShapeBounds(min, max, 0)
				handleLocation = self.startVec + (mouseEvent.drawingVec - mouseEvent.drawingStartVec)
				local v = handleLocation - center
				local v1 = self.lastVec - center
				local v2 = mouseEvent.drawingVec - center
				v2:Rotate(-math.atan2(v1.y, v1.x))
				local angle = math.atan2(v2.y, v2.x)
				angle = angle + self.selShape.fEffectRotation.value
				self.lastVec:Set(mouseEvent.drawingVec)
				self.selShape.fEffectRotation:SetValue(moho.drawingLayerFrame, angle)
				if (mouseEvent.shiftKey) then
					self.selShape.fEffectRotation:SetValue(moho.drawingLayerFrame, self.selShape.fEffectRotation.value / (math.pi / 4))
					self.selShape.fEffectRotation:SetValue(moho.drawingLayerFrame, (math.pi / 4) * LM.Round(self.selShape.fEffectRotation.value))
				end
				if (mouseEvent.ctrlKey) then
					self.selShape.fEffectScale:SetValue(moho.drawingLayerFrame, self.effectScale)
				else
					if (max.y - min.y > max.x - min.x) then
						self.selShape.fEffectScale:SetValue(moho.drawingLayerFrame, v:Mag() / ((max.y - min.y) / 2.0))
					else
						self.selShape.fEffectScale:SetValue(moho.drawingLayerFrame, v:Mag() / ((max.x - min.x) / 2.0))
					end
				end
			end
			moho:NewKeyframe(CHANNEL_FXXFORM)
			moho:UpdateSelectedChannels()
		end
	end

	mouseEvent.view:DrawMe()
end

function AE_SelectShape:OnMouseUp(moho, mouseEvent)
	if (self.eyedropperMode) then
		mouseEvent.ctrlKey = false
		LM_Eyedropper:OnMouseUp(moho, mouseEvent)
		self.eyedropperMode = false
		return
	end
	self.eyedropperMode = false
end

function AE_SelectShape:OnKeyDown(moho, keyEvent)
	local mesh = moho:DrawingMesh()
	if (mesh == nil) then
		return
	end
	
	local selectedShapes = {}
	for i = 0, mesh:CountShapes() - 1 do
		if mesh:Shape(i).fSelected then table.insert(selectedShapes, mesh:Shape(i)) end
	end
	
	if (keyEvent.keyCode == LM.GUI.KEY_UP) then
		if (keyEvent.ctrlKey) then -- select higher in the stacking order
			self.prevSelID = self.prevSelID + 1
			local id = keyEvent.view:PickShape(self.prevMousePt, self.prevSelID)
			for i = 0, mesh:CountShapes() - 1 do
				mesh:Shape(i).fSelected = false
			end
			if (id >= 0) then
				mesh:Shape(id).fSelected = true
			end
			moho:UpdateSelectedChannels()
		elseif (keyEvent.shiftKey) then -- raise shape to top
			moho.document:PrepUndo(moho.drawingLayer)
			moho.document:SetDirty()
			--[[
			for i = 0, mesh:CountShapes() - 1 do
				if (mesh:Shape(i).fSelected) then
					mesh:RaiseShape(i, true)
				end
			end
			--]]
			for j,shape in pairs(selectedShapes) do
				mesh:RaiseShape(mesh:ShapeID(shape), true)
			end
		else -- raise shape
			moho.document:PrepUndo(moho.drawingLayer)
			moho.document:SetDirty()
			--[[
			for i = mesh:CountShapes() - 1, 0, -1 do
				if (mesh:Shape(i).fSelected) then
					mesh:RaiseShape(i, false)
				end
			end
			--]]
			for j = #selectedShapes, 1, -1 do
				mesh:RaiseShape(mesh:ShapeID(selectedShapes[j]), false)
			end			
		end
		keyEvent.view:DrawMe()
	elseif (keyEvent.keyCode == LM.GUI.KEY_DOWN) then
		if (keyEvent.ctrlKey) then -- select lower in the stacking order
			self.prevSelID = self.prevSelID - 1
			id = keyEvent.view:PickShape(self.prevMousePt, self.prevSelID)
			for i = 0, mesh:CountShapes() - 1 do
				mesh:Shape(i).fSelected = false
			end
			if (id >= 0) then
				mesh:Shape(id).fSelected = true
			end
			moho:UpdateSelectedChannels()
		elseif (keyEvent.shiftKey) then -- lower shape to bottom
			moho.document:PrepUndo(moho.drawingLayer)
			moho.document:SetDirty()
			--[[
			for i = 0, mesh:CountShapes() - 1 do
				if (mesh:Shape(i).fSelected) then
					mesh:LowerShape(i, true)
				end
			end
			--]]
			for j = #selectedShapes, 1, -1 do
				mesh:LowerShape(mesh:ShapeID(selectedShapes[j]), true)
			end				
		else -- lower shape
			moho.document:PrepUndo(moho.drawingLayer)
			moho.document:SetDirty()
			--[[
			for i = 0, mesh:CountShapes() - 1 do
				if (mesh:Shape(i).fSelected) then
					mesh:LowerShape(i, false)
				end
			end
			--]]
			for j,shape in pairs(selectedShapes) do
				mesh:LowerShape(mesh:ShapeID(shape), false)
			end
		end
		keyEvent.view:DrawMe()
	elseif ((keyEvent.keyCode == LM.GUI.KEY_DELETE) or (keyEvent.keyCode == LM.GUI.KEY_BACKSPACE)) then
		moho.document:PrepUndo(moho.drawingLayer)
		moho.document:SetDirty()
		local i = 0
		while i < mesh:CountShapes() do
			if (mesh:Shape(i).fSelected) then
				mesh:DeleteShape(i)
			else
				i = i + 1
			end
		end
		keyEvent.view:DrawMe()
	end
	--[[
	self.dlog.mesh = moho:Mesh()
	self.dlog:PopulateList(moho)
	--]]
end

function AE_SelectShape:DrawMe(moho, view)
	if (self.eyedropperMode) then
		LM_Eyedropper:DrawMe(moho, view)
		return
	end

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

	local g = view:Graphics()
	local matrix = LM.Matrix:new_local()

	g:SetSmoothing(true)
	g:SetBezierTolerance(2.0)

	moho.drawingLayer:GetFullTransform(moho.frame, matrix, moho.document)
	g:Push()
	g:ApplyMatrix(matrix)

	local graphicsScale = g:CurrentScale(false)

	for i = 0, mesh:CountShapes() - 1 do
		local shape = mesh:Shape(i)
		if (shape:HasPositionDependentStyles()) then
			local center = shape:EffectHandle1()
			local handleLocation = shape:EffectHandle2()
			local handleColor = MOHO.MohoGlobals.ElemCol
			if (shape.fSelected) then
				handleColor = MOHO.MohoGlobals.SelCol
			end

			g:SetColor(handleColor.r, handleColor.g, handleColor.b, 96)
			g:FillCircle(center, self.handleRadius / graphicsScale)
			g:SetColor(handleColor)
			g:FrameCircle(center, self.handleRadius / graphicsScale)
			g:FrameCircle(handleLocation, self.handleRadius / graphicsScale)

			local v = handleLocation - center
			v:NormMe()
			v = v * self.handleRadius / graphicsScale
			center = center + v
			handleLocation = handleLocation - v
			g:DrawLine(center.x, center.y, handleLocation.x, handleLocation.y)
		end
	end

	g:Pop()
	g:SetSmoothing(false)
	
	if self.tracing then self:TraceSelected(moho, view) end
	
end

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

AE_SelectShape.FILL = MOHO.MSG_BASE
AE_SelectShape.FILLCOLOR = MOHO.MSG_BASE + 1
AE_SelectShape.LINE = MOHO.MSG_BASE + 2
AE_SelectShape.LINECOLOR = MOHO.MSG_BASE + 3
AE_SelectShape.LINEWIDTH = MOHO.MSG_BASE + 4

AE_SelectShape.REORDERBUTTON = MOHO.MSG_BASE + 5
AE_SelectShape.TRACECHECK = MOHO.MSG_BASE + 6
AE_SelectShape.OFFBUTTON = MOHO.MSG_BASE + 7
AE_SelectShape.SELECTALLBUTTON = MOHO.MSG_BASE + 8
AE_SelectShape.NAMEFIELD = MOHO.MSG_BASE + 9

AE_SelectShape.SHAPELIST = MOHO.MSG_BASE + 100



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

	local style = moho:CurrentEditStyle()
	if (style ~= nil) then
		self.fillCol:SetValue(style.fFillCol.value)
		self.lineCol:SetValue(style.fLineCol.value)
		self.lineWidth:SetValue(style.fLineWidth * moho.document:Height())
	end

	local shape = moho:SelectedShape()
	if (shape ~= nil) then
		self.fillCheck:SetValue(shape.fHasFill)
		self.fillCheck:Enable(shape.fFillAllowed)
		self.fillCol:Enable(shape.fHasFill)
		self.lineCheck:SetValue(shape.fHasOutline)
		self.lineCheck:Enable(true)
		self.lineCol:Enable(shape.fHasOutline)
		
	else
		self.fillCheck:SetValue(false)
		self.fillCheck:Enable(false)
		self.lineCheck:SetValue(false)
		self.lineCheck:Enable(false)

	end
	

	self:PopulateMenu(moho)
	self.selectShapeMenu_popup:Redraw()
	
	local singleSelected = false
	for i=0, mesh:CountShapes()-1 do
		if mesh:Shape(i).fSelected then
			if not singleSelected then singleSelected = true 
			else
				singleSelected = false
				break
			end
		end
	end
	if shape then
		if singleSelected then 
			local name = shape:Name()
			if name == "" then name = tostring(shape:ShapeID()) end
			self.shapeName:SetValue(name)
			self.shapeName:Enable(true)
		else
			self.shapeName:SetValue("  multiple selected")
			self.shapeName:Enable(false)
		end
	else 
		self.shapeName:SetValue("  nothing selected")
		self.shapeName:Enable(false)
	end
		
	self.traceCheck:SetValue(self.tracing)
	
end

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

	local style = moho:CurrentEditStyle()
	local shape = moho:SelectedShape()
	if (style == nil and shape == nil) then
		return
	end
	moho.document:PrepUndo(moho.drawingLayer)
	moho.document:SetDirty()

	if (msg == self.FILL) then
		if (shape ~= nil) then
			if (shape.fFillAllowed) then
				shape.fHasFill = self.fillCheck:Value()
			end
		end
		for i = 0, mesh:CountShapes() - 1 do
			local shape = mesh:Shape(i)
			if (shape.fSelected) then
				if (shape.fFillAllowed) then
					shape.fHasFill = self.fillCheck:Value()
				end
			end
		end
		moho:UpdateUI()
	elseif (msg == self.FILLCOLOR) then
		if (style ~= nil) then
			style.fFillCol:SetValue(moho.drawingLayerFrame, self.fillCol:Value())
		end
		for i = 0, mesh:CountShapes() - 1 do
			local shape = mesh:Shape(i)
			if (shape.fSelected) then
				shape.fMyStyle.fFillCol:SetValue(moho.drawingLayerFrame, self.fillCol:Value())
			end
		end
		moho:UpdateUI()
	elseif (msg == self.LINE) then
		if (shape ~= nil) then
			shape.fHasOutline = self.lineCheck:Value()
		end
		for i = 0, mesh:CountShapes() - 1 do
			local shape = mesh:Shape(i)
			if (shape.fSelected) then
				shape.fHasOutline = self.lineCheck:Value()
			end
		end
		moho:UpdateUI()
	elseif (msg == self.LINECOLOR) then
		if (style ~= nil) then
			style.fLineCol:SetValue(moho.drawingLayerFrame, self.lineCol:Value())
		end
		for i = 0, mesh:CountShapes() - 1 do
			local shape = mesh:Shape(i)
			if (shape.fSelected) then
				shape.fMyStyle.fLineCol:SetValue(moho.drawingLayerFrame, self.lineCol:Value())
			end
		end
		moho:UpdateUI()
	elseif (msg == self.LINEWIDTH) then
		if (style ~= nil) then
			local lineWidth = self.lineWidth:FloatValue()
			lineWidth = LM.Clamp(lineWidth, 0.25, 256)
			style.fLineWidth = lineWidth / moho.document:Height()
		end
		for i = 0, mesh:CountShapes() - 1 do
			local shape = mesh:Shape(i)
			if (shape.fSelected) then
				local lineWidth = self.lineWidth:FloatValue()
				lineWidth = LM.Clamp(lineWidth, 0.25, 256)
				shape.fMyStyle.fLineWidth = lineWidth / moho.document:Height()
			end
		end
		moho:UpdateUI()
	elseif (msg == self.REORDERBUTTON) then
		self:ReorderShapes(moho)
		moho:UpdateUI()
	elseif (msg == self.OFFBUTTON) then
		self:ToggleTransparency(moho)
		moho:UpdateUI()	
	elseif (msg == self.SELECTALLBUTTON) then
		if moho:Mesh() then
			for i=0, moho:Mesh():CountShapes()-1 do
				moho:Mesh():Shape(i).fSelected = true
			end
		end
		moho:UpdateUI()
		
	elseif (msg == self.TRACECHECK) then
		self.tracing = self.traceCheck:Value()
		
	elseif msg == self.NAMEFIELD then
		if shape then 
			shape:SetName(self.shapeName:Value())
			self:PopulateMenu(moho)
			self.selectShapeMenu_popup:Redraw()
		end

	elseif msg >= self.SHAPELIST then
		local s = msg - self.SHAPELIST
		--for i=0, mesh:CountShapes()-1 do mesh:Shape(i).fSelected = false end
		self.selectShapeMenu:SetChecked(msg, not self.selectShapeMenu:IsChecked(msg))
		mesh:Shape(s).fSelected = self.selectShapeMenu:IsChecked(msg)
		moho:UpdateUI()
	end
end

function AE_SelectShape:GetShapeByGroup(moho, groupName)
	local mesh = moho:Mesh()
	local group = nil
	for g=0, mesh:CountGroups()-1 do
		if mesh:Group(g):Name() == groupName then
			group =  mesh:Group(g)
			break
		end
	end
	if not group then return nil end
	local numPoints = group:CountPoints()
	for s=0, mesh:CountShapes()-1 do
		if mesh:Shape(s):CountPoints() == numPoints then
			local equal = true
			for p=0, countPoints-1 do
				local point = group:Point(p)
				if not mesh:Shape(s):ContainsPoint(mesh:PointID(point)) then
					equal = false
					break
				end				
			end
			if equal then return mesh:Shape(s) end
		end
	end
	return nil
end

function AE_SelectShape:GetGroupByShape(moho, shape)
	local mesh = moho:Mesh()
	local numPoints = shape:CountPoints()
	for g=0, mesh:CountGroups()-1 do
		if mesh:Group(g):CountPoints() == numPoints then
			local equal = true
			for p=0,numPoints-1 do
				local point = mesh:Group(g):Point(p)
				if not shape:ContainsPoint(mesh:PointID(point)) then
					equal = false
					break
				end
			end
			if equal then
				return mesh:Group(g):Name(), mesh:Group(g)
			end
		end
	end
	return nil
end

function AE_SelectShape:TraceSelected(moho, view)
	local mesh = moho:Mesh()
	if mesh == nil then return end	
	local g = view:Graphics()
	local matrix = LM.Matrix:new_local()
	moho.drawingLayer:GetFullTransform(moho.frame, matrix, moho.document)
	g:Push()
	g:ApplyMatrix(matrix)
	
	for s = 0, mesh:CountShapes()-1 do
		local shape = mesh:Shape(s)
		if shape.fSelected then
			for e = 0, shape:CountEdges()-1 do
				local c,p = shape:GetEdge(e)
				local startPercent, endPercent = mesh:Curve(c):GetSegmentRange(p)
				local step = (endPercent-startPercent)/10
				local p1 = mesh:Curve(c):GetPercentLocation(startPercent)
				for i=1,10 do
					local nextPercent = startPercent + step * i
					if nextPercent > endPercent then nextPercent = endPercent end
					local p2 = mesh:Curve(c):GetPercentLocation(nextPercent)
					g:DrawFatLine(3, p1.x, p1.y, p2.x, p2.y)
					p1:Set(p2)
				end
			end
		end
	end
	
	g:Pop()
	
end

function AE_SelectShape:ToggleTransparency(moho)
	local mesh = moho:Mesh()
	if mesh == nil then return end
	for s = 0, mesh:CountShapes()-1 do
		if mesh:Shape(s).fSelected then 
			local shape = mesh:Shape(s)
			local style = shape.fMyStyle
			if shape.fHasFill then
				local color = style.fFillCol.value
				if color.a == 0 then color.a = 255 else color.a = 0 end				
				style.fFillCol:SetValue(moho.frame, color)
			end
			if shape.fHasOutline then
				local color = style.fLineCol.value
				if color.a == 0 then color.a = 255 else color.a = 0 end				
				style.fLineCol:SetValue(moho.frame, color)				
			end			
		end
	end
end


function AE_SelectShape:GetShapeOrderChannel(moho, layer)
	local maxChannel = layer:CountChannels()-1
	local chInfo = MOHO.MohoLayerChannel:new_local()
	local orderChannel = nil
	for i=0, maxChannel do
		layer:GetChannelInfo(i, chInfo)
		if chInfo.channelID == CHANNEL_SHAPE_ORDER then
			return moho:ChannelAsAnimString(layer:Channel(i, 0, moho.document))
		end
	end
end


function AE_SelectShape:DoLayout(moho, layout)
	self.fillCheck = LM.GUI.CheckBox(MOHO.Localize("/Scripts/Tool/SelectShape/Fill=Fill:"), self.FILL)
	layout:AddChild(self.fillCheck)
	self.fillCol = LM.GUI.ShortColorSwatch(true, self.FILLCOLOR)
	layout:AddChild(self.fillCol)

	self.lineCheck = LM.GUI.CheckBox(MOHO.Localize("/Scripts/Tool/SelectShape/Stroke=Stroke:"), self.LINE)
	layout:AddChild(self.lineCheck)
	self.lineCol = LM.GUI.ShortColorSwatch(true, self.LINECOLOR)
	layout:AddChild(self.lineCol)

	layout:AddChild(LM.GUI.StaticText(MOHO.Localize("/Scripts/Tool/SelectShape/Width=Width:")))
	self.lineWidth = LM.GUI.TextControl(0, "00.0000", self.LINEWIDTH, LM.GUI.FIELD_UFLOAT)
	self.lineWidth:SetWheelInc(1.0)
	self.lineWidth:SetWheelInteger(true)
	layout:AddChild(self.lineWidth)
	
	layout:AddPadding(20)
	self.traceCheck = LM.GUI.CheckBox("Trace selected shapes", self.TRACECHECK)
	layout:AddChild(self.traceCheck)	
	layout:AddChild(LM.GUI.Button("on/off", self.OFFBUTTON))	
	

	layout:AddPadding(20)
	self.shapeName = LM.GUI.TextControl(120, "", self.NAMEFIELD, LM.GUI.FIELD_TEXT, "Name:")
	layout:AddChild(self.shapeName)	
	
	self.selectShapeMenu = LM.GUI.Menu("select shapes")
	self.selectShapeMenu_popup = LM.GUI.PopupMenu(120, false)
	self.selectShapeMenu_popup:SetMenu(self.selectShapeMenu)
	layout:AddChild(self.selectShapeMenu_popup)
	self.selectShapeMenu_popup:SetToolTip("Select shape")


	layout:AddChild(LM.GUI.Button("select ALL", self.SELECTALLBUTTON))
	
	
	
end

function AE_SelectShape:PopulateMenu(moho)
	self.selectShapeMenu:RemoveAllItems()
	local mesh = moho:Mesh()
	if not mesh then return end
	for s = mesh:CountShapes()-1,0,-1 do
		local nextShape = mesh:Shape(s)
		local nextName = nextShape:Name()
		if nextName == "" then nextName = tostring(nextShape:ShapeID()) end
		self.selectShapeMenu:AddItem(nextName, 0, self.SHAPELIST + s)
		if nextShape.fSelected then self.selectShapeMenu:SetChecked(self.SHAPELIST + s, true) end
	end
end


Tweak for Select Shape
Listed

Script type: Tool

Uploaded: Sep 21 2020, 11:14

Last modified: Oct 28 2020, 22:02

Select shape by name, turn shape on and off, trace outlines, fixed up/down moving of several shapes at a time
This script, and all other scripts on this site are distributed as free software under the GNU General Public License 3.0 or later.
Downloads count: 24