From 1d3418a4f86a37a58d8fd7d14b0b799cda210837 Mon Sep 17 00:00:00 2001 From: Greenlamp Date: Thu, 16 May 2024 02:11:55 +0200 Subject: [PATCH] comment TSDOC utils.ts --- src/system/settings-gamepad.ts | 2 +- .../settings/settings-keyboard-ui-handler.ts | 2 +- src/utils.ts | 26 +++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/system/settings-gamepad.ts b/src/system/settings-gamepad.ts index e835923dceb..b4208ae3c39 100644 --- a/src/system/settings-gamepad.ts +++ b/src/system/settings-gamepad.ts @@ -121,7 +121,7 @@ export function setSettingGamepad(scene: BattleScene, setting: SettingGamepad, v }; scene.ui.setOverlayMode(Mode.OPTION_SELECT, { options: [...gp.map((g) => ({ - label: truncateString(g, 30), // Truncate the gamepad name for display + label: truncateString(g, 22), // Truncate the gamepad name for display handler: () => changeGamepadHandler(g) })), { label: 'Cancel', diff --git a/src/ui/settings/settings-keyboard-ui-handler.ts b/src/ui/settings/settings-keyboard-ui-handler.ts index 8e34abaae5e..a518bac4e8b 100644 --- a/src/ui/settings/settings-keyboard-ui-handler.ts +++ b/src/ui/settings/settings-keyboard-ui-handler.ts @@ -147,7 +147,7 @@ export default class SettingsKeyboardUiHandler extends AbstractSettingsUiUiHandl if (_key === 'noKeyboard') continue; // Skip updating the no gamepad layout. // Update the text of the first option label under the current setting to the name of the chosen gamepad, // truncating the name to 30 characters if necessary. - this.layout[_key].optionValueLabels[index][0].setText(truncateString(this.scene.inputController.selectedDevice[Device.KEYBOARD], 30)); + this.layout[_key].optionValueLabels[index][0].setText(truncateString(this.scene.inputController.selectedDevice[Device.KEYBOARD], 22)); } } } diff --git a/src/utils.ts b/src/utils.ts index fc3d9f7968f..b7228fc8de5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -336,19 +336,45 @@ export function rgbaToInt(rgba: integer[]): integer { return (rgba[0] << 24) + (rgba[1] << 16) + (rgba[2] << 8) + rgba[3]; } +/** + * Truncate a string to a specified maximum length and add an ellipsis if it exceeds that length. + * + * @param str - The string to be truncated. + * @param maxLength - The maximum length of the truncated string, defaults to 10. + * @returns The truncated string with an ellipsis if it was longer than maxLength. + */ export function truncateString(str: String, maxLength: number = 10) { + // Check if the string length exceeds the maximum length if (str.length > maxLength) { + // Truncate the string and add an ellipsis return str.slice(0, maxLength - 3) + "..."; // Subtract 3 to accommodate the ellipsis } + // Return the original string if it does not exceed the maximum length return str; } +/** + * Perform a deep copy of an object. + * + * @param values - The object to be deep copied. + * @returns A new object that is a deep copy of the input. + */ export function deepCopy(values: object): object { + // Convert the object to a JSON string and parse it back to an object to perform a deep copy return JSON.parse(JSON.stringify(values)); } +/** + * Convert a space-separated string into a capitalized and underscored string. + * + * @param input - The string to be converted. + * @returns The converted string with words capitalized and separated by underscores. + */ export function reverseValueToKeySetting(input) { + // Split the input string into an array of words const words = input.split(' '); + // Capitalize the first letter of each word and convert the rest to lowercase const capitalizedWords = words.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()); + // Join the capitalized words with underscores and return the result return capitalizedWords.join('_'); } \ No newline at end of file