@senspark/ee
Version:
utility library for cocos creator
191 lines (190 loc) • 6.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var assert = require("assert");
var Dialog_1 = require("./Dialog");
var CommandType;
(function (CommandType) {
/** Pushes a dialog. */
CommandType[CommandType["Push"] = 0] = "Push";
/** Pops a dialog. */
CommandType[CommandType["Pop"] = 1] = "Pop";
})(CommandType || (CommandType = {}));
var Command = /** @class */ (function () {
function Command(type, dialog) {
this.type = type;
this.dialog = dialog;
}
Command.prototype.getType = function () {
return this.type;
};
Command.prototype.getDialog = function () {
return this.dialog;
};
return Command;
}());
var DefaultDialogManager = /** @class */ (function () {
function DefaultDialogManager(root) {
this.root = root;
this.lockingDialog = null;
this.dialogStack = [];
this.commandQueue = [];
}
DefaultDialogManager.prototype.pushDialog = function (dialog) {
assert(dialog !== null);
assert(dialog.getContainer() !== null);
this.pushCommand(new Command(CommandType.Push, dialog));
this.processCommandQueue();
};
DefaultDialogManager.prototype.popDialog = function (dialog) {
assert(dialog !== null);
assert(dialog.getContainer() !== null);
this.pushCommand(new Command(CommandType.Pop, dialog));
this.processCommandQueue();
};
/** Checks whether there is a locking dialog. */
DefaultDialogManager.prototype.isLocked = function () {
return this.lockingDialog !== null;
};
/** Locks all dialog behaviors with the specified dialog. */
DefaultDialogManager.prototype.lock = function (dialog) {
assert(this.lockingDialog === null);
this.lockingDialog = dialog;
};
/** Unlocks all dialog behaviors. */
DefaultDialogManager.prototype.unlock = function (dialog) {
assert(this.lockingDialog === dialog);
this.lockingDialog = null;
};
/** Gets the current (top) dialog. */
DefaultDialogManager.prototype.getCurrentDialog = function () {
var length = this.dialogStack.length;
if (length === 0) {
return null;
}
var dialog = this.dialogStack[length - 1];
assert(dialog !== null);
return dialog;
};
/** Gets the current (top) root node. */
DefaultDialogManager.prototype.getCurrentRoot = function () {
var dialog = this.getCurrentDialog();
if (dialog === null) {
return this.root;
}
return dialog.getContainer();
};
/** Attempts to process the current command queue. */
DefaultDialogManager.prototype.processCommandQueue = function () {
if (this.isLocked()) {
return false;
}
var length = this.commandQueue.length;
for (var i = 0; i < length; ++i) {
var command = this.commandQueue[i];
if (this.processCommand(command)) {
this.commandQueue.splice(i, 1);
return true;
}
}
return false;
};
DefaultDialogManager.prototype.processCommand = function (command) {
if (command.getType() === CommandType.Push) {
return this.processPushCommand(command.getDialog());
}
if (command.getType() === CommandType.Pop) {
return this.processPopCommand(command.getDialog());
}
assert(false);
return false;
};
DefaultDialogManager.prototype.processPushCommand = function (dialog) {
this.pushDialogImmediately(dialog);
return true;
};
DefaultDialogManager.prototype.processPopCommand = function (dialog) {
var topDialog = this.getCurrentDialog();
if (topDialog !== dialog) {
return false;
}
this.popDialogImmediately(dialog);
return true;
};
DefaultDialogManager.prototype.pushCommand = function (command) {
var length = this.commandQueue.length;
if (length > 0) {
var lastCommand = this.commandQueue[length - 1];
if (lastCommand.getType() === command.getType() &&
lastCommand.getDialog() === command.getDialog()) {
// Duplicated command.
return false;
}
}
this.commandQueue.push(command);
return true;
};
/**
* Immediately pushes the specified dialog:
* - Lock dialog.
* - Will-hide events.
* - Add dialog's container.
* - Push dialog.
* - Did-hide events.
* - Set active = true.
* - Unlock dialog.
* - Process next commands.
*/
DefaultDialogManager.prototype.pushDialogImmediately = function (dialog) {
var _this = this;
assert(!dialog.isActive());
// Lock first.
this.lock(dialog);
// Must be called before push + addChild.
dialog.processEvent(Dialog_1.DialogEventType.WillShow);
// Add child + push.
var root = this.getCurrentRoot();
root.addChild(dialog.getContainer());
this.dialogStack.push(dialog);
dialog.playShowingTransition(function () {
// Process event + set active.
dialog.processEvent(Dialog_1.DialogEventType.DidShow);
dialog.setActive(true);
// Unlock and process next commands.
_this.unlock(dialog);
_this.processCommandQueue();
});
};
/**
* Immediately pops the specified dialog:
* - Lock dialog.
* - Set active = false.
* - Will-hide events.
* - Pop dialog.
* - Remove dialog's container.
* - Did-hide events.
* - Unlock dialog.
* - Process next commands.
*/
DefaultDialogManager.prototype.popDialogImmediately = function (dialog) {
var _this = this;
assert(dialog.isActive());
// Lock first.
this.lock(dialog);
// Set active + process event.
dialog.setActive(false);
dialog.processEvent(Dialog_1.DialogEventType.WillHide);
dialog.playHidingTransition(function () {
// Pop + remove child.
_this.dialogStack.pop();
var container = dialog.getContainer();
container.removeFromParent(false);
// Must be called after pop + remove child.
dialog.processEvent(Dialog_1.DialogEventType.DidHide);
// Unlock and process next commands.
_this.unlock(dialog);
_this.processCommandQueue();
});
};
return DefaultDialogManager;
}());
exports.DefaultDialogManager = DefaultDialogManager;