/***   [scroll.c]
*
*	スクロールスピードインターフェイスウィンドウ		(C)ささがわ
*
*	For GNU C Compiler (GCC)   Version 1.39
*
***/

#include <stdio.h>
#include "graph.h"
#include "mos.h"
#include "window.h"
#include "icn.h"
#include "txwind.h"
#include "optparse.h"
#include "others.h"

#define WH_CAN		1
#define WH_TITLE	2
#define WH_ON		3
#define WH_OTHER	0

extern int	PAL_Back;
extern int	PAL_Black;
static int	wx, wy;
static int	SPEED;

static void	Draw_window(void);
static int	Where(int, int);
static void on(int);
static void off(int);

void SCR_init(void) {
	SPEED = OPT_Scroll();
	if (SPEED < 0 || 4 < SPEED)	SPEED = 2;
	TXW_rollset(SPEED);
}

int WIND_schroll(void) {
	int		ret = 0, spd;
	struct RECT	a, b;
	
	wx = 188;
	wy = 149;
	a.x1 = 319;	a.y1 = 259;
	a.x2 = 320;	a.y2 = 260;
	b.x1 = wx;	b.y1 = wy;
	b.x2 = wx + 263;	b.y2 = wy + 181;
	afterImage(&a, &b);
	
	Draw_window();
	spd = SPEED;
	MOS_disp(0);
	on(spd);
	MOS_disp(1);
	while (!ret) {
		char	mb;
		int		mx, my, wh;
		
		CLOCK(0);
		if (MOS_rdpos(&mb, &mx, &my), !(mb & 1))
			continue;
		
		if ((wh = Where(mx, my)) == WH_CAN) {
			if (Button(wx + 6, wy + 6, wx + 25, wy + 25))
				ret = -1;
		} else if (wh == WH_ON) {
			if (Button(wx + 167, wy + 148, wx + 246, wy + 169)) {
				SPEED = spd;
				ret = 1;
			}
		} else if (wh == WH_TITLE) {
			struct RECT	s, w;
			
			w.x1 = wx;	w.y1 = wy;
			w.x2 = wx + 263;	w.y2 = wy + 181;
			s.x1 = 0;	s.y1 = 40;	s.x2 = 639;	s.y2 = 463;
			if (dragWindow(mx, my, &w, &s, 0, 0)) {
				wx = w.x1;	wy = w.y1;
				MOS_disp(0);
				EGB_cls(0);
				MOS_disp(1);
				Draw_window();
				MOS_disp(0);
				on(spd);
				MOS_disp(1);
			}
		} else if (wh >= 10) {
			if (wh - 10 != spd) {
				MOS_disp(0);
				off(spd);
				on(spd = wh - 10);
				MOS_disp(1);
			}
		} else {
			while (MOS_rdpos(&mb, &mx, &my), mb & 1);
		}
	}
	
	if (ret == 1) {
		TXW_rollset(SPEED);
		return SPEED;
	} else
		return -1;
}

static void Draw_window(void) {
	int		i;
	struct opnwin_t	opw;
	
	opw.title = "スクロール スピード";
	opw.x1 = wx;
	opw.y1 = wy;
	opw.x2 = opw.x1 + 263;
	opw.y2 = opw.y1 + 181;
	opw.shdw = 1;
	opw.canb = 1;
	opw.nopt = 0;
	opw.wopt = NULL;
	opw.expb = 0;
	opw.ord = 0;
	MOS_disp(0);
	drawWindow(&opw);
	
	EGB_str2("○ 最  高  速", wx + 80, wy + 56, PAL_Black);
	EGB_str2("○     ↑    ", wx + 80, wy + 76, PAL_Black);
	EGB_str2("○  中    速 ", wx + 80, wy + 96, PAL_Black);
	EGB_str2("○     ↓    ", wx + 80, wy + 116, PAL_Black);
	EGB_str2("○ 最  低  速", wx + 80, wy + 136, PAL_Black);
	DrawButton(1, wx + 166, wy + 147, wx + 247, wy + 170);
	for (i = 0; i < 2; i++)
		EGB_str2(" 実    行 ", wx + 167 + i, wy + 166, PAL_Black);
	MOS_disp(1);
}

static int Where(int x, int y) {
	int		i, ret;
	
	x -= wx;
	y -= wy;
	if (5 < x && x < 26 && 5 < y && y < 26)
		ret = WH_CAN;
	else if (26 < x && x < 258 && 5 < y && y < 26)
		ret = WH_TITLE;
	else if (166 < x && x < 247 && 147 < y && y < 170)
		ret = WH_ON;
	else if (80 <= x && x <= 183 && (i = y - 41) >= 0 && i / 20 < 5 && i % 20 < 16)
		ret = i / 20 + 10;
	else
		ret = WH_OTHER;
	
	return ret;
}

static void on(int spd) {
	EGB_str3("●", wx + 80, wy + 56 + spd * 20, PAL_Black, PAL_Back);
}

static void off(int spd) {
	EGB_str3("○", wx + 80, wy + 56 + spd * 20, PAL_Black, PAL_Back);
}
