//--------------------------------------------------------------------------- #include #include "MainImage.h" #pragma hdrstop #include #include "Common.h" #include "Exception.h" #include "CarpetTable_F.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TCarpetTableForm *CarpetTableForm; #define IDS_FCAPTION StringTable[0] #define IDS_FTEXT StringTable[1] //--------------------------------------------------------------------------- // CTEXTURE struct //--------------------------------------------------------------------------- AnsiString __fastcall CTEXTURE::GetName() { int size = strcspn(name, "."); return AnsiString(name, size); } //--------------------------------------------------------------------------- void __fastcall CTEXTURE::Draw(Graphics::TBitmap *bmp) { int xx, yy, l, i, j, gy, gx, x, y, rx, ry, px, py; int w, h; Byte *IP, *DP; w = bmp->Width; h = bmp->Height; ry = h / ly; rx = w / lx; if (ry < rx) l = ry; else l = rx; if (l > 10) l = 10; else if (l > 5) l = 5; else if (l == 0) l = 1; py = h / l + 1; px = w / l + 1; for (i = 0; i < py; i++) { gy = h - i / ly; for (j = 0; j < px; j++) { gx = j / lx; x = j % lx; y = i % ly; IP = (Byte *) map + (lx* y + x); for (yy = 0; yy < l; yy++) { y = gy - i * l - yy; if (0 <= y && y < h) { DP = (Byte *) bmp->ScanLine[y]; for (xx = 0; xx < l; xx++) { x = gx + j * l + xx; if (0 <= x && x < w) { DP[x] = *IP; } } } } } } bmp->Canvas->Pen->Color = clRed; for (i = j = 0; i < h; i+=l, j++) { if (j % ly == 0) { bmp->Canvas->MoveTo(0, h - i + 1); bmp->Canvas->LineTo(w, h - i + 1); i++; } } for (i = j = 0; i < w; i+=l, j++) { if (j % lx == 0) { bmp->Canvas->MoveTo(i - 1, h); bmp->Canvas->LineTo(i - 1, 0); i++; } } } //--------------------------------------------------------------------------- // CarpetTableForm //--------------------------------------------------------------------------- __fastcall TCarpetTableForm::TCarpetTableForm(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TCarpetTableForm::FormDestroy(TObject *Sender) { CTEXTURE *ctf; while (CTexList->Count > 0) { ctf = (CTEXTURE *) CTexList->Last(); CTexList->Remove(ctf); delete[] ctf->map; delete ctf; } delete CTexList; CTexList = NULL; delete Bitmap; Bitmap = NULL; } //--------------------------------------------------------------------------- void __fastcall TCarpetTableForm::CarpetGridDrawCell(TObject *Sender, int Col, int Row, TRect &rect, TGridDrawState State) { int x, y; TPoint sp; TSize vs = { 116, 116 }; int n = Row * CarpetGrid->ColCount+Col; if (n < CTexList->Count) { sp.x = rect.Left + 2; sp.y = rect.Top + 2; CTEXTURE *ct = (CTEXTURE *) CTexList->Items[n]; ct->Draw(Bitmap); CarpetGrid->Canvas->Draw(sp.x, sp.y, Bitmap); AnsiString str = ct->GetName(); TRect TextRect = Rect(rect.Left, rect.Top + (vs.cy + 4), rect.Right, rect.Bottom); x = ((vs.cx + 4) - CarpetGrid->Canvas->TextWidth(str)) / 2; y = (30 - CarpetGrid->Canvas->TextHeight(str)) / 2; CarpetGrid->Canvas->TextRect(TextRect, TextRect.Left + x, TextRect.Top + y, str); } } //--------------------------------------------------------------------------- void __fastcall TCarpetTableForm::CarpetGridDblClick(TObject *Sender) { int n = CarpetGrid->Row * CarpetGrid->ColCount+CarpetGrid->Col; if (CTexList == NULL) return; if (n < CTexList->Count) { CTEXTURE *ct = (CTEXTURE *) CTexList->Items[n]; if (sbRead->Down) { if (FOnAccept) FOnAccept(ct); } else if (sbSave->Down) { if (SaveCheck()) { delete[] ct->map; MakeMap(ct); SaveTexture(ct); CarpetGrid->Repaint(); } } else if (sbDelete->Down) { CTexList->Remove(ct); delete[] ct->map; delete ct; AnsiString fn = DirectoryTexture + "\\carpet\\" + ct->GetName() + ".ctx"; DeleteFile(fn); CarpetGrid->Repaint(); } sbRead->Down = true; } } //--------------------------------------------------------------------------- void __fastcall TCarpetTableForm::sbSaveAsClick(TObject *Sender) { AnsiString fn = ""; CTEXTURE *ct=NULL; if (SaveCheck()) { if (InputQuery("Save As", "Input the new file name.", fn)) { fn = PasteExt(fn); ct = new CTEXTURE; if (!ct) goto fail; strcpy(ct->name, fn.c_str()); MakeMap(ct); CTexList->Add(ct); SaveTexture(ct); CarpetGrid->Repaint(); } } return; fail : EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); return; } //--------------------------------------------------------------------------- // Private Method //--------------------------------------------------------------------------- void __fastcall TCarpetTableForm::InitTexture() { TSearchRec sr; int status; status = FindFirst(DirectoryTexture + "\\Carpet\\*.ctx", faReadOnly | faArchive, sr); while(status == 0) { if (sr.Attr & (faReadOnly | faArchive)) { ReadTexture(sr.Name); } status = FindNext(sr); } FindClose(sr); } //--------------------------------------------------------------------------- bool __fastcall TCarpetTableForm::ReadTexture(AnsiString fn) { int size; AnsiString FileName; CTEXTURE *ctf = NULL; // int ec; HANDLE hFile; DWORD dwRead; TPException ec = EC_NONE; FileName = DirectoryTexture + "\\carpet\\" + fn; if ((hFile = CreateFile(FileName.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_OPEN; goto fail; } ctf = new CTEXTURE; if (!ctf) {ec = EC_MEMORY_LACK; goto fail;} strcpy(ctf->name, fn.c_str()); if (SetFilePointer(hFile, 4, NULL, FILE_CURRENT) == 0xFFFFFFFF) goto fail; if (!ReadFile(hFile, &ctf->lx, sizeof(short), &dwRead, NULL)) goto fail; if (!ReadFile(hFile, &ctf->ly, sizeof(short), &dwRead, NULL)) goto fail; size = ctf->lx * ctf->ly; ctf->map = new Byte[size]; if (!ctf->map) {ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(hFile, ctf->map, size * sizeof(Byte), &dwRead, NULL)) goto fail; CTexList->Add(ctf); CloseHandle(hFile); return true; fail: if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile); if (ctf) delete ctf; if (ec == EC_NONE) ec = EC_FILE_NOT_READ; EXCEPTION_MESSAGE_OK(ec); return false; } //--------------------------------------------------------------------------- bool __fastcall TCarpetTableForm::SaveTexture(CTEXTURE *ctf) { HANDLE hFile = INVALID_HANDLE_VALUE; DWORD dwWrite; int size; AnsiString fn = ctf->GetName() + ".ctx"; TTexVersion ver = { 'P', 'T', 100 }; TPException ec = EC_NONE; fn = DirectoryTexture + "\\carpet\\" + fn; if ((hFile = CreateFile(fn.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } if (!WriteFile(hFile, &ver, sizeof (TTexVersion), &dwWrite, NULL)) goto fail; if (!WriteFile(hFile, &ctf->lx, sizeof(short), &dwWrite, NULL)) goto fail; if (!WriteFile(hFile, &ctf->ly, sizeof(short), &dwWrite, NULL)) goto fail; size = ctf->lx * ctf->ly; if (!WriteFile(hFile, ctf->map, size * sizeof(Byte), &dwWrite, NULL)) goto fail; CloseHandle(hFile); return true; fail: if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile); if (ec == EC_NONE) ec = EC_FILE_NOT_WRITE; EXCEPTION_MESSAGE_OK(ec); return false; } //--------------------------------------------------------------------------- void __fastcall TCarpetTableForm::MakeMap(CTEXTURE *ct) { RECT r; Byte *IP, *DP; TTexpiaBitmap *Bmp; int x, y, size; Bmp = MainImageForm->iMainImage->Bitmap; r = MainImageForm->WorkArea->Range; ct->lx = r.right - r.left; ct->ly = r.bottom - r.top; size = ct->lx * ct->ly; ct->map = new Byte[size]; if (!ct->map) goto fail; if (!Bmp->StartScanLine()) goto fail; for (y = 0; y < ct->ly; y++) { IP = Bmp->GetScanLine(r.top + y); DP = ct->map + y * ct->lx; for (x = 0; x < ct->lx; x++, DP++) { *DP = IP[x + r.left]; } } Bmp->StopScanLine(); return; fail : if (ct->map) delete ct->map; Bmp->StopScanLine(); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); return; } //--------------------------------------------------------------------------- bool __fastcall TCarpetTableForm::SaveCheck() { Byte *IP; RECT r; int w, h, x, y; TTexpiaBitmap *Bmp = MainImageForm->iMainImage->Bitmap; if (MainImageForm->WorkArea->Mask == NULL) return false; r = MainImageForm->WorkArea->Range; w = r.right - r.left; h = r.bottom - r.top; if (w <= 0 || w >= 11 || h <= 0 || h == 0) return false; if (!Bmp->StartScanLine()) goto fail; for (y = r.top; y < r.bottom; y++) { IP = Bmp->GetScanLine(y); for (x = r.left; x < r.right; x++) { // user choice is 1 - 6 but, pal num is 2 - 7 if (IP[x] <= 1 || IP[x] > 8) return false; } } Bmp->StopScanLine(); return true; fail : Bmp->StopScanLine(); return false; } //--------------------------------------------------------------------------- AnsiString __fastcall TCarpetTableForm::PasteExt(AnsiString fn) { char *s; s = AnsiStrScan(fn.c_str(), '.'); if (s==NULL) fn = fn + ".ctx"; return fn; } //--------------------------------------------------------------------------- // Public Method //--------------------------------------------------------------------------- void __fastcall TCarpetTableForm::InitForm(RGBQUAD *rgb) { CTexList = new TList; if (!CTexList) goto fail; InitTexture(); Bitmap = new Graphics::TBitmap; if (!Bitmap) goto fail; Bitmap->PixelFormat = pf8bit; Bitmap->Width = CarpetGrid->DefaultColWidth - 4; Bitmap->Height = CarpetGrid->DefaultRowHeight - 34; SetDIBColorTable(Bitmap->Canvas->Handle, 0, 256, rgb); return; fail : if (CTexList) delete CTexList; EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); return; } //--------------------------------------------------------------------------- bool __fastcall TCarpetTableForm::CompareData(AnsiString name, CTEXTURE *ct) { CTEXTURE *st; int i, size; for (i = 0; i < CTexList->Count; i++) { st = (CTEXTURE *) CTexList->Items[i]; if (name == st->GetName()) { strcpy(ct->name, st->name); ct->lx = st->lx; ct->ly = st->ly; size = ct->lx * ct->ly; ct->map = new Byte[size]; if (!ct->map) goto fail; memcpy(ct->map, st->map, size); return true; } } return false; fail : EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); return false; } //--------------------------------------------------------------------------- void __fastcall TCarpetTableForm::ReDrawGrid(RGBQUAD *rgbquad) { if (Bitmap) { SetDIBColorTable(Bitmap->Canvas->Handle, 0, 256, rgbquad); CarpetGrid->Repaint(); } } //--------------------------------------------------------------------------- void __fastcall TCarpetTableForm::FormCreate(TObject *Sender) { TIniFile *IniFile; //=================================================================== StringTable.Create(DirectoryBin+"\\Carpet", Language, "CarpetTable"); SetSmallFont(Font); Caption = IDS_FCAPTION; sbRead->Caption = IDS_COMMON_READ; sbSave->Caption = IDS_COMMON_SAVE; sbSaveAs->Caption = IDS_COMMON_SAVEAS; sbDelete->Caption = IDS_COMMON_DELETE; StaticText1->Caption = IDS_FTEXT; //=================================================================== } //---------------------------------------------------------------------------