@if exp="typeof(global.rpmode_object) == 'undefined'"
@iscript


class CompleteLayer extends Layer {
	var Layers = [];
	var timer = void;
	var count;
	var img = [];
	var p = 'image/mode/%/';

	function CompleteLayer(win, par){
		super.Layer(...);
		loadImages(p+'%');
		setSizeToImageSize();
		visible = true;

		for(var i=0; i<3; i++){
			Layers[i] = new global.Layer(window, this);
			with(Layers[i]){
				.setPos(15+43*i, 0, 200, 70);
				.visible = true;
			}
		}

		timer = new Timer(onTimer, "");
		timer.interval = 20;
		timer.enabled = false;
	}

	function finalize(){
		for(var i=0; i<Layers.count; i++) invalidate Layers[i];
		invalidate timer;
		super.finalize();
	}

	function onTimer(){
		for(var i=0; i<3; i++) Layers[i].loadImages(p+intrandom(0, 9));
		count++;
		if(count > 50){
			for(var i=0; i<3; i++) Layers[i].loadImages(p+img[i]);
			timer.enabled = false;
		}
	}

	function loadLayers(nume, deno){
		var rate = nume * 100 \ deno;
		img[0] = rate \ 100 % 10;
		img[1] = rate \ 10 % 10;
		img[2] = rate % 10;
		count = 0;
		timer.enabled = true;
	}
}

class RPModeLayer extends Layer {
	var Data = [];
	var Buttons = [];
	var Complayer = void;
	var Pagelayer = void;
	var MaxPagelayer = void;
	var Plugin = void;
	var Page;
	var PageMax;
	var Slot = 6;
	var p = 'image/mode/';

	function RPModeLayer(win, par, Plugin){
		super.Layer(win, par);
		this.Plugin = Plugin;
		PageMax = (RPList.count-2) \ Slot;
		Page = 0;

		loadImages(p+'rpmode');
		setSizeToImageSize();
		absolute = 1000001;
		hitThreshold = 0;
		createButtons();

		Complayer = new CompleteLayer(window, this);
		Complayer.setPos(800, 25);
		Complayer.absolute = absolute;

		MaxPagelayer = new global.Layer(window, this);
		with(MaxPagelayer){
			.loadImages(p+(PageMax));
			.setPos(615, 653);
			.absolute = absolute;
			.visible = true;
		}

		Pagelayer = new global.Layer(window, this);
		with(Pagelayer){
			.loadImages(p+(Page));
			.setPos(545, 653);
			.absolute = absolute;
			.visible = true;
		}
	}

	function finalize(){
		for(var i=0; i<Data.count; i++) invalidate Data[i];
		for(var i=0; i<Buttons.count; i++) invalidate Buttons[i];
		invalidate Complayer;
		invalidate MaxPagelayer;
		invalidate Pagelayer;
		invalidate Plugin;
		super.finalize();
	}

	function createButtons(){
		for(var i=0; i<Slot; i++){
		Data[i] = new CustomButtonLayer(window, this, onView);
		with(Data[i]){
			.setPos(175+285*(i%3), 145+250*(i\3));
			.hitThreshold = 1;
			.hint = "";
			.name = i;
		}
		}

		Buttons[0] = new CustomButtonLayer(window, this, function(){with(rpmode_object.rpmode){.onPageMove(-1,.PageMax);}});
		with(Buttons[0]){
			.loadImages(p+'move0_0', p+'move0_1', p+'move0_2');
			.setPos(500, 653);
			.hitThreshold = 1;
			.hint = "";
		}

		Buttons[1] = new CustomButtonLayer(window, this, function(){rpmode_object.rpmode.onPageMove(1,0);});
		with(Buttons[1]){
			.loadImages(p+'move1_0', p+'move1_1', p+'move1_2');
			.setPos(660, 653);
			.hitThreshold = 1;
			.hint = "";
		}

		Buttons[2] = new CustomButtonLayer(window, this, Plugin.onClose);
		with(Buttons[2]){
			.loadImages('back0', 'back1');
			.setPos(0, 700);
			.hitThreshold = 0;
			.hint = "";
		}

	}

	function Change(){
		for(var i=0; i<Slot; i++){
			with(Data[i]){
				.enabled = false;
				.visible = true;
				.loadImages(p+'rpno');
				var n = Slot*Page + i;
				if(RPExist[n]){
					.enabled = true;
					.loadImages(p+'rp/'+RPList[n][1]);
				}
				else if(RPExist[n] === void){
					.visible = false;
				}
			}
		}
		Pagelayer.loadImages(p+Page);
	}

	function onView(obj){
		var i = +obj.name+Slot*Page;
		tf.storage = 'scenario/scenario/'+RPList[i][0]+'.ks';
		tf.label = '*'+RPList[i][1];
		tf.page = Page;
		Plugin.onClose();
	}

	function Check(){
		RPCount = 0;
		for(var i=0; i<RPList.count-1; i++){
			RPExist[i] = kag.sflags["trail_"+RPList[i][0]+"_"+RPList[i][1]];
			if(RPExist[i]) RPCount++;
			else RPExist[i] = 0;
		}
	}

	function Complete(){
		var deno = RPExist.count;
		var nume = RPCount;
		Complayer.loadLayers(nume, deno);
	}

	function onPageMove(i,n){
		Page += i;
		if(Page<0 || PageMax<Page) Page = n;
		Pagelayer.loadImages(p+Page);
		Change();
	}

	function onKeyUp(key, shift){
		if(key == VK_ESCAPE) Plugin.onClose();
	}

	function onMouseEnter(){
		if(!parent.focusable){
			focusable = true;
			focus();
		}
	}

	function onMouseUp(x, y, button, shift){
		if(button == mbRight) Plugin.onClose();
	}

	function onMouseWheel(shift, delta, x, y){
		if(delta > 0) onPageMove(-1,PageMax);
		else if(delta < 0) onPageMove(1,0);
	}
}

class RPModePlugin extends KAGPlugin {
	var rpmode = void;

	function RPModePlugin(){
		super.KAGPlugin();
		rpmode = new RPModeLayer(kag, kag.fore.base, this);
	}

	function finalize(){
		invalidate rpmode;
		super.finalize();
	}

	function Display(){
		with(rpmode){
			.parent = kag.back.base;
			.Check();
			.Complete();
			if(tf.page !== void) .Page = tf.page;
			.Change();
			.visible = true;
		}
	}

	function onClose(){
		with(rpmode){
			.Complayer.timer.enabled = false;
			.enabled = false;
			.hitThreshold = 256;
		}
		kag.trigger('rpmode');
	}
}

@endscript
@endif


@macro name=rpreturn
@eval exp="if(tf.label!='')kag.processCall('rpmode.ks','*rpreturn')"
@endmacro
@return


*rpreturn

@sysbtn visible=false
;;日付を初期化
@eval exp="f.week = void;"
@iscript
	for(var i=0; i<2; i++){
		system_object.enabled1(i, true);
		system_object.enabled2(i, true);
	}
	system_object.selectErase();				//選択肢をリセット（次の呼び出しまで非表示＋選択不可状態）
	kag.NameTagClear();
	kag.historyLayer.clear();
	tf.storage = '', tf.label = '';
	kag.skipMode = 0, kag.autoMode = false;
	kag.allClear("title");						//戻りBGM
@endscript
@ef t=500


*rpmode

@iscript
	kag.updateBeforeCh = 1;
	kag.backupLayer(%[], true);
	title_object.title.enabled=false;
	kag.addPlugin(global.rpmode_object = new RPModePlugin());
	rpmode_object.Display();
@endscript
@ef t=250
@iscript
	title_object.title.visible=false;
@endscript

@waittrig name=rpmode

@if exp="tf.label!=''"
@ef t=500
@sysbtn visible=true
@iscript
	for(var i=0; i<2; i++){
		system_object.enabled1(i, false);
		system_object.enabled2(i, false);
	}
	system_object.selectErase();			//選択肢をリセット（次の呼び出しまで非表示＋選択不可状態）
	//断面図 文字カットインボタンの初期化
	system_object.sectionButtonErase();
	system_object.cutinStrButtonErase();

	free(rpmode_object);
@endscript
@return storage=&tf.storage target=&tf.label+'x'
@endif

@call storage=title.ks target=*title
@iscript
	tf.page = void;
	free(rpmode_object);
@endscript
@return storage=first.ks target=*Title

