Skip to content

Commit

Permalink
Added Json controls and Canvas update (#1038)
Browse files Browse the repository at this point in the history
* Updated with 2 things

Added create primitive mod

Added seperator mod

* Updated these with lint

* Updated these with lint

* Updated canvas_create_primitive_MOD.js

* Added json_read_MOD

added json_write_MOD.js

* Added json_read_MOD

added json_write_MOD.js

* Added json_read_MOD

added json_write_MOD.js

* Added json_read_MOD

added json_write_MOD.js

* Fixed lint issue I hope

* Fixed lint issue I hope

* Fixed prettier issues

Fixed linting issues inside json write

Fixed issue with canvas paint not using cases

* Added json random
  • Loading branch information
thekingofspace authored Jun 4, 2024
1 parent 49e3b9c commit fbcd592
Show file tree
Hide file tree
Showing 4 changed files with 488 additions and 13 deletions.
113 changes: 100 additions & 13 deletions actions/canvas_create_primitive_MOD.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,24 @@ module.exports = {

subtitle(data) {
const info = parseInt(data.info, 10);
if (info === 0) {
return data.color ? `Create Circle with Color ${data.color}` : 'No color circle has been created';
}
if (info === 1) {
return data.color ? `Create Rectangle with Color ${data.color}` : 'No color rectangle has been created';
switch (info) {
case 0:
return data.color ? `Create Circle with Color ${data.color}` : 'No color circle has been created';
case 1:
return data.color ? `Create Rectangle with Color ${data.color}` : 'No color rectangle has been created';
case 2:
return data.color ? `Create Triangle with Color ${data.color}` : 'No color triangle has been created';
case 3:
return data.color ? `Create Hexagon with Color ${data.color}` : 'No color hexagon has been created';
case 4:
return data.color ? `Create Pentagon with Color ${data.color}` : 'No color pentagon has been created';
case 5:
return data.color ? `Create Ellipse with Color ${data.color}` : 'No color ellipse has been created';
case 6:
return data.color ? `Create Star with Color ${data.color}` : 'No color star has been created';
default:
return '';
}
// Add more cases for different shapes as needed
},

variableStorage(data, varType) {
Expand All @@ -35,12 +46,15 @@ module.exports = {
<select id="shapeType" class="round">
<option value="0" selected>Circle</option>
<option value="1">Rectangle</option>
<!-- Add more options for different shapes -->
<option value="2">Triangle</option>
<option value="3">Hexagon</option>
<option value="4">Pentagon</option>
<option value="5">Ellipse</option>
<option value="6">Star</option>
</select>
</div>
</div>
<br><br>
<div>
<div>
<span class="dbminputlabel">Width (px)</span>
Expand All @@ -52,15 +66,13 @@ module.exports = {
</div>
</div>
<br><br>
<div>
<div>
<span class="dbminputlabel">Color</span>
<input id="color" class="round" type="text" placeholder="Insert Color Hex code here"><br>
</div>
</div>
<br><br>
<store-in-variable dropdownLabel="Store In" selectId="storage" variableContainerId="varNameContainer" variableInputId="varName"></store-in-variable>
`;
},
Expand All @@ -75,18 +87,93 @@ module.exports = {
const ctx = canvas.getContext('2d');
const color = this.evalMessage(data.color, cache);

let sideLength;
let xCenter;
let yCenter;
let angle;
let x;
let y;
let angleStep;
let radius;
let centerX;
let centerY;
let numPoints;
let outerRadius;
let innerRadius;

switch (shapeType) {
case 0: // Circle
case 0:
ctx.beginPath();
ctx.arc(width / 2, height / 2, Math.min(width, height) / 2, 0, 2 * Math.PI);
ctx.fillStyle = color;
ctx.fill();
break;
case 1: // Rectangle
case 1:
ctx.fillStyle = color;
ctx.fillRect(0, 0, width, height);
break;
// Add more cases for different shapes
case 2:
ctx.beginPath();
ctx.moveTo(width / 2, 0);
ctx.lineTo(width, height);
ctx.lineTo(0, height);
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
break;
case 3:
ctx.beginPath();
sideLength = Math.min(width, height) / 2;
xCenter = width / 2;
yCenter = height / 2;
for (let i = 0; i < 6; i++) {
angle = (Math.PI / 3) * i;
x = xCenter + sideLength * Math.cos(angle);
y = yCenter + sideLength * Math.sin(angle);
if (i === 0) {
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
}
}
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
break;
case 4:
ctx.beginPath();
angleStep = (2 * Math.PI) / 5;
radius = Math.min(width, height) / 2;
centerX = width / 2;
centerY = height / 2;
ctx.moveTo(centerX + radius * Math.cos(0), centerY + radius * Math.sin(0));
for (let i = 1; i <= 5; i++) {
ctx.lineTo(centerX + radius * Math.cos(angleStep * i), centerY + radius * Math.sin(angleStep * i));
}
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
break;
case 5:
ctx.beginPath();
ctx.ellipse(width / 2, height / 2, width / 2, height / 2, 0, 0, 2 * Math.PI);
ctx.fillStyle = color;
ctx.fill();
break;
case 6:
numPoints = 5;
outerRadius = Math.min(width, height) / 2;
innerRadius = outerRadius * 0.5;
ctx.beginPath();
for (let i = 0; i < numPoints * 2; i++) {
const radius = i % 2 === 0 ? outerRadius : innerRadius;
const angle = (i * Math.PI) / numPoints - Math.PI / 2;
ctx.lineTo(width / 2 + radius * Math.cos(angle), height / 2 + radius * Math.sin(angle));
}
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
break;
default:
break;
}
Expand Down
99 changes: 99 additions & 0 deletions actions/json_random_MOD.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const fs = require('fs');
const path = require('path');

module.exports = {
name: 'Pick Random JSON Item',
section: 'File Stuff',
fields: ['filepath', 'title', 'storage', 'varName'],

subtitle(data) {
return `Pick random item from JSON file "${data.filepath}"`;
},

variableStorage(data, varType) {
if (parseInt(data.storage, 10) !== varType) return;
return [data.varName, 'Text'];
},

html() {
return `
<div>
<div style="padding: 10px;">
<span class="dbminputlabel">File Path</span>
<input id="filepath" class="round" type="text" placeholder="./data.json">
</div>
<div style="padding: 10px;">
<span class="dbminputlabel">Title</span>
<input id="title" class="round" type="text" placeholder="Title (optional)">
</div>
<div style="padding: 10px;">
<store-in-variable dropdownLabel="Store Result In" selectId="storage" variableContainerId="varNameContainer" variableInputId="varName"></store-in-variable>
</div>
</div>
`;
},

init() {},

async action(cache) {
const data = cache.actions[cache.index];
let filepath = this.evalMessage(data.filepath, cache);
const title = this.evalMessage(data.title, cache);
const storage = parseInt(data.storage, 10);
const varName = this.evalMessage(data.varName, cache);

if (filepath.startsWith('./')) {
filepath = path.join(__dirname, '..', filepath.substring(2));
}

let jsonData;

try {
if (fs.existsSync(filepath)) {
const fileData = fs.readFileSync(filepath);
if (fileData.length === 0) {
console.warn('JSON file is empty.');
this.storeValue(undefined, storage, varName, cache);
return this.callNextAction(cache);
}
jsonData = JSON.parse(fileData);
} else {
throw new Error('File does not exist');
}
} catch (error) {
console.error(`Error reading JSON file: ${error}`);
this.storeValue(undefined, storage, varName, cache);
return this.callNextAction(cache);
}

let result;

try {
if (title) {
const titleData = jsonData.find((item) => item.Title === title);
if (!titleData) throw new Error('Title not found');

const keys = Object.keys(titleData).filter((key) => key !== 'Title');
if (keys.length === 0) throw new Error('No items found under specified title');

const randomKey = keys[Math.floor(Math.random() * keys.length)];
result = randomKey;
} else {
const items = jsonData.flatMap((item) => Object.keys(item).filter((key) => key !== 'Title'));
if (items.length === 0) throw new Error('No items found in JSON');

const randomItem = items[Math.floor(Math.random() * items.length)];
result = randomItem;
}
} catch (error) {
console.error(`Error accessing data: ${error}`);
this.storeValue(undefined, storage, varName, cache);
return this.callNextAction(cache);
}

this.storeValue(result, storage, varName, cache);
this.callNextAction(cache);
},

mod() {},
};
102 changes: 102 additions & 0 deletions actions/json_read_MOD.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const fs = require('fs');
const path = require('path');

module.exports = {
name: 'Read JSON File',
section: 'File Stuff',
fields: ['filepath', 'title', 'contentTitle', 'storage', 'varName'],

subtitle(data) {
return `Read JSON file "${data.filepath}"`;
},

variableStorage(data, varType) {
if (parseInt(data.storage, 10) !== varType) return;
return [data.varName, 'Unknown'];
},

html() {
return `
<div>
<div style="padding: 10px;">
<span class="dbminputlabel">File Path</span>
<input id="filepath" class="round" type="text" placeholder="./data.json">
</div>
<div style="padding: 10px;">
<span class="dbminputlabel">Title</span>
<input id="title" class="round" type="text" placeholder="Title">
</div>
<div style="padding: 10px;">
<span class="dbminputlabel">Content Title</span>
<input id="contentTitle" class="round" type="text" placeholder="Content Title">
</div>
<div style="padding: 10px;">
<store-in-variable dropdownLabel="Store Result In" selectId="storage" variableContainerId="varNameContainer" variableInputId="varName"></store-in-variable>
</div>
</div>
`;
},

init() {},

async action(cache) {
const data = cache.actions[cache.index];
let filepath = this.evalMessage(data.filepath, cache);
const title = this.evalMessage(data.title, cache);
const contentTitle = this.evalMessage(data.contentTitle, cache);
const storage = parseInt(data.storage, 10);
const varName = this.evalMessage(data.varName, cache);

if (filepath.startsWith('./')) {
filepath = path.join(__dirname, '..', filepath.substring(2));
}

let jsonData;

try {
if (fs.existsSync(filepath)) {
const fileData = fs.readFileSync(filepath);
if (fileData.length === 0) {
console.warn('JSON file is empty.');
this.storeValue(undefined, storage, varName, cache);
return this.callNextAction(cache);
}
jsonData = JSON.parse(fileData);
} else {
throw new Error('File does not exist');
}
} catch (error) {
console.error(`Error reading JSON file: ${error}`);
this.storeValue(undefined, storage, varName, cache);
return this.callNextAction(cache);
}

let result;
try {
const titleData = jsonData.find((item) => item.Title === title);
if (!titleData) throw new Error('Title not found');

if (contentTitle.includes('/')) {
const contentKeys = contentTitle.split('/');
result = {};
for (const key of contentKeys) {
if (titleData[key] !== undefined) {
result[key] = titleData[key];
}
}
} else {
if (titleData[contentTitle] === undefined) throw new Error('Content Title not found');
result = titleData[contentTitle];
}
} catch (error) {
console.error(`Error accessing data: ${error}`);
this.storeValue(undefined, storage, varName, cache);
return this.callNextAction(cache);
}

this.storeValue(result, storage, varName, cache);
this.callNextAction(cache);
},

mod() {},
};
Loading

0 comments on commit fbcd592

Please sign in to comment.