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

ScriptName = "SZ_TimelineMarkersTool"

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

SZ_TimelineMarkersTool = {}

function SZ_TimelineMarkersTool:Name()
	return self:Localize('UILabel')
end

function SZ_TimelineMarkersTool:Version()
	return '1.0'
end

function SZ_TimelineMarkersTool:UILabel()
	return self:Localize('UILabel')
end

function SZ_TimelineMarkersTool:Creator()
	return 'Stan from 2danimator.ru'
end

function SZ_TimelineMarkersTool:Description()
	return self:Localize('Description')
end

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

function SZ_TimelineMarkersTool:IsRelevant(moho)
	return true
end

function SZ_TimelineMarkersTool:IsEnabled(moho)
	return true
end

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

SZ_TimelineMarkersTool.shortSpace = 3
SZ_TimelineMarkersTool.mediumSpace = 8
SZ_TimelineMarkersTool.longSpace = 12
SZ_TimelineMarkersTool.negativeSpace = -16

-- **************************************************
-- Recurring Values
-- **************************************************

SZ_TimelineMarkersTool.startFrame = 1
SZ_TimelineMarkersTool.endFrame = 240
SZ_TimelineMarkersTool.interval = 24
SZ_TimelineMarkersTool.duration = 0
SZ_TimelineMarkersTool.label = ''
SZ_TimelineMarkersTool.documentWide = true
SZ_TimelineMarkersTool.colorCode = 11

-- **************************************************
-- Prefs
-- **************************************************

function SZ_TimelineMarkersTool:LoadPrefs(prefs)
    self.startFrame = prefs:GetInt("SZ_TimelineMarkersTool.startFrame", 1)
    self.endFrame = prefs:GetInt("SZ_TimelineMarkersTool.endFrame", 240)
    self.interval = prefs:GetInt("SZ_TimelineMarkersTool.interval", 24)
    self.duration = prefs:GetInt("SZ_TimelineMarkersTool.duration", 0)
    self.label = prefs:GetString("SZ_TimelineMarkersTool.label", '')
    self.documentWide = prefs:GetBool("SZ_TimelineMarkersTool.documentWide", true)
    self.colorCode = prefs:GetInt("SZ_TimelineMarkersTool.colorCode", 11)
end

function SZ_TimelineMarkersTool:SavePrefs(prefs)
    prefs:SetInt("SZ_TimelineMarkersTool.startFrame", self.startFrame)
    prefs:SetInt("SZ_TimelineMarkersTool.endFrame", self.endFrame)
    prefs:SetInt("SZ_TimelineMarkersTool.interval", self.interval)
    prefs:SetInt("SZ_TimelineMarkersTool.duration", self.duration)
    prefs:SetString("SZ_TimelineMarkersTool.label", self.label)
    prefs:SetBool("SZ_TimelineMarkersTool.documentWide", self.documentWide)
    prefs:SetInt("SZ_TimelineMarkersTool.colorCode", self.colorCode)
end

function SZ_TimelineMarkersTool:ResetPrefs()
    self.startFrame = 1
    self.endFrame = 240
    self.interval = 24
    self.duration = 0
    self.label = ''
    self.documentWide = true
    self.colorCode = 11
end

-- **************************************************
-- Keyboard/Mouse Control
-- **************************************************

function SZ_TimelineMarkersTool:OnMouseDown(moho, mouseEvent)
	
end

-- **************************************************
-- Tool Panel Layout
-- **************************************************

SZ_TimelineMarkersTool.START_FRAME = MOHO.MSG_BASE
SZ_TimelineMarkersTool.END_FRAME = MOHO.MSG_BASE + 1
SZ_TimelineMarkersTool.INTERVAL = MOHO.MSG_BASE + 2
SZ_TimelineMarkersTool.DURATION = MOHO.MSG_BASE + 3
SZ_TimelineMarkersTool.LABEL = MOHO.MSG_BASE + 4
SZ_TimelineMarkersTool.DOCUMENTWIDE = MOHO.MSG_BASE + 5
SZ_TimelineMarkersTool.CREATE = MOHO.MSG_BASE + 6
SZ_TimelineMarkersTool.REMOVE = MOHO.MSG_BASE + 7
SZ_TimelineMarkersTool.CLEAR_LAYER = MOHO.MSG_BASE + 8
SZ_TimelineMarkersTool.CLEAR_DOC = MOHO.MSG_BASE + 9
SZ_TimelineMarkersTool.GET_CURRENT_FRAME = MOHO.MSG_BASE + 10
SZ_TimelineMarkersTool.GET_END_FRAME = MOHO.MSG_BASE + 11
SZ_TimelineMarkersTool.CLEAR_LABEL = MOHO.MSG_BASE + 12

SZ_TimelineMarkersTool.PLAIN		= MOHO.MSG_BASE + 100
SZ_TimelineMarkersTool.RED			= MOHO.MSG_BASE + 100 + 1
SZ_TimelineMarkersTool.ORANGE		= MOHO.MSG_BASE + 100 + 2
SZ_TimelineMarkersTool.YELLOW		= MOHO.MSG_BASE + 100 + 3
SZ_TimelineMarkersTool.GREEN		= MOHO.MSG_BASE + 100 + 4
SZ_TimelineMarkersTool.BLUE			= MOHO.MSG_BASE + 100 + 5
SZ_TimelineMarkersTool.PURPLE		= MOHO.MSG_BASE + 100 + 6
SZ_TimelineMarkersTool.TAN			= MOHO.MSG_BASE + 100 + 7
SZ_TimelineMarkersTool.PINK			= MOHO.MSG_BASE + 100 + 8
SZ_TimelineMarkersTool.TURQUOISE	= MOHO.MSG_BASE + 100 + 9
SZ_TimelineMarkersTool.CADET_BLUE	= MOHO.MSG_BASE + 100 + 10
SZ_TimelineMarkersTool.CORAL		= MOHO.MSG_BASE + 100 + 11

function SZ_TimelineMarkersTool:DoLayout(moho, layout)
	local path = moho:UserAppDir()
	local pc = string.find(path,"\\")
	local mac = string.find(path,"/")
	local delim = ""
	if mac ~= nil then
		delim =  "/"
	end

	if pc ~= nil then
		delim = "\\"
	end


	path = "ScriptResources" .. delim .. "sz_timeline_markers_tool" .. delim
	
	self.markerColorMenu = LM.GUI.Menu(self:Localize('Marker Color'))
	self.markerColorMenu_popup = LM.GUI.PopupMenu(100, true)
	self.markerColorMenu_popup:SetMenu(self.markerColorMenu)
	layout:AddChild(self.markerColorMenu_popup, LM.GUI.ALIGN_LEFT, 0)
	self.markerColorMenu:AddItem(self:Localize('Plain'), 0, self.PLAIN)
	self.markerColorMenu:AddItem(self:Localize('Red'), 0, self.RED)
	self.markerColorMenu:AddItem(self:Localize('Orange'), 0, self.ORANGE)
	self.markerColorMenu:AddItem(self:Localize('Yellow'), 0, self.YELLOW)
	self.markerColorMenu:AddItem(self:Localize('Green'), 0, self.GREEN)
	self.markerColorMenu:AddItem(self:Localize('Blue'), 0, self.BLUE)
	self.markerColorMenu:AddItem(self:Localize('Purple'), 0, self.PURPLE)
	self.markerColorMenu:AddItem(self:Localize('Tan'), 0, self.TAN)
	self.markerColorMenu:AddItem(self:Localize('Pink'), 0, self.PINK)
	self.markerColorMenu:AddItem(self:Localize('Turquoise'), 0, self.TURQUOISE)
	self.markerColorMenu:AddItem(self:Localize('Cadet Blue'), 0, self.CADET_BLUE)
	self.markerColorMenu:AddItem(self:Localize('Coral'), 0, self.CORAL)

	layout:AddPadding(self.shortSpace)

    self.startFrameInput = LM.GUI.TextControl(0, '000', self.START_FRAME, LM.GUI.FIELD_UINT, self:Localize('Start Frame:'))
    layout:AddChild(self.startFrameInput, LM.GUI.ALIGN_LEFT, 0)
	layout:AddPadding(self.negativeSpace)
	self.currentFrameButton = LM.GUI.ImageButton(path .."current-frame25x20", self:Localize("CurrentFrame"), false, self.GET_CURRENT_FRAME, false)
	layout:AddChild(self.currentFrameButton, LM.GUI.ALIGN_LEFT, 1)

    layout:AddPadding(self.shortSpace)

    self.endFrameInput = LM.GUI.TextControl(0, '000', self.END_FRAME, LM.GUI.FIELD_UINT, self:Localize('End Frame:'))
    layout:AddChild(self.endFrameInput, LM.GUI.ALIGN_LEFT, 0)
	layout:AddPadding(self.negativeSpace)
	self.endFrameButton = LM.GUI.ImageButton(path .."end-frame25x20", self:Localize("EndFrame"), false, self.GET_END_FRAME, false)
	layout:AddChild(self.endFrameButton, LM.GUI.ALIGN_LEFT, 1)

    layout:AddPadding(self.shortSpace)

    self.intervalInput = LM.GUI.TextControl(0, '00', self.INTERVAL, LM.GUI.FIELD_UINT, self:Localize('Interval:'))
    layout:AddChild(self.intervalInput, LM.GUI.ALIGN_LEFT, 0)

    layout:AddPadding(self.shortSpace)

    self.durationInput = LM.GUI.TextControl(0, '00', self.DURATION, LM.GUI.FIELD_UINT, self:Localize('Duration:'))
    layout:AddChild(self.durationInput, LM.GUI.ALIGN_LEFT, 0)

    layout:AddPadding(self.shortSpace)

    self.labelInput = LM.GUI.TextControl(0, 'WWWWWWWWWWW', self.LABEL, LM.GUI.FIELD_TEXT, self:Localize('Label:'))
    layout:AddChild(self.labelInput, LM.GUI.ALIGN_LEFT, 0)
	layout:AddPadding(self.negativeSpace)
	self.clearLabelButton = LM.GUI.ImageButton(path .."clear16x16", self:Localize("ClearLabel"), false, self.CLEAR_LABEL, false)
	layout:AddChild(self.clearLabelButton, LM.GUI.ALIGN_LEFT, 3)

    layout:AddPadding(self.mediumSpace)

    self.documentWideCheckbox = LM.GUI.CheckBox(self:Localize('Document-wide'), self.DOCUMENTWIDE)
    layout:AddChild(self.documentWideCheckbox, LM.GUI.ALIGN_LEFT, 0)

    layout:AddPadding(self.longSpace)

    self.createButton = LM.GUI.Button(self:Localize('Create'), self.CREATE)
    layout:AddChild(self.createButton, LM.GUI.ALIGN_LEFT, 0)

    layout:AddPadding(self.longSpace)
	
    self.removeButton = LM.GUI.Button(self:Localize('Remove'), self.REMOVE)
    layout:AddChild(self.removeButton, LM.GUI.ALIGN_LEFT, 0)

    layout:AddPadding(self.longSpace)

    self.clearLayerButton = LM.GUI.Button(self:Localize('Clear Layer'), self.CLEAR_LAYER)
    layout:AddChild(self.clearLayerButton, LM.GUI.ALIGN_LEFT, 0)
	
	layout:AddPadding(self.longSpace)

    self.clearDocButton = LM.GUI.Button(self:Localize('Clear Doc'), self.CLEAR_DOC)
    layout:AddChild(self.clearDocButton, LM.GUI.ALIGN_LEFT, 0)
end

function SZ_TimelineMarkersTool:UpdateWidgets(moho)
    self.markerColorMenu:UncheckAll()
    self.markerColorMenu:SetChecked(self.colorCode + MOHO.MSG_BASE + 100, true)
	self.startFrameInput:SetValue(self.startFrame)
    self.endFrameInput:SetValue(self.endFrame)
    self.intervalInput:SetValue(self.interval)
    self.durationInput:SetValue(self.duration)
    self.labelInput:SetValue(self.label)
    self.documentWideCheckbox:SetValue(self.documentWide)
end

function SZ_TimelineMarkersTool:HandleMessage(moho, view, msg)
    if msg == self.START_FRAME then
        self.startFrame = self.startFrameInput:IntValue()
    elseif msg == self.GET_CURRENT_FRAME then
		self.startFrame = moho.frame
		self:UpdateWidgets(moho)
	elseif msg == self.GET_END_FRAME then
		self.endFrame = moho.document:EndFrame()
		self:UpdateWidgets(moho)
    elseif msg == self.END_FRAME then
        self.endFrame = self.endFrameInput:IntValue()
    elseif msg == self.INTERVAL then
        self.interval = self.intervalInput:IntValue()
    elseif msg == self.DURATION then
        self.duration = self.durationInput:IntValue()
    elseif msg == self.LABEL then
		self.label = self.labelInput:Value()
	elseif msg == self.CLEAR_LABEL then
		self.label = ''
		self:UpdateWidgets(moho)
    elseif msg == self.DOCUMENTWIDE then
        self.documentWide = self.documentWideCheckbox:Value()
    elseif msg == self.CREATE then
		self.startFrame = self.startFrameInput:IntValue()
		self.endFrame = self.endFrameInput:IntValue()
		self.interval = self.intervalInput:IntValue()
		self.duration = self.durationInput:IntValue()
		self.label = self.labelInput:Value()
		self:CreateMarkers(moho)
    elseif msg == self.REMOVE then
		self:ClearMarkers(moho)
	elseif msg == self.CLEAR_LAYER then
        if LM.GUI.Alert(LM.GUI.ALERT_WARNING, self:Localize("clearLayerConfirm"), self:Localize("clearConfirm2"), nil, self:Localize("OK"), self:Localize("Cancel")) == 0 then  
			self:ClearLayer(moho)
		end
    elseif msg == self.CLEAR_DOC then
        if LM.GUI.Alert(LM.GUI.ALERT_WARNING, self:Localize("clearDocConfirm"), self:Localize("clearConfirm2"), nil, self:Localize("OK"), self:Localize("Cancel")) == 0 then  
			self:ClearDoc(moho)
		end
    elseif msg > MOHO.MSG_BASE + 99 then
        self.colorCode = msg - MOHO.MSG_BASE - 100
		self:UpdateWidgets(moho)
    end
end

-- **************************************************
-- Functions
-- **************************************************

function SZ_TimelineMarkersTool:CreateMarkers(moho)
	moho.document:PrepUndo()
	moho.document:SetDirty()
	for i = self.startFrame, self.endFrame, self.interval do
		self:CreateMarker(moho, self.documentWide, i, self.colorCode, self.duration, self.label)
	end
	moho:UpdateUI()
end

function SZ_TimelineMarkersTool:CreateMarker(moho, documentWide, frame, colorCode, duration, label)
	local interp = MOHO.InterpSetting:new_local()
	local channel = documentWide and moho.document.fTimelineMarkers or moho.layer.fTimelineMarkers
	interp.tags = colorCode
	interp.hold = duration
	channel:SetValue(frame, label)
	channel:SetKeyInterp(frame, interp)
end

function SZ_TimelineMarkersTool:ClearMarkers(moho)
	moho.document:PrepUndo()
	moho.document:SetDirty()
	local interp = MOHO.InterpSetting:new_local()
	local channel = self.documentWide and moho.document.fTimelineMarkers or moho.layer.fTimelineMarkers
	for i = 0, self.endFrame do
		if channel:HasKey(i) then
			channel:GetKeyInterp(i, interp)
			if interp.tags == self.colorCode then
				channel:DeleteKey(i)
			end
		end
	end
	moho:UpdateUI()
end

function SZ_TimelineMarkersTool:ClearLayer(moho)
	moho.document:PrepUndo()
	moho.document:SetDirty()
	local channel = moho.layer.fTimelineMarkers
	channel:ClearAfter(0)
	channel:DeleteKey(0)
	moho:UpdateUI()
end

function SZ_TimelineMarkersTool:ClearDoc(moho)
	moho.document:PrepUndo()
	moho.document:SetDirty()
	local channel = moho.document.fTimelineMarkers
	channel:ClearAfter(0)
	channel:DeleteKey(0)
	moho:UpdateUI()
end

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

function SZ_TimelineMarkersTool:Localize(text)
	local phrase = {}

	phrase['Description'] = 'Create or remove timeline markers'
	phrase['UILabel'] = 'Timeline Markers Tool'
	phrase['Marker Color'] = 'Marker Color'
	phrase['Plain'] = 'Plain'
	phrase['Red'] = 'Red'
	phrase['Orange'] = 'Orange'
	phrase['Yellow'] = 'Yellow'
	phrase['Green'] = 'Green'
	phrase['Blue'] = 'Blue'
	phrase['Purple'] = 'Purple'
	phrase['Tan'] = 'Tan'
	phrase['Pink'] = 'Pink'
	phrase['Turquoise'] = 'Turquoise'
	phrase['Cadet Blue'] = 'Cadet Blue'
	phrase['Coral'] = 'Coral'
	phrase['Start Frame:'] = 'Start Frame:'
	phrase['End Frame:'] = 'End Frame:'
	phrase['Interval:'] = 'Interval:'
	phrase['Duration:'] = 'Duration:'
	phrase['Label:'] = 'Label:'
	phrase['Document-wide'] = 'Document-wide'
	phrase['Create'] = 'Create'
	phrase['Remove'] = 'Clear Current Color'
	phrase['Clear Layer'] = 'Clear All Layer Markers'
	phrase['Clear Doc'] = 'Clear All Document-wide Markers'
	phrase['CurrentFrame'] = 'Current Frame'
	phrase['EndFrame'] = 'Last Frame'
	phrase['ClearLabel'] = 'Clear Label'
	phrase['clearLayerConfirm'] = 'This will remove ALL Layer markers.'
	phrase['clearDocConfirm'] = 'This will remove ALL Document-wide markers.'
	phrase['clearConfirm2'] = 'Are you sure?'
	phrase['OK'] = 'OK'
	phrase['Cancel'] = 'Cancel'

	local fileWord = MOHO.Localize("/Menus/File/File=File")
	if fileWord == "Файл" then
		phrase['Description'] = 'Создать или удалить маркеры в таймлайне'
		phrase['UILabel'] = 'Маркеры'
		phrase['Marker Color'] = 'Цвет маркера'
		phrase['Plain'] = 'Обычный'
		phrase['Red'] = 'Красный'
		phrase['Orange'] = 'Оранжевый'
		phrase['Yellow'] = 'Желтый'
		phrase['Green'] = 'Зеленый'
		phrase['Blue'] = 'Синий'
		phrase['Purple'] = 'Лиловый'
		phrase['Tan'] = 'Кремовый'
		phrase['Pink'] = 'Розовый'
		phrase['Turquoise'] = 'Бирюзовый'
		phrase['Cadet Blue'] = 'Серо-синий'
		phrase['Coral'] = 'Коралловый'
		phrase['Start Frame:'] = 'От кадра:'
		phrase['End Frame:'] = 'До кадра:'
		phrase['Interval:'] = 'Интервал:'
		phrase['Duration:'] = 'Длительность:'
		phrase['Label:'] = 'Метка:'
		phrase['Document-wide'] = 'На весь документ'
		phrase['Create'] = 'Создать'
		phrase['Remove'] = 'Очистить текущий цвет'
		phrase['Clear Layer'] = 'Очистить все маркеры слоя'
		phrase['Clear Doc'] = 'Очистить все маркеры документа'
		phrase['CurrentFrame'] = 'Текущий кадр'
		phrase['EndFrame'] = 'Последний кадр'
		phrase['ClearLabel'] = 'Очистить метку'
		phrase['clearLayerConfirm'] = 'Все маркеры слоя будут удалены.'
		phrase['clearDocConfirm'] = 'Все маркеры документа будут удалены.'
		phrase['clearConfirm2'] = 'Вы уверены?'
		phrase['OK'] = 'OK'
		phrase['Cancel'] = 'Отмена'
	end

	return phrase[text]
end

Icon
Timeline Markers Tool
Listed

Author: Stan View Script
Script type: Tool

Uploaded: Jan 15 2021, 22:44

Create a number of timeline markers with the interval of your choice.
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: 181