Skip to content

Commit

Permalink
add new token blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Miah Nelah committed Sep 1, 2022
1 parent f52002a commit 6ec9dcb
Show file tree
Hide file tree
Showing 5 changed files with 406 additions and 58 deletions.
23 changes: 22 additions & 1 deletion languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,28 @@
"LibBlockly.Blocks.Token.GetAllTokensInScene.HelpUrl": "",
"LibBlockly.Blocks.Token.GetAllTokensInScene.Colour": "230",


"LibBlockly.Blocks.Token.RotateToken.Title": "Rotate token %1 %2 %3 °",
"LibBlockly.Blocks.Token.RotateToken.Tooltip": "",
"LibBlockly.Blocks.Token.RotateToken.HelpUrl": "",
"LibBlockly.Blocks.Token.RotateToken.Colour": "230",
"LibBlockly.Blocks.Token.RotateToken.AngleMode.By": "by",
"LibBlockly.Blocks.Token.RotateToken.AngleMode.To": "to",

"LibBlockly.Blocks.Token.SetTokenScale.Title": "Set token %1 scale to %2",
"LibBlockly.Blocks.Token.SetTokenScale.Tooltip": "",
"LibBlockly.Blocks.Token.SetTokenScale.HelpUrl": "",
"LibBlockly.Blocks.Token.SetTokenScale.Colour": "230",

"LibBlockly.Blocks.Token.ResetTokenScale.Title": "Reset token %1 scale",
"LibBlockly.Blocks.Token.ResetTokenScale.Tooltip": "",
"LibBlockly.Blocks.Token.ResetTokenScale.HelpUrl": "",
"LibBlockly.Blocks.Token.ResetTokenScale.Colour": "230",

"LibBlockly.Blocks.Token.MoveToken.Title": "Move token %1 %2 %3 %4",
"LibBlockly.Blocks.Token.MoveToken.Tooltip": "",
"LibBlockly.Blocks.Token.MoveToken.HelpUrl": "",
"LibBlockly.Blocks.Token.MoveToken.Colour": "230",


"LibBlockly.Blocks.Roll.Roll.Title": "Roll %1",
"LibBlockly.Blocks.Roll.Roll.Tooltip": "",
Expand Down
26 changes: 23 additions & 3 deletions languages/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,29 @@
"LibBlockly.Blocks.Token.GetAllTokensInScene.HelpUrl": "",
"LibBlockly.Blocks.Token.GetAllTokensInScene.Colour": "230",



"LibBlockly.Blocks.Roll.Roll.Title": "Lander un dé %1",
"LibBlockly.Blocks.Token.RotateToken.Title": "Tourner le jeton %1 %2 %3 °",
"LibBlockly.Blocks.Token.RotateToken.Tooltip": "",
"LibBlockly.Blocks.Token.RotateToken.HelpUrl": "",
"LibBlockly.Blocks.Token.RotateToken.Colour": "230",
"LibBlockly.Blocks.Token.RotateToken.AngleMode.By": "de",
"LibBlockly.Blocks.Token.RotateToken.AngleMode.To": "à",

"LibBlockly.Blocks.Token.SetTokenScale.Title": "Régler la taille du jeton %1 à un ratio de %2",
"LibBlockly.Blocks.Token.SetTokenScale.Tooltip": "",
"LibBlockly.Blocks.Token.SetTokenScale.HelpUrl": "",
"LibBlockly.Blocks.Token.SetTokenScale.Colour": "230",

"LibBlockly.Blocks.Token.ResetTokenScale.Title": "Réinitialiser la taille du jeton %1",
"LibBlockly.Blocks.Token.ResetTokenScale.Tooltip": "",
"LibBlockly.Blocks.Token.ResetTokenScale.HelpUrl": "",
"LibBlockly.Blocks.Token.ResetTokenScale.Colour": "230",

"LibBlockly.Blocks.Token.MoveTokenScale.Title": "Déplacer le jeton %1 de %2 %3 %4",
"LibBlockly.Blocks.Token.MoveTokenScale.Tooltip": "",
"LibBlockly.Blocks.Token.MoveTokenScale.HelpUrl": "",
"LibBlockly.Blocks.Token.MoveTokenScale.Colour": "230",

"LibBlockly.Blocks.Roll.Roll.Title": "Lancer un dé %1",
"LibBlockly.Blocks.Roll.Roll.Tooltip": "",
"LibBlockly.Blocks.Roll.Roll.HelpUrl": "",
"LibBlockly.Blocks.Roll.Roll.Colour": "230",
Expand Down
97 changes: 95 additions & 2 deletions src/BlockManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {LibBlockly} from "./LibBlockly.js";
import { LibBlockly } from "./LibBlockly.js";

/**
* @typedef {Object} CustomBlock
Expand Down Expand Up @@ -32,6 +32,99 @@ export class BlockManager {
* @private
*/
_wrapInit(block) {
return mergeObject({type: block.key, ...block.init()});
return mergeObject({ type: block.key, ...block.init() });
}
}

export class Helpers {

async rotateToken(token, angle, mode = "by") {
if (canvas === undefined || canvas.scene === undefined) return;
if (token === undefined) return;
if (angle === undefined || typeof angle !== 'number') return;
if (!["by", "to"].includes(mode.toLowerCase())) return;

if (Array.isArray(token) && token.length > 0) {
await canvas.scene.updateEmbeddedDocuments("Token", token.map(t => {
const originalRotation = mode === "by" ? t.document.rotation : 0;
return { _id: t.id, rotation: originalRotation + angle };
}));
} else if (token instanceof Token) {
const originalRotation = mode === "by" ? token.document.rotation : 0;
await canvas.scene.updateEmbeddedDocuments("Token", ({ _id: token.id, rotation: originalRotation + angle }));
}
}

async setTokenVisibility(token, hidden) {
if (canvas === undefined || canvas.scene === undefined) return;
if (token === undefined) return;
if (hidden === undefined || typeof hidden !== 'boolean') return;

if (Array.isArray(token) && token.length > 0) {
await canvas.scene.updateEmbeddedDocuments("Token", token.map(t => ({ _id: t.id, hidden: hidden })));
} else if (token instanceof Token) {
await canvas.scene.updateEmbeddedDocuments("Token", { _id: token.id, hidden: hidden });
}
}

async toggleTokenVisibility(token) {
if (canvas === undefined || canvas.scene === undefined) return;
if (token === undefined) return;

if (Array.isArray(token) && token.length > 0) {
await Promise.all(token.map(t => t.toggleVisibility()));
} else if (token instanceof Token) {
await token.toggleVisibility();
}
}

async toggleTokenCombatState(token) {
if (canvas === undefined || canvas.scene === undefined) return;
if (token === undefined) return;

if (Array.isArray(token) && token.length > 0) {
return await Promise.all(token.map(t => t.toggleCombat()));
} else if (token instanceof Token) {
return await token.toggleCombat();
}
}

async applyVectorToToken(token, vector) {
if (canvas === undefined || canvas.scene === undefined) return;
if (token === undefined) return;
if (vector === undefined || typeof vector !== 'object' || vector.x === undefined || vector.y === undefined) return;

if (Array.isArray(token) && token.length > 0) {
return await canvas.scene.updateEmbeddedDocuments("Token", token.map(t => {
return {
_id: t.id,
x: t.document.x + vector.x,
y: t.document.y + vector.y
};
}));
} else if (token instanceof Token) {
await canvas.scene.updateEmbeddedDocuments("Token", {
_id: token.id,
x: token.document.x + vector.x,
y: token.document.y + vector.y
});
}
}

async setTokenScale(token, scale = 1.0) {
if (canvas === undefined || canvas.scene === undefined) return;
if (token === undefined) return;
if (scale === undefined || typeof scale !== 'number' || scale > 3 || scale < 0.2) return;

if (Array.isArray(token) && token.length > 0) {
return await canvas.scene.updateEmbeddedDocuments("Token", token.map(t => ({ _id: t.id, "texture.scaleX": scale, "texture.scaleY": scale })));
} else if (token instanceof Token) {
return await canvas.scene.updateEmbeddedDocuments("Token", [{ _id: token.id, "texture.scaleX": scale, "texture.scaleY": scale }]);
}
}

getAllTokensInScene(scene) {
if (scene === undefined || (!scene instanceof Scene)) return [];
return scene.tokens.contents;
}
}
10 changes: 6 additions & 4 deletions src/LibBlockly.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {BlocklyEditorSheet} from "./BlocklyEditorSheet.js";
import {ToolboxManager} from "./ToolboxManager.js";
import {BlockManager} from "./BlockManager.js";
import { BlocklyEditorSheet } from "./BlocklyEditorSheet.js";
import { ToolboxManager } from "./ToolboxManager.js";
import { BlockManager, Helpers } from "./BlockManager.js";

export const LibBlockly = {
ID: "libblockly",
toolboxManager: new ToolboxManager(),
blockManager: new BlockManager(),
helpers: new Helpers(),
backpack: [],

init: function () {
Expand All @@ -14,6 +15,7 @@ export const LibBlockly = {
this._registerSettings();
game.modules.get(this.ID).toolboxManager = this.toolboxManager;
game.modules.get(this.ID).blockManager = this.blockManager;
game.modules.get(this.ID).helpers = this.helpers;
},

_registerSettings: function () {
Expand Down Expand Up @@ -139,7 +141,7 @@ export const LibBlockly = {
const workspace = this._loadWorkspace(macro, this._buildWorkspaceConfig());
const initialCommand = macro.command;
const initialType = macro.type;
macro.command = Blockly.JavaScript.workspaceToCode(workspace);
macro.command = `const helpers = game.modules.get("libblockly").helpers;\r\n` + Blockly.JavaScript.workspaceToCode(workspace);
macro.type = "script";
let result;
try {
Expand Down
Loading

0 comments on commit 6ec9dcb

Please sign in to comment.