forked from F3XTeam/RBX-Building-Tools
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCore.lua
More file actions
2966 lines (2380 loc) · 80.7 KB
/
Core.lua
File metadata and controls
2966 lines (2380 loc) · 80.7 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
------------------------------------------
-- Create references to important objects
------------------------------------------
Services = {
Workspace = Game:GetService 'Workspace';
Players = Game:GetService 'Players';
Debris = Game:GetService 'Debris';
MarketplaceService = Game:GetService 'MarketplaceService';
ContentProvider = Game:GetService 'ContentProvider';
SoundService = Game:GetService 'SoundService';
UserInputService = Game:GetService 'UserInputService';
TestService = Game:GetService 'TestService';
Selection = Game:GetService 'Selection';
CoreGui = Game:GetService 'CoreGui';
HttpService = Game:GetService 'HttpService';
JointsService = Game.JointsService;
};
Assets = {
DarkSlantedRectangle = 'http://www.roblox.com/asset/?id=127774197';
LightSlantedRectangle = 'http://www.roblox.com/asset/?id=127772502';
ActionCompletionSound = 'http://www.roblox.com/asset/?id=99666917';
ExpandArrow = 'http://www.roblox.com/asset/?id=134367382';
UndoActiveDecal = 'http://www.roblox.com/asset/?id=141741408';
UndoInactiveDecal = 'http://www.roblox.com/asset/?id=142074557';
RedoActiveDecal = 'http://www.roblox.com/asset/?id=141741327';
RedoInactiveDecal = 'http://www.roblox.com/asset/?id=142074553';
DeleteActiveDecal = 'http://www.roblox.com/asset/?id=141896298';
DeleteInactiveDecal = 'http://www.roblox.com/asset/?id=142074644';
ExportActiveDecal = 'http://www.roblox.com/asset/?id=141741337';
ExportInactiveDecal = 'http://www.roblox.com/asset/?id=142074569';
CloneActiveDecal = 'http://www.roblox.com/asset/?id=142073926';
CloneInactiveDecal = 'http://www.roblox.com/asset/?id=142074563';
PluginIcon = 'http://www.roblox.com/asset/?id=142287521';
GroupLockIcon = 'http://www.roblox.com/asset/?id=175396862';
GroupUnlockIcon = 'http://www.roblox.com/asset/?id=160408836';
GroupUpdateOKIcon = 'http://www.roblox.com/asset/?id=164421681';
GroupUpdateIcon = 'http://www.roblox.com/asset/?id=160402908';
};
-- The ID of the tool model on ROBLOX
ToolAssetID = 142785488;
Tool = script.Parent;
Player = Services.Players.LocalPlayer;
Mouse = nil;
-- Set tool or plugin-specific references
if plugin then
ToolType = 'plugin';
GUIContainer = Services.CoreGui;
-- Create the toolbar button
ToolbarButton = plugin:CreateToolbar( 'Building Tools by F3X' ):CreateButton( '', 'Building Tools by F3X', Assets.PluginIcon );
-- Initiate a server only if not in solo testing mode
-- (checked in a potentially unreliable way)
wait( 3 );
if Services.Players.NumPlayers == 0 then
Game:GetService 'NetworkServer';
end;
elseif Tool:IsA 'Tool' then
ToolType = 'tool';
GUIContainer = Player:WaitForChild 'PlayerGui';
end;
------------------------------------------
-- Load external dependencies
------------------------------------------
RbxUtility = LoadLibrary 'RbxUtility';
-- Preload external assets
for ResourceName, ResourceUrl in pairs( Assets ) do
Services.ContentProvider:Preload( ResourceUrl );
end;
repeat wait( 0 ) until _G.gloo;
Gloo = _G.gloo;
Tool:WaitForChild 'HttpInterface';
Tool:WaitForChild 'Interfaces';
------------------------------------------
-- Define functions that are depended-upon
------------------------------------------
function _findTableOccurrences( haystack, needle )
-- Returns the positions of instances of `needle` in table `haystack`
local positions = {};
-- Add any indexes from `haystack` that have `needle`
for index, value in pairs( haystack ) do
if value == needle then
table.insert( positions, index );
end;
end;
return positions;
end;
function _getCollectionInfo( part_collection )
-- Returns the size and position of collection of parts `part_collection`
-- Get the corners
local corners = {};
-- Create shortcuts to certain things that are expensive to call constantly
-- (note: otherwise it actually becomes an issue if the selection grows
-- considerably large)
local table_insert = table.insert;
local newCFrame = CFrame.new;
for _, Part in pairs( part_collection ) do
local PartCFrame = Part.CFrame;
local partCFrameOffset = PartCFrame.toWorldSpace;
local PartSize = Part.Size / 2;
local size_x, size_y, size_z = PartSize.x, PartSize.y, PartSize.z;
table_insert( corners, partCFrameOffset( PartCFrame, newCFrame( size_x, size_y, size_z ) ) );
table_insert( corners, partCFrameOffset( PartCFrame, newCFrame( -size_x, size_y, size_z ) ) );
table_insert( corners, partCFrameOffset( PartCFrame, newCFrame( size_x, -size_y, size_z ) ) );
table_insert( corners, partCFrameOffset( PartCFrame, newCFrame( size_x, size_y, -size_z ) ) );
table_insert( corners, partCFrameOffset( PartCFrame, newCFrame( -size_x, size_y, -size_z ) ) );
table_insert( corners, partCFrameOffset( PartCFrame, newCFrame( -size_x, -size_y, size_z ) ) );
table_insert( corners, partCFrameOffset( PartCFrame, newCFrame( size_x, -size_y, -size_z ) ) );
table_insert( corners, partCFrameOffset( PartCFrame, newCFrame( -size_x, -size_y, -size_z ) ) );
end;
-- Get the extents
local x, y, z = {}, {}, {};
for _, Corner in pairs( corners ) do
table_insert( x, Corner.x );
table_insert( y, Corner.y );
table_insert( z, Corner.z );
end;
local x_min, y_min, z_min = math.min( unpack( x ) ),
math.min( unpack( y ) ),
math.min( unpack( z ) );
local x_max, y_max, z_max = math.max( unpack( x ) ),
math.max( unpack( y ) ),
math.max( unpack( z ) );
-- Get the size between the extents
local x_size, y_size, z_size = x_max - x_min,
y_max - y_min,
z_max - z_min;
local Size = Vector3.new( x_size, y_size, z_size );
-- Get the centroid of the collection of points
local Position = CFrame.new( x_min + ( x_max - x_min ) / 2,
y_min + ( y_max - y_min ) / 2,
z_min + ( z_max - z_min ) / 2 );
-- Return the size of the collection of parts
return Size, Position;
end;
function _round( number, places )
-- Returns `number` rounded to the number of decimal `places`
-- (from lua-users)
local mult = 10 ^ ( places or 0 );
return math.floor( number * mult + 0.5 ) / mult;
end
function _cloneTable( source )
-- Returns a deep copy of table `source`
-- Get a copy of `source`'s metatable, since the hacky method
-- we're using to copy the table doesn't include its metatable
local source_mt = getmetatable( source );
-- Return a copy of `source` including its metatable
return setmetatable( { unpack( source ) }, source_mt );
end;
function _getAllDescendants( Parent )
-- Recursively gets all the descendants of `Parent` and returns them
local descendants = {};
for _, Child in pairs( Parent:GetChildren() ) do
-- Add the direct descendants of `Parent`
table.insert( descendants, Child );
-- Add the descendants of each child
for _, Subchild in pairs( _getAllDescendants( Child ) ) do
table.insert( descendants, Subchild );
end;
end;
return descendants;
end;
function _pointToScreenSpace( Point )
-- Returns Vector3 `Point`'s position on the screen when rendered
-- (kudos to stravant for this)
local point = Services.Workspace.CurrentCamera.CoordinateFrame:pointToObjectSpace( Point );
local aspectRatio = Mouse.ViewSizeX / Mouse.ViewSizeY;
local hfactor = math.tan( math.rad( Services.Workspace.CurrentCamera.FieldOfView ) / 2 )
local wfactor = aspectRatio * hfactor;
local x = ( point.x / point.z ) / -wfactor;
local y = ( point.y / point.z ) / hfactor;
local screen_pos = Vector2.new( Mouse.ViewSizeX * ( 0.5 + 0.5 * x ), Mouse.ViewSizeY * ( 0.5 + 0.5 * y ) );
if ( screen_pos.x < 0 or screen_pos.x > Mouse.ViewSizeX ) or ( screen_pos.y < 0 or screen_pos.y > Mouse.ViewSizeY ) then
return nil;
end;
if Services.Workspace.CurrentCamera.CoordinateFrame:toObjectSpace( CFrame.new( Point ) ).z > 0 then
return nil;
end;
return screen_pos;
end;
function _cloneParts( parts )
-- Returns a table of cloned `parts`
local new_parts = {};
-- Copy the parts into `new_parts`
for part_index, Part in pairs( parts ) do
new_parts[part_index] = Part:Clone();
end;
return new_parts;
end;
function _replaceParts( old_parts, new_parts )
-- Removes `old_parts` and inserts `new_parts`
-- Remove old parts
for _, OldPart in pairs( old_parts ) do
OldPart.Parent = nil;
end;
-- Insert `new_parts
for _, NewPart in pairs( new_parts ) do
NewPart.Parent = Services.Workspace;
NewPart:MakeJoints();
end;
end;
function _splitString( str, delimiter )
-- Returns a table of string `str` split by pattern `delimiter`
local parts = {};
local pattern = ( "([^%s]+)" ):format( delimiter );
str:gsub( pattern, function ( part )
table.insert( parts, part );
end );
return parts;
end;
function _generateSerializationID()
-- Returns a random 5-character string
-- with characters A-Z, a-z, and 0-9
-- (there are 916,132,832 unique IDs)
local characters = {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
local serialization_id = "";
-- Pick out 5 random characters
for _ = 1, 5 do
serialization_id = serialization_id .. ( characters[math.random( #characters )] );
end;
return serialization_id;
end;
function _splitNumberListString( str )
-- Returns the contents of _splitString( str, ", " ), except
-- each value in the table is turned into a number
-- Get the number strings
local numbers = _splitString( str, ", " );
-- Turn them into numbers
for number_index, number in pairs( numbers ) do
numbers[number_index] = tonumber( number );
end;
-- Return `numbers`
return numbers;
end;
function _getSerializationPartType( Part )
-- Returns a special number that determines the type of
-- part `Part` is
local Types = {
Normal = 1,
Truss = 2,
Wedge = 3,
Corner = 4,
Cylinder = 5,
Ball = 6,
Seat = 7,
VehicleSeat = 8,
Spawn = 9
};
-- Return the appropriate type number
if Part.ClassName == "Part" then
if Part.Shape == Enum.PartType.Block then
return Types.Normal;
elseif Part.Shape == Enum.PartType.Cylinder then
return Types.Cylinder;
elseif Part.Shape == Enum.PartType.Ball then
return Types.Ball;
end;
elseif Part.ClassName == "Seat" then
return Types.Seat;
elseif Part.ClassName == "VehicleSeat" then
return Types.VehicleSeat;
elseif Part.ClassName == "SpawnLocation" then
return Types.Spawn;
elseif Part.ClassName == "WedgePart" then
return Types.Wedge;
elseif Part.ClassName == "CornerWedgePart" then
return Types.Corner;
elseif Part.ClassName == "TrussPart" then
return Types.Truss;
end;
end;
function _serializeParts( parts )
-- Returns JSON-encoded data about parts in
-- table `parts` that can be used to recreate them
local data = {
version = 1,
parts = {}
};
local objects = {};
-- Store part data
for _, Part in pairs( parts ) do
local part_id = _generateSerializationID();
local PartData = {
_getSerializationPartType( Part ),
_splitNumberListString( tostring( Part.Size ) ),
_splitNumberListString( tostring( Part.CFrame ) ),
Part.BrickColor.Number,
Part.Material.Value,
Part.Anchored,
Part.CanCollide,
Part.Reflectance,
Part.Transparency,
Part.TopSurface.Value,
Part.BottomSurface.Value,
Part.LeftSurface.Value,
Part.RightSurface.Value,
Part.FrontSurface.Value,
Part.BackSurface.Value
};
data.parts[part_id] = PartData;
objects[part_id] = Part;
end;
-- Get any welds in the selection
local welds = {};
for object_id, Object in pairs( objects ) do
if Object:IsA( "BasePart" ) then
for _, Joint in pairs( _getAllDescendants( Services.Workspace ) ) do
if Joint:IsA( "Weld" ) and Joint.Name == "BTWeld" then
if Joint.Part0 == Object and #_findTableOccurrences( objects, Joint.Part1 ) > 0 then
table.insert( welds, Joint );
end;
end;
end;
end;
end;
-- Serialize any welds
if #welds > 0 then
data.welds = {};
for _, Weld in pairs( welds ) do
local weld_id = _generateSerializationID();
local WeldData = {
_findTableOccurrences( objects, Weld.Part0 )[1],
_findTableOccurrences( objects, Weld.Part1 )[1],
_splitNumberListString( tostring( Weld.C1 ) )
};
data.welds[weld_id] = WeldData;
objects[weld_id] = Weld;
end;
end;
-- Get any meshes in the selection
local meshes = {};
for _, Part in pairs( parts ) do
local Mesh = _getChildOfClass( Part, "SpecialMesh" );
if Mesh then
table.insert( meshes, Mesh );
end;
end;
-- Serialize any meshes
if #meshes > 0 then
data.meshes = {};
for _, Mesh in pairs( meshes ) do
local mesh_id = _generateSerializationID();
local MeshData = {
_findTableOccurrences( objects, Mesh.Parent )[1],
Mesh.MeshType.Value,
_splitNumberListString( tostring( Mesh.Scale ) ),
Mesh.MeshId,
Mesh.TextureId,
_splitNumberListString( tostring( Mesh.VertexColor ) )
};
data.meshes[mesh_id] = MeshData;
objects[mesh_id] = Mesh;
end;
end;
-- Get any textures in the selection
local textures = {};
for _, Part in pairs( parts ) do
local textures_found = _getChildrenOfClass( Part, "Texture" );
for _, Texture in pairs( textures_found ) do
table.insert( textures, Texture );
end;
local decals_found = _getChildrenOfClass( Part, "Decal" );
for _, Decal in pairs( decals_found ) do
table.insert( textures, Decal );
end;
end;
-- Serialize any textures
if #textures > 0 then
data.textures = {};
for _, Texture in pairs( textures ) do
local texture_type;
if Texture.ClassName == "Decal" then
texture_type = 1;
elseif Texture.ClassName == "Texture" then
texture_type = 2;
end;
local texture_id = _generateSerializationID();
local TextureData = {
_findTableOccurrences( objects, Texture.Parent )[1],
texture_type,
Texture.Face.Value,
Texture.Texture,
Texture.Transparency,
texture_type == 2 and Texture.StudsPerTileU or nil,
texture_type == 2 and Texture.StudsPerTileV or nil
};
data.textures[texture_id] = TextureData;
objects[texture_id] = Texture;
end;
end;
-- Get any lights in the selection
local lights = {};
for _, Part in pairs( parts ) do
local lights_found = _getChildrenOfClass( Part, "Light", true );
for _, Light in pairs( lights_found ) do
table.insert( lights, Light );
end;
end;
-- Serialize any lights
if #lights > 0 then
data.lights = {};
for _, Light in pairs( lights ) do
local light_type;
if Light:IsA( "PointLight" ) then
light_type = 1;
elseif Light:IsA( "SpotLight" ) then
light_type = 2;
end;
local light_id = _generateSerializationID();
local LightData = {
_findTableOccurrences( objects, Light.Parent )[1];
light_type,
_splitNumberListString( tostring( Light.Color ) ),
Light.Brightness,
Light.Range,
Light.Shadows,
light_type == 2 and Light.Angle or nil,
light_type == 2 and Light.Face.Value or nil
};
data.lights[light_id] = LightData;
objects[light_id] = Light;
end;
end;
-- Get any decorations in the selection
local decorations = {};
for _, Part in pairs( parts ) do
table.insert( decorations, _getChildOfClass( Part, 'Smoke' ) )
table.insert( decorations, _getChildOfClass( Part, 'Fire' ) );
table.insert( decorations, _getChildOfClass( Part, 'Sparkles' ) );
end;
-- Serialize any decorations
if #decorations > 0 then
data.decorations = {};
for _, Decoration in pairs( decorations ) do
local decoration_type;
if Decoration:IsA( 'Smoke' ) then
decoration_type = 1;
elseif Decoration:IsA( 'Fire' ) then
decoration_type = 2;
elseif Decoration:IsA( 'Sparkles' ) then
decoration_type = 3;
end;
local decoration_id = _generateSerializationID();
local DecorationData = {
_findTableOccurrences( objects, Decoration.Parent )[1],
decoration_type
};
if decoration_type == 1 then
DecorationData[3] = _splitNumberListString( tostring( Decoration.Color ) );
DecorationData[4] = Decoration.Opacity;
DecorationData[5] = Decoration.RiseVelocity;
DecorationData[6] = Decoration.Size;
elseif decoration_type == 2 then
DecorationData[3] = _splitNumberListString( tostring( Decoration.Color ) );
DecorationData[4] = _splitNumberListString( tostring( Decoration.SecondaryColor ) );
DecorationData[5] = Decoration.Heat;
DecorationData[6] = Decoration.Size;
elseif decoration_type == 3 then
DecorationData[3] = _splitNumberListString( tostring( Decoration.SparkleColor ) );
end;
data.decorations[decoration_id] = DecorationData;
objects[decoration_id] = Decoration;
end;
end;
return RbxUtility.EncodeJSON( data );
end;
function _getChildOfClass( Parent, class_name, inherit )
-- Returns the first child of `Parent` that is of class `class_name`
-- or nil if it couldn't find any
-- Look for a child of `Parent` of class `class_name` and return it
if not inherit then
for _, Child in pairs( Parent:GetChildren() ) do
if Child.ClassName == class_name then
return Child;
end;
end;
else
for _, Child in pairs( Parent:GetChildren() ) do
if Child:IsA( class_name ) then
return Child;
end;
end;
end;
return nil;
end;
function _getChildrenOfClass( Parent, class_name, inherit )
-- Returns a table containing the children of `Parent` that are
-- of class `class_name`
local matches = {};
if not inherit then
for _, Child in pairs( Parent:GetChildren() ) do
if Child.ClassName == class_name then
table.insert( matches, Child );
end;
end;
else
for _, Child in pairs( Parent:GetChildren() ) do
if Child:IsA( class_name ) then
table.insert( matches, Child );
end;
end;
end;
return matches;
end;
function _HSVToRGB( hue, saturation, value )
-- Returns the RGB equivalent of the given HSV-defined color
-- (adapted from some code found around the web)
-- If it's achromatic, just return the value
if saturation == 0 then
return value;
end;
-- Get the hue sector
local hue_sector = math.floor( hue / 60 );
local hue_sector_offset = ( hue / 60 ) - hue_sector;
local p = value * ( 1 - saturation );
local q = value * ( 1 - saturation * hue_sector_offset );
local t = value * ( 1 - saturation * ( 1 - hue_sector_offset ) );
if hue_sector == 0 then
return value, t, p;
elseif hue_sector == 1 then
return q, value, p;
elseif hue_sector == 2 then
return p, value, t;
elseif hue_sector == 3 then
return p, q, value;
elseif hue_sector == 4 then
return t, p, value;
elseif hue_sector == 5 then
return value, p, q;
end;
end;
function _RGBToHSV( red, green, blue )
-- Returns the HSV equivalent of the given RGB-defined color
-- (adapted from some code found around the web)
local hue, saturation, value;
local min_value = math.min( red, green, blue );
local max_value = math.max( red, green, blue );
value = max_value;
local value_delta = max_value - min_value;
-- If the color is not black
if max_value ~= 0 then
saturation = value_delta / max_value;
-- If the color is purely black
else
saturation = 0;
hue = -1;
return hue, saturation, value;
end;
if red == max_value then
hue = ( green - blue ) / value_delta;
elseif green == max_value then
hue = 2 + ( blue - red ) / value_delta;
else
hue = 4 + ( red - green ) / value_delta;
end;
hue = hue * 60;
if hue < 0 then
hue = hue + 360;
end;
return hue, saturation, value;
end;
function CreateSignal()
-- Returns a ROBLOX-like signal for connections (RbxUtility's is buggy)
local Signal = {
Connections = {};
Connect = function ( Signal, Handler )
table.insert( Signal.Connections, Handler );
local ConnectionController = {
Handler = Handler;
Disconnect = function ( Connection )
local ConnectionSearch = _findTableOccurrences( Signal.Connections, Connection.Handler );
if #ConnectionSearch > 0 then
local ConnectionIndex = ConnectionSearch[1];
table.remove( Signal.Connections, ConnectionIndex );
end;
end;
};
-- Add compatibility aliases
ConnectionController.disconnect = ConnectionController.Disconnect;
return ConnectionController;
end;
Fire = function ( Signal, ... )
for _, Connection in pairs( Signal.Connections ) do
Connection( ... );
end;
end;
};
-- Add compatibility aliases
Signal.connect = Signal.Connect;
Signal.fire = Signal.Fire;
return Signal;
end;
------------------------------------------
-- Prepare the UI
------------------------------------------
-- Wait for all parts of the base UI to fully replicate
if ToolType == 'tool' then
local UIComponentCount = (Tool:WaitForChild 'UIComponentCount').Value;
repeat wait( 0.1 ) until #_getAllDescendants( Tool.Interfaces ) >= UIComponentCount;
end;
------------------------------------------
-- Create data containers
------------------------------------------
ActiveKeys = {};
CurrentTool = nil;
function equipTool( NewTool )
-- If it's a different tool than the current one
if CurrentTool ~= NewTool then
-- Run (if existent) the old tool's `Unequipped` listener
if CurrentTool and CurrentTool.Listeners.Unequipped then
CurrentTool.Listeners.Unequipped();
end;
CurrentTool = NewTool;
-- Recolor the handle
if ToolType == 'tool' then
Tool.Handle.BrickColor = NewTool.Color;
end;
-- Highlight the right button on the dock
for _, Button in pairs( Dock.ToolButtons:GetChildren() ) do
Button.BackgroundTransparency = 1;
end;
local Button = Dock.ToolButtons:FindFirstChild( getToolName( NewTool ) .. "Button" );
if Button then
Button.BackgroundTransparency = 0;
end;
-- Run (if existent) the new tool's `Equipped` listener
if NewTool.Listeners.Equipped then
NewTool.Listeners.Equipped();
end;
end;
end;
function cloneSelection()
-- Clones the items in the selection
-- Make sure that there are items in the selection
if #Selection.Items > 0 then
local item_copies = {};
-- Make a copy of every item in the selection and add it to table `item_copies`
for _, Item in pairs( Selection.Items ) do
local ItemCopy = Item:Clone();
ItemCopy.Parent = Services.Workspace;
table.insert( item_copies, ItemCopy );
end;
-- Replace the selection with the copied items
Selection:clear();
for _, Item in pairs( item_copies ) do
Selection:add( Item );
end;
local HistoryRecord = {
copies = item_copies;
unapply = function ( self )
for _, Copy in pairs( self.copies ) do
if Copy then
Copy.Parent = nil;
end;
end;
end;
apply = function ( self )
Selection:clear();
for _, Copy in pairs( self.copies ) do
if Copy then
Copy.Parent = Services.Workspace;
Copy:MakeJoints();
Selection:add( Copy );
end;
end;
end;
};
History:add( HistoryRecord );
-- Play a confirmation sound
local Sound = RbxUtility.Create "Sound" {
Name = "BTActionCompletionSound";
Pitch = 1.5;
SoundId = Assets.ActionCompletionSound;
Volume = 1;
Parent = Player or Services.SoundService;
};
Sound:Play();
Sound:Destroy();
-- Highlight the outlines of the new parts
coroutine.wrap( function ()
for transparency = 1, 0.5, -0.1 do
for Item, SelectionBox in pairs( SelectionBoxes ) do
SelectionBox.Transparency = transparency;
end;
wait( 0.1 );
end;
end )();
end;
end;
function deleteSelection()
-- Deletes the items in the selection
if #Selection.Items == 0 then
return;
end;
local selection_items = _cloneTable( Selection.Items );
-- Create a history record
local HistoryRecord = {
targets = selection_items;
parents = {};
apply = function ( self )
for _, Target in pairs( self.targets ) do
if Target then
Target.Parent = nil;
end;
end;
end;
unapply = function ( self )
Selection:clear();
for _, Target in pairs( self.targets ) do
if Target then
Target.Parent = self.parents[Target];
Target:MakeJoints();
Selection:add( Target );
end;
end;
end;
};
for _, Item in pairs( selection_items ) do
HistoryRecord.parents[Item] = Item.Parent;
Item.Parent = nil;
end;
History:add( HistoryRecord );
end;
function prismSelect()
-- Selects all the parts within the area of the selected parts
-- Make sure parts to define the area are present
if #Selection.Items == 0 then
return;
end;
local parts = {};
-- Get all the parts in workspace
local workspace_parts = {};
local workspace_children = _getAllDescendants( Services.Workspace );
for _, Child in pairs( workspace_children ) do
if Child:IsA( 'BasePart' ) and not Selection:find( Child ) then
table.insert( workspace_parts, Child );
end;
end;
-- Go through each part and perform area tests on each one
local checks = {};
for _, Item in pairs( workspace_parts ) do
checks[Item] = 0;
for _, SelectionItem in pairs( Selection.Items ) do
-- Calculate the position of the item in question in relation to the area-defining parts
local offset = SelectionItem.CFrame:toObjectSpace( Item.CFrame );
local extents = SelectionItem.Size / 2;
-- Check the item off if it passed this test (if it's within the range of the extents)
if ( math.abs( offset.x ) <= extents.x ) and ( math.abs( offset.y ) <= extents.y ) and ( math.abs( offset.z ) <= extents.z ) then
checks[Item] = checks[Item] + 1;
end;
end;
end;
-- Delete the parts that were used to select the area
local selection_items = _cloneTable( Selection.Items );
local selection_item_parents = {};
for _, Item in pairs( selection_items ) do
selection_item_parents[Item] = Item.Parent;
Item.Parent = nil;
end;
-- Select the parts that passed any area checks
for _, Item in pairs( workspace_parts ) do
if checks[Item] > 0 then
Selection:add( Item );
end;
end;
-- Add a history record
History:add( {
selection_parts = selection_items;
selection_part_parents = selection_item_parents;
new_selection = _cloneTable( Selection.Items );
apply = function ( self )
Selection:clear();
for _, Item in pairs( self.selection_parts ) do
Item.Parent = nil;
end;
for _, Item in pairs( self.new_selection ) do
Selection:add( Item );
end;
end;
unapply = function ( self )
Selection:clear();
for _, Item in pairs( self.selection_parts ) do
Item.Parent = self.selection_part_parents[Item];
Selection:add( Item );
end;
end;
} );
end;
function toggleHelp()
-- Make sure the dock is ready
if not Dock then
return;
end;
-- Toggle the visibility of the help tooltip
Dock.HelpInfo.Visible = not Dock.HelpInfo.Visible;
end;
function getToolName( tool )
-- Returns the name of `tool` as registered in `Tools`
local name_search = _findTableOccurrences( Tools, tool );
if #name_search > 0 then
return name_search[1];
end;
end;
function isSelectable( Object )
-- Returns whether `Object` is selectable
if not Object or not Object.Parent or not Object:IsA( "BasePart" ) or Object.Locked or Selection:find( Object ) or Groups:IsPartIgnored( Object ) then
return false;
end;
-- If it passes all checks, return true
return true;
end;
function IsVersionOutdated()
-- Returns whether this version of Building Tools is out of date
-- Check the most recent version number
local AssetInfo = Services.MarketplaceService:GetProductInfo( ToolAssetID, Enum.InfoType.Asset );
local VersionID = AssetInfo.Description:match( '%[Version: (.+)%]' );
local CurrentVersionID = ( Tool:WaitForChild 'Version' ).Value;