-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeycam.nut
289 lines (227 loc) · 8.26 KB
/
keycam.nut
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
IncludeScript("P2-KeyCam/PCapture-Lib") // I LOVE U!
IncludeScript("P2-KeyCam/core/keyframes.nut")
IncludeScript("P2-KeyCam/core/profile.nut")
IncludeScript("P2-KeyCam/core/editor")
IncludeScript("P2-KeyCam/core/binds")
IncludeScript("P2-KeyCam/ui/viewer")
IncludeScript("P2-KeyCam/ui/screeninfo")
if("profiles" in getroottable()) {
return dev.error("KeyCam is currently active and does'n require reinitialization!")
}
/******************************************************************************
* INIT
******************************************************************************/
// Create a camera entity
camera <- entLib.CreateByClassname("point_viewcontrol", {targetname = "camera"})
// Create a game UI entity with specific properties
gameui <- createGameUi()
// Define global variables for managing profiles and settings
::profiles <- List()
::currentProfile <- null
::globalSpeed <- 1
::isPlaying <- false
// Initialize KeyCam and setup the main player view and HUD
function Init() {
::playerEx <- GetPlayerEx()
CreateProfile() // Create an initial camera profile
SendToConsole("sv_alternateticks 0")
StartDrawFrames() // Begin the process to draw frames onto the HUD
UpdateHudInfo()
}
ScheduleEvent.Add("global", Init, 0.2)
/******************************************************************************
* KEYFRAME MANAGEMENT
******************************************************************************/
// Add a new keyframe at the player's current position
function AddFrame() {
if(isPlaying) return
local origin = playerEx.EyePosition()
local angles = playerEx.EyeAngles()
local forward = playerEx.EyeForwardVector()
local key = KeyFrame(origin, angles, forward)
currentProfile.AddFrame(key)
UpdateHudInfo()
}
// Remove a keyframe by index
function DeleteFrame(idx) {
currentProfile.RemoveFrame(idx)
UpdateHudInfo()
}
// Remove the last keyframe in the current profile
function DeleteLastFrame() {
if(isPlaying) return
currentProfile.keyframes.pop()
UpdateHudInfo()
}
// Clear all keyframes in the current profile
function ClearFrames() {
currentProfile.ClearFrames()
UpdateHudInfo()
}
/******************************************************************************
* SETTERS
******************************************************************************/
// Set the camera movement speed for the current profile
function SetSpeed(units) {
if(isPlaying) return
globalSpeed = units;
currentProfile.cameraSpeed = units;
UpdateHudInfo()
}
// Set camera speed for all profiles
function SetSpeedEx(units) {
if(isPlaying) return
globalSpeed = units;
foreach(profile in profiles.iter())
profile.cameraSpeed = units;
UpdateHudInfo()
}
// Get the current profile's speed
function GetSpeed() {
return currentProfile.getSpeed()
}
// Set the interpolation function (lerp) for smooth transitions
function SetLerp(lerpFunc) {
if(isPlaying) return
currentProfile.lerpFunc = lerpFunc
UpdateHudInfo()
}
// Get the current interpolation method as a string
function GetLerp() {
local lerpName = "linear"
if(currentProfile.lerpFunc) {
foreach(name, func in math.lerp)
if(func == currentProfile.lerpFunc) {
lerpName = name
break
}
}
return lerpName
}
/******************************************************************************
* PLAYBACK CONTROL
******************************************************************************/
// Play the current profile's keyframes in sequence
function PlayCurrentProfile() {
if(isPlaying) return
_preparePlay()
local animTime = _playProfile(currentProfile)
ScheduleEvent.Add("cam", StopPlayback, animTime)
}
// Play the special profile's keyframes in sequence
function PlayProfile(idx) {
if(isPlaying) return
_preparePlay()
local animTime = _playProfile(profiles[idx])
ScheduleEvent.Add("cam", StopPlayback, animTime)
}
// Play all profiles consecutively
function PlayAllProfiles() {
if(isPlaying) return
_preparePlay()
local delay = 0
foreach(idx, profile in profiles.iter()) {
delay = _playProfile(profile, delay)
}
ScheduleEvent.Add("cam", StopPlayback, delay)
}
// Stop playback and restore HUD elements
function StopPlayback() {
StartDrawFrames()
ScreenInfo.Enable()
isPlaying = false
SendToConsole("KeyCam_ShowHud")
EntFireByHandle(camera, "Disable")
ScheduleEvent.TryCancel("cam")
}
function _preparePlay() {
EndDrawFrames()
ScreenInfo.Disable()
beep.Enable()
isPlaying = true
SendToConsole("KeyCam_HideHud")
EntFireByHandle(camera, "Enable")
}
function _playProfile(profile, globalDelay = 0) {
local len = profile.len() - 1
for(local idx = 0; idx < len; idx++) {
local k1 = profile.GetFrame(idx)
local k2 = profile.GetFrame(idx + 1)
local settings = {globalDelay = globalDelay, eventName = "cam", lerp = profile.lerpFunc}
local animTime = animate.RT.PositionTransitionBySpeed(camera, k1.GetOrigin(), k2.GetOrigin(), profile.cameraSpeed, settings)
animate.RT.AnglesTransitionByTime(camera, k1.GetAngles(), k2.GetAngles(), animTime, settings)
globalDelay += animTime
}
return globalDelay
}
/******************************************************************************
* PROFILES CONTROL
******************************************************************************/
// Create a new camera profile and set it as active
function CreateProfile() {
if(isPlaying) return
local newProfile = Profile(globalSpeed)
currentProfile = newProfile;
profiles.append(newProfile);
UpdateHudInfo()
}
// Switch to the next profile in the list
function SwitchProfile() {
if(isPlaying) return
local idx = profiles.search(currentProfile)
if(idx >= profiles.len() - 1)
idx = -1
currentProfile = profiles[++idx]
UpdateHudInfo()
}
// Delete a specific profile by index
function DeleteProfile(idx) {
if(profiles[idx - 1] == currentProfile)
SwitchProfile()
profiles.remove(idx - 1)
UpdateHudInfo()
}
// Clear all profiles and create a new default profile
function ClearProfiles() {
if(isPlaying) return
profiles.clear()
CreateProfile()
}
/******************************************************************************
* EXPORT/IMPORT
******************************************************************************/
// Export all profiles to a file
function Export(name = "test") {
// Check if there is no data to export
if(profiles.len() == 1 && currentProfile.len() == 0)
return printl("No data for export!")
local file = File("demo_export_" + name)
file.writeRawData(macros.format("// Export from {} (time: {})", GetMapName(), Time()))
file.writeRawData("script profiles.clear()")
foreach(idx, profile in profiles.iter()) {
file.writeRawData("\\n// Profile " + idx)
file.writeRawData("script CreateProfile()")
file.writeRawData(macros.format("script SetSpeed({})", profile.GetSpeed()))
local lerp = GetLerp()
if(lerp != "linear")
file.writeRawData(macros.format("script SetLerp(math.lerp.{})", lerp))
foreach(frame in profile.keyframes.iter()) {
local origin = "Vector(" + macros.VecToStr(frame.GetOrigin(), ", ") + ")"
local angles = "Vector(" + macros.VecToStr(frame.GetAngles(), ", ") + ")"
local forward = "Vector(" + macros.VecToStr(frame.GetForwardVector(), ", ") + ")"
file.writeRawData(macros.format(
"script currentProfile.AddFrame( KeyFrame({}, {}, {}) )",
origin, angles, forward
))
}
}
file.writeRawData("\\n\\n")
SendToConsole("clear")
SendToConsole("script printl(\"The data has been successfully exported!\")")
}
// Import profiles from a file
function Import(name = "test") {
SendToConsole("exec demo_export_" + name + ".log")
SendToConsole("clear")
SendToConsole("script printl(\"The data has been successfully imported!\")")
}