2021-12-20 19:38:21 +01:00
|
|
|
import { INetscriptHelper } from "./INetscriptHelper";
|
|
|
|
import { WorkerScript } from "../Netscript/WorkerScript";
|
|
|
|
import { IPlayer } from "../PersonObjects/IPlayer";
|
|
|
|
import { getRamCost } from "../Netscript/RamCostGenerator";
|
2022-01-13 00:22:09 +01:00
|
|
|
import { IStyleSettings, UserInterface as IUserInterface, UserInterfaceTheme } from "../ScriptEditor/NetscriptDefinitions";
|
2021-12-20 19:38:21 +01:00
|
|
|
import { Settings } from "../Settings/Settings";
|
2022-01-05 02:25:16 +01:00
|
|
|
import { ThemeEvents } from "../ui/React/Theme";
|
|
|
|
import { defaultTheme } from "../Settings/Themes";
|
2022-01-13 00:22:09 +01:00
|
|
|
import { defaultStyles } from "../Settings/Styles";
|
2021-12-20 19:38:21 +01:00
|
|
|
|
|
|
|
export function NetscriptUserInterface(
|
|
|
|
player: IPlayer,
|
|
|
|
workerScript: WorkerScript,
|
|
|
|
helper: INetscriptHelper,
|
|
|
|
): IUserInterface {
|
|
|
|
return {
|
|
|
|
getTheme: function (): UserInterfaceTheme {
|
2022-01-05 01:09:34 +01:00
|
|
|
helper.updateDynamicRam("getTheme", getRamCost(player, "ui", "getTheme"));
|
|
|
|
return { ...Settings.theme };
|
2021-12-20 19:38:21 +01:00
|
|
|
},
|
2022-01-05 02:25:16 +01:00
|
|
|
|
2022-01-13 00:22:09 +01:00
|
|
|
getStyles: function (): IStyleSettings {
|
|
|
|
helper.updateDynamicRam("getStyles", getRamCost(player, "ui", "getStyles"));
|
|
|
|
return { ...Settings.styles };
|
|
|
|
},
|
|
|
|
|
2022-01-05 02:25:16 +01:00
|
|
|
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(', ')}`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-01-13 00:22:09 +01:00
|
|
|
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(', ')}`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-01-05 02:25:16 +01:00
|
|
|
resetTheme: function (): void {
|
|
|
|
helper.updateDynamicRam("resetTheme", getRamCost(player, "ui", "resetTheme"));
|
2022-01-13 00:22:09 +01:00
|
|
|
Settings.theme = { ...defaultTheme };
|
2022-01-05 02:25:16 +01:00
|
|
|
ThemeEvents.emit();
|
|
|
|
workerScript.log("ui.resetTheme", () => `Reinitialized theme to default`);
|
|
|
|
},
|
2022-01-13 00:22:09 +01:00
|
|
|
|
|
|
|
resetStyles: function (): void {
|
|
|
|
helper.updateDynamicRam("resetStyles", getRamCost(player, "ui", "resetStyles"));
|
|
|
|
Settings.styles = { ...defaultStyles };
|
|
|
|
ThemeEvents.emit();
|
|
|
|
workerScript.log("ui.resetStyles", () => `Reinitialized styles to default`);
|
|
|
|
}
|
2022-01-05 02:25:16 +01:00
|
|
|
}
|
2021-12-20 19:38:21 +01:00
|
|
|
}
|