?? logocmdu.cpp
字號:
//---------------------------------------------------------------------------
#pragma hdrstop
#include <Math.h>
#include <Classes.hpp>
#include "LogoCmdU.h"
//#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
const static double A0 = M_PI/180.0;
//CmdList LogoCmd::List[1000]={0};
const static struct CmdTable{
char *cmd;
bool (__fastcall *fun)(LogoCmd*,int);}Cmd[]={
{"up",LogoCmd::Up},
{"down",LogoCmd::Down},
{"clear",LogoCmd::Clear},
{"color",LogoCmd::PenColor},
{"left",LogoCmd::Left},
{"right",LogoCmd::Right},
{"forward",LogoCmd::Forward},
{"back",LogoCmd::Back},
{"redo",LogoCmd::Redo},
{"show",LogoCmd::ShowIt},
{"hide",LogoCmd::HideIt},
{"顯示",LogoCmd::ShowIt},
{"隱藏",LogoCmd::HideIt},
{"提筆",LogoCmd::Up},
{"落筆",LogoCmd::Down},
{"清除",LogoCmd::Clear},
{"色彩",LogoCmd::PenColor},
{"左轉",LogoCmd::Left},
{"右轉",LogoCmd::Right},
{"前進",LogoCmd::Forward},
{"后退",LogoCmd::Back},
{"重復",LogoCmd::Redo}
};
typedef bool (__fastcall*Command)(LogoCmd*,int);
Command CmdGet(AnsiString src)
{
int n=sizeof(Cmd)/sizeof(CmdTable);
for(int i=0;i<n;++i)
if(src == Cmd[i].cmd)
return Cmd[i].fun;
return 0;
}
bool __fastcall LogoCmd::Redo(LogoCmd *logo,int count)
{
bool Ret=false;
if(logo && logo->List && count){
int pos = logo->Pos;
CmdList *cp = logo->List+pos,*p;
LogoCmd *lg = logo;
Command fp;
for(int i=0;i<count;++i){
p = cp+1;
logo->Pos = pos+1;
for(int j=0,n=cp->Len-1;j<n;p+=p->Len){
fp = p->Fun;
if(fp==LogoCmd::Redo)
logo->Pos = pos+j+1;
Ret = p->Fun(lg,p->Num);
if(!Ret)return Ret;j+=p->Len;
if(j>=n)break;}
}
logo->Pos = pos + cp->Len;
// Ret = true;
}
return Ret;
}
bool __fastcall LogoCmd::Run()
{
bool Ret=false;
if(List){
Pos = 0;
CmdList *p = List;
Clear(this,0);
Command fp;
for(int i=0;i<Count;){
fp = p->Fun;
if(fp==LogoCmd::Redo)
Pos = i;
Ret = fp(this,p->Num);
if(!Ret)return Ret;
i += p->Len;
if(i>=Count)break;
p += p->Len;
}
// Ps->Left = x - Ps->Width/2;
// Ps->Top = y - Ps->Height/2;
}
return Ret;
}
bool __fastcall LogoCmd::Left(LogoCmd *logo,int ange)
{
bool Ret=false;
if(logo){
logo->A += ange*A0;
// logo->Pos+=1;
Ret=true;
}
return Ret;
}
bool __fastcall LogoCmd::Right(LogoCmd *logo,int ange)
{
bool Ret=false;
if(logo){
logo->A -= ange*A0;
// logo->Pos+=1;
Ret = true;
}
return Ret;
}
bool __fastcall LogoCmd::Forward(LogoCmd *logo,int len)
{
bool Ret=false;
if(logo){
// float x1 = logo->x,y1 = logo->y;
logo->x += len*cos(logo->A);
logo->y += len*sin(logo->A);
if(logo->Draw)
logo->Ca->LineTo(logo->x,logo->y);
else
logo->Ca->MoveTo(logo->x,logo->y);
// if(logo->Show&&logo->Ps){
// logo->Ps->Left = logo->x - logo->Ps->Width/2;
// logo->Ps->Top = logo->y - logo->Ps->Height/2;}
// logo->Pos+=1;
Ret =true;
}
return Ret;
}
bool __fastcall LogoCmd::Back(LogoCmd *logo,int len)
{
bool Ret=false;
if(logo){
// float x1 = logo->x,y1 = logo->y;
logo->x -= len*cos(logo->A);
logo->y -= len*sin(logo->A);
if(logo->Draw)
logo->Ca->LineTo(logo->x,logo->y);
else
logo->Ca->MoveTo(logo->x,logo->y);
// if(logo->Show&&logo->Ps){
// logo->Ps->Left = logo->x - logo->Ps->Width/2;
// logo->Ps->Top = logo->y - logo->Ps->Height/2;}
// logo->Pos+=1;
Ret =true;
}
return Ret;
}
bool __fastcall LogoCmd::Up(LogoCmd *logo,int)
{
bool Ret=false;
if(logo){
logo->Draw = false;
// logo->Pos+=1;
Ret =true;
}
return Ret;
}
bool __fastcall LogoCmd::Down(LogoCmd *logo,int)
{
bool Ret=false;
if(logo){
logo->Draw = true;
// logo->Pos+=1;
Ret =true;
}
return Ret;
}
bool __fastcall LogoCmd::ShowIt(LogoCmd *logo,int)
{
bool Ret=false;
if(logo){
logo->Show = true;
logo->Pos+=1;
Ret =true;
}
return Ret;
}
bool __fastcall LogoCmd::HideIt(LogoCmd *logo,int)
{
bool Ret=false;
if(logo){
logo->Show = false;
logo->Pos+=1;
Ret =true;
}
return Ret;
}
bool __fastcall LogoCmd::PenColor(LogoCmd *logo,int clr)
{
static TColor Cl[16]={clNone,clBlue,clRed,clGreen,clBtnFace,clBlue+clRed,
clBlue+clGreen,clRed+clGreen,0x00202020,0x00404040,0x00606060,0x00808080,
0x00a0a0a0,0x00c0c0c0,0x00e0e0e0,0x00ffffff};
bool Ret=false;
if(logo){
logo->Ca->Pen->Color = Cl[clr%16];
logo->Pos+=1;
Ret =true;
}
return Ret;
}
//
//bool __fastcall LogoCmd::Color(LogoCmd *logo,int clr)
//{
// bool Ret=false;
// if(logo){
// }
// return Ret;
//}
//
bool __fastcall LogoCmd::Clear(LogoCmd *logo,int)
{
bool Ret=false;
if(logo){
logo->Ca->FillRect(logo->Ca->ClipRect);
logo->Ca->MoveTo(logo->x0,logo->y0);
logo->x = logo->x0;
logo->y = logo->y0;
// if(logo->Show&&logo->Ps){
// logo->Ps->Left = logo->x - logo->Ps->Width/2;
// logo->Ps->Top = logo->y - logo->Ps->Height/2;}
logo->Pos+=1;
Ret =true;
}
return Ret;
}
bool __fastcall LogoCmd::Read(AnsiString str)
{
bool Ret=false;
AnsiString s=str.Trim().LowerCase();
if(s.IsEmpty()){
return Ret;}
char *p = s.c_str(),*q,*st,ch;
q=p;
bool gs = (*p=='{'||*p=='}');
if(gs)
return Ret;
TStrings *ss = 0;
try {
ss = new TStringList();
}
catch(...){
return Ret;}
bool bc = false;
while(*p)
switch(*p){
case ' ':case '\t':case '\n':case '\r':
if(bc){
st = p;p++;*st = '\0';
ss->Append(q);
bc = false;}
else p++;
// bc = false;
continue;
case '{':case '}':
if(bc){
ch = *p;
*p = '\0';
ss->Append(q);
*p = ch;
bc = false;}
q = p++;
ch = *p;
*p = '\0';
ss->Append(q);
*p = ch;
// bc = false;
break;
default:
if(!bc){
q = p;
bc = true;
}
p++;
}
if(bc)
ss->Append(q);
int count = ss->Count;
FreeList();
if(!SetList(count))
return Ret;
int Kn[20]={0},k=0,c,j;
for(int i=j=0;i<count;++i)
switch(c = (ss->Strings[i].c_str())[0]){
case '{':Kn[k++]=j-1;continue;
case '}':List[Kn[--k]].Len = j - Kn[k];Kn[k]=0;continue;
default:
switch(c){
case '0':case '1':case '2':case '3':case '4':
case '5':case '6':case '7':case '8':case '9':
case '-':case '+':
List[j-1].Num =ss->Strings[i].ToInt();continue;
default:
List[j].Fun = CmdGet(ss->Strings[i]);
if(List[j].Fun==0){
delete ss;
return Ret;}
List[j].Len =1;
j++;
}
}
Count = j;
Ret = true;
if(ss)delete ss;
return Ret;
}
bool __fastcall LogoCmd::SetList(int n)
{
if (List)return false;
try{
List = new CmdList[n];
}
catch(...){
return false;
}
return true;
}
bool __fastcall LogoCmd::FreeList()
{
if(List){
delete []List;
List = 0;
return true;
}
return false;
}
bool __fastcall LogoCmd::ReadFromFile(AnsiString filename)
{
bool Ret = false;
if(filename.Trim().IsEmpty())return Ret;
if(FileExists(filename))return Ret;
TStream *fs = 0;
try{
fs = new TFileStream(filename,fmOpenRead|fmShareDenyNone);
}
catch(...){
return Ret;}
TStrings *ss = 0;
try {
ss = new TStringList();
}
catch(...){
delete fs;
return Ret;}
AnsiString s="";
char c;
fs->Position =0;
for(int i=0,n = fs->Size;i<n;++i){
fs->Read(&c,1);
switch(c){
case ' ':case '\t':case '\n':case '\r':case ';':
ss->Append(s);s="";continue;
case '{':case '}':
ss->Append(s);s=c;ss->Append(s);s="";continue;
default:
s = s+c;}}
if(!s.IsEmpty())
ss->Append(s);
delete fs;
int count = ss->Count;
FreeList();
if(!SetList(count))
return Ret;
int Kn[20]={0},k=0,j;
for(int i=j=0;i<count;++i)
switch(c = (ss->Strings[i].c_str())[0]){
case '{':Kn[k++]=j-1;continue;
case '}':List[Kn[--k]].Len = j - Kn[k];Kn[k]=0;continue;
default:
switch(c){
case '0':case '1':case '2':case '3':case '4':
case '5':case '6':case '7':case '8':case '9':
case '-':case '+':
List[j-1].Num =ss->Strings[i].ToInt();continue;
default:
List[j].Fun = CmdGet(ss->Strings[i]);
if(List[j].Fun==0){
delete ss;
return Ret;}
List[j].Len =1;
j++;
}
}
Count = j;
Ret = true;
if(ss)delete ss;
return Ret;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -