avg-core-ts
Version:
Develop Adventure Game (or ADV, Visual Novel, Galgame, etc.) on your own!
94 lines (73 loc) • 2.33 kB
JavaScript
/**
* @file Plugin to enable transition to node.
* @author MicroMatrix <yangpan.1985@bytedance.com>
* @copyright 2012-2020 bytedance
* @link git@code.byted.org:yangpan.1985/avg.git
*/
import core from 'core/core';
// const logger = core.getLogger('Transition Plugin');
class TransitionPlugin {
constructor(node, method) {
this.node = node;
this.method = method;
this.clickCallback = false;
this.unskippable = false;
core.use('script-trigger', async (ctx, next) => {
if (this.clickCallback && !this.unskippable) {
this.node.completeImmediate();
this.clickCallback = false;
this.unskippable = false;
} else {
await next();
}
});
}
static wrap(node, method) {
const wrapped = new TransitionPlugin(node, method);
return wrapped.process.bind(wrapped);
}
async process(ctx, next) {
const layer = this.node;
const method = this.method;
const { flags, params } = ctx;
const isSkip = flags.includes('_skip_');
if (flags.includes('pretrans')) {
// const renderer = core.getRenderer();
if (layer.transitionStatus !== 'prepare') {
// layer.transitionStatus = 'prepare';
layer.prepare();
}
return method(ctx, next);
} else if (flags.includes('trans') || params.trans) {
params.method = params.method || 'crossfade';
// const renderer = core.getRenderer();
if (layer.transitionStatus !== 'prepare') {
// layer.transitionStatus = 'prepare';
layer.prepare();
}
await method(ctx, next);
// layer.transitionStatus = 'start';
const promise = layer.start(params.method, params)
.then(() => (layer.transitionStatus = null));
this.clickCallback = true;
if (flags.includes('unskippable')) {
this.unskippable = true;
}
// FIXME
if (layer.visible && !flags.includes('nowait') && !isSkip) {
await promise;
}
if (isSkip) {
layer.completeImmediate();
}
this.clickCallback = false;
} else {
return method(ctx, next);
}
return Promise.resolve();
}
}
export default function transition(node, execute) {
const wrapped = new TransitionPlugin(node, execute);
return wrapped.process.bind(wrapped);
}