Repository navigation
Expand file tree
/
Copy pathTouchControls.lua
More file actions
1680 lines (1442 loc) · 59.8 KB
/
Copy pathTouchControls.lua
File metadata and controls
1680 lines (1442 loc) · 59.8 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
-- This is responsible for all touch controls we show (as of this writing, only on iOS/Android)
-- this includes character move thumbsticks, and buttons for jump, use of items, camera, etc.
-- Written by Ben Tkacheff/Jason Roth, Copyright Roblox 2013
-- fast flag for new controls
local touchFlagExists, touchFlagValue = pcall(function () return settings():GetFFlag("NewTouchControlScript") end)
local hasNewTouchControls = touchFlagExists and touchFlagValue
if hasNewTouchControls then -- NEW TOUCH CONTROLS WITH THUMBPAD
local UserInputService = game:GetService('UserInputService')
local GuiService = game:GetService('GuiService')
local Players = game:GetService('Players')
local success = pcall(function() UserInputService:IsLuaTouchControls() end)
if not success then
script:Destroy()
end
--[[ Variables ]]--
local CoreGui = game:WaitForChild('CoreGui')
local RobloxGui = CoreGui:WaitForChild('RobloxGui')
local ScreenResolution = GuiService:GetScreenResolution()
local function isSmallScreenDevice()
return ScreenResolution.y <= 500
end
local GameSettings = UserSettings().GameSettings
local Player = nil
while not Players.LocalPlayer do wait() end
Player = Players.LocalPlayer
--[[ Constants ]]--
local CAMERA_ZOOM_SENSITIVITY = 0.03
local CAMERA_ROTATE_SENSITIVITY = 0.007
local JUMP_BUTTON_SIZE = isSmallScreenDevice() and 70 or 90
local THUMBSTICK_SIZE = isSmallScreenDevice() and 70 or 120
--[[ Images ]]--
local TOUCH_CONTROL_SHEET = "rbxasset://textures/ui/TouchControlsSheet.png"
local DPAD_SHEET = "rbxasset://textures/ui/DPadSheet.png"
----------------------------------------------------------------------------
----------------------------------------------------------------------------
--
-- Helper Functions
--
-- used to create arrows for both dpad and new controls
local function createArrowLabel(name, parent, pos, size, spOffset, spSize)
local arrow = Instance.new('ImageLabel')
arrow.Name = name
arrow.Image = DPAD_SHEET
arrow.ImageRectOffset = spOffset
arrow.ImageRectSize = spSize
arrow.BackgroundTransparency = 1
arrow.Size = size
arrow.Position = pos
arrow.Parent = parent
return arrow
end
local function createControlFrame(name, position, size)
local frame = Instance.new('Frame')
frame.Name = name
frame.Active = true
frame.Size = UDim2.new(0, size, 0, size)
frame.Position = position
frame.BackgroundTransparency = 1
return frame
end
local function createOuterImage(name, position, parent)
local image = Instance.new('ImageLabel')
image.Name = name
image.Image = TOUCH_CONTROL_SHEET
image.ImageRectOffset = Vector2.new(0, 0)
image.ImageRectSize = Vector2.new(220, 220)
image.BackgroundTransparency = 1
image.Size = UDim2.new(0, THUMBSTICK_SIZE, 0, THUMBSTICK_SIZE)
image.Position = position
image.Parent = parent
return image
end
----------------------------------------------------------------------------
----------------------------------------------------------------------------
--
-- Jump Controls
--
local JumpButton = nil
local JumpTouchObject = nil
local function setupJumpButton(parentFrame)
if not JumpButton then
JumpButton = Instance.new('ImageButton')
JumpButton.Name = "JumpButton"
JumpButton.BackgroundTransparency = 1
JumpButton.Image = TOUCH_CONTROL_SHEET
JumpButton.ImageRectOffset = Vector2.new(176, 222)
JumpButton.ImageRectSize = Vector2.new(174, 174)
JumpButton.Size = UDim2.new(0, JUMP_BUTTON_SIZE, 0, JUMP_BUTTON_SIZE)
if isSmallScreenDevice() then
JumpButton.Position = UDim2.new(1, JUMP_BUTTON_SIZE * -2.25, 1, -JUMP_BUTTON_SIZE - 20)
else
JumpButton.Position = UDim2.new(1, JUMP_BUTTON_SIZE * -2.75, 1, -JUMP_BUTTON_SIZE - 120)
end
local playerJump_func = Player.JumpCharacter
local doJumpLoop = function()
while JumpTouchObject do
if Player then
playerJump_func(Player)
end
wait(1/60)
end
end
JumpButton.InputBegan:connect(function(inputObject)
if inputObject.UserInputType ~= Enum.UserInputType.Touch or JumpTouchObject then
return
end
JumpTouchObject = inputObject
JumpButton.ImageRectOffset = Vector2.new(0, 222)
doJumpLoop()
end)
JumpButton.InputEnded:connect(function(inputObject)
if inputObject.UserInputType ~= Enum.UserInputType.Touch then return end
if inputObject == JumpTouchObject then
JumpTouchObject = nil
JumpButton.ImageRectOffset = Vector2.new(176, 222)
end
end)
JumpButton.Parent = parentFrame
JumpButton.Visible = not UserInputService.ModalEnabled
end
end
----------------------------------------------------------------------------
----------------------------------------------------------------------------
--
-- Thumbstick Controls
--
local ThumbstickFrame = nil
local IsFollowStick = true
local function setupThumbstick(parentFrame)
if ThumbstickFrame then return end
local isSmallScreen = isSmallScreenDevice()
local position = isSmallScreen and UDim2.new(0, (THUMBSTICK_SIZE/2) - 10, 1, -THUMBSTICK_SIZE - 20) or
UDim2.new(0, THUMBSTICK_SIZE/2, 1, -THUMBSTICK_SIZE * 1.75)
ThumbstickFrame = createControlFrame("ThumbstickFrame", position, THUMBSTICK_SIZE)
local outerImage = createOuterImage("OuterImage", UDim2.new(0, 0, 0, 0), ThumbstickFrame)
local innerImage = Instance.new('ImageLabel')
innerImage.Name = "InnerImage"
innerImage.Image = TOUCH_CONTROL_SHEET
innerImage.ImageRectOffset = Vector2.new(220, 0)
innerImage.ImageRectSize = Vector2.new(111, 111)
innerImage.BackgroundTransparency = 1
innerImage.Size = UDim2.new(0, THUMBSTICK_SIZE/2, 0, THUMBSTICK_SIZE/2)
innerImage.Position = UDim2.new(0, ThumbstickFrame.Size.X.Offset/2 - THUMBSTICK_SIZE/4, 0, ThumbstickFrame.Size.Y.Offset/2 - THUMBSTICK_SIZE/4)
innerImage.ZIndex = 2
innerImage.Parent = ThumbstickFrame
local centerPosition = nil
local deadZone = 0.05
local function doMove(direction)
local inputAxis = direction / (THUMBSTICK_SIZE/2)
-- Scaled Radial Dead Zone
local inputAxisMagnitude = inputAxis.magnitude
if inputAxisMagnitude < deadZone then
inputAxis = Vector3.new()
else
inputAxis = inputAxis.unit * ((inputAxisMagnitude - deadZone) / (1 - deadZone))
-- NOTE: Making this a unit vector will cause the player to instantly go max speed
inputAxis = Vector3.new(inputAxis.x, 0, inputAxis.y)
end
if Player then
Player:Move(inputAxis, true)
end
end
local function moveStick(position)
local relativePosition = Vector2.new(position.x - centerPosition.x, position.y - centerPosition.y)
local length = relativePosition.magnitude
local maxLength = ThumbstickFrame.AbsoluteSize.x/2
if IsFollowStick and length > maxLength then
local offset = relativePosition.unit * maxLength
ThumbstickFrame.Position = UDim2.new(
0, position.x - ThumbstickFrame.AbsoluteSize.x/2 - offset.x,
0, position.y - ThumbstickFrame.AbsoluteSize.y/2 - offset.y)
else
length = math.min(length, maxLength)
relativePosition = relativePosition.unit * length
end
innerImage.Position = UDim2.new(0, relativePosition.x + innerImage.AbsoluteSize.x/2, 0, relativePosition.y + innerImage.AbsoluteSize.y/2)
end
-- input connections
local moveTouchObject = nil
ThumbstickFrame.InputBegan:connect(function(inputObject)
if moveTouchObject or inputObject.UserInputType ~= Enum.UserInputType.Touch then
return
end
moveTouchObject = inputObject
ThumbstickFrame.Position = UDim2.new(0, inputObject.Position.x - ThumbstickFrame.Size.X.Offset/2, 0, inputObject.Position.y - ThumbstickFrame.Size.Y.Offset/2)
centerPosition = Vector2.new(ThumbstickFrame.AbsolutePosition.x + ThumbstickFrame.AbsoluteSize.x/2,
ThumbstickFrame.AbsolutePosition.y + ThumbstickFrame.AbsoluteSize.y/2)
local direction = Vector2.new(inputObject.Position.x - centerPosition.x, inputObject.Position.y - centerPosition.y)
moveStick(inputObject.Position)
end)
UserInputService.TouchMoved:connect(function(inputObject, isProcessed)
if inputObject == moveTouchObject then
centerPosition = Vector2.new(ThumbstickFrame.AbsolutePosition.x + ThumbstickFrame.AbsoluteSize.x/2,
ThumbstickFrame.AbsolutePosition.y + ThumbstickFrame.AbsoluteSize.y/2)
local direction = Vector2.new(inputObject.Position.x - centerPosition.x, inputObject.Position.y - centerPosition.y)
doMove(direction)
moveStick(inputObject.Position)
end
end)
UserInputService.TouchEnded:connect(function(inputObject, isProcessed)
if inputObject == moveTouchObject then
ThumbstickFrame.Position = position
innerImage.Position = UDim2.new(0, ThumbstickFrame.Size.X.Offset/2 - THUMBSTICK_SIZE/4, 0, ThumbstickFrame.Size.Y.Offset/2 - THUMBSTICK_SIZE/4)
moveTouchObject = nil
if Player then
Player:Move(Vector3.new(), true)
end
end
end)
ThumbstickFrame.Parent = parentFrame
end
----------------------------------------------------------------------------
----------------------------------------------------------------------------
--
-- DPad Control
--
local DPadFrame = nil
local function setupDPadControls(parentFrame)
if DPadFrame then return end
local position = UDim2.new(0, 10, 1, -230)
DPadFrame = createControlFrame("DPadFrame", position, 192)
local smArrowSize = UDim2.new(0, 23, 0, 23)
local lgArrowSize = UDim2.new(0, 64, 0, 64)
local smImgOffset = Vector2.new(46, 46)
local lgImgOffset = Vector2.new(128, 128)
local bBtn = createArrowLabel("BackButton", DPadFrame, UDim2.new(0.5, -32, 1, -64), lgArrowSize, Vector2.new(0, 0), lgImgOffset)
local fBtn = createArrowLabel("ForwardButton", DPadFrame, UDim2.new(0.5, -32, 0, 0), lgArrowSize, Vector2.new(0, 258), lgImgOffset)
local lBtn = createArrowLabel("LeftButton", DPadFrame, UDim2.new(0, 0, 0.5, -32), lgArrowSize, Vector2.new(129, 129), lgImgOffset)
local rBtn = createArrowLabel("RightButton", DPadFrame, UDim2.new(1, -64, 0.5, -32), lgArrowSize, Vector2.new(0, 129), lgImgOffset)
local jumpBtn = createArrowLabel("JumpButton", DPadFrame, UDim2.new(0.5, -32, 0.5, -32), lgArrowSize, Vector2.new(129, 0), lgImgOffset)
local flBtn = createArrowLabel("ForwardLeftButton", DPadFrame, UDim2.new(0, 35, 0, 35), smArrowSize, Vector2.new(129, 258), smImgOffset)
local frBtn = createArrowLabel("ForwardRightButton", DPadFrame, UDim2.new(1, -55, 0, 35), smArrowSize, Vector2.new(176, 258), smImgOffset)
flBtn.Visible = false
frBtn.Visible = false
-- input connections
local playerJump_func = Player.JumpCharacter
jumpBtn.InputBegan:connect(function(inputObject)
playerJump_func(Player)
end)
local compassDir = {
Vector3.new(1, 0, 0), -- E
Vector3.new(1, 0, 1).unit, -- SE
Vector3.new(0, 0, 1), -- S
Vector3.new(-1, 0, 1).unit, -- SW
Vector3.new(-1, 0, 0), -- W
Vector3.new(-1, 0, -1).unit, -- NW
Vector3.new(0, 0, -1), -- N
Vector3.new(1, 0, -1).unit, -- NE
}
local movementVector = Vector3.new(0, 0, 0)
local function setDirection(inputPosition)
local jumpRadius = jumpBtn.AbsoluteSize.x/2
local centerPos = Vector2.new(DPadFrame.AbsolutePosition.x + DPadFrame.AbsoluteSize.x/2, DPadFrame.AbsolutePosition.y + DPadFrame.AbsoluteSize.y/2)
local direction = Vector2.new(inputPosition.x - centerPos.x, inputPosition.y - centerPos.y)
if direction.magnitude > jumpRadius then
local angle = math.atan2(direction.y, direction.x)
local octant = (math.floor(8 * angle / (2 * math.pi) + 8.5)%8) + 1
movementVector = compassDir[octant]
end
if not flBtn.Visible and movementVector == compassDir[7] then
flBtn.Visible = true
frBtn.Visible = true
end
end
local moveTouchObject = nil
local endStep = 0.1
local function endMoveFunc()
flBtn.Visible = false
frBtn.Visible = false
spawn(function()
while not moveTouchObject and movementVector.magnitude ~= 0 do
local newX = movementVector.x
local newZ = movementVector.y
if movementVector.x > 0 then
newX = movementVector.x - endStep
if newX < 0 then newX = 0 end
elseif movementVector.x < 0 then
newX = movementVector.x + endStep
if newX > 0 then newX = 0 end
end
if movementVector.z > 0 then
newZ = movementVector.z - endStep
if newZ < 0 then newZ = 0 end
elseif movementVector.z < 0 then
newZ = movementVector.z + endStep
if newZ > 0 then newZ = 0 end
end
movementVector = Vector3.new(newX, 0, newZ)
Player:Move(movementVector, true)
wait()
end
if movementVector.magnitude ~= 0 then
movementVector = Vector3.new(0, 0, 0)
Player:Move(movementVector, true)
end
end)
end
DPadFrame.InputBegan:connect(function(inputObject)
if moveTouchObject or inputObject.UserInputType ~= Enum.UserInputType.Touch then
return
end
moveTouchObject = inputObject
setDirection(inputObject.Position)
Player:Move(movementVector, true)
end)
DPadFrame.InputChanged:connect(function(inputObject)
if inputObject == moveTouchObject then
setDirection(inputObject.Position)
Player:Move(movementVector, true)
end
end)
DPadFrame.InputEnded:connect(function(inputObject)
if inputObject == moveTouchObject then
moveTouchObject = nil
endMoveFunc()
end
end)
DPadFrame.Parent = parentFrame
end
----------------------------------------------------------------------------
----------------------------------------------------------------------------
--
-- New Thumbpad Controls
--
local ThumbpadFrame = nil
local function setupThumbpad(parentFrame)
if ThumbpadFrame then return end
local isSmallScreen = isSmallScreenDevice()
local position = isSmallScreen and UDim2.new(0, THUMBSTICK_SIZE * 0.5 - 20, 1, -THUMBSTICK_SIZE - 30) or
UDim2.new(0, THUMBSTICK_SIZE/2 - 10, 1, -THUMBSTICK_SIZE * 1.75 - 10)
ThumbpadFrame = createControlFrame("ThumbpadFrame", position, THUMBSTICK_SIZE + 20)
local outerImage = createOuterImage("OuterImage", UDim2.new(0, 10, 0, 10), ThumbpadFrame)
-- arrow set up
local smArrowSize = isSmallScreen and UDim2.new(0, 32, 0, 32) or UDim2.new(0, 64, 0, 64)
local lgArrowSize = UDim2.new(0, smArrowSize.X.Offset * 2, 0, smArrowSize.Y.Offset * 2)
local imgRectSize = Vector2.new(116, 116)
local smImgOffset = isSmallScreen and -4 or -9
local lgImgOffset = isSmallScreen and -28 or -55
local dArrow = createArrowLabel("DownArrow", outerImage, UDim2.new(0.5, -smArrowSize.X.Offset/2, 1, lgImgOffset), smArrowSize, Vector2.new(6, 6), imgRectSize)
local uArrow = createArrowLabel("UpArrow", outerImage, UDim2.new(0.5, -smArrowSize.X.Offset/2, 0, smImgOffset), smArrowSize, Vector2.new(6, 264), imgRectSize)
local lArrow = createArrowLabel("LeftArrow", outerImage, UDim2.new(0, smImgOffset, 0.5, -smArrowSize.Y.Offset/2), smArrowSize, Vector2.new(135, 135), imgRectSize)
local rArrow = createArrowLabel("RightArrow", outerImage, UDim2.new(1, lgImgOffset, 0.5, -smArrowSize.Y.Offset/2), smArrowSize, Vector2.new(6, 135), imgRectSize)
local function doTween(guiObject, endSize, endPosition)
guiObject:TweenSizeAndPosition(endSize, endPosition, Enum.EasingDirection.InOut, Enum.EasingStyle.Linear, 0.15, true)
end
local padOrigin = nil
local deadZone = 0.5
local isRight, isLeft, isUp, isDown = false, false, false, false
local vForward = Vector3.new(0, 0, -1)
local vRight = Vector3.new(1, 0, 0)
local function doMove(pos)
local delta = Vector2.new(pos.x, pos.y) - padOrigin
local inputAxis = delta / (THUMBSTICK_SIZE/2)
-- Scaled Radial Dead Zone
local inputAxisMagnitude = inputAxis.magnitude
if inputAxisMagnitude < deadZone then
inputAxis = Vector3.new(0, 0, 0)
else
inputAxis = inputAxis.unit * ((inputAxisMagnitude - deadZone) / (1 - deadZone))
-- catch possible NAN vector
if inputAxis.magnitude == 0 then
inputAxis = Vector3.new(0, 0, 0)
else
inputAxis = Vector3.new(inputAxis.x, 0, inputAxis.y).unit
end
end
if Player then
Player:Move(inputAxis, true)
end
local forwardDot = inputAxis:Dot(vForward)
local rightDot = inputAxis:Dot(vRight)
if forwardDot > 0.5 then -- UP
if not isUp then
isUp, isDown = true, false
doTween(uArrow, lgArrowSize, UDim2.new(0.5, -smArrowSize.X.Offset, 0, smImgOffset - smArrowSize.Y.Offset * 1.5))
doTween(dArrow, smArrowSize, UDim2.new(0.5, -smArrowSize.X.Offset/2, 1, lgImgOffset))
end
elseif forwardDot < -0.5 then -- DOWN
if not isDown then
isDown, isUp = true, false
doTween(dArrow, lgArrowSize, UDim2.new(0.5, -smArrowSize.X.Offset, 1, lgImgOffset + smArrowSize.Y.Offset/2))
doTween(uArrow, smArrowSize, UDim2.new(0.5, -smArrowSize.X.Offset/2, 0, smImgOffset))
end
else
isUp, isDown = false, false
doTween(dArrow, smArrowSize, UDim2.new(0.5, -smArrowSize.X.Offset/2, 1, lgImgOffset))
doTween(uArrow, smArrowSize, UDim2.new(0.5, -smArrowSize.X.Offset/2, 0, smImgOffset))
end
if rightDot > 0.5 then
if not isRight then
isRight, isLeft = true, false
doTween(rArrow, lgArrowSize, UDim2.new(1, lgImgOffset + smArrowSize.X.Offset/2, 0.5, -smArrowSize.Y.Offset))
doTween(lArrow, smArrowSize, UDim2.new(0, smImgOffset, 0.5, -smArrowSize.Y.Offset/2))
end
elseif rightDot < -0.5 then
if not isLeft then
isLeft, isRight = true, false
doTween(lArrow, lgArrowSize, UDim2.new(0, smImgOffset - smArrowSize.X.Offset * 1.5, 0.5, -smArrowSize.Y.Offset))
doTween(rArrow, smArrowSize, UDim2.new(1, lgImgOffset, 0.5, -smArrowSize.Y.Offset/2))
end
else
isRight, isLeft = false, false
doTween(lArrow, smArrowSize, UDim2.new(0, smImgOffset, 0.5, -smArrowSize.Y.Offset/2))
doTween(rArrow, smArrowSize, UDim2.new(1, lgImgOffset, 0.5, -smArrowSize.Y.Offset/2))
end
end
--input connections
local moveTouchObject = nil
ThumbpadFrame.InputBegan:connect(function(inputObject)
if moveTouchObject or inputObject.UserInputType ~= Enum.UserInputType.Touch then
return
end
moveTouchObject = inputObject
padOrigin = Vector2.new(ThumbpadFrame.AbsolutePosition.x + ThumbpadFrame.AbsoluteSize.x/2,
ThumbpadFrame.AbsolutePosition.y + ThumbpadFrame.AbsoluteSize.y/2)
doMove(inputObject.Position)
end)
UserInputService.InputChanged:connect(function(inputObject, isProcessed)
if inputObject == moveTouchObject then
doMove(inputObject.Position)
end
end)
UserInputService.InputEnded:connect(function(inputObject)
if inputObject == moveTouchObject then
if Player then
Player:Move(Vector3.new(), true)
end
moveTouchObject = nil
isUp, isDown, isLeft, isRight = false, false, false, false
doTween(dArrow, smArrowSize, UDim2.new(0.5, -smArrowSize.X.Offset/2, 1, lgImgOffset))
doTween(uArrow, smArrowSize, UDim2.new(0.5, -smArrowSize.X.Offset/2, 0, smImgOffset))
doTween(lArrow, smArrowSize, UDim2.new(0, smImgOffset, 0.5, -smArrowSize.Y.Offset/2))
doTween(rArrow, smArrowSize, UDim2.new(1, lgImgOffset, 0.5, -smArrowSize.Y.Offset/2))
end
end)
ThumbpadFrame.Parent = parentFrame
end
----------------------------------------------------------------------------
----------------------------------------------------------------------------
--
-- Movement Mode Setup/Config
--
local function configTouchControls()
if UserInputService.ModalEnabled then
JumpButton.Visible = false
ThumbstickFrame.Visible = false
ThumbpadFrame.Visible = false
DPadFrame.Visible = false
if JumpTouchObject then
JumpTouchObject = nil
end
else
local isThumbstickMode = false
local isThumbpadMode = false
local modeName = GameSettings.TouchMovementMode.Name
-- TODO: Need option from c++, is called "Thumbpad"
if modeName == "Default" or modeName == "Thumbstick" then
isThumbstickMode = true
isThumbpadMode = false
elseif modeName == "Thumbpad" then
isThumbstickMode = false
isThumbpadMode = true
else
isThumbstickMode = false
isThumbpadMode = false
end
JumpButton.Visible = isThumbstickMode or isThumbpadMode
ThumbstickFrame.Visible = isThumbstickMode
ThumbpadFrame.Visible = isThumbpadMode
DPadFrame.Visible = not isThumbstickMode and not isThumbpadMode
end
end
local function setupTouchControls()
local touchControlFrame = Instance.new('Frame')
touchControlFrame.Name = "TouchControlFrame"
touchControlFrame.Size = UDim2.new(1, 0, 1, 0)
touchControlFrame.BackgroundTransparency = 1
touchControlFrame.Parent = RobloxGui
setupJumpButton(touchControlFrame)
setupThumbstick(touchControlFrame)
setupDPadControls(touchControlFrame)
setupThumbpad(touchControlFrame)
UserInputService.Changed:connect(function(property)
if property == "ModalEnabled" then
configTouchControls()
end
end)
configTouchControls()
end
----------------------------------------------------------------------------
----------------------------------------------------------------------------
--
-- Camera Control
--
local CameraTouchObject = nil
local PinchTouchObject = nil
local LastCameraInputPosition = nil
local LastPinchScale = nil
local function onTouchStarted(inputObject, isProcessed)
if isProcessed then return end
if CameraTouchObject then
PinchTouchObject = inputObject
else
CameraTouchObject = inputObject
LastCameraInputPosition = inputObject.Position
end
end
local function onTouchMoved(inputObject, isProcessed)
-- pinch zoom
if CameraTouchObject and PinchTouchObject then
if not LastPinchScale then
if inputObject == CameraTouchObject then
LastPinchScale = (inputObject.Position - PinchTouchObject.Position).magnitude
elseif inputObject == PinchTouchObject then
LastPinchScale = (inputObject.Position - CameraTouchObject.Position).magnitude
end
else
local newPinchScale = 0
if inputObject == CameraTouchObject then
newPinchScale = (inputObject.Position - PinchTouchObject.Position).magnitude
elseif inputObject == PinchTouchObject then
newPinchScale = (inputObject.Position - CameraTouchObject.Position).magnitude
end
if newPinchScale ~= 0 then
local delta = newPinchScale - LastPinchScale
if delta ~= 0 then
UserInputService:ZoomCamera(delta * CAMERA_ZOOM_SENSITIVITY)
end
LastPinchScale = newPinchScale
end
end
-- camera pan
elseif inputObject == CameraTouchObject then
local currentPosition = inputObject.Position
if LastCameraInputPosition then
local touchDelta = (LastCameraInputPosition - currentPosition) * CAMERA_ROTATE_SENSITIVITY
UserInputService:RotateCamera(Vector2.new(touchDelta.x, touchDelta.y))
end
LastCameraInputPosition = currentPosition
end
end
local function onTouchEnded(inputObject, isProcessed)
if inputObject == CameraTouchObject then
-- swap input objects
if PinchTouchObject then
CameraTouchObject = PinchTouchObject
LastCameraInputPosition = CameraTouchObject.Position
PinchTouchObject = nil
LastPinchScale = nil
else
CameraTouchObject = nil
LastCameraInputPosition = nil
end
elseif inputObject == PinchTouchObject then
if CameraTouchObject then
LastCameraInputPosition = CameraTouchObject.Position
end
PinchTouchObject = nil
LastPinchScale = nil
end
end
UserInputService.TouchStarted:connect(onTouchStarted)
UserInputService.TouchMoved:connect(onTouchMoved)
UserInputService.TouchEnded:connect(onTouchEnded)
if UserInputService:IsLuaTouchControls() then
setupTouchControls()
else
script:Destroy()
end
GameSettings.Changed:connect(function(property)
if property == "TouchMovementMode" then
configTouchControls()
end
end)
else -- OLD TOUCH CONTROLS WITH THUMBPAD
-- obligatory stuff to make sure we don't access nil data
while not Game do
wait()
end
while not Game:FindFirstChild("Players") do
wait()
end
while not Game:GetService("Players").LocalPlayer do
wait()
end
while not Game:FindFirstChild("CoreGui") do
wait()
end
while not Game:GetService("CoreGui"):FindFirstChild("RobloxGui") do
wait()
end
local userInputService = Game:GetService("UserInputService")
local success = pcall(function() userInputService:IsLuaTouchControls() end)
if not success then
script:Destroy()
end
----------------------------------------------------------------------------
----------------------------------------------------------------------------
-- Variables
local screenResolution = Game:GetService("GuiService"):GetScreenResolution()
function isSmallScreenDevice()
return screenResolution.y <= 500
end
local GameSettings = UserSettings().GameSettings
local localPlayer = Game:GetService("Players").LocalPlayer
local isInThumbstickMode = false
local thumbstickInactiveAlpha = 0.3
local thumbstickSize = 120
if isSmallScreenDevice() then
thumbstickSize = 70
end
local touchControlsSheet = "rbxasset://textures/ui/TouchControlsSheet.png"
local DPadSheet = "rbxasset://textures/ui/DPadSheet.png"
local ThumbstickDeadZone = 5
local ThumbstickMaxPercentGive = 0.92
local thumbstickTouches = {}
local jumpButtonSize = 90
if isSmallScreenDevice() then
jumpButtonSize = 70
end
local oldJumpTouches = {}
local currentJumpTouch = nil
local CameraRotateSensitivity = 0.007
local CameraRotateDeadZone = CameraRotateSensitivity * 16
local CameraZoomSensitivity = 0.03
local PinchZoomDelay = 0.2
local cameraTouch = nil
local dpadTouch = nil
local thumbpadTouchObject = nil
-- make sure all of our images are good to go
Game:GetService("ContentProvider"):Preload(touchControlsSheet)
Game:GetService("ContentProvider"):Preload(DPadSheet)
----------------------------------------------------------------------------
----------------------------------------------------------------------------
-- Functions
local isPointInRect = function(point, rectPos, rectSize)
if point.x >= rectPos.x and point.x <= (rectPos.x + rectSize.x) then
if point.y >= rectPos.y and point.y <= (rectPos.y + rectSize.y) then
return true
end
end
return false
end
--
-- Thumbstick Control
--
function setCameraTouch(newTouch)
cameraTouch = newTouch
if newTouch == nil then
pcall(function() userInputService.InCameraGesture = false end)
else
pcall(function() userInputService.InCameraGesture = true end)
end
end
function DistanceBetweenTwoPoints(point1, point2)
local dx = point2.x - point1.x
local dy = point2.y - point1.y
return math.sqrt( (dx*dx) + (dy*dy) )
end
function transformFromCenterToTopLeft(pointToTranslate, guiObject)
return UDim2.new(0,pointToTranslate.x - guiObject.AbsoluteSize.x/2,0,pointToTranslate.y - guiObject.AbsoluteSize.y/2)
end
function rotatePointAboutLocation(pointToRotate, pointToRotateAbout, radians)
local sinAnglePercent = math.sin(radians)
local cosAnglePercent = math.cos(radians)
local transformedPoint = pointToRotate
-- translate point back to origin:
transformedPoint = Vector2.new(transformedPoint.x - pointToRotateAbout.x, transformedPoint.y - pointToRotateAbout.y)
-- rotate point
local xNew = transformedPoint.x * cosAnglePercent - transformedPoint.y * sinAnglePercent
local yNew = transformedPoint.x * sinAnglePercent + transformedPoint.y * cosAnglePercent
-- translate point back:
transformedPoint = Vector2.new(xNew + pointToRotateAbout.x, yNew + pointToRotateAbout.y)
return transformedPoint
end
function dotProduct(v1,v2)
return ((v1.x*v2.x) + (v1.y*v2.y))
end
function stationaryThumbstickTouchMove(thumbstickFrame, thumbstickOuter, touchLocation)
local thumbstickOuterCenterPosition = Vector2.new(thumbstickOuter.Position.X.Offset + thumbstickOuter.AbsoluteSize.x/2, thumbstickOuter.Position.Y.Offset + thumbstickOuter.AbsoluteSize.y/2)
local centerDiff = DistanceBetweenTwoPoints(touchLocation, thumbstickOuterCenterPosition)
-- thumbstick is moving outside our region, need to cap its distance
if centerDiff > (thumbstickSize/2) then
local thumbVector = Vector2.new(touchLocation.x - thumbstickOuterCenterPosition.x,touchLocation.y - thumbstickOuterCenterPosition.y);
local normal = thumbVector.unit
if normal.x == math.nan or normal.x == math.inf then
normal = Vector2.new(0,normal.y)
end
if normal.y == math.nan or normal.y == math.inf then
normal = Vector2.new(normal.x,0)
end
local newThumbstickInnerPosition = thumbstickOuterCenterPosition + (normal * (thumbstickSize/2))
thumbstickFrame.Position = transformFromCenterToTopLeft(newThumbstickInnerPosition, thumbstickFrame)
else
thumbstickFrame.Position = transformFromCenterToTopLeft(touchLocation,thumbstickFrame)
end
return Vector2.new(thumbstickFrame.Position.X.Offset - thumbstickOuter.Position.X.Offset,thumbstickFrame.Position.Y.Offset - thumbstickOuter.Position.Y.Offset)
end
function followThumbstickTouchMove(thumbstickFrame, thumbstickOuter, touchLocation)
local thumbstickOuterCenter = Vector2.new(thumbstickOuter.Position.X.Offset + thumbstickOuter.AbsoluteSize.x/2, thumbstickOuter.Position.Y.Offset + thumbstickOuter.AbsoluteSize.y/2)
-- thumbstick is moving outside our region, need to position outer thumbstick texture carefully (to make look and feel like actual joystick controller)
if DistanceBetweenTwoPoints(touchLocation, thumbstickOuterCenter) > thumbstickSize/2 then
local thumbstickInnerCenter = Vector2.new(thumbstickFrame.Position.X.Offset + thumbstickFrame.AbsoluteSize.x/2, thumbstickFrame.Position.Y.Offset + thumbstickFrame.AbsoluteSize.y/2)
local movementVectorUnit = Vector2.new(touchLocation.x - thumbstickInnerCenter.x, touchLocation.y - thumbstickInnerCenter.y).unit
local outerToInnerVectorCurrent = Vector2.new(thumbstickInnerCenter.x - thumbstickOuterCenter.x, thumbstickInnerCenter.y - thumbstickOuterCenter.y)
local outerToInnerVectorCurrentUnit = outerToInnerVectorCurrent.unit
local movementVector = Vector2.new(touchLocation.x - thumbstickInnerCenter.x, touchLocation.y - thumbstickInnerCenter.y)
-- First, find the angle between the new thumbstick movement vector,
-- and the vector between thumbstick inner and thumbstick outer.
-- We will use this to pivot thumbstick outer around thumbstick inner, gives a nice joystick feel
local crossOuterToInnerWithMovement = (outerToInnerVectorCurrentUnit.x * movementVectorUnit.y) - (outerToInnerVectorCurrentUnit.y * movementVectorUnit.x)
local angle = math.atan2(crossOuterToInnerWithMovement, dotProduct(outerToInnerVectorCurrentUnit, movementVectorUnit))
local anglePercent = angle * math.min( (movementVector.magnitude)/(outerToInnerVectorCurrent.magnitude), 1.0);
-- If angle is significant, rotate about the inner thumbsticks current center
if math.abs(anglePercent) > 0.00001 then
local outerThumbCenter = rotatePointAboutLocation(thumbstickOuterCenter, thumbstickInnerCenter, anglePercent)
thumbstickOuter.Position = transformFromCenterToTopLeft(Vector2.new(outerThumbCenter.x,outerThumbCenter.y), thumbstickOuter)
end
-- now just translate outer thumbstick to make sure it stays nears inner thumbstick
thumbstickOuter.Position = UDim2.new(0,thumbstickOuter.Position.X.Offset+movementVector.x,0,thumbstickOuter.Position.Y.Offset+movementVector.y)
end
thumbstickFrame.Position = transformFromCenterToTopLeft(touchLocation,thumbstickFrame)
-- a bit of error checking to make sure thumbsticks stay close to eachother
thumbstickFramePosition = Vector2.new(thumbstickFrame.Position.X.Offset,thumbstickFrame.Position.Y.Offset)
thumbstickOuterPosition = Vector2.new(thumbstickOuter.Position.X.Offset,thumbstickOuter.Position.Y.Offset)
if DistanceBetweenTwoPoints(thumbstickFramePosition, thumbstickOuterPosition) > thumbstickSize/2 then
local vectorWithLength = (thumbstickOuterPosition - thumbstickFramePosition).unit * thumbstickSize/2
thumbstickOuter.Position = UDim2.new(0,thumbstickFramePosition.x + vectorWithLength.x,0,thumbstickFramePosition.y + vectorWithLength.y)
end
return Vector2.new(thumbstickFrame.Position.X.Offset - thumbstickOuter.Position.X.Offset,thumbstickFrame.Position.Y.Offset - thumbstickOuter.Position.Y.Offset)
end
function movementOutsideDeadZone(movementVector)
return ( (math.abs(movementVector.x) > ThumbstickDeadZone) or (math.abs(movementVector.y) > ThumbstickDeadZone) )
end
function constructThumbstick(defaultThumbstickPos, updateFunction, stationaryThumbstick)
local thumbstickFrame = Instance.new("Frame")
thumbstickFrame.Name = "ThumbstickFrame"
thumbstickFrame.Active = true
thumbstickFrame.Size = UDim2.new(0,thumbstickSize,0,thumbstickSize)
thumbstickFrame.Position = defaultThumbstickPos
thumbstickFrame.BackgroundTransparency = 1
local outerThumbstick = Instance.new("ImageLabel")
outerThumbstick.Name = "OuterThumbstick"
outerThumbstick.Image = touchControlsSheet
outerThumbstick.ImageRectOffset = Vector2.new(0,0)
outerThumbstick.ImageRectSize = Vector2.new(220,220)
outerThumbstick.BackgroundTransparency = 1
outerThumbstick.Size = UDim2.new(0,thumbstickSize,0,thumbstickSize)
outerThumbstick.Position = defaultThumbstickPos
outerThumbstick.Parent = Game:GetService("CoreGui").RobloxGui
local innerThumbstick = Instance.new("ImageLabel")
innerThumbstick.Name = "InnerThumbstick"
innerThumbstick.Image = touchControlsSheet
innerThumbstick.ImageRectOffset = Vector2.new(220,0)
innerThumbstick.ImageRectSize = Vector2.new(111,111)
innerThumbstick.BackgroundTransparency = 1
innerThumbstick.Size = UDim2.new(0,thumbstickSize/2,0,thumbstickSize/2)
innerThumbstick.Position = UDim2.new(0, thumbstickFrame.Size.X.Offset/2 - thumbstickSize/4, 0, thumbstickFrame.Size.Y.Offset/2 - thumbstickSize/4)
innerThumbstick.Parent = thumbstickFrame
innerThumbstick.ZIndex = 2
local thumbstickTouch = nil
local userInputServiceTouchMovedCon = nil
local userInputSeviceTouchEndedCon = nil
local startInputTracking = function(inputObject)
if thumbstickTouch then return end
if inputObject == cameraTouch then return end
if inputObject == currentJumpTouch then return end
if inputObject.UserInputType ~= Enum.UserInputType.Touch then return end
thumbstickTouch = inputObject
table.insert(thumbstickTouches,thumbstickTouch)
thumbstickFrame.Position = transformFromCenterToTopLeft(thumbstickTouch.Position,thumbstickFrame)
outerThumbstick.Position = thumbstickFrame.Position
userInputServiceTouchMovedCon = userInputService.TouchMoved:connect(function(movedInput)
if movedInput == thumbstickTouch then
local movementVector = nil
if stationaryThumbstick then
movementVector = stationaryThumbstickTouchMove(thumbstickFrame,outerThumbstick,Vector2.new(movedInput.Position.x,movedInput.Position.y))
else
movementVector = followThumbstickTouchMove(thumbstickFrame,outerThumbstick,Vector2.new(movedInput.Position.x,movedInput.Position.y))
end
if updateFunction then
updateFunction(movementVector,outerThumbstick.Size.X.Offset/2)
end
end
end)
userInputSeviceTouchEndedCon = userInputService.TouchEnded:connect(function(endedInput)
if endedInput == thumbstickTouch then
if updateFunction then
updateFunction(Vector2.new(0,0),1)
end
userInputSeviceTouchEndedCon:disconnect()
userInputServiceTouchMovedCon:disconnect()
thumbstickFrame.Position = defaultThumbstickPos
outerThumbstick.Position = defaultThumbstickPos
for i, object in pairs(thumbstickTouches) do
if object == thumbstickTouch then
table.remove(thumbstickTouches,i)
break
end
end
thumbstickTouch = nil
end
end)
end
thumbstickFrame.Visible = not userInputService.ModalEnabled
outerThumbstick.Visible = not userInputService.ModalEnabled
thumbstickFrame.InputBegan:connect(startInputTracking)
return thumbstickFrame, outerThumbstick
end
----------------------------------------------------------------------------
----------------------------------------------------------------------------
--
-- DPad Control
--
function createDPadArrowButton(name, parent, position, size, image, spriteOffset, spriteSize)
local DPadArrow = Instance.new("ImageButton")
DPadArrow.Name = name
DPadArrow.Image = image
DPadArrow.ImageRectOffset = spriteOffset
DPadArrow.ImageRectSize = spriteSize
DPadArrow.BackgroundTransparency = 1
DPadArrow.Size = size
DPadArrow.Position = position
DPadArrow.Parent = parent
return DPadArrow
end
function createDPad()
local DPadFrame = Instance.new("Frame")
DPadFrame.Name = "DPadFrame"
DPadFrame.Active = true
DPadFrame.Size = UDim2.new(0,192,0,192)
DPadFrame.Position = UDim2.new(0,10,1,-230)
DPadFrame.BackgroundTransparency = 1
-- local image = "rbxassetid://133293265"
local image = DPadSheet
local bigSize = UDim2.new(0,64,0,64)
local smallSize = UDim2.new(0,23,0,23)
local spriteSizeLarge = Vector2.new(128, 128)
local spriteSizeSmall = Vector2.new(46, 46)
createDPadArrowButton("BackButton", DPadFrame, UDim2.new(0.5, -32, 1, -64), bigSize, image, Vector2.new(0, 0), spriteSizeLarge)
createDPadArrowButton("ForwardButton", DPadFrame, UDim2.new(0.5, -32, 0, 0), bigSize, image, Vector2.new(0, 258), spriteSizeLarge)
createDPadArrowButton("JumpButton", DPadFrame, UDim2.new(0.5, -32, 0.5, -32), bigSize, image, Vector2.new(129, 0), spriteSizeLarge)
createDPadArrowButton("LeftButton", DPadFrame, UDim2.new(0, 0, 0.5, -32), bigSize, image, Vector2.new(129,129), spriteSizeLarge)
createDPadArrowButton("RightButton", DPadFrame, UDim2.new(1, -64, 0.5, -32), bigSize, image, Vector2.new(0, 129), spriteSizeLarge)
createDPadArrowButton("forwardLeftButton", DPadFrame, UDim2.new(0, 35, 0, 35), smallSize, image, Vector2.new(129,258), spriteSizeSmall)
createDPadArrowButton("forwardRightButton", DPadFrame, UDim2.new(1, -55, 0, 35), smallSize, image, Vector2.new(176,258), spriteSizeSmall)
return DPadFrame
end
function setupDPadControls(DPadFrame)
local moveCharacterFunc = localPlayer.MoveCharacter
DPadFrame.JumpButton.InputBegan:connect(function(inputObject)
localPlayer:JumpCharacter()
end)
local movementVector = Vector2.new(0,0)