Skip to content

Commit

Permalink
Feat - allow players to place extras as tokens.
Browse files Browse the repository at this point in the history
  • Loading branch information
Azmoria committed Sep 22, 2023
1 parent de179f6 commit d461057
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 8 deletions.
41 changes: 40 additions & 1 deletion CharactersPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,44 @@ $(function() {

let recentCharacterUpdates = {};

const debounce_add_extras = mydebounce(() => {
if (is_abovevtt_page()) {
const extraRows = $('.ct-extra-row')
const thisPC = window.pcs.filter(d => d.characterId == window.PLAYER_ID);
for(let i=0; i<extraRows.length; i++){

$(extraRows[i]).append($(`<button class='add-monster-token-to-vtt' data-index=${i}>+</button>`))
}

$('.add-monster-token-to-vtt').off('click.addExtra').on('click.addExtra', async function(){
const centerView = center_of_view();
let playerData = await DDBApi.fetchCharacterDetails([window.PLAYER_ID])
let monsterData = playerData[0].extras.creatures[$(this).attr('data-index')];

let extraOptions = {
hitPointInfo: {
current: $(this).parent().find('.ct-extra-row__hp-value--current').text(),
maximum: $(this).parent().find('.ct-extra-row__hp-value--total').text(),
},
armorClass: $(this).parent().find('.ct-extra-row__ac').text(),
sizeId: monsterData.sizeId,
name: monsterData.name
}

let centerMap = convert_point_from_view_to_map(centerView.x, centerView.y)
window.MB.sendMessage("custom/myVTT/place-extras-token", {
monsterData: monsterData,
centerView: centerMap,
sceneId: window.CURRENT_SCENE_DATA.id,
extraOptions: extraOptions
});
})



}

}, 100);

const sendCharacterUpdateEvent = mydebounce(() => {
if (window.DM) return;
Expand Down Expand Up @@ -623,7 +660,7 @@ function observe_character_sheet_changes(documentToObserve) {
}
}



if(mutationTarget.hasClass("ddbc-tab-list__content")){
if (!is_player_sheet_full_width()) {
Expand Down Expand Up @@ -656,6 +693,8 @@ function observe_character_sheet_changes(documentToObserve) {
send_senses();
} else if (mutationTarget.hasClass("ct-status-summary-mobile__button--interactive") && mutationTarget.text() === "Inspiration") {
character_sheet_changed({inspiration: mutationTarget.hasClass("ct-status-summary-mobile__button--active")});
} else if(mutationParent.find('[class*="ct-extras"]').length>0 && mutationParent.find('.add-monster-token-to-vtt').length==0){
debounce_add_extras();
}

break;
Expand Down
4 changes: 2 additions & 2 deletions Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function parse_img(url) {
console.warn("parse_img is removing a data url because those are not allowed");
retval = "";
} else if (retval.startsWith("https://drive.google.com") && retval.indexOf("uc?id=") < 0) {
const parsed = 'https://drive.google.com/uc?id=' + retval.split('/')[5];
const parsed = 'https://drive.google.com/uc?id=' + retval.split('/')[5]+"&export=media";
console.log("parse_img is converting", url, "to", parsed);
retval = parsed;
}
Expand Down Expand Up @@ -427,7 +427,7 @@ async function load_scenemap(url, is_video = false, width = null, height = null,
}
else if (is_video === "0" || !is_video) {
$("#scene_map_container").toggleClass('video', false);
let newmap = $(`<img id='scene_map' src='${url}' style='position:absolute;top:0;left:0;z-index:10'>`);
let newmap = $(`<img id='scene_map' src='${url}&date=${new Date().getTime()}' style='position:absolute;top:0;left:0;z-index:10'>`);


if(UVTTFile && width != null){
Expand Down
30 changes: 27 additions & 3 deletions MessageBroker.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ function addVideo(stream,streamerid) {
const red = frame.data[i + 0];
const green = frame.data[i + 1];
const blue = frame.data[i + 2];
if ((red < 8) && (green < 8) && (blue < 8))
if ((red < 24) && (green < 24) && (blue < 24))
frame.data[i + 3] = 128;
if ((red < 4) && (green < 4) && (blue < 4))
if ((red < 8) && (green < 8) && (blue < 8))
frame.data[i + 3] = 0;


Expand Down Expand Up @@ -474,7 +474,8 @@ class MessageBroker {
"custom/myVTT/drawing",
"custom/myVTT/drawdata",
"custom/myVTT/highlight",
"custom/myVTT/pointer"
"custom/myVTT/pointer",
"custom/myVTT/place-extras-token"
].includes(msg.eventType)) {
console.log("skipping msg from a different scene");
return;
Expand All @@ -501,6 +502,29 @@ class MessageBroker {
}
}
}
if(msg.eventType == "custom/myVTT/place-extras-token"){
if(window.DM){
let left = parseInt(msg.data.centerView.x);
let top = parseInt(msg.data.centerView.y);

fetch_and_cache_monsters([msg.data.monsterId]);
create_and_place_token(window.cached_monster_items[msg.data.monsterId], undefined, undefined, left, top, undefined, undefined, true);

}
}



if(msg.eventType == "custom/myVTT/place-extras-token"){
if(window.DM){
let left = parseInt(msg.data.centerView.x);
let top = parseInt(msg.data.centerView.y);
let monsterId = msg.data.monsterData.baseId;
fetch_and_cache_monsters([monsterId]);
create_and_place_token(window.cached_monster_items[monsterId], undefined, undefined, left, top, undefined, undefined, true, msg.data.extraOptions);

}
}

if (msg.eventType === "custom/myVTT/fetchscene") {
if (window.startupSceneId === msg.data.sceneid) {
Expand Down
16 changes: 14 additions & 2 deletions TokensPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,10 @@ function update_pc_token_rows() {
* @param disableSnap {boolean} if true, tokens will not snap to the grid. This is false by default and only used when placing multiple tokens
* @param nameOverride {string} if present will override the list items name with this name. This is for dragging out player aoe tokens from sheets
*/
function create_and_place_token(listItem, hidden = undefined, specificImage= undefined, eventPageX = undefined, eventPageY = undefined, disableSnap = false, nameOverride = "") {


function create_and_place_token(listItem, hidden = undefined, specificImage= undefined, eventPageX = undefined, eventPageY = undefined, disableSnap = false, nameOverride = "", mapPoint=false, extraOptions={}) {


if (listItem === undefined) {
console.warn("create_and_place_token was called without a listItem");
Expand Down Expand Up @@ -1137,16 +1140,25 @@ function create_and_place_token(listItem, hidden = undefined, specificImage= und
}
options.imgsrc = random_image_for_item(listItem, specificImage);
// TODO: figure out if we still need to do this, and where they are coming from
if(extraOptions != {}){
options = {
...options,
...extraOptions
}
}
delete options.undefined;
delete options[""];
console.log("create_and_place_token about to place token with options", options, hidden);

if (eventPageX === undefined || eventPageY === undefined) {
place_token_in_center_of_view(options);
} else {
} else if(mapPoint==false){
let mapPosition = convert_point_from_view_to_map(eventPageX, eventPageY, disableSnap);
place_token_at_map_point(options, mapPosition.x, mapPosition.y);
}
else{
place_token_at_map_point(options, eventPageX, eventPageY);
}
}

/**
Expand Down

0 comments on commit d461057

Please sign in to comment.