Skip to content

Commit b0a4c21

Browse files
committed
feat: phase out @rbxts/gamejoy in favor of @rbxts/mechanism
1 parent 8575da0 commit b0a4c21

5 files changed

Lines changed: 79 additions & 54 deletions

File tree

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,16 @@
4444
"@rbxts/firebase": "^1.0.1",
4545
"@rbxts/flamework-binary-serializer": "^0.6.0",
4646
"@rbxts/flamework-meta-utils": "^1.0.1",
47-
"@rbxts/gamejoy": "^1.1.4",
4847
"@rbxts/id": "^1.0.0",
4948
"@rbxts/instance-utility": "^1.0.1",
5049
"@rbxts/iris": "^2.2.0-ts.0",
5150
"@rbxts/janitor": "^1.15.7-ts.0",
5251
"@rbxts/lazy": "^1.0.1",
5352
"@rbxts/lazy-iterator": "^1.0.1",
54-
"@rbxts/mechanism": "^1.0.0",
53+
"@rbxts/mechanism": "^1.0.6",
5554
"@rbxts/object-utils": "^1.0.4",
5655
"@rbxts/quaternion": "^1.0.0",
57-
"@rbxts/runit": "^1.1.1",
56+
"@rbxts/runit": "^1.2.0",
5857
"@rbxts/signal": "^1.1.1",
5958
"@rbxts/strict-map": "^1.0.2",
6059
"@rbxts/string-builder": "^1.0.0",
@@ -63,4 +62,4 @@
6362
"@rbxts/wave": "^1.0.0",
6463
"node": "^18.20.1"
6564
}
66-
}
65+
}

src/client/controllers/control-panel.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { createMappingDecorator } from "shared/utility/meta";
1010
import type { ControlPanelDropdownRenderer } from "shared/structs/control-panel";
1111

1212
import type { MouseController } from "./mouse";
13+
import { StandardActionBuilder } from "@rbxts/mechanism";
1314

1415
const [renderableMeta, ControlPanelRenderable] = createMappingDecorator<ControlPanelDropdownRenderer, never[], [dropdownName: string, order?: number]>();
1516
export { ControlPanelRenderable };
@@ -43,13 +44,13 @@ export class ControlPanelController implements OnStart, LogStart {
4344
Iris.Connect(() => this.render());
4445
}
4546

46-
@OnInput("Comma")
47+
@OnInput(new StandardActionBuilder(Enum.KeyCode.Comma))
4748
public open(): void {
4849
if (!isDeveloper(Player)) return;
4950
this.windowOpened.set(!this.windowOpened.get());
5051
}
5152

52-
@OnInput("P")
53+
@OnInput(new StandardActionBuilder(Enum.KeyCode.P))
5354
public unlockMouse(): void {
5455
if (!isDeveloper(Player)) return;
5556
this.mouseUnlocked = !this.mouseUnlocked;

src/client/controllers/input.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,30 @@ import { Controller } from "@flamework/core";
22

33
import type { LogStart } from "shared/hooks";
44
import { OnInput, OnInputRelease } from "client/decorators";
5+
import { StandardActionBuilder } from "@rbxts/mechanism";
56

7+
export const enum ActionID {
8+
LMB,
9+
RMB,
10+
MMB,
11+
LeftTrigger,
12+
RightTrigger,
13+
Crouch
14+
}
615

716
/** Handles all game input */
817
@Controller()
918
export class InputController implements LogStart {
10-
@OnInput("C", "crouch")
19+
@OnInput(
20+
new StandardActionBuilder(Enum.KeyCode.C)
21+
.setID(ActionID.Crouch)
22+
)
1123
public crouch(): void {
1224
// code for crouching
1325
print("crouched")
1426
}
1527

16-
@OnInputRelease("crouch")
28+
@OnInputRelease(ActionID.Crouch)
1729
public stand(): void {
1830
// code for standing
1931
print("stood")

src/client/controllers/mouse.ts

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { Controller, type OnInit, type OnRender } from "@flamework/core";
22
import { UserInputService as UserInput, Workspace as World } from "@rbxts/services";
33
import { RaycastParamsBuilder } from "@rbxts/builders";
4-
import { Context as InputContext } from "@rbxts/gamejoy";
5-
import { Axis } from "@rbxts/gamejoy/out/Actions";
64
import Charm, { atom } from "@rbxts/charm";
75
import Signal from "@rbxts/signal";
86

9-
import { OnInput, OnAxisInput, OnInputRelease } from "client/decorators";
7+
import { OnInput, OnAxisInput, OnInputRelease, inputManager } from "client/decorators";
108
import { Player } from "client/utility";
9+
import { AxisAction, AxisActionBuilder, StandardActionBuilder } from "@rbxts/mechanism";
10+
import { ActionID } from "./input";
1111

1212
const { abs } = math;
1313

@@ -24,14 +24,17 @@ export class MouseController implements OnInit, OnRender {
2424
public readonly behavior = atom<Enum.MouseBehavior>(Enum.MouseBehavior.Default);
2525

2626
private readonly playerMouse = Player.GetMouse();
27-
private readonly rightThumbstickAxis = new Axis("Thumbstick2");
27+
private readonly rightThumbstickAxis = new AxisActionBuilder(Enum.KeyCode.Thumbstick2);
2828
private readonly thumbstickDeadzone = 0.1;
2929

3030
private lastInput?: Enum.UserInputType;
3131
private delta = new Vector2;
3232

3333
public onInit(): void {
34-
new InputContext().Bind(this.rightThumbstickAxis, () => { /* this is only so that the Position property computes */ });
34+
inputManager
35+
.bind(this.rightThumbstickAxis)
36+
.bind(new StandardActionBuilder(Enum.KeyCode.ButtonL2).setID(ActionID.LeftTrigger))
37+
.bind(new StandardActionBuilder(Enum.KeyCode.ButtonR2).setID(ActionID.RightTrigger));
3538

3639
// Touch controls
3740
UserInput.TouchPinch.Connect((_, scale) => this.scrolled.Fire((scale < 1 ? 1 : -1) * abs(scale - 2)));
@@ -51,8 +54,15 @@ export class MouseController implements OnInit, OnRender {
5154
this.delta = UserInput.GetMouseDelta();
5255
break;
5356
}
54-
case Enum.UserInputType.Gamepad1: {
55-
const { X, Y } = this.rightThumbstickAxis.Position;
57+
case Enum.UserInputType.Gamepad1:
58+
case Enum.UserInputType.Gamepad2:
59+
case Enum.UserInputType.Gamepad3:
60+
case Enum.UserInputType.Gamepad4:
61+
case Enum.UserInputType.Gamepad5:
62+
case Enum.UserInputType.Gamepad6:
63+
case Enum.UserInputType.Gamepad7:
64+
case Enum.UserInputType.Gamepad8: {
65+
const { X, Y } = this.rightThumbstickAxis.position;
5666
this.delta = new Vector2(
5767
this.applyThumbstickDeadzone(X),
5868
this.applyThumbstickDeadzone(-Y)
@@ -90,74 +100,83 @@ export class MouseController implements OnInit, OnRender {
90100
}
91101

92102
/** @hidden */
93-
@OnAxisInput("MouseWheel")
94-
public onScroll(axis: Axis<"MouseWheel">): void {
95-
this.scrolled.Fire(-axis.Position.Z);
103+
@OnAxisInput(new AxisActionBuilder(Enum.UserInputType.MouseWheel))
104+
public onScroll(axis: AxisAction): void {
105+
this.scrolled.Fire(-axis.position.Z);
96106
}
97107

98108
/** @hidden */
99-
@OnAxisInput("ButtonR2", "axisR2")
100-
public onR2AxisChange(axis: Axis<"ButtonR2">): void {
109+
@OnAxisInput(new AxisActionBuilder(Enum.KeyCode.ButtonR2))
110+
public onR2AxisChange(axis: AxisAction): void {
101111
this.triggerAxesChange(axis, this.isLmbDown);
102112
}
103113

104114
/** @hidden */
105-
@OnAxisInput("ButtonL2", "axisL2")
106-
public onL2AxisChange(axis: Axis<"ButtonL2">): void {
115+
@OnAxisInput(new AxisActionBuilder(Enum.KeyCode.ButtonL2))
116+
public onL2AxisChange(axis: AxisAction): void {
107117
this.triggerAxesChange(axis, this.isLmbDown);
108118
}
109119

110120
/** @hidden */
111-
@OnInputRelease("axisR2")
121+
@OnInputRelease(ActionID.RightTrigger)
112122
public onR2Release(): void {
113123
this.rmbUp();
114124
}
115125

116126
/** @hidden */
117-
@OnInputRelease("axisL2")
127+
@OnInputRelease(ActionID.LeftTrigger)
118128
public onL2Release(): void {
119129
this.lmbUp();
120130
}
121131

122132
/** @hidden */
123-
@OnInputRelease("mmb")
133+
@OnInputRelease(ActionID.MMB)
124134
public mmbUp(): void {
125135
this.isMmbDown(false);
126136
}
127137

128138
/** @hidden */
129-
@OnInput("MouseButton3", "mmb")
139+
@OnInput(
140+
new StandardActionBuilder(Enum.KeyCode.MouseMiddleButton)
141+
.setID(ActionID.MMB)
142+
)
130143
public mmbDown(): void {
131144
this.isMmbDown(true);
132145
}
133146

134147
/** @hidden */
135-
@OnInputRelease("rmb")
148+
@OnInputRelease(ActionID.RMB)
136149
public rmbUp(): void {
137150
this.isRmbDown(false);
138151
}
139152

140153
/** @hidden */
141-
@OnInput("MouseButton2", "rmb")
154+
@OnInput(
155+
new StandardActionBuilder(Enum.KeyCode.MouseRightButton)
156+
.setID(ActionID.RMB)
157+
)
142158
public rmbDown(): void {
143159
this.isRmbDown(true);
144160
}
145161

146162
/** @hidden */
147-
@OnInputRelease("lmb")
163+
@OnInputRelease(ActionID.LMB)
148164
public lmbUp(): void {
149165
this.isLmbDown(false);
150166
}
151167

152168
/** @hidden */
153-
@OnInput("MouseButton1", "lmb")
169+
@OnInput(
170+
new StandardActionBuilder(Enum.KeyCode.MouseLeftButton)
171+
.setID(ActionID.LMB)
172+
)
154173
public lmbDown(): void {
155174
this.isLmbDown(true);
156175
}
157176

158-
private triggerAxesChange(axis: Axis<"ButtonL2" | "ButtonR2">, isDown: Charm.Atom<boolean>): void {
159-
if (axis.Delta.Z < 0) return void isDown(false);
160-
if (axis.Delta.Z < 0.05) return;
177+
private triggerAxesChange(axis: AxisAction, isDown: Charm.Atom<boolean>): void {
178+
if (axis.delta.Z < 0) return void isDown(false);
179+
if (axis.delta.Z < 0.05) return;
161180
isDown(true);
162181
}
163182

src/client/decorators.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Modding } from "@flamework/core";
2-
import { InputManager } from "@rbxts/mechanism";
3-
import { BaseAction } from "@rbxts/mechanism/out/base-action";
2+
import { InputManager, StandardAction, AxisAction } from "@rbxts/mechanism";
43
import { callMethodOnDependency } from "@rbxts/flamework-meta-utils";
54
import type { ClientReceiver as ClientEventReceiver } from "@flamework/networking/out/events/types";
65
import type { ClientReceiver as ClientFunctionReceiver } from "@flamework/networking/out/functions/types";
@@ -9,31 +8,27 @@ import type { Serializer } from "@rbxts/flamework-binary-serializer";
98
import { FlameworkIgnited } from "shared/constants";
109
import Log from "shared/log";
1110

12-
const inputManager = new InputManager;
11+
export const inputManager = new InputManager;
1312

14-
export const OnInput = Modding.createDecorator<[binding: BaseAction]>(
13+
export const OnInput = Modding.createDecorator<[binding: StandardAction]>(
1514
"Method",
1615
(descriptor, [action]) => {
1716
FlameworkIgnited.Once(() => {
18-
inputManager.bind(action)
19-
context.Bind(<ActionLike<RawActionEntry>>action, () => {
17+
inputManager.bind(action);
18+
action.activated.Connect(() => {
2019
const object = <Record<string, Callback>>Modding.resolveSingleton(descriptor.constructor!);
2120
void task.spawn(object[descriptor.property], object, action);
2221
});
2322
});
2423
}
2524
);
2625

27-
export const OnAxisInput = Modding.createDecorator<[binding: AxisActionEntry, actionName?: string, process?: boolean]>(
26+
export const OnAxisInput = Modding.createDecorator<[binding: AxisAction]>(
2827
"Method",
29-
(descriptor, [rawAction, actionName, process]) => {
30-
const axis = new Axis(rawAction);
31-
if (actionName !== undefined)
32-
inputActions[actionName] = axis;
33-
28+
(descriptor, [axis]) => {
3429
FlameworkIgnited.Once(() => {
35-
const context = process ? processedContext : inputContext;
36-
context.Bind(axis, () => {
30+
inputManager.bind(axis);
31+
axis.updated.Connect(() => {
3732
const object = <Record<string, Callback>>Modding.resolveSingleton(descriptor.constructor!);
3833
void task.spawn(object[descriptor.property], object, axis);
3934
});
@@ -42,21 +37,20 @@ export const OnAxisInput = Modding.createDecorator<[binding: AxisActionEntry, ac
4237
);
4338

4439
/** **Note:** You need to provide an action name to the OnInput decorator to use this decorator, with which you will use the same action name. */
45-
export const OnInputRelease = Modding.createDecorator<[actionName: string, process?: boolean]>(
40+
export const OnInputRelease = Modding.createDecorator<[actionID: string | number]>(
4641
"Method",
47-
(descriptor, [actionName, process]) => task.spawn(() => {
42+
(descriptor, [actionID]) => task.spawn(() => {
4843
FlameworkIgnited.Once(() => {
49-
let action = inputActions[actionName];
44+
let action = inputManager.getActionByID(actionID, StandardAction);
5045
if (action === undefined) {
5146
task.wait(0.1);
52-
action = inputActions[actionName];
47+
action = inputManager.getActionByID(actionID, StandardAction);
5348
}
5449

5550
if (action === undefined)
56-
throw Log.fatal(`Failed to bind method "${descriptor.property}" using @OnInputRelease decorator: No input action "${actionName}" exists`);
51+
throw Log.fatal(`Failed to bind method "${descriptor.property}" using @OnInputRelease decorator: No input action with ID "${actionID}" exists`);
5752

58-
const context = process ? processedContext : inputContext;
59-
context.BindEvent(actionName, action.Released, () => {
53+
action.deactivated.Connect(() => {
6054
const object = <Record<string, Callback>>Modding.resolveSingleton(descriptor.constructor!);
6155
void task.spawn(object[descriptor.property], object, action);
6256
});

0 commit comments

Comments
 (0)