diff --git a/markdown/bitburner.userinterface.getstyles.md b/markdown/bitburner.userinterface.getstyles.md new file mode 100644 index 000000000..90b371b6f --- /dev/null +++ b/markdown/bitburner.userinterface.getstyles.md @@ -0,0 +1,23 @@ + + +[Home](./index.md) > [bitburner](./bitburner.md) > [UserInterface](./bitburner.userinterface.md) > [getStyles](./bitburner.userinterface.getstyles.md) + +## UserInterface.getStyles() method + +Get the current styles + +Signature: + +```typescript +getStyles(): IStyleSettings; +``` +Returns: + +IStyleSettings + +An object containing the player's styles + +## Remarks + +RAM cost: cost: 0 GB + diff --git a/markdown/bitburner.userinterface.md b/markdown/bitburner.userinterface.md index 93975662f..e0b74cebe 100644 --- a/markdown/bitburner.userinterface.md +++ b/markdown/bitburner.userinterface.md @@ -16,7 +16,10 @@ interface UserInterface | Method | Description | | --- | --- | +| [getStyles()](./bitburner.userinterface.getstyles.md) | Get the current styles | | [getTheme()](./bitburner.userinterface.gettheme.md) | Get the current theme | +| [resetStyles()](./bitburner.userinterface.resetstyles.md) | Resets the player's styles to the default values | | [resetTheme()](./bitburner.userinterface.resettheme.md) | Resets the player's theme to the default values | +| [setStyles(newStyles)](./bitburner.userinterface.setstyles.md) | Sets the current styles | | [setTheme(newTheme)](./bitburner.userinterface.settheme.md) | Sets the current theme | diff --git a/markdown/bitburner.userinterface.resetstyles.md b/markdown/bitburner.userinterface.resetstyles.md new file mode 100644 index 000000000..da37413b1 --- /dev/null +++ b/markdown/bitburner.userinterface.resetstyles.md @@ -0,0 +1,21 @@ + + +[Home](./index.md) > [bitburner](./bitburner.md) > [UserInterface](./bitburner.userinterface.md) > [resetStyles](./bitburner.userinterface.resetstyles.md) + +## UserInterface.resetStyles() method + +Resets the player's styles to the default values + +Signature: + +```typescript +resetStyles(): void; +``` +Returns: + +void + +## Remarks + +RAM cost: cost: 0 GB + diff --git a/markdown/bitburner.userinterface.setstyles.md b/markdown/bitburner.userinterface.setstyles.md new file mode 100644 index 000000000..0388081e9 --- /dev/null +++ b/markdown/bitburner.userinterface.setstyles.md @@ -0,0 +1,38 @@ + + +[Home](./index.md) > [bitburner](./bitburner.md) > [UserInterface](./bitburner.userinterface.md) > [setStyles](./bitburner.userinterface.setstyles.md) + +## UserInterface.setStyles() method + +Sets the current styles + +Signature: + +```typescript +setStyles(newStyles: IStyleSettings): void; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| newStyles | IStyleSettings | | + +Returns: + +void + +## Remarks + +RAM cost: cost: 0 GB + +## Example + +Usage example (NS2) + +```ts +const styles = ns.ui.getStyles(); +styles.fontFamily = 'Comic Sans Ms'; +ns.ui.setStyles(styles); +``` + diff --git a/src/Netscript/RamCostGenerator.ts b/src/Netscript/RamCostGenerator.ts index af8a34462..c026bd642 100644 --- a/src/Netscript/RamCostGenerator.ts +++ b/src/Netscript/RamCostGenerator.ts @@ -364,6 +364,9 @@ export const RamCosts: IMap = { getTheme: 0, setTheme: 0, resetTheme: 0, + getStyles: 0, + setStyles: 0, + resetStyles: 0, }, heart: { diff --git a/src/NetscriptFunctions/UserInterface.ts b/src/NetscriptFunctions/UserInterface.ts index 466479277..ca5a581cc 100644 --- a/src/NetscriptFunctions/UserInterface.ts +++ b/src/NetscriptFunctions/UserInterface.ts @@ -2,10 +2,11 @@ import { INetscriptHelper } from "./INetscriptHelper"; import { WorkerScript } from "../Netscript/WorkerScript"; import { IPlayer } from "../PersonObjects/IPlayer"; import { getRamCost } from "../Netscript/RamCostGenerator"; -import { UserInterface as IUserInterface, UserInterfaceTheme } from "../ScriptEditor/NetscriptDefinitions"; +import { IStyleSettings, UserInterface as IUserInterface, UserInterfaceTheme } from "../ScriptEditor/NetscriptDefinitions"; import { Settings } from "../Settings/Settings"; import { ThemeEvents } from "../ui/React/Theme"; import { defaultTheme } from "../Settings/Themes"; +import { defaultStyles } from "../Settings/Styles"; export function NetscriptUserInterface( player: IPlayer, @@ -18,6 +19,11 @@ export function NetscriptUserInterface( return { ...Settings.theme }; }, + getStyles: function (): IStyleSettings { + helper.updateDynamicRam("getStyles", getRamCost(player, "ui", "getStyles")); + return { ...Settings.styles }; + }, + setTheme: function (newTheme: UserInterfaceTheme): void { helper.updateDynamicRam("setTheme", getRamCost(player, "ui", "setTheme")); const hex = /^(#)((?:[A-Fa-f0-9]{3}){1,2})$/; @@ -43,11 +49,41 @@ export function NetscriptUserInterface( } }, + setStyles: function (newStyles: IStyleSettings): void { + helper.updateDynamicRam("setStyles", getRamCost(player, "ui", "setStyles")); + + const currentStyles = {...Settings.styles} + const errors: string[] = []; + for (const key of Object.keys(newStyles)) { + if (!((currentStyles as any)[key])) { + // Invalid key + errors.push(`Invalid key "${key}"`); + } else { + (currentStyles as any)[key] = (newStyles as any)[key]; + } + } + + if (errors.length === 0) { + Object.assign(Settings.styles, currentStyles); + ThemeEvents.emit(); + workerScript.log("ui.setStyles", () => `Successfully set styles`); + } else { + workerScript.log("ui.setStyles", () => `Failed to set styles. Errors: ${errors.join(', ')}`); + } + }, + resetTheme: function (): void { helper.updateDynamicRam("resetTheme", getRamCost(player, "ui", "resetTheme")); - Settings.theme = defaultTheme; + Settings.theme = { ...defaultTheme }; ThemeEvents.emit(); workerScript.log("ui.resetTheme", () => `Reinitialized theme to default`); }, + + resetStyles: function (): void { + helper.updateDynamicRam("resetStyles", getRamCost(player, "ui", "resetStyles")); + Settings.styles = { ...defaultStyles }; + ThemeEvents.emit(); + workerScript.log("ui.resetStyles", () => `Reinitialized styles to default`); + } } } diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 99068487a..0cfd5bb64 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -1,3 +1,5 @@ +import React from 'react'; + /** * @public */ @@ -119,7 +121,7 @@ export interface IPort { /** add data to port if not full. * @returns true if added and false if full and not added */ tryWrite: (value: any) => boolean; - /** reads and removes first element from port + /** reads and removes first element from port * if no data in port returns "NULL PORT DATA" */ read: () => any; @@ -3880,6 +3882,37 @@ interface UserInterface { * RAM cost: cost: 0 GB */ resetTheme(): void; + + + /** + * Get the current styles + * @remarks + * RAM cost: cost: 0 GB + * + * @returns An object containing the player's styles + */ + getStyles(): IStyleSettings; + + /** + * Sets the current styles + * @remarks + * RAM cost: cost: 0 GB + * @example + * Usage example (NS2) + * ```ts + * const styles = ns.ui.getStyles(); + * styles.fontFamily = 'Comic Sans Ms'; + * ns.ui.setStyles(styles); + * ``` + */ + setStyles(newStyles: IStyleSettings): void; + + /** + * Resets the player's styles to the default values + * @remarks + * RAM cost: cost: 0 GB + */ + resetStyles(): void; } /** @@ -6354,3 +6387,12 @@ interface UserInterfaceTheme { backgroundsecondary: string; button: string; } + +/** + * Interface Styles + * @internal + */ +interface IStyleSettings { + fontFamily: React.CSSProperties["fontFamily"]; + lineHeight: React.CSSProperties["lineHeight"]; +} diff --git a/src/Settings/Settings.ts b/src/Settings/Settings.ts index fe27b0def..f524694f1 100644 --- a/src/Settings/Settings.ts +++ b/src/Settings/Settings.ts @@ -1,9 +1,10 @@ import { ISelfInitializer, ISelfLoading } from "../types"; import { OwnedAugmentationsOrderSetting, PurchaseAugmentationsOrderSetting } from "./SettingEnums"; import { defaultTheme, ITheme } from "./Themes"; -import { defaultStyles, IStyleSettings } from "./Styles"; +import { defaultStyles } from "./Styles"; import { WordWrapOptions } from "../ScriptEditor/ui/Options"; import { OverviewSettings } from "../ui/React/Overview"; +import { IStyleSettings } from "../ScriptEditor/NetscriptDefinitions"; /** * Represents the default settings the player could customize. diff --git a/src/Settings/Styles.ts b/src/Settings/Styles.ts index f231d9237..ef4f97ab2 100644 --- a/src/Settings/Styles.ts +++ b/src/Settings/Styles.ts @@ -1,9 +1,4 @@ -import React from "react"; - -export interface IStyleSettings { - fontFamily: React.CSSProperties["fontFamily"]; - lineHeight: React.CSSProperties["lineHeight"]; -} +import { IStyleSettings } from "../ScriptEditor/NetscriptDefinitions"; export const defaultStyles: IStyleSettings = { lineHeight: 1.5, diff --git a/src/ui/React/StyleEditorModal.tsx b/src/ui/React/StyleEditorModal.tsx index bd5d6820d..276f37252 100644 --- a/src/ui/React/StyleEditorModal.tsx +++ b/src/ui/React/StyleEditorModal.tsx @@ -11,8 +11,9 @@ import SaveIcon from '@mui/icons-material/Save'; import { ThemeEvents } from "./Theme"; import { Settings } from "../../Settings/Settings"; -import { IStyleSettings, defaultStyles } from "../../Settings/Styles"; +import { defaultStyles } from "../../Settings/Styles"; import { Tooltip } from "@mui/material"; +import { IStyleSettings } from "../../ScriptEditor/NetscriptDefinitions"; interface IProps { open: boolean;