TSDoc ready + less lengthy comments

This commit is contained in:
Greenlamp 2024-05-10 15:11:09 +02:00
parent b0a34d2b1e
commit 7fc6834f92

View File

@ -120,14 +120,25 @@ export default class SettingsUiHandler extends UiHandler {
return true; return true;
} }
/**
* Processes input from a specified button.
* This method handles navigation through a UI menu, including movement through menu items
* and handling special actions like cancellation. Each button press may adjust the cursor
* position or the menu scroll, and plays a sound effect if the action was successful.
*
* @param button - The button pressed by the user.
* @returns `true` if the action associated with the button was successfully processed, `false` otherwise.
*/
processInput(button: Button): boolean { processInput(button: Button): boolean {
const ui = this.getUi(); const ui = this.getUi();
const rowsToDisplay = 9; // we can fit 9 rows on the screen // Defines the maximum number of rows that can be displayed on the screen.
const rowsToDisplay = 9;
let success = false; let success = false;
if (button === Button.CANCEL) { if (button === Button.CANCEL) {
success = true; success = true;
// Reverts UI to its previous state on cancel.
this.scene.ui.revertMode(); this.scene.ui.revertMode();
} else { } else {
const cursor = this.cursor + this.scrollCursor; const cursor = this.cursor + this.scrollCursor;
@ -139,14 +150,10 @@ export default class SettingsUiHandler extends UiHandler {
else else
success = this.setScrollCursor(this.scrollCursor - 1); success = this.setScrollCursor(this.scrollCursor - 1);
} else { } else {
// we land here if cursor is equal to 0, it means we are at the top of the menu. // When at the top of the menu and pressing UP, move to the bottommost item.
// so by clicking on UP, we want to go to the bottom // First, set the cursor to the last visible element, preparing for the scroll to the end.
// to do that, first, we say, on the screen, we want the lastest element visible to be selected
// we do that with the setCursor method
const successA = this.setCursor(rowsToDisplay - 1); const successA = this.setCursor(rowsToDisplay - 1);
// if we stop here, our cursor will be on the 9th element of the menu. but we want the latest one // Then, adjust the scroll to display the bottommost elements of the menu.
// to do so, we fix the scroll to the bottom
// the scrolls go from 0 to the amount of element minus how many element we can fit on the screen
const successB = this.setScrollCursor(this.optionValueLabels.length - rowsToDisplay); const successB = this.setScrollCursor(this.optionValueLabels.length - rowsToDisplay);
success = successA && successB; // success is just there to play the little validation sound effect success = successA && successB; // success is just there to play the little validation sound effect
} }
@ -158,30 +165,27 @@ export default class SettingsUiHandler extends UiHandler {
else if (this.scrollCursor < this.optionValueLabels.length - rowsToDisplay) else if (this.scrollCursor < this.optionValueLabels.length - rowsToDisplay)
success = this.setScrollCursor(this.scrollCursor + 1); success = this.setScrollCursor(this.scrollCursor + 1);
} else { } else {
// we land here if cursor is equal to how many element there is in the menu. // When at the bottom of the menu and pressing DOWN, move to the topmost item.
// why is cursor = this.cursor + this.scrollCursor ? Remember: // First, set the cursor to the first visible element, resetting the scroll to the top.
// the scrolls go from 0 to the amount of element minus how many element we can fit on the screen
// this.cursor go rom 0 to how many element we can fit on the screen
// the max combination of both of them, give the latest element
// so by clicking on DOWN, we want to go to the top
// to do that, first, we say, on the screen, we want the first element visible to be selected
const successA = this.setCursor(0); const successA = this.setCursor(0);
// and then we set the scroll to the minimum -> 0 // Then, reset the scroll to start from the first element of the menu.
const successB = this.setScrollCursor(0); const successB = this.setScrollCursor(0);
success = successA && successB; // success is just there to play the little validation sound effect success = successA && successB; // Indicates a successful cursor and scroll adjustment.
} }
break; break;
case Button.LEFT: case Button.LEFT:
if (this.optionCursors[cursor]) if (this.optionCursors[cursor]) // Moves the option cursor left, if possible.
success = this.setOptionCursor(cursor, this.optionCursors[cursor] - 1, true); success = this.setOptionCursor(cursor, this.optionCursors[cursor] - 1, true);
break; break;
case Button.RIGHT: case Button.RIGHT:
// Moves the option cursor right, if possible.
if (this.optionCursors[cursor] < this.optionValueLabels[cursor].length - 1) if (this.optionCursors[cursor] < this.optionValueLabels[cursor].length - 1)
success = this.setOptionCursor(cursor, this.optionCursors[cursor] + 1, true); success = this.setOptionCursor(cursor, this.optionCursors[cursor] + 1, true);
break; break;
} }
} }
// Plays a select sound effect if an action was successfully processed.
if (success) if (success)
ui.playSelect(); ui.playSelect();