Added command flipping
This commit is contained in:
parent
72110bd4f8
commit
da29424126
1 changed files with 45 additions and 21 deletions
66
index.js
66
index.js
|
@ -41,7 +41,7 @@ function calculatePosition(scale, anchor, imageSize) {
|
||||||
return imageSize * anchor.position / 100 - anchor.offset * scale;
|
return imageSize * anchor.position / 100 - anchor.offset * scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
function render(template, img, size) {
|
function render(template, img, size, flipH) {
|
||||||
var imgWidth = img.width;
|
var imgWidth = img.width;
|
||||||
var imgHeight = img.height;
|
var imgHeight = img.height;
|
||||||
if (size && size.height) {
|
if (size && size.height) {
|
||||||
|
@ -87,7 +87,7 @@ function render(template, img, size) {
|
||||||
const toDraw = [{
|
const toDraw = [{
|
||||||
z: 1,
|
z: 1,
|
||||||
image: img,
|
image: img,
|
||||||
x: imageOffsetX,
|
x: flipH ? resultingWidth - imageOffsetX - imgWidth : imageOffsetX,
|
||||||
y: imageOffsetY,
|
y: imageOffsetY,
|
||||||
h: imgHeight,
|
h: imgHeight,
|
||||||
w: imgWidth,
|
w: imgWidth,
|
||||||
|
@ -99,7 +99,8 @@ function render(template, img, size) {
|
||||||
y: templateOffsetY,
|
y: templateOffsetY,
|
||||||
h: template.image.height * templateScale,
|
h: template.image.height * templateScale,
|
||||||
w: template.image.width * templateScale,
|
w: template.image.width * templateScale,
|
||||||
name: "template "+template.src
|
name: "template "+template.src,
|
||||||
|
flipH
|
||||||
}].sort((u,v) => u.z > v.z);
|
}].sort((u,v) => u.z > v.z);
|
||||||
|
|
||||||
var canvas = new Canvas(resultingWidth, resultingHeight);
|
var canvas = new Canvas(resultingWidth, resultingHeight);
|
||||||
|
@ -107,9 +108,17 @@ function render(template, img, size) {
|
||||||
|
|
||||||
for(let i=0;i<toDraw.length;++i) {
|
for(let i=0;i<toDraw.length;++i) {
|
||||||
const subject = toDraw[i];
|
const subject = toDraw[i];
|
||||||
console.log("Drawing "+subject.name)
|
console.log("Drawing "+subject.name+(subject.flipH?" (flipped)":""))
|
||||||
try {
|
try {
|
||||||
|
if(subject.flipH) {
|
||||||
|
ctx.save();
|
||||||
|
ctx.translate(resultingWidth, 0);
|
||||||
|
ctx.scale(-1, 1);
|
||||||
|
}
|
||||||
ctx.drawImage(subject.image, subject.x, subject.y, subject.w, subject.h);
|
ctx.drawImage(subject.image, subject.x, subject.y, subject.w, subject.h);
|
||||||
|
if(subject.flipH) {
|
||||||
|
ctx.restore();
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw new Error(JSON.stringify({ status: 400, error: "Invalid template" }))
|
throw new Error(JSON.stringify({ status: 400, error: "Invalid template" }))
|
||||||
|
@ -197,18 +206,29 @@ function loadImage(url) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
function reverseString(str) {
|
||||||
|
return str.split("").reverse().join("");
|
||||||
|
}
|
||||||
|
|
||||||
|
const commands = Object.keys(templates).map(x => '/'+x).join(', ');
|
||||||
|
const otherCommands = {
|
||||||
|
"invite": `Invite link: <${invitelink}>`,
|
||||||
|
"help": `Available commands: ${commands}.\nUse \\<command> to flip the template horizontally.\nInvite link: <${invitelink}>`,
|
||||||
|
"beebot": `Available commands: ${commands}.\nUse \\<command> to flip the template horizontally.\nInvite link: <${invitelink}>`
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
client.on('message', async function (message) {
|
client.on('message', async function (message) {
|
||||||
console.log(`[${message.guild.name} - ${message.channel.name}] ${message.author.username}#${message.author.discriminator}: ${message.cleanContent}`);
|
console.log(`[${message.guild.name} - ${message.channel.name}] ${message.author.username}#${message.author.discriminator}: ${message.cleanContent}`);
|
||||||
|
|
||||||
if(message.cleanContent.startsWith('/invite')) {
|
let commandParsed = /^([\/\\])(\w+)\b/.exec(message.cleanContent);
|
||||||
message.channel.send(`Invite link: <${invitelink}>`);
|
if(commandParsed) {
|
||||||
return;
|
const [, direction, command] = commandParsed;
|
||||||
}
|
if(otherCommands[command]) {
|
||||||
if(message.cleanContent.startsWith('/help') || message.cleanContent.startsWith('/beebot')) {
|
const text = otherCommands[command];
|
||||||
let commands = Object.keys(templates).map(x => '/'+x).join(', ');
|
message.channel.send(direction === "\\" ? reverseString(text) : text);
|
||||||
message.channel.send(`Available commands: ${commands}`);
|
return;
|
||||||
return;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const messageSplit = message.cleanContent.split(" ");
|
const messageSplit = message.cleanContent.split(" ");
|
||||||
|
@ -219,15 +239,19 @@ client.on('message', async function (message) {
|
||||||
if (emoji) {
|
if (emoji) {
|
||||||
let name = emoji.name;
|
let name = emoji.name;
|
||||||
for (var i = 0; i < messageSplit.length && count < 4; ++i) {
|
for (var i = 0; i < messageSplit.length && count < 4; ++i) {
|
||||||
const commandParsed = /^\/(\w+)\b/.exec(messageSplit[i]);
|
commandParsed = /^([\/\\])(\w+)\b/.exec(messageSplit[i]);
|
||||||
if (commandParsed && templates[commandParsed[1]]) {
|
if (commandParsed) {
|
||||||
count++;
|
const [, direction, command] = commandParsed;
|
||||||
name += commandParsed[1];
|
console.log("Got command ",direction, command, direction === '\\' ? "flipped":"not flipped");
|
||||||
if (result === null) result = await loadImage(emoji.url);
|
if(templates[command]) {
|
||||||
const templateData = templates[commandParsed[1]];
|
count++;
|
||||||
all(templateData, template => {
|
name += command;
|
||||||
result = render(template, result);
|
if (result === null) result = await loadImage(emoji.url);
|
||||||
})
|
const templateData = templates[command];
|
||||||
|
all(templateData, template => {
|
||||||
|
result = render(template, result, null, direction === '\\');
|
||||||
|
})
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if(i===0) return;
|
if(i===0) return;
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue