-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDragTransfer.js
105 lines (70 loc) · 2.63 KB
/
DragTransfer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// DragTransfer
// (c) 2021 David Zvekic
"use strict";
import {registerSettings} from './settings/settings.js';
let dragTransfer= new Object();
Hooks.once('init',()=>{
registerSettings();
});
function isSuperset(set, subset) {
for (let elem of subset) {
if (!set.has(elem)) {
return false
}
}
return true
}
Hooks.on('dropActorSheetData',(dragTarget,sheet,dragSource,user)=>{
function isAlt(){
// check if Alt and only Alt is being pressed during the drop event.
let downKeys;
let alt;
if (typeof game.keyboard.downKeys !== 'undefined') {
// FoundyVTT v9+ compatible
downKeys = game.keyboard.downKeys;
alt = new Set(['AltLeft']);
} else {
// FoundryVTT v7, v8 compatible
downKeys = game.keyboard._downKeys;
alt = new Set(['Alt']);
}
return (isSuperset(alt,downKeys) && isSuperset(downKeys,alt));
}
if (isAlt()) return; // ignore Drag'N'Transfer when Alt is pressed to drop.
if(dragSource.type=="Item" && dragSource.actorId) {
if(!dragTarget.data._id) {
console.warn("Drag'n'Transfer - target has no data._id?",dragTarget);
return;
}
if(dragTarget.data._id == dragSource.actorId) return; // ignore dropping on self
let sourceActor = game.actors.get(dragSource.actorId);
if(sourceActor) {
// if both source and target have the same type then allow deleting original item.
// this is a safety check because some game systems may allow dropping on targets
// that don't actually allow the GM or player to see the inventory, making the item
// inaccessible.
function checkCompatable(a,b){
console.info('DragNTransfer - Check Compatability: Dragging Item:"' + String(dragSource.data.type) + '" from sourceActor.data.type:"' +String(a) +'" to dragTarget.data.type:"'+String(b)+'".');
if(a==b) return true;
try {
const transferPairs = JSON.parse("{"+ game.settings.get('DragTransfer', 'actorTransferPairs') +"}");
if(transferPairs[a]==b) return true;
if(transferPairs[b]==a) return true;
}
catch(err){
console.error('DragTransfer: ',err.message);
ui.notifications.error('DragTransfer: '+err.message);
}
return false;
};
if ( checkCompatable(sourceActor.data.type,dragTarget.data.type) ) {
if(sourceActor.deleteEmbeddedDocuments!=undefined) {
sourceActor.deleteEmbeddedDocuments( "Item",[dragSource.data._id]);
} else
{
sourceActor.deleteOwnedItem( dragSource.data._id);
}
}
}
}
});