Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

Commit 00983b7

Browse files
SolarCraneBen Tkacheff
authored andcommitted
VR-36 Show/Hide 2D GUIs with New Toggle Button (#415)
1 parent a44185b commit 00983b7

File tree

3 files changed

+119
-8
lines changed

3 files changed

+119
-8
lines changed

CoreScriptsRoot/CoreScripts/Topbar.lua

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ local DEBOUNCE_TIME = 0.25
3838
local defeatableTopbarSuccess, defeatableTopbarFlagValue = pcall(function() return settings():GetFFlag("EnableSetCoreTopbarEnabled") end)
3939
local defeatableTopbar = (defeatableTopbarSuccess and defeatableTopbarFlagValue == true)
4040

41+
local vr3dGuisSuccess, vr3dGuisFlagValue = pcall(function() return settings():GetFFlag("RenderUserGuiIn3DSpace") end)
42+
local vr3dGuis = (vr3dGuisSuccess and vr3dGuisFlagValue == true)
43+
4144
--[[ END OF FFLAG VALUES ]]
4245

4346

@@ -50,6 +53,7 @@ local InputService = game:GetService('UserInputService')
5053
local StarterGui = game:GetService('StarterGui')
5154
local ContextActionService = game:GetService("ContextActionService")
5255
local RunService = game:GetService('RunService')
56+
local TextService = game:GetService('TextService')
5357

5458
--[[ END OF SERVICES ]]
5559

@@ -511,7 +515,12 @@ local function Create3DMenuItem()
511515

512516
function menuItem:SetHoverText(text)
513517
hoverTextLabel.Text = text
514-
local frameWidth = hoverTextLabel.TextBounds.X + 20
518+
local textSize = TextService:GetTextSize(
519+
hoverTextLabel.Text,
520+
24,
521+
hoverTextLabel.Font,
522+
Vector2.new(1000, 10000))
523+
local frameWidth = textSize.x + 20
515524
hoverTextFrame.Position = UDim2.new(0.5, -frameWidth / 2, 0, 55)
516525
hoverTextFrame.Size = UDim2.new(0, frameWidth, 0, 60)
517526
end
@@ -1253,6 +1262,70 @@ local function Create3DChatIcon(topBarInstance, panel, menubar)
12531262
return menuItem
12541263
end
12551264

1265+
local function Create3DUserGuiToggleIcon(topBarInstance, panel, menubar)
1266+
local menuItem = Create3DMenuItem()
1267+
menuItem:SetHoverText("2D UI")
1268+
1269+
local icon = Util.Create "ImageLabel" {
1270+
Parent = menuItem:GetInstance(),
1271+
1272+
Position = UDim2.new(0.5, -14, 0.5, -14),
1273+
Size = UDim2.new(0, 28, 0, 28),
1274+
1275+
BackgroundTransparency = 1,
1276+
1277+
Image = "rbxasset://textures/ui/VR/toggle2D.png"
1278+
}
1279+
1280+
local userGuiPanel = Panel3D.Get("UserGui")
1281+
userGuiPanel:SetType(Panel3D.Type.Fixed)
1282+
userGuiPanel:ResizePixels(300, 125)
1283+
userGuiPanel:SetVisible(false, false)
1284+
1285+
menuItem.MouseButton1Click:connect(function()
1286+
if InputService.VREnabled then
1287+
userGuiPanel:SetVisible(not userGuiPanel.isVisible, false)
1288+
end
1289+
end)
1290+
1291+
function userGuiPanel:OnVisibilityChanged(visible)
1292+
if visible then
1293+
local headLook = Panel3D.GetHeadLookXZ(true)
1294+
userGuiPanel.localCF = headLook * CFrame.Angles(math.rad(5), 0, 0) * CFrame.new(0, 0, 5)
1295+
end
1296+
menuItem:SetActive(visible)
1297+
local success, msg = pcall(function()
1298+
CoreGuiService:SetUserGuiRendering(true, visible and userGuiPanel:GetPart() or nil, Enum.NormalId.Front)
1299+
end)
1300+
if not success then
1301+
print("Topbar - userGuiPanel:OnVisibilityChanged:" , msg)
1302+
end
1303+
end
1304+
1305+
local function OnVREnabled(prop)
1306+
if prop == 'VREnabled' then
1307+
local guiPart = nil
1308+
if InputService.VREnabled then
1309+
if userGuiPanel.isVisible then
1310+
guiPart = userGuiPanel:GetPart()
1311+
end
1312+
else
1313+
userGuiPanel:SetVisible(false, false)
1314+
end
1315+
local success, msg = pcall(function()
1316+
CoreGuiService:SetUserGuiRendering(InputService.VREnabled, guiPart, Enum.NormalId.Front)
1317+
end)
1318+
if not success then
1319+
print("Topbar - OnVREnabled:" , msg)
1320+
end
1321+
end
1322+
end
1323+
InputService.Changed:connect(OnVREnabled)
1324+
spawn(function() OnVREnabled("VREnabled") end)
1325+
1326+
return menuItem
1327+
end
1328+
12561329
-----------
12571330

12581331
--- Backpack ---
@@ -1346,6 +1419,7 @@ local nameAndHealthMenuItem = CreateUsernameHealthMenuItem()
13461419

13471420
local settingsIcon3D = Create3DSettingsIcon(TopBar, Topbar3DPanel, ThreeDMenubar)
13481421
local chatIcon3D = Create3DChatIcon(TopBar, Topbar3DPanel, ThreeDMenubar)
1422+
local userGuiIcon3D = Create3DUserGuiToggleIcon(TopBar, Topbar3DPanel, ThreeDMenubar)
13491423

13501424
local LEFT_ITEM_ORDER = {}
13511425
local RIGHT_ITEM_ORDER = {}
@@ -1382,6 +1456,9 @@ end
13821456
if chatIcon3D then
13831457
THREE_D_ITEM_ORDER[chatIcon3D] = 2
13841458
end
1459+
if userGuiIcon3D then
1460+
THREE_D_ITEM_ORDER[userGuiIcon3D] = 3
1461+
end
13851462

13861463
-------------------------
13871464

@@ -1476,6 +1553,39 @@ if nameAndHealthMenuItem and topbarEnabled and not isTenFootInterface then
14761553
AddItemInOrder(RightMenubar, nameAndHealthMenuItem, RIGHT_ITEM_ORDER)
14771554
end
14781555

1556+
if userGuiIcon3D and vr3dGuis then
1557+
local function FindScreenGuiChild(object)
1558+
for _, child in pairs(object:GetChildren()) do
1559+
if child:IsA('ScreenGui') then
1560+
return child
1561+
end
1562+
end
1563+
end
1564+
1565+
local function onPlayerGuiAdded(playerGui)
1566+
playerGui.ChildAdded:connect(function(child)
1567+
if FindScreenGuiChild(playerGui) then
1568+
AddItemInOrder(ThreeDMenubar, userGuiIcon3D, THREE_D_ITEM_ORDER)
1569+
end
1570+
end)
1571+
playerGui.ChildRemoved:connect(function(child)
1572+
if child:IsA('ScreenGui') and not FindScreenGuiChild(playerGui) then
1573+
ThreeDMenubar:RemoveItem(userGuiIcon3D)
1574+
end
1575+
end)
1576+
if FindScreenGuiChild(playerGui) then
1577+
AddItemInOrder(ThreeDMenubar, userGuiIcon3D, THREE_D_ITEM_ORDER)
1578+
end
1579+
end
1580+
Player.ChildAdded:connect(function(child)
1581+
if child:IsA('PlayerGui') then
1582+
onPlayerGuiAdded(child)
1583+
end
1584+
end)
1585+
if Player:FindFirstChild('PlayerGui') then
1586+
onPlayerGuiAdded(Player:FindFirstChild('PlayerGui'))
1587+
end
1588+
end
14791589

14801590

14811591
local function MoveHamburgerTo3D()

CoreScriptsRoot/StarterScript.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ end
5757
if useVRKeyboard then
5858
require(RobloxGui.Modules.VR.VirtualKeyboard)
5959
end
60+

PlayerScripts/PlayerScriptsPlace/StarterPlayer/StarterPlayerScripts/CameraScript/ShiftLockController.rbxmx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<roblox xmlns:xmime="http://www.w3.org/2005/05/xmlmime" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.roblox.com/roblox.xsd" version="4">
22
<External>null</External>
33
<External>nil</External>
4-
<Item class="ModuleScript" referent="RBX7441C7686732497F97F2119BEAB662F5">
4+
<Item class="ModuleScript" referent="RBX2C86BBBF120542588339BAC4360424A7">
55
<Properties>
66
<Content name="LinkedSource"><null></null></Content>
77
<string name="Name">ShiftLockController</string>
@@ -95,12 +95,12 @@ local function initialize()
9595
ShiftLockIcon.Position = UDim2.new(0, 12, 0, 2)
9696
ShiftLockIcon.BackgroundTransparency = 1
9797
ShiftLockIcon.Image = IsShiftLocked and SHIFT_LOCK_ON or SHIFT_LOCK_OFF
98-
ShiftLockIcon.Visible = IsShiftLockMode
98+
ShiftLockIcon.Visible = true
9999
ShiftLockIcon.Parent = frame
100100

101101
ShiftLockIcon.MouseButton1Click:connect(onShiftLockToggled)
102102

103-
ScreenGui.Parent = PlayerGui
103+
ScreenGui.Parent = IsShiftLockMode and PlayerGui or nil
104104
end
105105

106106
--[[ Public API ]]--
@@ -120,7 +120,7 @@ local mouseLockSwitchFunc = function(actionName, inputState, inputObject)
120120
end
121121

122122
local function disableShiftLock()
123-
if ShiftLockIcon then ShiftLockIcon.Visible = false end
123+
if ScreenGui then ScreenGui.Parent = nil end
124124
IsShiftLockMode = false
125125
Mouse.Icon = ""
126126
if InputCn then
@@ -144,8 +144,8 @@ end
144144
local function enableShiftLock()
145145
IsShiftLockMode = isShiftLockMode()
146146
if IsShiftLockMode then
147-
if ShiftLockIcon then
148-
ShiftLockIcon.Visible = true
147+
if ScreenGui then
148+
ScreenGui.Parent = PlayerGui
149149
end
150150
if IsShiftLocked then
151151
Mouse.Icon = SHIFT_LOCK_CURSOR
@@ -213,4 +213,4 @@ return ShiftLockController
213213
]]></ProtectedString>
214214
</Properties>
215215
</Item>
216-
</roblox>
216+
</roblox>

0 commit comments

Comments
 (0)