This repository has been archived by the owner on Jun 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
TransformDragger.rbxmx
6720 lines (5225 loc) · 203 KB
/
TransformDragger.rbxmx
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
<roblox xmlns:xmime="http://www.w3.org/2005/05/xmlmime" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.roblox.com/roblox.xsd" version="4">
<External>null</External>
<External>nil</External>
<Item class="Model" referent="RBXFF48B6F5FC3A49258275900CE593573C">
<Properties>
<CoordinateFrame name="ModelInPrimary">
<X>0</X>
<Y>0</Y>
<Z>0</Z>
<R00>1</R00>
<R01>0</R01>
<R02>0</R02>
<R10>0</R10>
<R11>1</R11>
<R12>0</R12>
<R20>0</R20>
<R21>0</R21>
<R22>1</R22>
</CoordinateFrame>
<string name="Name">PrecisionDragger</string>
<Ref name="PrimaryPart">null</Ref>
</Properties>
<Item class="Script" referent="RBX32238E2E0B5D45398607F92063AEAE64">
<Properties>
<bool name="Disabled">false</bool>
<Content name="LinkedSource"><null></null></Content>
<string name="Name">Precision</string>
<string name="ScriptGuid"></string>
<ProtectedString name="Source"><![CDATA[local plugin, settings = plugin, settings
-----------------------------------
-----------MODULE SCRIPTS----------
-----------------------------------
local Collision = require(script.Parent.Collision)
local Utility = require(script.Parent.Utility)
local List = require(script.Parent.List)
local Selection = require(script.Parent.Selection)
local Metapart = require(script.Parent.Metapart)
local FuzzyMath = require(script.Parent.FuzzyMath)
local Round = require(script.Parent.Round)
local Extent = require(script.Parent.Extent)
local Adorn = require(script.Parent.Adornments)
local Input = require(script.Parent.Input)
local RubberBand = require(script.Parent.Rubberband)
-----------------------------------
--------------VARIABLES------------
-----------------------------------
local loaded = false
local on = false
local toolbar = plugin:CreateToolbar("Transform")
local toolbarbutton = toolbar:CreateButton("Transform", "Precision Dragger", "")
local mouse = plugin:GetMouse(true)
Input.setMouse(mouse)
local allowYSnap = false
local initializedDragPlane = false
local adornmentUpdateNeeded = true
local cg = game:GetService("CoreGui")
local uis = game:GetService("UserInputService")
local shouldBreakJoints = true
local currentlySelecting = false
local planeObject = nil
local holoBox = nil
local planeDragging = false
local localSpace = true
local rotateAdornPart = nil
local hoveredHandles = {n = 0}
local currentDist = nil
local originalSize = nil
local originalCFrame = nil
local lastDist = Vector3.new(0,0,0)
local originalPosition = nil;
local freeDragging = nil;
local startLocation = nil
local baseDragPlane = nil
local dragPlane = nil
local secondaryPart = nil
local secondaryLocation = nil
local secondaryPartCFrame = nil
local secondaryPartSideSelected = nil
local workplaneOffset = 0
local invisiblePart = nil
local previousAABBCFrame = nil
local selectionBoxStart = nil
local pV0, pV1, pV2 = nil
local w0, w1, w2, w3 = nil
local workplaneFrame = CFrame.new()
local currentlyOverHandle = false
local initialPos = nil
local mouseOffsetOnGrabHandle = nil
------------------------------------
local castPart = nil
local castSecondaryPart = nil
local castSecondaryLocation = nil
local castSecondaryPartSideSelected = nil
local castPartAdorn = nil
local castPlane = nil
local castWorkplaneOffset = nil
local waypointUndoConnection = nil
local waypointRedoConnection = nil
local inputBeganConnection = nil
local inputEndedConnection = nil
local inputChangedConnection = nil
local selectionChangedConnection = nil
local renderSteppedConnection = nil
local dragEnterConnection = nil
local selectedPartCFrameBeforeDrag = nil
local dragFromToolbox = false
local clickOnUpdate = false
local updateInvisiblePartNeeded = 0
local handleWasDragged = false
local dragPart
local dragPartHoloBox
local TOP_SIDE = 0
local BOTTOM_SIDE = 1
local LEFT_SIDE = 2
local RIGHT_SIDE = 3
local FRONT_SIDE = 4
local BACK_SIDE = 5
local function getWorkplane()
return workplaneFrame, workplaneOffset
end
Adorn.setWorkplaneAccessor(getWorkplane)
game:GetService("ContentProvider"):Preload("rbxassetid://232196030")
game:GetService("ContentProvider"):Preload("rbxassetid://233257118")
--handleDeclaration
local H_NONE = 0
local T_Y_POS = 1
local S_X_POS = 2
local S_X_NEG = 3
local S_Z_POS = 4
local S_Z_NEG = 5
local S_Y_POS = 6
local S_X_POS_Z_POS = 7
local S_X_POS_Z_NEG = 8
local S_X_NEG_Z_POS = 9
local S_X_NEG_Z_NEG = 10
local R_XY = 11
local R_XZ = 12
local R_YZ = 13
local H_PLANE = 14
local handlesCurrentlyOver = {}
local setAnchoredStateForMovingParts = false
local states = {}
local function BreakJoints(part)
if part:IsA("Wrapped") then
workspace:UnjoinFromOutsiders({part.Object})
elseif part:IsA("BasePart") then
workspace:UnjoinFromOutsiders({part})
end
end
local function JoinSelection()
local selection = Selection.getFilteredSelection()
workspace:JoinToOutsiders(selection, plugin:GetJoinMode())
end
local function UnjoinSelection()
local selection = Selection.getFilteredSelection()
workspace:UnjoinFromOutsiders(selection)
end
--duplicate code, please consolidate
local roots = {}
local function getAllRoots(item)
if item:IsA("BasePart") and item:GetRootPart() then
roots[item:GetRootPart()] = true
end
local children = item:GetChildren()
for i, v in ipairs(children) do
getAllRoots(v)
end
end
local function getSelectionRoots()
local selection = Selection.getFilteredSelection()
roots = {}
for i, v in ipairs(selection) do
getAllRoots(v)
end
return roots
end
local function setModelCFrame(model, finalCF)
roots = {}
getAllRoots(model)
local originalCF = model:GetModelCFrame()
for k, v in pairs(roots) do
k.CFrame = finalCF:toWorldSpace(originalCF:toObjectSpace(k.CFrame))
end
end
local function moveSelection(initialCFrame, finalCFrame)
local rootSelection = getSelectionRoots()
for k, v in pairs(rootSelection) do
k.CFrame = finalCFrame:toWorldSpace(initialCFrame:toObjectSpace(k.CFrame))
end
end
local function updateRotatePart()
local selection = Selection.getFilteredSelection()
if #selection > 0 then
if not rotateAdornPart then
rotateAdornPart = Instance.new("Part", game.CoreGui)
rotateAdornPart.Name = "RotateAdornPart"
end
local filteredSelectionMetaPart = Selection.getFilteredSelectionMetapart()
rotateAdornPart.CFrame = filteredSelectionMetaPart.CFrame
rotateAdornPart.Size = filteredSelectionMetaPart.Size
Adorn.adornInstanceWithRotate(rotateAdornPart)
end
end
local function updateAdornments()
local filteredSelectionMetapart = Selection.getFilteredSelectionMetapart()
if filteredSelectionMetapart then
filteredSelectionMetapart.ClearCache()
end
updateInvisiblePart()
updateRotatePart()
end
local function onPressMouse()
Adorn.grabHandle()
local currentHandle = Adorn.getCurrentHandle()
if currentHandle == H_NONE and not Adorn.isPlaneSelectingModeOn() then selectPart() end
if currentHandle == H_PLANE then
initialPos = mouse.Origin.p
else
local currentAdorn = Adorn.getCurrentAdornment()
if currentAdorn then
initialPos = Adorn.getAdornmentWorldCFrame(Adorn.getCurrentAdornment()[1]).p
end
end
grabHandle(currentHandle, initialPos)
end
local function onReleaseMouse()
initialPos = nil
if not RubberBand.isRubberBandDragInProgress() then
releaseHandle()
end
releasePart()
Adorn.releaseHandle()
planeDragging = false
if planeObject and false then
planeObject:Destroy()
planeObject = nil
end
end
-------------------------------------------------
toolbarbutton.Click:connect(function()
if on and Off then
Off()
elseif loaded and On then
On()
end
end)
plugin.Deactivation:connect(function()
if on and Off then Off() end
end)
--------------------MATH STUFFS-------------------
function squaredMagnitude(vect3)
return vect3.X*vect3.X + vect3.Y*vect3.Y + vect3.Z*vect3.Z
end
function vector3Direction(vect3)
local lenSquared = squaredMagnitude(vect3)
local invSqrt = 1.0 / math.sqrt(lenSquared)
return Vector3.new(vect3.X * invSqrt, vect3.Y * invSqrt, vect3.Z *invSqrt)
end
function vector3LessThanOrEqualTo(vect1, vect2)
return vect1.x <= vect2.x and vect1.y <= vect2.y and vect1.z <= vect2.z
end
function vector3GreaterThanOrEqualTo(vect1, vect2)
return vect1.x >= vect2.x and vect1.y >= vect2.y and vect1.z >= vect2.z
end
function createPlane(vector0, vector1, vector2)
local p1 = vector1 - vector0
local p2 = vector2 - vector0
local cross = p1:Cross(p2)
local _normal = vector3Direction(cross)
local _distance = _normal:Dot(vector0)
return {v0=vector0, v1=vector1, v2=vector2, normal=_normal, distance=_distance}
end
function rayPlaneIntersection(ray, plane)
local dotProd = ray.Direction:Dot(plane.normal)
local t = -((-plane.distance) + ray.Origin:Dot(plane.normal)) / dotProd
return ray.Origin + ray.Direction * t
end
function boxSideTest(ray, normal, size, p)
local origin = size * normal
local t = (-((-normal:Dot(origin)) + ray.Origin:Dot(normal))) / ray.Direction:Dot(normal)
local hit = ray.Origin + ray.Direction * t
if (origin.x ~= 0 or hit.x <= size.x) and
(origin.x ~= 0 or hit.x >= -size.x) and
(origin.y ~= 0 or hit.y <= size.y) and
(origin.y ~= 0 or hit.y >= -size.y) and
(origin.z ~= 0 or hit.z <= size.z) and
(origin.z ~= 0 or hit.z >= -size.z) then
return hit
end
return nil
end
function rayBoxIntersection(ray, cframe, size)
size = size / 2
local localRay = Ray.new(cframe:pointToObjectSpace(ray.Origin), cframe:pointToObjectSpace(ray.Direction + cframe.p).unit )
if (localRay.Origin.x < -size.x) and (localRay.Direction.x > 0) then
local result = boxSideTest(localRay, Vector3.new(-1, 0, 0), size)
if result then return cframe:pointToWorldSpace(result) end
elseif (localRay.Origin.x > size.x) and (localRay.Direction.x < 0) then
local result = boxSideTest(localRay, Vector3.new(1, 0, 0), size, true)
if result then return cframe:pointToWorldSpace(result) end
end
if (localRay.Origin.y < -size.y) and (localRay.Direction.y > 0) then
local result = boxSideTest(localRay, Vector3.new(0, -1, 0), size)
if result then return cframe:pointToWorldSpace(result) end
elseif (localRay.Origin.y > size.y) and (localRay.Direction.y < 0) then
local result = boxSideTest(localRay, Vector3.new(0, 1, 0), size)
if result then return cframe:pointToWorldSpace(result) end
end
if (localRay.Origin.z < -size.z) and (localRay.Direction.z > 0) then
local result = boxSideTest(localRay, Vector3.new(0, 0, -1), size)
if result then return cframe:pointToWorldSpace(result) end
elseif (localRay.Origin.z > size.z) and (localRay.Direction.z < 0) then
local result = boxSideTest(localRay, Vector3.new(0, 0, 1), size)
if result then return cframe:pointToWorldSpace(result) end
end
return cframe:pointToWorldSpace(Vector3.new(0,0,0))
end
function projectVectorToPlane(vect, planeNormal)
return vect - (vect:Dot(planeNormal.Unit) * planeNormal.Unit)
end
--- Collision ---
local function moveUntilCollideWrapper(part, threshold, ignoreList, direction, maximum)
if not ignoreList then ignoreList = {} end
return Collision.moveUntilCollide(part, ignoreList, direction, threshold, maximum)
end
local function safeMoveWrapper(part, threshold, ignoreList, direction)
Collision.SafeMove(part, ignoreList, direction)
end
local function safeMoveWhiteList(part, direction, whiteList)
Collision.SafeMove(part, List.createIgnoreListGivenWhiteList(game.Workspace, whiteList), direction)
end
local function safeRotate(part, previousChange)
--TODO: Actual safe rotate
part.CFrame = (previousChange) * part.CFrame
end
--- Get Grid ---
function getRotationalIntervalFromGrid()
local grid = plugin.GridSize
if FuzzyMath.fuzzyCompare(grid, 0.2) then
return 15
elseif FuzzyMath.fuzzyCompare(grid, 0.01) then
return 1
else
return 45
end
end
---Round Num ---
function roundToNearestGrid(value)
local interval = plugin.GridSize
if interval ~= 0 then
return Round.roundToNearest(value, interval)
end
return value
end
function Vector3ToNearestGrid(value)
return Vector3.new( roundToNearestGrid(value.X), roundToNearestGrid(value.Y), roundToNearestGrid(value.Z))
end
function getScaleHandleLocalVector(handle)
if handle == S_X_POS then
return Vector3.new(1, 0, 0)
elseif handle == S_X_NEG then
return Vector3.new(-1, 0, 0)
elseif handle == S_Z_POS then
return Vector3.new(0, 0, 1)
elseif handle == S_Z_NEG then
return Vector3.new(0, 0, -1)
elseif handle == S_Y_POS then
return Vector3.new(0, 1, 0)
--elseif handle == S_Y_NEG then
-- return Vector3.new(0, -1, 0)
elseif handle == S_X_POS_Z_POS then
return Vector3.new(1, 0, 1)
elseif handle == S_X_POS_Z_NEG then
return Vector3.new(1, 0, -1)
elseif handle == S_X_NEG_Z_POS then
return Vector3.new(-1, 0, 1)
elseif handle == S_X_NEG_Z_NEG then
return Vector3.new(-1, 0, -1)
end
return Vector3.new(0, 0, 0)
end
function snapVector3ByHandle(value, handle)
if handle == S_X_POS or
handle == S_X_NEG or
handle == S_X_POS_Z_POS or
handle == S_X_POS_Z_NEG or
handle == S_X_NEG_Z_POS or
handle == S_X_NEG_Z_NEG then
value = Vector3.new(math.max(roundToNearestGrid(value.X), plugin.GridSize), value.Y, value.Z)
end
if handle == S_Z_POS or
handle == S_Z_NEG or
handle == S_X_POS_Z_POS or
handle == S_X_POS_Z_NEG or
handle == S_X_NEG_Z_POS or
handle == S_X_NEG_Z_NEG then
value = Vector3.new(value.X, value.Y, math.max(roundToNearestGrid(value.Z), plugin.GridSize))
end
if handle == S_Y_POS or
handle == T_Y_POS then
value = Vector3.new(value.X, math.max(roundToNearestGrid(value.Y), plugin.GridSize), value.Z)
end
return value
end
--------------------------------------------------
function getSelectedPart()
local selectedItems = Selection.getFilteredSelection()
if (#selectedItems < 1) then return nil end
return selectedItems[1]
end
function getCurrentSelectionWithChildren(children, t)
if not t then t = {} end
if not children then children = Selection.getFilteredSelection() end
for i,v in pairs(children) do
if v:IsA("BasePart") then table.insert(t, v) end
if #v:GetChildren() then
t = getCurrentSelectionWithChildren(v:GetChildren(), t)
end
end
return t
end
function setPartPosition(part, position)
part.CFrame = part.CFrame - part.CFrame.p + position
end
function setPartRotation(part, cframe)
part.CFrame = cframe - cframe.p + part.CFrame.p
end
------------------------------------------------
function cosineSimilarity(v1, v2)
local value = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z)/ (math.sqrt(math.pow(v1.x,2) + math.pow(v1.y, 2) + math.pow(v1.z, 2)) * math.sqrt(math.pow(v2.x,2) + math.pow(v2.y, 2) + math.pow(v2.z, 2)))
local radAngle = math.acos(value)
return math.deg(radAngle)
end
function setWaypoint()
removeDragPart()
local filteredSelectionMetapart = Selection.getFilteredSelectionMetapart()
local currentHandle = Adorn.getCurrentHandle()
local handleType = "Unknown"
if currentHandle == S_X_POS or
currentHandle == S_Y_POS or
currentHandle == S_Z_POS or
currentHandle == S_X_NEG or
currentHandle == S_Z_NEG or
currentHandle == S_X_POS_Z_POS or
currentHandle == S_X_NEG_Z_POS or
currentHandle == S_X_NEG_Z_NEG or
currentHandle == S_X_POS_Z_NEG then
handleType = "Scale"
elseif currentHandle == T_Y_POS then
handleType = "Move"
elseif currentHandle == R_XY or
currentHandle == R_XZ or
currentHandle == R_YZ then
handleType = "Rotate"
end
if filteredSelectionMetapart and (filteredSelectionMetapart.CFrame ~= originalPosition or filteredSelectionMetapart.Size ~= originalSize) then
game:GetService("ChangeHistoryService"):SetWaypoint(handleType)
end
end
local function setSelection(newSelection)
game:GetService("Selection"):Set(newSelection)
end
function rotateCFrame(cframe, side)
if side == BOTTOM_SIDE then
cframe = cframe * CFrame.Angles(math.rad(180), 0, 0)
elseif side == RIGHT_SIDE then
cframe = cframe * CFrame.Angles(0, 0, -math.rad(90))
elseif side == LEFT_SIDE then
cframe = cframe * CFrame.Angles(0, 0, math.rad(90))
elseif side == FRONT_SIDE then
cframe = cframe * CFrame.Angles(-math.rad(90), 0, 0)
elseif side == BACK_SIDE then
cframe = cframe * CFrame.Angles(math.rad(90), 0, 0)
end
return cframe
end
function updateInvisiblePart()
if not invisiblePart then return end
local selection = Selection.getFilteredSelection()
if #selection < 1 then
return
end
if #selection > 1 then
end
local filteredSelectionMetapart = Selection.getFilteredSelectionMetapart()
if secondaryPart then
if filteredSelectionMetapart then
filteredSelectionMetapart.UpdatePlaneCFrame = workplaneFrame
invisiblePart.CFrame = filteredSelectionMetapart.PlaneAlignedCFrame
invisiblePart.Size = filteredSelectionMetapart.PlaneAlignedSize
else
local tmpCFrame = secondaryPart.CFrame
invisiblePart.CFrame = CFrame.new(invisiblePart.CFrame.p)
Extent.setPartCFrameToExtents(invisiblePart, rotateCFrame(tmpCFrame, secondaryPartSideSelected))
end
else
Extent.setPartCFrameToExtents(invisiblePart, nil)
end
if secondaryLocation then
workplaneOffset = -invisiblePart.CFrame:pointToObjectSpace(secondaryLocation).y
end
end
local function resetDragger()
Adorn.resetDragger()
end
------------------------------------------------------------------------------------------
------------------------------Adornment Drag Part Update----------------------------------
------------------------------------------------------------------------------------------
local rotationalDiff = 0
local isInsideRotate = false
local intersectionPoint = nil
--TODO:: lots of copy pasta here, please consolidate
function preUpdatePart()
--calculateCurrentDistance
local currentHandle = Adorn.getCurrentHandle()
if currentHandle == H_NONE then return end
if not initialPos then return end
if currentHandle == H_PLANE then return end
local currentAdornment = Adorn.getCurrentAdornment()
local adornee = currentAdornment[1].Adornee
if not adornee then return end
local worldCFrame = Adorn.getAdornmentWorldCFrame(currentAdornment[1])
local lookVector = mouse.UnitRay
if currentHandle == T_Y_POS or currentHandle == S_Y_POS then
local upVector = (adornee.CFrame:pointToWorldSpace(Vector3.new(0, 1, 0)) - adornee.CFrame.p).Unit
local planeNormal = lookVector.Direction - (lookVector.Direction:Dot(upVector) * upVector)
local dist = planeNormal:Dot(worldCFrame.p)
local dotProd = lookVector.Direction:Dot(planeNormal)
local t = -((-dist) + lookVector.Origin:Dot(planeNormal)) / dotProd
local intersection = lookVector.Origin + lookVector.Direction * t
local origin = initialPos - (upVector.Unit * 800)
local point = Ray.new(initialPos , upVector.Unit):ClosestPoint(intersection)
local upV = Vector3.new(0, 1, 0)
if point == initialPos then
point = Ray.new(initialPos , -upVector.Unit):ClosestPoint(intersection)
upV = upV * -1
end
currentDist = (point - initialPos).Magnitude * upV
elseif currentHandle == S_X_POS or currentHandle == S_X_NEG then
local moveVector = (adornee.CFrame:pointToWorldSpace(Vector3.new(1, 0, 0)) - adornee.CFrame.p).Unit
local planeNormal = lookVector.Direction - (lookVector.Direction:Dot(moveVector) * moveVector)
local dist = planeNormal:Dot(worldCFrame.p)
local dotProd = lookVector.Direction:Dot(planeNormal)
local t = -((-dist) + lookVector.Origin:Dot(planeNormal)) / dotProd
local intersection = lookVector.Origin + lookVector.Direction * t
local origin = initialPos - (moveVector.Unit * 800)
local point = Ray.new(initialPos , moveVector.Unit):ClosestPoint(intersection)
local upV = Vector3.new(1, 0, 0)
if point == initialPos then
point = Ray.new(initialPos , -moveVector.Unit):ClosestPoint(intersection)
upV = upV * -1
end
currentDist = (point - initialPos).Magnitude * upV
if currentHandle == S_X_NEG then
currentDist = currentDist * -1
end
elseif currentHandle == S_Z_POS or currentHandle == S_Z_NEG then
local moveVector = (adornee.CFrame:pointToWorldSpace(Vector3.new(0, 0, 1)) - adornee.CFrame.p).Unit
local planeNormal = lookVector.Direction - (lookVector.Direction:Dot(moveVector) * moveVector)
local dist = planeNormal:Dot(worldCFrame.p)
local dotProd = lookVector.Direction:Dot(planeNormal)
local t = -((-dist) + lookVector.Origin:Dot(planeNormal)) / dotProd
local intersection = lookVector.Origin + lookVector.Direction * t
local origin = initialPos - (moveVector.Unit * 800)
local point = Ray.new(initialPos , moveVector.Unit):ClosestPoint(intersection)
local upV = Vector3.new(0, 0, 1)
if point == initialPos then
point = Ray.new(initialPos , -moveVector.Unit):ClosestPoint(intersection)
upV = upV * -1
end
currentDist = (point - initialPos).Magnitude * upV
if currentHandle == S_Z_NEG then
currentDist = currentDist * -1
end
elseif currentHandle == S_X_POS_Z_POS or currentHandle == S_X_NEG_Z_NEG or currentHandle == S_X_POS_Z_NEG or currentHandle == S_X_NEG_Z_POS then
local planeNormal = (adornee.CFrame:pointToWorldSpace(Vector3.new(0, 1, 0)) - adornee.CFrame.p).Unit
local dist = planeNormal:Dot(worldCFrame.p)
local dotProd = lookVector.Direction:Dot(planeNormal)
local t = -((-dist) + lookVector.Origin:Dot(planeNormal)) / dotProd
local intersection = lookVector.Origin + lookVector.Direction * t
currentDist = originalCFrame:pointToObjectSpace(intersection + originalCFrame.p) - originalCFrame:pointToObjectSpace(initialPos + originalCFrame.p)
if currentHandle == S_X_NEG_Z_POS or currentHandle == S_X_NEG_Z_NEG then
currentDist = currentDist * Vector3.new(-1, 1, 1)
end
if currentHandle == S_X_POS_Z_NEG or currentHandle == S_X_NEG_Z_NEG then
currentDist = currentDist * Vector3.new(1, 1, -1)
end
elseif currentHandle == R_XY or currentHandle == R_XZ or currentHandle == R_YZ then
local planeNormal = (adornee.CFrame:pointToWorldSpace(Vector3.new(currentHandle == R_YZ and 1 or 0, currentHandle == R_XZ and 1 or 0, currentHandle == R_XY and 1 or 0)) - adornee.CFrame.p).Unit
local dist = planeNormal:Dot(worldCFrame.p)
local dotProd = lookVector.Direction:Dot(planeNormal)
local t = -((-dist) + lookVector.Origin:Dot(planeNormal)) / dotProd
intersectionPoint = lookVector.Origin + lookVector.Direction * t
currentDist = originalCFrame:pointToObjectSpace(intersectionPoint + originalCFrame.p) - originalCFrame:pointToObjectSpace(initialPos + originalCFrame.p)
isInsideRotate = false
end
if not mouseOffsetOnGrabHandle then
mouseOffsetOnGrabHandle = currentDist
else
currentDist = currentDist - mouseOffsetOnGrabHandle
end
end
local planePreviouslySelected = false
local itemToUpdate = nil
local sanitizationPrecision = 1000000
local function sanitizeFloatTest(value)
return value > 0.0 and (math.ceil((value * sanitizationPrecision) - 0.5) / sanitizationPrecision) or (math.floor((value * sanitizationPrecision) + 0.5) / sanitizationPrecision)
end
local function sanitizeCFrameTest(value)
local x, y, z, r00, r01, r02, r10, r11, r12, r20, r21, r22 = value:components()
return CFrame.new(
sanitizeFloat(x),
sanitizeFloat(y),
sanitizeFloat(z),
sanitizeFloat(r00),
sanitizeFloat(r01),
sanitizeFloat(r02),
sanitizeFloat(r10),
sanitizeFloat(r11),
sanitizeFloat(r12),
sanitizeFloat(r20),
sanitizeFloat(r21),
sanitizeFloat(r22))
end
function getShapeRenderSize(shape, size)
if (shape == Enum.PartType.Cylinder) then
local minYZ = math.min(size.Y, size.Z)
return Vector3.new(size.X, minYZ, minYZ)
end
return size
end
function updateChildAttachments(part, initialSize, finalSize)
if (not part:IsA("BasePart")) then return end
local children = part:GetChildren()
local shape = part.Shape
initialSize = getShapeRenderSize(shape, initialSize)
finalSize = getShapeRenderSize(shape, finalSize)
for i = 1, #children do
local child = children[i]
if (child:IsA("Attachment")) then
child.Position = (child.Position / initialSize) * finalSize
end
end
end
function updatePart()
local filteredSelectionMetapart = Selection.getFilteredSelectionMetapart()
local filteredSelection = Selection.getFilteredSelection()
local currentHandle = Adorn.getCurrentHandle()
if currentHandle == H_NONE then return end
if not setAnchoredStateForMovingParts then
local selection = getCurrentSelectionWithChildren()
setAnchoredStateForMovingParts = true
for i, v in ipairs(selection) do
states[i] = v.Anchored
v.Anchored = true
end
end
preUpdatePart()
if ((not originalSize or not originalCFrame) and currentHandle ~= T_Y_POS or not currentDist) then
if currentHandle ~= R_XY and
currentHandle ~= R_XZ and
currentHandle ~= R_YZ and
currentHandle ~= H_PLANE then
return
end
end
local allowAdornUpdate = true
if selectedPart and shouldBreakJoints then
UnjoinSelection()
--BreakJoints(selectedPart)
end
local refreshDragPart = true
local refreshInvisiblePart = true
local preactionCFrame
local preActionSize
if selectedPart and not itemToUpdate then
preactionCFrame = selectedPart.CFrame
preActionSize = selectedPart.Size
end
handleWasDragged = true
local selection = Selection.getFilteredSelection()
if #selection == 0 then
resetDragger()
return
end
local isPreviouslyColliding = false
if plugin.CollisionEnabled then
invisiblePart.Parent = workspace
if List.itemsHasItemNotInList(invisiblePart:GetTouchingParts(), selection) then
isPreviouslyColliding = true
end
invisiblePart.Parent = nil
end
if currentHandle == S_Y_POS then
local yScale = -Adorn.getYScale()
currentDist = currentDist * -yScale
selectedPart.Size = snapVector3ByHandle(originalSize + currentDist, currentHandle)
if shouldBreakJoints then
UnjoinSelection()
BreakJoints(selectedPart)
end
selectedPart.CFrame = originalCFrame:toWorldSpace(CFrame.new(yScale * ((originalSize - selectedPart.Size) / 2)))
local totalDist = Utility.distanceVector3(preactionCFrame.p, selectedPart.CFrame.p)
local diff = (preactionCFrame.p - selectedPart.CFrame.p).unit
if plugin.CollisionEnabled and #selectedPart:GetTouchingParts() > 0 then
selectedPart.Size = preActionSize
if shouldBreakJoints then
UnjoinSelection()
BreakJoints(selectedPart)
end
selectedPart.CFrame = preactionCFrame
if isPreviouslyColliding then return end
moveUntilCollideWrapper(selectedPart, 0.00001, nil, diff * -1, totalDist * 2)
local distanceMoved = Utility.distanceVector3(preactionCFrame.p, selectedPart.CFrame.p)
local positionDiff = preactionCFrame.p - selectedPart.CFrame.p
selectedPart.Size = preActionSize - (distanceMoved * getScaleHandleLocalVector(currentHandle) * yScale)
if shouldBreakJoints then
UnjoinSelection()
BreakJoints(selectedPart)
end
selectedPart.CFrame = preactionCFrame - (positionDiff / 2)
end
local normal = selectedPart.CFrame:pointToWorldSpace(selectedPart.Size / 2 * Vector3.new(0, 1, 0)) - selectedPart.CFrame.p
local cameraDirection = game.Workspace.Camera.CoordinateFrame.lookVector
local projectedCameraDirection = cameraDirection - (cameraDirection:Dot(normal) * normal)
local tangent = projectedCameraDirection:Cross(normal).unit
Adorn.scaleOne(selectedPart.CFrame:pointToWorldSpace(selectedPart.Size / 2 * Vector3.new(0, -1, 0)),
selectedPart.CFrame:pointToWorldSpace(selectedPart.Size / 2 * Vector3.new(0, 1, 0)),
tangent)
elseif currentHandle == S_X_POS or
currentHandle == S_Z_POS then
if shouldBreakJoints then
UnjoinSelection()
BreakJoints(selectedPart)
end
selectedPart.Size = snapVector3ByHandle(originalSize + currentDist, currentHandle)
selectedPart.CFrame = originalCFrame:toWorldSpace(CFrame.new(-((originalSize - selectedPart.Size) * 0.5)))
local totalDist = Utility.distanceVector3(preactionCFrame.p, selectedPart.CFrame.p)
local diff = (preactionCFrame.p - selectedPart.CFrame.p).unit
if plugin.CollisionEnabled and #selectedPart:GetTouchingParts() > 0 then
selectedPart.Size = preActionSize
if shouldBreakJoints then
UnjoinSelection()
BreakJoints(selectedPart)
end
selectedPart.CFrame = preactionCFrame
if isPreviouslyColliding then return end
moveUntilCollideWrapper(selectedPart, 0.00001, nil, diff * -1, totalDist * 2)
local distanceMoved = Utility.distanceVector3(preactionCFrame.p, selectedPart.CFrame.p)
local positionDiff = preactionCFrame.p - selectedPart.CFrame.p
selectedPart.Size = preActionSize + (distanceMoved * getScaleHandleLocalVector(currentHandle))