#include "WinTmpl.h"
#include "WinTmpl.cpp"
/* Ex2.cpp: Mouse, Key Timerのテスト */

// コンパイルするときは、user32と gdi32をリンクしておく 
// BCC32の場合:      bcc32 -W Ex2.cpp
//	または:	     bcc32 Ex2.cpp -luser32 -lgdi32 		
// Cygnus GCCの場合: g++ Ex2.cpp -mwindows -mno-cygwin
//	     または: g++ Ex2.cpp -luser32 -lgdi32

/* 大域変数の宣言はこの辺り */

#include <math.h>

int xPos = 320, yPos=240;
double t=0;

TForm::TForm() {
    /* Formのサイズ・色はここで変更する */
   Width =  640;
   Height = 600;

   Color=clWhite;
}

void TForm::FormCreate(TObject* sender) {
    /* Timerは、ここでセットしておく */
    Timer->SetInterval(200);
    Timer->SetEnabled(true);
}

void TForm::FormMouseDown(TObject* sender, TMouseButton which,
			  TShiftState shift, int x, int y) {
   xPos=x; yPos=y;
   Repaint();
}

void TForm::FormKeyDown(TObject* sender, WORD key, TShiftState shift) {
    switch (key) {
    case 'h': case 'H':
	xPos-=5;
	break;
    case 'j': case 'J':
	yPos+=5;
	break;
    case 'k': case 'K':
	yPos-=5;
	break;
    case 'l': case 'L':
	xPos+=5;
	break;
    case 's': case 'S':
	Timer->SetEnabled(false);
	break;
    case 'r': case 'R':
	Timer->SetEnabled(true);
	break;
    case ' ':
	Timer->SetEnabled(!Timer->GetEnabled());
	break;
    }
   Repaint();
}

void TForm::TimerTimer(TObject* sender) {
    xPos = 320+(int)(200*cos(t));
    yPos = 240-(int)(200*sin(t));
    Canvas->Font->Color=RGB(((int)(t*60))%256, 0, 255-((int)(t*60))%256);
    t += 0.05;
    Repaint();
}

void TForm::FormPaint(TObject* sender) {
  int i;
  Canvas->Brush->Style=bsClear;
  Canvas->TextOut(xPos, yPos, "Hello! "+AnsiString(t));
}

