67 lines
2.4 KiB
TypeScript
67 lines
2.4 KiB
TypeScript
import { Controller, OnStart } from "@flamework/core";
|
|
import { CmdrClient } from "@rbxts/cmdr";
|
|
import { Players } from "@rbxts/services";
|
|
import { Trove } from "@rbxts/trove";
|
|
import React from "@rbxts/react";
|
|
import { createRoot } from "@rbxts/react-roblox";
|
|
import { UserInputService, VRService } from "@rbxts/services";
|
|
import { ButtonAction } from "client/input/button-action";
|
|
import { ActionTouchButton } from "client/ui/action-touch-button";
|
|
import { CMDR_ACCESS_ATTRIBUTE } from "shared/cmdr";
|
|
import { TEMPLATE_NAME } from "shared/template-name";
|
|
|
|
@Controller()
|
|
export class CmdrController implements OnStart {
|
|
private readonly trove = new Trove();
|
|
private action?: ButtonAction;
|
|
private renderTouch?: () => void;
|
|
|
|
public onStart(): void {
|
|
CmdrClient.SetActivationKeys([]);
|
|
CmdrClient.SetPlaceName(TEMPLATE_NAME);
|
|
CmdrClient.SetActivationUnlocksMouse(true);
|
|
this.action = new ButtonAction({
|
|
name: "ToggleConsole",
|
|
label: "Console",
|
|
keyboard: [Enum.KeyCode.F2],
|
|
gamepad: [Enum.KeyCode.ButtonY],
|
|
vr: [Enum.KeyCode.ButtonY],
|
|
allowWhileTyping: true,
|
|
onPressed: () => CmdrClient.Toggle(),
|
|
});
|
|
this.trove.add(this.action);
|
|
const mount = new Instance("Folder");
|
|
mount.Name = "ConsoleInputRoot";
|
|
mount.Parent = Players.LocalPlayer.WaitForChild("PlayerGui");
|
|
const root = createRoot(mount);
|
|
this.trove.add(mount);
|
|
this.trove.add(() => root.unmount());
|
|
this.renderTouch = () => {
|
|
const visible =
|
|
Players.LocalPlayer.GetAttribute(CMDR_ACCESS_ATTRIBUTE) === true &&
|
|
UserInputService.TouchEnabled &&
|
|
!VRService.VREnabled;
|
|
root.render(
|
|
visible ? React.createElement(ActionTouchButton, { action: this.action! }) : undefined,
|
|
);
|
|
};
|
|
this.trove.connect(UserInputService.GetPropertyChangedSignal("TouchEnabled"), this.renderTouch);
|
|
this.trove.connect(VRService.GetPropertyChangedSignal("VREnabled"), this.renderTouch);
|
|
this.trove.connect(Players.LocalPlayer.GetAttributeChangedSignal(CMDR_ACCESS_ATTRIBUTE), () =>
|
|
this.updateAccess(),
|
|
);
|
|
this.trove.attachToInstance(script);
|
|
this.updateAccess();
|
|
}
|
|
|
|
private updateAccess(): void {
|
|
const enabled = Players.LocalPlayer.GetAttribute(CMDR_ACCESS_ATTRIBUTE) === true;
|
|
CmdrClient.SetEnabled(enabled);
|
|
this.action?.setEnabled(enabled);
|
|
this.renderTouch?.();
|
|
if (!enabled) {
|
|
CmdrClient.Hide();
|
|
}
|
|
}
|
|
}
|