-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
260 lines (216 loc) · 7.8 KB
/
index.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import { cardHotbarPopulator } from './scripts/card-hotbar-populator.js';
import { cardHotbar } from './card-hotbar.js';
import { cardHotbarSettings } from './scripts/card-hotbar-settings.js';
async function cardHotbarInit() {
console.debug("Card Hotbar | Initializing...");
window.cardHotbar = new cardHotbarPopulator();
ui.cardHotbar = new cardHotbar(window.cardHotbar);
//ui.cardHotbar = ui.cardHotbar.getData(options);
let obj = {
left: 100,
top: 100,
width: 502,
height: 52,
scale: 1.0,
log: true,
renderContext: "card-hotbar",
renderData: "init"
};
cardHotbarSettings.register();
//apply settings styles, first for card hotbar, then for core hotbar
//For each setting, use flag if present, otherwise use game setting.
var css =
'#card-hotbar'
+ ` { bottom: ${cardHotbarSettings.getCHBYPos()}px; `
+ ` left: ${cardHotbarSettings.getCHBXPos()}px; `
+ ' }'
/*
+ '#card-hotbar #card-macro-list'
+ ` {`
+ ` border: 1px solid ${cardHotbarSettings.getCHBBorderColor()};`
+ ' }'
*/
+ '#card-hotbar .bar-controls'
/* Hard-coded for now */
+ ` { background: #00000080;`
+ ` border: 1px solid ${cardHotbarSettings.getCHBBorderColor()};`
+ ' }'
+ '#card-hotbar .macro'
+ ` { background: ${cardHotbarSettings.getCHBPrimaryColor()};`
+ ` border: 1px dashed ${cardHotbarSettings.getCHBBorderColor()};`
+ ' }'
+ '#card-hotbar .macro.active:hover'
+ ' {'
+ ` border: 1px solid ${cardHotbarSettings.getCHBBorderColorActive()};`
+ ' }'
+ '#card-hotbar .macro.inactive:hover'
+ ' {'
+ ` border: 1px solid ${cardHotbarSettings.getCHBBorderColorInactive()};`
+ ' }'
, head = document.head || document.getElementsByTagName('head')[0]
, style = document.createElement('style');
head.appendChild(style);
style.type = 'text/css';
style.appendChild(document.createTextNode(css));
// ui.hotbar.render();
Array.from(document.getElementsByClassName("macro")).forEach(function (element) {
element.ondragstart = ui.hotbar._onDragStart;
element.ondragend = ui.hotbar._onDrop;
});
/* Add support for dragging tile from canvas onto hotbar later
//add handler for dragging tiles onto hotbar, with thanks to Vance
let dragging = false;
//only trigger for card tiles
const ogPlaceableObject = PlaceableObject.prototype._onDragLeftStart;
PlaceableObject.prototype._onDragLeftStart = function (...args) {
const e = args[0];
let tokens = e.data.clones;
if (tokens) {
dragging = tokens[0].actor;
console.log(`Picked up: ${dragging.data.name}`);
}
return ogPlaceableObject.apply(this, args);
}
$(document).mouseup((e) => {
if(dragging) {
console.log(`Dropped: ${dragging.data.name} onto ${e.target}`);
dragging = false;
}
})
*/
ui.cardHotbar.render(true, obj);
ui.cardHotbar.getNextSlot();
}
Hooks.once("init", async () => {
CONFIG.ui.hotbar = class extends Hotbar {
_onDragStart(...arg) {
document.getElementsByClassName("tooltip")[0].style.display = "none";
super._onDragStart(...arg);
}
};
});
Hooks.once('ready', () => {
console.debug("Card Hotbar | Foundry setup...");
//Check to make sure that a hotbar rendered before initilizing so that PopOut module windows do not have unwanted card hotbars.
let hotbarTest = ui.hotbar;
console.debug("Card Hotbar | Core Foundry Hotbar Present?");
console.debug(hotbarTest);
if ( hotbarTest ) {
cardHotbarInit();
}
});
Hooks.on("renderSettingsConfig", async () => {
//add CSS ids and classes to cardHotbar settings section for styling
let settingsDiv = document.getElementById("client-settings");
let chbSetDiv = $( `#${settingsDiv.id} div h2.module-header:contains("card Hotbar")` ).next();
$(chbSetDiv).addClass('chb-setting');
$(chbSetDiv).addClass('chb-global');
$(chbSetDiv).attr('id', 'chbSetDiv');
let coreSetDiv = $(chbSetDiv).next();
$(coreSetDiv).addClass('chb-setting');
$(coreSetDiv).addClass('chb-global');
$(coreSetDiv).attr('id', 'coreSetDiv');
let chbFlagDiv = $(coreSetDiv).next();
$(chbFlagDiv).addClass('chb-setting');
$(chbFlagDiv).addClass('chb-user');
$(chbFlagDiv).attr('id', 'chbFlagDiv');
let coreFlagDiv = $(chbFlagDiv).next();
$(coreFlagDiv).addClass('chb-setting');
$(coreFlagDiv).addClass('chb-user');
$(coreFlagDiv).attr('id', 'coreFlagDiv');
});
Hooks.on("hotbarDrop", (hotbar, data, slot) => {
console.debug("Card Hotbar | Creating Macro")
if (data.type !== "JournalEntry") return true;
const journal = game.journal.get(data.id);
if (!journal) return true;
// Make a new macro for the Journal
Macro.create({
name: `Card: ${journal.name}`,
type: "script",
flags: {
"world": {
"cardID": `${journal.id}`,
}
},
scope: "global",
//Change first argument to "text" to show the journal entry as default.
//NOTE: In order for this macro to work (0.6.5 anyway) there MUST be text (content attribute must not be null).
command: `game.journal.get("${journal.id}").show("image", false);`,
img: `${game.journal.get(journal.id).data.img}`
}).then(macro => {
game.user.assignHotbarMacro(macro, slot);
});
return false;
});
Hooks.once('rendercardHotbar', () => {
console.debug("Card Hotbar | Performing initial collapse");
ui.cardHotbar.collapse();
});
Hooks.on("renderHotbar", async () => {
console.debug("Card Hotbar | The core hotbar just rendered!");
});
Hooks.on('rendercardHotbar', async () => {
console.debug("Card Hotbar | The card hotbar just rendered!");
});
// Add the listener to the board html element
//remember to use new 0.70 hook to cancel harmless error about no slot available
Hooks.once("canvasReady", (_) => {
document.getElementById("board").addEventListener("drop", async (event) => {
// Try to extract the data (type + src)
let data;
// try {
data = JSON.parse(event.dataTransfer.getData("text/plain"));
if(data.type == "Folder"){return;}
let m = game.macros.get(data.id);
let je = game.journal.get( m.getFlag("world", "cardID") );
console.debug("Card Hotbar | Canvas drop detected");
console.debug(event);
console.debug(data);
console.debug(m);
console.debug(je);
await createTileFromItem(je.id, event.clientX, event.clientY)
// } catch (err) {
// return;
// }
});
});
async function createTileFromItem(objId, x, y){
let imgPath = game.journal.get(objId).data.img
console.log(imgPath);
// Determine the Tile Size:
const tex = await loadTexture(imgPath);
const _width = tex.width;
const _height = tex.height;
// Project the tile Position
let t = canvas.tiles.worldTransform;
const _x = (x - t.tx) / canvas.stage.scale.x
const _y = (y - t.ty) / canvas.stage.scale.y
//cardScale is a value between 0 and 9, usually a decimal value between 0 and 1 representing a percentage.
//eventually will be replaced with a setting.
const cardScale = 0.25;
await Tile.create({
img: imgPath,
x: _x,
y: _y,
width: _width * cardScale,
height: _height * cardScale,
flags: {
"world": {
"cardID": `${objId}`,
}
}
})
}
/* NOTE: ERRORS/ISSUES WITH CORE HOTBAR (LOL, SHRUG)
0.6.4, DND 5E 0.93 (ALL MODS DISABLED)
1. file directory to canvas:
foundry.js:29725 Uncaught (in promise) Error: No available Hotbar slot exists
at User.assignHotbarMacro (foundry.js:29725)
at Canvas._onDrop (foundry.js:11425)
at DragDrop.callback (foundry.js:13785)
at DragDrop._handleDrop (foundry.js:13836)
2. Macro execute for spell, than cancel : uncaught in promise, 5e error?)
3. Drag macro onto itself, it is removed
4. Sometimes when you drag off of core, a ghost set of slots to left and right of core slot is grabbed also. Seems to happen if you click near a border between macro slots.
*/