summaryrefslogtreecommitdiff
path: root/mac/.config/mpv/scripts/UndoRedo.lua
blob: 67d3f2b75974f050e8f3303a3cc955c4ed738439 (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
-- Copyright (c) 2021, Eisa AlAwadhi
-- License: BSD 2-Clause License

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

local utils = require("mp.utils")
local msg = require("mp.msg")
local seconds = 0
local countTimer = -1
local seekTime = 0
local seekNumber = 0
local currentIndex = 0
local seekTable = {}
local seeking = 0
local undoRedo = 0
local pause = false
seekTable[0] = 0

----------------------------USER CUSTOMIZATION SETTINGS-----------------------------------
--These settings are for users to manually change some options in the script.
--Keybinds can be defined in the bottom of the script.

local osd_messages = true --true is for displaying osd messages when actions occur, Change to false will disable all osd messages generated from this script

---------------------------END OF USER CUSTOMIZATION SETTINGS---------------------

local function prepareUndoRedo()
	if pause == true then
		seconds = seconds
	else
		seconds = seconds - 0.5
	end
	seekTable[currentIndex] = seekTable[currentIndex] + seconds
	seconds = 0
end

local function getUndoRedo()
	if seeking == 0 then
		prepareUndoRedo()

		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

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
			timer:resume()
			getUndoRedo()
		end
	end)

	timer2:stop()
end)

mp.register_event("seek", function()
	countTimer = 0
	timer2:resume()
	timer:stop()
end)

mp.observe_property("pause", "bool", function(name, value)
	if value then
		if timer ~= nil then
			timer:stop()
		end
		pause = true
	else
		if timer ~= nil then
			timer:resume()
		end
		pause = false
	end
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 = -1
	seekTable[0] = 0
end)

local function undo()
	if (filePath ~= nil) and (countTimer >= 0) and (countTimer < 0.6) and (seeking == 0) then
		timer2:stop()

		getUndoRedo()

		currentIndex = currentIndex - 1
		if currentIndex < 0 then
			if osd_messages == true then
				mp.osd_message("No Undo Found")
			end
			currentIndex = 0
			msg.info("No undo found")
		else
			if seekTable[currentIndex] < 0 then
				seekTable[currentIndex] = 0
			end

			seeking = 1

			mp.commandv("seek", seekTable[currentIndex], "absolute", "exact")

			undoRedo = 1
			if osd_messages == true then
				mp.osd_message("Undo")
			end
			msg.info("Seeked using undo")
		end
	elseif (filePath ~= nil) and (currentIndex > 0) then
		prepareUndoRedo()
		currentIndex = currentIndex - 1

		if seekTable[currentIndex] < 0 then
			seekTable[currentIndex] = 0
		end

		seeking = 1
		mp.commandv("seek", seekTable[currentIndex], "absolute", "exact")

		undoRedo = 1
		if osd_messages == true then
			mp.osd_message("Undo")
		end
		msg.info("Seeked using undo")
	elseif (filePath ~= nil) and (currentIndex == 0) then
		if osd_messages == true then
			mp.osd_message("No Undo Found")
		end
		msg.info("No undo found")
	end
end

local function redo()
	if (filePath ~= nil) and (currentIndex < seekNumber) then
		prepareUndoRedo()
		currentIndex = currentIndex + 1

		if seekTable[currentIndex] < 0 then
			seekTable[currentIndex] = 0
		end

		seeking = 1
		mp.commandv("seek", seekTable[currentIndex], "absolute", "exact")

		undoRedo = 0

		if osd_messages == true then
			mp.osd_message("Redo")
		end
		msg.info("Seeked using redo")
	elseif (filePath ~= nil) and (currentIndex == seekNumber) then
		if osd_messages == true then
			mp.osd_message("No Redo Found")
		end
		msg.info("No redo found")
	end
end

local function undoLoop()
	if (filePath ~= nil) and (undoRedo == 0) then
		undo()
	elseif (filePath ~= nil) and (undoRedo == 1) then
		redo()
	elseif (filePath ~= nil) and (countTimer == -1) then
		if osd_messages == true then
			mp.osd_message("No Undo Found")
		end
		msg.info("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", "undoLoop", undoLoop)
mp.add_key_binding("ctrl+alt+Z", "undoLoopCaps", undoLoop)