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-03-31 15:41:19 +02:00
|
|
|
import {
|
|
|
|
GameInfo,
|
|
|
|
IStyleSettings,
|
|
|
|
UserInterface as IUserInterface,
|
|
|
|
UserInterfaceTheme,
|
|
|
|
} from "../ScriptEditor/NetscriptDefinitions";
|
2021-12-20 19:38:21 +01:00
|
|
|
import { Settings } from "../Settings/Settings";
|
2022-01-19 13:02:58 +01:00
|
|
|
import { ThemeEvents } from "../Themes/ui/Theme";
|
|
|
|
import { defaultTheme } from "../Themes/Themes";
|
2022-01-19 15:49:08 +01:00
|
|
|
import { defaultStyles } from "../Themes/Styles";
|
2022-01-16 16:41:02 +01:00
|
|
|
import { CONSTANTS } from "../Constants";
|
|
|
|
import { hash } from "../hash/hash";
|
2021-12-20 19:38:21 +01:00
|
|
|
|
|
|
|
export function NetscriptUserInterface(
|
|
|
|
player: IPlayer,
|
|
|
|
workerScript: WorkerScript,
|
|
|
|
helper: INetscriptHelper,
|
|
|
|
): IUserInterface {
|
2022-03-31 15:41:19 +02:00
|
|
|
const updateRam = (funcName: string): void => helper.updateDynamicRam(funcName, getRamCost(player, "ui", funcName));
|
2021-12-20 19:38:21 +01:00
|
|
|
return {
|
|
|
|
getTheme: function (): UserInterfaceTheme {
|
2022-03-31 15:41:19 +02:00
|
|
|
updateRam("getTheme");
|
2022-01-05 01:09:34 +01:00
|
|
|
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 {
|
2022-03-31 15:41:19 +02:00
|
|
|
updateRam("getStyles");
|
2022-01-13 00:22:09 +01:00
|
|
|
return { ...Settings.styles };
|
|
|
|
},
|
|
|
|
|
2022-01-05 02:25:16 +01:00
|
|
|
setTheme: function (newTheme: UserInterfaceTheme): void {
|
2022-03-31 15:41:19 +02:00
|
|
|
updateRam("setTheme");
|
2022-01-27 19:21:17 +01:00
|
|
|
const hex = /^(#)((?:[A-Fa-f0-9]{2}){3,4}|(?:[A-Fa-f0-9]{3}))$/;
|
2022-03-31 15:41:19 +02:00
|
|
|
const currentTheme = { ...Settings.theme };
|
2022-01-05 02:25:16 +01:00
|
|
|
const errors: string[] = [];
|
|
|
|
for (const key of Object.keys(newTheme)) {
|
|
|
|
if (!currentTheme[key]) {
|
|
|
|
// Invalid key
|
|
|
|
errors.push(`Invalid key "${key}"`);
|
2022-03-31 15:41:19 +02:00
|
|
|
} else if (!hex.test(newTheme[key] ?? "")) {
|
2022-01-05 02:25:16 +01:00
|
|
|
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 {
|
2022-03-31 15:41:19 +02:00
|
|
|
workerScript.log("ui.setTheme", () => `Failed to set theme. Errors: ${errors.join(", ")}`);
|
2022-01-05 02:25:16 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-01-13 00:22:09 +01:00
|
|
|
setStyles: function (newStyles: IStyleSettings): void {
|
2022-03-31 15:41:19 +02:00
|
|
|
updateRam("setStyles");
|
2022-01-13 00:22:09 +01:00
|
|
|
|
2022-03-31 15:41:19 +02:00
|
|
|
const currentStyles = { ...Settings.styles };
|
2022-01-13 00:22:09 +01:00
|
|
|
const errors: string[] = [];
|
|
|
|
for (const key of Object.keys(newStyles)) {
|
2022-03-31 15:41:19 +02:00
|
|
|
if (!(currentStyles as any)[key]) {
|
2022-01-13 00:22:09 +01:00
|
|
|
// 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 {
|
2022-03-31 15:41:19 +02:00
|
|
|
workerScript.log("ui.setStyles", () => `Failed to set styles. Errors: ${errors.join(", ")}`);
|
2022-01-13 00:22:09 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-01-05 02:25:16 +01:00
|
|
|
resetTheme: function (): void {
|
2022-03-31 15:41:19 +02:00
|
|
|
updateRam("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 {
|
2022-03-31 15:41:19 +02:00
|
|
|
updateRam("resetStyles");
|
2022-01-13 00:22:09 +01:00
|
|
|
Settings.styles = { ...defaultStyles };
|
|
|
|
ThemeEvents.emit();
|
|
|
|
workerScript.log("ui.resetStyles", () => `Reinitialized styles to default`);
|
2022-01-16 16:41:02 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
getGameInfo: function (): GameInfo {
|
2022-03-31 15:41:19 +02:00
|
|
|
updateRam("getGameInfo");
|
2022-01-16 16:41:02 +01:00
|
|
|
const version = CONSTANTS.VersionString;
|
|
|
|
const commit = hash();
|
2022-03-31 15:41:19 +02:00
|
|
|
const platform = navigator.userAgent.toLowerCase().indexOf(" electron/") > -1 ? "Steam" : "Browser";
|
2022-01-16 16:41:02 +01:00
|
|
|
|
|
|
|
const gameInfo = {
|
2022-03-31 15:41:19 +02:00
|
|
|
version,
|
|
|
|
commit,
|
|
|
|
platform,
|
|
|
|
};
|
2022-01-16 16:41:02 +01:00
|
|
|
|
|
|
|
return gameInfo;
|
2022-03-31 15:41:19 +02:00
|
|
|
},
|
|
|
|
};
|
2021-12-20 19:38:21 +01:00
|
|
|
}
|