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

ScriptName = "SZ_RecolorShape"

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

SZ_RecolorShape = {}

function SZ_RecolorShape:Name()
	return self:Localize("Name")
end

function SZ_RecolorShape:Version()
	return "1.01"
end

function SZ_RecolorShape:Description()
	return self:Localize("Description")
end

function SZ_RecolorShape:Creator()
	return "Stan from 2danimator.ru"
end

function SZ_RecolorShape:UILabel()
	return self:Localize("Label")
end

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

SZ_RecolorShape.defaultFillColor1 = MOHO.MohoGlobals.FillCol
SZ_RecolorShape.defaultStrokeColor1 = MOHO.MohoGlobals.LineCol
SZ_RecolorShape.defaultFillColor2 = MOHO.MohoGlobals.FillCol
SZ_RecolorShape.defaultStrokeColor2 = MOHO.MohoGlobals.LineCol

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

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

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

function SZ_RecolorShape:Localize(text)
	local fileWord = MOHO.Localize("/Menus/File/File=File")
	
	local phrase = {}
	
	phrase["Name"] = "Recolor Shape"
	phrase["Description"] = "Recolor shapes by a single click. Hold <Alt> to push color 2."
	phrase["Label"] = "Recolor Shape"
	phrase["Fill1"] = "Fill 1:"
	phrase["Stroke1"] = "Stroke 1:"
	phrase["Fill2"] = "Fill 2:"
	phrase["Stroke2"] = "Stroke 2:"
	phrase["Button1"] = "Copy from Style panel"
	phrase["Button2"] = "Copy from Style panel"
	
	
	if fileWord == "Файл" then
		phrase["Name"] = "Перекрасить форму"
		phrase["Description"] = "Перекрашивает форму одним кликом по ней. Удерживайте <Alt>, чтобы перекрасить в цвет 2."
		phrase["Label"] = "Перекрасить форму"
		phrase["Fill1"] = "Заливка 1:"
		phrase["Stroke1"] = "Контур 1:"
		phrase["Fill2"] = "Заливка 2:"
		phrase["Stroke2"] = "Контур 2:"
		phrase["Button1"] = "Копировать из панели стиля"
		phrase["Button2"] = "Копировать из панели стиля"
	end
	
	return phrase[text];
end

function SZ_RecolorShape:OnMouseMoved(moho, mouseEvent)

	local shapeID = mouseEvent.view:PickShape(mouseEvent.pt)
	if shapeID < 0 then return false end

	local shape = moho:Mesh():Shape(shapeID)
	local fillVec = shape.fMyStyle.fFillCol:GetValue(moho.layerFrame)
	local fillRgb = fillVec:AsColorStruct()
	local strokeVec = shape.fMyStyle.fLineCol:GetValue(moho.layerFrame)
	local strokeRgb = strokeVec:AsColorStruct()
	local recolored = false
	
	if (mouseEvent.altKey) then
		if self:AreColorsDifferent(self.defaultFillColor2, fillRgb) or self:AreColorsDifferent(self.defaultStrokeColor2, strokeRgb) then
			moho.document:PrepUndo(moho.layer)
			moho.document:SetDirty()
			shape.fMyStyle.fFillCol:SetValue(moho.layerFrame, self.defaultFillColor2)
			shape.fMyStyle.fLineCol:SetValue(moho.layerFrame, self.defaultStrokeColor2)
			recolored = true
		end
	else
		if self:AreColorsDifferent(self.defaultFillColor1, fillRgb) or self:AreColorsDifferent(self.defaultStrokeColor1, strokeRgb) then
			moho.document:PrepUndo(moho.layer)
			moho.document:SetDirty()
			shape.fMyStyle.fFillCol:SetValue(moho.layerFrame, self.defaultFillColor1)
			shape.fMyStyle.fLineCol:SetValue(moho.layerFrame, self.defaultStrokeColor1)
			recolored = true
		end
	end
	
	if recolored then
		moho:NewKeyframe(CHANNEL_FILL)
		moho:NewKeyframe(CHANNEL_LINE)
		moho:UpdateSelectedChannels()
		mouseEvent.view:DrawMe()
	end
end

function SZ_RecolorShape:OnMouseDown(moho, mouseEvent)
	self:OnMouseMoved(moho, mouseEvent)
end

function SZ_RecolorShape:AreColorsDifferent(col1, col2)
	if col1.r == col2.r and col1.g == col2.g and col1.b == col2.b and col1.a == col2.a then
		return false
	else
		return true
	end
end

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

SZ_RecolorShape.BUTTON1 = MOHO.MSG_BASE
SZ_RecolorShape.BUTTON2 = MOHO.MSG_BASE+1
SZ_RecolorShape.FILLCOL1 = MOHO.MSG_BASE+2
SZ_RecolorShape.FILLCOL2 = MOHO.MSG_BASE+3
SZ_RecolorShape.STROKECOL1 = MOHO.MSG_BASE+4
SZ_RecolorShape.STROKECOL2 = MOHO.MSG_BASE+5

function SZ_RecolorShape:DoLayout(moho, layout)
	layout:AddPadding(30)
	
	layout:AddChild(LM.GUI.StaticText(self:Localize("Fill1")))	
	self.fillColor1 = LM.GUI.ColorSwatch(true, self.FILLCOL1)
	layout:AddChild(self.fillColor1, LM.GUI.ALIGN_LEFT, 2)
	
	layout:AddChild(LM.GUI.StaticText(self:Localize("Stroke1")))	
	self.strokeColor1 = LM.GUI.ColorSwatch(true, self.STROKECOL1)
	layout:AddChild(self.strokeColor1, LM.GUI.ALIGN_LEFT, 2)
	
	layout:AddChild(LM.GUI.Button(self:Localize("Button1"), self.BUTTON1))
	
	layout:AddPadding(30)
	layout:AddChild(LM.GUI.Divider(true), LM.GUI.ALIGN_FILL, 2)
	layout:AddPadding(30)
	
	layout:AddChild(LM.GUI.StaticText(self:Localize("Fill2")))	
	self.fillColor2 = LM.GUI.ColorSwatch(true, self.FILLCOL2)
	layout:AddChild(self.fillColor2, LM.GUI.ALIGN_LEFT, 2)
	
	layout:AddChild(LM.GUI.StaticText(self:Localize("Stroke2")))	
	self.strokeColor2 = LM.GUI.ColorSwatch(true, self.STROKECOL2)
	layout:AddChild(self.strokeColor2, LM.GUI.ALIGN_LEFT, 2)
	
	layout:AddChild(LM.GUI.Button(self:Localize("Button2"), self.BUTTON2))
end

function SZ_RecolorShape:UpdateWidgets(moho)
	local mesh = moho:Mesh()
	if (mesh == nil) then
		return
	end	
	self.fillColor1:SetValue(self.defaultFillColor1)
	self.strokeColor1:SetValue(self.defaultStrokeColor1)
	self.fillColor2:SetValue(self.defaultFillColor2)
	self.strokeColor2:SetValue(self.defaultStrokeColor2)
end

function SZ_RecolorShape:HandleMessage(moho, view, msg)
	if (msg == self.BUTTON1) then
		local style = moho:CurrentEditStyle()
		if (style ~= nil) then
			self.fillColor1:SetValue(style.fFillCol.value)
			self.strokeColor1:SetValue(style.fLineCol.value)
			self.defaultFillColor1 = self.fillColor1:Value()
			self.defaultStrokeColor1 = self.strokeColor1:Value()			
		end
	elseif (msg == self.BUTTON2) then
		local style = moho:CurrentEditStyle()
		if (style ~= nil) then
			self.fillColor2:SetValue(style.fFillCol.value)
			self.strokeColor2:SetValue(style.fLineCol.value)
			self.defaultFillColor2 = self.fillColor2:Value()
			self.defaultStrokeColor2 = self.strokeColor2:Value()
		end
	elseif (msg == self.FILLCOL1) then
		self.defaultFillColor1 = self.fillColor1:Value()
	elseif (msg == self.FILLCOL2) then
		self.defaultFillColor2 = self.fillColor2:Value()
	elseif (msg == self.STROKECOL1) then
		self.defaultStrokeColor1 = self.strokeColor1:Value()
	elseif (msg == self.STROKECOL2) then
		self.defaultStrokeColor2 = self.strokeColor2:Value()
	end
end

Icon
Recolor Shape
Listed

Author: Stan View Script
Script type: Tool

Uploaded: Dec 26 2020, 23:50

Quickly change shape's color, creating a keyframe.
Image

Click a shape to recolor it to Style 1, Alt+click to recolor to Style 2.

Image
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: 168