1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
-- Frame Duration Helper v0.1
--
-- Used to check and update frame duration
-- This script should support v1.2.10+ (API version=3+)
-- https://github.com/linecross/

local isSelectedFrame = app.range.type == RangeType.FRAMES
local selectKeyword = isSelectedFrame and "Selected" or "Total"
local frames = isSelectedFrame and app.range.frames or app.sprites[1].frames
local tags = app.sprites[1].tags
local frameCount = 0

local util = {
	isShowInfo = false,
	isDebugMode = false
}

function util.debug(s)
	if util.isDebugMode then
		print(s)
	end
end

function util.print(s)
	if util.isShowInfo then
		print(s)
	end
end

function toggleResultConsole()
	util.isShowInfo = not util.isShowInfo
end

function refreshData()
	isSelectedFrame = app.range.type == RangeType.FRAMES
	selectKeyword = isSelectedFrame and "Selected" or "Total"
	frames = isSelectedFrame and app.range.frames or app.sprites[1].frames
	frameCount = 0
	for i=1, #frames do
		local frame = frames[i]
		frameCount = frameCount + 1
	end
	tags = app.sprites[1].tags
end

function isFrameinTag(frame, tag)
	local frameNo = frame.frameNumber
	local startFrame = tag.fromFrame.frameNumber
	local endFrame = tag.toFrame.frameNumber
	return frameNo >= startFrame and frameNo <= endFrame
end

function frameInfoDlg()
	refreshData()
	local timeCount = 0
	local summaryMsg = ""
	local detailMsg = ""
	for i=1, #frames do
		local frame = frames[i]
		timeCount = timeCount + frame.duration
		local tagMsg = ""
		for j=1, #tags do
			if (isFrameinTag(frame, tags[j])) then
				tagMsg = tagMsg .. tags[j].name .. ' , '
			end
		end
		if (string.len(tagMsg) > 0) then
			tagMsg = string.sub(tagMsg, 1, string.len(tagMsg)-2)
			tagMsg = "        ( " .. tagMsg .. " )"
		end
		detailMsg = detailMsg .. "Frame " .. frame.frameNumber .. " : " .. (frame.duration * 1000) .. tagMsg .. "\n"
	end
	summaryMsg = selectKeyword .. " Frames = " .. frameCount .. "\n"
	summaryMsg = summaryMsg .. selectKeyword .. " Duration = " .. timeCount .. " (s)\n\n"
	print("Frame Summary")
	print("============================\n")
	print(summaryMsg .. detailMsg)
end

function constantRateDlg()
	refreshData()
	local dlg = Dialog('Frame Duration: Constant Rate')
	dlg:label{ label=selectKeyword .. " Frames = ", text= tostring(frameCount)}
	
	dlg:number{ id="duration", label="Duration (ms) : " , text="100" }
	dlg:button{ id="ok", text="OK" }
	dlg:button{ id="cancel", text="Cancel" }
	dlg:show()
	
	local data = dlg.data
	if data.ok then
		if (data.duration <= 0) then
			app.alert({title="Invalid Input", text="Duration must be >= 0"})
		else
			util.print("Frames duration changed to " .. data.duration .. " ms.\n")
			for i=1, #frames do
				local frame = frames[i]
				util.print("Frame " .. frame.frameNumber .. " : " .. (frame.duration * 1000) .. " --> " .. data.duration)
				frame.duration = data.duration / 1000
			end
		end
	end
end

function scaleRateDlg()
	refreshData()
	local dlg = Dialog('Frame Duration: Scale')
	dlg:label{ label=selectKeyword .. " Frames = ", text= tostring(frameCount)}
	
	dlg:number{ id="scale", label="Scale (%) : " , text="100" }
	dlg:button{ id="ok", text="OK" }
	dlg:button{ id="cancel", text="Cancel" }
	dlg:show()
	
	local data = dlg.data
	if data.ok then
		if (data.scale <= 0) then
			app.alert({title="Invalid Input", text="Scale must be >= 0"})
		else
			util.print("Frames scale to " .. data.scale .. " %.\n")
			for i=1, #frames do
				local frame = frames[i]
				util.print("Frame " .. frame.frameNumber .. " : " .. (frame.duration * 1000) .. " --> " .. (frame.duration * 1000) * (data.scale / 100))
				frame.duration = frame.duration * (data.scale / 100)
			end
		end
	end
end

function addDurationDlg()
	refreshData()
	local dlg = Dialog('Frame Duration: Add Duration')
	dlg:label{ label=selectKeyword .. " Frames = ", text= tostring(frameCount)}
	
	dlg:number{ id="duration", label="Duration (ms) : " , text="0" }
	dlg:button{ id="ok", text="OK" }
	dlg:button{ id="cancel", text="Cancel" }
	dlg:show()
	
	local data = dlg.data
	if data.ok then
		util.print("Frames added duration " .. data.duration .. " ms.\n")
		for i=1, #frames do
			local frame = frames[i]
			util.print("Frame " .. frame.frameNumber .. " : " .. (frame.duration * 1000) .. " --> " .. ((frame.duration * 1000) + data.duration))
			frame.duration = frame.duration + (data.duration / 1000)
		end
	end
end

function manualEditDlg()
	refreshData()
	local dlg = Dialog('Frame Duration: Manual Edit')
	dlg:label{ label=selectKeyword .. " Frames = ", text= tostring(frameCount)}
	
	for i=1, #frames do
		local frame = frames[i]
		dlg:entry{ id="frame_" .. frame.frameNumber, label="Frame " .. frame.frameNumber .. " : ", text="" .. frame.duration * 1000 }
	end
	dlg:button{ id="ok", text="OK" }
	dlg:button{ id="cancel", text="Cancel" }
	dlg:show()
	
	local data = dlg.data
	if data.ok then
		local isValidInput = true
		local errMsg = {}
		for i=1, #frames do
			local frame = frames[i]
			if tonumber(data["frame_" .. frame.frameNumber]) == nil then
				table.insert(errMsg, "Invalid Input for Frame " .. frame.frameNumber .. " : " .. data["frame_" .. frame.frameNumber])
				isValidInput = false
			end
		end
		if (isValidInput) then
			util.print("Frames updated duration as below.\n")
			for i=1, #frames do
				local frame = frames[i]
				util.print("Frame " .. frame.frameNumber .. " : " .. frame.duration * 1000 .. " --> " .. data["frame_" .. frame.frameNumber])
				frame.duration = data["frame_" .. frame.frameNumber] / 1000
			end
		else
			app.alert({title="Invalid Input", text=errMsg})
		end
	end
end

function mainDlg()
	local dlg = Dialog("Frame Duration Helper")
	dlg
	  :button{text="Frames Summary",onclick=function() frameInfoDlg() end}
	  :newrow()
	  :button{text="Scale Rate",onclick=function() scaleRateDlg() end}
	  :button{text="Add Duration",onclick=function() addDurationDlg() end}
	  :newrow()
	  :button{text="Constant Rate",onclick=function() constantRateDlg() end}
	  :button{text="Manual Edit",onclick=function() manualEditDlg() end}
	  :check{text="Display Result Console", selected=util.isShowInfo, onclick=toggleResultConsole}
	  :show{wait=false}
end

mainDlg()