@@ -16,115 +16,59 @@ export class MouseJoystickSource extends AnalogueSource {
16
16
this . mouseY = 0.5 ; // Normalized position (0-1)
17
17
this . isActive = false ;
18
18
this . via = null ; // Will be set later
19
-
20
- // Bind event handlers
21
- this . handleMouseMove = this . handleMouseMove . bind ( this ) ;
22
- this . handleMouseEnter = this . handleMouseEnter . bind ( this ) ;
23
- this . handleMouseLeave = this . handleMouseLeave . bind ( this ) ;
24
- this . handleMouseDown = this . handleMouseDown . bind ( this ) ;
25
- this . handleMouseUp = this . handleMouseUp . bind ( this ) ;
26
- this . handleGlobalMouseMove = this . handleGlobalMouseMove . bind ( this ) ;
27
-
28
- // Attach event listeners
29
- this . canvas . addEventListener ( "mousemove" , this . handleMouseMove ) ;
30
- this . canvas . addEventListener ( "mouseenter" , this . handleMouseEnter ) ;
31
- this . canvas . addEventListener ( "mouseleave" , this . handleMouseLeave ) ;
32
- this . canvas . addEventListener ( "mousedown" , this . handleMouseDown ) ;
33
- this . canvas . addEventListener ( "mouseup" , this . handleMouseUp ) ;
34
-
35
- // Also listen to global mouse moves to track position even when not over canvas
36
- document . addEventListener ( "mousemove" , this . handleGlobalMouseMove ) ;
37
19
}
38
20
39
21
/**
40
- * Handle mouse movement over the canvas
41
- * @param {MouseEvent } event - The mouse event
22
+ * Set the VIA reference for button handling
23
+ * @param {object } via - The system VIA
42
24
*/
43
- handleMouseMove ( event ) {
44
- if ( ! this . isActive ) return ;
45
-
46
- const rect = this . canvas . getBoundingClientRect ( ) ;
47
- const x = event . clientX - rect . left ;
48
- const y = event . clientY - rect . top ;
49
-
50
- // Normalize to 0-1 range
51
- this . mouseX = x / rect . width ;
52
- this . mouseY = y / rect . height ;
53
-
54
- // Clamp values
55
- this . mouseX = Math . max ( 0 , Math . min ( 1 , this . mouseX ) ) ;
56
- this . mouseY = Math . max ( 0 , Math . min ( 1 , this . mouseY ) ) ;
25
+ setVia ( via ) {
26
+ this . via = via ;
57
27
}
58
28
59
29
/**
60
- * Handle mouse entering the canvas
30
+ * Handle mouse movement event from external handler
31
+ * @param {number } x - Normalized X position (0-1)
32
+ * @param {number } y - Normalized Y position (0-1)
61
33
*/
62
- handleMouseEnter ( ) {
34
+ onMouseMove ( x , y ) {
35
+ this . mouseX = Math . max ( 0 , Math . min ( 1 , x ) ) ;
36
+ this . mouseY = Math . max ( 0 , Math . min ( 1 , y ) ) ;
63
37
this . isActive = true ;
64
38
}
65
39
66
40
/**
67
- * Handle mouse leaving the canvas
41
+ * Handle mouse button press from external handler
42
+ * @param {number } button - Mouse button number (0 = left, 1 = middle, 2 = right)
68
43
*/
69
- handleMouseLeave ( ) {
70
- this . isActive = false ;
71
- // Don't center when mouse leaves - keep last position
72
- }
73
-
74
- /**
75
- * Handle global mouse movement (even when not over canvas)
76
- * @param {MouseEvent } event - The mouse event
77
- */
78
- handleGlobalMouseMove ( event ) {
79
- const rect = this . canvas . getBoundingClientRect ( ) ;
80
- const x = event . clientX - rect . left ;
81
- const y = event . clientY - rect . top ;
82
-
83
- // Normalize to 0-1 range
84
- this . mouseX = x / rect . width ;
85
- this . mouseY = y / rect . height ;
86
-
87
- // Clamp values to 0-1 range
88
- this . mouseX = Math . max ( 0 , Math . min ( 1 , this . mouseX ) ) ;
89
- this . mouseY = Math . max ( 0 , Math . min ( 1 , this . mouseY ) ) ;
90
- }
91
-
92
- /**
93
- * Handle mouse button press
94
- * @param {MouseEvent } event - The mouse event
95
- */
96
- handleMouseDown ( event ) {
97
- if ( ! this . isActive || ! this . via ) return ;
44
+ onMouseDown ( button ) {
45
+ if ( ! this . via ) return ;
98
46
99
47
// Only handle left mouse button (button 0)
100
- if ( event . button === 0 ) {
101
- // Set fire button 1 pressed (PB4)
48
+ if ( button === 0 ) {
102
49
this . via . setJoystickButton ( 0 , true ) ;
103
- event . preventDefault ( ) ;
104
50
}
105
51
}
106
52
107
53
/**
108
- * Handle mouse button release
109
- * @param {MouseEvent } event - The mouse event
54
+ * Handle mouse button release from external handler
55
+ * @param {number } button - Mouse button number (0 = left, 1 = middle, 2 = right)
110
56
*/
111
- handleMouseUp ( event ) {
57
+ onMouseUp ( button ) {
112
58
if ( ! this . via ) return ;
113
59
114
60
// Only handle left mouse button (button 0)
115
- if ( event . button === 0 ) {
116
- // Release fire button 1 (PB4)
61
+ if ( button === 0 ) {
117
62
this . via . setJoystickButton ( 0 , false ) ;
118
- event . preventDefault ( ) ;
119
63
}
120
64
}
121
65
122
66
/**
123
- * Set the VIA reference for button handling
124
- * @param { object } via - The system VIA
67
+ * Check if mouse joystick is enabled and ready
68
+ * @returns { boolean } True if mouse joystick can handle events
125
69
*/
126
- setVia ( via ) {
127
- this . via = via ;
70
+ isEnabled ( ) {
71
+ return ! ! this . via ;
128
72
}
129
73
130
74
/**
@@ -152,15 +96,12 @@ export class MouseJoystickSource extends AnalogueSource {
152
96
}
153
97
154
98
/**
155
- * Clean up event listeners when source is no longer needed
99
+ * Clean up when source is no longer needed
156
100
* Called by ADC when switching to a different source
157
101
*/
158
102
dispose ( ) {
159
- this . canvas . removeEventListener ( "mousemove" , this . handleMouseMove ) ;
160
- this . canvas . removeEventListener ( "mouseenter" , this . handleMouseEnter ) ;
161
- this . canvas . removeEventListener ( "mouseleave" , this . handleMouseLeave ) ;
162
- this . canvas . removeEventListener ( "mousedown" , this . handleMouseDown ) ;
163
- this . canvas . removeEventListener ( "mouseup" , this . handleMouseUp ) ;
164
- document . removeEventListener ( "mousemove" , this . handleGlobalMouseMove ) ;
103
+ // Reset state
104
+ this . via = null ;
105
+ this . isActive = false ;
165
106
}
166
107
}
0 commit comments