Skip to content

Commit 69a6abe

Browse files
committed
b0.6.3
1 parent 70cbb13 commit 69a6abe

File tree

1 file changed

+91
-2
lines changed

1 file changed

+91
-2
lines changed

js/love2d.js

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*Copyright (c) 2024 oblerion
1+
/*Copyright (c) 2025 oblerion
22
33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
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,
1818
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
SOFTWARE.
2020
*/
21-
const LOVE2D_VERSION = "b0.6.2-2"
21+
const LOVE2D_VERSION = "b0.6.3"
2222
const LOVE2D_MOUSE = true;
2323
const LOVE2D_KEYBOARD = true;
2424
const LOVE2D_TOUCH = true;
25+
const LOVE2D_GAMEPAD = true;
2526

2627
function _sizeOf(value)
2728
{
@@ -60,6 +61,50 @@ function collide(a, b) {
6061
return true;
6162
return false;
6263
}
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+
63108
class Love
64109
{
65110
constructor()
@@ -96,7 +141,27 @@ class Love
96141

97142
this.lastUpdate = 0;
98143
this.loading = 0;
144+
145+
this.joysticks = [];
99146
}
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+
}
100165
system_writeSave(name,val)
101166
{
102167
if(typeof(name)=="string" &&
@@ -763,6 +828,26 @@ function _event_ontouchend(e) {
763828
}
764829
}
765830
}
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+
}
766851
// load event
767852
if (LOVE2D_KEYBOARD == true) {
768853
document.addEventListener('keydown', _event_onkeydown);
@@ -781,6 +866,10 @@ if (LOVE2D_TOUCH == true) {
781866
document.addEventListener("touchcancel", _event_ontouchend, false);
782867
_disableRightClickMenu();
783868
}
869+
if(LOVE2D_GAMEPAD == true){
870+
window.addEventListener("gamepadconnected",_event_gamepadConnect);
871+
window.addEventListener("gamepaddisconnected",_event_gamepadDisconnect);
872+
}
784873
window.requestAnimationFrame(_main_loop);
785874
export {love,math_random};
786875

0 commit comments

Comments
 (0)