1
- /*Copyright (c) 2024 oblerion
1
+ /*Copyright (c) 2025 oblerion
2
2
3
3
Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
of this software and associated documentation files (the "Software"), to deal
@@ -18,10 +18,11 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
18
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
19
SOFTWARE.
20
20
*/
21
- const LOVE2D_VERSION = "b0.6.2-2 "
21
+ const LOVE2D_VERSION = "b0.6.3 "
22
22
const LOVE2D_MOUSE = true ;
23
23
const LOVE2D_KEYBOARD = true ;
24
24
const LOVE2D_TOUCH = true ;
25
+ const LOVE2D_GAMEPAD = true ;
25
26
26
27
function _sizeOf ( value )
27
28
{
@@ -60,6 +61,50 @@ function collide(a, b) {
60
61
return true ;
61
62
return false ;
62
63
}
64
+
65
+ class Joystick
66
+ {
67
+ constructor ( joystick )
68
+ {
69
+ this . joystick = joystick ;
70
+ }
71
+ getName ( )
72
+ {
73
+ return this . joystick . id ;
74
+ }
75
+ getId ( )
76
+ {
77
+ return this . joystick . index ;
78
+ }
79
+ getButtonCount ( )
80
+ {
81
+ return this . joystick . buttons . length ;
82
+ }
83
+ getAxisCount ( )
84
+ {
85
+ return this . joystick . axes . length ;
86
+ }
87
+ isDown ( id )
88
+ {
89
+ if ( id > - 1 &&
90
+ id < this . getButtonCount ( ) )
91
+ {
92
+ return this . joystick . buttons [ id ] . pressed ;
93
+ }
94
+ return false ;
95
+ }
96
+ getAxis ( id )
97
+ {
98
+ if ( id > - 1 &&
99
+ id < this . getAxisCount ( )
100
+ )
101
+ {
102
+ return this . joystick . axes [ id ] ;
103
+ }
104
+ return 0 ;
105
+ }
106
+ }
107
+
63
108
class Love
64
109
{
65
110
constructor ( )
@@ -96,7 +141,27 @@ class Love
96
141
97
142
this . lastUpdate = 0 ;
98
143
this . loading = 0 ;
144
+
145
+ this . joysticks = [ ] ;
99
146
}
147
+ _update_joystick ( )
148
+ {
149
+ this . joysticks = [ ] ;
150
+ let l = Navigator . getGamepads ( ) ;
151
+ for ( let i = 0 ; i < l . length ; i ++ )
152
+ {
153
+ let jt = new Joystick ( l [ i ] ) ;
154
+ this . joysticks . push ( jt ) ;
155
+ }
156
+ }
157
+ joystick_getJoysticks ( )
158
+ {
159
+ return this . joysticks ;
160
+ }
161
+ joystick_getJoystickCount ( )
162
+ {
163
+ return this . joysticks . length
164
+ }
100
165
system_writeSave ( name , val )
101
166
{
102
167
if ( typeof ( name ) == "string" &&
@@ -763,6 +828,26 @@ function _event_ontouchend(e) {
763
828
}
764
829
}
765
830
}
831
+ function _event_gamepadConnect ( e )
832
+ {
833
+ console . log (
834
+ "Gamepad connected at index %d: %s. %d buttons, %d axes." ,
835
+ e . gamepad . index ,
836
+ e . gamepad . id ,
837
+ e . gamepad . buttons . length ,
838
+ e . gamepad . axes . length ,
839
+ ) ;
840
+ love . _update_joystick ( ) ;
841
+ }
842
+ function _event_gamepadDisconnect ( e )
843
+ {
844
+ console . log (
845
+ "Gamepad disconnected from index %d: %s" ,
846
+ e . gamepad . index ,
847
+ e . gamepad . id ,
848
+ ) ;
849
+ love . _update_joystick ( ) ;
850
+ }
766
851
// load event
767
852
if ( LOVE2D_KEYBOARD == true ) {
768
853
document . addEventListener ( 'keydown' , _event_onkeydown ) ;
@@ -781,6 +866,10 @@ if (LOVE2D_TOUCH == true) {
781
866
document . addEventListener ( "touchcancel" , _event_ontouchend , false ) ;
782
867
_disableRightClickMenu ( ) ;
783
868
}
869
+ if ( LOVE2D_GAMEPAD == true ) {
870
+ window . addEventListener ( "gamepadconnected" , _event_gamepadConnect ) ;
871
+ window . addEventListener ( "gamepaddisconnected" , _event_gamepadDisconnect ) ;
872
+ }
784
873
window . requestAnimationFrame ( _main_loop ) ;
785
874
export { love , math_random } ;
786
875
0 commit comments