-
Notifications
You must be signed in to change notification settings - Fork 0
Menus
Doing similar logic to the Buttons, abstract menus can be found in the 'menus.py' file. A menu is a collection of buttons, displayed on a specific background surface, with a specific display center.
Selection finding is done iterating through each button, telling each one which one is selected. It moves only if the selection matches.
# snippet from 'menus.py'
def move(self, direction: Dpad | None):
for button in self.buttons: # for each button, checks if it's the selected one, then moves to it
next = button.move(direction)
if next is not None:
return next
return None
In the button hierarchy, the dynamic button's pointing button has priority in front of the directional links. We account this into this update loop:
# snippet from 'menus.py'
def updateSelections(self, direction: Dpad | None) -> Selected:
FINAL = None
for button in self.buttons:
move_to = button.move(direction)
try:
next = button.getNext() # if it's a dynamic button and is pressed
if next is not None: # get the button linked with the press
FINAL = next
except: pass
if move_to is not None: # otherwise, if you detect movement on the current button
FINAL = move_to # get the linked button for the specific direction
return FINAL
All the submenus are managed by the main menu class from the 'main.py' file. This class gets the keyboard input key and the joystick button presses to pass as parameters to the submenus and subsequently to the buttons. The update loop can be seen in the following snippet:
# snippet from 'main.py'
def onScreen(self, screen: pygame.Surface):
self.clicked = self.controls.joystick_detector[self.controls.keybinds.zero_button].rising
self.default = self.controls.joystick_detector[self.controls.keybinds.erase_trail_button].rising
key = self.__updatePressedDpad()
moved = False
for menu in self.menus: # loop through each menu
menu.update(self.selected, self.clicked, default = self.default, value = self.value)
toggles = menu.getToggles()
for item in toggles: # enable all the menus the toggle buttons say you to do
for each in self.menus:
if item[0] is each.name:
each.ENABLED.set(item[1])
next = menu.updateSelections(key) # gets the next button to move to
if next is not None and not moved: # you can move only once / loop, because otherwise things go boom
if key is not None:
inverse = Controls.Keybinds.inverse(key)
for each in self.menus: # check in all the menus, in all the buttons
for button in each.buttons: # the desired next button, to see if has to remember the move
if button.remember_links[inverse][0] and button.name is next:
try:
if not button.on()[1]:
raise("wise words here")
except: button.remember_links[inverse][1] = self.selected
self.selected = next
self.enable() # update menus
moved = True
menu.onScreen(screen)