SE3-TTSDeck/resources/ttsdeck/TTSDeck.js

175 lines
4.3 KiB
JavaScript
Raw Normal View History

2022-01-03 13:10:48 -05:00
useLibrary("threads");
const Card = require("./Card.js");
const TTS_CARDS_PER_IMAGE = 69;
const TTS_MAX_ROWS = 7;
function TTSDeckPage(image_format, image_resolution, page_num, cards) {
this.image_format = image_format;
this.image_resolution = image_resolution;
this.page_num = page_num;
this.cards = cards;
this.rows = Math.min(Math.ceil(Math.sqrt(cards.length)), TTS_MAX_ROWS);
this.columns = Math.ceil(cards.length / this.rows);
this.deck_image = null;
this.card_jsons = [];
this.face_url = String(
Card.getImageFile(cards[0].member.parent, image_format, this.page_num)
.toPath()
.toUri()
);
this.back_url = String(
Card.getImageFile(cards[0].member.parent, image_format, "back")
.toPath()
.toUri()
2022-01-03 13:10:48 -05:00
);
}
TTSDeckPage.prototype.build = function build(busy_props) {
2021-09-29 18:12:09 -04:00
let deck_graphics;
for (let row = 0; row < this.rows; row++) {
2022-01-03 13:10:48 -05:00
for (
let col = 0;
col < this.columns && row * this.columns + col < this.cards.length;
2022-01-03 13:10:48 -05:00
col++
) {
if (busy_props.cancelled) return this;
let index = row * this.columns + col;
let card = this.cards[index];
busy_props.status = "Processing Card " + card.member;
busy_props.currentProgress =
(this.page_num - 1) * TTS_CARDS_PER_IMAGE + index;
2021-09-29 18:12:09 -04:00
try {
let copies = card.copyCount();
2021-09-29 18:12:09 -04:00
for (let ii = 0; ii < copies; ii++) {
this.card_jsons.push(card.makeJSON(this.page_num * 100 + index));
2021-09-29 18:12:09 -04:00
}
let card_image = card.makeImage(
this.image_format,
this.image_resolution
);
2021-09-29 18:12:09 -04:00
if (!this.deck_image) {
this.deck_image = ImageUtils.create(
2022-01-03 13:10:48 -05:00
card_image.width * this.columns,
card_image.height * this.rows,
false
);
deck_graphics = this.deck_image.createGraphics();
2021-09-29 18:12:09 -04:00
}
2022-01-03 13:10:48 -05:00
deck_graphics.drawImage(
card_image,
col * card_image.width,
row * card_image.height,
null
);
2021-09-29 18:12:09 -04:00
} catch (ex) {
2022-01-03 13:10:48 -05:00
Thread.invokeLater(() =>
alert("Error while processing " + card + ": " + ex, true)
);
2021-09-29 18:12:09 -04:00
}
}
println("End of Row ", row);
}
return this;
};
function TTSDeck(image_format, image_resolution, cards) {
this.image_format = image_format;
this.image_resolution = image_resolution;
this.cards = cards;
2021-09-29 18:12:09 -04:00
this.pages = [];
}
TTSDeck.prototype.build = function build(busy_props) {
2021-09-30 03:00:32 -04:00
busy_props.title = "Processing Cards";
busy_props.maximumProgress = this.cards.length;
2021-09-30 03:00:32 -04:00
2022-01-03 13:10:48 -05:00
for (
let page_num = 0;
page_num * TTS_CARDS_PER_IMAGE < this.cards.length;
2022-01-03 13:10:48 -05:00
page_num++
) {
if (busy_props.cancelled) return this;
let page_cards = this.cards.slice(
2022-01-03 13:10:48 -05:00
page_num * TTS_CARDS_PER_IMAGE,
(page_num + 1) * TTS_CARDS_PER_IMAGE
);
printf("Making page %d, with %d cards:\n", page_num + 1, page_cards.length);
this.pages.push(
2022-01-03 13:10:48 -05:00
new TTSDeckPage(
this.image_format,
this.image_resolution,
2022-01-03 13:10:48 -05:00
page_num + 1,
page_cards
).build(busy_props)
2022-01-03 13:10:48 -05:00
);
}
return this;
};
TTSDeck.prototype.getImages = function getImages() {
return this.pages.map((page) => page.deck_image);
};
TTSDeck.prototype.makeJSON = function makeJSON(nickname, description) {
return {
Name: "DeckCustom",
Transform: {
posX: 0,
posY: 0,
posZ: 0,
rotX: 0,
rotY: 0.0,
rotZ: 0.0,
scaleX: 1.0,
scaleY: 1.0,
scaleZ: 1.0,
},
Nickname: String(nickname || ""),
Description: String(description || ""),
ColorDiffuse: {
r: 0.713239133,
g: 0.713239133,
b: 0.713239133,
2022-01-03 13:10:48 -05:00
},
Grid: true,
Locked: false,
SidewaysCard: false,
DeckIDs: this.pages
.map((page) => page.card_jsons.map((card) => card.CardID))
.reduce((acc, val) => acc.concat(val), []),
CustomDeck: this.pages.reduce((acc, page, index) => {
acc[String(index + 1)] = {
FaceURL: String(page.face_url),
BackURL: String(page.back_url),
NumWidth: page.columns,
NumHeight: page.rows,
BackIsHidden: true,
};
return acc;
}, {}),
ContainedObjects: this.pages
.map((page) => page.card_jsons)
.reduce((acc, val) => acc.concat(val), []),
};
};
module.exports = {
TTSDeckPage: TTSDeckPage,
TTSDeck: TTSDeck,
};