-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButterflyUI.lua
1943 lines (1811 loc) · 88.4 KB
/
ButterflyUI.lua
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
--[[ Butterfly UI Made by @0xVB ]]--
--Version 2.0
--#region Declarations
--#region Services
local Players = game:GetService("Players");
local Debris = game:GetService("Debris");
local TweenService = game:GetService("TweenService");
local RunService = game:GetService("RunService");
local UserInputService = game:GetService("UserInputService");
local CoreGui = game:GetService("CoreGui");
--#endregion Services
local PlayerGui = Players.LocalPlayer.PlayerGui;
local CoreButterflySpace = {Constructors = {}, Meta = {}, MetaRef = {}, Functions = {}};
local ButterflySpace = {};
local MainUI = Instance.new("ScreenGui", PlayerGui);
MainUI.Name = "ButterflyUIMain";
pcall(function ()
MainUI.Parent = CoreGui;
end);
--#endregion Declarations
--#region Error System
--[[Error System Expalnation:
The error system is simply a bunch of functions that lead to common errors.
Some take certain parameters, others don't.
Regardless, the point of this system is to have consistency in errors.
It also helps reduce the number of constants, although mostly irrelevant, is still present.
]]--
local Read_Only_NDX = function () error("This Proxy is Read-Only."); end;
local SELFCALL_Expected = function () error("Unable to get meta. Make sure you used ':' instead of '.' to call this function."); end;
local Destroyed_Object = function() error("This object has been destroyed."); end;
local Invalid_Type = function (Parameter, Expected)
error("Invalid type for " .. Parameter .. ". (" .. Expected .. " Expected.)");
end;
--[[ TypeString Structure:
The TypeString starts with a byte (either 0 or 1).
1 -> The type is nullable .(Can be NULL.)
0 -> Cannot be NULL. Must be of the provided type.
The Nullable byte is then followed by the provided type, as a string from indices 2 to -1 (string extension.)
ButterflyUI has "BaseColor" & "BaseNumber" types added.
They are used to set certain attributes that are supposed to vary according to time or other factors.
Read the documentation for a more detailed expalnation.
Ex: ButterflySpace.ButterflyUI.Button.Properties.RippleColor -> Could be a Color3 or a ColorSequence.
Color3 -> The Ripple is the provided Color3 at all times.
ColorSequence -> The Ripple follows the ColorSequence from start to finish. This is useful for effortless blend effects.
IMPORTANT: ButterflyMath is responsible for parsing Color/NumberSequence(s) values.
]]--
local TCTable = {
BaseColor = {"Color3", "ColorSequence"},
BaseNumber = {"number", "NumberRange", "NumberSequence"}
};
local function TypeCheck(Value, Type)
if (not Type) then return true; end;
local Nullable = (Type:byte() == 1);
if (Nullable and (Value == nil)) then return true; end;
Type = Type:sub(2, -1);
if (Type == "PTInstance") then
return (typeof(Value) == "Instance" and Value:IsA("GuiBase")) or ButterflySpace.ButterflyUI.IsA(Value, "ButterflyInstance");
end;
if (TCTable[Type]) then
for _, TS in pairs(TCTable[Type]) do
if (typeof(Value) == TS) then return true; end;
end;
return false;
end;
if (typeof(Value) == Type) then return true; end;
if (ButterflySpace.ButterflyEnum.IsA(Value, Type)) then return true; end;
if (ButterflySpace.ButterflyDefaults.IsA(Value, Type)) then return true; end;
if (ButterflySpace.ButterflyEvents.IsA(Value, Type)) then return true; end;
if (ButterflySpace.ButterflyUI.IsA(Value, Type)) then return true; end;
return false;
end;
local function AssertTypeCheck(Value, Type, Attribute)
if (not TypeCheck(Value, Type)) then
Invalid_Type(Attribute or Type:sub(2, -1), Type:sub(2, -1));
end;
end;
--#endregion Error System
--#region Butterfly Math Library
--[[ Butterfly Math Library Expalnation:
A library that contains various math functions that do not exist in Vanilla Lua's math library.
Check the documentation for a more detailed expalnation on each function.
This is not an initiated (Proxy-handled) library.
It simply declares local functions for later use.
]]--
local function Map(Number, Start1, Stop1, Start2, Stop2)
return ((Number - Start1) / (Stop1 - Start1)) * (Stop2 - Start2) + Start2;
end;
local function GetLargeAxis(Main)
if (Main.AbsoluteSize.X >= Main.AbsoluteSize.Y) then
return "X";
end;
return "Y";
end;
local function Round(Number)
local FDiff = Number - math.floor(Number);
local CDiff = math.ceil(Number) - Number;
return ((FDiff > CDiff) and math.ceil(Number)) or math.floor(Number);
end;
local function MapColor(StartColor, EndColor, StartFrame, EndFrame, Frame)
return Color3.new(
Map(Frame, StartFrame, EndFrame, StartColor.r, EndColor.r),
Map(Frame, StartFrame, EndFrame, StartColor.g, EndColor.g),
Map(Frame, StartFrame, EndFrame, StartColor.b, EndColor.b)
);
end;
local function MapUDim2(Frame, StartFrame, EndFrame, StartUDim2, EndUDim2)
return UDim2.new(
Map(Frame, StartFrame, EndFrame, StartUDim2.X.Scale, EndUDim2.X.Scale),
Map(Frame, StartFrame, EndFrame, StartUDim2.X.Offset, EndUDim2.X.Offset),
Map(Frame, StartFrame, EndFrame, StartUDim2.Y.Scale, EndUDim2.Y.Scale),
Map(Frame, StartFrame, EndFrame, StartUDim2.Y.Offset, EndUDim2.Y.Offset)
);
end;
local function ParseNumber(Value, Interval)
if (type(Interval) ~= "number") then error("Expected number for argument #1."); end;
if (typeof(Value) == "number") then
return Value;
elseif (typeof(Value) == "NumberRange") then
local Min, Max = Value.Min, Value.Max;
return Map(Interval, 0, 1, Min, Max);
elseif (typeof(Value) == "NumberSequence") then
local MinTime, MaxTime, MinNumber, MaxNumber;
for i = 1, #Value.Keypoints do
local v = Value.Keypoints[i];
local Number, Time = v.Value, v.Time;
if (Time > Interval) then
MaxNumber = Number;
MaxTime = Time;
break;
elseif (Time < Interval) then
MinNumber = Number;
MinTime = Time;
elseif (Time == Interval) then
return Number;
end;
end;
local RelativeInterval = Map(Interval, MinTime, MaxTime, 0, 1);
return Map(RelativeInterval, 0, 1, MinNumber, MaxNumber);
end;
error("Expected BaseNumber for argument #2.");
end;
local function ParseColor(Value, Interval)
if (type(Interval) ~= "number") then error("Expected number for argument #1."); end;
if (typeof(Value) == "Color3") then
return Value;
elseif (typeof(Value) == "ColorSequence") then
local MinTime, MaxTime, MinColor, MaxColor;
for i = 1, #Value.Keypoints do
local v = Value.Keypoints[i];
local Number, Time = v.Value, v.Time;
if (Time > Interval) then
MaxColor = Number;
MaxTime = Time;
break;
elseif (Time < Interval) then
MinColor = Number;
MinTime = Time;
elseif (Time == Interval) then
return Number;
end;
end;
local RelativeInterval = Map(Interval, MinTime, MaxTime, 0, 1);
return Color3.new(
Map(RelativeInterval, 0, 1, MinColor.R, MaxColor.R),
Map(RelativeInterval, 0, 1, MinColor.G, MaxColor.G),
Map(RelativeInterval, 0, 1, MinColor.B, MaxColor.B)
);
end;
end;
local function TotalTweenTime(Info)
return Info.Time + Info.DelayTime;
end;
local function DirectionalMagnitude(V0, V1)
local Magnitude = V1 - V0;
local X1, Y1 = (Magnitude.X == 0 and 1) or 0, (Magnitude.Y == 0 and 1) or 0;
return (X1 == 0 and 1) or 0, X1, (Y1 == 0 and 1) or 0, Y1;
end;
--#endregion Butterfly Math Library
--#region Enumerator System
--[[
Handles special value types, "Enumerator".
They are used to declare values such as BackgroundType & so on.
Basically, values too complicated to be expressed as simple values, such as numbers & strings.
They do not exist in the Vanilla RLua EnumItems.
Users can create custom Butterfly Enumerators using the function, CoreLibrary.CreateEnum().
Read the documentation for a more detailed expalnation.
]]--
local function Init_ButterflyEnum()
if (ButterflySpace.ButterflyEnum) then return; end;--The library is already loaded.
local MetaRef = {};--Keys: ButterflyEnum Proxies | Values: the metatable of said Proxy.
--[[ ButterflyEnum Meta Structure:
[0] -> Name
[1] -> Parent (Could be nil.)
[2] -> Value
[3] -> ByteValue
[4] -> Children/SubEnums
]]
--#region Functions
local GetPath;
GetPath = function(self)
local Meta = MetaRef[self];
if (not Meta) then SELFCALL_Expected(); end;
if (not Meta["\1"]) then return Meta["\0"]; end;
return GetPath(Meta["\1"]) .. "." .. Meta["\0"];
end;
local function IsA(self, Type)
if (type(self) ~= "userdata") then return false; end;
local Meta = MetaRef[self];
if (not Meta) then return false; end;
local PType = Meta["\1"];
if (PType and MetaRef[PType]) then PType = MetaRef[PType]["\0"]; else PType = Meta["\0"] end;
if (not Meta) then SELFCALL_Expected(); end;
return (Type == "EnumItem" or Type == "ButterflyEnum") or (Type == Meta["\0"] or Type == PType) or (Type == "ButterflyProxy");
end;
local function GetValue(self)
local Meta = MetaRef[self];
if (not Meta) then SELFCALL_Expected(); end;
return Meta["\2"];
end;
local function ParseEnum(Value, ExpectedType)
if (MetaRef[Value]) then return Value; end;
local Meta = MetaRef[ExpectedType];
return Meta["\4"][Value];
end;
--#endregion Functions
--#region Meta
local Meta_ButterflyEnum;
Meta_ButterflyEnum = {
KeyTranslation = {Name = "\0", Path = "\0", Parent = "\1", Container = "\1", Type = "\1", Value = "\3", ByteValue = "\3",
GetPath = GetPath, GetValue = GetValue, IsA = IsA},
__tostring = function (self)
return GetPath(self);
end,
__index = function (self, Attribute)
local Meta = MetaRef[self];
local KeyTranslation = Meta_ButterflyEnum.KeyTranslation[Attribute]
if (type(KeyTranslation) == "string") then
return Meta[KeyTranslation];
elseif (type(KeyTranslation) == "function") then
return KeyTranslation;
elseif (Meta["\4"][Attribute]) then
return Meta["\4"][Attribute];
end;
error("Unable to find Attribute '" .. tostring(Attribute) .."'.");
end,
__newindex = Read_Only_NDX,
__metatable = "Konrushi!"
};
--#endregion Meta
--#region Construction
local function Constructor_ButterflyEnum(Name, Parent, Value, ByteValue)
local Proxy = newproxy(true);
local Meta = getmetatable(Proxy);
MetaRef[Proxy] = Meta;
--#region Attributes
Meta["\0"] = Name;
Meta["\1"] = Parent;
Meta["\2"] = Value;
Meta["\3"] = ByteValue;
Meta["\4"] = {};
--#endregion Attributes
--#region Meta Methods
Meta.__tostring = Meta_ButterflyEnum.__tostring;
Meta.__index = Meta_ButterflyEnum.__index;
Meta.__newindex = Meta_ButterflyEnum.__newindex;
Meta.__metatable = Meta_ButterflyEnum.__metatable;
--#endregion Meta Methods
if (Parent and MetaRef[Parent]) then
Meta = MetaRef[Parent];
if (Name) then Meta["\4"][Name] = Proxy; end;
if (ByteValue) then Meta["\4"][ByteValue] = Proxy; end;
end;
return Proxy;
end;
local ButterflyEnum = Constructor_ButterflyEnum("ButterflyEnum");
--#endregion Construction
CoreButterflySpace.MetaRef.ButterflyEnum = MetaRef;
CoreButterflySpace.Meta.ButterflyEnum = Meta_ButterflyEnum;
CoreButterflySpace.Functions.ParseEnum = ParseEnum;
CoreButterflySpace.Constructors.Enum = Constructor_ButterflyEnum;
ButterflySpace.ButterflyEnum = ButterflyEnum;
return ButterflyEnum;
end;
--#endregion Enumerator System
--#region Default System
--[[ Default System Expalnation:
A system used to store values for certain attributes.
Essentially, it is used to define the default appearance of newly-created ButterflyUI Objects.
It was also created with the intent of making different themes an easy job with ButterflyUI, &
to reduce the number of modifications needed to customize an Object.
Changing the settings in this library BEFORE creating ButterflyUI Objects could save a lot of time.
Read the documentation for a more detailed expalnation.
]]--
local function Init_ButterflyDefaults()
if (not ButterflySpace.ButterflyEnum) then error("ButterflyEnum is required to initiate ButterflyDefaults."); end;
if (ButterflySpace.ButterflyDefaults) then return; end;--The library is already loaded.
local MetaRef = {};--Keys: ButterflyDefault Proxies | Values: the metatable of said Proxy.
--[[ ButterflyDefault Meta Structure:
[0] -> Name
[1] -> Parent (Could be nil.)
[2] -> Value
[3] -> TypeLock
[4] -> Children/SubDefaults
]]
--#region Functions
local function IsA(self, Type)
if (type(self) ~= "userdata") then return false; end;
local Meta = MetaRef[self];
if (not Meta) then return false; end;
local PType = Meta["\1"];
if (PType and MetaRef[PType]) then PType = MetaRef[PType]["\0"]; else PType = Meta["\0"] end;
if (not Meta) then SELFCALL_Expected(); end;
return (Type == "ButterflyDefault") or (Type == Meta["\0"] or Type == PType) or (Type == "ButterflyProxy");
end;
local function GetValue(self)
local Meta = MetaRef[self];
if (not Meta) then SELFCALL_Expected(); end;
return Meta["\2"];
end;
--#endregion Functions
--#region Meta
local Meta_ButterflyDefaults;
Meta_ButterflyDefaults = {
KeyTranslation = {Name = "\0", Parent = "\1", Type = "\1", Value = "\2",
GetValue = GetValue, IsA = IsA},
__tostring = function (self)
return MetaRef[self]["\0"];
end,
__index = function (self, Attribute)
local Meta = MetaRef[self];
local KeyTranslation = Meta_ButterflyDefaults.KeyTranslation[Attribute]
if (type(KeyTranslation) == "string") then
return Meta[KeyTranslation];
elseif (type(KeyTranslation) == "function") then
return KeyTranslation;
elseif (Meta["\4"][Attribute]) then
return Meta["\4"][Attribute];
end;
error("Unable to find Attribute '" .. tostring(Attribute) .."'.");
end,
__newindex = function (self, Attribute, Value)
if (Attribute ~= "Value") then error("Only 'Value' Attribute can be modified in ButterflyDefaults."); end;
local Meta = MetaRef[self];
if (not Meta["\3"]) then error("Unable to change Value in a DefaultLibrary."); end;
AssertTypeCheck(Value, Meta["\3"], "Value");
Meta["\2"] = Value;
end,
__metatable = "Konrushi!"
};
--#endregion Meta
--#region Construction
local function Constructor_ButterflyDefault(Name, Parent, Value, TypeLock)
local Proxy = newproxy(true);
local Meta = getmetatable(Proxy);
MetaRef[Proxy] = Meta;
--#region Attributes
Meta["\0"] = Name;
Meta["\1"] = Parent;
Meta["\2"] = Value;
Meta["\3"] = TypeLock;
Meta["\4"] = {};
--#endregion Attributes
--#region Meta Methods
Meta.__tostring = Meta_ButterflyDefaults.__tostring;
Meta.__index = Meta_ButterflyDefaults.__index;
Meta.__newindex = Meta_ButterflyDefaults.__newindex;
Meta.__metatable = Meta_ButterflyDefaults.__metatable;
--#endregion Meta Methods
if (Parent and MetaRef[Parent]) then
Meta = MetaRef[Parent];
if (Name) then Meta["\4"][Name] = Proxy; end;
end;
return Proxy;
end;
local ButterflyDefaults = Constructor_ButterflyDefault("ButterflyDefaults");
--#endregion Construction
CoreButterflySpace.MetaRef.ButterflyDefaults = MetaRef;
CoreButterflySpace.Meta.ButterflyDefaults = Meta_ButterflyDefaults;
CoreButterflySpace.Constructors.Defaults = Constructor_ButterflyDefault;
ButterflySpace.ButterflyDefaults = ButterflyDefaults;
return ButterflyDefaults;
end;
--#endregion Default System
--#region Butterfly Events
--[[ Butterfly Events Expalnation:
The reason ButterflyUI has its own Event System is due to Vanilla RLua's
Instance, "BindableEvent" lacking the ability to pass custom userdata
when firing the event.
This led to the creation of this library.
It is a wrapped version of RLua's BindableEvent.
]]--
local function Init_ButterflyEvents()
if (ButterflySpace.ButterflyEvents) then return; end;--The library is already loaded.
local MetaRef = {};--Keys: ButterflyEvents Proxies | Values: the metatable of said Proxy.
--[[ ButterflyEvents Meta Structure:
[0] -> Name
[1] -> Container (Could be nil.)
[2] -> Instance (or Function)
[3] -> Connections (Only in Events)
[4] -> Parameters (Only in Events)
]]
local function IsA(self, Type)
if (type(self) ~= "userdata") then return false; end;
local Meta = MetaRef[self];
if (not Meta) then return false; end;
return (Type == Meta["\0"]) or (Type == "ButterflyEvent" and not Meta["\0"] == "ButterflyConnection") or (Type == "ButterflyProxy");
end;
local function Destructor_ButterflyEvent(Proxy)
if (not Proxy) then SELFCALL_Expected(); end;
local Meta = MetaRef[Proxy];
if (not Meta) then return; end;
if (Meta["\0"] == "ButterflyConnection" and Meta["\1"]) then
for _, Connection in pairs(MetaRef[Meta["\1"]]["\3"])do
if (Connection == Proxy) then
table.remove(MetaRef[Meta["\1"]]["\3"], _);
end;
end;
end;
MetaRef[Proxy] = nil;
Meta["\0"], Meta["\1"], Meta["\2"], Meta["\4"] = nil, nil, nil, nil;
if (type(Meta["\3"]) ~= "table") then Meta["\3"] = nil; return; end;
for _, Connection in pairs(Meta["\3"]) do
Connection:Destroy();
Meta["\3"][_] = nil;
end;
end;
local function GetConnections(self)
if (not self) then SELFCALL_Expected(); end;
local Meta = MetaRef[self];
if (not Meta) then error("DestroyedObject"); end;
if (type(Meta["\3"]) ~= "table") then error("Unable to get Connections."); end;
local Connections = {};
for _, Connection in pairs(Meta["\3"])do
Connections[_] = Connection;
end;
return Connections;
end
local function Fire(self, ...)
if (not self) then SELFCALL_Expected(); end;
local Meta = MetaRef[self];
if (not Meta) then error("DestroyedObject"); end;
if (type(Meta["\2"]) == "function") then
return Meta["\2"](...);
elseif (typeof(Meta["\2"]) == "Instance") then
Meta["\4"] = {...};
Meta["\2"]:Fire();
for _, Connection in pairs(Meta["\3"])do
Connection:Fire(...);
end;
end;
end;
local function Await(self)
if (not self) then SELFCALL_Expected(); end;
local Meta = MetaRef[self];
if (not Meta) then error("DestroyedObject"); end;
if (Meta["\0"] == "ButterflyConnection") then error("Unable to Await ButterflyConnection."); end;
Meta["\2"].Event:Wait();
return table.unpack(Meta["\4"]);
end;
local Meta_ButterflyEvents;
Meta_ButterflyEvents = {
KeyTranslation = {Name = "\0", Container = "\1", Function = "\1",
IsA = IsA, Await = Await, Fire = Fire, Wait = Await, Disconnect = Destructor_ButterflyEvent, GetConnections = GetConnections, Destroy = Destructor_ButterflyEvent, Remove = Destructor_ButterflyEvent},
__tostring = function (self)
return (MetaRef[self]) and MetaRef[self]["\0"] or "<DestroyedObject>";
end,
__index = function (self, Attribute)
local Meta = MetaRef[self];
if (not Meta) then return; end;
local KeyTranslation = Meta_ButterflyEvents.KeyTranslation[Attribute];
if (type(KeyTranslation) == "string") then
return Meta[KeyTranslation];
elseif (type(KeyTranslation) == "function") then
return KeyTranslation;
end;
error("Unable to find Attribute '" .. tostring(Attribute) .."'.");
end,
__newindex = Read_Only_NDX,
__metatable = "Konrushi!"
};
local function Constructor_ButterflyEvent(Name, Parent)
local Proxy = newproxy(true);
local Meta = getmetatable(Proxy);
local Event = Instance.new("BindableEvent");
MetaRef[Proxy] = Meta;
--#region Attributes
Meta["\0"] = Name;
Meta["\1"] = Parent;
Meta["\2"] = Event;
Meta["\3"] = {};
--#endregion Attributes
--#region Meta Methods
Meta.__tostring = Meta_ButterflyEvents.__tostring;
Meta.__index = Meta_ButterflyEvents.__index;
Meta.__newindex = Meta_ButterflyEvents.__newindex;
Meta.__metatable = Meta_ButterflyEvents.__metatable;
--#endregion Meta Methods
return Proxy;
end;
local function Constructor_ButterflyConnection(Event, Function)
local Proxy = newproxy(true);
local Meta = getmetatable(Proxy);
MetaRef[Proxy] = Meta;
--#region Attributes
Meta["\0"] = "ButterflyConnection";
Meta["\1"] = Event;
Meta["\2"] = Function;
--#endregion Attributes
--#region Meta Methods
Meta.__tostring = Meta_ButterflyEvents.__tostring;
Meta.__index = Meta_ButterflyEvents.__index;
Meta.__newindex = Meta_ButterflyEvents.__newindex;
Meta.__metatable = Meta_ButterflyEvents.__metatable;
--#endregion Meta Methods
if (Event and MetaRef[Event]) then table.insert(MetaRef[Event]["\3"], Proxy); end;
return Proxy;
end;
Meta_ButterflyEvents.KeyTranslation.Connect = Constructor_ButterflyConnection;
CoreButterflySpace.MetaRef.ButterflyEvent = MetaRef;
CoreButterflySpace.Meta.ButterflyEvent = Meta_ButterflyEvents;
CoreButterflySpace.Constructors.Event = Constructor_ButterflyEvent;
CoreButterflySpace.Constructors.Connection = Constructor_ButterflyConnection;
ButterflySpace.ButterflyEvents = {
IsA = IsA,
GetConnections = GetConnections,
Fire = Fire,
Await = Await,
Create = Constructor_ButterflyEvent
};
return ButterflySpace.ButterflyEvents;
end;
--#endregion Butterfly Events
--#region ButterflyUI
local function Init_ButterflyUI()
if (not ButterflySpace.ButterflyEvents) then error("ButterflyEvents is required to initiate ButterflyUI."); end;
if (not ButterflySpace.ButterflyEnum) then error("ButterflyEnum is required to initiate ButterflyUI."); end;
if (not ButterflySpace.ButterflyDefaults) then error("ButterflyDefaults is required to initiate ButterflyUI."); end;
if (ButterflySpace.ButterflyUI) then return; end;--The library is already loaded.
local MetaRef = {};
--[[ ButterflyUI Meta Structure:
Class:
{
[0] -> Name
[1] -> ClassType
[2] -> Attributes.Properties
[3] -> Attributes.Methods
[4] -> Attributes.Events
[5] -> Constructor
[9] -> Type
[10] -> Proxy
}
Instance:
{
[0] -> Name
[1] -> Class
[2] -> Properties
[3] -> Components (0: Main | 1: Parent)
[4] -> Connections
[9] -> Type
[10] -> Proxy
}
Attribute:
{
[0] -> Name (PME)
[1] -> AttributeType (PME)
[2] -> Value (Property -> DefaultValue | Method -> Function | Event -> nil) (PM)
[3] -> Get (P)
[4] -> Set (Could be nil, which translates to this Attribute being Read-Only.) (P)
[5] -> TypeLock (Could be nil, which disabled calling AssertTypeCheck before calling Set.) (P)
[9] -> Type (PME)
[10] -> Proxy (PME)
PME -> Exists in Properties, Methods & Events.
PM -> Exists in Properties & Methods only.
P -> Exists in Properties only.
}
]]
--#region Enum Creation
local PTClass, PTInstance, PTAttribute;
local CTBase, CTCore, CTStandard;
local ATProperty, ATMethod, ATEvent;
--Wrapped inside a do end in order to save local variables from clumping up in autocomplete suggestions.
do
local Create = CoreButterflySpace.Constructors.Enum;
local ButterflyEnum = ButterflySpace.ButterflyEnum;
--#region Libraries
local BackgroundType = Create("BackgroundType", ButterflyEnum);
local ProxyType = Create("ProxyType", ButterflyEnum);
local ClassType = Create("ClassType", ButterflyEnum);
local AttributeType = Create("AttributeType", ButterflyEnum);
local BarType = Create("BarType", ButterflyEnum);
local SizeType = Create("SizeType", ButterflyEnum);
--#endregion Libraries
--#region BackgroundType
Create("Sharp", BackgroundType, {3457842171, 400, 0}, 0);
Create("Round", BackgroundType, {3457843087, 400, 0}, 1);
Create("Tilted", BackgroundType, {3457843868, 400, 0}, 2);
Create("Shadow", BackgroundType, {4793740839, 400, 0}, 3);
--#endregion BackgroundType
--#region BarType
Create("Rectangle", BarType, {3457842171, Rect.new(400, 400, 400, 400)}, 0);
Create("Capsule", BarType, {3457843087, Rect.new(400, 400, 400, 400)}, 1);
Create("Tilted", BarType, {3457843868, Rect.new(400, 400, 400, 400)}, 2);
Create("Shadow", BarType, {4793740839, Rect.new(400, 400, 400, 400)}, 3);
--#endregion BarType
--#region SizeType
Create("UDim2", SizeType, function (Main, Value)
AssertTypeCheck(Value, "\0UDim2", "Size");
Main.Size = Value;
end, 0);
Create("Vector2", SizeType, function (Main, Value, SizeScaling)
AssertTypeCheck(Value, "\0Vector2", "Size");
Main.Size = UDim2.new(
SizeScaling.X.Scale,
SizeScaling.X.Offset * Value.X,
SizeScaling.Y.Scale,
SizeScaling.Y.Offset * Value.Y
);
end, 1);
Create("Scale", SizeType, function (Main, Value, SizeScaling)
AssertTypeCheck(Value, "\0number", "Size");
Main.Size = UDim2.new(
SizeScaling.X.Scale * Value,
SizeScaling.X.Offset,
SizeScaling.Y.Scale * Value,
SizeScaling.Y.Offset
);
end, 2);
Create("Offset", SizeType, function (Main, Value, SizeScaling)
AssertTypeCheck(Value, "\0number", "Size");
Main.Size = UDim2.new(
SizeScaling.X.Scale,
SizeScaling.X.Offset * Value,
SizeScaling.Y.Scale,
SizeScaling.Y.Offset * Value
);
end, 2);
Create("BaseNumber", SizeType, function (Main, Value, SizeScaling, Interval)
AssertTypeCheck(Value, "\0BaseNumber", "Size");
Interval = Interval or 0;
Interval = ParseNumber(Value, Interval)
Main.Size = UDim2.new(
SizeScaling.X.Scale,
SizeScaling.X.Offset * Interval,
SizeScaling.Y.Scale,
SizeScaling.Y.Offset * Interval
);
end, 3);
--#endregion SizeType
--#region ProxyType
PTClass = Create("Class", ProxyType, "ButterflyClass", 0);
PTInstance = Create("Instance", ProxyType, "ButterflyInstance", 1);
PTAttribute = Create("Attribute", ProxyType, "ButterflyAttribute", 2);
--#endregion ProxyType
--#region ProxyType
CTBase = Create("Base", ClassType, "BaseClass", 0);
CTCore = Create("Core", ClassType, "CoreClass", 1);
CTStandard = Create("Standard", ClassType, "StandardClass", 2);
--#endregion ProxyType
--#region AttributeType
ATProperty = Create("Property", AttributeType, "\2", 0);
ATMethod = Create("Method", AttributeType, "\3", 1);
ATEvent = Create("Event", AttributeType, "\4", 2);
--#endregion AttributeType
end;
--#endregion Enum Creation
--#region Default Creation
local BackgroundDefaults, TextDefaults, TextBoxDefaults, GeneralDefaults;
--Wrapped inside a do end in order to save local variables from clumping up in autocomplete suggestions.
do
local Create = CoreButterflySpace.Constructors.Defaults;
local ButterflyDefaults = ButterflySpace.ButterflyDefaults;
--#region Libraries
local Background = Create("Background", ButterflyDefaults);
local Text = Create("Text", ButterflyDefaults);
local TextBox = Create("TextBox", ButterflyDefaults);
local General = Create("General", ButterflyDefaults);
BackgroundDefaults, TextDefaults, TextBoxDefaults, GeneralDefaults = Background, Text, TextBox, General;
--#endregion Libraries
--#region Background
Create("Color", Background, Color3.fromRGB(19, 22, 22), "\0Color3");
Create("Transparency", Background, 0, "\0number");
Create("BackgroundType", Background, ButterflySpace.ButterflyEnum.BackgroundType.Round, "\0BackgroundType");
Create("CornerSize", Background, 8, "\0number");
--#endregion Background
--#region Text
Create("Color", Text, Color3.fromRGB(229, 249, 255), "\0Color3");
Create("Transparency", Text, 0, "\0number");
Create("StrokeColor", Text, Color3.new(), "\0Color3");
Create("StrokeTransparency", Text, 1, "\0number");
--#endregion Text
--#region TextBox
Create("BackColor", TextBox, Color3.fromRGB(38, 44, 43), "\0Color3");
Create("TextColor", TextBox, Color3.fromRGB(229, 249, 255), "\0Color3");
Create("TextTransparency", TextBox, 0, "\0number");
Create("BackTransparency", TextBox, 0, "\0number");
Create("StrokeColor", TextBox, Color3.new(), "\0Color3");
Create("StrokeTransparency", TextBox, 1, "\0number");
Create("PlaceholderColor", TextBox, Color3.fromRGB(76, 87, 87), "\0Color3");
Create("Placeholder", TextBox, "Konrushi!", "\0string");
--#endregion TextBox
--#region General
Create("PrimaryColor", General, Color3.fromRGB(154, 150, 166), "\0Color3");
Create("SecondaryColor", General, Color3.fromRGB(28, 33, 33), "\0Color3");
Create("IndentColor", General, Color3.fromRGB(47, 55, 54), "\0Color3");
Create("InactiveColor", General, Color3.fromRGB(76, 87, 87), "\0Color3");
Create("InactiveBackColor", General, Color3.fromRGB(19, 22, 22), "\0Color3");
Create("HighlightColor", General, Color3.fromRGB(26, 158, 153), "\0Color3");
Create("HighlightBackColor", General, Color3.fromRGB(23, 60, 59), "\0Color3");
--#endregion General
end;
--#endregion Default Creation
--#region Functions
local GetEnumValue = ButterflySpace.ButterflyEnum.GetValue;
local function IsA(self, Type)
if (not self) then return false; end;
if (not MetaRef[self]) then return false; end;
return (MetaRef[self]["\9"]:GetValue() == Type) or (Type == "ButterflyProxy") or (Type == "ButterflyInstance");
end;
local function DefaultGet(Meta, Attribute)
return Meta["\2"][Attribute];
end;
local function DefaultSet(Meta, Attribute, Value)
Meta["\2"][Attribute] = Value;
end;
local function AttributeExists(Proxy, Attribute)
if (not Proxy) then return; end;
local Meta = MetaRef[Proxy];
if (not Meta) then return; end;
return (Meta["\2"][Attribute] ~= nil);
end;
--#endregion Functions
--[[
Get:
Function -> Calls with (self, Attribute)
string -> Components[string:sub(1, 1)][string:sub(2, -1)]
Get
nil -> Read-Only
Function -> Calls with (self, Attribute, Value) | Asserts TypeCheck if TypeLock exists.
string -> Components[string:sub(1, 1)][string:sub(2, -1)] = Value
]]--
--#region Meta
local Meta_ButterflyUI;
Meta_ButterflyUI = {
__tostring = function (self)
return (MetaRef[self]) and MetaRef[self]["\0"] or "<DestroyedObject>";
end,
__index = function (self, Attribute)-- Methods >> Events >> Properties
local Meta = MetaRef[self];
if (not Meta) then return; end;
local Class = MetaRef[Meta["\1"]];
if (Class["\3"][Attribute]) then
return MetaRef[Class["\3"][Attribute]]["\2"];
elseif (Class["\4"][Attribute]) then
return Meta["\2"][Attribute];
elseif (Class["\2"][Attribute]) then
Class = MetaRef[Class["\2"][Attribute]]["\3"];
if (type(Class) == "function") then
return Class(Meta, Attribute);
elseif (type(Class) == "string") then
return Meta["\3"][Class:sub(1, 1)][Class:sub(2, -1)];
end;
end;
error("Unable to get Attribute " .. tostring(Attribute) .. ".");
end,
__newindex = function (self, Attribute, Value)
local Meta = MetaRef[self];
if (not Meta) then return; end;
local Class = MetaRef[Meta["\1"]];
if (Class["\2"][Attribute]) then
Class = MetaRef[Class["\2"][Attribute]];
local Set = Class["\4"];
if (not Set) then error(tostring(Attribute) .. " is Read-Only."); end;
local TypeLock = Class["\5"];
if (TypeLock) then AssertTypeCheck(Value, TypeLock, tostring(Attribute)); end;
if (type(Set) == "function") then
Set(Meta, Attribute, Value);
if (Meta["\2"].AttributeSet) then
Meta["\2"].AttributeSet:Fire(Attribute);
end;
return;
elseif (type(Set) == "string") then
Meta["\3"][Set:sub(1, 1)][Set:sub(2, -1)] = Value;
return;
end;
end;
error("Unable to set Attribute " .. tostring(Attribute) .. ".");
end,
__metatable = "Konrushi!"
};
--#endregion Meta
--#region Constructors
local function Constructor_ButterflyProxy(Name, Type, SetMeta)
--#region Declarations
local Proxy = newproxy(true);
local Meta = getmetatable(Proxy);
local Pointer = tostring(Proxy);
MetaRef[Proxy] = Meta;
Meta["\0"] = Name;
Meta["\9"] = Type;
Meta["\10"] = Proxy;
--#endregion Declarations
--#region Meta Methods
Meta.__tostring = Meta_ButterflyUI.__tostring;
if (not SetMeta) then return Proxy, Meta, Pointer; end;
Meta.__index = Meta_ButterflyUI.__index;
Meta.__newindex = Meta_ButterflyUI.__newindex;
Meta.__metatable = Meta_ButterflyUI.__metatable;
--#endregion Meta Methods
return Proxy, Meta, Pointer;
end;
local Classes = {};
local function Constructor_ButterflyClass(Name, ClassType, Constructor, ...)
local Proxy, Meta, Pointer = Constructor_ButterflyProxy(Name, PTClass);
local Inherits = {...};
--#region Meta
Meta["\1"] = ClassType;
Meta["\2"] = {};
Meta["\3"] = {};
Meta["\4"] = {};
Meta["\5"] = Constructor;
Classes[Name] = Proxy;
--#endregion Meta
--#region Inheritance
for _, Class in pairs(Inherits) do
Class = MetaRef[Class];
for _, Property in pairs(Class["\2"]) do
Meta["\2"][_] = Property;
end;
for _, Method in pairs(Class["\3"]) do
Meta["\3"][_] = Method;
end;
for _, Event in pairs(Class["\4"]) do
Meta["\4"][_] = Event;
end;
end;
--#endregion Inheritance
return Proxy, Meta, Pointer;
end;
local function Construct_ButterflyAttribute(Name, Class, AttributeType, ...)
local Proxy, Meta, Pointer = Constructor_ButterflyProxy(Name, PTAttribute);
Class = MetaRef[Class];
local AttributeParams = {...};
--#region Meta
if (AttributeType == ATMethod) then
Meta["\2"] = AttributeParams[1];
Class["\3"][Name] = Proxy;--Function
elseif (AttributeType == ATProperty) then
Meta["\2"] = AttributeParams[1];--Value
Meta["\3"] = AttributeParams[2];--Get
Meta["\4"] = AttributeParams[3];--Set
Meta["\5"] = AttributeParams[4];--TypeLock
Class["\2"][Name] = Proxy;
end;
Meta["\0"] = Name;
Meta["\1"] = AttributeType;
Class[GetEnumValue(AttributeType)][Name] = Proxy;
--#endregion Meta
return Proxy, Meta, Pointer;
end;
local function Constructor_ButterflyInstance(Class)
Class = MetaRef[Class];
local Proxy, Meta, Pointer = Constructor_ButterflyProxy(Class["\0"], PTInstance, true);
--#region Meta
Meta["\1"] = Class["\10"];
Meta["\2"] = {};
Meta["\3"] = {};
Meta["\4"] = {};
--#endregion Meta
--#region Properties
for _, Property in pairs(Class["\2"]) do
Property = MetaRef[Property];
if (ButterflySpace.ButterflyDefaults.IsA(Property["\2"], "ButterflyDefault")) then
Meta["\2"][_] = Property["\2"].Value;
else
Meta["\2"][_] = Property["\2"];
end;
end;
--#endregion Properties
--#region Events
for _, Event in pairs(Class["\4"]) do
Event = MetaRef[Event];
Meta["\2"][_] = CoreButterflySpace.Constructors.Event(Event["\0"], Proxy);
end;
--#endregion Events
return Proxy, Meta, Pointer;
end;
local function RemoveAttribute(Class, Name, AttributeType)
MetaRef[Class][GetEnumValue(AttributeType)][Name] = nil;
end;
local function ConstructClass(Class, ...)
Class = MetaRef[Class];
if (Class["\1"] == CTBase) then error(Class["\0"] .. " is a BaseClass (Used for inheritance, has no Constructors.)"); end;
if (type(Class["\5"]) ~= "function") then error("This class is corrupted or incorrectly created."); end;
return Class["\5"](...);
end;
local function CreateInstance(Class, ...)
Class = Classes[Class];
if (not Class) then error("Invalid class."); end;
Class = MetaRef[Class];
if (not Class) then error("Unable to create class."); end;
if (Class["\1"] == CTCore) then error("This class is a CoreClass (Can only be created internally through ConstructClass.)"); end;
return ConstructClass(Class["\10"], ...);
end;
local function Deconstructor_ButterflyUI(Proxy)
local Meta = MetaRef[Proxy];
if (not Meta) then return false; end;
--#region Meta Methods
Meta.__index = Destroyed_Object;
Meta.__newindex = Destroyed_Object;
--#endregion Meta Methods
--#region Meta
local Class = MetaRef[Meta["\1"]];
Meta["\0"] = nil;
Meta["\1"] = nil;
Meta["\9"] = nil;
Meta["\10"] = nil;
for _, Component in pairs(Meta["\3"]) do pcall(function ()
Component:Destroy();
Meta["\3"][_] = nil;
end) end;
Meta["\3"] = nil
for _, Connection in pairs(Meta["\4"]) do pcall(function ()
Connection:Disconnect();
Meta["\4"][_] = nil;
end) end;
Meta["\4"] = nil;
for _, Event in pairs(Class["\4"]) do pcall(function ()
Event = MetaRef[Event]["\0"];