//=============================================================================
// Final Fantasy XIII styled Title Screen
//=============================================================================

/*:
* @plugindesc Stylized title screen akin to FF13's, more importantly the animation from the start.
* @author Orochii Zouveleki
*
* @param Title Name
* @desc Title graphic filename, unless you're using the pregenerated letters.
* @default title_name
* @param Title X
* @desc Put auto, center or a numeric for an absolute value.
* @default center
* @param Title Y
* @desc Put auto, center or a numeric for an absolute value.
* @default auto
*
* @param Centered Image
* @desc Centered Appearing image.
* @default title_name
* @param Centered X
* @desc Put auto, center or a numeric for an absolute value.
* @default center
* @param Centered Y
* @desc Put auto, center or a numeric for an absolute value.
* @default auto
*
* @param Splash Image
* @desc Splash that goes behind the text.
* @default title_name
* @param Splash X
* @desc Put auto, center or a numeric for an absolute value.
* @default center
* @param Splash Y
* @desc Put auto, center or a numeric for an absolute value.
* @default auto
*
* @help
*
* Plugin Command:
* EnemyBook open # Open the enemy book screen
* EnemyBook add 3 # Add enemy #3 to the enemy book
* EnemyBook remove 4 # Remove enemy #4 from the enemy book
* EnemyBook complete # Complete the enemy book
* EnemyBook clear # Clear the enemy book
*
* Enemy Note:
* <desc1:foobar> # Description text in the enemy book, line 1
* <desc2:blahblah> # Description text in the enemy book, line 2
* <book:no> # This enemy does not appear in the enemy book
*/

(function() {

var parameters = PluginManager.parameters('FFXIII_TitleScreen');
var titleName = String(parameters || 'title_name');
var titleXPos = String(parameters || 'center');
var titleYPos = String(parameters || 'auto');

var titleCenter = String(parameters || 'title_bar');
var centerXPos = String(parameters || 'center');
var centerYPos = String(parameters || 'auto');

var titleSplash = String(parameters || 'title_splash');
var splashXPos = String(parameters || 'center');
var splashYPos = String(parameters || 'auto');

var state = 0;
var visibleBarWidth = 0;
var visibleTitleWidth = 0;
var progress = 0;
//-----------------------------------------------------------------------------
// Scene_Title
//
// The scene class of the title screen.

Scene_Title.prototype.create = function() {
Scene_Base.prototype.create.call(this);
this.createBackground();
this.createForeground();
this.createWindowLayer();
this.createCommandWindow();
};

Scene_Title.prototype.start = function() {
Scene_Base.prototype.start.call(this);
SceneManager.clearStack();
this.centerSprite(this._backSprite1);
this.centerSprite(this._backSprite2);
this.playTitleMusic();
this.startFadeIn(this.fadeSpeed(), false);
};

Scene_Title.prototype.update = function() {
// Skip whatever if button is pressed.
if ((Input.isLongPressed('ok') || TouchInput.isLongPressed())) {
visibleTitleWidth = this._gameTitleSprite.bitmap.width;
progress = 255;
}

// Animation states.
switch(state) {
case 0:
// Appearing bar
this.positionTitleSprite(this._appearingBarSprite, centerXPos, centerYPos);
visibleBarWidth += 2;
this.setSpriteWidthFrame(this._appearingBarSprite, visibleBarWidth);
if (visibleBarWidth >= this._appearingBarSprite.bitmap.width / 2) {
state = 1;
}
break;

case 1:
// Appearing bar end
if (visibleBarWidth < this._appearingBarSprite.bitmap.width) {
//this.positionTitleSprite(this._appearingBarSprite, centerXPos, centerYPos);
visibleBarWidth += 2;
this.setSpriteWidthFrame(this._appearingBarSprite, visibleBarWidth);
}
// Update title animation
this.positionTitleSprite(this._gameTitleSprite, titleXPos, titleYPos);
visibleTitleWidth += 2;
this.setSpriteWidthFrame(this._gameTitleSprite, visibleTitleWidth);
if (visibleTitleWidth >= this._gameTitleSprite.bitmap.width) {
state = 2;
}
break;

case 2:
// Appear background
progress += 1;
this._backSprite1.opacity = progress;
this._backSprite2.opacity = progress;
if (progress >= 128) {
state = 3;
}
break;

case 3:
// Appear background and splash
progress += 1;
this._backSprite1.opacity = progress;
this._backSprite2.opacity = progress;
this._splashSprite.opacity = progress/2;
if (progress >= 255) {
state = 99;
}
break;

default:
if (!this.isBusy()) {
this._commandWindow.open();
}
}

Scene_Base.prototype.update.call(this);
};

Scene_Title.prototype.createBackground = function() {
this._backSprite1 = new Sprite(ImageManager.loadTitle1($dataSystem.title1Name));
this._backSprite2 = new Sprite(ImageManager.loadTitle2($dataSystem.title2Name));
this.addChild(this._backSprite1);
this.addChild(this._backSprite2);
this._backSprite1.opacity = 0;
this._backSprite2.opacity = 0;
};

Scene_Title.prototype.createForeground = function() {
// Create splash image
this.loadSplashSprite();
this._splashSprite.opacity = 0;

// Create title text
if ($dataSystem.optDrawTitle) {
this.drawGameTitle();
} else {
this.loadGameTitle();
}
this.positionTitleSprite(this._gameTitleSprite, titleXPos, titleYPos);
this.setSpriteWidthFrame(this._gameTitleSprite, visibleTitleWidth);

// Create apearing bar
this.loadAppearingBar();
this.positionTitleSprite(this._appearingBarSprite, centerXPos, centerYPos);
this.setSpriteWidthFrame(this._appearingBarSprite, visibleBarWidth);
};

Scene_Title.prototype.loadAppearingBar = function() {
this._appearingBarSprite = new Sprite(ImageManager.loadSystem(titleCenter));
this.addChild(this._appearingBarSprite);
};
Scene_Title.prototype.loadSplashSprite = function() {
this._splashSprite = new Sprite(ImageManager.loadSystem(titleSplash));
this.addChild(this._splashSprite);
};
Scene_Title.prototype.loadGameTitle = function() {
this._gameTitleSprite = new Sprite(ImageManager.loadSystem(titleName));
this.addChild(this._gameTitleSprite);
};
Scene_Title.prototype.setSpriteWidthFrame = function(sprite, width) {
sprite.setFrame(0, 0, width, sprite.bitmap.height);
};

Scene_Title.prototype.drawGameTitle = function() {
var text = $dataSystem.gameTitle;
var t = new Bitmap(1, 1);
t.outlineColor = 'black';
t.outlineWidth = 8;
t.fontSize = 72;
var text_width = t.measureTextWidth(text) + 24;
console.log(text_width);
this._gameTitleSprite = new Sprite(new Bitmap(text_width, 96));
this.addChild(this._gameTitleSprite);

this._gameTitleSprite.bitmap.outlineColor = 'black';
this._gameTitleSprite.bitmap.outlineWidth = 8;
this._gameTitleSprite.bitmap.fontSize = 72;
this._gameTitleSprite.bitmap.drawText(text, 0, 24, text_width, 48, 'center');
};

Scene_Title.prototype.positionTitleSprite = function(sprite, xpos, ypos) {
switch(xpos) {
case "center":
sprite.x = (Graphics.width - sprite.bitmap.width) / 2;
break;
case "auto":
sprite.x = Graphics.width / 3 - sprite.bitmap.width / 2;
break;
default:
sprite.x = xpos;
}
switch(ypos) {
case "center":
sprite.y = (Graphics.height - sprite.bitmap.height) / 2;
break;
case "auto":
sprite.y = Graphics.height / 3 - sprite.bitmap.height / 2;
break;
default:
sprite.y = ypos;
}
}

Scene_Title.prototype.centerSprite = function(sprite) {
sprite.x = Graphics.width / 2;
sprite.y = Graphics.height / 2;
sprite.anchor.x = 0.5;
sprite.anchor.y = 0.5;
};

Scene_Title.prototype.createCommandWindow = function() {
this._commandWindow = new Window_TitleCommand();
this._commandWindow.setHandler('newGame', this.commandNewGame.bind(this));
this._commandWindow.setHandler('continue', this.commandContinue.bind(this));
this._commandWindow.setHandler('options', this.commandOptions.bind(this));
this.addWindow(this._commandWindow);
};

})();