@kmamal/sdl 0.5.2 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,120 @@
1
+ const Bindings = require('../bindings')
2
+ const Enums = require('../enums')
3
+ const Globals = require('../globals')
4
+ const { EventsViaPoll } = require('../events/events-via-poll')
5
+
6
+ const validEvents = [
7
+ 'axisMotion',
8
+ 'ballMotion',
9
+ 'buttonDown',
10
+ 'buttonUp',
11
+ 'hatMotion',
12
+ 'close',
13
+ ]
14
+
15
+ class JoystickInstance extends EventsViaPoll {
16
+ constructor (device) {
17
+ super(validEvents)
18
+
19
+ const result = Bindings.joystick_open(device._index)
20
+
21
+ this._firmwareVersion = result.firmwareVersion
22
+ this._serialNumber = result.serialNumber
23
+ this._hasLed = result.hasLed
24
+ this._hasRumble = result.hasRumble
25
+ this._hasRumbleTriggers = result.hasRumbleTriggers
26
+ this._axes = result.axes
27
+ this._balls = result.balls
28
+ this._buttons = result.buttons
29
+ this._hats = result.hats.map((x) => Enums.hatPositionNames[x])
30
+
31
+ this._device = device
32
+
33
+ this._closed = false
34
+
35
+ Globals.joystickInstances.all.add(this)
36
+ let collection = Globals.joystickInstances.byId.get(this._device.id)
37
+ if (!collection) {
38
+ collection = new Set()
39
+ Globals.joystickInstances.byId.set(this._device.id, collection)
40
+ }
41
+ collection.add(this)
42
+ }
43
+
44
+ get device () { return this._device }
45
+ get firmwareVersion () { return this._firmwareVersion }
46
+ get serialNumber () { return this._serialNumber }
47
+
48
+ get axes () { return this._axes }
49
+ get balls () { return this._balls }
50
+ get buttons () { return this._buttons }
51
+ get hats () { return this._hats }
52
+
53
+ get power () {
54
+ const power = Bindings.joystick_getPower(this._device.id)
55
+ return Enums.powerLevelNames[power]
56
+ }
57
+
58
+ get hasLed () { return this._hasLed }
59
+ setLed (red, green, blue) {
60
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
61
+
62
+ if (!Number.isFinite(red)) { throw Object.assign(new Error("red must be a number"), { red }) }
63
+ if (red < 0 || red > 1) { throw Object.assign(new Error("red must be between 0 and 1"), { red }) }
64
+ if (!Number.isFinite(green)) { throw Object.assign(new Error("green must be a number"), { green }) }
65
+ if (green < 0 || green > 1) { throw Object.assign(new Error("green must be between 0 and 1"), { green }) }
66
+ if (!Number.isFinite(blue)) { throw Object.assign(new Error("blue must be a number"), { blue }) }
67
+ if (blue < 0 || blue > 1) { throw Object.assign(new Error("blue must be between 0 and 1"), { blue }) }
68
+
69
+ Bindings.joystick_rumble(this._device.id, red, green, blue)
70
+ }
71
+
72
+ get hasRumble () { return this._hasRumble }
73
+ rumble (lowFreqRumble = 1, highFreqRumble = 1, duration = 1e3) {
74
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
75
+
76
+ if (!Number.isFinite(lowFreqRumble)) { throw Object.assign(new Error("lowFreqRumble must be a number"), { lowFreqRumble }) }
77
+ if (lowFreqRumble < 0 || lowFreqRumble > 1) { throw Object.assign(new Error("lowFreqRumble must be between 0 and 1"), { lowFreqRumble }) }
78
+ if (!Number.isFinite(highFreqRumble)) { throw Object.assign(new Error("highFreqRumble must be a number"), { highFreqRumble }) }
79
+ if (highFreqRumble < 0 || highFreqRumble > 1) { throw Object.assign(new Error("highFreqRumble must be between 0 and 1"), { highFreqRumble }) }
80
+ if (!Number.isFinite(duration)) { throw Object.assign(new Error("duration must be a number"), { duration }) }
81
+
82
+ Bindings.joystick_rumble(this._device.id, lowFreqRumble, highFreqRumble, duration)
83
+ }
84
+
85
+ stopRumble () { this.rumble(0, 0) }
86
+
87
+ get hasRumbleTriggers () { return this._hasRumbleTriggers }
88
+ rumbleTriggers (leftRumble = 1, rightRumble = 1, duration = 1e3) {
89
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
90
+
91
+ if (!Number.isFinite(leftRumble)) { throw Object.assign(new Error("leftRumble must be a number"), { leftRumble }) }
92
+ if (leftRumble < 0 || leftRumble > 1) { throw Object.assign(new Error("leftRumble must be between 0 and 1"), { leftRumble }) }
93
+ if (!Number.isFinite(rightRumble)) { throw Object.assign(new Error("rightRumble must be a number"), { rightRumble }) }
94
+ if (rightRumble < 0 || rightRumble > 1) { throw Object.assign(new Error("rightRumble must be between 0 and 1"), { rightRumble }) }
95
+ if (!Number.isFinite(duration)) { throw Object.assign(new Error("duration must be a number"), { duration }) }
96
+
97
+ Bindings.joystick_rumbleTriggers(this._device.id, leftRumble, rightRumble, duration)
98
+ }
99
+
100
+ stopRumbleTriggers () { this.rumbleTriggers(0, 0) }
101
+
102
+ get closed () { return this._closed }
103
+ close () {
104
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
105
+
106
+ this.emit('close')
107
+ this.removeAllListeners()
108
+ this._closed = true
109
+
110
+ Globals.joystickInstances.all.delete(this)
111
+ const collection = Globals.joystickInstances.byId.get(this._device.id)
112
+ collection.delete(this)
113
+ if (collection.size === 0) {
114
+ Bindings.joystick_close(this._device.id)
115
+ Globals.joystickInstances.byId.delete(this._device.id)
116
+ }
117
+ }
118
+ }
119
+
120
+ module.exports = { JoystickInstance }
@@ -1,22 +1,22 @@
1
1
  const Bindings = require('../bindings')
2
- const { enums } = require('../enums')
2
+ const Enums = require('../enums')
3
3
  const { mapping, reverseMapping } = require('./key-mapping')
4
4
 
5
5
  const keyboard = {
6
- get SCANCODE () { return enums.scancode },
6
+ get SCANCODE () { return Enums.scancode },
7
7
 
8
8
  getKey (scancode) {
9
9
  if (!Number.isFinite(scancode)) { throw Object.assign(new Error("scancode must be a number"), { scancode }) }
10
10
  if (scancode < 0 || scancode >= 512) { throw Object.assign(new Error("invalid scancode"), { scancode }) }
11
11
 
12
12
  const _key = Bindings.keyboard_getKey(scancode)
13
- return mapping[_key] ?? (_key && Array.from(_key).length === 1 ? _key : null)
13
+ return mapping[_key] ?? _key?.length === 1 ? _key : null
14
14
  },
15
15
 
16
16
  getScancode (key) {
17
17
  if (typeof key !== 'string') { throw Object.assign(new Error("key must be a string"), { key }) }
18
18
 
19
- const _key = reverseMapping[key] ?? (Array.from(key).length === 1 ? key : null)
19
+ const _key = reverseMapping[key] ?? key.length === 1 ? key : null
20
20
  if (_key === null) { throw Object.assign(new Error("invalid key"), { key }) }
21
21
 
22
22
  return Bindings.keyboard_getScancode(_key)
@@ -1,8 +1,8 @@
1
1
  const Bindings = require('../bindings')
2
- const { enums } = require('../enums')
2
+ const Enums = require('../enums')
3
3
 
4
4
  const mouse = {
5
- get BUTTON () { return enums.button },
5
+ get BUTTON () { return Enums.button },
6
6
 
7
7
  getButton (button) {
8
8
  if (!Number.isFinite(button)) { throw Object.assign(new Error("button must be a number"), { button }) }
@@ -27,7 +27,7 @@ const mouse = {
27
27
  },
28
28
 
29
29
  setCursor (cursor) {
30
- const _cursor = enums.cursor[cursor]
30
+ const _cursor = Enums.cursor[cursor]
31
31
  if (typeof cursor !== 'string') { throw Object.assign(new Error("cursor must be a string"), { cursor }) }
32
32
  if (!_cursor) { throw Object.assign(new Error("invalid cursor"), { cursor }) }
33
33
 
@@ -35,7 +35,7 @@ const mouse = {
35
35
  },
36
36
 
37
37
  setCursorImage (width, height, stride, format, buffer, x, y) {
38
- const _format = enums.pixelFormat[format] ?? null
38
+ const _format = Enums.pixelFormat[format] ?? null
39
39
 
40
40
  if (!Number.isFinite(width)) { throw Object.assign(new Error("width must be a number"), { width }) }
41
41
  if (width <= 0) { throw Object.assign(new Error("invalid width"), { width }) }
@@ -1,13 +1,13 @@
1
1
  const Bindings = require('../bindings')
2
2
  const Globals = require('../globals')
3
- const { enums } = require('../enums')
3
+ const Enums = require('../enums')
4
4
  const { Window } = require('./window')
5
5
 
6
6
  const video = {
7
7
  get displays () {
8
8
  const displays = Bindings.video_getDisplays()
9
9
  for (const display of displays) {
10
- display.format = enums.pixelFormatNames[display.format]
10
+ display.format = Enums.pixelFormatNames[display.format]
11
11
  }
12
12
  return displays
13
13
  },
@@ -1,6 +1,6 @@
1
1
  const Bindings = require('../bindings')
2
2
  const Globals = require('../globals')
3
- const { enums } = require('../enums')
3
+ const Enums = require('../enums')
4
4
  const { EventsViaPoll } = require('../events/events-via-poll')
5
5
  const { maybeTriggerQuit } = require('../events/quit')
6
6
 
@@ -106,7 +106,7 @@ class Window extends EventsViaPoll {
106
106
  this._borderless = result.borderless
107
107
  this._accelerated = result.accelerated
108
108
  this._vsync = result.vsync
109
- this._native = result.native
109
+ this._native = result.native ?? null
110
110
 
111
111
  this._title = title
112
112
  this._visible = visible
@@ -120,10 +120,12 @@ class Window extends EventsViaPoll {
120
120
 
121
121
  Globals.windows.all.set(this._id, this)
122
122
 
123
- // Keep node.js alive while windows are open
124
- this.once('close', () => {})
123
+ // This also keeps Node.js alive while windows are open
124
+ this.on('close', () => {
125
+ this.removeAllListeners()
126
+ })
125
127
 
126
- // Manually emit an initial resize event (for convenience)
128
+ // Manually emit an initial resize event for convenience
127
129
  process.nextTick(() => {
128
130
  this.emit('resize', {
129
131
  width: this._width,
@@ -280,7 +282,7 @@ class Window extends EventsViaPoll {
280
282
 
281
283
  if (this._opengl) { throw new Error("can't call render in opengl mode") }
282
284
 
283
- const _format = enums.pixelFormat[format] ?? null
285
+ const _format = Enums.pixelFormat[format] ?? null
284
286
 
285
287
  if (!Number.isFinite(width)) { throw Object.assign(new Error("width must be a number"), { width }) }
286
288
  if (width <= 0) { throw Object.assign(new Error("invalid width"), { width }) }
@@ -299,7 +301,7 @@ class Window extends EventsViaPoll {
299
301
  setIcon (width, height, stride, format, buffer) {
300
302
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
301
303
 
302
- const _format = enums.pixelFormat[format] ?? null
304
+ const _format = Enums.pixelFormat[format] ?? null
303
305
 
304
306
  if (!Number.isFinite(width)) { throw Object.assign(new Error("width must be a number"), { width }) }
305
307
  if (width <= 0) { throw Object.assign(new Error("invalid width"), { width }) }