//--------------------------------------------------------------------------- #include #include #include #include #pragma hdrstop #include "YarnDesign_F.h" #include "BaseYarnDesign_F.h" #include "Common.h" #include "Exception.h" #include "MainImage.h" #include "Palette.h" #include "YarnRepeat_F.h" #include "YarnTable_F.h" #include "YarnUnit_F.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "Rulers" #pragma link "TPImage" #pragma link "TPSelectImage" #pragma link "TPSpin" #pragma resource "*.dfm" //--------------------------------------------------------------------------- #define WIDTH 800 #define HEIGHT 80 #define MAXWIDTH 3200 #define IDS_INFORMATION StringTable[0] #define IDS_YARNUNIT StringTable[1] #define IDS_YARNNUMBER StringTable[2] #define IDS_YARN StringTable[3] #define IDS_TOTALCOUNT StringTable[4] #define IDS_TWIST StringTable[5] #define IDS_TPI StringTable[6] #define IDS_HAIR StringTable[7] #define IDS_HAIREFFECT StringTable[8] #define IDS_LENGTH StringTable[9] #define IDS_DENSITY StringTable[10] #define IDS_MAKE StringTable[11] #define IDS_FUNCTION StringTable[12] #define IDS_FANCY StringTable[13] #define IDS_SUPER StringTable[14] #define IDS_MKFOLDER StringTable[15] #define IDS_DYEDYARN StringTable[16] #define IDS_SELECTCOLOR StringTable[17] #define IDS_METHOD StringTable[18] #define IDS_REDO StringTable[19] #define IDS_PENWIDTH StringTable[20] #define IDS_DEL_FOLDER StringTable[21] #define IDS_SWINDOWOFF StringTable[22] //--------------------------------------------------------------------------- TYarnDesignForm *YarnDesignForm; struct ymakedata { short hdot[5]; // one - repeat height except hair short wdot[5]; // one - repeat width short width; // Image width } ymake = { {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, WIDTH }; //--------------------------------------------------------------------------- __fastcall TYarnDesignForm::TYarnDesignForm(TComponent* Owner) : TForm(Owner) { hWnd = ((TWinControl *)Owner)->Handle; } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::FormCreate(TObject *Sender) { //======================================================= StringTable.Create(DirectoryBin, Language, "YarnDesign"); SetSmallFont(Font); SetSmallFont(StaticText1->Font); SetSmallFont(lbYarnUnit->Font); SetSmallFont(lbYarn1->Font); SetSmallFont(lbYarn2->Font); SetSmallFont(lbYarn3->Font); SetSmallFont(lbYarn4->Font); SetSmallFont(lbUnit->Font); SetSmallFont(stTwist->Font); SetSmallFont(sbYarnUnit->Font); StaticText1->Caption = IDS_INFORMATION; Label1->Caption = IDS_YARNUNIT; Label2->Caption = IDS_COMMON_LENGTH; Label3->Caption = IDS_YARNNUMBER; gbYarn->Caption = IDS_YARN; Label9->Caption = IDS_TOTALCOUNT; gbTwist->Caption = IDS_TWIST; Label4->Caption = IDS_METHOD; stTwist->Caption = "S " + AnsiString(IDS_COMMON_DIRECTION); Label5->Caption = IDS_TPI; Label6->Caption = IDS_PENWIDTH; gbHair->Caption = IDS_HAIR; cbHair->Caption = IDS_HAIREFFECT; Label7->Caption = IDS_LENGTH; Label8->Caption = IDS_DENSITY; MakeButton->Caption = IDS_MAKE; gbFunction->Caption = IDS_FUNCTION; sbFile->Hint = IDS_COMMON_FILE; sbGrid->Hint = IDS_COMMON_GRID; sbUndo->Hint = IDS_COMMON_UNDO; sbRepeat->Hint = IDS_COMMON_REPEAT; sbWindow->Hint = IDS_COMMON_WINDOW; sbMove->Hint = IDS_COMMON_MOVE; sbCopy->Hint = IDS_COMMON_COPY; cbFancy->Caption = IDS_FANCY; cbSuper->Caption = IDS_SUPER; sbRead->Caption = IDS_COMMON_READ; sbMkdir->Caption = IDS_MKFOLDER; sbSave->Caption = IDS_COMMON_SAVE; sbSaveAs->Caption = IDS_COMMON_SAVEAS; sbDelete->Caption = IDS_COMMON_DELETE; sbRename->Caption = IDS_COMMON_RENAME; sbDeldir->Caption = IDS_DEL_FOLDER; sbDyedYarn->Caption = IDS_DYEDYARN; Label12->Caption = IDS_SELECTCOLOR; Label16->Caption = IDS_COMMON_POSITION; sbRect->Caption = IDS_COMMON_RECTANGLE; sbOff->Caption = IDS_SWINDOWOFF; //======================================================= TIniFile *IniFile = new TIniFile(DirectoryItem+"\\YarnDesign.ini"); if (IniFile) { Left = IniFile->ReadInteger("Form", "Left", (Screen->Width-Width)>>1); Top = IniFile->ReadInteger("Form", "Top", (Screen->Height-Height)>>1); delete IniFile; } yarncount = 1; FYarnUnit.Method = YU_DIRECT; FYarnUnit.System = YU_TEX; if (MainImageForm) { FLengthUnit = MainImageForm->CurrentUnit; yDPI = MainImageForm->CanvasInfor.DotsPerInch; } else { FLengthUnit = uCm; yDPI = 160; } UnitChange(); memset(FTempYarnInfor, 0, 4 * sizeof(TYarnInfor *)); ChangeTempYarnInfor(); SZ = false; Review(); Palette = new TPalette; Palette->ColorData[1]->SetNormal(255, 255, 255); Palette->ToRGBQUAD(rgb, 256); if (FullImage->Bitmap->Create(WIDTH, HEIGHT, 8, rgb) == false) goto fail; FullImage->Bitmap->FillRect(Rect(0, 0, WIDTH, HEIGHT), Palette->ColorData[1]->Color); if (PartImage->Bitmap->Create(WIDTH, HEIGHT, 8, rgb) == false) goto fail; PartImage->Bitmap->FillRect(Rect(0, 0, WIDTH, HEIGHT), Palette->ColorData[1]->Color); PartImage->SetZoom(8, 1); bpp = 8; WorkArea = NULL; // qe WorkArea = new TPWorkArea(&(PartImage->Bitmap)); WorkArea->undoMax = 0; YarnUndo = new TUndo; YarnUndo->Set(PartImage->Bitmap, WorkArea); // qe TempBitmap = NULL; Bitmap = NULL; // if ((TempBitmap = new TTexpiaBitmap) == NULL) goto fail; // if ((Bitmap = new TTexpiaBitmap) == NULL) goto fail; // IsDraw = false; Down = false; bmake = false; fload = false; startx = 0; step = 0; pencolor = 1; presy = 0; preey = HEIGHT; FilePanel->SendToBack(); YarnTableForm->Visible = false; YarnTableForm->OnYarnChange = YarnDataChange; YarnTableForm->OnDirChange = DeleteFolder; YarnTableForm->OnCloseForm = ExitFile; IndicateRectangle(0, true); FullImage->Repaint(); PartImage->Repaint(); return; fail: EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); PostMessage(hWnd, TPM_EXITFUNCTION, 0, 0); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::FormDestroy(TObject *Sender) { for (int i = 0; i < 4; i++) { if (FTempYarnInfor[i]) { delete FTempYarnInfor[i]; FTempYarnInfor[i] = NULL; } } delete YarnUndo; delete Palette; TIniFile *IniFile = new TIniFile(DirectoryItem+"\\YarnDesign.ini"); if (IniFile) { if (Left<0) Left = 10; if (Top<0) Top = 10; IniFile->WriteInteger("Form", "Left", Left); IniFile->WriteInteger("Form", "Top", Top); delete IniFile; } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::sbYarnUnitClick(TObject *Sender) { TPoint ptScreen; TYarnUnitForm *Form = new TYarnUnitForm(this); ptScreen = ClientToScreen(Point(sbYarnUnit->Left+sbYarnUnit->Width+10, sbYarnUnit->Top)); Form->Left = ptScreen.x; Form->Top = ptScreen.y; Form->YarnUnit = FYarnUnit; if (Form->ShowModal()==mrOk) { FYarnUnit = Form->YarnUnit; KillFancy(); } delete Form; Review(); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::seYarnNumberChange(TObject *Sender) { TLabel *lb = NULL; TSpeedButton *sb = NULL; if (seYarnNumber->Text.Length()>0) { yarncount = seYarnNumber->Value; ChangeTempYarnInfor(); int tot = 0; for (int i = 1; i <= 4; i++) { lb = (TLabel *) FindComponent("lbYarn" + IntToStr(i)); sb = (TSpeedButton *) FindComponent("sbYarn" + IntToStr(i)); lb->Enabled = (i <= yarncount); sb->Enabled = (i <= yarncount); if (FTempYarnInfor[i-1]) { tot += FTempYarnInfor[i-1]->Detail.Count; } } KillFancy(); bchange = true; seCount->Value = tot; bchange = false; Review(); } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::sbYarnClick(TObject *Sender) { TPoint ptScreen; TSpeedButton *sb = (TSpeedButton *)Sender; TBaseYarnDesignForm *Form = new TBaseYarnDesignForm(this); Form->DIB256Palette->Palette = PaletteForm->DIB256Palette->Palette; if (sb==sbYarn1) { Form->YarnInfor = FTempYarnInfor[0]; } else if (sb==sbYarn2) { Form->YarnInfor = FTempYarnInfor[1]; } else if (sb==sbYarn3) { Form->YarnInfor = FTempYarnInfor[2]; } else if (sb==sbYarn4) { Form->YarnInfor = FTempYarnInfor[3]; } ptScreen = ClientToScreen(Point(gbYarn->Left+sb->Left+sb->Width+10, gbYarn->Top+sb->Top)); Form->Left = ptScreen.x; Form->Top = ptScreen.y; Form->TakeUnit(FYarnUnit, (FLengthUnit==uInch)); if (Form->ShowModal() == mrOk) { if (FYarnUnit.Method) { double ftot = 0.0; for (int i = 0; i < 4; i++) { if (FTempYarnInfor[i]) { ftot += (double) (1.0 / FTempYarnInfor[i]->Detail.Count); } } bchange = true; seCount->Value = 1.0 / ftot; bchange = false; } else { int itot = 0; for (int i = 0; i < 4; i++) { if (FTempYarnInfor[i]) { itot += FTempYarnInfor[i]->Detail.Count; } } bchange = true; seCount->Value = itot; bchange = false; } KillFancy(); Review(); } delete Form; } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::seCountChange(TObject *Sender) { int i, v; if (bchange) return; if (seCount->Text.Length() > 0) { if (seCount->Value > seCount->MaxValue) seCount->Value = seCount->MaxValue; else if (seCount->Value < seCount->MinValue) seCount->Value = seCount->MinValue; if (seCount->Value > 3000) { seCount->Value = 3000; return; } if (FYarnUnit.Method) v = seCount->Value * yarncount; else v = seCount->Value / yarncount; for (i = 0; i < yarncount; i++) { FTempYarnInfor[i]->Detail.Count = v; } Review(); } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::stTwistClick(TObject *Sender) { if (SZ) { SZ = false; stTwist->Caption = "S " + AnsiString(IDS_COMMON_DIRECTION); } else { SZ = true; stTwist->Caption = "Z " + AnsiString(IDS_COMMON_DIRECTION); } KillFancy(); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::EditClick(TObject *Sender) { TPSpinEdit *ed = (TPSpinEdit *) Sender; ed->SetFocus(); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::seLengthClick(TObject *Sender) { seLength->SetFocus(); seLength->SelectAll(); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::MakeButtonClick(TObject *Sender) { if(WorkArea->Mask) sbOffClick(this); if(menuitem == TMove || menuitem == TCopy) ResetMoveCopyMode(); InitMakeData(); Simulation(); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::Simulation() { TPMemoryArea **TP = NULL; // 6 : firstcolor, 2: type, 8: color TPMemoryArea *UNIT = NULL; TPMemoryArea *TEMP = NULL; int i, j; YarnUndo->RemoveAll(); FillPalette(); if (FYarnData.Infor.Detail.TYC > 1) { TP = new TPMemoryArea * [FYarnData.Infor.Detail.TYC]; if (TP == NULL) goto fail; for (i = 0; i < FYarnData.Infor.Detail.TYC; i++) { TP[i] = NULL; TP[i] = new TPMemoryArea(ymake.wdot[i+1], ymake.hdot[i+1]); if (TP[i] == NULL) goto fail; if (TP[i]->lock() == NULL) goto fail; DrawTPUnit(TP[i], i+1); } UNIT = new TPMemoryArea(ymake.width, ymake.hdot[0]); if (UNIT == NULL) goto fail; if (UNIT->lock() == NULL) goto fail; MakeMultiTwistUnit(UNIT, TP); } else { UNIT = new TPMemoryArea(ymake.width, ymake.hdot[0]); if (UNIT == NULL) goto fail; if (UNIT->lock() == NULL) goto fail; DrawTPUnit(UNIT, 0); } if (FYarnData.Infor.Detail.TYC > 1) { for (i = 0; i < FYarnData.Infor.Detail.TYC; i++) { TP[i]->unlock(); delete TP[i]; TP[i] = NULL; } delete[] TP; } TEMP = new TPMemoryArea (ymake.width, HEIGHT); if (TEMP == NULL) goto fail; if (TEMP->lock() == NULL) goto fail; memset(TEMP->pointer(), 1, ymake.width * HEIGHT); ProcessRainbow(UNIT, TEMP); if (UNIT) { UNIT->unlock(); delete UNIT; UNIT = NULL; } if (FYarnData.Infor.Detail.Hair) { if (cbFancy->Checked && (presy || fload)) DrawFancy(TEMP); else DrawHair(TEMP); } DrawYarnImage(TEMP); if (TEMP) { TEMP->unlock(); delete TEMP; TEMP = NULL; } ImageRepaint(true); presy = (HEIGHT - FYarnData.Thick) / 2; preey = presy + FYarnData.Thick; sbRepeat->Enabled = false; cbSuper->Enabled = true; bmake = true; return; fail: if (TP) { if (FYarnData.Infor.Detail.TYC > 1) { for (i = 0; i < FYarnData.Infor.Detail.TYC; i++) { if (TP[i]) { delete TP[i]; TP[i] = NULL; } } delete[] TP; } } if (UNIT) { delete UNIT; UNIT = NULL; } if (TEMP) { delete TEMP; TEMP = NULL; } EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::seLengthChange(TObject *Sender) { if (seLength->Text.Length() > 0) { if (FLengthUnit == uCm) { ymake.width = seLength->Value * yDPI / 2.54 + 0.5; if (ymake.width > MAXWIDTH) { seLength->Value = MAXWIDTH * 2.54 / yDPI; ymake.width = MAXWIDTH; } else if (ymake.width <= 0) { seLength->Value = 2.54 * 16 / yDPI; ymake.width = 16; } } else { ymake.width = seLength->Value * yDPI; if (ymake.width > MAXWIDTH) { seLength->Value = (double) MAXWIDTH / yDPI; ymake.width = MAXWIDTH; } else if (ymake.width <= 16) { seLength->Value = 16.0 / yDPI; ymake.width = 16; } } bmake = false; KillFancy(); } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::sbExitWinClick(TObject *Sender) { FYarnData.Infor.Detail.Hair = cbHair->Checked; seHairLength->Enabled = cbHair->Checked; seHairDensity->Enabled = cbHair->Checked; } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::ScrollBarScroll(TObject *Sender, TScrollCode ScrollCode, int &ScrollPos) { FullImage ->PositionX = ScrollBar->Position; Ruler->Start = ScrollBar->Position; } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::seTPIChange(TObject *Sender) { if (seTPI->Text.Length() > 0) { if (seTPI->Value > 99) seTPI->Value = 99; KillFancy(); } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::seHairChange(TObject *Sender) { TPSpinEdit *sp = (TPSpinEdit *) Sender; if (sp->Text.Length() > 0) { if (sp->Value > 100) sp->Value = 100; if (sp->Value < 0) sp->Value = 0; } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::sbGridClick(TObject *Sender) { PartImage->Grid = sbGrid->Down; } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::sbUndoClick(TObject *Sender) { if(menuitem == TCopy || menuitem == TMove) ResetMoveCopyMode(); if (sbUndo->Tag) { sbUndo->Hint = IDS_REDO; sbUndo->Tag = 0; } else { sbUndo->Hint = IDS_COMMON_UNDO; sbUndo->Tag = 1; } YarnUndo->Read(Rect(0, 0, PartImage->Bitmap->Width, HEIGHT)); ImageRepaint(false); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::sbFileClick(TObject *Sender) { InitFile(); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::sbExitClick(TObject *Sender) { ExitFile(); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::FullImageMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { RectangleMove(X); Down = true; } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::FullImageMouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { if (Down) RectangleMove(X); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::FullImageMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { if (Down) { RectangleMove(X); Down = false; } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::RepeatImageMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { TPMemoryArea *BTemp = NULL; TPMemoryArea *WTemp = NULL; WORD *DP, *SP; Byte *dp, *sp; int i, j; if (bmake) { if (step > 0) { step = 0; IndicateRectangle(0, true); Rsx = min(isx, X) + FullImage->PositionX; Rex = max(isx, X) + FullImage->PositionX; if (Rsx == Rex) return; YarnUndo->Save(Rect(0, 0, PartImage->Bitmap->Width, HEIGHT)); if (bpp == 8) { BTemp = new TPMemoryArea (Rex - Rsx, HEIGHT); if (BTemp == NULL) goto fail; if (BTemp->lock() == NULL) goto fail; if (PartImage->Bitmap->StartScanLine() == false) goto fail; for (i = 0; i < HEIGHT; i++) { sp = (Byte *) PartImage->Bitmap->GetScanLine(i); dp = BTemp->pointer(0, i); for (j = Rsx; j < Rex; j++, dp++) *dp = sp[j]; } PartImage->Bitmap->StopScanLine(); RepeatYarnImage8(BTemp); BTemp->unlock(); delete BTemp; } else { WTemp = new TPMemoryArea (Rex - Rsx, HEIGHT); if (WTemp == NULL) goto fail; if (WTemp->lock() == NULL) goto fail; if (PartImage->Bitmap->StartScanLine() == false) goto fail; for (i = 0; i < HEIGHT; i++) { SP = (WORD *) PartImage->Bitmap->GetScanLine(i); DP = WTemp->pointer(0, i); for (j = Rsx; j < Rex; j++, DP++) *DP = SP[j]; } PartImage->Bitmap->StopScanLine(); RepeatYarnImage15(WTemp); WTemp->unlock(); delete WTemp; } sbRepeat->Enabled = true; } else { step = 1; isx = X; IndicateRectangle(X, false); } } return; fail: if (BTemp) delete BTemp; if (WTemp) delete WTemp; EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::RepeatImageMouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { if (step > 0) IndicateRectangle(X, false); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::PartImageMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { X = (X / 8) + PartImage->PositionX; Y = Y / 8 + 1; switch (menuitem) { case TOff : int pw; if (Button == mbMiddle || (Shift.Contains(ssShift) && Button==mbLeft)) { COLORREF c = PartImage->Bitmap->GetPixelColor(X, Y-1); for(int i=0;i<3;i++)for(int j=0;j<3;j++){ if(X+i-1>=0 && X+i-1<=PartImage->Width-1 && Y+j-1>=0 && Y+j-1<=PartImage->Height-1) nearc[i+j*3]=PartImage->Bitmap->GetPixelColor(X+i-1, Y+j-1-1); else nearc[i+j*3]=c; } if (bpp == 8) { if (c > 1) pencolor = ((c - 2) / 7) * 7 + 2; else pencolor = c; Shape->Brush->Color = Palette->ColorData[pencolor]->Color; } else { RGBQUAD q; TColor2RGB(TColor(c), q.rgbRed, q.rgbGreen, q.rgbBlue); pencolor = RGBToColor15(q); Shape->Brush->Color = TColor(c); } } else if (Button == mbLeft) { Down = true; pw = spPenWidth->Value; if (pw == 1) { ls = 0; rs = 1; } else { ls = pw / 2.0; rs = pw - ls; } YarnUndo->Save(Rect(0, 0, PartImage->Bitmap->Width, HEIGHT)); DrawDot(X, Y); } return; fail: Down = false; EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); return; case TWin: if (Button == mbLeft) WindowMouseDown(X, Y); break; case TMove: case TCopy: //YarnUndo->Save(Rect(0, 0, PartImage->Bitmap->Width, HEIGHT)); MoveCopyMouseDown(X, Y); break; } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::PartImageMouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { X = (X / 8) + PartImage->PositionX; Y = Y / 8 + 1; switch (menuitem) { case TOff: if (Down) { DrawDot(X, Y); } XLabel->Caption = Format("X: %d", OPENARRAY(TVarRec, (X))); YLabel->Caption = Format("Y: %d", OPENARRAY(TVarRec, (Y))); break; case TWin: WindowMouseMove(X, Y); break; case TMove: case TCopy: MoveCopyMouseMove(X, Y); break; } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::PartImageMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { X = (X / 8) + PartImage->PositionX; Y = Y / 8 + 1; if (Down) { DrawDot(X, Y); ImageRepaint(false); Down = false; } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::sbFileFunctionClick(TObject *Sender) { TSpeedButton *sb = (TSpeedButton *) Sender; FItem = sb->Tag; } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::sbMkdirClick(TObject *Sender) { AnsiString dir = ""; if (InputQuery(IDS_COMMON_MAKEFOLDER, IDS_COMMON_INPUTNAME, dir)) { CreateDirectory(dir.c_str(), NULL); YarnTableForm->DirectoryListBox->Update(); } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::sbSaveAsClick(TObject *Sender) { AnsiString fn = "Noname.yan"; if (InputQuery(IDS_COMMON_SAVEAS, IDS_COMMON_INPUTNAME, fn)) { fn = FileNameExt(fn); if (!FileExists(fn)) { YarnSave(fn); } else { EXCEPTION_MESSAGE_OK(EC_FILE_EXIST_ALREADY); sbSaveAsClick(Sender); } } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::sbRepeatClick(TObject *Sender) { TYarnRepeatForm *Form = NULL; TPMemoryArea *Temp = NULL; TPMemoryArea *WTemp = NULL; TUndoData *ud = NULL; WORD *DP, *SP; Byte *dp, *sp; int ec, i, j; if(WorkArea->Mask) sbOffClick(this); //qe Form = new TYarnRepeatForm(this); if ((ud = YarnUndo->Last) == NULL) {ec = EC_UNDO_NONE; goto fail;} if (bpp == 8) { Temp = new TPMemoryArea (Rex - Rsx, HEIGHT); if (Temp == NULL) {ec = EC_MEMORY_LACK; goto fail;} if (Temp->lock() == NULL) {ec = EC_MEMORY_LACK; goto fail;} if (ud->Bitmap->StartScanLine() == false) goto fail; for (i = 0; i < HEIGHT; i++) { sp = (Byte *) ud->Bitmap->GetScanLine(i); dp = Temp->pointer(0, i); for (j = Rsx; j < Rex; j++, dp++) *dp = sp[j]; } ud->Bitmap->StopScanLine(); if (Form->InitForm8(Temp, rgb) == false) {ec = EC_MEMORY_LACK; goto fail;} if (Form->ShowModal() == mrOk) { RepeatYarnImage8(Temp); } Temp->unlock(); delete Temp; } else { WTemp = new TPMemoryArea (Rex - Rsx, HEIGHT); if (WTemp == NULL) {ec = EC_MEMORY_LACK; goto fail;} if (WTemp->lock() == NULL) {ec = EC_MEMORY_LACK; goto fail;} if (ud->Bitmap->StartScanLine() == false) goto fail; for (i = 0; i < HEIGHT; i++) { SP = (WORD *) ud->Bitmap->GetScanLine(i); DP = WTemp->pointer(0, i); for (j = Rsx; j < Rex; j++, DP++) *DP = SP[j]; } ud->Bitmap->StopScanLine(); if (Form->InitForm15(WTemp) == false) {ec = EC_MEMORY_LACK; goto fail;} if (Form->ShowModal() == mrOk) { RepeatYarnImage15(WTemp); } WTemp->unlock(); delete WTemp; } delete Form; return; fail: if (Temp) delete Temp; if (WTemp) delete WTemp; if (Form) delete Form; EXCEPTION_MESSAGE_OK(ec); } //--------------------------------------------------------------------------- // Private Method //--------------------------------------------------------------------------- // Initdata method void __fastcall TYarnDesignForm::Review() { TLabel *label; AnsiString str; lbYarnUnit->Caption = FYarnUnit.Name(); lbUnit->Caption = FYarnUnit.Name(); lbLengthUnit->Caption = FLengthUnit==uCm ? "Cm" : "Inch"; if (FYarnUnit.Method) str = Format("%d/%d", OPENARRAY(TVarRec, (yarncount, (int)seCount->Value))); else str = Format("%d/1", OPENARRAY(TVarRec, ((int)seCount->Value))); lbCount->Caption = str; for (int i = 0; i < 4; i++) { if (FTempYarnInfor[i]) { label = (TLabel *) FindComponent("lbYarn" + IntToStr(i+1)); label->Caption = GetlbYarnCaption(FTempYarnInfor[i], i+1); } } } //--------------------------------------------------------------------------- AnsiString __fastcall TYarnDesignForm::GetlbYarnCaption(TYarnInfor *yi, int i) { AnsiString str; if (yi->Detail.Type == 0) str = "Normal"; else if (yi->Detail.Type == 1) str = "Melange"; else if (yi->Detail.Type == 2) str = "Rainbow"; if (yi->Detail.Count) str = Format(FYarnUnit.Method==YU_DIRECT ? "%d) %s, %d/1" : "%d) %s, 1/%d", OPENARRAY(TVarRec, (i, str.c_str(), yi->Detail.Count))); else str = Format("%d) None", OPENARRAY(TVarRec, (i))); return str; } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::ChangeTempYarnInfor() { for (int i = 0; i < 4; i++) { if (i < yarncount) { if (FTempYarnInfor[i] == NULL) { FTempYarnInfor[i] = new TYarnInfor; InitYarnInfor(i); } } else { if (FTempYarnInfor[i]) { delete FTempYarnInfor[i]; FTempYarnInfor[i] = NULL; } } } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::InitYarnInfor(int i) { FTempYarnInfor[i]->Detail.Count = 5; FTempYarnInfor[i]->Detail.TPI = 5; FTempYarnInfor[i]->Colors = 1; FTempYarnInfor[i]->Color = new TColorData *[1]; FTempYarnInfor[i]->Color[0] = new TColorData; FTempYarnInfor[i]->Color[0]->SetNormal(0, 0, 0); } //--------------------------------------------------------------------------- // simulation method void __fastcall TYarnDesignForm::InitMakeData() { FYarnData.Infor.Detail.TYC = yarncount; if (yarncount > 1) { if (FYarnData.Infor.Infor) { delete[] FYarnData.Infor.Infor; FYarnData.Infor.Infor = NULL; } FYarnData.Infor.Infor = new TYarnInfor[yarncount]; for (int i = 0; i < yarncount; i++) { FYarnData.Infor.Infor[i].Copy(FTempYarnInfor[i]); } } else { FYarnData.Infor.Copy(FTempYarnInfor[0]); } FYarnData.Infor.Detail.TYC = yarncount; FYarnData.Infor.Detail.SZ = SZ; FYarnData.Infor.Detail.TPI = seTPI->Value; FYarnData.Infor.Detail.Hair = cbHair->Checked; FYarnData.Infor.Detail.Count = seCount->Value; FYarnData.Unit.Copy(FYarnUnit); FYarnData.DPI = yDPI; if (FLengthUnit == uCm) FYarnData.Length = seLength->Value * FYarnData.DPI / 2.54; else FYarnData.Length = seLength->Value * FYarnData.DPI; CalculateWidthHeight(); FYarnData.Thick = ymake.hdot[0]; FYarnData.ExactThick = ExactDot; if (FYarnData.Infor.Detail.Hair) FYarnData.Width = HEIGHT; // FYarnData.Width = ((HEIGHT - FYarnData.Thick) * (seHairLength->Value / 100)) + FYarnData.Thick; else FYarnData.Width = FYarnData.Thick; FYarnData.Ratio = YarnRatio; FYarnData.Dyed = false; FYarnData.Rearrange(); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::FillPalette() { int i, j, c; for (i = 0; i < FYarnData.Colors; i++) { for (j = -3; j <= 3; j++) { c = (i * 7) + j + 5; Palette->ColorData[c]->Copy(FYarnData.Color[i]->GetColor(j)); } } Palette->ColorData[1]->SetNormal(255, 255, 255); Palette->ToRGBQUAD(rgb, 256); FullImage->Bitmap->PutColors(0, 256, rgb); PartImage->Bitmap->PutColors(0, 256, rgb); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::CalculateWidthHeight() { int i; double dot; for (i = 0; i <= FYarnData.Infor.Detail.TYC; i++) { if (i) { if (FYarnData.Infor.Infor) { dot = Count2Dot(FYarnData.DPI, FYarnUnit, FYarnData.Infor.Infor[i-1].Detail.Count, YarnRatio); ymake.wdot[i] = FYarnData.DPI / FYarnData.Infor.Infor[i-1].Detail.TPI; } else break; } else { dot = Count2Dot(FYarnData.DPI, FYarnUnit, FYarnData.Infor.Detail.Count, YarnRatio); ymake.wdot[0] = FYarnData.DPI / FYarnData.Infor.Detail.TPI; ExactDot = dot; } ymake.hdot[i] = (int)ceil(dot); if (dot < 1) dot = 1; } } //--------------------------------------------------------------------------- void __fastcall ReadyToMelangeColor(Byte *cs, TYarnInfor *yi) { int i, j, k; for (i = 0, j = 0, k = 0; i < 100; i++, k++) { if (k >= yi->Method[j].Number) { j++; k = 0; } cs[i] = j; } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::DrawTPUnit(TPMemoryArea *Image, int i) { TYarnInfor *yi; double slope, dpc, cpx, dx, ycolor, xcolor; WORD *IP; Byte cs[100], fc, type; int x, y, w, inc, plus; //qe if (i) { yi = &FYarnData.Infor.Infor[i-1]; } else { yi = &FYarnData.Infor; } fc = yi->FirstColor; slope = (double) 8.0 / Image->size.y; cpx = (double) 8.0 / ymake.wdot[i]; type = yi->Detail.Type; if (type == 1) { ///// Melange Draw /////////// ReadyToMelangeColor(cs, yi); if (yi->Detail.SZ) { //////// Z Direction for (y = 0; y < Image->size.y; y++) { IP = Image->pointer(Image->size.x-1, y); if (y > Image->size.y / 2) { ycolor = slope * (Image->size.y - (y - 1)); dx = -cpx; } else { ycolor = slope * y; dx = cpx; } if (ycolor > 4) xcolor = 4; else if (ycolor < 0) xcolor = 0; else xcolor = ycolor; if(y > 1.0/3.0*Image->size.y && y <= 2.0/3.0*Image->size.y) plus = 2; else plus = 0; for (x = 0; x < Image->size.x; x++, IP--) { *IP = (fc << 10) | (type << 8) | (Byte)((fc + cs[random(100)]) * 7 + 2 + xcolor + plus); xcolor += dx; if (xcolor >= 4) { xcolor = 4; dx = -cpx; } if (xcolor <= 0) { xcolor = 0; dx = cpx; } } } } else { //////// S Direction for (y = 0; y < Image->size.y; y++) { IP = Image->pointer(0, y); if (y > Image->size.y / 2) { ycolor = slope * (Image->size.y - (y - 1)); dx = -cpx; } else { ycolor = slope * y; dx = cpx; } if (ycolor > 4) xcolor = 4; else if (ycolor < 0) xcolor = 0; else xcolor = ycolor; if(y > 1.0/3.0*Image->size.y && y <= 2.0/3.0*Image->size.y) plus = 2; else plus = 0; for (x = 0; x < Image->size.x; x++, IP++) { *IP = (fc << 10) | (type << 8) | (Byte)((fc + cs[random(100)]) * 7 + 2 + xcolor + plus); xcolor += dx; if (xcolor >= 4) { xcolor = 4; dx = -cpx; } if (xcolor <= 0) { xcolor = 0; dx = cpx; } } } } } else { ///// Normal Draw & Rainbow Draw /////// if (yi->Detail.SZ) { //////// Z Direction for (y = 0; y < Image->size.y; y++) { IP = Image->pointer(Image->size.x-1, y); if (y > Image->size.y / 2) { ycolor = slope * (Image->size.y - (y - 1)); dx = -cpx; } else { ycolor = slope * y; dx = cpx; } if (ycolor > 4) xcolor = 4; else if (ycolor < 0) xcolor = 0; else xcolor = ycolor; if(y > 1.0/3.0*Image->size.y && y <= 2.0/3.0*Image->size.y) plus = 2; else plus = 0; for (x = 0; x < Image->size.x; x++, IP--) { *IP = (fc << 10) | (type << 8) | (Byte)(fc * 7 + 2 + xcolor + plus); xcolor += dx; if (xcolor >= 4) { xcolor = 4; dx = -cpx; } if (xcolor <= 0) { xcolor = 0; dx = cpx; } } } } else { //////// S Direction for (y = 0; y < Image->size.y; y++) { IP = Image->pointer(0, y); if (y > Image->size.y / 2) { ycolor = slope * (Image->size.y - (y - 1)); dx = -cpx; } else { ycolor = slope * y; dx = cpx; } if (ycolor > 4) xcolor = 4; else if (ycolor < 0) xcolor = 0; else xcolor = ycolor; if(y > 1.0/3.0*Image->size.y && y <= 2.0/3.0*Image->size.y) plus = 2; else plus = 0; for (x = 0; x < Image->size.x; x++, IP++) { *IP = (fc << 10) | (type << 8) | (Byte)(fc * 7 + 2 + xcolor + plus); xcolor += dx; if (xcolor >= 4) { xcolor = 4; dx = -cpx; } if (xcolor <= 0) { xcolor = 0; dx = cpx; } } } } } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::MakeMultiTwistUnit(TPMemoryArea *Image, TPMemoryArea **TP) { int w, x, y, k, wid, hei, i, pileh, index, ux, uy, xx, yy; short grp, tot, uh[4] = {0, 0, 0, 0}, curve[4] = {0, 0, 0, 0}; int dh[4] = {0, 0, 0, 0}; double theta, dx, dy; TPoint rest; WORD *DP, *SP; Byte fcb, fct, color, temp; double st, ct; pileh = 0; for (i = 1; i <= yarncount; i++) { pileh += ymake.hdot[i]; dh[i-1] = pileh; } if (pileh >= ymake.wdot[0]) { // ³Ê¹« ²Ê¹ö¸° °æ¿ì.. for (i = 0; i < yarncount; i++) { uh[i] = ymake.wdot[0] * ((double) ymake.hdot[i+1] / pileh) + 0.5; curve[i] = uh[i] / 8; if (curve[i] == 0) curve[i] = uh[i] / 4; } ux = 0; // 90µµ ȸÀü for (y = 0; y < Image->size.y; y++, ux++) { DP = Image->pointer(0, Image->size.y - y - 1); index = 0; uy = 0; if (ux >= ymake.wdot[index+1]) ux = 0; for (x = 0; x < Image->size.x; x++, uy++, DP++) { if (uy >= uh[index]) { index++; uy = 0; if (index == yarncount) index = 0; } SP = TP[index]->pointer(ux, uy); *DP = *SP; } } // Curve Effect index = 0; tot = 0; for (x = 0; x < Image->size.x; x++) { if (x == tot) { xx = 0; if (curve[index] == 1 && uh[i] > 3) goto next; while (xx < curve[index]) { if (x + xx >= 0 && x + xx < Image->size.x) { DP = Image->pointer(x + xx, 0); *DP = 1; DP = Image->pointer(x + xx, Image->size.y - 1); *DP = 1; } xx++; } next: xx = curve[index]; while (xx > 0) { if (x - xx >= 0 && x - xx < Image->size.x) { DP = Image->pointer(x - xx, 0); *DP = 1; DP = Image->pointer(x - xx, Image->size.y - 1); *DP = 1; } xx--; } tot += uh[index]; index++; if (index == yarncount) index = 0; } } ytox = 0; } else { // Àû´çÈ÷ ²Á °æ¿ì.. theta = asin((double) pileh / ymake.wdot[0]); st = sin(theta); ct = cos(theta); if (FYarnData.Infor.Detail.SZ) { // Z direction dy = (double) Image->size.x * st; hei = dy + (double) Image->size.y * ct; for (y = 0; y < Image->size.y; y++) { for (x = 0; x < Image->size.x; x++) { xx = x * ct + y * st; yy = y * ct - x * st + dy; if (xx >= 0 && yy >= 0) { rest.y = (hei - yy - 1) % pileh; if (rest.y < 0) continue; for (i = 0; i < yarncount; i++) { if (rest.y < dh[i]) { uy = dh[i] - rest.y - 1; break; } } ux = xx % ymake.wdot[i+1]; SP = TP[i]->pointer(ux, uy); DP = Image->pointer(x, y); *DP = *SP; } } } if (Image->size.y > 5) { // Curve Effect grp = (double) Image->size.y / (3 * tan(theta)); fct = 0; fcb = 0; for (x = 0; x < Image->size.x; x++) { DP = Image->pointer(x, Image->size.y - 1); color = (*DP >> 10); if (fct != color) { for (xx = 0; xx < grp; xx++) { w = x - xx - 1; if (w >= 0) { DP = Image->pointer(w , Image->size.y - 1); *DP = (*DP & 0xFF00) | 1; } } fct = color; } DP = Image->pointer(x, 0); color = (*DP >> 10); if (fcb != color) { if (x == 0) { DP = Image->pointer(x, 1); temp = (*DP >> 10); if (temp) goto step3; else goto step4; } step4: for (xx = 0; xx < grp; xx++) { w = x + xx; if (w < Image->size.x) { DP = Image->pointer(w , 0); *DP = (*DP & 0xFF00) | 1; } else { DP = Image->pointer(w - Image->size.x, 0); *DP = (*DP & 0xFF00) | 1; } } step3: fcb = color; } } } } else { // S direction dx = (double) Image->size.y * st + 0.5; for (y = 0; y < Image->size.y; y++) { for (x = 0; x < Image->size.x; x++) { xx = x * ct - y * st + dx; yy = x * st + y * ct; if (xx >= 0 && yy >= 0) { rest.y = yy % pileh; for (i = 0; i < yarncount; i++) { if (rest.y < dh[i]) { if (i == 0) uy = rest.y; else uy = rest.y - dh[i-1]; break; } } ux = xx % ymake.wdot[i+1]; SP = TP[i]->pointer(ux, uy); DP = Image->pointer(x, y); *DP = *SP; } } } if (Image->size.y > 5) { // Curve Effect grp = (double) Image->size.y / (3 * tan(theta)); fct = 0; fcb = 0; for (x = 0; x < Image->size.x; x++) { DP = Image->pointer(x, 0); color = (*DP >> 10); if (fct != color) { for (xx = 0; xx < grp; xx++) { w = x - xx - 1; if (w >= 0) { DP = Image->pointer(w , 0); *DP = (*DP & 0xFF00) | 1; } } fct = color; } DP = Image->pointer(x, Image->size.y - 1); color = (*DP >> 10); if (fcb != color) { if (x == 0) { DP = Image->pointer(x, Image->size.y - 2); temp = (*DP >> 10); if (temp) goto step1; else goto step2; } step2: for (xx = 0; xx < grp; xx++) { w = x + xx; if (w < Image->size.x) { DP = Image->pointer(w , Image->size.y - 1); *DP = (*DP & 0xFF00) | 1; } else { DP = Image->pointer(w - Image->size.x, Image->size.y - 1); *DP = (*DP & 0xFF00) | 1; } } step1: fcb = color; } } } } ytox = 1 / tan(theta); } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::ProcessRainbow(TPMemoryArea *Image, TPMemoryArea *YarnImage) { int x, y, sx, sy, ex, ey, mm, k, pos; short uw, i, j, *length = NULL, *partlen = NULL, diff, delta; Byte fc, *DP; WORD *SP; sx = 0; sy = (HEIGHT - Image->size.y) / 2; ex = Image->size.x; ey = sy + Image->size.y; if (yarncount > 1) { bool RB = false; for (i = 0; i < yarncount; i++) { if (FYarnData.Infor.Infor[i].Detail.Type == 2) { RB = true; break; } } for (y = sy; y < ey; y++) { DP = YarnImage->pointer(0, y); SP = Image->pointer(0, y - sy); for (x = sx; x < ex; x++, DP++, SP++) { *DP = *SP & 0xFF; } } if (RB) { for (i = 0; i < yarncount; i++) { mm = FYarnData.Infor.Infor[i].Methods; fc = FYarnData.Infor.Infor[i].FirstColor; if (mm) { length = (short *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mm * 2); partlen = (short *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mm * 2); if (length && partlen) { for (j = 0, uw = 0; j < mm; j++) { partlen[j] = FYarnData.Infor.Infor[i].Method[j].Number; uw += partlen[j]; length[j] = uw; } //---- private variable ---y¿¡ ´ëÇÑ x º¯È­À² if (FYarnData.Infor.Detail.SZ) diff = -(ytox + 0.5); else diff = ytox + 0.5; for (k = 0, y = sy; y < ey; y++, k++) { DP = YarnImage->pointer(0, y); SP = Image->pointer(0, y - sy); delta = k*diff; pos = 0; for (j = 0, x = sx; x < ex; x++, DP++, SP++) { if ((*SP>>10) == fc && (*SP & 0xFF) != 1) { if ((x + delta) % uw > length[j]) { if (pos > partlen[j]) { pos = 0; j++; } } *DP = (*SP & 0xFF) + 7 * j; } pos++; if (x % uw == 0) { pos = 0; j = 0; } } } HeapFree(GetProcessHeap(), 0, partlen); HeapFree(GetProcessHeap(), 0, length); } else { #ifdef TPDEBUG SHOWDEBUG; return; #endif } } } } } else { if (FYarnData.Infor.Detail.Type == 2) { mm = FYarnData.Infor.Methods; length = (short *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mm * 2); if (length) { for (i = 0, uw = 0; i < mm; i++) { uw += FYarnData.Infor.Method[i].Number; length[i] = uw; } for (y = sy; y < ey; y++) { DP = YarnImage->pointer(0, y); SP = Image->pointer(0, y - sy); for (i = 0, x = sx; x < ex; x++, DP++, SP++) { if (x % uw == length[i]-1) { i++; if (i == mm) i = 0; } *DP = (*SP & 0xFF) + 7 * i; } } HeapFree(GetProcessHeap(), 0, length); } else { #ifdef TPDEBUG SHOWDEBUG; return; #endif } } else { for (y = sy; y < ey; y++) { DP = YarnImage->pointer(0, y); SP = Image->pointer(0, y - sy); for (x = sx; x < ex; x++, DP++, SP++) { *DP = *SP & 0xFF; } } } } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::HairAxis(int &col, int &row, Byte &q) { char h_dir; short axis; h_dir = (((q+4)%8) + 2) % 8; while (1) { axis = rand() % 100; if (axis<35) q = rand() % 2 + 2; else if (axis<70) q = rand() % 2 + 5; else if (axis<85) q = 4; else q = rand() % 8; if (q==h_dir || q==(h_dir+1)%8 || q==(h_dir+2)%8 || q==(h_dir+3)%8 || q==(h_dir+4)%8) { break; } } if (q==0) { col = 0; row = -1; } else if (q==1) { col = 1; row = -1; } else if (q==2) { col = 1; row = 0; } else if (q==3) { col = 1; row = 1; } else if (q==4) { col = 0; row = 1; } else if (q==5) { col = -1; row = 1; } else if (q==6) { col = -1; row = 0; } else if (q==7) { col = -1; row = -1; } if (FYarnData.Infor.Detail.SZ) col *= -1; } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::DrawHair(TPMemoryArea *YarnImage) { Byte c, q, ch = 1; int i, j, tab, lng, col, row; int y, dx, dy, density, length; Byte *DP; density = seHairDensity->Value; length = seHairLength->Value; if (density == 0 || length == 0) return; tab = 100.0 / density * (rand()%5); y = (HEIGHT - ymake.hdot[0]) / 2; for (i = 0; i < YarnImage->size.x; i += tab) { dx = i; DP = YarnImage->pointer(i, y); if (*DP == 1) dy = y + 1; else dy = y ; DP = YarnImage->pointer(i, dy); c = ((*DP - 2) / 7) * 7 + 2; lng = ((double)((HEIGHT - ymake.hdot[0]) / 2.0) * length/100) / (rand()%3+1) + 1; q = 4; col = 0; row = 1; for (j = 0; j < lng; j++) { dx += col; dy -= row; if (dx >= 0 && dx < YarnImage->size.x && dy >= 0 && dy < YarnImage->size.y) { DP = YarnImage->pointer(dx, dy); *DP = c + rand() % 7; } if (ch == 0) HairAxis(col, row, q); ch++ ; if (ch > 1) ch = 0; } tab = 100.0 / density * (rand()%5 + 1); } y = (HEIGHT - ymake.hdot[0]) / 2 + ymake.hdot[0]; for (i = 0; i < YarnImage->size.x; i += tab) { dx = i; DP = YarnImage->pointer(i, y); if (*DP == 1) dy = y - 1; else dy = y ; DP = YarnImage->pointer(i, dy); c = ((*DP - 2) / 7) * 7 + 2; lng = ((double)((HEIGHT - ymake.hdot[0]) / 2.0) * length/100) / (rand()%3+1) + 1; q = 4; col = 0; row = 1; for (j = 0; j < lng; j++) { dx -= col; dy += row; if (dx >= 0 && dx < YarnImage->size.x && dy >= 0 && dy < YarnImage->size.y) { DP = YarnImage->pointer(dx, dy); *DP = c + rand() % 7; } if (ch == 0) HairAxis(col, row, q); ch++ ; if (ch>1) ch = 0; } tab = 100.0 / density * (rand()%5 + 1); } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::DrawYarnImage(TPMemoryArea *YarnImage) { Byte *DP, *SP; bool fb = false, pb = false; int y; if (YarnImage->size.x > WIDTH) { fb = FullImage->Bitmap->Create(YarnImage->size.x, HEIGHT, 8, rgb); if (fb == false) goto fail; pb = PartImage->Bitmap->Create(YarnImage->size.x, HEIGHT, 8, rgb); if (pb == false) goto fail; ScrollBar->Enabled = true; ScrollBar->Max = YarnImage->size.x - WIDTH; } else { ScrollBar->Position = 0; ScrollBar->Enabled = false; } if (PartImage->Bitmap->StartScanLine() == false) goto fail; for (y = 0; y < HEIGHT; y++) { DP = PartImage->Bitmap->GetScanLine(y); memset(DP, 1, PartImage->Bitmap->Width); PartImage->Bitmap->PutScanLine(y); } for (y = 0; y < HEIGHT; y++) { DP = PartImage->Bitmap->GetScanLine(y); SP = YarnImage->pointer(0, y); memcpy(DP, SP, YarnImage->size.x); PartImage->Bitmap->PutScanLine(y); } PartImage->Bitmap->StopScanLine(); return; fail: if (fb) FullImage->Bitmap->Destroy(); if (fb) PartImage->Bitmap->Destroy(); PartImage->Bitmap->StopScanLine(); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- // FullView Rectangle void __fastcall TYarnDesignForm::WADraw() { HDC hDC = NULL; HBRUSH hOldBrush; HPEN hOldPen; int nDrawMode; hDC = FullImage->Bitmap->CreateDC(); if (hDC == NULL) goto fail; hOldBrush = SelectObject(hDC, GetStockObject(NULL_BRUSH)); hOldPen = SelectObject(hDC, GetStockObject(WHITE_PEN)); nDrawMode = GetROP2(hDC); SetROP2(hDC, R2_XORPEN); Rectangle(hDC, startx, 0, startx + 100, HEIGHT); SetROP2(hDC, nDrawMode); SelectObject(hDC, hOldBrush); SelectObject(hDC, hOldPen); FullImage->Bitmap->DeleteDC(hDC); return; fail: FullImage->Bitmap->DeleteDC(hDC); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::RectangleMove(int X) { if (IsDraw) WADraw(); startx = X + FullImage->PositionX - 50; if (startx < 0) startx = 0; if (startx > FullImage->Bitmap->Width - 100) startx = FullImage->Bitmap->Width - 100; WADraw(); IsDraw = true; PartImage->PositionX = startx; RulerZoom->Start = startx; FullImage->Repaint(); PartImage->Repaint(); } //--------------------------------------------------------------------------- // Repeat Image void __fastcall TYarnDesignForm::IndicateRectangle(int X, bool erase) { RepeatImage->Canvas->Brush->Color = clWhite; RepeatImage->Canvas->FillRect(Rect(0, 0, RepeatImage->Width, RepeatImage->Height)); if (!erase) { RepeatImage->Canvas->Brush->Color = clBlue; RepeatImage->Canvas->Pen->Color = clYellow; RepeatImage->Canvas->Rectangle(min(isx, X), 0, max(isx, X), RepeatImage->Height); } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::RepeatYarnImage8(TPMemoryArea *Temp) { Byte *TP, *DP, *UD; int x, y, dx; TTexpiaBitmap *tb = YarnUndo->Last->Bitmap; if (PartImage->Bitmap->StartScanLine() == false) goto fail; if (cbSuper->Checked) { int sy = (HEIGHT - FYarnData.Thick) / 2; int ey = sy + FYarnData.Thick - 1; if (tb->StartScanLine() == false) goto fail; for (y = 0; y < HEIGHT; y++) { DP = PartImage->Bitmap->GetScanLine(y); TP = Temp->pointer(0, y); if (y < sy || y > ey) { UD = tb->GetScanLine(y); for (x = 0, dx = 0; x < ymake.width; x++, dx++, DP++, UD++) { if (dx >= Temp->size.x) dx = 0; if (*UD > 1) *DP = *UD; else *DP = TP[dx]; } } else { for (x = 0, dx = 0; x < ymake.width; x++, dx++, DP++) { if (dx >= Temp->size.x) dx = 0; *DP = TP[dx]; } } PartImage->Bitmap->PutScanLine(y); } tb->StopScanLine(); } else { for (y = 0; y < HEIGHT; y++) { DP = PartImage->Bitmap->GetScanLine(y); TP = Temp->pointer(0, y); for (x = 0, dx = 0; x < ymake.width; x++, dx++, DP++) { if (dx >= Temp->size.x) dx = 0; *DP = TP[dx]; } PartImage->Bitmap->PutScanLine(y); } } PartImage->Bitmap->StopScanLine(); ImageRepaint(true); return; fail: tb->StopScanLine(); PartImage->Bitmap->StopScanLine(); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::RepeatYarnImage15(TPMemoryArea *Temp) { WORD *TP, *DP; int x, y, dx; if (PartImage->Bitmap->StartScanLine()) { for (y = 0; y < HEIGHT; y++) { DP = (WORD *)PartImage->Bitmap->GetScanLine(y); TP = Temp->pointer(0, y); for (x = 0, dx = 0; x < ymake.width; x++, dx++, DP++) { if (dx >= Temp->size.x) dx = 0; *DP = TP[dx]; } PartImage->Bitmap->PutScanLine(y); } PartImage->Bitmap->StopScanLine(); ImageRepaint(true); } else EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- // Draw Image void __fastcall TYarnDesignForm::DrawDot(int X, int Y) { Byte *DP; WORD *WDP; int sx, ex, sy, ey, i, j; RGBQUAD rgb; sx = X - ls; if (sx < 0) sx = 0; ex = X + rs; if (ex > PartImage->Bitmap->Width) ex = PartImage->Bitmap->Width; sy = Y - ls; if (sy < 0) sy = 0; ey = Y + rs; if (ey > PartImage->Bitmap->Height) ey = PartImage->Bitmap->Height; if (PartImage->Bitmap->StartScanLine()) { if (bpp == 8) { for (i = sy; i < ey; i++) { DP = (Byte *) PartImage->Bitmap->GetScanLine(i); for (j = sx; j < ex; j++) { if (pencolor > 1) DP[j] = pencolor + (rand() % 7); else DP[j] = 1; } PartImage->Bitmap->PutScanLine(i); } } else { for (i = sy; i < ey; i++) { WDP = (WORD *) PartImage->Bitmap->GetScanLine(i); for (j = sx; j < ex; j++) { static int i=0; int mapping[9]; mapping[0]=0;mapping[1]=1;mapping[2]=2;mapping[3]=5;mapping[4]=8; mapping[5]=7;mapping[6]=6;mapping[7]=3;mapping[8]=4; TColor2RGB(TColor(nearc[mapping[ ( (i++)+rand()%2 )%9 ]]), rgb.rgbRed, rgb.rgbGreen, rgb.rgbBlue); i++; if(i==9)i=0; WDP[j] = RGBToColor15(rgb); } PartImage->Bitmap->PutScanLine(i); } } PartImage->Bitmap->StopScanLine(); PartImage->Repaint(); } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::ImageRepaint(bool zeropos) { FullImage->Bitmap->Copy(PartImage->Bitmap, SRCCOPY); if (zeropos) { FullImage->PositionX = 0; PartImage->PositionX = 0; if (ScrollBar->Enabled) ScrollBar->Position = 0; startx = 0; } WADraw(); IsDraw = true; FullImage->Repaint(); PartImage->Repaint(); } //--------------------------------------------------------------------------- // File Method void __fastcall TYarnDesignForm::InitFile() { FilePanel->BringToFront(); YarnTableForm->Visible = true; YarnTableForm->BringToFront(); FItem = 0; sbRead->Down = true; sbSave->Enabled = bmake; sbSaveAs->Enabled = bmake; if(WorkArea->Mask) WorkArea->ResetRegion((TPItemImage*) PartImage); //qe »õ·Î¿î ÆÄÀÏ ºÎ¸£±âÀü¿¡ PartImage->Repaint(); //ÀÛ¾÷±¸¿ªÀÌ ÀâÇôÀÖÀ¸¸é ÇØÁ¦½ÃÅ´ StatusItem(0); sbWinClick(this); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::ExitFile() { FilePanel->SendToBack(); YarnTableForm->Visible = false; } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::YarnDataChange(AnsiString fn) { switch (FItem) { case 0: FileRead(fn); ExitFile(); break; case 1: FileSave(fn); break; case 2: FileDelete(fn); break; case 3: FileRename(fn); break; } FItem = 0; sbRead->Down = true; } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::DeleteFolder(AnsiString FolderName) { SHFILEOPSTRUCT shfile; char *ucFrom = NULL; if (FItem == 6) { memset(&shfile, 0, sizeof(SHFILEOPSTRUCT)); shfile.hwnd = Handle; shfile.wFunc = FO_DELETE; ucFrom = (char *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, FolderName.Length()+2); if (ucFrom) { strcpy(ucFrom, FolderName.c_str()); shfile.pFrom = ucFrom; shfile.fFlags = FOF_ALLOWUNDO ; SHFileOperation(&shfile); HeapFree(GetProcessHeap(), 0, ucFrom); } YarnTableForm->DirectoryListBox->Update(); } FItem = 0; sbRead->Down = true; } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::FileRead(AnsiString fn) { TPException ec = EC_NONE; if (!FileExists(fn)) { ec = EC_FILE_NOT_EXIST; goto fail; } fload = true; FYarnData.Remove(&FYarnData.Infor); if ((ec = FYarnData.LoadFromFile(fn)) != EC_NONE) goto fail; FYarnUnit = FYarnData.Unit; if (FLengthUnit == uCm) seLength->Value = 2.54 * FYarnData.Length / yDPI; else seLength->Value = FYarnData.Length / yDPI; yarncount = FYarnData.Infor.Detail.TYC; seYarnNumber->Value = yarncount; if (yarncount > 1) { for (int i = 0; i < yarncount; i++) { FTempYarnInfor[i]->Copy(&FYarnData.Infor.Infor[i]); } } else FTempYarnInfor[0]->Copy(&FYarnData.Infor); seCount->Value = FYarnData.Infor.Detail.Count; SZ = FYarnData.Infor.Detail.SZ; stTwist->Caption = (SZ) ? "Z Direction" : "S Direction"; seTPI->Value = FYarnData.Infor.Detail.TPI; cbHair->Checked = FYarnData.Infor.Detail.Hair; if (FYarnData.Dyed) { sbDyedYarn->Down = true; bpp = 16; InitImage(); DrawScanData(); bmake = true; InitFile(); } else { sbDyedYarn->Down = false; bpp = 8; InitImage(); CalculateWidthHeight(); Simulation(); } delete[] FYarnData.Map; FYarnData.Map = NULL; FYarnData.Width = HEIGHT; //////// I'll fix later Review(); fload = false; return; fail: if (ec == EC_NONE) ec = EC_FILE_NOT_READ; EXCEPTION_MESSAGE_OK(ec); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::DrawScanData() { int ssy, dsy, dey, dx, sx, dy, sy; WORD *DP, *SP; bool fb = false, pb = false; if (FYarnData.Width > HEIGHT) { dsy = 0; dey = HEIGHT; ssy = (FYarnData.Width - HEIGHT) / 2; } else { dsy = (HEIGHT - FYarnData.Width) / 2; dey = dsy + FYarnData.Width; ssy = 0; } if (FYarnData.Length > WIDTH) { fb = FullImage->Bitmap->Create(FYarnData.Length, HEIGHT, 16); if (fb == false) goto fail; pb = PartImage->Bitmap->Create(FYarnData.Length, HEIGHT, 16); if (pb == false) goto fail; } if (PartImage->Bitmap->StartScanLine() == false) goto fail; for (dy = dsy, sy = ssy; dy < dey; dy++, sy++) { DP = (WORD *) PartImage->Bitmap->GetScanLine(dy); SP = (WORD *) FYarnData.Map + (FYarnData.Length * sy); for(dx = 0; dx < FYarnData.Length; dx++, DP++, SP++) { if (!(*SP & 0x8000)) *DP = *SP; } PartImage->Bitmap->PutScanLine(dy); } PartImage->Bitmap->StopScanLine(); ImageRepaint(); return; fail: PartImage->Bitmap->StopScanLine(); if (fb) FullImage->Bitmap->Destroy(); if (pb) PartImage->Bitmap->Destroy(); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::YarnSave(AnsiString fn) { Byte *mp, *dp, fc, gr; WORD *wsp, *wdp; int sy, ey; int size = FYarnData.Width*FYarnData.Length; sy = (HEIGHT - FYarnData.Width) / 2; ey = sy + FYarnData.Width; if (FYarnData.Dyed) { FYarnData.Map = (WORD *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WORD)*size); if (FYarnData.Map == NULL) goto fail; wdp = (WORD *) FYarnData.Map; if (PartImage->Bitmap->StartScanLine() == false) goto fail; for (int y = sy; y < ey; y++) { wsp =(WORD *) PartImage->Bitmap->GetScanLine(y); for (int x = 0; x < FYarnData.Length; x++, wsp++, wdp++) { if (*wsp == 0x7FFF) *wdp = 0x8000; else *wdp = *wsp; } } PartImage->Bitmap->StopScanLine(); if (FYarnData.SaveToFile(fn) != EC_NONE) goto fail; HeapFree(GetProcessHeap(), 0, FYarnData.Map); FYarnData.Map = NULL; } else { FYarnData.Map = (Byte *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size); if (FYarnData.Map == NULL) goto fail; mp = (Byte *) FYarnData.Map; if (PartImage->Bitmap->StartScanLine() == false) goto fail; for (int y = sy; y < ey; y++) { dp = PartImage->Bitmap->GetScanLine(y); for (int x = 0; x < FYarnData.Length; x++, dp++, mp++) { if (*dp > 1) { fc = (*dp - 2) / 7; gr = *dp - ((fc * 7) + 2) + 1; *mp = (fc<<3) | gr; } else *mp = 0; } } PartImage->Bitmap->StopScanLine(); if (FYarnData.SaveToFile(fn) != EC_NONE) goto fail; HeapFree(GetProcessHeap(), 0, FYarnData.Map); FYarnData.Map = NULL; } YarnTableForm->FileListBox->Update(); return; fail: PartImage->Bitmap->StopScanLine(); if (FYarnData.Map) {HeapFree(GetProcessHeap(), 0, FYarnData.Map); FYarnData.Map = NULL;} EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::FileSave(AnsiString fn) { if (FileExists(fn)) YarnSave(fn); } //--------------------------------------------------------------------------- AnsiString __fastcall TYarnDesignForm::FileNameExt(AnsiString fn) { char *s = AnsiStrScan(fn.c_str(), '.'); if (s==NULL) return fn + ".yan"; else return fn; } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::FileDelete(AnsiString fn) { if (FileExists(fn)) { if (MessageDlg(IDS_MESSAGE_DELETEFILE+ fn, mtWarning, TMsgDlgButtons()<FileListBox->Update(); } } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::FileRename(AnsiString fn) { AnsiString s; if (FileExists(fn)) { s = fn; if (InputQuery(IDS_COMMON_RENAME, IDS_COMMON_INPUTNAME, s)) { s = FileNameExt(s); for (int i = 0; i < YarnTableForm->FileListBox->Items->Count; i++) { if (s == YarnTableForm->FileListBox->Items->Strings[i]) { EXCEPTION_MESSAGE_OK(EC_FILE_EXIST_ALREADY); return; } } RenameFile(fn, FileNameExt(s)); YarnTableForm->FileListBox->Update(); } } } //--------------------------------------------------------------------------- /////// Public Method ///////////////////////////////////////////////////// //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::UnitChange() { if (MainImageForm) { FLengthUnit = MainImageForm->CurrentUnit; if (FLengthUnit==uCm) { seLength->Value = seLength->Value * 2.54; } else { seLength->Value = seLength->Value / 2.54; } RulerZoom->AUnit = FLengthUnit; Ruler->AUnit = FLengthUnit; Review(); } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::DrawFancy(TPMemoryArea *YarnImage) { int sy, ey, y, x, yy, msy, mey; TPMemoryArea *Fancy = NULL; Byte *DP, *SP; sy = (HEIGHT - FYarnData.Thick) / 2; ey = sy + FYarnData.Thick; if (FYarnData.Map) { // file load and not dyed msy = (FYarnData.Width - FYarnData.Thick) / 2; mey = msy + FYarnData.Thick; // yy = sy - (msy - 1); yy = sy - msy; for (y = 0; y <= msy; y++) { SP = (Byte *) FYarnData.Map + y * FYarnData.Length; DP = YarnImage->pointer(0, yy + y); for (x = 0; x < YarnImage->size.x; x++, SP++, DP++) { if (*SP) *DP = ((*SP>>3) * 7 + 2) + (*SP&7 - 1); } } for (y = mey - 1, yy = ey - 1; y < FYarnData.Width; y++, yy++) { SP = (Byte *) FYarnData.Map + y * FYarnData.Length; DP = YarnImage->pointer(0, yy); for (x = 0; x < YarnImage->size.x; x++, SP++, DP++) { if (*SP) *DP = ((*SP>>3) * 7 + 2) + (*SP&7 - 1); } } } else { Fancy = new TPMemoryArea (FYarnData.Length, HEIGHT); if (Fancy == NULL) goto fail; if (Fancy->lock() == NULL) goto fail; if (PartImage->Bitmap->StartScanLine() == false) goto fail; for(y = 0; y < HEIGHT; y++) { SP = PartImage->Bitmap->GetScanLine(y); memcpy(Fancy->pointer(0, y), SP, Fancy->size.x); PartImage->Bitmap->PutScanLine(y); } PartImage->Bitmap->StopScanLine(); for (y = sy - 1, msy = presy - 1; y >= 0; y--, msy--) { if (y < 0 || msy < 0) break; memcpy(YarnImage->pointer(0, y), Fancy->pointer(0, msy), YarnImage->size.x); } for (y = ey, mey = preey; y < HEIGHT; y++, mey++) { if (y > HEIGHT-1 || mey > HEIGHT-1) break; memcpy(YarnImage->pointer(0, y), Fancy->pointer(0, mey), YarnImage->size.x); } Fancy->unlock(); delete Fancy; } return; fail: PartImage->Bitmap->StopScanLine(); if (Fancy) delete Fancy; EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::KillFancy() { if (!fload) cbFancy->Checked = false; } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::sbDyedYarnClick(TObject *Sender) { if(WorkArea->Mask) sbOffClick(this);//qe if(menuitem == TMove || menuitem == TCopy) ResetMoveCopyMode(); if (sbDyedYarn->Down) { bpp = 16; InitImage(); } else { bpp = 8; InitImage(); yarncount = 1; seYarnNumber->Value = 1; } bmake = false; cbSuper->Enabled = false; } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::InitImage() { int enable; bool fb = false, pb = false; if (bpp == 8) { enable = 1; fb = FullImage->Bitmap->Create(WIDTH, HEIGHT, 8, rgb); if (fb == false) goto fail; FullImage->Bitmap->FillRect(Rect(0, 0, WIDTH, HEIGHT), Palette->ColorData[1]->Color); pb = PartImage->Bitmap->Create(WIDTH, HEIGHT, 8, rgb); if (pb == false) goto fail; PartImage->Bitmap->FillRect(Rect(0, 0, WIDTH, HEIGHT), Palette->ColorData[1]->Color); for (int i = 1; i <= yarncount; i++) { ((TLabel *) FindComponent("lbYarn" + IntToStr(i)))->Enabled = true; ((TSpeedButton *) FindComponent("sbYarn" + IntToStr(i)))->Enabled = true; } Shape->Brush->Color = clWhite; pencolor = 1; } else { enable = 0; fb = FullImage->Bitmap->Create(WIDTH, HEIGHT, 16); if (fb == false) goto fail; FullImage->Bitmap->FillRect(Rect(0, 0, WIDTH, HEIGHT), clWhite); pb = PartImage->Bitmap->Create(WIDTH, HEIGHT, 16); if (pb == false) goto fail; PartImage->Bitmap->FillRect(Rect(0, 0, WIDTH, HEIGHT), clWhite); lbYarn1->Enabled = enable; lbYarn2->Enabled = enable; lbYarn3->Enabled = enable; lbYarn4->Enabled = enable; sbYarn1->Enabled = enable; sbYarn2->Enabled = enable; sbYarn3->Enabled = enable; sbYarn4->Enabled = enable; Shape->Brush->Color = clWhite; RGBQUAD q; q.rgbRed = 255; q.rgbGreen = 255; q.rgbBlue = 255; pencolor = RGBToColor15(q); for(int i=0;i<9;i++)nearc[i]=clWhite; } Label1->Enabled = enable; lbYarnUnit->Enabled = enable; sbYarnUnit->Enabled = enable; Label2->Enabled = enable; seLength->Enabled = enable; lbLengthUnit->Enabled = enable; Label3->Enabled = enable; seYarnNumber->Enabled = enable; Label9->Enabled = enable; lbCount->Enabled = enable; lbUnit->Enabled = enable; seCount->Enabled = enable; Label4->Enabled = enable; stTwist->Enabled = enable; Label5->Enabled = enable; seTPI->Enabled = enable; cbHair->Enabled = enable; Label7->Enabled = enable; Label8->Enabled = enable; seHairLength->Enabled = enable; seHairDensity->Enabled = enable; MakeButton->Enabled = enable; cbFancy->Enabled = enable; YarnUndo->RemoveAll(); return; fail: if (fb) FullImage->Bitmap->Destroy(); if (pb) PartImage->Bitmap->Destroy(); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::sbWindowClick(TObject *Sender) { if(menuitem == TMove || menuitem == TCopy) ResetMoveCopyMode(); if(sbWindow->Down){ menuitem = TWin; InitWin(); //WinPanel->BringToFront(); //sbRect->Down =true; }else{ sbWinClick(this); } } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::sbOffClick(TObject *Sender) { WorkArea->ResetRegion((TPItemImage*) PartImage); PartImage->Repaint(); StatusItem(0); sbWinClick(this); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::WindowMouseDown(int X, int Y) //added by qe { if (WinStep == 0) { WorkArea->ResetRegion((TPItemImage*)PartImage); First = Point(X, Y); IsDraw = false; WinStep = 1; } else { if (IsDraw) { WorkAreaDraw(); IsDraw = false; } Second = Point(X, Y); First.y = First.y - 1; Second.y = Second.y - 1; WorkArea->SetShapeRegion(PartImage->Bitmap, First, Second); StatusItem(1); WinStep = 0; } PartImage->Repaint(); } //---------------------------------------------------------------------------- void __fastcall TYarnDesignForm::WindowMouseMove(int X, int Y) { if (WinStep > 0) { PartImage->Repaint(); /* if(IsDraw){ WorkAreaDraw(); IsDraw = false; } */ Second = Point(X, Y); WorkAreaDraw(); IsDraw = true; } } //---------------------------------------------------------------------------- void __fastcall TYarnDesignForm::MoveCopyMouseDown(int X, int Y) { int dx, dy, x, y; RECT rc = WorkArea->Range; Byte *DP; PartImage->Bitmap->Copy(Bitmap, SRCCOPY); YarnUndo->Save(Rect(0, 0, PartImage->Bitmap->Width, HEIGHT)); if (menuitem == TMove) { if (PartImage->Bitmap->StartScanLine() == false) goto fail; for (y = rc.top; y < rc.bottom; y++) { DP = PartImage->Bitmap->GetScanLine(y); if(bpp == 8){memset(DP+rc.left, 1, rc.right - rc.left);} else if(bpp == 16) {memset(DP+rc.left*2, clWhite, (rc.right - rc.left)*2);} PartImage->Bitmap->PutScanLine(y); } PartImage->Bitmap->StopScanLine(); } dx = X - TempBitmap->Width / 2; dy = Y - TempBitmap->Height / 2; if (dx < 0) dx = 0; if (dx + TempBitmap->Width > PartImage->Bitmap->Width) dx = PartImage->Bitmap->Width - TempBitmap->Width; if (dy < 0) dy = 0; if (dy + TempBitmap->Height > PartImage->Bitmap->Height) dx = PartImage->Bitmap->Height - TempBitmap->Height; PartImage->Bitmap->CopyToRect(dx, dy, TempBitmap, SRCCOPY); if (menuitem == TMove) { sbMove->Down = false; menuitem = TOff; WorkArea->ResetRegion((TPItemImage*) PartImage); WorkArea->SetShapeRegion(PartImage->Bitmap, Point(dx, dy), Point(dx+TempBitmap->Width - 1, dy+TempBitmap->Height - 1)); } else { Bitmap->Copy(PartImage->Bitmap, SRCCOPY); } PartImage->Repaint(); ImageRepaint(false); return; fail: PartImage->Bitmap->StopScanLine(); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::MoveCopyMouseMove(int X, int Y) { int dx, dy; PartImage->Bitmap->Copy(Bitmap, SRCCOPY); dx = X - TempBitmap->Width / 2; dy = Y - TempBitmap->Height / 2; if (dx < 0) dx = 0; if (dx + TempBitmap->Width > PartImage->Bitmap->Width) dx = PartImage->Bitmap->Width - TempBitmap->Width; if (dy < 0) dy = 0; if (dy + TempBitmap->Height > PartImage->Bitmap->Height) dy = PartImage->Bitmap->Height - TempBitmap->Height; PartImage->Bitmap->CopyToRect(dx, dy, TempBitmap,SRCCOPY); PartImage->Repaint(); move = true; } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::WorkAreaDraw() { HDC hDC = NULL; HBRUSH hOldBrush; HPEN hOldPen, hPen = NULL; int nDrawMode, fx, fy, sx, sy; COLORREF clOldBk; hDC=PartImage->Canvas->Handle; clOldBk = SetBkColor(hDC, clWhite); hPen = CreatePen(PS_DOT, 1, clBlack); hOldPen = SelectObject(hDC, hPen); hOldBrush = SelectObject(hDC, GetStockObject(NULL_BRUSH)); fx = (First.x -PartImage->PositionX)*8; fy = (First.y)*8; sx = (Second.x -PartImage->PositionX)*8; sy = (Second.y )*8; Rectangle(hDC,min(fx, sx), min(fy, sy)-8, max(fx,sx)+8, max(fy, sy)); SetBkColor(hDC, clOldBk); SelectObject(hDC, hOldPen); DeleteObject(hPen); SelectObject(hDC, hOldBrush); // PartImage->Canvas->DeleteDC(hDC); return; fail: //if (hDC) PartImage->Canvas->DeleteDC(hDC); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::StatusItem(int i) { sbMove->Enabled = i; sbCopy->Enabled = i; sbOff->Enabled = i; } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::Timer1Timer(TObject *Sender) { PartImage->OutlineBitmapRgn(); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::sbWinClick(TObject *Sender) { sbWindow->Down = false; menuitem = TOff; WinStep = 0; WinPanel->SendToBack(); PartImage->Repaint(); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::InitWin() { WinPanel->BringToFront(); sbRect->Down =true; } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::sbMoveCopyClick(TObject *Sender) { bool tp = false, bt = false; TSpeedButton *sp = (TSpeedButton *) Sender; if(menuitem == TMove || menuitem == TCopy) ResetMoveCopyMode(); if(sp->Down){ sbWinClick(this); if(sp == sbMove){ menuitem = TMove; } else if (sp == sbCopy) { menuitem = TCopy; } RECT rc = WorkArea->Range; if(bpp == 8){ tp = TempBitmap->Create(rc.right - rc.left, rc.bottom - rc.top, 8, rgb); if (tp == false) goto fail; TempBitmap->CopyFromRect(PartImage->Bitmap, rc.left, rc.top, SRCCOPY); bt = Bitmap->Create(PartImage->Bitmap->Width, PartImage->Bitmap->Height, 8, rgb); if (bt == false) goto fail; Bitmap->Copy(PartImage->Bitmap, SRCCOPY); }else if(bpp == 16){ tp = TempBitmap->Create(rc.right - rc.left, rc.bottom - rc.top, 16); if (tp == false) goto fail; TempBitmap->CopyFromRect(PartImage->Bitmap, rc.left, rc.top, SRCCOPY); bt = Bitmap->Create(PartImage->Bitmap->Width, PartImage->Bitmap->Height, 16); if (bt == false) goto fail; Bitmap->Copy(PartImage->Bitmap, SRCCOPY); } }else { menuitem = TOff; } return; fail: if (bt) Bitmap->Destroy(); if (tp) TempBitmap->Destroy(); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TYarnDesignForm::ResetMoveCopyMode() { PartImage->Bitmap->Copy(Bitmap, SRCCOPY); PartImage->Repaint(); sbCopy->Down = false; sbMove->Down = false; menuitem = TOff; } //---------------------------------------------------------------------------