//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Tex.h" #include "common.h" //--------------------------------------------------------------------------- #pragma package(smart_init) //--------------------------------------------------------------------------- __fastcall Tex::Tex(){ Curve = new TList; Wales = new TList; Courses = new TList; Raise = new TList; Force = new TList; Visible = true; leftShift=0; WaleState *ws = new WaleState; Wales->Add(ws);//¾ðÁ¦³ª 1°³ ÀÌ»ó old_iotc=0; lpThreading=NULL; } //--------------------------------------------------------------------------- __fastcall Tex::~Tex(){ if(lpThreading) VirtualUnlock(lpThreading,old_iotc*sizeof(Threading)); if(lpThreading) VirtualFree(lpThreading,old_iotc*sizeof(Threading),MEM_DECOMMIT); if(Curve){ while(Curve->Count){ delete (KO *)Curve->Last(); Curve->Remove(Curve->Last()); } delete Curve; } if(Wales){ while(Wales->Count){ delete (WaleState *)Wales->Last(); Wales->Remove(Wales->Last()); } delete Wales; } if(Courses){ while(Courses->Count){ delete (CourseState *)Courses->Last(); Courses->Remove(Courses->Last()); } delete Courses; } if(Raise){ while(Raise->Count){ delete (bool *)Raise->Last(); Raise->Remove(Raise->Last()); } delete Raise; } if(Force){ while(Force->Count){ delete (bool *)Force->Last(); Force->Remove(Force->Last()); } delete Force; } } //--------------------------------------------------------------------------- void __fastcall Tex::SetHeight(int h){ Height = h; if(Curve->Count>h){ while(Curve->Count>h){ delete (KO *)Curve->Last(); Curve->Remove(Curve->Last()); } } else { while(Curve->CountCount%2){ KO *ko = new KO; ko->in = 1; ko->via = 0; ko->out = 1; Curve->Add(ko); } else { KO *ko = new KO; ko->in = 2; ko->via = 3; ko->out = 2; Curve->Add(ko); } } } if(Courses->Count>h){ while(Courses->Count>h){ delete (CourseState *)Courses->Last(); Courses->Remove(Courses->Last()); } } else { while(Courses->Countemit=DEFAULT_YARN_EMIT; Courses->Add(cs); } } if(Raise->Count>h){ while(Raise->Count>h){ delete (bool *)Raise->Last(); Raise->Remove(Raise->Last()); } } else { while(Raise->CountAdd(ol); } } if(Force->Count>h){ while(Force->Count>h){ delete (bool *)Force->Last(); Force->Remove(Force->Last()); } } else { while(Force->CountAdd(ol); } } } //--------------------------------------------------------------------------- int __fastcall Tex::GetHeight(){ return Curve->Count; } //--------------------------------------------------------------------------- void __fastcall Tex::Draw(HDC hDC, TList *YarnList, RECT r, int DistX, int DistY, bool OneCurve, POINT delta){ if(!Curve)return; if(Curve->Count==0)return; Correction(); KO *ko; int w = (r.right-r.left)/DistX; int h = (r.bottom-r.top)/DistY; if(Wales->Count<0) { return; } SetThreading(); if(lpThreading==NULL) return; for(int j = -w-leftShift; j <= w-leftShift; j++ ){ Yarn *yarn = NULL; yarn = GetYarn(YarnList, j, OneCurve); if(yarn==NULL) continue; HPEN hOldPen,hPen; HBRUSH hOldBrush = SelectObject(hDC, GetStockObject(NULL_BRUSH)); hPen = CreatePen(PS_SOLID,1,yarn->color); hOldPen = SelectObject(hDC, hPen); for(int i=0;iItems[i%Curve->Count]); DrawKo(hDC, ko , r, j+leftShift, i, DistX, DistY, delta);//leftShift Á·Π½¬ÇÁÆ® } SelectObject(hDC, hOldPen); SelectObject(hDC, hOldBrush); DeleteObject(hPen); } } //--------------------------------------------------------------------------- void __fastcall Tex::MouseMove(TShiftState Shift, int X , int Y){ } //--------------------------------------------------------------------------- void __fastcall Tex::MouseDown(TShiftState Shift, int X , int Y, bool Complex){ if(Y>=Curve->Count)return; KO *ko = (KO *)(Curve->Items[Y]); int x = X -leftShift; if(Complex){ if(Shift.Contains(ssCtrl)){ ko->in = x; } else if(Shift.Contains(ssAlt)){ ko->via = x; } else { ko->out = x; } } else { if(!Shift.Contains(ssCtrl)){ if(ko->in>ko->via){ ko->in = x; ko->via = x+1; } else if(ko->invia){ ko->in = x + 1; ko->via = x; } else { ko->in = x; ko->via = x+1; ko->out = x; } } else { if(ko->in>ko->via){ if(ko->via>x) ko->out = ko->via; else if(ko->viaout = ko->via+1; else { if(ko->out>ko->via) ko->out = ko->via; else ko->out = ko->via+1; } } else if(ko->invia){ if(ko->via-1>x) ko->out = ko->via-1; else if(ko->via-1out = ko->via; else { if(ko->outvia) ko->out = ko->via; else ko->out = ko->via-1; } } } } Correction(); } //--------------------------------------------------------------------------- void __fastcall Tex::DrawKo(HDC hDC, KO *ko , RECT r, int x, int y, int DistX, int DistY, POINT delta){ r.right--; // r.bottom--; // correction int inx, outx; int minx,maxx; double midx; minx = min(ko->in,min(ko->via,ko->out)); maxx = max(ko->in,max(ko->via,ko->out)); midx = (double)(minx + maxx)/2.0; double R = DistX/4.0; int inMode; /* 0 == bottom, 1 == top, 2 == vert. */ bool cw; if(ko->invia) cw = false; /* µ¹¾Æ°¡´Â ¹æÇâÀ» Á¤ÇÏ´Â°Ô °¡Àå ¿ì¼±ÀÌ´Ù */ else cw = true; /* cw == clock-wise */ if(cw) { outx = ko->via; inx = ko->in - 1; } else { outx = ko->via-1; inx = ko->in; } static int oldvia = ko->in; if(y==0) oldvia = ko->in; if(midx>ko->in) { if(oldvia>ko->in) inMode = 0; else inMode = 1; } else { if(oldviain) inMode = 0; else inMode = 1; } oldvia = ko->via; static int oldout = ko->in; if(y==0) oldout = ko->in; if(oldout==ko->in) inMode=2; oldout = ko->out; if(minx!=maxx){ if(y==0) { DrawKoInVert(hDC, r, x + inx, y, DistX, DistY, cw, delta); } else { if(inMode==1)DrawKoInTop(hDC, r, x + inx, y, DistX, DistY, cw, delta); else if(inMode==0)DrawKoInBottom(hDC, r, x + inx, y, DistX, DistY, cw, delta); else if(inMode==2)DrawKoInVert(hDC, r, x + inx, y, DistX, DistY, cw, delta); } } if(minx==maxx){ if(y>0)LineTo(hDC, r.right - (ko->in + x)*DistX , r.bottom - y*DistY - 0.5*DistY + R ); else MoveToEx(hDC, r.right - (ko->in + x)*DistX , r.bottom - y*DistY - 0.5*DistY + R, NULL ); LineTo(hDC, r.right - (ko->in + x)*DistX , r.bottom - y*DistY - 0.5*DistY - R ); } else if(minx+1via==ko->out) { DrawKoOutOpen(hDC, r, x + outx, y, DistX, DistY, cw, delta); } else { KO *ko2 = (KO *)(Curve->Items[(y+1)%(Curve->Count)]); if(ko->out!=ko2->in)DrawKoOutClosed(hDC, r, x + outx, y, DistX, DistY, cw, delta); else DrawKoOutClosedVert(hDC, r, x + outx, y, DistX, DistY, cw, delta); } } } //--------------------------------------------------------------------------- void __fastcall Tex::DrawKoInTop(HDC hDC, RECT r, int x, int y, int DistX, int DistY, bool cw, POINT delta){ POINT p; POINT c; c.x = r.right - x*DistX - 0.5*DistX; c.y = r.bottom - y*DistY - 0.5*DistY; c.x+=delta.x?1:0; c.y+=delta.y?1:0; double R = DistX/4.0; if(cw==true) { p.x = c.x - R; p.y = c.y - R*0.4; } else { p.x = c.x + R; p.y = c.y - R*0.4; } if(y>0) { LineTo(hDC, p.x, p.y ); } else { MoveToEx(hDC, p.x , p.y ,NULL); } if(cw==true) { LineTo(hDC, c.x - R/1.414 , c.y - R/1.414); Arc(hDC, c.x - R , c.y - R, c.x + R , c.y + R, c.x , c.y - R, c.x - R/1.414 , c.y - R/1.414); MoveToEx(hDC, c.x , c.y - R,NULL); } else { LineTo(hDC, c.x + R/1.414 , c.y - R/1.414); Arc(hDC, c.x - R , c.y - R, c.x + R , c.y + R, c.x + R/1.414 , c.y - R/1.414, c.x , c.y - R); MoveToEx(hDC, c.x , c.y - R,NULL); } } //--------------------------------------------------------------------------- void __fastcall Tex::DrawKoInBottom(HDC hDC, RECT r, int x, int y, int DistX, int DistY, bool cw, POINT delta){ POINT p; POINT c; c.x = r.right - x*DistX - 0.5*DistX; c.y = r.bottom - y*DistY - 0.5*DistY; c.x+=delta.x?1:0; c.y+=delta.y?1:0; double R = DistX/4.0; if(cw==true) { p.x = c.x - R*0.4; p.y = c.y + R; } else { p.x = c.x + R*0.4; p.y = c.y + R; } if(y>0) { LineTo(hDC, p.x, p.y ); } else { MoveToEx(hDC, p.x , p.y ,NULL); } if(cw==true) { LineTo(hDC, c.x - R/1.414 , c.y + R/1.414); Arc(hDC, c.x - R , c.y - R, c.x + R , c.y + R, c.x , c.y - R, c.x - R/1.414 , c.y + R/1.414); MoveToEx(hDC, c.x , c.y - R,NULL); } else { LineTo(hDC, c.x + R/1.414 , c.y + R/1.414); Arc(hDC, c.x - R , c.y - R, c.x + R , c.y + R, c.x + R/1.414 , c.y + R/1.414, c.x , c.y - R); MoveToEx(hDC, c.x , c.y - R,NULL); } } //--------------------------------------------------------------------------- void __fastcall Tex::DrawKoInVert(HDC hDC, RECT r, int x, int y, int DistX, int DistY, bool cw, POINT delta){ POINT p; POINT c; c.x = r.right - x*DistX - 0.5*DistX; c.y = r.bottom - y*DistY - 0.5*DistY; c.x+=delta.x?1:0; c.y+=delta.y?1:0; double R = DistX/4.0; if(cw==true) { p.x = c.x - R/**0.4*/; p.y = c.y /*+ R*/; } else { p.x = c.x + R/**0.4*/; p.y = c.y /*+ R*/; } if(y>0) { LineTo(hDC, p.x, p.y ); } else { MoveToEx(hDC, p.x , p.y ,NULL); } if(cw==true) { //LineTo(hDC, c.x - R/1.414 , c.y + R/1.414); Arc(hDC, c.x - R , c.y - R, c.x + R , c.y + R, c.x , c.y - R, c.x - R/*/1.414*/ , c.y /*+ R/1.414*/); MoveToEx(hDC, c.x , c.y - R,NULL); } else { //LineTo(hDC, c.x + R/1.414 , c.y + R/1.414); Arc(hDC, c.x - R , c.y - R, c.x + R , c.y + R, c.x + R/*/1.414 */, c.y /*+ R/1.414*/, c.x , c.y - R); MoveToEx(hDC, c.x , c.y - R,NULL); } } //--------------------------------------------------------------------------- void __fastcall Tex::DrawKoOutOpen(HDC hDC, RECT r, int x, int y, int DistX, int DistY, bool cw, POINT delta){ POINT c; c.x = r.right - x*DistX - 0.5*DistX; c.y = r.bottom - y*DistY - 0.5*DistY; c.x+=delta.x?1:0; c.y+=delta.y?1:0; double R = DistX/4.0; if(cw==true) { Arc(hDC, c.x - R , c.y - R, c.x + R , c.y + R, c.x + R/*/1.414*/ , c.y /*+ R/1.414*/, c.x , c.y - R); MoveToEx(hDC, c.x + R/*/1.414*/ , c.y /*+ R/1.414*/ ,NULL); //LineTo(hDC, c.x + R , c.y + R*0.4); } else { Arc(hDC, c.x - R , c.y - R, c.x + R , c.y + R, c.x , c.y - R, c.x - R/*/1.414*/ , c.y /*+ R/1.414*/); MoveToEx(hDC, c.x - R/*/1.414*/ , c.y /*+ R/1.414*/ ,NULL); //LineTo(hDC, c.x - R , c.y + R*0.4); } } //--------------------------------------------------------------------------- void __fastcall Tex::DrawKoOutClosedVert(HDC hDC, RECT r, int x, int y, int DistX, int DistY, bool cw, POINT delta){ POINT c; c.x = r.right - x*DistX - 0.5*DistX; c.y = r.bottom - y*DistY - 0.5*DistY; c.x+=delta.x?1:0; c.y+=delta.y?1:0; double R = DistX/4.0; if(cw==true) { Arc(hDC, c.x - R , c.y - R, c.x + R , c.y + R, c.x - R/*/1.414*/ , c.y /*+ R/1.414*/, c.x , c.y - R); MoveToEx(hDC, c.x - R/*/1.414*/ , c.y /*+ R/1.414*/ ,NULL); //LineTo(hDC, c.x - R , c.y + R*0.4); } else { Arc(hDC, c.x - R , c.y - R, c.x + R , c.y + R, c.x , c.y - R, c.x + R/*/1.414*/ , c.y /*+ R/1.414*/); MoveToEx(hDC, c.x + R/*/1.414*/ , c.y /*+ R/1.414*/ ,NULL); //LineTo(hDC, c.x + R , c.y + R*0.4); } } //--------------------------------------------------------------------------- void __fastcall Tex::DrawKoOutClosed(HDC hDC, RECT r, int x, int y, int DistX, int DistY, bool cw, POINT delta){ POINT c; c.x = r.right - x*DistX - 0.5*DistX; c.y = r.bottom - y*DistY - 0.5*DistY; c.x+=delta.x?1:0; c.y+=delta.y?1:0; double R = DistX/4.0; if(cw==true) { Arc(hDC, c.x - R , c.y - R, c.x + R , c.y + R, c.x - R/1.414 , c.y + R/1.414, c.x , c.y - R); MoveToEx(hDC, c.x - R/1.414 , c.y + R/1.414 ,NULL); LineTo(hDC, c.x - R , c.y + R*0.4); } else { Arc(hDC, c.x - R , c.y - R, c.x + R , c.y + R, c.x , c.y - R, c.x + R/1.414 , c.y + R/1.414); MoveToEx(hDC, c.x + R/1.414 , c.y + R/1.414 ,NULL); LineTo(hDC, c.x + R , c.y + R*0.4); } } //--------------------------------------------------------------------------- void __fastcall Tex::Correction(void){ KO *ko1,*ko2; for(int i=0;iCount;i++){ ko1 = (KO *)Curve->Items[i]; ko2 = (KO *)Curve->Items[(i+1)%Curve->Count]; if(ko1->via>ko1->in){ if(ko1->via<=ko2->in) ko1->out = ko1->via; else ko1->out = ko1->via-1; } else if(ko1->viain){ if(ko1->via>=ko2->in) ko1->out = ko1->via; else ko1->out = ko1->via+1; } else { ko1->out = ko1->in; } } } //--------------------------------------------------------------------------- bool __fastcall Tex::ReadFromFile(HANDLE hFile, int main_ver){ DWORD dwRead; int curveCnt,waleCnt,courseCnt,overlapCnt,forceCnt; int Width,Height,leftShift; bool Visible; try{ if(Curve){ while(Curve->Count){ delete (KO *)Curve->Last(); Curve->Remove(Curve->Last()); } //delete Curve; } if(Wales){ while(Wales->Count){ delete (WaleState *)Wales->Last(); Wales->Remove(Wales->Last()); } //delete Wales; } if(Courses){ while(Courses->Count){ delete (CourseState *)Courses->Last(); Courses->Remove(Courses->Last()); } //delete Courses; } if(Raise){ while(Raise->Count){ delete (bool *)Raise->Last(); Raise->Remove(Raise->Last()); } //delete Raise; } if(Force){ while(Force->Count){ delete (bool *)Force->Last(); Force->Remove(Force->Last()); } //delete Force; } if(main_ver>=100){ int ver; ReadFile(hFile, &ver, sizeof(int), &dwRead, NULL); ReadFile(hFile, &Width, sizeof(int), &dwRead, NULL); ReadFile(hFile, &Height, sizeof(int), &dwRead, NULL); ReadFile(hFile, &leftShift, sizeof(int), &dwRead, NULL); ReadFile(hFile, &Visible, sizeof(bool), &dwRead, NULL); this->Width=Width; this->Height=Height; this->leftShift=leftShift; this->Visible=Visible; ReadFile(hFile, &curveCnt, sizeof(int), &dwRead, NULL); for(int i=0;iAdd(ko); } ReadFile(hFile, &waleCnt, sizeof(int), &dwRead, NULL); for(int i=0;iReadFromFile(hFile); Wales->Add(ws); } ReadFile(hFile, &courseCnt, sizeof(int), &dwRead, NULL); for(int i=0;iAdd(cs); } if(ver>=2){ ReadFile(hFile, &overlapCnt, sizeof(int), &dwRead, NULL); for(int i=0;iAdd(ol); } } if(ver>=3){ ReadFile(hFile, &forceCnt, sizeof(int), &dwRead, NULL); for(int i=0;iAdd(ol); } } } else if(main_ver>=1){ ReadFile(hFile, &Width, sizeof(int), &dwRead, NULL); ReadFile(hFile, &Height, sizeof(int), &dwRead, NULL); ReadFile(hFile, &leftShift, sizeof(int), &dwRead, NULL); ReadFile(hFile, &Visible, sizeof(bool), &dwRead, NULL); this->Width=Width; this->Height=Height; this->leftShift=leftShift; this->Visible=Visible; ReadFile(hFile, &curveCnt, sizeof(int), &dwRead, NULL); for(int i=0;iAdd(ko); } ReadFile(hFile, &waleCnt, sizeof(int), &dwRead, NULL); for(int i=0;iReadFromFile(hFile, main_ver);//ÀÏ´Ü Àаí TexLayer¿¡¼­ WaleState·Î º¯È¯ÇÑ´Ù Wales->Add(ws); } ReadFile(hFile, &courseCnt, sizeof(int), &dwRead, NULL); for(int i=0;iAdd(cs); } } } catch(...){ ShowMessage(IDS_MESSAGE_LOAD_FAIL); return false; } return true; } //--------------------------------------------------------------------------- bool __fastcall Tex::SaveToFile(HANDLE hFile){ DWORD dwWrite; try{ int ver=3; WriteFile(hFile, &ver, sizeof(int), &dwWrite, NULL); WriteFile(hFile, &Width, sizeof(int), &dwWrite, NULL); WriteFile(hFile, &Height, sizeof(int), &dwWrite, NULL); WriteFile(hFile, &leftShift, sizeof(int), &dwWrite, NULL); WriteFile(hFile, &Visible, sizeof(bool), &dwWrite, NULL); WriteFile(hFile, &(Curve->Count), sizeof(int), &dwWrite, NULL); for(int i=0;iCount;i++){ WriteFile(hFile, Curve->Items[i], sizeof(KO), &dwWrite, NULL); } WriteFile(hFile, &(Wales->Count), sizeof(int), &dwWrite, NULL); for(int i=0;iCount;i++){ ((WaleState *)Wales->Items[i])->SaveToFile(hFile); } WriteFile(hFile, &(Courses->Count), sizeof(int), &dwWrite, NULL); for(int i=0;iCount;i++){ WriteFile(hFile, Courses->Items[i], sizeof(CourseState), &dwWrite, NULL); } WriteFile(hFile, &(Raise->Count), sizeof(int), &dwWrite, NULL); for(int i=0;iCount;i++){ WriteFile(hFile, Raise->Items[i], sizeof(bool), &dwWrite, NULL); } WriteFile(hFile, &(Force->Count), sizeof(int), &dwWrite, NULL); for(int i=0;iCount;i++){ WriteFile(hFile, Force->Items[i], sizeof(bool), &dwWrite, NULL); } }catch(...){ ShowMessage(IDS_MESSAGE_SAVE_FAIL); return false; } return true; } //--------------------------------------------------------------------------- double __fastcall Tex::DefaultLength(int i, double Denier, int Multi, double CPC, int WPI, int length_method){ double p=10.0/CPC; double w=25.4/WPI; double d=0; KO *ko2 = (KO *)Curve->Items[(i+1)%Curve->Count]; KO *ko = (KO *)Curve->Items[i]; int n = fabs((ko2->in+ko2->via)-(ko->in+ko->via))/2.0; if(ko->in==ko->via) return 0; switch(length_method){ case 0: { d=0.01371*sqrt(Denier); break; } case 1: { d=0.019*sqrt(12*Multi-3)/3.0; break; } case 2: { d=0.0114*sqrt(Denier); break; } default: { d=0.01371*sqrt(Denier); break; } } return sqrt(p*p+n*n*w*w)+2.05*sqrt(p*p+4.0*d*d)+7.28*d; } //--------------------------------------------------------------------------- double __fastcall Tex::DefaultLengthPer480Course(double Denier, int Multi, double CPC, int WPI, int length_method){ if(Curve->Count==0)return 0; double l=0; for(int i=0;iCount;i++){ l+=DefaultLength(i,Denier,Multi,CPC,WPI,length_method); } l/=Curve->Count; return l*480.0; } //--------------------------------------------------------------------------- double __fastcall Tex::LengthPer480Course(){ if(Courses->Count==0)return 0; double meanEmit=0; CourseState *cs; for(int h=0;hCount;h++){ cs = (CourseState *)Courses->Items[h]; meanEmit+=cs->emit; } meanEmit/=Courses->Count; return meanEmit; } //--------------------------------------------------------------------------- double __fastcall Tex::mPerYard(double Denier, int Multi, double CPC, int WPI, int length_method){ if(Courses->Count==0){ return DefaultLengthPer480Course(Denier, Multi, CPC, WPI, length_method)/480.0*CPC*91.44/1000.0; } else { return LengthPer480Course()/480.0*CPC*91.44/1000.0; } } //--------------------------------------------------------------------------- double __fastcall Tex::gPerYard(double Denier, int Multi, double CPC, int WPI, int length_method, int total/*ÃѺ»¼ö*/){ return mPerYard(Denier, Multi, CPC, WPI, length_method)*(double)total*Denier/9000.0; } //--------------------------------------------------------------------------- void __fastcall Tex::SetThreading(){ if(lpThreading) VirtualUnlock(lpThreading,old_iotc*sizeof(Threading)); if(lpThreading) VirtualFree(lpThreading,old_iotc*sizeof(Threading),MEM_DECOMMIT); lpThreading=NULL; old_iotc=0; for(int i=0;iCount;i++){ old_iotc+=((WaleState *)Wales->Items[i])->cnt; } lpThreading = (Threading *)VirtualAlloc(NULL,old_iotc*sizeof(Threading),MEM_COMMIT,PAGE_READWRITE); if(lpThreading) VirtualLock(lpThreading,old_iotc*sizeof(Threading)); int k=0; for(int i=0;iCount;i++){ WaleState *ws = (WaleState *)Wales->Items[i]; for(int j=0;jcnt;j++){ (lpThreading+k)->yarnIndex=ws->yarnIndex; (lpThreading+k)->in=ws->in; (lpThreading+k)->isTransParent=ws->isTransParent; k++; } } } //--------------------------------------------------------------------------- Yarn * __fastcall Tex::GetYarn(TList *YarnList, int i, bool OneCurve){ int l=i; while(l<0){l+=old_iotc;} l%=old_iotc; if(OneCurve==true){ if(i==0) { return (Yarn *)YarnList->Items[(lpThreading)->yarnIndex]; } else { return NULL; } } else { if((lpThreading+l)->in){ return (Yarn *)YarnList->Items[(lpThreading+l)->yarnIndex]; } else { return NULL; } } } //--------------------------------------------------------------------------- bool __fastcall Tex::GetTransParent(int i){ int l=i; while(l<0){l+=old_iotc;} l%=old_iotc; if((lpThreading+l)->in){ return (lpThreading+l)->isTransParent; } else { return true; } } //---------------------------------------------------------------------------