-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcl_animeditor.lua
More file actions
1499 lines (1138 loc) · 41.3 KB
/
cl_animeditor.lua
File metadata and controls
1499 lines (1138 loc) · 41.3 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
if _INCLUDEDANIMEDITOR then return end
_INCLUDEDANIMEDITOR = true
surface.CreateFont("DefaultFontVerySmall", {font = "tahoma", size = 10, weight = 0, antialias = false})
surface.CreateFont("DefaultFontSmall", {font = "tahoma", size = 11, weight = 0, antialias = false})
surface.CreateFont("DefaultFontSmallDropShadow", {font = "tahoma", size = 11, weight = 0, shadow = true, antialias = false})
surface.CreateFont("DefaultFont", {font = "tahoma", size = 13, weight = 500, antialias = false})
surface.CreateFont("DefaultFontBold", {font = "tahoma", size = 13, weight = 1000, antialias = false})
surface.CreateFont("DefaultFontLarge", {font = "tahoma", size = 16, weight = 0, antialias = false})
local boneList = {}
boneList["ValveBiped.Bip01"] = {
"ValveBiped.Bip01_Pelvis",
"ValveBiped.Bip01_Spine",
"ValveBiped.Bip01_Spine1",
"ValveBiped.Bip01_Spine2",
"ValveBiped.Bip01_Spine4",
"ValveBiped.Bip01_Neck1",
"ValveBiped.Bip01_Head1",
"ValveBiped.Bip01_R_Clavicle",
"ValveBiped.Bip01_R_UpperArm",
"ValveBiped.Bip01_R_Forearm",
"ValveBiped.Bip01_R_Hand",
"ValveBiped.Bip01_L_Clavicle",
"ValveBiped.Bip01_L_UpperArm",
"ValveBiped.Bip01_L_Forearm",
"ValveBiped.Bip01_L_Hand",
"ValveBiped.Bip01_R_Thigh",
"ValveBiped.Bip01_R_Calf",
"ValveBiped.Bip01_R_Foot",
"ValveBiped.Bip01_R_Toe0",
"ValveBiped.Bip01_L_Thigh",
"ValveBiped.Bip01_L_Calf",
"ValveBiped.Bip01_L_Foot",
"ValveBiped.Bip01_L_Toe0"
}
boneList["ValveBiped"] = {
"ValveBiped.hips",
"ValveBiped.leg_bone1_L",
"ValveBiped.leg_bone2_L",
"ValveBiped.leg_bone3_L",
"ValveBiped.Bip01_L_Foot",
"ValveBiped.Bip01_L_Toe0",
"ValveBiped.leg_bone1_R",
"ValveBiped.leg_bone2_R",
"ValveBiped.leg_bone3_R",
"ValveBiped.Bip01_R_Foot",
"ValveBiped.Bip01_R_Toe0",
"ValveBiped.spine1",
"ValveBiped.spine2",
"ValveBiped.spine3",
"ValveBiped.spine4",
"ValveBiped.neck1",
"ValveBiped.neck2",
"ValveBiped.head",
"ValveBiped.clavical_L",
"ValveBiped.arm1_L",
"ValveBiped.arm2_L",
"ValveBiped.hand1_L",
"ValveBiped.clavical_R",
"ValveBiped.arm1_R",
"ValveBiped.arm2_R",
"ValveBiped.hand1_R",
"ValveBiped.bone",
"ValveBiped.bone1",
"ValveBiped.bone2"
}
local animationData = {}
local function animprint()
PrintTable(animationData)
end
concommand.Add("animprint",animprint)
local animName
local animType
local subAnimationsLoaded = {}
local playBarOffset = 0
local playingAnimation = false
local camDist = 200
local camHeight = 60 --offset from GetPos
local pressedPos = {}
local angToPlayer = Angle(0,0,0)
local selectedBoneSet = "ValveBiped.Bip01"
local selectedFrame
local draggingDir
local tblLineEndPoints = {}
local timeLine
local mainSettings
local sliders
local subAnims
local animating = false
local editingAnimation = false
local rightDown = false
local leftDown = false
local mwheelDown = false
local TypeTable = {}
TypeTable[0] = "TYPE_GESTURE"
TypeTable[1] = "TYPE_POSTURE"
TypeTable[2] = "TYPE_STANCE"
TypeTable[3] = "TYPE_SEQUENCE"
local function DistFromPointToLine(x,y,x1,y1,x2,y2)
local A = x - x1;
local B = y - y1;
local C = x2 - x1;
local D = y2 - y1;
local dot = A * C + B * D;
local len_sq = C * C + D * D;
local param = dot / len_sq;
local xx,yy;
if(param < 0) then
xx = x1;
yy = y1;
elseif(param > 1) then
xx = x2;
yy = y2;
else
xx = x1 + param * C;
yy = y1 + param * D;
end
return math.Dist(x,y,xx,yy)
end
local function PaintTopBar()
local wide = ScrW()
draw.RoundedBox(0,0,0,wide,26,Color(0,0,0,255))
draw.RoundedBox(0,1,1,wide-2,24,Color(50,50,50,255))
draw.SimpleText("Lua Animation Editor (API by JetBoom)","DefaultFont",25,13,Color(255,255,255,255),0,1)
if type(animName ) == "string" then
draw.SimpleText("Working On: ("..animName..")","DefaultFont",wide*0.5,13,Color(255,255,255,255),1,1)
else
draw.SimpleText("No Animation Loaded!","DefaultFont",wide*0.5,13,Color(255,255,255,255),1,1)
end
if selectedBone && selectedBone != "" then
local boneID = LocalPlayer():LookupBone(selectedBone)
if boneID then
local matrix = LocalPlayer():GetBoneMatrix(boneID)
local vec = matrix:GetTranslation()
local ang = matrix:GetAngles()
local upDir = ang:Up()*20
local rightDir = ang:Right()*20
local forwardDir = ang:Forward()*20
local origin = vec:ToScreen()
if !leftDown && draggingDir then
draggingDir = nil
end
surface.SetDrawColor(255,0,0,255)
local p1 = (vec+rightDir):ToScreen()
local dist = DistFromPointToLine(gui.MouseX(),gui.MouseY(),origin.x,origin.y,p1.x,p1.y)
if (dist < 10 && !draggingDir) || draggingDir == "RR" then
surface.SetDrawColor(255,255,255,255)
draggingDir = "RR"
tblLineEndPoints[1] = {x=origin.x,origin.y}
tblLineEndPoints[1] = {p1.x,p1.y}
end
surface.DrawLine(origin.x,origin.y,p1.x,p1.y)
surface.SetDrawColor(0,255,0,255)
local p2 = (vec+upDir):ToScreen()
local dist = DistFromPointToLine(gui.MouseX(),gui.MouseY(),origin.x,origin.y,p2.x,p2.y)
if (dist < 10 && !draggingDir) || draggingDir == "RU" then
surface.SetDrawColor(255,255,255,255)
draggingDir = "RU"
tblLineEndPoints[1] = {x=origin.x,origin.y}
tblLineEndPoints[1] = {p1.x,p1.y}
end
surface.DrawLine(origin.x,origin.y,p2.x,p2.y)
surface.SetDrawColor(0,0,255,255)
local p3 = (vec+forwardDir):ToScreen()
local dist = DistFromPointToLine(gui.MouseX(),gui.MouseY(),origin.x,origin.y,p3.x,p3.y)
if (dist < 10 && !draggingDir) || draggingDir == "RF" then
surface.SetDrawColor(255,255,255,255)
draggingDir = "RF"
tblLineEndPoints[1] = {x=origin.x,origin.y}
tblLineEndPoints[1] = {p1.x,p1.y}
end
surface.DrawLine(origin.x,origin.y,p3.x,p3.y)
end
end
end
local animEditorPanels = {}
local function AnimationStarted(bLoaded)
subAnims:Refresh()
if !bLoaded then
animationData = {}
animationData.FrameData = {}
animationData.Type = animType
for i,v in pairs(animEditorPanels) do
if v.OnNewAnimation then
v:OnNewAnimation()
end
end
else
for i,v in pairs(animEditorPanels) do
if v.OnLoadAnimation then
v:OnLoadAnimation()
end
end
end
editingAnimation = true
timeLine.subAnims:Clear()
table.Empty(subAnimationsLoaded)
end
local function NewAnimation()
local frame = vgui.Create("DFrame")
form = vgui.Create("DForm",frame)
form:SetPos(5,25)
form:SetWide(300)
form:SetTall(300)
form:SetName("Animation Properties")
local entry = form:TextEntry("Animation Name")
local info = form:Help([[Gestures are keyframed animations that use the current position and angles of the bones. They play once and then stop automatically.
Postures are static animations that use the current position and angles of the bones. They stay that way until manually stopped. Use TimeToArrive if you want to have a posture lerp.
Stances are keyframed animations that use the current position and angles of the bones. They play forever until manually stopped. Use RestartFrame to specify a frame to go to if the animation ends (instead of frame 1).
Sequences are keyframed animations that use the origin and angles of the entity. They play forever until manually stopped. Use RestartFrame to specify a frame to go to if the animation ends (instead of frame 1).
You can also use StartFrame to specify a starting frame for the first loop.]])
local type = form:ComboBox("Animation Type")
type:SetTall(100)
type:AddChoice("TYPE_GESTURE", TYPE_GESTURE)
type:AddChoice("TYPE_POSTURE", TYPE_POSTURE)
type:AddChoice("TYPE_STANCE", TYPE_STANCE)
type:AddChoice("TYPE_SEQUENCE", TYPE_SEQUENCE, true)
local help = form:Help("Select your options")
local begin = form:Button("Begin")
begin.DoClick = function()
animName = entry:GetValue()
animType = _G[type:GetText()]
if animName == "" then help:SetText("Write a name for this animation") return end
if !animType then help:SetText("Select a valid animation type!") return end
frame:Remove()
AnimationStarted()
end
frame:MakePopup()
timer.Simple(0.01,function()frame:SetSize(form:GetWide()+10,450) frame:Center() end)
end
local function LoadAnimation()
local frame = vgui.Create("DFrame")
frame:SetSize(300,300)
frame:SetTitle("Load Animation")
local box = vgui.Create("DComboBox",frame)
--box:SetMultiple(false) adurp
box:StretchToParent(5,25,5,35)
for i,v in pairs(GetLuaAnimations()) do
if i != "editortest" && i != "editingAnim" && !string.find(i,"subPosture_") then --anim editor uses this internally
box:AddChoice(i)
end
end
local button = vgui.Create("DButton",frame)
button:SetWide(frame:GetWide()-10)
button:SetPos(5,frame:GetTall()-25)
button:SetText("Load Animation")
button.DoClick = function()
animName = box:GetText()
animationData = GetLuaAnimations()[animName]
animType = animationData.Type
frame:Remove()
AnimationStarted(true)
LocalPlayer():StopAllLuaAnimations()
end
frame:Center()
end
--return lua code for the loaded animation
local function OutputCode()
if !animName then surface.PlaySound("buttons/button10.wav") return end
local animData = table.Copy(animationData)
--clean out unneeded entries
for i,v in pairs(animData.FrameData) do
for BoneName,BoneData in pairs(v.BoneInfo) do
for MoveRot,Val in pairs(BoneData) do
if Val == 0 then
animData.FrameData[i].BoneInfo[BoneName][MoveRot] = nil
end
end
end
end
local str = "RegisterLuaAnimation('"..animName.."', {\r\n"
str = str .. "\tFrameData = {\r\n"
local numFrames = table.Count(animData.FrameData)
local numFrame = 1
for frameIndex,frameData in pairs(animData.FrameData) do
local commaFrame = ","
if numFrame == numFrames then commaFrame = "" end
str = str .. "\t\t{\r\n"
str = str .. "\t\t\tBoneInfo = {\r\n"
local numBones = table.Count(frameData.BoneInfo)
local numBone = 1
for boneName,boneData in pairs(frameData.BoneInfo) do
local commaBone = ","
if numBones == numBone then
commaBone = ""
end
str = str .. "\t\t\t\t['"..boneName.."'] = {\r\n"
local numChanges = table.Count(boneData)
local numInner = 1
for MoveRot,Value in pairs(boneData) do
local commaInner = ","
if numChanges == numInner then commaInner = "" end
local innerStr = "\t\t\t\t\t"..MoveRot.." = "..Value..commaInner.."\r\n"
str = str..innerStr
numInner = numInner + 1
end
str = str .. "\t\t\t\t}"..commaBone.."\r\n"
numBone = numBone + 1
end
numFrame = numFrame + 1
str = str .. "\t\t\t},\r\n"
if frameData.FrameRate then
str = str .. "\t\t\tFrameRate = "..frameData.FrameRate.."\r\n"
end
str = str .. "\t\t}"..commaFrame.."\r\n"
end
str = str .. "\t},\r\n"
if animData.RestartFrame then
str = str .. "\tRestartFrame = "..animData.RestartFrame..",\r\n"
end
if animData.StartFrame then
str = str .. "\tStartFrame = "..animData.StartFrame..",\r\n"
end
str = str .. "\tType = "..TypeTable[animData.Type].."\r\n})"
return str
end
--calculates all the bone movements up to the current frame for DISPLAY PURPOSES.
local function ApplyEndResults()
local currentFrame = selectedFrame:GetAnimationIndex()
local postureAnim = {Type = TYPE_POSTURE,FrameData = {{BoneInfo = {}}}}
--[[local timeInSeconds = 0
for frameIndex,frameData in pairs(animationData.FrameData) do
timeInSeconds = timeInSeconds + 1/(frameData.FrameRate or 1)
for boneName, boneData in pairs(frameData.BoneInfo) do
postureAnim.FrameData[1].BoneInfo[boneName] = postureAnim.FrameData[1].BoneInfo[boneName] or {}
for moveType,moveVal in pairs(boneData) do
postureAnim.FrameData[1].BoneInfo[boneName][moveType] = (postureAnim.FrameData[1].BoneInfo[boneName][moveType] or 0) + moveVal
end
end
if frameIndex == currentFrame then break end
end]]
postureAnim.FrameData[1] = table.Copy(animationData.FrameData[currentFrame])
--[[local subPostures = {}
--load all the sub animations up to the exact point where the keyframe in the main animation ends...
for i,v in pairs(subAnimationsLoaded) do
local subPostureAnim = {Type = TYPE_POSTURE,FrameData = {{BoneInfo = {}}}}
local anim = GetLuaAnimations()[i]
local totalAnimTimeInSeconds = 0
local timeToStart = 0
local timeSoFar = 0
--get the time from the actual start(0) to the animation start (StartFrame)
if anim.StartFrame && anim.StartFrame > 1 then
for i=1,anim.StartFrame-1 do
timeToStart = timeToStart + 1/(anim.FrameData[i].FrameRate or 1)
end
end
--pregather animation time
for i=anim.StartFrame or 1,table.getn(anim.FrameData) do
totalAnimTimeInSeconds = totalAnimTimeInSeconds + 1/(anim.FrameData[i].FrameRate or 1)
end
for frameIndex=1,table.getn(anim.FrameData) do
local frameData = anim.FrameData[frameIndex]
--this frame starts before the selected main keyframe ends
if timeSoFar < timeInSeconds then
local prevTime = timeSoFar
timeSoFar = timeSoFar + 1/(frameData.FrameRate or 1)
--we've reached a keyframe that extends beyond our main animation's current keyframe endpos
local delta = 1
if timeSoFar > timeInSeconds && (anim.StartFrame or 1) <= frameIndex then
--thanks sassafrass
delta = (timeInSeconds-prevTime)/(timeSoFar-prevTime)
end
for boneName,boneData in pairs(frameData.BoneInfo) do
subPostureAnim.FrameData[1].BoneInfo[boneName] = subPostureAnim.FrameData[1].BoneInfo[boneName] or {}
for moveType,moveVal in pairs(boneData) do
if !subPostureAnim.FrameData[1].BoneInfo[boneName][moveType] then
subPostureAnim.FrameData[1].BoneInfo[boneName][moveType] = moveVal*delta
else
subPostureAnim.FrameData[1].BoneInfo[boneName][moveType] = subPostureAnim.FrameData[1].BoneInfo[boneName][moveType] + moveVal*delta
end
end
end
end
end
RegisterLuaAnimation("subPosture_"..i,subPostureAnim)
table.insert(subPostures,"subPosture_"..i)
end]]
RegisterLuaAnimation("editingAnim",postureAnim)
LocalPlayer():StopAllLuaAnimations()
LocalPlayer():SetLuaAnimation("editingAnim")
--[[for i,v in pairs(subPostures) do
LocalPlayer():SetLuaAnimation(v)
end]]
end
local function LoadAnimationFromFile()
local frame = vgui.Create("DFrame")
frame:SetSize(300,300)
frame:SetTitle("Load Animation From File")
local box = vgui.Create("DComboBox",frame)
--box:SetMultiple(false)
box:StretchToParent(5,25,5,35)
for i,v in pairs(file.Find("animations/*.txt", "DATA")) do
box:AddChoice(string.sub(v,1,-5))
end
local button = vgui.Create("DButton",frame)
button:SetWide(frame:GetWide()-10)
button:SetPos(5,frame:GetTall()-25)
button:SetText("Load Animation")
button.DoClick = function()
local name = box:GetText()
local str = file.Read("animations/"..name..".txt", "DATA")
if !str then return end
local success, t = pcall(util.JSONToTable, str)
if !success then
ErrorNoHalt("WARNING: Animation '"..name.."' failed to load\n")
else
RegisterLuaAnimation(name,t)
animName = name
animationData = GetLuaAnimations()[animName]
animType = animationData.Type
frame:Remove()
AnimationStarted(true)
LocalPlayer():StopAllLuaAnimations()
end
end
frame:Center()
end
local function RegisterAll()
for i,v in pairs(file.Find("animations/*.txt", "DATA")) do
local str = file.Read("animations/"..string.sub(v,1,-5)..".txt", "DATA")
if !str then return end
local success,t = pcall(Deserialize, str)
if !success then
ErrorNoHalt("WARNING: Animation '"..string.sub(v,1,-5).."' failed to load\n")
else
RegisterLuaAnimation(string.sub(v,1,-5),t)
end
end
subAnims:Refresh()
end
local function SaveAnimation()
if(!file.Exists("animations","DATA")) then file.CreateDir"animations" end
Derma_StringRequest( "Question",
"Save as...",
animName or "",
function( strTextOut ) RegisterLuaAnimation(strTextOut,animationData) file.Write("animations/"..strTextOut..".txt", util.TableToJSON(animationData)) end,
function( strTextOut ) end,
"Save",
"Cancel" )
end
local topLevelPanels = {}
local function IsMouseOverPanel()
local mouseX = gui.MouseX()
local mouseY = gui.MouseY()
for i,v in pairs(topLevelPanels) do
if ValidPanel(v) && v:IsVisible() then
local bChild = IsChildOfHiddenParent(v)
if !bChild then
local x,y = v:GetPos()
local w = v:GetWide()
local h = v:GetTall()
local overX = mouseX > x && mouseX < x+w
local overY = mouseY > y && mouseY < y+h
if overX && overY then return true end
end
else
table.remove(topLevelPanels,i)
end
end
return false
end
local function FixMouse()
if !animating then return end
local notOverPanel = !IsMouseOverPanel()
if !input.IsMouseDown(MOUSE_RIGHT) && rightDown then
rightDown = false
elseif input.IsMouseDown(MOUSE_RIGHT) && !rightDown && notOverPanel then
rightDown = true
pressedPos[1] = gui.MouseX()
pressedPos[2] = gui.MouseY()
elseif input.IsMouseDown(MOUSE_RIGHT) && rightDown then
local mvmtX = gui.MouseX()-pressedPos[1]
angToPlayer.yaw = angToPlayer.yaw + mvmtX
local mvmtY = (gui.MouseY()-pressedPos[2])*0.1
camHeight = math.max(10,camHeight - mvmtY)
gui.SetMousePos(pressedPos[1],pressedPos[2])
end
if input.IsMouseDown(MOUSE_LEFT) && !leftDown && notOverPanel then
if draggingDir then
pressedPos[1] = gui.MouseX()
pressedPos[2] = gui.MouseY()
leftDown = true
end
elseif !input.IsMouseDown(MOUSE_LEFT) && leftDown then
leftDown = false
elseif input.IsMouseDown(MOUSE_LEFT) && leftDown then
local mvmtX = gui.MouseX()-pressedPos[1]
local mvmtY = gui.MouseY()-pressedPos[2]
local dist = math.Distance(gui.MouseX(),gui.MouseY(),pressedPos[1],pressedPos[2])
if mvmtX < 0 then
dist = dist * -1
end
sliders:Dragged3D(dist,draggingDir)
gui.SetMousePos(pressedPos[1],pressedPos[2])
end
if(input.IsMouseDown(MOUSE_WHEEL_DOWN)) then
print":O"
end
end
local function AnimationEditorView(pl,origin,angles,fov)
local t = {}
local vec = pl:GetForward()*camDist
local camTarget = pl:GetPos()+Vector(0,0,camHeight)
vec:Rotate(angToPlayer)
t.origin=camTarget+vec
t.angles=(camTarget-t.origin):Angle()
return t
end
local function AnimationEditorOff()
for i,v in pairs(animEditorPanels) do
v:Remove()
end
hook.Remove("HUDPaint","PaintTopBar")
hook.Remove("CalcView","AnimationView")
hook.Remove("Think","FixMouse")
hook.Remove("ShouldDrawLocalPlayer","DrawMe")
LocalPlayer():StopAllLuaAnimations()
gui.EnableScreenClicker(false)
animating = false
animName = nil
animationData = nil
animType = nil
editingAnimation = false
end
local function AnimationEditorOn()
if not LocalPlayer():IsSuperAdmin() and not game.SinglePlayer() then return end
if animating then AnimationEditorOff() return end
for i,v in pairs(animEditorPanels) do
v:Remove()
end
local close = vgui.Create("DButton")
close:SetText("C")
close.DoClick = function(slf) AnimationEditorOff() end
close:SetSize(16,16)
close:SetPos(4,4)
table.insert(animEditorPanels,close)
timeLine = vgui.Create("AnimEditor_TimeLine")
table.insert(animEditorPanels,timeLine)
local frame=vgui.Create("DFrame")
frame:SetTitle("Main Menu")
frame:ShowCloseButton(false)
table.insert(animEditorPanels,frame)
mainSettings = vgui.Create("AnimEditor_MainSettings",frame)
frame:SetSize(mainSettings:GetWide(), mainSettings:GetTall()+22)
timer.Simple(0.01, function() frame:SetPos(ScrW()-200,ScrH()-mainSettings:GetTall()-timeLine:GetTall()*1.7) end)
table.insert(animEditorPanels,mainSettings)
local sliderFrame = vgui.Create("DFrame")
sliderFrame:ShowCloseButton(false)
sliderFrame:SetTitle("Sliders")
sliderFrame:MakePopup()
sliders = vgui.Create("AnimEditor_Sliders",sliderFrame)
table.insert(animEditorPanels,sliderFrame)
subAnims = vgui.Create("AnimEditor_SubAnimations")
table.insert(animEditorPanels,subAnims)
hook.Add("HUDPaint","PaintTopBar",PaintTopBar)
hook.Add("CalcView","AnimationView",AnimationEditorView)
hook.Add("Think","FixMouse",FixMouse)
hook.Add("ShouldDrawLocalPlayer","DrawMe",function() return true end)
gui.EnableScreenClicker(true)
animating = true
end
concommand.Add("animate",AnimationEditorOn)
local secondDistance = 200 --100px per second on timeline
local MAIN = {}
function MAIN:Init()
self:SetName("Main Settings")
self:SetSize(200,315)
self:SetPos(0,22)
local newanim = self:Button("New Animation")
newanim.DoClick = NewAnimation
local loadanim = self:Button("Load Registered Animation")
loadanim.DoClick = LoadAnimation
local loadanim = self:Button("Load Animation From File")
loadanim.DoClick = LoadAnimationFromFile
local saveanim = self:Button("Save Animation To File")
saveanim.DoClick = SaveAnimation
local register = self:Button("Register All Animations")
register.DoClick = RegisterAll
local viewcode = self:Button("Copy Raw Lua To Clipboard")
viewcode.DoClick = function() local str = OutputCode() if !str then return end SetClipboardText(str) end
local distSlider = self:NumSlider("Cam Distance", nil, 40, 200, 0 )
distSlider:SetValue(200)
distSlider.OnValueChanged = function(s,v) camDist = v end
local boneSet = self:ComboBox("Bone Set")
for i,v in pairs(boneList) do
boneSet:AddChoice(i)
end
boneSet:SetText(selectedBoneSet)
boneSet.OnSelect = function(s,i,v,d) selectedBoneSet = v self:RefreshBoneSet() end
self.bones = self:ComboBox("Selected Bone")
self.bones:SetTall(200)
--self.bones:SetMultiple(false)
self.bones.OnSelect = function(me, index, value, data)
selectedBone = value
sliders:SetFrameData()
end
self:RefreshBoneSet()
end
function MAIN:RefreshBoneSet()
if not boneList[selectedBoneSet] then return end
self.bones:Clear()
for i, v in pairs(boneList[selectedBoneSet]) do
--self.bones:AddItem(v).DoClick = function(s) selectedBone = s:GetValue() sliders:SetFrameData() end
local id = self.bones:AddChoice(v)
end
end
vgui.Register("AnimEditor_MainSettings", MAIN, "DForm")
local firstPass = true
local TIMELINE = {}
function TIMELINE:Init()
self:SetTitle("Timeline")
self:ShowCloseButton(false)
self:SetSize(ScrW(),150)
self:SetPos(0,ScrH()-150)
self:SetDraggable(false)
local timeLine = vgui.Create("DHorizontalScroller",self)
timeLine:SetPos(5,45)
timeLine:SetSize(self:GetWide()-self:GetTall()-30,20)
self.timeLine = timeLine
self.subAnims = vgui.Create("DPanelList",self)
self.subAnims:SetSize(timeLine:GetWide(),self:GetTall()-75)
self.subAnims:SetPos(5,50+timeLine:GetTall())
self.subAnims:EnableVerticalScrollbar()
local timeLineTop = vgui.Create("DPanel",self)
timeLineTop:SetPos(5,25)
timeLineTop:SetSize(self:GetWide()-self:GetTall(),20)
timeLineTop.Paint = function(s)
local XPos = timeLine.OffsetX
draw.RoundedBox(0,0,0,self:GetWide(),16,Color(200,200,200,255))
if animName then
if playingAnimation then
playBarOffset = playBarOffset + FrameTime()*secondDistance
end
local subtraction = 0
if firstPass && animationData.StartFrame then
for i=1,animationData.StartFrame do
local v = animationData.FrameData[i]
subtraction = subtraction+(1/(v.FrameRate or 1))
end
elseif !firstPass && animationData.RestartFrame then
for i=1,animationData.RestartFrame do
local v = animationData.FrameData[i]
subtraction = subtraction+(1/(v.FrameRate or 1))
end
end
if (playBarOffset-subtraction)/secondDistance > self:GetAnimationTime() then
local restartPos = self:ResolveRestart()
playBarOffset = restartPos*secondDistance
end
draw.RoundedBox(0,playBarOffset-1,0,2,16,Color(255,0,0,240))
end
local previousSecond = XPos-(XPos%secondDistance)
for i=previousSecond,previousSecond+s:GetWide(),secondDistance/4 do
if i-XPos > 0 && i-XPos < ScrW() then
local sec = i/secondDistance
draw.SimpleText(sec,"DefaultFontSmall",i-XPos,6,Color(0,0,0,255),1,1)
end
end
end
local addKeyButton = vgui.Create("DButton",self)
addKeyButton:SetText("Add KeyFrame")
addKeyButton.DoClick = function() self:AddKeyFrame() end
addKeyButton:SetSize(self:GetTall()-20,self:GetTall()-60)
addKeyButton:SetPos(self:GetWide()-self:GetTall()+10,30)
self.addKeyButton = addKeyButton
addKeyButton:SetDisabled(true)
self.isPlaying = false
local play = vgui.Create("DButton",self)
play:SetPos(self:GetWide()-self:GetTall()+10,self:GetTall()-25)
play:SetWide(self:GetTall()-60)
play:SetText("Play")
play.DoClick = function()
self:Toggle()
end
self.play = play
self.play:SetDisabled(true)
end
function TIMELINE:Toggle(bForce)
if bForce != nil then
self.isPlaying = bForce
else
self.isPlaying = !self.isPlaying
end
if self.isPlaying then
RegisterLuaAnimation("editortest",animationData)
LocalPlayer():StopAllLuaAnimations()
LocalPlayer():SetLuaAnimation("editortest")
for i,v in pairs(subAnimationsLoaded) do
LocalPlayer():SetLuaAnimation(i)
end
playingAnimation = true
playBarOffset = self:ResolveStart()*secondDistance
self.play:SetText("Stop")
for i,v in pairs(subAnimationsLoaded) do
v.subPlayBarOffset = v.storedTimeTillStart
end
else
LocalPlayer():StopAllLuaAnimations()
playingAnimation = false
playBarOffset = self:ResolveStart()*secondDistance
self.play:SetText("Play")
for i,v in pairs(subAnimationsLoaded) do
v.subPlayBarOffset = v.storedTimeTillStart
end
end
end
function TIMELINE:OnNewAnimation()
for i,v in pairs(self.timeLine.Panels) do
v:Remove()
self.timeLine.Panels[i] = nil
end
self.addKeyButton:SetDisabled(false)
self.play:SetDisabled(false)
self:AddKeyFrame() --helper add first frame
end
local addFrame = true
function TIMELINE:OnLoadAnimation()
for i,v in pairs(self.timeLine.Panels) do
v:Remove()
self.timeLine.Panels[i] = nil
end
self.addKeyButton:SetDisabled(false)
self.play:SetDisabled(false)
addFrame = false
for i,v in pairs(animationData.FrameData) do
local keyframe = self:AddKeyFrame() --helper add first frame
keyframe:SetFrameData(i,v)
end
addFrame = true
end
local flip = false
function TIMELINE:LoadSubAnimation(name)
local anim = GetLuaAnimations()[name]
if !anim then return end
if subAnimationsLoaded[name] then
self.subAnims:RemoveItem(subAnimationsLoaded[name])
subAnimationsLoaded[name] = nil
else
flip = !flip
local timeLine = vgui.Create("DHorizontalScroller")
timeLine:SetPos(5,45)