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

ScriptName = "AE_ZoomSelection"

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

AE_ZoomSelection = {}

function AE_ZoomSelection:Name()
	return 'Zoom Selection'
end

function AE_ZoomSelection:Version()
	return '1.4'
end

function AE_ZoomSelection:UILabel()
	return 'Zoom Selection'
end

function AE_ZoomSelection:Creator()
	return 'Alexandra Evseeva'
end

function AE_ZoomSelection:Description()
	return 'Zoom Selection'
end


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

function AE_ZoomSelection:IsRelevant(moho)
	return true
end

function AE_ZoomSelection:IsEnabled(moho)
	return true
end

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

function AE_ZoomSelection:GetBoneGlobalPos(moho, bone, x)
	local matrix = bone.fMovedMatrix
	
	local testVector = LM.Vector2:new_local()
	if x then testVector:Set(x, 0) end
	
	matrix:Transform(testVector)
	
	return testVector
end

function AE_ZoomSelection:FindAreaForBoneLayer(moho)
	local skel = moho:Skeleton()	
	if skel:SelectedBoneID() == -1 then return moho.layer:Bounds() end
	local rect = LM.BBox:new_local()
	for b = 0, skel:CountBones()-1 do
		local nextBone = skel:Bone(b)
		if nextBone.fSelected then
			local globalPos = self:GetBoneGlobalPos(moho, nextBone)
			rect:AccumulatePoint(globalPos)
			local boneEnd = self:GetBoneGlobalPos(moho, nextBone, nextBone.fLength)
			rect:AccumulatePoint(boneEnd)
		end
	end	
	rect.fMin:Set(rect.fMin.x, rect.fMin.y, 0)
	rect.fMax:Set(rect.fMax.x, rect.fMax.y, 0)
	return rect 
end

function AE_ZoomSelection:Run(moho)
	moho.document:SetDirty()
	moho.document:PrepUndo(nil)
	
	local rect = nil
	
	if moho.layer:LayerType() == MOHO.LT_BONE then
		rect = self:FindAreaForBoneLayer(moho)	
	elseif moho.layer:LayerType() == MOHO.LT_VECTOR then
		moho.view:ResetView(1)
		return 
	else rect = moho.layer:Bounds()
	end
	
	if not rect then return end
	
	local matrix = LM.Matrix:new_local()
	moho.layer:GetFullTransform(moho.frame, matrix, moho.document)
	
	local center = rect:Center2D()	
	matrix:Transform(center)
	
	local graphics = moho.view:Graphics()	
	graphics:SetViewOffset(-center.x, -center.y)	

	matrix:Transform(rect)
	local size = rect.fMax - rect.fMin
	local vSize = size.y

	if (size.x/size.y) > (moho.view:Width()/moho.view:Height()) then
		vSize = size.x * (moho.view:Height()/moho.view:Width())
	end

	vSize = vSize * 1.1
	if vSize == 0 then vSize = 0.1 end
	local zoom = 2 * (0.2 - rect:Center().z)/vSize

	graphics:SetViewZoom(zoom)
	
end

Icon
Zoom selection
Listed

Script type: Button/Menu

Uploaded: Jun 06 2021, 02:34

Last modified: Jun 06 2021, 10:15

Zoom viewport to selected points' or bones' bounding box

Installation Options:

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