4
4
from .utils import bit , cached_property
5
5
6
6
7
+ def refresh_inputs ():
8
+ """Refreshes inputs.
9
+ Should normally be called at least once
10
+ within every iteration of your main loop.
11
+ """
12
+ _nx .hid_scan_input ()
13
+
14
+
7
15
def _determine_controller_type (player ):
8
16
# TODO determine the type of the controller for this player via _nx
9
17
return DualJoyconController
@@ -187,18 +195,17 @@ class SwitchProController(StandardController):
187
195
188
196
class DualJoyconController (StandardController ):
189
197
"""Represents two Joy-Cons in combination, attached to rails"""
198
+ is_attached = True
199
+
190
200
def __init__ (self , player ):
191
201
super ().__init__ (player )
192
- self .is_attached = True
193
202
self .left_joycon = JoyconController (player , is_left = True , parent = self )
194
203
self .right_joycon = JoyconController (player , is_left = False , parent = self )
195
204
196
205
197
206
class FreeDualJoyconController (DualJoyconController ):
198
207
"""Represents two Joy-Cons in combination, detached from rails"""
199
- def __init__ (self , player ):
200
- super ().__init__ (player )
201
- self .is_attached = False
208
+ is_attached = False
202
209
203
210
204
211
class Button :
@@ -242,7 +249,7 @@ def __init__(self, *buttons):
242
249
243
250
@property
244
251
def pressed (self ):
245
- return which_pressed (self .player , self .buttons )
252
+ return which_pressed (self .player , * self .buttons )
246
253
247
254
248
255
class Stick :
@@ -301,7 +308,6 @@ def x(self):
301
308
:return: The float value of the stick's x location.
302
309
:rtype: float
303
310
"""
304
- _nx .hid_scan_input ()
305
311
keys_pressed = _nx .hid_keys_down (self .player .number - 1 )
306
312
if keys_pressed & self .left_key_bit :
307
313
return - 1.0
@@ -317,7 +323,6 @@ def y(self):
317
323
:return: The float value of the stick's y location.
318
324
:rtype: float
319
325
"""
320
- _nx .hid_scan_input ()
321
326
keys_pressed = _nx .hid_keys_down (self .player .number - 1 )
322
327
if keys_pressed & self .up_key_bit :
323
328
return 1.0
@@ -326,7 +331,7 @@ def y(self):
326
331
return 0.0
327
332
328
333
329
- def any_pressed (player , * buttons : Button , refresh_input = True ):
334
+ def any_pressed (player , * buttons : Button , refresh_input = False ):
330
335
"""Checks if any of the given buttons are pressed, or if
331
336
any buttons are pressed at all in case no buttons are given.
332
337
@@ -339,10 +344,10 @@ def any_pressed(player, *buttons: Button, refresh_input=True):
339
344
refresh_input: Optional[bool]
340
345
Whether or not to check for new inputs.
341
346
Checks with inputs from last refresh if False.
342
- Defaults to True .
347
+ Defaults to False .
343
348
"""
344
349
if refresh_input :
345
- _nx . hid_scan_input ()
350
+ refresh_inputs ()
346
351
keys_pressed = _nx .hid_keys_down (player .number - 1 )
347
352
if len (buttons ) == 0 :
348
353
return not keys_pressed == 0
@@ -353,7 +358,7 @@ def any_pressed(player, *buttons: Button, refresh_input=True):
353
358
return False
354
359
355
360
356
- def is_pressed (player , button : Button , refresh_input = True ):
361
+ def is_pressed (player , button : Button , refresh_input = False ):
357
362
"""Checks if any of the given buttons are pressed, or if
358
363
any buttons are pressed at all in case no buttons are given.
359
364
@@ -366,12 +371,12 @@ def is_pressed(player, button: Button, refresh_input=True):
366
371
refresh_input: Optional[bool]
367
372
Whether or not to check for new inputs.
368
373
Checks with inputs from last refresh if False.
369
- Defaults to True .
374
+ Defaults to False .
370
375
"""
371
376
return any_pressed (player , button , refresh_input = refresh_input )
372
377
373
378
374
- def which_pressed (player , * buttons : Button , refresh_input = True ):
379
+ def which_pressed (player , * buttons : Button , refresh_input = False ):
375
380
"""Checks which of the given buttons are pressed.
376
381
377
382
Parameters
@@ -383,7 +388,7 @@ def which_pressed(player, *buttons: Button, refresh_input=True):
383
388
refresh_input: Optional[bool]
384
389
Whether or not to check for new inputs.
385
390
Checks with inputs from last refresh if False.
386
- Defaults to True .
391
+ Defaults to False .
387
392
388
393
Returns
389
394
-------
@@ -394,4 +399,12 @@ def which_pressed(player, *buttons: Button, refresh_input=True):
394
399
keys_pressed = _nx .hid_keys_down (player .number - 1 )
395
400
if not buttons :
396
401
raise TypeError ("At least one Button must be passed" )
397
- return [button for button in buttons if keys_pressed & button .key_bit ]
402
+ if refresh_input :
403
+ refresh_inputs ()
404
+ keys_pressed = _nx .hid_keys_down (player .number - 1 )
405
+ buttons_pressed = []
406
+ for button in buttons :
407
+ for key_bit in button .key_bits :
408
+ if keys_pressed & key_bit :
409
+ buttons_pressed .append (button )
410
+ return buttons_pressed
0 commit comments