Add access to icons and locale from export

Add models for accessing and using the icons and locale that's exported
when an instance export is runned.
This commit is contained in:
Hornwitser committed 2021-03-20 03:35:05 +01:00
1 parent 6c19145734
commit cebe83542f
5 files changed
+70

No files matched your search

File renamed without changes.

Before

Width:  |  Height:  |  Size: 5.6 KiB

After

Width:  |  Height:  |  Size: 5.6 KiB

+6
View File
@@ -259,3 +259,9 @@ body {
.json-input {
font-family: monospace;
}
.factorio-icon.item-unknown-item {
background: url("./images/unknown-item.png");
height: 32px;
width: 32px;
}
+2
View File
@@ -3,6 +3,8 @@ export { default as notify, notifyErrorHandler } from "./util/notify.jsx";
export * from "./model/instance.jsx";
export * from "./model/slave.jsx";
export * from "./model/locale.jsx";
export * from "./model/item_metadata.jsx";
export { default as ControlContext } from "./components/ControlContext";
@@ -0,0 +1,38 @@
import React, { useEffect, useState } from "react";
let itemMetadataCache = null;
export function useItemMetadata() {
let [itemMetadata, setItemMetadata] = useState(itemMetadataCache || new Map());
useEffect(() => {
async function load() {
let response = await fetch(`${webRoot}export/item-metadata.json`);
if (response.ok) {
let data = await response.json();
itemMetadataCache = new Map(data);
let style = document.createElement("style");
style.type = "text/css";
document.head.appendChild(style);
for (let [name, meta] of itemMetadataCache) {
style.sheet.insertRule(
`.item-${CSS.escape(name)} {
background-image: url("${webRoot}export/item-spritesheet.png");
background-repeat: no-repeat;
background-position: -${meta.x}px -${meta.y}px;
height: ${meta.size}px;
width: ${meta.size}px;
}`
);
}
setItemMetadata(itemMetadataCache);
}
}
if (!itemMetadataCache) {
load();
}
}, []);
return itemMetadata;
}
+24
View File
@@ -0,0 +1,24 @@
import React, { useEffect, useState } from "react";
let localeCache = null;
export function useLocale() {
let [locale, setLocale] = useState(localeCache || new Map());
useEffect(() => {
async function load() {
let response = await fetch(`${webRoot}export/locale.json`);
if (response.ok) {
let data = await response.json();
localeCache = new Map(data);
setLocale(localeCache);
}
}
if (!localeCache) {
load();
}
}, []);
return locale;
}