2
2
import {JoystickProfileId } from " ../../enum/JoystickProfileId" ;
3
3
import Joystick from " ../../model/Joystick" ;
4
4
import {PS5_JOYSTICK_CURVE } from " ../../helper/bytesToProfile" ;
5
- import {ref } from " vue" ;
5
+ import {onMounted , ref , inject , onUnmounted } from " vue" ;
6
+ import type {Ref } from " vue" ;
6
7
7
- defineProps ({
8
+ const props = defineProps ({
8
9
leftJoystick: {
9
10
type: Joystick ,
10
11
required: true ,
@@ -16,8 +17,12 @@ defineProps({
16
17
});
17
18
18
19
const leftJoystickRange = ref ();
20
+ const leftStickCurveCanvas: Ref <HTMLCanvasElement | undefined > = ref ();
21
+ const rightStickCurveCanvas: Ref <HTMLCanvasElement | undefined > = ref ();
19
22
const rightJoystickRange = ref ();
20
23
24
+ const edgeHIDController: Ref <HIDDevice > = inject (' HIDController' )! ;
25
+
21
26
const getCurrentCurve = (joystick : Joystick ): number => {
22
27
23
28
console .log (joystick .getCurveValues (), joystick .getProfileId ());
@@ -37,10 +42,86 @@ const changeJoyStickIndex = (joystick: Joystick, event: Event) => {
37
42
joystick .setCurveValues (PS5_JOYSTICK_CURVE [joystick .getProfileId ()].getAdjustments ().map (curve => curve .getByIndex (event .target .value )));
38
43
}
39
44
45
+ const drawCurve = (ctx : CanvasRenderingContext2D , joystick : Joystick , testProgress : number ) => {
46
+
47
+ ctx .clearRect (0 , 0 , ctx .canvas .width , ctx .canvas .height );
48
+
49
+ const rows = 4 ;
50
+ const cols = 4 ;
51
+
52
+ const cellWidth = ctx .canvas .width / cols ;
53
+ const cellHeight = ctx .canvas .height / rows ;
54
+
55
+ ctx .strokeStyle = ' #000000' ;
56
+
57
+ // If dark mode
58
+ if (window .matchMedia && window .matchMedia (' (prefers-color-scheme: dark)' ).matches ) {
59
+ ctx .strokeStyle = ' #ffffff' ;
60
+ }
61
+
62
+ ctx .lineWidth = 0.1 ;
63
+
64
+ // Draw the grid
65
+ for (let i = 0 ; i < cols ; i ++ ) {
66
+ for (let j = 0 ; j < rows ; j ++ ) {
67
+ const x = i * cellWidth ;
68
+ const y = j * cellHeight ;
69
+ ctx .strokeRect (x , y , cellWidth , cellHeight );
70
+ }
71
+ }
72
+
73
+ ctx .beginPath ();
74
+ ctx .moveTo (0 , ctx .canvas .height );
75
+ ctx .lineWidth = 2 ;
76
+
77
+ for (let i = 0 ; i < joystick .getModifier () - 1 ; i ++ ) {
78
+ ctx .lineTo ((joystick .getCurveValues ()[i + i ] / 255 ) * ctx .canvas .width , ctx .canvas .height - (joystick .getCurveValues ()[i + i + 1 ] / 255 ) * ctx .canvas .height );
79
+ }
80
+ ctx .lineTo (ctx .canvas .width ,0 );
81
+ ctx .stroke ();
82
+ ctx .closePath ();
83
+ ctx .beginPath ();
84
+ // If darkmode
85
+ if (window .matchMedia && window .matchMedia (' (prefers-color-scheme: dark)' ).matches ) {
86
+ ctx .fillStyle = ' rgba(242,95,76,0.5)' ;
87
+ } else {
88
+ ctx .fillStyle = ' rgba(64,142,198,0.5)' ;
89
+ }
90
+ ctx .fillRect (0 , 0 , (testProgress * 2 / 255 ) * ctx .canvas .width , ctx .canvas .height );
91
+ ctx .stroke ();
92
+ ctx .closePath ();
93
+ }
94
+
95
+ const readInput = (e : HIDInputReportEvent ) => {
96
+ const inputStream = new Uint8Array (e .data .buffer );
97
+ let maxInputLeft = Math .max (inputStream [0 ], inputStream [1 ]) - 128 ;
98
+ let maxInputRight = Math .max (inputStream [2 ], inputStream [3 ]) - 128 ;
99
+ drawCurve (leftStickCurveCanvas .value ! .getContext (' 2d' )! , props .leftJoystick , maxInputLeft );
100
+ drawCurve (rightStickCurveCanvas .value ! .getContext (' 2d' )! , props .rightJoystick , maxInputRight );
101
+ }
102
+
103
+ onMounted (() => {
104
+ edgeHIDController .value .addEventListener (' inputreport' , readInput );
105
+ });
106
+
107
+ onUnmounted (() => {
108
+ edgeHIDController .value .removeEventListener (' inputreport' , readInput );
109
+ });
110
+
40
111
</script >
41
112
<template >
42
113
<section >
114
+ <span class =" note" >
115
+ Note: Changing curve values will not be applied immediately.
116
+ Save your changes first in order to test them.
117
+ </span >
118
+ <span class =" note" >
119
+ Before testing, make sure your controller is set to the right profile using FN + ACTION button.
120
+ </span >
43
121
<h3 >Left stick</h3 >
122
+ <div class =" canvasContainer" >
123
+ <canvas ref =" leftStickCurveCanvas" class =" curve" ></canvas >
124
+ </div >
44
125
<select
45
126
v-bind:value =" leftJoystick.getProfileId()"
46
127
@change =" (e: any) => {
@@ -69,7 +150,7 @@ const changeJoyStickIndex = (joystick: Joystick, event: Event) => {
69
150
</option >
70
151
</select >
71
152
<input type =" range"
72
- @change =" e => changeJoyStickIndex(leftJoystick, e)"
153
+ @input =" e => changeJoyStickIndex(leftJoystick, e)"
73
154
:value =" getCurrentCurve(leftJoystick)"
74
155
min =" 0"
75
156
max =" 10"
@@ -79,6 +160,9 @@ const changeJoyStickIndex = (joystick: Joystick, event: Event) => {
79
160
</section >
80
161
<section >
81
162
<h3 >Right stick</h3 >
163
+ <div class =" canvasContainer" >
164
+ <canvas ref =" rightStickCurveCanvas" class =" curve" ></canvas >
165
+ </div >
82
166
<select
83
167
v-bind:value =" rightJoystick.getProfileId()"
84
168
@change =" (e: any) => {
@@ -107,7 +191,7 @@ const changeJoyStickIndex = (joystick: Joystick, event: Event) => {
107
191
</option >
108
192
</select >
109
193
<input type =" range"
110
- @change =" (e: any) => changeJoyStickIndex(rightJoystick, e)"
194
+ @input =" (e: any) => changeJoyStickIndex(rightJoystick, e)"
111
195
:value =" getCurrentCurve(rightJoystick)"
112
196
min =" 0"
113
197
max =" 10"
@@ -128,4 +212,20 @@ h3 {
128
212
color : #fffffe ;
129
213
}
130
214
}
215
+ .note {
216
+ margin-top : 21px ;
217
+ display : block ;
218
+ }
219
+ .canvasContainer {
220
+ width : 520px ;
221
+ height : 255px ;
222
+ resize : both ;
223
+ overflow : hidden ;
224
+ border : 1px solid white ;
225
+ }
226
+ .curve {
227
+ width : 100% ;
228
+ height : 100% ;
229
+ display : block ;
230
+ }
131
231
</style >
0 commit comments