mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-17 21:23:54 +01:00
Add ns.ui.setTheme() and ns.ui.resetTheme()
This commit is contained in:
parent
50919a88cd
commit
c9ca1e9d37
@ -17,4 +17,6 @@ interface UserInterface
|
|||||||
| Method | Description |
|
| Method | Description |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
| [getTheme()](./bitburner.userinterface.gettheme.md) | Get the current theme |
|
| [getTheme()](./bitburner.userinterface.gettheme.md) | Get the current theme |
|
||||||
|
| [resetTheme()](./bitburner.userinterface.resettheme.md) | Resets the player's theme to the default values |
|
||||||
|
| [setTheme(newTheme)](./bitburner.userinterface.settheme.md) | Sets the current theme |
|
||||||
|
|
||||||
|
21
markdown/bitburner.userinterface.resettheme.md
Normal file
21
markdown/bitburner.userinterface.resettheme.md
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
|
||||||
|
|
||||||
|
[Home](./index.md) > [bitburner](./bitburner.md) > [UserInterface](./bitburner.userinterface.md) > [resetTheme](./bitburner.userinterface.resettheme.md)
|
||||||
|
|
||||||
|
## UserInterface.resetTheme() method
|
||||||
|
|
||||||
|
Resets the player's theme to the default values
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
resetTheme(): void;
|
||||||
|
```
|
||||||
|
<b>Returns:</b>
|
||||||
|
|
||||||
|
void
|
||||||
|
|
||||||
|
## Remarks
|
||||||
|
|
||||||
|
RAM cost: cost: 0 GB
|
||||||
|
|
38
markdown/bitburner.userinterface.settheme.md
Normal file
38
markdown/bitburner.userinterface.settheme.md
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
|
||||||
|
|
||||||
|
[Home](./index.md) > [bitburner](./bitburner.md) > [UserInterface](./bitburner.userinterface.md) > [setTheme](./bitburner.userinterface.settheme.md)
|
||||||
|
|
||||||
|
## UserInterface.setTheme() method
|
||||||
|
|
||||||
|
Sets the current theme
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
setTheme(newTheme: UserInterfaceTheme): void;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Parameters
|
||||||
|
|
||||||
|
| Parameter | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| newTheme | UserInterfaceTheme | |
|
||||||
|
|
||||||
|
<b>Returns:</b>
|
||||||
|
|
||||||
|
void
|
||||||
|
|
||||||
|
## Remarks
|
||||||
|
|
||||||
|
RAM cost: cost: 0 GB
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
Usage example (NS2)
|
||||||
|
|
||||||
|
```ts
|
||||||
|
const theme = ns.ui.getTheme();
|
||||||
|
theme.primary = '#ff5500';
|
||||||
|
ns.ui.setTheme(theme);
|
||||||
|
```
|
||||||
|
|
@ -361,6 +361,8 @@ export const RamCosts: IMap<any> = {
|
|||||||
|
|
||||||
ui: {
|
ui: {
|
||||||
getTheme: 0,
|
getTheme: 0,
|
||||||
|
setTheme: 0,
|
||||||
|
resetTheme: 0,
|
||||||
},
|
},
|
||||||
|
|
||||||
heart: {
|
heart: {
|
||||||
|
@ -4,6 +4,8 @@ import { IPlayer } from "../PersonObjects/IPlayer";
|
|||||||
import { getRamCost } from "../Netscript/RamCostGenerator";
|
import { getRamCost } from "../Netscript/RamCostGenerator";
|
||||||
import { UserInterface as IUserInterface, UserInterfaceTheme } from "../ScriptEditor/NetscriptDefinitions";
|
import { UserInterface as IUserInterface, UserInterfaceTheme } from "../ScriptEditor/NetscriptDefinitions";
|
||||||
import { Settings } from "../Settings/Settings";
|
import { Settings } from "../Settings/Settings";
|
||||||
|
import { ThemeEvents } from "../ui/React/Theme";
|
||||||
|
import { defaultTheme } from "../Settings/Themes";
|
||||||
|
|
||||||
export function NetscriptUserInterface(
|
export function NetscriptUserInterface(
|
||||||
player: IPlayer,
|
player: IPlayer,
|
||||||
@ -15,5 +17,37 @@ export function NetscriptUserInterface(
|
|||||||
helper.updateDynamicRam("getTheme", getRamCost(player, "ui", "getTheme"));
|
helper.updateDynamicRam("getTheme", getRamCost(player, "ui", "getTheme"));
|
||||||
return { ...Settings.theme };
|
return { ...Settings.theme };
|
||||||
},
|
},
|
||||||
};
|
|
||||||
|
setTheme: function (newTheme: UserInterfaceTheme): void {
|
||||||
|
helper.updateDynamicRam("setTheme", getRamCost(player, "ui", "setTheme"));
|
||||||
|
const hex = /^(#)((?:[A-Fa-f0-9]{3}){1,2})$/;
|
||||||
|
const currentTheme = {...Settings.theme}
|
||||||
|
const errors: string[] = [];
|
||||||
|
for (const key of Object.keys(newTheme)) {
|
||||||
|
if (!currentTheme[key]) {
|
||||||
|
// Invalid key
|
||||||
|
errors.push(`Invalid key "${key}"`);
|
||||||
|
} else if (!hex.test(newTheme[key] ?? '')) {
|
||||||
|
errors.push(`Invalid color "${key}": ${newTheme[key]}`);
|
||||||
|
} else {
|
||||||
|
currentTheme[key] = newTheme[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (errors.length === 0) {
|
||||||
|
Object.assign(Settings.theme, currentTheme);
|
||||||
|
ThemeEvents.emit();
|
||||||
|
workerScript.log("ui.setTheme", () => `Successfully set theme`);
|
||||||
|
} else {
|
||||||
|
workerScript.log("ui.setTheme", () => `Failed to set theme. Errors: ${errors.join(', ')}`);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
resetTheme: function (): void {
|
||||||
|
helper.updateDynamicRam("resetTheme", getRamCost(player, "ui", "resetTheme"));
|
||||||
|
Settings.theme = defaultTheme;
|
||||||
|
ThemeEvents.emit();
|
||||||
|
workerScript.log("ui.resetTheme", () => `Reinitialized theme to default`);
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
25
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
25
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
@ -1321,10 +1321,10 @@ export interface Singularity {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* SF4.1 - Workout at the gym.
|
* SF4.1 - Workout at the gym.
|
||||||
*
|
*
|
||||||
* @remarks
|
* @remarks
|
||||||
* RAM cost: 2 GB
|
* RAM cost: 2 GB
|
||||||
*
|
*
|
||||||
|
|
||||||
* This function will automatically set you to start working out at a gym to train
|
* This function will automatically set you to start working out at a gym to train
|
||||||
* a particular stat. If you are already in the middle of some “working” action
|
* a particular stat. If you are already in the middle of some “working” action
|
||||||
@ -3833,6 +3833,27 @@ interface UserInterface {
|
|||||||
* @returns An object containing the theme's colors
|
* @returns An object containing the theme's colors
|
||||||
*/
|
*/
|
||||||
getTheme(): UserInterfaceTheme;
|
getTheme(): UserInterfaceTheme;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the current theme
|
||||||
|
* @remarks
|
||||||
|
* RAM cost: cost: 0 GB
|
||||||
|
* @example
|
||||||
|
* Usage example (NS2)
|
||||||
|
* ```ts
|
||||||
|
* const theme = ns.ui.getTheme();
|
||||||
|
* theme.primary = '#ff5500';
|
||||||
|
* ns.ui.setTheme(theme);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
setTheme(newTheme: UserInterfaceTheme): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resets the player's theme to the default values
|
||||||
|
* @remarks
|
||||||
|
* RAM cost: cost: 0 GB
|
||||||
|
*/
|
||||||
|
resetTheme(): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user