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

ScriptName = "SZ_BoneSelectionButtons"

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

SZ_BoneSelectionButtons = {}

-- **************************************************
-- Settings
-- **************************************************

SZ_BoneSelectionButtons.numberOfGroupButtons = 8
SZ_BoneSelectionButtons.initialString = "WWWWWWWW" -- this string defines the width of the button

SZ_BoneSelectionButtons.delimiter = string.char(31) -- separates bone names in groups
SZ_BoneSelectionButtons.caseSensitive = false

SZ_BoneSelectionButtons.dividerSpace = -4

SZ_BoneSelectionButtons.saveSeparator = string.char(9) -- separates keys and values in saved presets
SZ_BoneSelectionButtons.scriptDataPrefix = "SZ_BoneSelectionButtons_"

SZ_BoneSelectionButtons.emptyLabel = "---"

-- **************************************************
-- General methods
-- **************************************************

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

function SZ_BoneSelectionButtons:Version()
	return "1.02"
end

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

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

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

function SZ_BoneSelectionButtons:SavePrefs(prefs)
	for i = 1, self.numberOfGroupButtons do
		prefs:SetString("SZ_BoneSelectionButtons.groupNames"..i, self.groupNames[i])
	end
end

function SZ_BoneSelectionButtons:LoadPrefs(prefs)
	for i = 1, self.numberOfGroupButtons do
		self.groupNames[i] = prefs:GetString("SZ_BoneSelectionButtons.groupNames"..i, "")
	end
end

function SZ_BoneSelectionButtons:ResetPrefs()
	for i = 1, self.numberOfGroupButtons do
		self.groupNames[i] = ""
		self.groupLabels[i] = self.emptyLabel
	end
end

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

SZ_BoneSelectionButtons.groupNames = {}
SZ_BoneSelectionButtons.groupLabels = {}
for i = 1, SZ_BoneSelectionButtons.numberOfGroupButtons do
	SZ_BoneSelectionButtons.groupNames[i] = ""
	SZ_BoneSelectionButtons.groupLabels[i] = SZ_BoneSelectionButtons.emptyLabel
end
SZ_BoneSelectionButtons.selBoneID = -1
SZ_BoneSelectionButtons.selRect = LM.Rect:new_local()
SZ_BoneSelectionButtons.previousX = 0
SZ_BoneSelectionButtons.previousY = 0
SZ_BoneSelectionButtons.isMouseDragging = false
-- **************************************************
-- The guts of this script
-- **************************************************

function SZ_BoneSelectionButtons:IsEnabled(moho)
	local skel = moho:Skeleton()
	if skel then 
		return true
	end
	return false
end

function SZ_BoneSelectionButtons:IsRelevant(moho)
	local skel = moho:Skeleton()
	if skel then 
		return true
	end
	return false
end

function SZ_BoneSelectionButtons:OnMouseDown(moho, mouseEvent)
	self.isMouseDragging = true

	self.selRect.left = -10
	self.selRect.top = -10
	self.selRect.right = -5
	self.selRect.bottom = -5

	-- Try to select a bone by clicking on it
	self:Select(moho, mouseEvent.pt, mouseEvent.vec, mouseEvent.view, mouseEvent.shiftKey, mouseEvent.ctrlKey)

	-- If no bone was selected, start dragging out a marquee selection
	local lassoMode = false
	self.ctrlKeySelection = false
	if (self.lassoMode) then
		if (not(mouseEvent.ctrlKey)) then
			lassoMode = true
		else
			self.ctrlKeySelection = true
		end
	else
		if (mouseEvent.ctrlKey) then
			lassoMode = true
			self.ctrlKeySelection = true
		end
	end

	mouseEvent.view:DrawMe()
	if (self.selBoneID == -1) then
		if (lassoMode) then
			self.lassoList = { { mouseEvent.startPt.x, mouseEvent.startPt.y } }
			self.previousX = mouseEvent.startPt.x
			self.previousY = mouseEvent.startPt.y
		else
			self.selRect.left = mouseEvent.startPt.x
			self.selRect.top = mouseEvent.startPt.y
			self.selRect.right = mouseEvent.pt.x
			self.selRect.bottom = mouseEvent.pt.y
			mouseEvent.view:Graphics():SelectionRect(self.selRect)
		end
	end
end

function SZ_BoneSelectionButtons:OnMouseMoved(moho, mouseEvent)
	--self:Select(moho, mouseEvent.pt, mouseEvent.vec, mouseEvent.view, mouseEvent.shiftKey, mouseEvent.ctrlKey)
	if (self.selBoneID >= 0) then
		return
	end

	local lassoMode = false
	if (self.lassoMode) then
		if (not(self.ctrlKeySelection)) then
			lassoMode = true
		end
	else
		if (self.ctrlKeySelection) then
			lassoMode = true
		end
	end

	if (lassoMode) then
		local g = mouseEvent.view:Graphics()

		g:SetSmoothing(true)
		g:Push()
		local m = g:CurrentTransform()
		m:Invert()
		g:ApplyMatrix(m)
		g:SetColor(MOHO.MohoGlobals.SelCol)
		g:MoveTo(self.previousX, self.previousY)
		g:LineTo(mouseEvent.pt.x, mouseEvent.pt.y)
		g:Pop()
		g:SetSmoothing(false)
		mouseEvent.view:RefreshView()

		table.insert(self.lassoList, { mouseEvent.pt.x, mouseEvent.pt.y })
		self.previousX = mouseEvent.pt.x
		self.previousY = mouseEvent.pt.y
	else
		mouseEvent.view:Graphics():SelectionRect(self.selRect)
		self.selRect.right = mouseEvent.pt.x
		self.selRect.bottom = mouseEvent.pt.y
		mouseEvent.view:Graphics():SelectionRect(self.selRect)
		mouseEvent.view:RefreshView()
	end
	mouseEvent.view:DrawMe()
end

function SZ_BoneSelectionButtons:OnMouseUp(moho, mouseEvent)
	if (self.selBoneID >= 0) then
		return
	end

	self.isMouseDragging = false

	local lassoMode = false
	if (self.lassoMode) then
		if (not(self.ctrlKeySelection)) then
			lassoMode = true
		end
	else
		if (self.ctrlKeySelection) then
			lassoMode = true
		end
	end

	if (lassoMode) then
		-- draw the finalized lasso outline
		local g = mouseEvent.view:Graphics()

		g:SetSmoothing(true)
		g:Push()
		local m = g:CurrentTransform()
		m:Invert()
		g:ApplyMatrix(m)
		g:SetColor(MOHO.MohoGlobals.SelCol)
		g:MoveTo(self.previousX, self.previousY)
		g:LineTo(mouseEvent.startPt.x, mouseEvent.startPt.y)
		g:Pop()
		g:SetSmoothing(false)
		mouseEvent.view:RefreshView()
		LM.Snooze(100)
	end

	-- 1 - Draw the selection shape, either a lasso or a rectangle
	local end1 = LM.Vector2:new_local()
	local end2 = LM.Vector2:new_local()
	local g = mouseEvent.view:Graphics()
	g:Clear(0, 0, 0, 0)
	g:Push()
	local m = g:CurrentTransform()
	m:Invert()
	g:ApplyMatrix(m)
	g:SetColor(255, 255, 255, 255)
	g:BeginShape()
	if (lassoMode) then
		for i = 1, #self.lassoList - 1 do
			end1:Set(self.lassoList[i][1], self.lassoList[i][2])
			end2:Set(self.lassoList[i + 1][1], self.lassoList[i + 1][2])
			g:AddLine(end1, end2)
		end
		end1:Set(self.lassoList[#self.lassoList][1], self.lassoList[#self.lassoList][2])
		end2:Set(self.lassoList[1][1], self.lassoList[1][2])
		g:AddLine(end1, end2)
	else
		end1:Set(mouseEvent.startPt.x, mouseEvent.startPt.y)
		end2:Set(mouseEvent.pt.x, mouseEvent.startPt.y)
		g:AddLine(end1, end2)
		
		end1:Set(end2.x, end2.y)
		end2:Set(mouseEvent.pt.x, mouseEvent.pt.y)
		g:AddLine(end1, end2)
		
		end1:Set(end2.x, end2.y)
		end2:Set(mouseEvent.startPt.x, mouseEvent.pt.y)
		g:AddLine(end1, end2)
		
		end1:Set(end2.x, end2.y)
		end2:Set(mouseEvent.startPt.x, mouseEvent.startPt.y)
		g:AddLine(end1, end2)
	end
	g:EndShape()
	g:Pop()
	-- test code to view the lasso's shape
	--mouseEvent.view:RefreshView()
	--LM.Snooze(1000)

	-- 2 - Do hit testing on the selection shape
	local v = LM.Vector2:new_local()
	local screenPt = LM.Point:new_local()
	local m = LM.Matrix:new_local()
	local parentSkeleton = false
	local skel = moho:Skeleton()
	moho.layer:GetFullTransform(moho.frame, m, moho.document)
	if (skel == nil) then
		if (self:ShouldUseParentSkeleton(moho)) then
			skel = moho:ParentSkeleton()
			parentSkeleton = true
			moho.layer:ControllingBoneLayer():GetFullTransform(moho.frame, m, moho.document)
		end
	end

	if (skel ~= nil) then
		for i = 0, skel:CountBones() - 1 do
			local bone = skel:Bone(i)
			local boneMatrix = bone.fMovedMatrix
			if (moho.frame == 0) then
				boneMatrix = bone.fRestMatrix
			end
			for j = 0, 10 do
				v:Set(bone.fLength * j / 10.0, 0)
				boneMatrix:Transform(v)
				m:Transform(v)
				g:WorldToScreen(v, screenPt)
				if (g:IsFullWhite(screenPt)) then
					if (mouseEvent.shiftKey) then
						bone.fSelected = not bone.fSelected
					else
						bone.fSelected = true
					end
					break
				end
			end
		end
	end

	self.lassoList = nil

	mouseEvent.view:DrawMe()
	moho:UpdateSelectedChannels()
end

function SZ_BoneSelectionButtons:Select(moho, mousePt, mouseVec, mouseView, shiftSelect, ctrlSelect)
	self.selBoneID = -1

	local parentSkeleton = false
	local skel = moho:Skeleton()
	if (skel == nil) then
		if (self:ShouldUseParentSkeleton(moho)) then
			skel = moho:ParentSkeleton()
			parentSkeleton = true
		end
		if (skel == nil) then
			return
		end
	end

	local id = -1
	if (parentSkeleton) then
		id = mouseView:PickBone(mousePt, mouseVec, moho.layer:ControllingBoneLayer(), true)
	else
		id = mouseView:PickBone(mousePt, mouseVec, moho.layer, true)
	end
	self.selBoneID = id

	if (shiftSelect) then
		if (id >= 0) then
			skel:Bone(id).fSelected = true
		end
	elseif (ctrlSelect) then
		if (id >= 0) then
			skel:Bone(id).fSelected = not skel:Bone(id).fSelected
		end
	else
		for i = 0, skel:CountBones() - 1 do
			skel:Bone(i).fSelected = (i == id)
		end
	end

	moho:UpdateBonePointSelection()
	mouseView:DrawMe()
	moho:UpdateSelectedChannels()
end

function SZ_BoneSelectionButtons:OnKeyDown(moho, keyEvent)
	local skel = moho:Skeleton()
	if (skel == nil) then
		if (self:ShouldUseParentSkeleton(moho)) then
			skel = moho:ParentSkeleton()
		end
		if (skel == nil) then
			return
		end
	end

	if (keyEvent.keyCode == LM.GUI.KEY_UP and not keyEvent.ctrlKey) then
		for i = 0, skel:CountBones() - 1 do
			if (skel:Bone(i).fSelected) then
				local pID = skel:Bone(i).fParent
				if (pID >= 0) then
					skel:Bone(i).fSelected = false
					skel:Bone(pID).fSelected = true
				end
				break
			end
		end
		moho:UpdateBonePointSelection()
		keyEvent.view:DrawMe()
		moho:UpdateSelectedChannels()
	elseif (keyEvent.keyCode == LM.GUI.KEY_DOWN and not keyEvent.ctrlKey) then
		for i = 0, skel:CountBones() - 1 do
			if (skel:Bone(i).fSelected) then
				local childID = -1
				if (skel:CountBoneChildren(i) == 1) then
					childID = skel:GetFirstChildBone(i)
				end
				if (childID >= 0) then
					skel:Bone(i).fSelected = false
					skel:Bone(childID).fSelected = true
				end
				break
			end
		end
		moho:UpdateBonePointSelection()
		keyEvent.view:DrawMe()
		moho:UpdateSelectedChannels()
	elseif ((keyEvent.keyCode == LM.GUI.KEY_DELETE) or (keyEvent.keyCode == LM.GUI.KEY_BACKSPACE)) then
		if (moho.layer:IsBoneType() and (moho.layerFrame == 0 or not moho:DisableDrawingTools())) then
			local boneLayer = moho:LayerAsGroup(moho.layer)
			moho.document:PrepUndo(moho.layer, false)
			moho.document:SetDirty()
			keepTrying = true
			while keepTrying do
				keepTrying = false
				for i = 0, skel:CountBones() - 1 do
					if (skel:Bone(i).fSelected) then
						for j = 0, boneLayer:CountLayers() - 1 do
							boneLayer:Layer(j):DeleteParentBone(i)
						end
						skel:DeleteBone(i)
						keepTrying = true
						break
					end
				end
			end
			moho:SetCurFrame(moho.frame) -- trigger an update of all the bones in case they're undergoing a preview with the manipulate bones tool or something
			keyEvent.view:DrawMe()
		end
	end
end

function SZ_BoneSelectionButtons:DrawMe(moho, view)
	if (self.isMouseDragging) then
		local g = view:Graphics()
		local lassoMode = false
		if (self.lassoMode) then
			if (not(self.ctrlKeySelection)) then
				lassoMode = true
			end
		else
			if (self.ctrlKeySelection) then
				lassoMode = true
			end
		end

		if (lassoMode and self.lassoList) then
			g:SetSmoothing(true)
			g:Push()
			local m = g:CurrentTransform()
			m:Invert()
			g:ApplyMatrix(m)
			g:SetColor(MOHO.MohoGlobals.SelCol)
			g:MoveTo(self.lassoList[1][1], self.lassoList[1][2])
			for i = 2, #self.lassoList do
				g:LineTo(self.lassoList[i][1], self.lassoList[i][2])
			end
			g:Pop()
			g:SetSmoothing(false)
		else
			g:SelectionRect(self.selRect)
		end
	end
end

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

SZ_BoneSelectionButtons.CLEAR_ALL = MOHO.MSG_BASE
SZ_BoneSelectionButtons.SAVE = MOHO.MSG_BASE + 1
SZ_BoneSelectionButtons.LOAD = MOHO.MSG_BASE + 2
SZ_BoneSelectionButtons.BUTTON_MSG_BASE = MOHO.MSG_BASE + 3
SZ_BoneSelectionButtons.ALT_BUTTON_MSG_BASE = MOHO.MSG_BASE + 103


SZ_BoneSelectionButtons.groupButtons = {}

function SZ_BoneSelectionButtons:DoLayout(moho, layout)
	for i=1, self.numberOfGroupButtons do
		self.groupButtons[i] = LM.GUI.Button(self.initialString, self.BUTTON_MSG_BASE + i)
		self.groupButtons[i]:SetAlternateMessage(self.ALT_BUTTON_MSG_BASE + i)
		layout:AddChild(self.groupButtons[i])
	end
	layout:AddPadding(self.dividerSpace)
	layout:AddChild(LM.GUI.Divider(true), LM.GUI.ALIGN_FILL, 0)
	layout:AddPadding(self.dividerSpace)
	layout:AddChild(LM.GUI.Button(self:Localize("ClearAll"), self.CLEAR_ALL))
	layout:AddPadding(self.dividerSpace)
	layout:AddChild(LM.GUI.Divider(true), LM.GUI.ALIGN_FILL, 0)
	layout:AddPadding(self.dividerSpace)
	layout:AddChild(LM.GUI.Button(self:Localize("Save"), self.SAVE))
	layout:AddChild(LM.GUI.Button(self:Localize("Load"), self.LOAD))
end

function SZ_BoneSelectionButtons:UpdateWidgets(moho)
	local scriptData = moho.layer:ScriptData()
	for i=1, self.numberOfGroupButtons do
		if scriptData:HasKey(self.scriptDataPrefix .. "names_" .. i) and scriptData:HasKey(self.scriptDataPrefix .. "label_" .. i) then
			local label = scriptData:GetString(self.scriptDataPrefix .. "label_" .. i)
			local names = scriptData:GetString(self.scriptDataPrefix .. "names_" .. i)
			self.groupLabels[i] = label
			self.groupNames[i] = names
			self.groupButtons[i]:SetLabel(label, false)
			self.groupButtons[i]:SetToolTip(self:GetNamesStringBeautified(self.groupNames[i]))
		else
			self.groupNames[i] = ""
			self.groupLabels[i] = self.emptyLabel
			self.groupButtons[i]:SetLabel(self.groupLabels[i], false)
			self.groupButtons[i]:SetToolTip(nil)
		end
	end
end

function SZ_BoneSelectionButtons:HandleMessage(moho, view, msg)
	local scriptData = moho.layer:ScriptData()
	
	if msg == self.CLEAR_ALL then
		if LM.GUI.Alert(LM.GUI.ALERT_WARNING, self:Localize("clearConfirm1"), self:Localize("clearConfirm2"), nil, self:Localize("OK"), self:Localize("Cancel")) == 0 then  
			for i=1, self.numberOfGroupButtons do
				self.groupNames[i] = ""
				self.groupLabels[i] = self.emptyLabel
				scriptData:Remove(self.scriptDataPrefix .. "names_" .. i)
				scriptData:Remove(self.scriptDataPrefix .. "label_" .. i)
			end
			self:UpdateWidgets(moho)
		end
	elseif msg == self.SAVE then
		local fileName = LM.GUI.SaveFile(self:Localize("Name") .. " - " .. self:Localize("SaveCaption"))		
		if fileName ~= "" then
			local str = ""
			for i=1, self.numberOfGroupButtons do
				str = str .. i .. self.saveSeparator .. self.groupLabels[i] .. self.saveSeparator .. self.groupNames[i] .. "\n"
			end
			local f = io.open(fileName, "w")
			if f == nil then
				LM.GUI.Alert(LM.GUI.ALERT_WARNING, self:Localize("FileSaveError1"), self:Localize("FileSaveError2"), self:Localize("FileSaveError3"), self:Localize("OK"))
			else		
				f:write(str)
				f:close()
			end
		end		
	elseif msg == self.LOAD then
		local fileName = LM.GUI.OpenFile(self:Localize("Name") .. " - " .. self:Localize("LoadCaption"))
		if fileName ~= "" then
			local fileLines = io.lines(fileName)
			if fileLines then
				for line in fileLines do
					local i, label, names = string.match(line, "(.+)"..self.saveSeparator.."(.+)"..self.saveSeparator.."(.*)")
					-- print(tostring(i))
					self.groupLabels[tonumber(i)] = label
					scriptData:Set(self.scriptDataPrefix .. "label_" .. i, label)
					self.groupNames[tonumber(i)] = names
					scriptData:Set(self.scriptDataPrefix .. "names_" .. i, names)
				end
			end
			self:UpdateWidgets(moho)
		end
	else	
		for i=1, self.numberOfGroupButtons do
			if msg == self.BUTTON_MSG_BASE + i then
				self:SearchBones(moho, i)
				break
			elseif msg == self.ALT_BUTTON_MSG_BASE + i then
				if moho:CountSelectedBones() > 0 then
					local label = self:GetLabelName(moho, i)
					if label then
						self.groupLabels[i] = label
						scriptData:Set(self.scriptDataPrefix .. "label_" .. i, label)
						local names = self:GetSelectedBonesSearchString(moho)
						self.groupNames[i] = names
						scriptData:Set(self.scriptDataPrefix .. "names_" .. i, names)
						self:UpdateWidgets(moho)
					end
				end
				break
			end
		end
	end
end

-- **************************************************
-- The search itself
-- **************************************************

function SZ_BoneSelectionButtons:SearchBones(moho, i)
	if self.groupNames[i] == "" then return end
	moho.document:PrepUndo(moho.layer)
	moho.document:SetDirty()
	local searchStrings = self:Split(self.groupNames[i], self.delimiter)
	local skel = moho:Skeleton()
	-- deselect all bones:
	for i=0, skel:CountBones()-1 do
		local bone = skel:Bone(i)
		bone.fSelected = false
	end
	-- do the search:
	for i=0, skel:CountBones()-1 do
		local bone = skel:Bone(i)
		local name = bone:Name()
		for _, searchString in ipairs(searchStrings) do
			if not self.caseSensitive then
				searchString = string.lower(searchString)
				name = string.lower(name)
			end
			if string.match(name, searchString) then
				bone.fSelected = true
			end
		end
	end
	moho:UpdateUI()
end
-- **************************************************
-- Dialog
-- **************************************************

SZ_BoneSelectionButtonsDialog = {}
	
function SZ_BoneSelectionButtonsDialog:new(moho, i)
	local d = LM.GUI.SimpleDialog(SZ_BoneSelectionButtons:Localize('DialogLabel'), SZ_BoneSelectionButtonsDialog)
	d.i = i
	local l = d:GetLayout()
	l:AddChild(LM.GUI.StaticText(SZ_BoneSelectionButtons:Localize('enterGroupName')), LM.GUI.ALIGN_LEFT)
	local selectedBones = SZ_BoneSelectionButtons:GetNamesStringBeautified(SZ_BoneSelectionButtons:GetSelectedBonesSearchString(moho), false)
	d.textInput = LM.GUI.TextControl(200, selectedBones, 0, LM.GUI.FIELD_TEXT)
	l:AddChild(d.textInput, LM.GUI.ALIGN_LEFT)	
	return d
end

function SZ_BoneSelectionButtonsDialog:OnValidate()
	local b = true
	if self.textInput:Value() == "" then
		b = false
	end
	return b
end

function SZ_BoneSelectionButtonsDialog:UpdateWidgets()
	SZ_BoneSelectionButtons.groupLabels[self.i] = self.textInput:Value()
end

function SZ_BoneSelectionButtonsDialog:OnOK()
	SZ_BoneSelectionButtons.groupLabels[self.i] = self.textInput:Value()
end

function SZ_BoneSelectionButtons:GetLabelName(moho, i)
	local nameDialog = SZ_BoneSelectionButtonsDialog:new(moho, i)
	if nameDialog:DoModal() == LM.GUI.MSG_CANCEL then
		return false
	end
	return self.groupLabels[i]
end

-- **************************************************
-- Utility methods
-- **************************************************

function SZ_BoneSelectionButtons:GetNamesStringBeautified(namesString, addLeadingAndTrailingSpaces)
	local arr = self:Split(namesString, self.delimiter)
	local result = self:Join(arr, ", ")
	result = result:gsub('%$', '')
	result = result:gsub('%^', '')
	result = result:gsub('%%%%', string.char(26))
	result = result:gsub('%%', '')
	result = result:gsub(string.char(26), '%%')
	if addLeadingAndTrailingSpaces then
		result = '   ' .. result .. '   '
	end
	return result
end

function SZ_BoneSelectionButtons:Split(s, delimiter)
	-- https://stackoverflow.com/questions/1426954/split-string-in-lua
	local result = {}
	for match in (s..delimiter):gmatch("(.-)"..delimiter) do
		table.insert(result, match)
	end
	return result
end

function SZ_BoneSelectionButtons:Join(arr, delimiter)
	local result = ""
	for _, str in ipairs(arr) do
		result = result .. str .. delimiter
	end
	result = result:gsub(delimiter .. '$', '')
	return result
end

function SZ_BoneSelectionButtons:GetSelectedBonesSearchString(moho)
	local result = ""
	local skel = moho:Skeleton()
	for i=0, skel:CountBones()-1 do
		local bone = skel:Bone(i)
		if bone.fSelected then
			result = result .. '^' .. self:EscapeSpecialChars(bone:Name()) .. '$' .. self.delimiter
		end
	end
	result = result:gsub(self.delimiter .. '$', '')
	return result
end

function SZ_BoneSelectionButtons:EscapeSpecialChars(str)
	str = str:gsub('%%', '%%%%')
	str = str:gsub('%.', '%%.')
	str = str:gsub('%+', '%%+')
	str = str:gsub('%-', '%%-')
	str = str:gsub('%?', '%%?')
	str = str:gsub('%*', '%%*')
	str = str:gsub('%^', '%%^')
	str = str:gsub('%$', '%%$')
	str = str:gsub('%(', '%%(')
	str = str:gsub('%)', '%%)')
	str = str:gsub('%[', '%%[')
	str = str:gsub('%]', '%%]')
	return str
end

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

function SZ_BoneSelectionButtons:Localize(text)
	local fileWord = MOHO.Localize("/Menus/File/File=File")
	
	local phrase = {}
	
	phrase["Name"] = "Bone Selection Buttons"
	phrase["Description"] = "Select bones by clicking a button. Alt+click a button to create a group from the current selection."
	phrase["Label"] = "Bone Selection Buttons"
	phrase["ClearAll"] = "Clear all"		
	phrase["Group "] = "Group "		
	phrase["Save"] = "Save"		
	phrase["SaveCaption"] = "Save bone groups"
	phrase["Load"] = "Load"
	phrase["LoadCaption"] = "Load bone groups"
	phrase["FileSaveError1"] = "Error creating file!"
	phrase["FileSaveError2"] = "Perhaps the path contains non-English characters."
	phrase["FileSaveError3"] = "Please try saving in another location, with English characters only."
	phrase["OK"] = "OK"
	phrase["Cancel"] = "Cancel"
	phrase["DialogLabel"] = "Define a bone group"
	phrase["enterGroupName"] = "Enter group name"
	phrase["clearConfirm1"] = "Warning! This will clear all buttons!"
	phrase["clearConfirm2"] = "Are you sure?"
	
	if fileWord == "Файл" then
		phrase["Name"] = "Кнопки выделения костей"
		phrase["Description"] = "Выделяет группу костей по клику на кнопку. Alt+клик на кнопку создает группу из текущего выделения."
		phrase["Label"] = "Кнопки выделения костей"
		phrase["ClearAll"] = "Очистить все"		
		phrase["Group "] = "Группа "		
		phrase["Save"] = "Сохранить"		
		phrase["SaveCaption"] = "Сохранить настройки групп"
		phrase["Load"] = "Загрузить"
		phrase["LoadCaption"] = "Загрузить настройки групп"
		phrase["FileSaveError1"] = "Не удалось создать файл!"
		phrase["FileSaveError2"] = "Скорее всего, из-за неанглийских букв в пути."
		phrase["FileSaveError3"] = "Попробуйте сохранить в папку, названную английскими буквами."
		phrase["OK"] = "OK"
		phrase["Cancel"] = "Отмена"
		phrase["DialogLabel"] = "Назначить группу костей"
		phrase["enterGroupName"] = "Введите имя группы"
		phrase["clearConfirm1"] = "Внимание! Это очистит все кнопки!"
		phrase["clearConfirm2"] = "Вы уверены?"
	end
	
	return phrase[text];
end

Icon
Bone Selection Buttons
Listed

Author: Stan View Script
Script type: Tool

Uploaded: Dec 28 2020, 23:51

Last modified: Jan 10 2021, 09:09

Buttons that can be assigned for quick selection of bones.

Image




See also: Layer Selection Buttons tool.
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: 285