Argonius-Angelus / lancer-clocks

A module that lets you create Lancer-themed progress clocks as seen in No Room For a Wallflower Act 1 (https://massif-press.itch.io/no-room-for-a-wallflower-act-1) within Foundry VTT (https://foundryvtt.com/). This is a modification of the original Blades in the Dark-esque progress clocks (https://foundryvtt.com/packages/clocks/) made by troygoode.
MIT License
3 stars 4 forks source link

Macro for Clocks #5

Open Galdormin opened 2 years ago

Galdormin commented 2 years ago

Hi, here are some macros I use to easily create, increase and decrease the value of clocks.

New Clock Macro

Allows you to create a new clock. The lists folders, sizes and themes are used to set the options for the clock creation. For folders, only the name should be used (even if it is a sub-folder).

const folders = ["Clocks", "Projets"];
const sizes = [2, 3, 4, 5, 6, 8, 10, 12];
const themes = ['lancer_gms_red', 'lancer_gms_red_grey', 'lancer_wallflower_green', 'lancer_wallflower_green_grey'];
main();

async function main() {

  let sizeList;
  sizes.map((el) => {      
    sizeList += `<option value="${el}">${el}</option>`;      
  }); 

  let themeList;
  themes.map((el) => {      
    themeList += `<option value="${el}">${el}</option>`;      
  }); 

  let folderList;
  folders.map((el) => {      
    folderList += `<option value="${el}">${el}</option>`;      
  }); 

  new Dialog({
    title: `Create a new Clock`,
    content: `
    <h2>Create a New Clock</h2>
    <p><input type="text" id="clockName" style="width: 100%" value="New Clock"/></p>
    <hr>
    <p>
      <i class="fa fa-palette"></i>
      <select id="clockTheme" style="width: 78%">${themeList}</select>
      <i class="fa fa-chart-pie"></i>
      <select id="clockSize" style="width: 12%">${sizeList }</select>
    </p>
    <p>
      <i class="fa fa-folder-open"></i>
      <select id="clockFolder" style="width: 95%">${folderList }</select>
    </p>
    `,
    buttons: {
      roll: {
        label: "Create",
        callback: (html) => {
          createClock(html);
        }
      }, 
      cancel: {
        label: "Cancel"
      }
    }
  }).render(true)
}

async function createClock(html) {
  let clockName = html.find("#clockName")[0].value;
  let size = html.find("#clockSize")[0].value;
  let theme = html.find("#clockTheme")[0].value;
  let folder = game.folders.getName(html.find("#clockFolder")[0].value);

  const imgLink = '/modules/lancer-clocks/themes/' + theme + '/' + size + 'clock_0.png';

  await Actor.create({
    name: clockName,
    type: "🕛 clock",
    img: imgLink,
    actorLink: true,
    folder: folder.id,
    ['flags.lancer-clocks.progress']: 0,
    ['flags.lancer-clocks.size']: size,
    ['flags.lancer-clocks.theme']: theme,
    token: {
      disposition: 0,
      displayName: 50,
      scale: 1
    }
  });
}

Increase Clock Value

Increase the value of a clock from its token. The value of the token, the actor and all other tokens are increased by 1.

if (!actor) {
  ui.notifications.warn('You need to select a token before using this macro!');
}else if (actor.data.type != "🕛 clock") {
  ui.notifications.warn('The selected token is not a Clock!!!');
}else{
  const progress = actor.getFlag("lancer-clocks", "progress") + 1; 
  const size = actor.getFlag("lancer-clocks", "size");
  const theme = actor.getFlag("lancer-clocks", "theme");
  const imgLink = '/modules/lancer-clocks/themes/' + theme + '/' + size + 'clock_' + progress + '.png';

  if(progress > size) {
    ui.notifications.warn('The Clock is already filled!!!');
  }else{
    // Update Token
    for (const t of actor.getActiveTokens()) {
      await t.document.update({
        name: actor.name,
        img: imgLink,
        actorLink: true
      });
    }

    // Update Actor
    const updateData = {
      ['flags.lancer-clocks.progress']: progress,
      img: imgLink,
      token: {img: imgLink}
    };
    await actor.update(updateData);
  }
}

Decrease Clock Value

Decrease the value of a clock from its token. The value of the token, the actor and all other tokens are decreased by 1.

if (!actor) {
  ui.notifications.warn('You need to select a token before using this macro!');
}else if (actor.data.type != "🕛 clock") {
  ui.notifications.warn('The selected token is not a Clock!!!');
}else{
  const progress = actor.getFlag("lancer-clocks", "progress") - 1; 
  const size = actor.getFlag("lancer-clocks", "size");
  const theme = actor.getFlag("lancer-clocks", "theme");
  const imgLink = '/modules/lancer-clocks/themes/' + theme + '/' + size + 'clock_' + progress + '.png';

  if(progress < 0) {
    ui.notifications.warn('The Clock is already cleared!!!');
  }else{
    // Update Token
    for (const t of actor.getActiveTokens()) {
      await t.document.update({
        name: actor.name,
        img: imgLink,
        actorLink: true
      });
    }

    // Update Actor
    const updateData = {
      ['flags.lancer-clocks.progress']: progress,
      img: imgLink,
      token: {img: imgLink}
    };
    await actor.update(updateData);
  }
}
theredspoon commented 1 year ago

I updated the increment and decrement macros to:

Clock + 1

if (!actor) {
  ui.notifications.warn('Select a Clock before using this macro.');
}else if (actor.type != "🕛 clock") {
  ui.notifications.warn('The selected token is not a Clock.');
}else{
  const progress = actor.getFlag("lancer-clocks", "progress") + 1; 
  const size = actor.getFlag("lancer-clocks", "size");
  const theme = actor.getFlag("lancer-clocks", "theme");
  const imgLink = '/lancer-clocks/' + theme + '/' + size + 'clock_' + progress + '.png';

  if(progress > size) {
    ui.notifications.warn('The Clock is already filled.');
  }else{
    // Update Token
    for (const t of actor.getActiveTokens()) {
      await t.document.update({
        name: actor.name,
        img: imgLink,
        actorLink: true,
        light: {
            animation: {
                speed: Math.round(10 / size * progress),
                type: "pulse"
            }
        }
      });
    }

    // Update Actor
    const updateData = {
      ['flags.lancer-clocks.progress']: progress,
      img: imgLink,
      token: {img: imgLink}
    };
    await actor.update(updateData);
  }
}

Clock - 1

if (!actor) {
  ui.notifications.warn('Select a Clock to use this macro.');
}else if (actor.type != "🕛 clock") {
  ui.notifications.warn('The selected token is not a Clock.');
}else{
  const progress = actor.getFlag("lancer-clocks", "progress") - 1; 
  const size = actor.getFlag("lancer-clocks", "size");
  const theme = actor.getFlag("lancer-clocks", "theme");
  const imgLink = '/lancer-clocks/' + theme + '/' + size + 'clock_' + progress + '.png';

  if(progress < 0) {
    ui.notifications.warn('The Clock is already cleared.');
  }else{
    // Update Token
    for (const t of actor.getActiveTokens()) {
      await t.document.update({
        name: actor.name,
        img: imgLink,
        actorLink: true,
        light: {
            animation: {
                speed: Math.round( 10 / size * progress),
                type: "pulse"
            }
        }
      });
    }

    // Update Actor
    const updateData = {
      ['flags.lancer-clocks.progress']: progress,
      img: imgLink,
      token: {img: imgLink}
    };
    await actor.update(updateData);
  }
}
BBlayne commented 1 year ago

Hi, how do I use the macro? There seems to be an emoji in it and googling what "Actor.type" refers to in the Foundry API has yielded no results; what should this line be? How do I set the "type" an actor, or know an actor's type to know what to change the macro to?