Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QoL - performance improvement when selecting / deseleting tokens #1579

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions Fog.js
Original file line number Diff line number Diff line change
Expand Up @@ -2265,17 +2265,17 @@ function drawing_mouseup(e) {

else if (window.DRAWFUNCTION == "select") {
// FIND TOKENS INSIDE THE AREA
var c = 0;
let c = 0;
for (let id in window.TOKEN_OBJECTS) {
var curr = window.TOKEN_OBJECTS[id];
let curr = window.TOKEN_OBJECTS[id];


let tokenImageRect = $("#tokens>div[data-id='" + curr.options.id + "'] .token-image")[0].getBoundingClientRect();
let size = window.TOKEN_OBJECTS[curr.options.id].options.size;
var toktop = (parseInt(tokenImageRect.top) + window.scrollY - window.VTTMargin) * (1.0 / window.ZOOM);
var tokleft = (parseInt(tokenImageRect.left) + window.scrollX - window.VTTMargin) * (1.0 / window.ZOOM);
var tokright = (parseInt(tokenImageRect.right) + window.scrollX - window.VTTMargin) * (1.0 / window.ZOOM);
var tokbottom = (parseInt(tokenImageRect.bottom) + window.scrollY - window.VTTMargin) * (1.0 / window.ZOOM);
let toktop = (parseInt(tokenImageRect.top) + window.scrollY - window.VTTMargin) * (1.0 / window.ZOOM);
let tokleft = (parseInt(tokenImageRect.left) + window.scrollX - window.VTTMargin) * (1.0 / window.ZOOM);
let tokright = (parseInt(tokenImageRect.right) + window.scrollX - window.VTTMargin) * (1.0 / window.ZOOM);
let tokbottom = (parseInt(tokenImageRect.bottom) + window.scrollY - window.VTTMargin) * (1.0 / window.ZOOM);
let scaledRemainderTop = (tokbottom-toktop-size)/2;
let scaledRemainderLeft = (tokright-tokleft-size)/2;
if(window.TOKEN_OBJECTS[curr.options.id].options.tokenStyleSelect == 'circle' || window.TOKEN_OBJECTS[curr.options.id].options.tokenStyleSelect == 'square' || $("#tokens>div[data-id='" + curr.options.id + "']").hasClass("isAoe")){
Expand All @@ -2293,16 +2293,15 @@ function drawing_mouseup(e) {
let tokenDiv = $("#tokens>div[data-id='" + curr.options.id + "']")
if(tokenDiv.css("pointer-events")!="none" && tokenDiv.css("display")!="none" && !tokenDiv.hasClass("ui-draggable-disabled")) {
curr.selected = true;
curr.place();
}
}

}
$("#temp_overlay").css('z-index', '25');
window.MULTIPLE_TOKEN_SELECTED = (c > 1);

redraw_fog();
draw_selected_token_bounding_box();


console.log("READY");
}
else if (window.DRAWFUNCTION == "measure") {
Expand Down
47 changes: 30 additions & 17 deletions Token.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ class Token {
await this.place(100)
this.update_and_sync();
}
draw_selected_token_bounding_box()


}

Expand Down Expand Up @@ -1644,6 +1644,7 @@ class Token {
if (typeof this.options.tokendataname !== "undefined") {
old.attr("data-tokendataname", this.options.tokendataname);
}

console.groupEnd()
}
else { // adding a new token
Expand Down Expand Up @@ -2324,7 +2325,6 @@ class Token {
let count = 0;
if (shiftHeld == false) {
deselect_all_tokens();

}
if (thisSelected == true) {
parentToken.addClass('tokenselected');
Expand Down Expand Up @@ -2367,6 +2367,8 @@ class Token {
new Promise(debounceLightChecks),
]);
console.groupEnd()

return true;
}

// key: String, numberRemaining: Number; example: track_ability("1stlevel", 2) // means they have 2 1st level spell slots remaining
Expand Down Expand Up @@ -2772,7 +2774,7 @@ function deselect_all_tokens() {
let curr = window.TOKEN_OBJECTS[id];
if (curr.selected) {
curr.selected = false;
curr.place();
$(`.token[data-id='${id}']`).toggleClass('tokenselected', false);
}
}
remove_selected_token_bounding_box();
Expand Down Expand Up @@ -3174,27 +3176,38 @@ async function do_draw_selected_token_bounding_box() {
remove_selected_token_bounding_box()
// hold a separate list of selected ids so we don't have to iterate all tokens during bulk token operations like rotation
window.CURRENTLY_SELECTED_TOKENS = [];
let promises = []
for (let id in window.TOKEN_OBJECTS) {
let promises = [];
let selected = Object.fromEntries(
Object.entries(window.TOKEN_OBJECTS).filter(
([key, val])=> window.TOKEN_OBJECTS[key].selected == true
)
);
let groupIDs = [];
for (let id in selected) {
let selector = "div[data-id='" + id + "']";

promises.push(new Promise((resolve) => {
toggle_player_selectable(window.TOKEN_OBJECTS[id], $("#tokens").find(selector));
resolve();
}));
if (window.TOKEN_OBJECTS[id].selected) {
if(!window.CURRENTLY_SELECTED_TOKENS.some(i => i == id))
window.CURRENTLY_SELECTED_TOKENS.push(id);

$(`.token[data-group-id='${window.TOKEN_OBJECTS[id].options.groupId}']`).each(function(){
if(window.CURRENTLY_SELECTED_TOKENS.some(id => id == $(this).attr('data-id')))
return;
$(this).toggleClass('tokenselected', true);
window.TOKEN_OBJECTS[$(this).attr('data-id')].selected = true;
window.CURRENTLY_SELECTED_TOKENS.push($(this).attr('data-id'));
})
}));
window.CURRENTLY_SELECTED_TOKENS.push(id);
$("#tokens").find(selector).toggleClass('tokenselected', true);
if(window.TOKEN_OBJECTS[id].options.groupId && !groupIDs.includes(window.TOKEN_OBJECTS[id].options.groupId)){
groupIDs.push(window.TOKEN_OBJECTS[id].options.groupId)
}
}

for(let index in groupIDs){
let tokens = $(`.token[data-group-id='${groupIDs[index]}']`)
tokens.each(function(){
if(window.CURRENTLY_SELECTED_TOKENS.includes($(this).attr('data-id')))
return;
$(this).toggleClass('tokenselected', true);
window.TOKEN_OBJECTS[$(this).attr('data-id')].selected = true;
window.CURRENTLY_SELECTED_TOKENS.push($(this).attr('data-id'));
})
}

Promise.allSettled(promises)
.then(() => {
if (window.CURRENTLY_SELECTED_TOKENS == undefined || window.CURRENTLY_SELECTED_TOKENS.length == 0) {
Expand Down