Added preset features :3

This commit is contained in:
MeowcaTheoRange 2022-11-30 12:32:19 -06:00
parent 01968f5f91
commit b944f37a7e
5 changed files with 150 additions and 2 deletions

58
main.js
View file

@ -26,6 +26,10 @@ function createWindow () {
fs.mkdirSync(thePath);
}
if (!fs.existsSync(path.join(thePath, "xmls"))) {
fs.mkdirSync(path.join(thePath, "xmls"));
}
if (!fs.existsSync(path.join(thePath, "config.json"))) {
fs.writeFileSync(path.join(thePath, "config.json"), JSON.stringify({
"binds": {
@ -51,11 +55,62 @@ function createWindow () {
}, null, 2));
}
if (!fs.existsSync(path.join(thePath, "presets.json"))) {
fs.writeFileSync(path.join(thePath, "presets.json"), JSON.stringify({
"jimarrows": {
name: "Jim Arrows",
binds: {
"A": "left",
"S": "down",
"W": "up",
"D": "right",
"ArrowLeft": "left",
"ArrowDown": "down",
"ArrowUp": "up",
"ArrowRight": "right",
"idle": "idle"
}
},
"jimaskl": {
name: "Jim ASKL",
binds: {
"A": "left",
"S": "down",
"K": "up",
"L": "right"
}
},
"jimdfjk": {
name: "Jim DFJK",
binds: {
"D": "left",
"F": "down",
"J": "up",
"K": "right"
}
},
"jimqwop": {
name: "Jim QWOP",
binds: {
"Q": "left",
"W": "down",
"O": "up",
"P": "right"
}
}
}, null, 2));
}
if (!fs.existsSync(path.join(thePath, "char.png")) || !fs.existsSync(path.join(thePath, "char.xml"))) {
fs.copyFileSync(path.join(app.getAppPath(), 'web', 'assets', 'jim.png'), path.join(thePath, "char.png"));
fs.copyFileSync(path.join(app.getAppPath(), 'web', 'assets', 'jim.xml'), path.join(thePath, "char.xml"));
}
if (!fs.existsSync(path.join(thePath, "xmls", "jim.png")) || !fs.existsSync(path.join(thePath, "xmls", "jim.xml"))) {
fs.copyFileSync(path.join(app.getAppPath(), 'web', 'assets', 'jim.png'), path.join(thePath, "xmls", "jim.png"));
fs.copyFileSync(path.join(app.getAppPath(), 'web', 'assets', 'jim.xml'), path.join(thePath, "xmls", "jim.xml"));
}
ipcMain.handle('resizeToSprite', (e, w, h) => {
mainWindow.setResizable(true);
mainWindow.setSize(w, h);
@ -101,8 +156,9 @@ function createWindow () {
if (err) alert("File error, stat " + fileval);
});
});
ipcMain.handle('writeConfig', (e, obj) => {
ipcMain.handle('writeConfig', (e, obj, binds) => {
fs.writeFileSync(path.join(thePath, "config.json"), obj);
fs.writeFileSync(path.join(thePath, "presets.json"), binds);
});
ipcMain.handle('navApp', (e, obj) => {

View file

@ -62,4 +62,8 @@ button.inset {
button.inset:hover {
color: #aaa;
}
div.bind {
display: inline-block;
}

View file

@ -20,12 +20,16 @@
<button id="file" onclick="ipcRenderer.invoke('promptForImage');">Look for path</button><input name="filePath" id="filePath"><br /><br />
<label for="xml">Character XML:</label><br />
<button id="xml" onclick="ipcRenderer.invoke('promptForXml')">Look for path</button><input name="xmlPath" id="xmlPath"><br /><br />
<button onclick="addImage()">Add To XML Folder...</button>
<div id="xmlPres"></div>
<label for="validAnims">Character Animations:</label>
<ul id="validAnims"><li><i>Import an XML!</i></li></ul><br />
<label for="idle">Idle Anim ID:</label><br />
<input type="text" name="idle" id="idle" placeholder="idle" /><br /><br />
<label for="binds">JSON Binds:<br>Format like "BIND": "ANIMATION ID"</label><br />
<textarea name="binds" id="binds" placeholder='{"KEY_LEFT": "left"...}'></textarea>
<button onclick="addPreset('#presetname')">Add to Bind Presets As...</button><input name="presetname" id="presetname" style="width: initial;">
<div id="presets"></div>
<h2>Character Look</h2>
<label for="fps">Character FPS:</label><br />
<input type="number" min="1" step="1" max="120" name="fps" id="fps" placeholder="15" /><br /><br />

View file

@ -2,18 +2,24 @@ var fs = require("fs");
var json;
var nameGetter = /(.*?)(?=[0-9]+$)/gm;
var parser = new DOMParser();
var jsonbinds;
var xmllist;
var dirpath;
const { ipcRenderer } = require('electron');
const pathjs = require('path');
function loadForm(path) {
json = JSON.parse(fs.readFileSync(pathjs.join(path, "config.json")));
console.log(json);
seeValidBinds(pathjs.join(path, "char.xml"));
ipcRenderer.invoke('resizeToSprite', 600, 400);
document.querySelector("input#idle").value = json.binds.idle;
document.querySelector("input#zoom").value = json.charZoom;
document.querySelector("input#opacity").value = json.opacity;
document.querySelector("input#fps").value = json.fps;
document.querySelector("div#presets").innerHTML = getPresets(path);
document.querySelector("div#xmlPres").innerHTML = getImages(path);
if (json.align) {
document.querySelector("#alignRadioLeft").checked = isTrue(json.align[0]);
document.querySelector("#alignRadioRight").checked = isTrue(json.align[1]);
@ -25,7 +31,84 @@ function loadForm(path) {
document.querySelector("textarea#binds").value = JSON.stringify(json.binds, null, 2);
}
function getImages(path) {
xmllist = fs.readdirSync(pathjs.join(path, "xmls")).filter((v) => {
return v.endsWith(".xml");
});
return reloadImages();
}
function reloadImages() {
var html = [];
Object.entries(xmllist).forEach((xml) => {
console.log(xml);
html.push(`<div class="bind"><span>${xml[1]}</span><br /><button onclick='loadImage("${xml[1]}")'>^</button></div>`);
});
return html.join("\n");
}
function loadImage(xmlName) {
var xmlTemp = fs.readFileSync(pathjs.join(dirpath, "xmls", xmlName), {encoding: "utf-8"});
var pngName = parser.parseFromString(xmlTemp.replace(/[^ \S]/gm, ""), "text/xml").getElementsByTagName("TextureAtlas")[0].getAttribute("imagePath");
document.querySelector("input#xmlPath").value = pathjs.join(dirpath, "xmls", xmlName);
document.querySelector("input#filePath").value = pathjs.join(dirpath, "xmls", pngName);
}
function addImage() {
var filepath = document.querySelector("input#filePath").value;
var xmlpath = document.querySelector("input#xmlPath").value
fs.copyFile(filepath, pathjs.join(dirpath, "xmls", pathjs.basename(filepath)), (err) => {
if (err) console.log("File error, stat " + fileval);
});
fs.copyFile(xmlpath, pathjs.join(dirpath, "xmls", pathjs.basename(xmlpath)), (err) => {
if (err) console.log("File error, stat " + fileval);
});
getImages(dirpath);
}
function getPresets(path) {
jsonbinds = JSON.parse(fs.readFileSync(pathjs.join(path, "presets.json")));
return reloadPresets();
}
function reloadPresets() {
var html = [];
Object.entries(jsonbinds).forEach((binds) => {
html.push(`<div class="bind"><span>${binds[1].name}</span><br /><button onclick='loadPreset("${binds[0]}")'>^</button><button onclick='removePreset("${binds[0]}")'>X</button></div>`);
});
console.log(html);
return html.join("\n");
}
function loadPreset(bind) {
document.querySelector("textarea#binds").value = JSON.stringify(jsonbinds[bind].binds, null, 2);
document.querySelector("input#idle").value = jsonbinds[bind].idle;
document.querySelector("input#fps").value = jsonbinds[bind].fps;
document.querySelector("input#zoom").value = jsonbinds[bind].zoom;
document.querySelector("#presetname").value = bind;
}
function addPreset(name) {
var theName = document.querySelector(name);
var id = theName.value.replace(/[\W\d]/gm, "").toLowerCase();
jsonbinds[id] = {
name: theName.value,
binds: JSON.parse(document.querySelector("textarea#binds").value),
idle: document.querySelector("input#idle").value,
fps: document.querySelector("input#fps").value,
zoom: document.querySelector("input#zoom").value
};
theName.value = "";
document.querySelector("div#presets").innerHTML = reloadPresets();
}
function removePreset(bind) {
delete jsonbinds[bind];
document.querySelector("div#presets").innerHTML = reloadPresets();
}
ipcRenderer.on('configPath', function (evt, path) {
dirpath = path;
loadForm(path);
});
@ -55,7 +138,7 @@ function saveForm() {
console.log(json);
if (fileval)
ipcRenderer.invoke('copyFiles', fileval, xmlval);
ipcRenderer.invoke('writeConfig', JSON.stringify(json, null, 2));
ipcRenderer.invoke('writeConfig', JSON.stringify(json, null, 2), JSON.stringify(jsonbinds, null, 2));
return true;
}

View file

@ -220,6 +220,7 @@ function doanimate(st) {
}
function theThing() {
if (anim == null || anim == undefined) ipcRenderer.invoke('navApp', 'web/config.html');
char.style.objectPosition = `-${anim[fn].x}px -${anim[fn].y}px`;
char.style.width = `${anim[fn].w}px`;
char.style.height = `${anim[fn].h}px`;