summaryrefslogtreecommitdiff
path: root/.config/mpv/scripts/UndoRedo-1.5.2.lua
blob: 3916e74298ea09d3279d8ad23844e7b6c85acd75 (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
-- Copyright (c) 2020, Eisa AlAwadhi
-- License: BSD 2-Clause License

-- Creator: Eisa AlAwadhi
-- Project: UndoRedo
-- Version: 1.5.2

local utils = require 'mp.utils'
local seconds = 0
local countTimer = 0
local seekTime = 0

local seekNumber = 0
local currentIndex = 0
local seekTable = {}
local seeking = 0

local undoRedo = 0

local pause = false

seekTable[0] = 0

mp.register_event('file-loaded', function()
	filePath = mp.get_property('path')
	
	timer = mp.add_periodic_timer(0.1, function()
		seconds = seconds + 0.1
	end)
	
	if (pause == true) then
		timer:stop()
	else
		timer:resume()
	end
	
	timer2 = mp.add_periodic_timer(0.1, function()
		countTimer = countTimer + 0.1
		
		if (countTimer == 0.6) then
		
			if (seeking == 0) then
				
				if (pause == true) then
					seconds = seconds
				else
					seconds = seconds - 0.7
				end
				
					seekTable[currentIndex] = seekTable[currentIndex] + seconds 	
					seconds = 0	
					
					seekNumber = currentIndex + 1
					currentIndex = seekNumber
					seekTime = math.floor(mp.get_property_number('time-pos'))
					table.insert(seekTable, seekNumber, seekTime)
					
					undoRedo = 0
			
			elseif (seeking == 1) then
				seeking = 0
			end
			
		end

	end)
	
	timer2:stop()
end)


mp.register_event('seek', function()
	timer2:resume()
	countTimer = 0
end)

mp.register_event('pause', function() 
	timer:stop()
	pause = true
end)

mp.register_event('unpause', function()
	timer:resume()
	pause = false
end)

mp.register_event('end-file', function()
	if timer ~= nil then
		timer:kill()
	end
	if timer2 ~= nil then
		timer2:kill()
	end
	seekNumber = 0
	currentIndex = 0
	undoRedo = 0
	seconds = 0
	countTimer = 0
	seekTable[0] = 0
end)

local function undo()
	if (filePath ~= nil) and (currentIndex > 0) and (seeking == 0) then
		
		if (pause == true) then
			seconds = seconds
		else
			seconds = seconds - 0.7
		end
		
		seekTable[currentIndex] = seekTable[currentIndex] + seconds
		seconds=0
		
		currentIndex = currentIndex - 1
		
		if (seekTable[currentIndex] < 0) then
			seekTable[currentIndex] = 0
		end
		
		mp.commandv('seek', seekTable[currentIndex], 'absolute', 'exact')
		
		seeking = 1
		undoRedo = 1
		
		mp.osd_message('Undo')
	elseif (filePath ~= nil) and (countTimer > 0) and (countTimer < 0.6) then 
		mp.osd_message('Seeking Still Running')
	elseif (filePath ~= nil) and (currentIndex == 0) then
		mp.osd_message('No Undo Found')
	end
end

local function redo()
	if (filePath ~= nil) and (currentIndex < seekNumber) and (seeking == 0) then
	
		if (pause == true) then
			seconds = seconds
		else
			seconds = seconds - 0.7
		end
		
		seekTable[currentIndex] = seekTable[currentIndex] + seconds
		seconds = 0
		
		currentIndex = currentIndex + 1
		
		if (seekTable[currentIndex] < 0) then
			seekTable[currentIndex] = 0
		end
		
		mp.commandv('seek', seekTable[currentIndex], 'absolute', 'exact')

		seeking = 1
		undoRedo = 0
		
		mp.osd_message('Redo')
	elseif (filePath ~= nil) and (countTimer > 0) and (countTimer < 0.6) then
		mp.osd_message('Seeking Still Running')
	elseif (filePath ~= nil) and (currentIndex == seekNumber) then
		mp.osd_message('No Redo Found')
	end
end

local function undoRedo()
	if (filePath ~= nil) and (countTimer > 0.5) and (undoRedo == 0) then
		
		if (pause == true) then
			seconds = seconds
		else
			seconds = seconds - 0.7
		end
		
		seekTable[currentIndex] = seekTable[currentIndex] + seconds
		seconds = 0
		
		currentIndex = currentIndex - 1
		
		if (seekTable[currentIndex] < 0) then
			seekTable[currentIndex] = 0
		end
		
		mp.commandv('seek', seekTable[currentIndex], 'absolute', 'exact')
		mp.osd_message('Undo')
		seeking = 1
		undoRedo = 1
	elseif (filePath ~= nil) and (countTimer > 0.5) and (undoRedo == 1) then
		
		if (pause == true) then
			seconds = seconds
		else
			seconds = seconds - 0.7
		end
		
		seekTable[currentIndex] = seekTable[currentIndex] + seconds
		seconds = 0
		
		currentIndex = currentIndex + 1
		
		if (seekTable[currentIndex] < 0) then
			seekTable[currentIndex] = 0
		end
		
		mp.commandv('seek', seekTable[currentIndex], 'absolute', 'exact')
		mp.osd_message('Redo')
		seeking = 1
		undoRedo = 0
	elseif (filePath ~= nil) and (countTimer > 0) and (countTimer < 0.6) then
		mp.osd_message('Seeking Still Running')
	elseif (filePath ~= nil) and (countTimer == 0) then
		mp.osd_message('No Undo Found')
	end
end


mp.add_key_binding("ctrl+z", "undo", undo)
mp.add_key_binding("ctrl+Z", "undoCaps", undo)

mp.add_key_binding("ctrl+y", "redo", redo)
mp.add_key_binding("ctrl+Y", "redoCaps", redo)

mp.add_key_binding("ctrl+alt+z", "undoRedo", undoRedo)
mp.add_key_binding("ctrl+alt+Z", "undoRedoCaps", undoRedo)