|
| 1 | +var API_Meta = API_Meta||{}; |
| 2 | +API_Meta.Fade={offset:Number.MAX_SAFE_INTEGER,lineCount:-1}; |
| 3 | +{try{throw new Error('');}catch(e){API_Meta.Fade.offset=(parseInt(e.stack.split(/\n/)[1].replace(/^.*:(\d+):.*$/,'$1'),10)-3);}} |
| 4 | + |
| 5 | +// Fade — Smooth opacity fading for Roll20 graphics |
| 6 | +// !fade --in[|time] [--all] |
| 7 | +// !fade --out[|time] [--all] |
| 8 | + |
| 9 | +on('ready', () => { |
| 10 | + |
| 11 | + const version = '1.0.0'; //version number set here |
| 12 | + log('-=> Fade v' + version + ' is loaded. Command !fade --|<in/out>|<number of seconds> <--all>.'); |
| 13 | + |
| 14 | + |
| 15 | +on('chat:message', (msg) => { |
| 16 | + if (msg.type !== 'api' || !msg.content.startsWith('!fade')) return; |
| 17 | + |
| 18 | + const player = getObj('player', msg.playerid); |
| 19 | + const args = msg.content.split(/\s+--/).slice(1).map(a => a.trim()); //better |
| 20 | + |
| 21 | + |
| 22 | + const FADE_STEPS = 20; |
| 23 | + let activeIntervals = []; |
| 24 | + |
| 25 | + // Parse arguments |
| 26 | + let fadeIn = false; |
| 27 | + let fadeOut = false; |
| 28 | + let fadeTime = 1; |
| 29 | + let affectAll = false; |
| 30 | + |
| 31 | + args.forEach(arg => { |
| 32 | + const [keyRaw, valueRaw] = arg.split('|'); |
| 33 | + const key = (keyRaw || '').trim().toLowerCase(); |
| 34 | + const value = valueRaw ? valueRaw.trim() : undefined; |
| 35 | + |
| 36 | + if (key === 'in') { |
| 37 | + fadeIn = true; |
| 38 | + fadeTime = value ? parseFloat(value) : 1; |
| 39 | + } else if (key === 'out') { |
| 40 | + fadeOut = true; |
| 41 | + fadeTime = value ? parseFloat(value) : 1; |
| 42 | + } else if (key === 'all') { |
| 43 | + affectAll = true; |
| 44 | + } |
| 45 | + }); |
| 46 | + // Defensive checks |
| 47 | + if (!fadeIn && !fadeOut) { |
| 48 | + sendChat('Fade', `/w "${player.get('displayname')}" You must specify --in or --out.`); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const targetOpacity = fadeIn ? 1.0 : 0.0; |
| 53 | +let pageId = |
| 54 | + (player && player.get('lastpage')) || |
| 55 | + Campaign().get('playerpageid'); |
| 56 | + |
| 57 | + if (!affectAll && (!msg.selected || msg.selected.length === 0)) { |
| 58 | + sendChat('Fade', `/w "${player.get('displayname')}" No graphics selected. Use --all to affect the entire page.`); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | +if (affectAll && !pageId) { |
| 63 | + sendChat('Fade', `/w "${player.get('displayname')}" Could not determine current page.`); |
| 64 | + return; |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | + // Collect target graphics |
| 69 | + let targets = affectAll |
| 70 | + ? findObjs({ _pageid: pageId, _type: 'graphic' }) || [] |
| 71 | + : msg.selected |
| 72 | + .map(sel => getObj(sel._type, sel._id)) |
| 73 | + .filter(obj => obj && obj.get('type') === 'graphic'); |
| 74 | + |
| 75 | + if (targets.length === 0) { |
| 76 | + sendChat('Fade', `/w "${player.get('displayname')}" No valid graphics found to fade.`); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + const stepInterval = (fadeTime * 1000) / FADE_STEPS; |
| 81 | + |
| 82 | + // Stop any active fades |
| 83 | + activeIntervals.forEach(interval => clearInterval(interval)); |
| 84 | + activeIntervals = []; |
| 85 | + |
| 86 | + // Precompute fixed per-graphic fade steps |
| 87 | + const fadeData = targets.map(g => { |
| 88 | + const start = parseFloat(g.get('baseOpacity')) || 0; |
| 89 | + const diff = targetOpacity - start; |
| 90 | + return { |
| 91 | + g, |
| 92 | + start, |
| 93 | + step: diff / FADE_STEPS, |
| 94 | + currentStep: 0 |
| 95 | + }; |
| 96 | + }).filter(fd => Math.abs(fd.step) > 0.0001); // skip already at target |
| 97 | + |
| 98 | + if (fadeData.length === 0) return; |
| 99 | + |
| 100 | + const intervalId = setInterval(() => { |
| 101 | + let done = true; |
| 102 | + |
| 103 | + fadeData.forEach(fd => { |
| 104 | + if (fd.currentStep < FADE_STEPS) { |
| 105 | + const newVal = fd.start + fd.step * (fd.currentStep + 1); |
| 106 | + fd.g.set('baseOpacity', Math.max(0, Math.min(1, newVal))); |
| 107 | + fd.currentStep++; |
| 108 | + done = false; |
| 109 | + } else { |
| 110 | + fd.g.set('baseOpacity', targetOpacity); |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + if (done) { |
| 115 | + clearInterval(intervalId); |
| 116 | + activeIntervals = activeIntervals.filter(id => id !== intervalId); |
| 117 | + } |
| 118 | + }, stepInterval); |
| 119 | + |
| 120 | + activeIntervals.push(intervalId); |
| 121 | +}); |
| 122 | +}); |
| 123 | +{ try { throw new Error(''); } catch (e) { API_Meta.Fade.lineCount = (parseInt(e.stack.split(/\n/)[1].replace(/^.*:(\d+):.*$/, '$1'), 10) - API_Meta.Fade.offset); } } |
0 commit comments