Skip to content

Commit 55c24e8

Browse files
authored
Merge pull request #9 from steffalon/5-changing-any-of-the-stick-curves-doesnt-apply-at-all
Changing any of the stick curves doesnt apply at all
2 parents 3ebb0f0 + de6be81 commit 55c24e8

File tree

1 file changed

+104
-4
lines changed

1 file changed

+104
-4
lines changed

src/components/Configuration/StickSensitivity.vue

+104-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
import {JoystickProfileId} from "../../enum/JoystickProfileId";
33
import Joystick from "../../model/Joystick";
44
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";
67
7-
defineProps({
8+
const props = defineProps({
89
leftJoystick: {
910
type: Joystick,
1011
required: true,
@@ -16,8 +17,12 @@ defineProps({
1617
});
1718
1819
const leftJoystickRange = ref();
20+
const leftStickCurveCanvas: Ref<HTMLCanvasElement | undefined> = ref();
21+
const rightStickCurveCanvas: Ref<HTMLCanvasElement | undefined> = ref();
1922
const rightJoystickRange = ref();
2023
24+
const edgeHIDController: Ref<HIDDevice> = inject('HIDController')!;
25+
2126
const getCurrentCurve = (joystick: Joystick): number => {
2227
2328
console.log(joystick.getCurveValues(), joystick.getProfileId());
@@ -37,10 +42,86 @@ const changeJoyStickIndex = (joystick: Joystick, event: Event) => {
3742
joystick.setCurveValues(PS5_JOYSTICK_CURVE[joystick.getProfileId()].getAdjustments().map(curve => curve.getByIndex(event.target.value)));
3843
}
3944
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+
40111
</script>
41112
<template>
42113
<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>
43121
<h3>Left stick</h3>
122+
<div class="canvasContainer">
123+
<canvas ref="leftStickCurveCanvas" class="curve"></canvas>
124+
</div>
44125
<select
45126
v-bind:value="leftJoystick.getProfileId()"
46127
@change="(e: any) => {
@@ -69,7 +150,7 @@ const changeJoyStickIndex = (joystick: Joystick, event: Event) => {
69150
</option>
70151
</select>
71152
<input type="range"
72-
@change="e => changeJoyStickIndex(leftJoystick, e)"
153+
@input="e => changeJoyStickIndex(leftJoystick, e)"
73154
:value="getCurrentCurve(leftJoystick)"
74155
min="0"
75156
max="10"
@@ -79,6 +160,9 @@ const changeJoyStickIndex = (joystick: Joystick, event: Event) => {
79160
</section>
80161
<section>
81162
<h3>Right stick</h3>
163+
<div class="canvasContainer">
164+
<canvas ref="rightStickCurveCanvas" class="curve"></canvas>
165+
</div>
82166
<select
83167
v-bind:value="rightJoystick.getProfileId()"
84168
@change="(e: any) => {
@@ -107,7 +191,7 @@ const changeJoyStickIndex = (joystick: Joystick, event: Event) => {
107191
</option>
108192
</select>
109193
<input type="range"
110-
@change="(e: any) => changeJoyStickIndex(rightJoystick, e)"
194+
@input="(e: any) => changeJoyStickIndex(rightJoystick, e)"
111195
:value="getCurrentCurve(rightJoystick)"
112196
min="0"
113197
max="10"
@@ -128,4 +212,20 @@ h3 {
128212
color: #fffffe;
129213
}
130214
}
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+
}
131231
</style>

0 commit comments

Comments
 (0)