Skip to content

Commit fe29ec8

Browse files
committed
Fixed lots of issues
Also made refresh_input default to false.
1 parent 6cf7764 commit fe29ec8

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

nx/controllers.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
from .utils import bit, cached_property
55

66

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+
715
def _determine_controller_type(player):
816
# TODO determine the type of the controller for this player via _nx
917
return DualJoyconController
@@ -187,18 +195,17 @@ class SwitchProController(StandardController):
187195

188196
class DualJoyconController(StandardController):
189197
"""Represents two Joy-Cons in combination, attached to rails"""
198+
is_attached = True
199+
190200
def __init__(self, player):
191201
super().__init__(player)
192-
self.is_attached = True
193202
self.left_joycon = JoyconController(player, is_left=True, parent=self)
194203
self.right_joycon = JoyconController(player, is_left=False, parent=self)
195204

196205

197206
class FreeDualJoyconController(DualJoyconController):
198207
"""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
202209

203210

204211
class Button:
@@ -242,7 +249,7 @@ def __init__(self, *buttons):
242249

243250
@property
244251
def pressed(self):
245-
return which_pressed(self.player, self.buttons)
252+
return which_pressed(self.player, *self.buttons)
246253

247254

248255
class Stick:
@@ -301,7 +308,6 @@ def x(self):
301308
:return: The float value of the stick's x location.
302309
:rtype: float
303310
"""
304-
_nx.hid_scan_input()
305311
keys_pressed = _nx.hid_keys_down(self.player.number - 1)
306312
if keys_pressed & self.left_key_bit:
307313
return -1.0
@@ -317,7 +323,6 @@ def y(self):
317323
:return: The float value of the stick's y location.
318324
:rtype: float
319325
"""
320-
_nx.hid_scan_input()
321326
keys_pressed = _nx.hid_keys_down(self.player.number - 1)
322327
if keys_pressed & self.up_key_bit:
323328
return 1.0
@@ -326,7 +331,7 @@ def y(self):
326331
return 0.0
327332

328333

329-
def any_pressed(player, *buttons: Button, refresh_input=True):
334+
def any_pressed(player, *buttons: Button, refresh_input=False):
330335
"""Checks if any of the given buttons are pressed, or if
331336
any buttons are pressed at all in case no buttons are given.
332337
@@ -339,10 +344,10 @@ def any_pressed(player, *buttons: Button, refresh_input=True):
339344
refresh_input: Optional[bool]
340345
Whether or not to check for new inputs.
341346
Checks with inputs from last refresh if False.
342-
Defaults to True.
347+
Defaults to False.
343348
"""
344349
if refresh_input:
345-
_nx.hid_scan_input()
350+
refresh_inputs()
346351
keys_pressed = _nx.hid_keys_down(player.number - 1)
347352
if len(buttons) == 0:
348353
return not keys_pressed == 0
@@ -353,7 +358,7 @@ def any_pressed(player, *buttons: Button, refresh_input=True):
353358
return False
354359

355360

356-
def is_pressed(player, button: Button, refresh_input=True):
361+
def is_pressed(player, button: Button, refresh_input=False):
357362
"""Checks if any of the given buttons are pressed, or if
358363
any buttons are pressed at all in case no buttons are given.
359364
@@ -366,12 +371,12 @@ def is_pressed(player, button: Button, refresh_input=True):
366371
refresh_input: Optional[bool]
367372
Whether or not to check for new inputs.
368373
Checks with inputs from last refresh if False.
369-
Defaults to True.
374+
Defaults to False.
370375
"""
371376
return any_pressed(player, button, refresh_input=refresh_input)
372377

373378

374-
def which_pressed(player, *buttons: Button, refresh_input=True):
379+
def which_pressed(player, *buttons: Button, refresh_input=False):
375380
"""Checks which of the given buttons are pressed.
376381
377382
Parameters
@@ -383,7 +388,7 @@ def which_pressed(player, *buttons: Button, refresh_input=True):
383388
refresh_input: Optional[bool]
384389
Whether or not to check for new inputs.
385390
Checks with inputs from last refresh if False.
386-
Defaults to True.
391+
Defaults to False.
387392
388393
Returns
389394
-------
@@ -394,4 +399,12 @@ def which_pressed(player, *buttons: Button, refresh_input=True):
394399
keys_pressed = _nx.hid_keys_down(player.number - 1)
395400
if not buttons:
396401
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

nx/filesystem.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,11 @@ def mount(self):
142142
if self.is_mounted:
143143
return
144144
if self.user is None:
145-
raise users.NoActiveUser("No active user, you need to launch and "
146-
"close a game prior to launching HBL.")
145+
raise RuntimeError("No active user, you need to launch and "
146+
"close a game prior to launching HBL.")
147147
_nx.fs_mount_savedata('save', self.title.id, self.user.id)
148+
global mounted_savedata # TODO: consider not using globals
149+
mounted_savedata = self
148150

149151
def commit(self):
150152
"""Commits the savedata filesystem."""

nx/players.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def __getattr__(self, item):
4141
except AttributeError:
4242
return None
4343

44-
def any_pressed(self, *buttons, refresh_input=True):
44+
def any_pressed(self, *buttons, refresh_input=False):
4545
return _any_pressed(self, *buttons, refresh_input=refresh_input)
4646

4747

test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99

1010
def main():
11-
nx.filesystem.SAVEDATA_BASE_PATH = ''
12-
nx.filesystem.ROMFS_BASE_PATH = ''
11+
nx.filesystem.SAVEDATA_BASE_PATH = '../save/'
12+
nx.filesystem.ROMFS_BASE_PATH = '../romfs/'
1313

1414
botw = nx.titles[BOTW_TITLE_ID] # get botw Title object
1515

0 commit comments

Comments
 (0)