2021-09-16 14:05:40 -04:00
|
|
|
/*
|
|
|
|
* TTSDeck.js
|
|
|
|
*
|
|
|
|
* Creates a deck image and corresponding "Saved Object" JSON for use
|
|
|
|
* in Tabletop Simulator
|
|
|
|
*/
|
|
|
|
|
2022-01-03 13:10:48 -05:00
|
|
|
useLibrary("project");
|
|
|
|
useLibrary("imageutils");
|
|
|
|
useLibrary("threads");
|
|
|
|
useLibrary("uilayout");
|
|
|
|
useLibrary("uicontrols");
|
2021-09-18 01:26:15 -04:00
|
|
|
importClass(arkham.project.CopiesList);
|
2021-09-16 14:05:40 -04:00
|
|
|
|
2022-01-03 13:10:48 -05:00
|
|
|
const TTSJson = require("./TTSJson.js");
|
2021-09-29 18:05:58 -04:00
|
|
|
|
2021-09-30 02:35:13 -04:00
|
|
|
const TTS_CARDS_PER_IMAGE = 69;
|
|
|
|
const TTS_MAX_ROWS = 7;
|
|
|
|
|
2022-01-03 13:10:48 -05:00
|
|
|
const getName = () => "TTSDeck";
|
|
|
|
const getDescription = () => "Generates a TTS deck image and JSON file";
|
2021-09-30 00:29:27 -04:00
|
|
|
const getVersion = () => 1.0;
|
|
|
|
const getPluginType = () => arkham.plugins.Plugin.INJECTED;
|
2021-09-16 14:05:40 -04:00
|
|
|
|
|
|
|
function unload() {
|
|
|
|
unregisterAll();
|
|
|
|
}
|
|
|
|
|
2021-09-17 17:04:17 -04:00
|
|
|
// Creates a test button during development that calls unload() to clean up.
|
|
|
|
testProjectScript();
|
|
|
|
|
2022-01-01 21:37:41 -05:00
|
|
|
// TODO: allow setting a default copy count
|
2021-09-18 01:26:15 -04:00
|
|
|
// Hack to override the default return value of 1
|
|
|
|
function copyCount(copies_list, name) {
|
2022-01-03 13:10:48 -05:00
|
|
|
const entries = copies_list.getListEntries().map((x) => String(x));
|
2021-09-18 01:26:15 -04:00
|
|
|
if (entries.indexOf(String(name)) == -1) {
|
2022-01-01 21:37:41 -05:00
|
|
|
return 1;
|
2021-09-18 01:26:15 -04:00
|
|
|
} else {
|
|
|
|
return copies_list.getCopyCount(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 13:07:09 -05:00
|
|
|
function getImageFile(parent, format, page_num) {
|
2022-01-03 13:10:48 -05:00
|
|
|
return new File(
|
|
|
|
parent.file,
|
|
|
|
parent.getName() + "_" + page_num + "." + format
|
|
|
|
);
|
|
|
|
}
|
2022-01-01 22:22:51 -05:00
|
|
|
|
2022-01-03 13:07:09 -05:00
|
|
|
function Card(member) {
|
|
|
|
this.member = member;
|
|
|
|
this.component = ResourceKit.getGameComponentFromFile(member.file);
|
2022-01-01 22:22:51 -05:00
|
|
|
|
2022-01-03 13:07:09 -05:00
|
|
|
this.makeImageUncached = function makeImageUncached(resolution, back) {
|
|
|
|
println("Generating image for card ", this.member);
|
|
|
|
const sheets = this.component.createDefaultSheets();
|
2022-01-03 13:10:48 -05:00
|
|
|
const card_image = sheets[back ? 1 : 0].paint(
|
|
|
|
arkham.sheet.RenderTarget.EXPORT,
|
|
|
|
resolution
|
|
|
|
);
|
2022-01-01 22:22:51 -05:00
|
|
|
|
|
|
|
return card_image;
|
2022-01-03 13:07:09 -05:00
|
|
|
};
|
2022-01-01 22:22:51 -05:00
|
|
|
|
2022-01-03 13:07:09 -05:00
|
|
|
// export front face, or retrive it from a cached file
|
|
|
|
// TODO: handle two-sided cards
|
|
|
|
this.makeImage = function makeImage(format, resolution) {
|
2022-01-03 13:10:48 -05:00
|
|
|
const cache_dir = new File(this.member.parent.file, ".ttsdeck_cache");
|
|
|
|
const cached_file = new File(
|
|
|
|
cache_dir,
|
|
|
|
this.member.file.name + "." + format
|
|
|
|
);
|
|
|
|
|
|
|
|
if (
|
|
|
|
cached_file.exists() &&
|
|
|
|
cached_file.lastModified() > this.member.file.lastModified()
|
|
|
|
) {
|
2022-01-03 13:07:09 -05:00
|
|
|
println("Got cached image for card", this.member);
|
|
|
|
return ImageUtils.read(cached_file);
|
|
|
|
} else {
|
|
|
|
const card_image = this.makeImageUncached(resolution);
|
2021-09-30 01:30:23 -04:00
|
|
|
|
2022-01-03 13:07:09 -05:00
|
|
|
cache_dir.mkdir();
|
|
|
|
ImageUtils.write(card_image, cached_file, format, -1, false, resolution);
|
2021-09-30 01:30:23 -04:00
|
|
|
|
2022-01-03 13:07:09 -05:00
|
|
|
return card_image;
|
|
|
|
}
|
|
|
|
};
|
2021-09-30 01:30:23 -04:00
|
|
|
}
|
|
|
|
|
2022-01-03 13:10:48 -05:00
|
|
|
function TTSDeckPage(
|
|
|
|
busy_props,
|
|
|
|
image_format,
|
|
|
|
image_resolution,
|
|
|
|
page_num,
|
|
|
|
page_cards,
|
|
|
|
copies_list
|
|
|
|
) {
|
2021-09-30 02:35:13 -04:00
|
|
|
this.rows = Math.min(Math.ceil(Math.sqrt(page_cards.length)), TTS_MAX_ROWS);
|
|
|
|
this.columns = Math.ceil(page_cards.length / this.rows);
|
|
|
|
this.deck_image = null;
|
2021-09-29 18:12:09 -04:00
|
|
|
let deck_graphics;
|
|
|
|
|
2021-09-30 02:35:13 -04:00
|
|
|
this.card_jsons = [];
|
|
|
|
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 < page_cards.length;
|
|
|
|
col++
|
|
|
|
) {
|
2021-09-30 03:00:32 -04:00
|
|
|
if (busy_props.cancelled) return;
|
2021-09-30 02:35:13 -04:00
|
|
|
let index = row * this.columns + col;
|
|
|
|
let card = page_cards[index];
|
2022-01-03 13:07:09 -05:00
|
|
|
busy_props.status = "Processing Card " + card.member;
|
2021-09-30 03:00:32 -04:00
|
|
|
busy_props.currentProgress = (page_num - 1) * TTS_CARDS_PER_IMAGE + index;
|
2021-09-29 18:12:09 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
let copies = copyCount(copies_list, card.baseName);
|
|
|
|
|
|
|
|
for (let ii = 0; ii < copies; ii++) {
|
2022-01-03 13:10:48 -05:00
|
|
|
this.card_jsons.push(
|
|
|
|
TTSJson.makeCardJSON(
|
|
|
|
page_num * 100 + index,
|
|
|
|
card.component.getName()
|
|
|
|
)
|
|
|
|
);
|
2021-09-29 18:12:09 -04:00
|
|
|
}
|
|
|
|
|
2022-01-03 13:07:09 -05:00
|
|
|
let card_image = card.makeImage(image_format, image_resolution);
|
2021-09-29 18:12:09 -04:00
|
|
|
|
2021-09-30 02:35:13 -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
|
|
|
|
);
|
2021-09-30 02:35:13 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-01-03 13:10:48 -05:00
|
|
|
this.face_url = String(
|
|
|
|
getImageFile(page_cards[0].member.parent, image_format, page_num)
|
|
|
|
.toPath()
|
|
|
|
.toUri()
|
|
|
|
);
|
|
|
|
this.back_url = String(
|
|
|
|
getImageFile(page_cards[0].member.parent, image_format, "back")
|
|
|
|
.toPath()
|
|
|
|
.toUri()
|
|
|
|
);
|
2021-09-30 02:35:13 -04:00
|
|
|
}
|
|
|
|
|
2022-01-03 13:10:48 -05:00
|
|
|
function makeTTSDeck(
|
|
|
|
busy_props,
|
|
|
|
image_format,
|
|
|
|
image_resolution,
|
|
|
|
cards,
|
|
|
|
copies_list
|
|
|
|
) {
|
2021-09-30 02:35:13 -04:00
|
|
|
const pages = [];
|
2021-09-29 18:12:09 -04:00
|
|
|
|
2021-09-30 03:00:32 -04:00
|
|
|
busy_props.title = "Processing Cards";
|
|
|
|
busy_props.maximumProgress = cards.length;
|
|
|
|
|
2022-01-03 13:10:48 -05:00
|
|
|
for (
|
|
|
|
let page_num = 0;
|
|
|
|
page_num * TTS_CARDS_PER_IMAGE < cards.length;
|
|
|
|
page_num++
|
|
|
|
) {
|
|
|
|
let page_cards = cards.slice(
|
|
|
|
page_num * TTS_CARDS_PER_IMAGE,
|
|
|
|
(page_num + 1) * TTS_CARDS_PER_IMAGE
|
|
|
|
);
|
2021-09-30 02:35:13 -04:00
|
|
|
printf("Making page %d, with %d cards:\n", page_num + 1, page_cards.length);
|
2022-01-03 13:10:48 -05:00
|
|
|
pages.push(
|
|
|
|
new TTSDeckPage(
|
|
|
|
busy_props,
|
|
|
|
image_format,
|
|
|
|
image_resolution,
|
|
|
|
page_num + 1,
|
|
|
|
page_cards,
|
|
|
|
copies_list
|
|
|
|
)
|
|
|
|
);
|
2021-09-30 03:00:32 -04:00
|
|
|
if (busy_props.cancelled) return [,];
|
2021-09-30 02:35:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const deck_json = TTSJson.makeDeckJSON(pages);
|
|
|
|
|
2022-01-03 13:10:48 -05:00
|
|
|
return [deck_json, pages.map((page) => page.deck_image)];
|
2021-09-29 18:12:09 -04:00
|
|
|
}
|
|
|
|
|
2022-01-01 21:37:06 -05:00
|
|
|
function settingsDialog(deck_task) {
|
|
|
|
const task_settings = deck_task.getSettings();
|
|
|
|
|
2022-01-01 21:22:18 -05:00
|
|
|
const image_format_field = comboBox([
|
|
|
|
ImageUtils.FORMAT_JPEG,
|
2022-01-03 13:10:48 -05:00
|
|
|
ImageUtils.FORMAT_PNG,
|
2022-01-01 21:22:18 -05:00
|
|
|
]);
|
2022-01-03 13:10:48 -05:00
|
|
|
image_format_field.setSelectedItem(
|
|
|
|
task_settings.get("tts_image_format", "jpg")
|
|
|
|
);
|
|
|
|
const resolution_field = textField(
|
|
|
|
task_settings.get("tts_image_resolution", "200"),
|
|
|
|
15
|
|
|
|
);
|
2022-01-01 21:22:18 -05:00
|
|
|
|
2022-01-01 21:37:06 -05:00
|
|
|
const clear_cache_button = button("Clear Cache", undefined, function (e) {
|
2022-01-03 13:10:48 -05:00
|
|
|
const cache_dir = new File(deck_task.file, ".ttsdeck_cache");
|
2022-01-01 21:37:06 -05:00
|
|
|
cache_dir.listFiles().forEach((file) => file.delete());
|
|
|
|
});
|
2022-01-01 21:22:18 -05:00
|
|
|
|
|
|
|
const panel = new Grid();
|
2022-01-03 13:10:48 -05:00
|
|
|
// prettier-ignore
|
2022-01-01 21:22:18 -05:00
|
|
|
panel.place(
|
|
|
|
"Image Format", "",
|
|
|
|
image_format_field, "grow,span",
|
|
|
|
"Resolution", "",
|
2022-01-01 21:37:06 -05:00
|
|
|
resolution_field, "grow,span",
|
|
|
|
clear_cache_button, "grow,span"
|
2022-01-01 21:22:18 -05:00
|
|
|
);
|
2022-01-03 13:10:48 -05:00
|
|
|
const close_button = panel.createDialog("TTS Export").showDialog();
|
|
|
|
return [
|
|
|
|
close_button,
|
|
|
|
image_format_field.getSelectedItem(),
|
|
|
|
Number(resolution_field.text),
|
|
|
|
];
|
2022-01-01 21:22:18 -05:00
|
|
|
}
|
|
|
|
|
2021-09-16 14:05:40 -04:00
|
|
|
function run() {
|
|
|
|
const ttsDeckAction = JavaAdapter(TaskAction, {
|
2022-01-03 13:10:48 -05:00
|
|
|
getLabel: () => "Generate TTS Deck",
|
|
|
|
getActionName: () => "ttsdeck",
|
2021-09-16 14:05:40 -04:00
|
|
|
// Applies to Deck Tasks
|
|
|
|
appliesTo: function appliesTo(project, task, member) {
|
2022-01-03 13:10:48 -05:00
|
|
|
if (member != null || task == null) {
|
2021-09-16 14:05:40 -04:00
|
|
|
return false;
|
2022-01-03 13:10:48 -05:00
|
|
|
}
|
|
|
|
const type = task.settings.get(Task.KEY_TYPE);
|
|
|
|
if (NewTaskType.DECK_TYPE.equals(type)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2021-09-16 14:05:40 -04:00
|
|
|
},
|
|
|
|
perform: function perform(project, task, member) {
|
2022-01-01 21:22:18 -05:00
|
|
|
let deck_task = ProjectUtilities.simplify(project, task, member);
|
2022-01-03 13:10:48 -05:00
|
|
|
const [close_button, image_format, image_resolution] =
|
|
|
|
settingsDialog(deck_task);
|
2022-01-01 21:22:18 -05:00
|
|
|
|
|
|
|
// User canceled the dialog or closed it without pressing ok
|
|
|
|
if (close_button != 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// persist settings
|
2022-01-01 21:37:06 -05:00
|
|
|
const task_settings = deck_task.getSettings();
|
2022-01-01 21:22:18 -05:00
|
|
|
task_settings.set("tts_image_format", image_format);
|
|
|
|
task_settings.set("tts_image_resolution", image_resolution);
|
|
|
|
deck_task.writeTaskSettings();
|
|
|
|
|
2021-09-16 14:05:40 -04:00
|
|
|
Eons.setWaitCursor(true);
|
|
|
|
try {
|
2021-09-30 03:00:32 -04:00
|
|
|
Thread.busyWindow(
|
2022-01-03 13:10:48 -05:00
|
|
|
(busy_props) =>
|
|
|
|
this.performImpl(
|
|
|
|
busy_props,
|
|
|
|
image_format,
|
|
|
|
image_resolution,
|
|
|
|
deck_task
|
|
|
|
),
|
|
|
|
"Setting up...",
|
|
|
|
true
|
|
|
|
);
|
2021-09-16 14:05:40 -04:00
|
|
|
} catch (ex) {
|
|
|
|
Error.handleUncaught(ex);
|
|
|
|
} finally {
|
|
|
|
Eons.setWaitCursor(false);
|
|
|
|
}
|
|
|
|
},
|
2022-01-03 13:10:48 -05:00
|
|
|
performImpl: function performImpl(
|
|
|
|
busy_props,
|
|
|
|
image_format,
|
|
|
|
image_resolution,
|
|
|
|
member
|
|
|
|
) {
|
2021-09-18 01:26:15 -04:00
|
|
|
let copies_list;
|
|
|
|
try {
|
|
|
|
copies_list = new CopiesList(member);
|
|
|
|
} catch (ex) {
|
|
|
|
copies_list = new CopiesList();
|
2022-01-03 13:10:48 -05:00
|
|
|
alert(
|
|
|
|
"unable to read copies list, using card count of 2 for all files",
|
|
|
|
true
|
|
|
|
);
|
2021-09-18 01:26:15 -04:00
|
|
|
}
|
|
|
|
|
2021-09-16 14:05:40 -04:00
|
|
|
const children = member.getChildren();
|
2022-01-03 13:07:09 -05:00
|
|
|
const cards = children
|
2022-01-03 13:10:48 -05:00
|
|
|
.map((child) => {
|
|
|
|
if (ProjectUtilities.matchExtension(child, "eon")) {
|
2022-01-03 13:07:09 -05:00
|
|
|
let card = new Card(child);
|
|
|
|
if (card.component.isDeckLayoutSupported()) {
|
|
|
|
return card;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
})
|
2022-01-03 13:10:48 -05:00
|
|
|
.filter((card) => card !== undefined);
|
|
|
|
|
|
|
|
const [deck_json, deck_images] = makeTTSDeck(
|
|
|
|
busy_props,
|
|
|
|
image_format,
|
|
|
|
image_resolution,
|
|
|
|
cards,
|
|
|
|
copies_list
|
|
|
|
);
|
2021-09-30 03:00:32 -04:00
|
|
|
if (busy_props.cancelled) return;
|
2022-01-03 13:10:48 -05:00
|
|
|
const saved_object = TTSJson.makeSavedObjectJSON(
|
|
|
|
[deck_json],
|
|
|
|
member.getName()
|
|
|
|
);
|
2021-09-16 16:46:15 -04:00
|
|
|
|
2021-09-30 03:00:32 -04:00
|
|
|
busy_props.status = "";
|
|
|
|
busy_props.maximumProgress = -1;
|
|
|
|
busy_props.title = "Writing JSON";
|
2022-01-03 13:10:48 -05:00
|
|
|
const json_file = new File(member.file, member.getName() + ".json");
|
|
|
|
ProjectUtilities.writeTextFile(
|
|
|
|
json_file,
|
|
|
|
JSON.stringify(saved_object, null, 4)
|
|
|
|
);
|
2021-09-16 16:46:15 -04:00
|
|
|
|
2021-09-30 03:00:32 -04:00
|
|
|
busy_props.title = "Writing Images";
|
|
|
|
busy_props.maximumProgress = deck_images.length;
|
2021-09-30 02:35:13 -04:00
|
|
|
deck_images.forEach((deck_image, index) => {
|
2021-09-30 03:00:32 -04:00
|
|
|
busy_props.currentProgress = index;
|
2022-01-01 22:22:51 -05:00
|
|
|
const image_file = getImageFile(member, image_format, index + 1);
|
2022-01-03 13:10:48 -05:00
|
|
|
ImageUtils.write(
|
|
|
|
deck_image,
|
|
|
|
image_file,
|
|
|
|
image_format,
|
|
|
|
-1,
|
|
|
|
false,
|
|
|
|
image_resolution
|
|
|
|
);
|
2021-09-30 02:35:13 -04:00
|
|
|
});
|
2021-09-16 14:05:40 -04:00
|
|
|
|
2022-01-03 13:07:09 -05:00
|
|
|
let back_image = cards[0].makeImageUncached(image_resolution, true);
|
2022-01-01 22:22:51 -05:00
|
|
|
const back_image_file = getImageFile(member, image_format, "back");
|
2022-01-03 13:10:48 -05:00
|
|
|
ImageUtils.write(
|
|
|
|
back_image,
|
|
|
|
back_image_file,
|
|
|
|
image_format,
|
|
|
|
-1,
|
|
|
|
false,
|
|
|
|
image_resolution
|
|
|
|
);
|
2022-01-01 22:22:51 -05:00
|
|
|
|
2021-09-16 14:05:40 -04:00
|
|
|
member.synchronize();
|
2022-01-03 13:10:48 -05:00
|
|
|
},
|
2021-09-16 14:05:40 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
ActionRegistry.register(ttsDeckAction, Actions.PRIORITY_IMPORT_EXPORT);
|
|
|
|
}
|