{"size": 1722, "ext": "js", "max_stars_repo_path": "js/plugins/mz/H2A_ImageToPicture/src/main.js", "max_stars_repo_name": "katai5plate/RPGMakerPlugins", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "js/plugins/mz/H2A_ImageToPicture/src/main.js", "max_issues_repo_name": "katai5plate/RPGMakerPlugins", "max_issues_repo_issues_event_min_datetime": "2021-09-17T23:12:11.000Z", "max_issues_repo_issues_event_max_datetime": "2021-10-20T17:23:20.000Z", "max_forks_repo_path": "js/plugins/mz/H2A_ImageToPicture/src/main.js", "max_forks_repo_name": "katai5plate/RPGMakerPlugins", "max_forks_repo_forks_event_min_datetime": "2021-09-20T19:27:33.000Z", "max_forks_repo_forks_event_max_datetime": "2021-09-20T19:27:33.000Z"}
{"max_stars_count": null, "max_issues_count": 7, "max_forks_count": 1, "avg_line_length": 30.2105263158, "max_line_length": 75, "alphanum_fraction": 0.5940766551}
const pluginName = document.currentScript.src.match(/^.*\/(.*).js$/)[1];
const {} = PluginManager.parameters(pluginName);

const bitmapAsyncLoad = (url) => {
  const bitmap = Bitmap.load(url);
  return new Promise((resolve, reject) => {
    const isOk = () => bitmap._loadingState === "loaded";
    const isNg = () => bitmap._loadingState === "error";
    const i = setInterval(() => {
      if (isOk()) {
        clearInterval(i);
        resolve(bitmap);
      }
      if (isNg()) {
        clearInterval(i);
        reject(new Error("存在しない画像: " + url));
      }
    }, 1);
  });
};
window.bitmapAsyncLoad = bitmapAsyncLoad;
const getPicture = (pictureId) =>
  SceneManager._scene.children.find(
    (c) => c.constructor.name === "Spriteset_Map"
  )._pictureContainer.children[pictureId - 1];

PluginManager.registerCommand(
  pluginName,
  "change",
  ({ _id, _name, _index, _splitCountX, _splitCountY }) => {
    const [id, name, index, splitCountX, splitCountY] = [
      Number(_id),
      _name,
      Number(_index),
      Number(_splitCountX),
      Number(_splitCountY),
    ];
    const picture = getPicture(id);
    bitmapAsyncLoad("img/" + name + ".png").then((loadedBitmap) => {
      const paper = new Bitmap(
        loadedBitmap.width / splitCountX,
        loadedBitmap.height / splitCountY
      );
      const [pw, ph] = [paper.width, paper.height];
      paper.context.drawImage(
        loadedBitmap.context.canvas,
        Math.floor((index % splitCountX) * pw),
        Math.floor(Math.floor(index / splitCountX) * ph),
        ...[pw, ph, 0, 0, pw, ph]
      );
      paper.context.canvas.toBlob(
        (blob) => (picture.bitmap = Bitmap.load(URL.createObjectURL(blob)))
      );
    });
  }
);
