bitburner-src/src/NetscriptFunctions/UserInterface.ts

111 lines
3.5 KiB
TypeScript
Raw Normal View History

import {
GameInfo,
IStyleSettings,
UserInterface as IUserInterface,
UserInterfaceTheme,
} from "../ScriptEditor/NetscriptDefinitions";
2021-12-20 19:38:21 +01:00
import { Settings } from "../Settings/Settings";
import { ThemeEvents } from "../Themes/ui/Theme";
import { defaultTheme } from "../Themes/Themes";
import { defaultStyles } from "../Themes/Styles";
import { CONSTANTS } from "../Constants";
import { hash } from "../hash/hash";
2022-08-08 19:43:41 +02:00
import { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
2022-05-06 12:29:21 +02:00
import { Terminal } from "../../src/Terminal";
2022-08-08 21:51:50 +02:00
import { helpers } from "../Netscript/NetscriptHelpers";
2021-12-20 19:38:21 +01:00
2022-05-08 01:08:07 +02:00
export function NetscriptUserInterface(): InternalAPI<IUserInterface> {
2021-12-20 19:38:21 +01:00
return {
windowSize: () => (): [number, number] => {
return [window.innerWidth, window.innerHeight];
},
2022-05-08 01:08:07 +02:00
getTheme: () => (): UserInterfaceTheme => {
2022-01-05 01:09:34 +01:00
return { ...Settings.theme };
2021-12-20 19:38:21 +01:00
},
2022-05-08 01:08:07 +02:00
getStyles: () => (): IStyleSettings => {
return { ...Settings.styles };
},
2022-05-08 01:08:07 +02:00
setTheme:
(ctx: NetscriptContext) =>
(newTheme: UserInterfaceTheme): void => {
const hex = /^(#)((?:[A-Fa-f0-9]{2}){3,4}|(?:[A-Fa-f0-9]{3}))$/;
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];
}
}
2022-05-08 01:08:07 +02:00
if (errors.length === 0) {
Object.assign(Settings.theme, currentTheme);
ThemeEvents.emit();
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => `Successfully set theme`);
2022-05-08 01:08:07 +02:00
} else {
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => `Failed to set theme. Errors: ${errors.join(", ")}`);
2022-05-08 01:08:07 +02:00
}
},
2022-05-08 01:08:07 +02:00
setStyles:
(ctx: NetscriptContext) =>
(newStyles: IStyleSettings): void => {
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];
}
}
2022-05-08 01:08:07 +02:00
if (errors.length === 0) {
Object.assign(Settings.styles, currentStyles);
ThemeEvents.emit();
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => `Successfully set styles`);
} else {
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => `Failed to set styles. Errors: ${errors.join(", ")}`);
}
2022-05-08 01:08:07 +02:00
},
2022-05-08 01:08:07 +02:00
resetTheme: (ctx: NetscriptContext) => (): void => {
Settings.theme = { ...defaultTheme };
ThemeEvents.emit();
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => `Reinitialized theme to default`);
},
2022-05-08 01:08:07 +02:00
resetStyles: (ctx: NetscriptContext) => (): void => {
Settings.styles = { ...defaultStyles };
ThemeEvents.emit();
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => `Reinitialized styles to default`);
},
2022-05-08 01:08:07 +02:00
getGameInfo: () => (): GameInfo => {
const version = CONSTANTS.VersionString;
const commit = hash();
const platform = navigator.userAgent.toLowerCase().indexOf(" electron/") > -1 ? "Steam" : "Browser";
const gameInfo = {
version,
commit,
platform,
};
return gameInfo;
},
2022-05-06 12:29:21 +02:00
2022-05-19 07:43:54 +02:00
clearTerminal: (ctx: NetscriptContext) => (): void => {
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => `Clearing terminal`);
2022-05-06 12:29:21 +02:00
Terminal.clear();
},
};
2021-12-20 19:38:21 +01:00
}