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

ScriptName = "SZ_MoveSelectedToCoord"

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

SZ_MoveSelectedToCoord = {}

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

function SZ_MoveSelectedToCoord:Version()
	return "1.1"
end

function SZ_MoveSelectedToCoord:UILabel()
	return self:Localize("UILabel")
end

function SZ_MoveSelectedToCoord:Creator()
	return "Stan"
end

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

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

SZ_MoveSelectedToCoord.coordX = 0
SZ_MoveSelectedToCoord.coordY = 0
SZ_MoveSelectedToCoord.defaultX = 0
SZ_MoveSelectedToCoord.defaultY = 0
SZ_MoveSelectedToCoord.applyX = true
SZ_MoveSelectedToCoord.applyY = true
SZ_MoveSelectedToCoord.defaultApplyX = true
SZ_MoveSelectedToCoord.defaultApplyY = true

-- **************************************************
-- Is Relevant / Is Enabled
-- **************************************************

function SZ_MoveSelectedToCoord:IsRelevant(moho)
	return true
end

function SZ_MoveSelectedToCoord:IsEnabled(moho)
	return true
end

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

function SZ_MoveSelectedToCoord:OnMouseDown(moho, mouseEvent)
	
end

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

SZ_MoveSelectedToCoord.GET_POS = MOHO.MSG_BASE
SZ_MoveSelectedToCoord.MOVE = MOHO.MSG_BASE + 1
SZ_MoveSelectedToCoord.GENERIC = MOHO.MSG_BASE + 2
SZ_MoveSelectedToCoord.RESET = MOHO.MSG_BASE + 3

function SZ_MoveSelectedToCoord:DoLayout(moho, layout)
	layout:AddPadding(20)
	
	self.get_pos = LM.GUI.Button(self:Localize('GetPos'), self.GET_POS)
	layout:AddChild(self.get_pos, LM.GUI.ALIGN_LEFT, 0)
	
	layout:AddPadding(10)
	
	self.coordXtxt = LM.GUI.TextControl(0, '000000000', self.GENERIC, LM.GUI.FIELD_FLOAT, 'X:')
	layout:AddChild(self.coordXtxt, LM.GUI.ALIGN_LEFT, 0)
	
	self.checkbox_x = LM.GUI.CheckBox(self:Localize(''), self.GENERIC)
    layout:AddChild(self.checkbox_x, LM.GUI.ALIGN_LEFT, 0)
	
	layout:AddPadding(5)

	self.coordYtxt = LM.GUI.TextControl(0, '000000000', self.GENERIC, LM.GUI.FIELD_FLOAT, 'Y:')
	layout:AddChild(self.coordYtxt, LM.GUI.ALIGN_LEFT, 0)
	
	self.checkbox_y = LM.GUI.CheckBox(self:Localize(''), self.GENERIC)
    layout:AddChild(self.checkbox_y, LM.GUI.ALIGN_LEFT, 0)
	
	layout:AddPadding(10)
	
	self.move = LM.GUI.Button(self:Localize('Move'), self.MOVE)
	layout:AddChild(self.move, LM.GUI.ALIGN_LEFT, 0)
	
	layout:AddPadding(10)

    self.resetBtn = LM.GUI.Button(self:Localize('Reset'), self.RESET)
    layout:AddChild(self.resetBtn, LM.GUI.ALIGN_LEFT, 0)
end

function SZ_MoveSelectedToCoord:UpdateWidgets(moho)
	self.coordXtxt:SetValue(self.coordX)
	self.coordYtxt:SetValue(self.coordY)
	self.checkbox_x:SetValue(self.applyX)
	self.checkbox_y:SetValue(self.applyY)
end

function SZ_MoveSelectedToCoord:HandleMessage(moho, view, msg)
	local skel = moho:Skeleton()
	local mesh = moho:Mesh()
	if not skel and not mesh then return end
	
	moho.document:PrepUndo(moho.layer)
	moho.document:SetDirty()
	
	if msg == self.GET_POS then
		if skel then
			local boneId = skel:SelectedBoneID()
			if boneId then
				local bone = skel:Bone(boneId)
				local pos = bone.fAnimPos:GetValue(moho.layerFrame)
				self.coordX = pos.x
				self.coordY = pos.y
				self:UpdateWidgets()
			end
		else
			if mesh then
				for i=0, mesh:CountPoints()-1 do
					local point = mesh:Point(i)
					if point.fSelected then
						local pos = point.fAnimPos:GetValue(moho.layerFrame)
						self.coordX = pos.x
						self.coordY = pos.y
						self:UpdateWidgets()
						break
					end
				end				
			end
		end
	elseif msg == self.MOVE then
		if skel then
			for i=0, skel:CountBones()-1 do
				local bone = skel:Bone(i)
				if bone.fSelected then
					local vec2 = LM.Vector2:new_local()
					local bonePos = bone.fAnimPos:GetValue(moho.layerFrame)
					local newX = self.applyX and self.coordX or bonePos.x
					local newY = self.applyY and self.coordY or bonePos.y
					vec2:Set(newX, newY)
					bone.fAnimPos:SetValue(moho.layerFrame, vec2)
				end
			end
		else
			if mesh then
				for i=0, mesh:CountPoints()-1 do
					local point = mesh:Point(i)
					if point.fSelected then
						local vec2 = LM.Vector2:new_local()
						local pointPos = point.fAnimPos:GetValue(moho.layerFrame)
						local newX = self.applyX and self.coordX or pointPos.x
						local newY = self.applyY and self.coordY or pointPos.y
						vec2:Set(newX, newY)
						point.fAnimPos:SetValue(moho.layerFrame, vec2)
					end
				end				
			end
		end
		moho.layer:UpdateCurFrame()
		moho:UpdateUI()
	elseif msg == self.GENERIC then
		self.applyX = self.checkbox_x:Value()
		self.applyY = self.checkbox_y:Value()
		self.coordX = self.coordXtxt:FloatValue()
		self.coordY = self.coordYtxt:FloatValue()
		self:UpdateWidgets()
	elseif msg == self.RESET then
		self.applyX = self.defaultApplyX
		self.applyY = self.defaultApplyY
		self.coordX = self.defaultX
		self.coordY = self.defaultY
		self:UpdateWidgets()
	end	
end

-- **************************************************
-- Localization
-- **************************************************

function SZ_MoveSelectedToCoord:Localize(text)
	local fileWord = MOHO.Localize("/Menus/File/File=File")
	
	local phrase = {}
	
	phrase["Name"] = "Move selected points/bones to one coordinate"
	phrase["Description"] = "Move selected points/bones to one coordinate"
	phrase["UILabel"] = "Move selected points/bones to one coordinate"
	phrase["GetPos"] = "Get position of current selection"
	phrase["Move"] = "Move selected"
	phrase["Reset"] = "Reset"
	
	if fileWord == "Файл" then
		phrase["Name"] = "Сместить выделенные точки/кости в одну координату"
		phrase["Description"] = "Сместить выделенные точки/кости в одну координату"
		phrase["UILabel"] = "Сместить выделенные точки/кости в одну координату"
		phrase["GetPos"] = "Взять позицию выделенной точки/кости"
		phrase["Move"] = "Переместить выделенное"
		phrase["Reset"] = "Сброс"
	end
	
	return phrase[text]
end

Icon
Move selected to coord
Listed

Author: Stan View Script
Script type: Tool

Uploaded: Jul 13 2020, 00:37

Move selected points/bones into a single coordinate
Image

This tool allows you to move selected points or bones into a single dot or a line.

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: 217