/*:
 * @plugindesc CG鉴赏画面
 * @author Joritian
 *
 * @param 数目
 * @desc CG的数量
 * @default 3
 *
 * @param 初始大小
 * @desc 打开CG时的初始缩放比例.
 * @default 1
 *
 * @param 缩放步长
 * @desc 每按一次缩放键CG缩放比例所改变的值.
 * @default 0.1
 *
 * @param 放大极限
 * @desc 缩放CG时所能达到的最大缩放比例.
 * @default 1
 *
 * @param 移动速度
 * @desc 按下方向键时CG的移动速度(像素/帧).
 * @default 5
 *
 * @help 开启此画面请在事件内运以下插件命令:
 * 打开CG鉴赏画面
 *
 * 解锁第i幅图片请使用以下插件命令:
 * 解锁CG i
 *
 * 你必须在img文件夹内创建一个名为gallery的文件夹
 * 文件夹内的CG图片命名应为纯数字(图片ID),"locked"为未解锁时显示的图片,"background"为鉴赏画面背景图,"text"为对CG进行缩放时屏幕上显示的操作说明图片.
 * 图片的大小不得小于游戏画面.
 */

Game_System.prototype.initialize = function() {
	this._saveEnabled = true;
	this._menuEnabled = true;
	this._encounterEnabled = true;
	this._formationEnabled = true;
	this._battleCount = 0;
	this._winCount = 0;
	this._escapeCount = 0;
	this._saveCount = 0;
	this._versionId = 0;
	this._framesOnSave = 0;
	this._bgmOnSave = null;
	this._bgsOnSave = null;
	this._windowTone = null;
	this._battleBgm = null;
	this._victoryMe = null;
	this._defeatMe = null;
	this._savedBgm = null;
	this._walkingBgm = null;
	this.gallery = []
};
 
var parameters = PluginManager.parameters('PictureGallery');
var maxPictures = Number(parameters['数目'] || 3);
var pictureSpeed = Number(parameters['移动速度'] || 5);
var pictureScaleInit = Number(parameters['初始大小'] || 1);
var pictureScaleDelta = Number(parameters['缩放步长'] || 0.1);
var pictureScaleMax = Number(parameters['放大极限'] || 1);

var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
    Game_Interpreter.prototype.pluginCommand = function(command, args) {
        _Game_Interpreter_pluginCommand.call(this, command, args);
        if (command === '打开CG鉴赏画面') {
			SceneManager.push(Scene_Gallery);
        }
		if (command === '解锁CG') {
			$gameSystem.gallery[args[0]] = true;
		}
    };

ImageManager.loadGallery = function(filename) {
    return this.loadBitmap('img/pictures/', filename, 0, true);
};

function Scene_Gallery() {
    this.initialize.apply(this, arguments);
}

Scene_Gallery.prototype = Object.create(Scene_Base.prototype);
Scene_Gallery.prototype.constructor = Scene_Gallery;

Scene_Gallery.prototype.initialize = function() {
    Scene_Base.prototype.initialize.call(this);
	this.number = 0;
	for (var i = 1; i <= maxPictures; i++) {
		if ($gameSystem.gallery[i]) {
			this.number++;
		}
	}
	this.rate = this.number / maxPictures;
};

Scene_Gallery.prototype.create = function() {
    Scene_Base.prototype.create.call(this);
	this._zoomed = false;
	this.createBackground();
	this.createWindowLayer();
	this.createGalleryWindow();
};

Scene_Gallery.prototype.start = function() {
    Scene_Base.prototype.start.call(this);
	this._galleryWindow.activate();
};

Scene_Gallery.prototype.update = function() {
    Scene_Base.prototype.update.call(this);
	this.updateInputA();
};

Scene_Gallery.prototype.updateInputA = function() {
	if (Input.isTriggered("escape")) {
		SoundManager.playCancel();
		if (this._galleryWindow.active) {
			SceneManager.pop(this);
		} else {
			this._picture.visible = false;
			this._pictureText.visible = false;
			this._galleryWindow.visible = true;
			this._galleryWindow.activate();
		}
	}
	if (!this._galleryWindow.active) {
		if (Input.isPressed("up")) {
			this._picture.y += pictureSpeed;
		}
		if (Input.isPressed("down")) {
			this._picture.y -= pictureSpeed;
		}
		if (Input.isPressed("left")) {
			this._picture.x += pictureSpeed;
		}
		if (Input.isPressed("right")) {
			this._picture.x -= pictureSpeed;
		}
		if (Input.isTriggered("ok")) {
			SoundManager.playCancel();
			this._picture.visible = false;
			this._pictureText.visible = false;
			this._galleryWindow.visible = true;
			this._galleryWindow.activate();
		}
		if (Input.isTriggered("pageup")) {
			if (this._picture.scale.x < pictureScaleMax) {
				this._picture.scale.x += pictureScaleDelta;
				this._picture.scale.y += pictureScaleDelta;
			}
		}
		if (Input.isTriggered("pagedown")) {
			this._picture.scale.x -= pictureScaleDelta;
			this._picture.scale.y -= pictureScaleDelta;
			if (this._picture.width * this._picture.scale.x <= Graphics.boxWidth || this._picture.height * this._picture.scale.y <= Graphics.boxHeight) {
				this._picture.scale.x += pictureScaleDelta;
				this._picture.scale.y += pictureScaleDelta;
			}
		}
		if (this._picture.x > 0) this._picture.x = 0;
		if (this._picture.y > 0) this._picture.y = 0;
		if (this._picture.x + this._picture.width * this._picture.scale.x < Graphics.boxWidth) this._picture.x = Graphics.boxWidth - this._picture.width * this._picture.scale.x;
		if (this._picture.y + this._picture.height * this._picture.scale.y < Graphics.boxHeight) this._picture.y = Graphics.boxHeight - this._picture.height * this._picture.scale.y
	}
}

Scene_Gallery.prototype.createGalleryWindow = function() {
    this._galleryWindow = new Window_Gallery(50, 100, Graphics.boxWidth - 100, Graphics.boxHeight - 170);
	this._galleryWindow.setHandler('ok', this.onItemOk.bind(this));
	this.addWindow(this._galleryWindow);
	this._galleryWindow.refresh();
};

Scene_Gallery.prototype.createBackground = function() {
	this._picture = new Sprite();
	this._pictureText = new Sprite();
    this._backSprite = new Sprite();
	this._textSprite = new Sprite();
	this._picture.visible = false;
	this._pictureText.visible = false;
	this._picture.bitmap = new Bitmap(Graphics.boxWidth, Graphics.boxHeight);
	this._pictureText.bitmap = ImageManager.loadGallery('Text');
    this._backSprite.bitmap = ImageManager.loadGallery('Background');
	this._textSprite.bitmap = new Bitmap(Graphics.boxWidth - 100, 100);
	this._textSprite.move(0, Graphics.boxHeight - 100);
	this._textSprite.bitmap.textColor = "white";
	this._textSprite.bitmap.drawText("画像 : " + this.number + " / " + maxPictures, 100, 0, Graphics.boxWidth - 100, 100, "left");
	this._textSprite.bitmap.drawText("完成率: " + Math.floor(100 * this.rate) + " %" , 0, 0, Graphics.boxWidth - 100, 100, "right");
    this.addChild(this._backSprite);
	this.addChild(this._textSprite);
	this.addChild(this._picture);
	this.addChild(this._pictureText);
};

Scene_Gallery.prototype.onItemOk = function() {
    this._picture.bitmap = ImageManager.loadGallery(this._galleryWindow.index() + 1);
	this._galleryWindow.visible = false;
	this._picture.scale = new Point(pictureScaleInit, pictureScaleInit);
	this._picture.move(0, 0);
	this._picture.visible = true;
	this._pictureText.visible = true;
};

function Window_Gallery() {
    this.initialize.apply(this, arguments);
}

Window_Gallery.prototype = Object.create(Window_Selectable.prototype);
Window_Gallery.prototype.constructor = Window_Gallery;

Window_Gallery.prototype.initialize = function(x, y, width, height) {
    Window_Selectable.prototype.initialize.call(this, x, y, width, height);
	this.lastIndex = 0;
	this.opacity = 0;
    this._data = [];
};

Window_Gallery.prototype.maxCols = function() {
    return 3;
};

Window_Gallery.prototype.spacing = function() {
    return 48;
};

Window_Gallery.prototype.maxItems = function() {
    return maxPictures;
};

Window_Gallery.prototype.item = function() {
};

Window_Gallery.prototype.makeItemList = function() {
};

Window_Gallery.prototype.drawItem = function(i) {
	var rect = this.itemRect(i);
	rect.width -= this.textPadding();
	this.drawItemName(i + 1, rect.x, rect.y);
};

Window_Gallery.prototype.refresh = function() {
    this.makeItemList();
    this.createContents();
    this.drawAllItems();
};

Window_Gallery.prototype.itemHeight = function() {
    return 110;
};

Window_Gallery.prototype.drawItemName = function(i, x, y) {
    this.resetTextColor();
    this.drawIcon(i, x + 2, y + 2);
	this.drawText("N - " + i, x + (Graphics.boxWidth / 816) * 65, y + 80);
};

Window_Gallery.prototype.drawIcon = function(index, x, y) {
	if ($gameSystem.gallery[index]) {
		var bitmap = ImageManager.loadGallery(index);
	} else {
		var bitmap = ImageManager.loadGallery("locked");
	}
    this.contents.blt(bitmap, 0, 0, bitmap.width, bitmap.height, x + 10, y + 10, (Graphics.boxWidth / 816) * (192 - 20), 108 - 20);
};

Window_Gallery.prototype.processOk = function() {
    if ($gameSystem.gallery[this.index() + 1]) {
        this.playOkSound();
        this.updateInputData();
        this.deactivate();
        this.callOkHandler();
    } else {
        this.playBuzzerSound();
    }
};

var old_loadSystemImages = Scene_Boot.prototype.loadSystemImages;
Scene_Boot.prototype.loadSystemImages = function() {
	ImageManager.loadGallery("locked");
	for (var i = 0; i <= maxPictures; i++) {
		ImageManager.loadGallery(i);
	}
	old_loadSystemImages();
};
