# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

View.cpp

Go to the documentation of this file.
00001 // View.cpp : implementation file
00002 //
00003 
00004 #include "stdafx.h"
00005 #include "resource.h"
00006 #include "View.h"
00007 #include "SelectionTerritoire.h"
00008 #include "select_rotation.h"
00009 #include "Browse.h"
00010 #include "popup.h"
00011 #include <direct.h>
00012 //#include "ListGroup.h"
00013 #include <nel/3d/tile_bank.h>
00014 //#include "ViewPopup.h"
00015 #include "pic\readpic.h"
00016 
00017 /*#ifdef _DEBUG
00018 #define new DEBUG_NEW
00019 #undef THIS_FILE
00020 static char THIS_FILE[] = __FILE__;
00021 #endif*/
00022 
00023 using namespace NL3D;
00024 
00025 extern CTileBank tileBank2;
00026 
00027 BEGIN_MESSAGE_MAP(CTView, CStatic)
00028         //{{AFX_MSG_MAP(CTView)
00029         ON_WM_DROPFILES()
00030         ON_WM_PAINT()
00031         ON_WM_LBUTTONDBLCLK()
00032         ON_WM_LBUTTONDOWN()
00033         ON_WM_RBUTTONDOWN()
00034         //}}AFX_MSG_MAP
00035 END_MESSAGE_MAP()
00036 
00037 CFont *normal_font = NULL;
00038 //int SortTile = 0; int sortMode = 1; //or, and
00039 __int64 flagGroupSort = 0; int showNULL = 0;
00040 //ViewPopup *theViewPopup = 0;
00041 
00042 bool zouille ()
00043 {
00044         return (GetAsyncKeyState(VK_F2)&(1<<15)) != 0;
00045 };
00046 
00047 // Rotate a buffer
00048 void rotateBuffer (uint &Width, uint &Height, std::vector<NLMISC::CBGRA>& Tampon)
00049 {
00050         // Make a copy
00051         std::vector<NLMISC::CBGRA> copy=Tampon;
00052 
00053         // Rotate
00054         for (uint y=0; y<Width; y++)
00055         {
00056                 // Line offset
00057                 uint tmp=y*Width;
00058                 for (uint x=0; x<Width; x++)
00059                 {
00060                         Tampon[y+(Width-x-1)*Height]=copy[x+tmp];
00061                 }
00062         }
00063 
00064         // New size
00065         uint tmp=Width;
00066         Width=Height;
00067         Height=tmp;
00068 }
00069 
00071 // CTView
00072 //Attention : windows veut que le buffer image commence du bas vers le haut
00073 int _LoadBitmap(const std::string& path,LPBITMAPINFO BitmapInfo, std::vector<NLMISC::CBGRA>&Tampon, std::vector<NLMISC::CBGRA>* Alpha, int rot)
00074 {
00075         //vector<NLMISC::CBGRA> Tampon;
00076         uint Width;
00077         uint Height;
00078         if (PIC_LoadPic(path, Tampon, Width, Height))
00079         {
00080                 BitmapInfo->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
00081                 BitmapInfo->bmiHeader.biWidth=Width;
00082                 BitmapInfo->bmiHeader.biHeight=-(int)Height;
00083                 BitmapInfo->bmiHeader.biPlanes=1;
00084                 BitmapInfo->bmiHeader.biBitCount=32;
00085                 BitmapInfo->bmiHeader.biCompression=BI_RGB;
00086                 BitmapInfo->bmiHeader.biSizeImage=0;
00087                 BitmapInfo->bmiHeader.biXPelsPerMeter=1;
00088                 BitmapInfo->bmiHeader.biYPelsPerMeter=1;
00089                 BitmapInfo->bmiHeader.biClrUsed=0;
00090                 BitmapInfo->bmiHeader.biClrImportant=0;
00091                 BitmapInfo->bmiColors->rgbBlue=0;
00092                 BitmapInfo->bmiColors->rgbRed=0;
00093                 BitmapInfo->bmiColors->rgbGreen=0;
00094 
00095                 while (rot)
00096                 {
00097                         // Rotate the buffer
00098                         rotateBuffer (Width, Height, Tampon);
00099                         rot--;
00100                 }
00101 
00102                 if ((Alpha)&&(Alpha->size()==Tampon.size()))
00103                 {
00104                         // Pre mul RGB componates by Alpha one
00105                         int nPixelCount=(int)(Width*Height);
00106                         for (int p=0; p<nPixelCount; p++)
00107                         {
00108                                 // Invert alpha ?
00109                                 int alpha=(*Alpha)[p].A;
00110                                 Tampon[p].R=(uint8)(((int)Tampon[p].R*alpha)>>8);
00111                                 Tampon[p].G=(uint8)(((int)Tampon[p].G*alpha)>>8);
00112                                 Tampon[p].B=(uint8)(((int)Tampon[p].B*alpha)>>8);
00113                         }
00114                 }
00115 
00116                 return 1;
00117         }
00118         else
00119                 return 0;
00120 }
00121 
00122 
00123 
00124 
00125 
00126 
00127 //TileInfo
00128 TileInfo::TileInfo()
00129 {
00130         nightLoaded = 0;
00131         alphaLoaded = 0;
00132         loaded = 0;
00133         Selected = 0; 
00134 }
00135 
00136 
00137 
00138 //TileList
00139 TileList::TileList()
00140 {
00141         last_id = 0; 
00142         _tileSet = -1;
00143 
00144         // Add 48 transitions
00145         int i;
00146         for (i=0; i<CTileSet::count; i++)
00147         {
00148                 TileInfo info;
00149                 info.id = i;
00150         
00151                 theListTransition.push_back (info);
00152         }
00153 
00154         // Add 16 displacements
00155         for (i=0; i<CTileSet::CountDisplace; i++)
00156         {
00157                 TileInfo info;
00158                 info.id = i;
00159         
00160                 theListDisplacement.push_back (info);
00161         }
00162 }
00163 
00164 
00165 int TileList::addTile128 ()
00166 {
00167 
00168         int index;
00169         tileBank2.getTileSet (_tileSet)->addTile128 (index, tileBank2);
00170         nlassert (index==(sint)theList128.size());
00171 
00172         TileInfo info;
00173         info.id = index;
00174         theList128.push_back (info);
00175 
00176         return index;
00177 }
00178 
00179 int TileList::addTile256 ()
00180 {
00181         int index;
00182         tileBank2.getTileSet (_tileSet)->addTile256 (index, tileBank2);
00183         nlassert (index==(sint)theList256.size());
00184 
00185         TileInfo info;
00186         info.id = index;
00187         theList256.push_back (info);
00188 
00189         return index;
00190 }
00191 
00192 bool RemovePath (std::string& path, const char* absolutePathToRemplace);
00193 
00194 int TileList::setTile128 (int tile, const std::string& name, NL3D::CTile::TBitmap type)
00195 {
00196         // Remove the absolute path from the path name
00197         std::string troncated=name;
00198         if (RemovePath (troncated, tileBank2.getAbsPath ().c_str()))
00199         {
00200                 vector<NLMISC::CBGRA> tampon;
00201                 uint Width;
00202                 uint Height;
00203                 if (!PIC_LoadPic(tileBank2.getAbsPath ()+troncated, tampon, Width, Height))
00204                 {
00205                         MessageBox (NULL, (tileBank2.getAbsPath ()+troncated).c_str(), "Can't load bitmap.", MB_OK|MB_ICONEXCLAMATION);
00206                         return 0;
00207                 }
00208                 else
00209                 {
00210                         CTileBorder border;
00211                         border.set (Width, Height, tampon);
00212 
00213                         CTileSet::TError error;
00214                         int pixel=-1;
00215                         int composante=4;
00216                         error=tileBank2.getTileSet(_tileSet)->checkTile128 (type, border, pixel, composante);
00217                         if ((error!=CTileSet::ok)&&(error!=CTileSet::addFirstA128128)&&!zouille ())
00218                         {
00219                                 char sTmp[512];
00220                                 static const char* comp[]={"Red", "Green", "Blue", "Alpha", ""};
00221                                 sprintf (sTmp, "%s\nPixel: %d (%s)", CTileSet::getErrorMessage (error), pixel, comp[composante]);
00222                                 MessageBox (NULL, sTmp, "Can't add tile", MB_OK|MB_ICONEXCLAMATION);
00223                                 return 0;
00224                         }
00225                         else
00226                         {
00227                                 if (error==CTileSet::addFirstA128128)
00228                                         tileBank2.getTileSet(_tileSet)->setBorder (type, border);
00229 
00230                                 tileBank2.getTileSet(_tileSet)->setTile128 (tile, troncated, type, tileBank2);
00231                                 switch (type)
00232                                 {
00233                                 case CTile::diffuse:
00234                                         theList128[tile].loaded=0;
00235                                         break;
00236                                 case CTile::additive:
00237                                         theList128[tile].nightLoaded=0;
00238                                         break;
00239                                 case CTile::alpha:
00240                                         theList128[tile].alphaLoaded=0;
00241                                         break;
00242                                 }
00243                                 theList128[tile].Load (tileBank2.getTileSet(_tileSet)->getTile128(tile), NULL);
00244                         }
00245                 }
00246         }
00247         else
00248         {
00249                 // Error: bitmap not in the absolute path..
00250                 char msg[512];
00251                 sprintf (msg, "The bitmap %s is not in the absolute path %s.", name.c_str(), tileBank2.getAbsPath ().c_str());
00252                 MessageBox (NULL, msg, "Load error", MB_OK|MB_ICONEXCLAMATION);
00253         }
00254 
00255         return 1;
00256 }
00257 
00258 int TileList::setTile256 (int tile, const std::string& name, NL3D::CTile::TBitmap type)
00259 {
00260         // Remove the absolute path from the path name
00261         std::string troncated=name;
00262         if (RemovePath (troncated, tileBank2.getAbsPath ().c_str()))
00263         {
00264                 vector<NLMISC::CBGRA> tampon;
00265                 uint Width;
00266                 uint Height;
00267                 if (!PIC_LoadPic(tileBank2.getAbsPath ()+troncated, tampon, Width, Height))
00268                 {
00269                         MessageBox (NULL, (tileBank2.getAbsPath ()+troncated).c_str(), "Can't load bitmap.", MB_OK|MB_ICONEXCLAMATION);
00270                         return 0;
00271                 }
00272                 else
00273                 {
00274                         CTileBorder border;
00275                         border.set (Width, Height, tampon);
00276                         
00277                         CTileSet::TError error;
00278                         int pixel=-1;
00279                         int composante=4;
00280 
00281                         if (((error=tileBank2.getTileSet(_tileSet)->checkTile256 (type, border, pixel, composante))!=CTileSet::ok)&&!zouille())
00282                         {
00283                                 char sTmp[512];
00284                                 static const char* comp[]={"Red", "Green", "Blue", "Alpha", ""};
00285                                 sprintf (sTmp, "%s\nPixel: %d (%s)", CTileSet::getErrorMessage (error), pixel, comp[composante]);
00286                                 MessageBox (NULL, sTmp, "Can't add tile", MB_OK|MB_ICONEXCLAMATION);
00287                                 return 0;
00288                         }
00289                         else
00290                         {
00291                                 tileBank2.getTileSet(_tileSet)->setTile256 (tile, troncated, type, tileBank2);
00292                                                 switch (type)
00293                                 {
00294                                 case CTile::diffuse:
00295                                         theList256[tile].loaded=0;
00296                                         break;
00297                                 case CTile::additive:
00298                                         theList256[tile].nightLoaded=0;
00299                                         break;
00300                                 case CTile::alpha:
00301                                         theList256[tile].alphaLoaded=0;
00302                                         break;
00303                                 }
00304                                 theList256[tile].Load (tileBank2.getTileSet(_tileSet)->getTile256(tile), NULL);
00305                         }
00306                 }
00307         }
00308         else
00309         {
00310                 // Error: bitmap not in the absolute path..
00311                 char msg[512];
00312                 sprintf (msg, "The bitmap %s is not in the absolute path %s.", name.c_str(), tileBank2.getAbsPath ().c_str());
00313                 MessageBox (NULL, msg, "Load error", MB_OK|MB_ICONEXCLAMATION);
00314         }
00315 
00316         return 1;
00317 }
00318 
00319 int TileList::setTileTransition (int tile, const std::string& name, NL3D::CTile::TBitmap type)
00320 {
00321         // Remove the absolute path from the path name
00322         std::string troncated=name;
00323         if (RemovePath (troncated, tileBank2.getAbsPath ().c_str()))
00324         {
00325                 // No alpha, use setTileTransitionAlpha
00326                 nlassert (CTile::alpha!=type);
00327 
00328                 vector<NLMISC::CBGRA> tampon;
00329                 uint Width;
00330                 uint Height;
00331                 if (!PIC_LoadPic(tileBank2.getAbsPath ()+troncated, tampon, Width, Height))
00332                 {
00333                         MessageBox (NULL, (tileBank2.getAbsPath ()+troncated).c_str(), "Can't load bitmap.", MB_OK|MB_ICONEXCLAMATION);
00334                         return 0;
00335                 }
00336                 else
00337                 {
00338                         CTileBorder border;
00339                         border.set (Width, Height, tampon);
00340                         
00341                         CTileSet::TError error;
00342                         int pixel=-1;
00343                         int composante=4;
00344                         error=tileBank2.getTileSet(_tileSet)->checkTile128 (type, border, pixel, composante);
00345                         if ((error!=CTileSet::ok)&&(error!=CTileSet::addFirstA128128)&&!zouille ())
00346                         {
00347                                 char sTmp[512];
00348                                 static const char* comp[]={"Red", "Green", "Blue", "Alpha", ""};
00349                                 sprintf (sTmp, "%s\nPixel: %d (%s)", CTileSet::getErrorMessage (error), pixel, comp[composante]);
00350                                 MessageBox (NULL, sTmp, "Can't add tile", MB_OK|MB_ICONEXCLAMATION);
00351                                 return 0;
00352                         }
00353                         else
00354                         {
00355                                 if (error==CTileSet::addFirstA128128)
00356                                         tileBank2.getTileSet(_tileSet)->setBorder (type, border);
00357                                 tileBank2.getTileSet(_tileSet)->setTileTransition ((CTileSet::TTransition)tile, troncated, type, tileBank2, border);
00358                                 switch (type)
00359                                 {
00360                                 case CTile::diffuse:
00361                                         theListTransition[tile].loaded=0;
00362                                         break;
00363                                 case CTile::additive:
00364                                         theListTransition[tile].nightLoaded=0;
00365                                         break;
00366                                 case CTile::alpha:
00367                                         theListTransition[tile].alphaLoaded=0;
00368                                         break;
00369                                 }
00370                                 theListTransition[tile].Load (tileBank2.getTileSet(_tileSet)->getTransition(tile)->getTile(), 
00371                                         &theListTransition[tile].alphaBits);
00372                         }
00373                 }
00374         }
00375         else
00376         {
00377                 // Error: bitmap not in the absolute path..
00378                 char msg[512];
00379                 sprintf (msg, "The bitmap %s is not in the absolute path %s.", name.c_str(), tileBank2.getAbsPath ().c_str());
00380                 MessageBox (NULL, msg, "Load error", MB_OK|MB_ICONEXCLAMATION);
00381         }
00382 
00383         return 1;
00384 }
00385 
00386 int TileList::setDisplacement (int tile, const std::string& name)
00387 {
00388         // Remove the absolute path from the path name
00389         std::string troncated=name;
00390         if (RemovePath (troncated, tileBank2.getAbsPath ().c_str()))
00391         {
00392                 // change the file name of the displacement map
00393                 tileBank2.getTileSet(_tileSet)->setDisplacement ((CTileSet::TDisplacement)tile, troncated);
00394 
00395                 // not loaded
00396                 theListDisplacement[tile].loaded=0;
00397 
00398                 // load it
00399                 if (name!="")
00400                 {
00401                         if (!_LoadBitmap(tileBank2.getAbsPath() + name, &theListDisplacement[tile].BmpInfo, theListDisplacement[tile].Bits, NULL, 0))
00402                                 MessageBox (NULL, (tileBank2.getAbsPath() + name).c_str(), "Can't load file", MB_OK|MB_ICONEXCLAMATION);
00403                         else
00404                                 theListDisplacement[tile].loaded=1;
00405                 }
00406         }
00407         else
00408         {
00409                 // Error: bitmap not in the absolute path..
00410                 char msg[512];
00411                 sprintf (msg, "The bitmap %s is not in the absolute path %s.", name.c_str(), tileBank2.getAbsPath ().c_str());
00412                 MessageBox (NULL, msg, "Load error", MB_OK|MB_ICONEXCLAMATION);
00413         }
00414 
00415         return 1;
00416 }
00417 
00418 int TileList::setTileTransitionAlpha (int tile, const std::string& name, int rot)
00419 {
00420         // Remove the absolute path from the path name
00421         std::string troncated=name;
00422         if (RemovePath (troncated, tileBank2.getAbsPath ().c_str()))
00423         {
00424                 vector<NLMISC::CBGRA> tampon;
00425                 uint Width;
00426                 uint Height;
00427                 if (!PIC_LoadPic(tileBank2.getAbsPath ()+troncated, tampon, Width, Height))
00428                 {
00429                         MessageBox (NULL, (tileBank2.getAbsPath ()+troncated).c_str(), "Can't load bitmap.", MB_OK|MB_ICONEXCLAMATION);
00430                         return 0;
00431                 }
00432                 else
00433                 {
00434                         CTileBorder border;
00435                         border.set (Width, Height, tampon);
00436 
00437                         // rotate the border
00438                         int rotBis=rot;
00439                         while (rotBis)
00440                         {
00441                                 border.rotate ();
00442                                 rotBis--;
00443                         }
00444                         
00445                         CTileSet::TError error;
00446                         int indexError;
00447                         int pixel=-1;
00448                         int composante=4;
00449                         if (((error=tileBank2.getTileSet(_tileSet)->checkTileTransition ((CTileSet::TTransition)tile, CTile::alpha, border, indexError,
00450                                 pixel, composante))!=CTileSet::ok)&&!zouille ())
00451                         {
00452                                 char sMsg[512];
00453                                 if ((error==CTileSet::topInterfaceProblem)||(error==CTileSet::bottomInterfaceProblem)||(error==CTileSet::leftInterfaceProblem)
00454                                         ||(error==CTileSet::rightInterfaceProblem)||(error==CTileSet::topBottomNotTheSame)||(error==CTileSet::rightLeftNotTheSame)
00455                                         ||(error==CTileSet::topInterfaceProblem))
00456                                 {
00457                                         static const char* comp[]={"Red", "Green", "Blue", "Alpha", ""};
00458                                         if (indexError!=-1)
00459                                                 sprintf (sMsg, "%s\nIncompatible with tile n°%d\nPixel: %d (%s)", CTileSet::getErrorMessage (error), indexError,
00460                                                         pixel, comp[composante]);
00461                                         else
00462                                                 sprintf (sMsg, "%s\nIncompatible with the 128x128 tile\nPixel: %d (%s)", CTileSet::getErrorMessage (error),
00463                                                         pixel, comp[composante]);
00464                                 }
00465                                 else
00466                                         sprintf (sMsg, "%s\nIncompatible filled tile", CTileSet::getErrorMessage (error));
00467                                 MessageBox (NULL, sMsg, "Can't add tile", MB_OK|MB_ICONEXCLAMATION);
00468                                 return 0;
00469                         }
00470                         else
00471                         {
00472                                 tileBank2.getTileSet(_tileSet)->setTileTransitionAlpha ((CTileSet::TTransition)tile, troncated, tileBank2, border, rot);
00473                                 theListTransition[tile].alphaLoaded=0;
00474                                 theListTransition[tile].Load (tileBank2.getTileSet(_tileSet)->getTransition(tile)->getTile(), NULL);
00475                         }
00476                 }
00477         }
00478         else
00479         {
00480                 // Error: bitmap not in the absolute path..
00481                 char msg[512];
00482                 sprintf (msg, "The bitmap %s is not in the absolute path %s.", name.c_str(), tileBank2.getAbsPath ().c_str());
00483                 MessageBox (NULL, msg, "Load error", MB_OK|MB_ICONEXCLAMATION);
00484         }
00485 
00486         return 1;
00487 }
00488 
00489 void TileList::removeTile128 (int index)
00490 {
00491         tileBank2.getTileSet (_tileSet)->removeTile128 (index, tileBank2);
00492         theList[0].erase (theList[0].begin()+index);
00493         for (int i=0; i<(sint)theList[0].size(); i++)
00494         {
00495                 theList[0][i].id=i;
00496         }
00497 }
00498 
00499 void TileList::removeTile256 (int index)
00500 {
00501         tileBank2.getTileSet (_tileSet)->removeTile256 (index, tileBank2);
00502         theList[1].erase (theList[1].begin()+index);
00503         for (int i=0; i<(sint)theList[1].size(); i++)
00504         {
00505                 theList[1][i].id=i;
00506         }
00507 }
00508 
00509 void TileList::clearTile128 (int index, CTile::TBitmap bitmap)
00510 {
00511         switch (bitmap)
00512         {
00513         case CTile::diffuse:
00514                 theList128[index].loaded=0;
00515                 theList128[index].Bits.resize(0);
00516                 break;
00517         case CTile::additive:
00518                 theList128[index].nightLoaded=0;
00519                 theList128[index].nightBits.resize(0);
00520                 break;
00521         case CTile::alpha:
00522                 theList128[index].alphaLoaded=0;
00523                 theList128[index].alphaBits.resize(0);
00524                 break;
00525         }
00526         tileBank2.getTileSet (_tileSet)->clearTile128 (index, bitmap, tileBank2);
00527 }
00528 
00529 void TileList::clearTile256 (int index, CTile::TBitmap bitmap)
00530 {
00531         switch (bitmap)
00532         {
00533         case CTile::diffuse:
00534                 theList256[index].loaded=0;
00535                 theList256[index].Bits.resize(0);
00536                 break;
00537         case CTile::additive:
00538                 theList256[index].nightLoaded=0;
00539                 theList256[index].nightBits.resize(0);
00540                 break;
00541         case CTile::alpha:
00542                 theList256[index].alphaLoaded=0;
00543                 theList256[index].alphaBits.resize(0);
00544                 break;
00545         }
00546         tileBank2.getTileSet (_tileSet)->clearTile256 (index, bitmap, tileBank2);
00547 }
00548 
00549 void TileList::clearTransition (int index, CTile::TBitmap bitmap)
00550 {
00551         switch (bitmap)
00552         {
00553         case CTile::diffuse:
00554                 theListTransition[index].loaded=0;
00555                 theListTransition[index].Bits.resize(0);
00556                 break;
00557         case CTile::additive:
00558                 theListTransition[index].nightLoaded=0;
00559                 theListTransition[index].nightBits.resize(0);
00560                 break;
00561         case CTile::alpha:
00562                 theListTransition[index].alphaLoaded=0;
00563                 theListTransition[index].alphaBits.resize(0);
00564                 break;
00565         }
00566         tileBank2.getTileSet (_tileSet)->clearTransition ((CTileSet::TTransition)index, bitmap, tileBank2);
00567 }
00568 
00569 void TileList::clearDisplacement (int index)
00570 {
00571         // Clear the displacement map filename
00572         tileBank2.getTileSet (_tileSet)->clearDisplacement ((CTileSet::TDisplacement)index);
00573 }
00574 
00575 tilelist::iterator TileList::GetFirst(int n)
00576 {
00577         //UpdateLF();
00578         return theList[n].begin();
00579 }
00580 
00581 tilelist::iterator TileList::GetLast(int n)
00582 {
00583         //UpdateLF();
00584         return theList[n].end();
00585 }
00586 
00587 int TileList::GetSize(int n)
00588 {
00589         //UpdateLF();
00590         return theList[n].size();
00591 }
00592 
00593 void TileInfo::Delete ()
00594 {
00595         loaded=0;
00596         nightLoaded=0;
00597         alphaLoaded=0;
00598 }
00599 
00600 
00601 tilelist::iterator TileList::Get(int i, int n)
00602 {
00603         return theList[n].begin()+i;
00604 }
00605 
00606 const std::string& TileInfo::getRelativeFileName (CTile::TBitmap type, int index)
00607 {
00608         return tileBank2.getTile (index)->getRelativeFileName (type);
00609 }
00610 
00611 bool TileInfo::Load (int index, std::vector<NLMISC::CBGRA>* Alpha)
00612 {
00613         bool bRes=true;
00614         if (!loaded && getRelativeFileName (CTile::diffuse, index)!="")
00615         {
00616                 if (!_LoadBitmap(tileBank2.getAbsPath() + getRelativeFileName (CTile::diffuse, index), &BmpInfo, Bits, Alpha, 0))
00617                 {
00618                         bRes=false;
00619                         MessageBox (NULL, (tileBank2.getAbsPath() + getRelativeFileName (CTile::diffuse, index)).c_str(), "Can't load file", MB_OK|MB_ICONEXCLAMATION);
00620                 }
00621                 else
00622                         loaded=1;
00623         }
00624         if (!nightLoaded && getRelativeFileName (CTile::additive, index)!="")
00625         {
00626                 if (!_LoadBitmap(tileBank2.getAbsPath() + getRelativeFileName (CTile::additive, index), &nightBmpInfo, nightBits, Alpha, 0))
00627                 {
00628                         bRes=false;
00629                         MessageBox (NULL, (tileBank2.getAbsPath() + getRelativeFileName (CTile::additive, index)).c_str(), "Can't load file", MB_OK|MB_ICONEXCLAMATION);
00630                 }
00631                 else
00632                         nightLoaded=1;
00633         }
00634         if (!alphaLoaded && getRelativeFileName (CTile::alpha, index)!="")
00635         {
00636                 if (!_LoadBitmap(tileBank2.getAbsPath() + getRelativeFileName (CTile::alpha, index), &alphaBmpInfo, alphaBits, NULL, 
00637                         tileBank2.getTile (index)->getRotAlpha ()))
00638                 {
00639                         bRes=false;
00640                         MessageBox (NULL, (tileBank2.getAbsPath() + getRelativeFileName (CTile::alpha, index)).c_str(), "Can't load file", MB_OK|MB_ICONEXCLAMATION);
00641                 }
00642                 else
00643                         alphaLoaded=1;
00644         }
00645         return bRes;
00646 }
00647 
00648 void TileList::Reload(int first, int count, int n) //recharge en memoire une tranche de tiles
00649 {
00650         for (int i=first; i<count; i++)
00651         {
00652                 switch (n)
00653                 {
00654                 case 0:
00655                         theList[n][i].Load (tileBank2.getTileSet(_tileSet)->getTile128 (i), NULL);
00656                         break;
00657                 case 1:
00658                         theList[n][i].Load (tileBank2.getTileSet(_tileSet)->getTile256 (i), NULL);
00659                         break;
00660                 case 2:
00661                         {
00662                                 int index=tileBank2.getTileSet(_tileSet)->getTransition (i)->getTile();
00663                                 if (index!=-1)
00664                                         theList[n][i].Load (index, &theListTransition[i].alphaBits);
00665                         }
00666                         break;
00667                 }
00668         }
00669 }
00670 
00671 //CTView
00672 CTView::CTView()
00673 {
00674         sizetile_x = SIZE_SMALL; sizetile_y = SIZE_SMALL;
00675         sizetext_y = 14;
00676         spacing_x = SPACING_SMALL_X; spacing_y = SPACING_SMALL_Y; 
00677         Zoom=3; Texture = 1; Sort = 1; InfoTexte = 1;
00678         count_ = 0; ViewTileMode = 0;
00679         scrollpos = 0; lastVBarPos = 0;
00680         smEdgeList = 0; bPopup = 0;
00681         //theViewPopup = 0;
00682 }
00683 
00684 CTView::~CTView()
00685 {
00686 }
00687 
00688 void CTView::Init(int _land, int n)
00689 {       
00690         InfoList._tileSet=_land;
00691         UpdateSize(n);
00692         pipo_buffer = (void *)new char[sizetile_x * sizetile_y * 3];
00693         bmp = new CBitmap;
00694         bmp->CreateBitmap(sizetile_x,sizetile_y,1,24,pipo_buffer);
00695         pImList = new CImageList;
00696         pImList->Create(sizetile_x,sizetile_y,ILC_COLOR24,0,1);
00697         pImList->Add(bmp,(CBitmap*)NULL);
00698         char *defautpath = ((SelectionTerritoire*)GetParent()->GetParent())->DefautPath.GetBuffer(256);
00699         count_=1;
00700 }
00701 
00702 void CTView::Delete()
00703 {
00704         count_=0; pImList = 0;
00705 }
00706 
00707 int CTView::GetNbTileLine(void)
00708 {
00709         RECT rect; GetClientRect(&rect);
00710         return (max (1, (rect.right - rect.left - spacing_x)/(sizeicon_x + spacing_x)));
00711 }
00712 
00713 int CTView::GetNbTileColumn(void)
00714 {
00715         RECT rect; GetClientRect(&rect);
00716         int deb;
00717         if (scrollpos<spacing_y) deb = -scrollpos;
00718         else deb = (scrollpos - spacing_y)%(sizeicon_y + spacing_y);
00719         if (deb>sizeicon_y) deb -=sizeicon_y + spacing_y;
00720         int ret= ((rect.bottom - rect.top /*- spacing_y*/ + deb)/(sizeicon_y + spacing_y)) +1 ;
00721         return max (1, ret);
00722 }
00723 
00724 void CTView::GetVisibility(int &First,int &Last, int n) //retourne l'indice du premier et du dernier item visible dans la fenetre
00725 {
00726         int y;
00727         int i = GetNbTileLine(); int j = GetNbTileColumn();
00728         if (scrollpos<spacing_y) y = 0;
00729         else y = (scrollpos - spacing_y)%(sizeicon_y + spacing_y);
00730         First = ((scrollpos - spacing_y)/(sizeicon_y + spacing_y));
00731         if (y>sizeicon_y) First++;
00732         First *= i;
00733         Last = First + i*j -1;
00734         if (InfoList.GetSize(n)>0 && Last>=InfoList.GetSize(n)) Last = InfoList.GetSize(n)-1;
00735         if (First>Last) First = Last;
00736 }
00737 
00738 int CTView::GetIndex(LPPOINT pt, int n) //retourne l'index d'un incone a partir de sa position dans le fenetre
00739 //si le curseur n'est pas sur un icon, retourne -1
00740 {
00741         POINT pts = *pt;
00742         pts.y += scrollpos;
00743         RECT rect; GetClientRect(&rect);
00744         int i = GetNbTileLine();
00745         int lf = (rect.right - rect.left) - (GetNbTileLine()*(spacing_x+sizeicon_x) + spacing_x);
00746         if (pts.x > (rect.right - rect.left - lf)) return -1;
00747         if (pts.x<0) return -1;
00748         if (pts.y<spacing_y) return -1;
00749         int y = (pts.y - spacing_y)%(spacing_y + sizeicon_y);
00750         if (y>sizeicon_y) return -1;
00751         int il = (pts.y - spacing_y)/(spacing_y + sizeicon_y);
00752         int x = (pts.x - spacing_x)%(spacing_x + sizeicon_x);
00753         if (x>sizeicon_x) return -1;
00754         int ic = (pts.x - spacing_x)/(spacing_x + sizeicon_x);
00755         int ret = ic + il*i;
00756         if (ret<InfoList.GetSize(n) && ret>=0) return ret;
00757         else return -1;
00758 }
00759 
00760 POINT CTView::GetPos(int i) //fonction inverse de GetIndex
00761 {
00762         POINT ret;
00763         int nl = max (1, GetNbTileLine());
00764         ret.x = (i%nl)*(spacing_x + sizeicon_x) + spacing_x;
00765         ret.y = (i/nl)*(spacing_y + sizeicon_y) + spacing_y - scrollpos;        
00766         return ret;
00767 }
00768 
00769 void CTView::UpdateSelection(LPRECT rect_,int mode, int n) //rect : coordonnees du rectangle de selection dans la fenetre parent
00770 {       
00771         RECT client,*rect; GetWindowRect(&client);
00772         rect = new RECT; memcpy(rect,rect_,sizeof(RECT));
00773         //on se met dans le repere de CTView
00774         POINT pt; pt.x = rect->left; pt.y = rect->top;
00775         GetParent()->ClientToScreen(&pt);
00776         ScreenToClient(&pt);
00777         rect->left = pt.x;
00778         rect->top = pt.y;       
00779         pt.x = rect->right; pt.y = rect->bottom;
00780         GetParent()->ClientToScreen(&pt);
00781         ScreenToClient(&pt);
00782         rect->right = pt.x;
00783         rect->bottom = pt.y;
00784         rect->top += scrollpos; rect->bottom += scrollpos;
00785 
00786         if (rect->left<300)
00787         {
00788                 int toto = 0;
00789         }
00790 
00791         //on clip
00792         if (rect->left<0) rect->left = 0;
00793         if (rect->top<scrollpos) rect->top = scrollpos;
00794         int lf = (client.right - client.left) - (GetNbTileLine()*(spacing_x+sizeicon_x) + spacing_x);
00795         if (rect->right>(client.right-client.left-spacing_x-lf)) 
00796         {
00797                 rect->right = client.right - client.left - spacing_x - lf;
00798         }
00799         if ((rect->bottom-scrollpos)>(client.bottom-client.top)) rect->bottom=client.bottom-client.top+scrollpos;       
00800         
00801         if (rect->left<spacing_x) rect->left = spacing_x;
00802         if ((rect->top-scrollpos)<spacing_y) rect->top = spacing_y + scrollpos;
00803         int rx = (rect->left - spacing_x)%(spacing_x + sizeicon_x);
00804         rect->left -= rx;
00805         if (rx>=sizeicon_x)
00806         {
00807                 rect->left += sizeicon_x + spacing_x;
00808                 if (rect->left>=(client.right - client.left)) rect->left = (client.right - client.left) - spacing_x;
00809         }
00810         int ry = (rect->top - spacing_y)%(spacing_y + sizeicon_y);
00811         rect->top -= ry;
00812         if (ry>=sizeicon_y)
00813         {
00814                 rect->top += sizeicon_y + spacing_y;
00815                 if ((rect->top-scrollpos)>=(client.bottom - client.top)) rect->top = (client.bottom - client.top) - spacing_y + scrollpos;
00816         }
00817 
00818         
00819         if (rect->right<spacing_x || (rect->bottom-scrollpos)<spacing_y) return;
00820         rx = (rect->right - spacing_x)%(spacing_x + sizeicon_x);
00821         rect->right += (sizeicon_x - rx -1 );
00822         ry = (rect->bottom - spacing_y)%(spacing_y + sizeicon_y);
00823         rect->bottom += (sizeicon_y - ry -1 );
00824 
00825         if (rect->bottom<rect->top || rect->left>rect->right) return;
00826         pt; pt.x = rect->left; pt.y = rect->top; pt.y -=scrollpos;
00827         int index = GetIndex(&pt, n);
00828         tilelist::iterator p = InfoList.GetFirst(n);    
00829         CDC *pDC = GetDC();
00830         if (index==-1 && !(mode&MK_SHIFT))
00831         {
00832                 for (int i = 0;i<InfoList.GetSize(n); i++)
00833                 {
00834                         if (p->Selected==1)
00835                         {
00836                                 p->Selected = 0;
00837                                 DrawTile(p,pDC,0,n);
00838                         }
00839                         p++;
00840                 }
00841                 if (pDC) 
00842                         ::ReleaseDC(*this,*pDC);
00843                 return;
00844         }
00845         for (int i = 0;i<index;i++) 
00846         {
00847                 if (p==InfoList.GetLast(n))
00848                 {
00849                         if (pDC!=NULL) 
00850                                 ::ReleaseDC(*this,*pDC);
00851                         return;
00852                 }
00853                 if (p->Selected==1) 
00854                 {
00855                         p->Selected = 0;
00856                         DrawTile(p,pDC,0,n);
00857                 }
00858                 else if (p->Selected&4)
00859                 {
00860                         if (p->Selected) p->Selected=2;
00861                         else p->Selected = 3;
00862                         DrawTile(p,pDC,0,n);
00863                 }
00864                 p++;
00865         }
00866         int nbline = GetNbTileLine();
00867         int incd = index%nbline;
00868         for (int j = rect->top;j<=rect->bottom;j+=sizeicon_y + spacing_y)
00869         {
00870                 int k = 0;
00871                 if (j!=rect->top)
00872                 {
00873                         for (;k<incd;k++) 
00874                         {
00875                                 if (p==InfoList.GetLast(n))
00876                                 {
00877                                         if (pDC!=NULL) 
00878                                                 ::ReleaseDC(*this,*pDC);
00879                                         return;
00880                                 }
00881                                 if (p->Selected==1)
00882                                 {
00883                                         p->Selected = 0; 
00884                                         DrawTile(p,pDC,0,n);
00885                                 }                               
00886                                 else if (p->Selected&4)
00887                                 {
00888                                         if (p->Selected&3) p->Selected=2;
00889                                         else p->Selected = 3;
00890                                         int k = !(p->Selected&1);
00891                                         DrawTile(p,pDC,0,n);
00892                                 }
00893                                 p++;
00894                         }
00895                 }
00896                 else k = incd;
00897                 for (int i = rect->left;i<=rect->right;i+=sizeicon_x + spacing_x)
00898                 {
00899                         if (p==InfoList.GetLast(n)) 
00900                         {
00901                                 if (pDC!=NULL) 
00902                                         ::ReleaseDC(*this,*pDC);
00903                                 return;
00904                         }
00905                         if (!(mode&MK_CONTROL))
00906                         {
00907                                 if (p->Selected!=1)
00908                                 {
00909                                         p->Selected = 1;
00910                                         DrawTile(p,pDC,0,n);
00911                                 }
00912                         }
00913                         else
00914                         {                               
00915                                 if ((p->Selected&4)==0)
00916                                 {
00917                                         int k = p->Selected;
00918                                         p->Selected = (p->Selected&3)?4:5;                              
00919                                         k = p->Selected;
00920                                         DrawTile(p,pDC,0,n);
00921                                 }
00922                         }
00923                         p++;
00924                         k++;
00925                 }
00926                 for (;k<nbline;k++) 
00927                 {
00928                         if (p==InfoList.GetLast(n)) 
00929                         {
00930                                 if (pDC!=NULL) 
00931                                         ::ReleaseDC(*this,*pDC);
00932                                 return;
00933                         }
00934                         if (p->Selected==1) 
00935                         {
00936                                 p->Selected = 0; 
00937                                 DrawTile(p,pDC,0,n);
00938                         } 
00939                         else if (p->Selected&4)
00940                         {
00941                                 if (p->Selected&3) p->Selected=2;
00942                                 else p->Selected = 3;
00943                                 DrawTile(p,pDC,0,n);
00944                         }
00945                         p++;
00946                 }
00947         }
00948         for (;p!=InfoList.GetLast(n);p++) 
00949         {
00950                 if (p->Selected==1) 
00951                 {
00952                         p->Selected = 0;
00953                         DrawTile(p,pDC,0,n);
00954                 }
00955                 else if (p->Selected&4)
00956                 {
00957                         if (p->Selected&3) p->Selected=2;
00958                         else p->Selected = 3;
00959                         DrawTile(p,pDC,0,n);
00960                 }
00961         }
00962         if (pDC!=NULL) 
00963                 ::ReleaseDC(*this,*pDC);
00964 }
00965                 
00966 void CTView::DeleteTile(tilelist::iterator p)
00967 {
00968         p->loaded = 0;
00969         p->nightLoaded = 0;
00970         p->alphaLoaded = 0;
00971 }
00972 
00973 void CTView::UpdateBar(int iFirst,int iLast, int n)
00974 {
00975         int i = GetNbTileColumn(); 
00976         int j = max (1, GetNbTileLine());
00977         int nbline = InfoList.GetSize(n)/j+1;
00978         int posline = iFirst/j;
00979         SCROLLINFO scr;
00980         scr.fMask = SIF_ALL ^ SIF_POS;
00981         scr.nMin = 0; scr.nMax = SCROLL_MAX; 
00982         if (nbline==0) {scr.nPage = SCROLL_MAX; scr.nPos = 0;}
00983         else 
00984         {
00985                 scr.nPage = (SCROLL_MAX*i)/nbline;
00986         }
00987         GetParent()->SetScrollInfo(SB_VERT,&scr);
00988 }
00989         
00990 
00991 
00992 
00993 int CTView::IsSelected(int i)
00994 {
00995         return 0;
00996 }
00997 
00998 void CTView::UpdateSize(int n)
00999 {
01000         spacing_tile_text = 3;
01001         if (Zoom==1) {sizetile_x = sizetile_y = SIZE_SMALL; spacing_x = SPACING_SMALL_X; spacing_y = SPACING_SMALL_Y;}
01002         if (Zoom==2) {sizetile_x = sizetile_y = SIZE_NORMAL; spacing_x = SPACING_NORMAL_X; spacing_y = SPACING_NORMAL_Y;}
01003         if (Zoom==3) {sizetile_x = sizetile_y = SIZE_BIG; spacing_x = SPACING_BIG_X; spacing_y = SPACING_BIG_Y;}
01004         if (n==1)
01005         {
01006                 sizetile_x *= 2;
01007                 sizetile_y *= 2;
01008         }
01009         sizeicon_x = sizetile_x; sizeicon_y = sizetile_y + sizetext_y + spacing_tile_text;
01010 }
01011 
01012 int debug = 0;
01013 
01014 void CTView::OnPaint() 
01015 {
01016         Browse *parent = (Browse*)this->GetParent();
01017         CPaintDC dc(this); // device context for painting
01018         // TODO: Add your message handler code here
01019         RECT rect; GetClientRect(&rect);        
01020         CBrush brush (GetSysColor(COLOR_3DFACE));
01021         
01022         
01023         dc.FillRect(&rect,&brush);
01024         
01025         if (InfoList.GetSize(parent->m_128x128)==0) return;
01026         
01027         debug=(debug+1)&1;
01028 
01029         if (debug==1)
01030         {
01031                 debug = 1;
01032         }
01033 
01034         CRgn clip;
01035         clip.CreateRectRgn(rect.left,rect.top,rect.right,rect.bottom);
01036         dc.SelectClipRgn(&clip);
01037         
01038         CRgn update;
01039         if (GetUpdateRgn(&update)==NULLREGION)
01040         {
01041                 ::ReleaseDC(*this,dc);
01042                 return;
01043         }
01044 
01045         GetVisibility(iFV, iLV, parent->m_128x128);
01046         UpdateBar(iFV, iLV, parent->m_128x128);
01047         if (!normal_font) 
01048         {
01049                 normal_font = new CFont;
01050                 normal_font->CreateFont(-10,0,0,0,FW_THIN,false,false,false,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FIXED_PITCH,NULL);
01051         }
01052         
01053         tilelist::iterator p = InfoList.GetFirst(parent->m_128x128);
01054         for (int i=0;i<iFV;i++) p++;
01055         for (i=iFV;i<=iLV;i++) 
01056                 DrawTile(p++,&dc,0, parent->m_128x128);
01057         ::ReleaseDC (*this, dc);
01058 }
01059 
01060 void CTView::OnDropFiles(HDROP hDropInfo) 
01061 {
01062         // TODO: Add your message handler code here and/or call default
01063         //first : on verifie s'il les tiles doivent etre inseres
01064         if (!lockInsertion)
01065         {
01066                 Browse *parent = (Browse*)this->GetParent();
01067                 char FileName[256];
01068                 int count=DragQueryFile(hDropInfo,0xffffffff,FileName,256); //count = nombre de fichiers dans le drop
01069                 
01070 
01071                 POINT pos;
01072                 DragQueryPoint(hDropInfo,&pos); //retrieve cursor position
01073                 CDC *pDC = GetDC();
01074                 for (int i=0;i<count;i++) 
01075                 {
01076                         DragQueryFile(hDropInfo,i,FileName,256);
01077                         switch (parent->m_128x128)
01078                         {
01079                         case 0:
01080                                 {
01081                                         int index=InfoList.addTile128 ();
01082                                         if (InfoList.setTile128 (index, FileName, Texture==1?CTile::diffuse:(Texture==2?CTile::additive:CTile::alpha)))
01083                                         {
01084                                                 tilelist::iterator it = InfoList.GetLast(parent->m_128x128);
01085                                                 it--;
01086                                         }
01087                                         else
01088                                         {
01089                                                 InfoList.removeTile128 (index);
01090                                         }
01091                                 }
01092                                 break;
01093                         case 1:
01094                                 {
01095                                         int index=InfoList.addTile256 ();
01096                                         if (InfoList.setTile256 (index, FileName, Texture==1?CTile::diffuse:(Texture==2?CTile::additive:CTile::alpha)))
01097                                         {
01098                                                 tilelist::iterator it = InfoList.GetLast(parent->m_128x128);
01099                                                 it--;
01100                                         }
01101                                         else
01102                                                 InfoList.removeTile256 (index);
01103                                 }
01104                                 break;
01105                         case 2:
01106                                 {
01107                                 }
01108                                 break;
01109                         }
01110                 }
01111                 
01112                 int iFV,iLV;
01113                 GetVisibility(iFV, iLV, parent->m_128x128);
01114                 UpdateBar(iFV, iLV, parent->m_128x128);
01115                 Invalidate ();
01116         }
01117         CStatic::OnDropFiles(hDropInfo);
01118 }
01119 
01120 void CTView::DrawTile(tilelist::iterator i,CDC *pDC,int clear, int n)
01121 {
01122         RECT rect; GetClientRect(&rect);        
01123         CBrush brush (GetSysColor(COLOR_3DFACE));
01124         if (InfoList.GetSize(n)==0) 
01125         {
01126                 return;
01127         }
01128 
01129         LPBITMAPINFO bmpinf;
01130         std::string pth; 
01131         std::vector<NLMISC::CBGRA> *bits;
01132         int loaded;
01133 
01134         switch (n)
01135         {
01136         case 0:
01137                 pth = i->getRelativeFileName ((CTile::TBitmap)(Texture-1), tileBank2.getTileSet (InfoList._tileSet)->getTile128 (i->id));
01138                 break;
01139         case 1:
01140                 pth = i->getRelativeFileName ((CTile::TBitmap)(Texture-1), tileBank2.getTileSet (InfoList._tileSet)->getTile256 (i->id));
01141                 break;
01142         case 2:
01143                 {
01144                         int index=tileBank2.getTileSet (InfoList._tileSet)->getTransition (i->id)->getTile();
01145                         if (index!=-1)
01146                                 pth = i->getRelativeFileName ((CTile::TBitmap)(Texture-1), index);
01147                         else
01148                                 pth = "";
01149                 }
01150                 break;
01151         case 3:
01152                 pth = tileBank2.getTileSet (InfoList._tileSet)->getDisplacementFileName ((CTileSet::TDisplacement)i->id);
01153                 break;
01154         }
01155 
01156         switch(Texture)
01157         {
01158                 case 1:
01159                         bmpinf = &(i->BmpInfo);
01160                         bits = &i->Bits;
01161                         loaded = i->loaded;
01162                         break;
01163                 case 2:
01164                         bmpinf = &(i->nightBmpInfo);
01165                         bits = &i->nightBits;
01166                         loaded = i->nightLoaded;
01167                         break;
01168                 case 3:
01169                         bmpinf = &(i->alphaBmpInfo);
01170                         bits = &i->alphaBits;
01171                         loaded = i->alphaLoaded;
01172                         break;
01173         }
01174 
01175         CRgn clip;
01176         clip.CreateRectRgn(rect.left,rect.top,rect.right,rect.bottom);
01177         pDC->SelectClipRgn(&clip);
01178         
01179         // Select a font
01180         CFont *pOldFont=pDC->SelectObject(normal_font);
01181         
01182         POINT pt;
01183         pt = GetPos(i->id);
01184         RECT rect_txt; 
01185         rect_txt.top = pt.y;
01186         rect_txt.bottom = pt.y + sizeicon_y + spacing_y;
01187         rect_txt.left = pt.x; rect_txt.right = pt.x + sizeicon_x + spacing_x;
01188 
01189         // Turn every other pixel to black
01190         COLORREF clrBk = pDC->SetBkColor( GetSysColor(COLOR_3DFACE) );
01191         COLORREF clrText = pDC->SetTextColor( RGB(0,0,0) );
01192         
01193         if (clear) pDC->FillRect(&rect_txt,&brush);
01194         
01195         if (!loaded)
01196         {
01197                 pDC->FillSolidRect( pt.x, pt.y, sizetile_x, sizetile_y, GetSysColor(COLOR_3DFACE) );
01198                 pDC->MoveTo (pt.x,pt.y);
01199                 pDC->LineTo (pt.x+sizetile_x,pt.y+sizetile_y);
01200                 pDC->MoveTo (pt.x+sizetile_x,pt.y);
01201                 pDC->LineTo (pt.x,pt.y+sizetile_y);
01202                 pDC->MoveTo (pt.x,pt.y);
01203                 pDC->LineTo (pt.x+sizetile_x,pt.y);
01204                 pDC->LineTo (pt.x+sizetile_x,pt.y+sizetile_y);
01205                 pDC->LineTo (pt.x,pt.y+sizetile_y);
01206                 pDC->LineTo (pt.x,pt.y);
01207 
01208                 pDC->MoveTo (pt.x+1,pt.y);
01209                 pDC->LineTo (pt.x+sizetile_x,pt.y+sizetile_y-1);
01210                 pDC->MoveTo (pt.x,pt.y+1);
01211                 pDC->LineTo (pt.x+sizetile_x-1,pt.y+sizetile_y);
01212                 
01213                 pDC->MoveTo (pt.x+sizetile_x-1,pt.y);
01214                 pDC->LineTo (pt.x,pt.y+sizetile_y-1);
01215                 pDC->MoveTo (pt.x+sizetile_x,pt.y+1);
01216                 pDC->LineTo (pt.x+1,pt.y+sizetile_y);
01217         }
01218         else
01219         {
01220                 StretchDIBits(pDC->m_hDC,pt.x,pt.y,
01221                         sizetile_x,sizetile_y,0,0,
01222                         bmpinf->bmiHeader.biWidth,
01223                         -bmpinf->bmiHeader.biHeight,
01224                         &*bits->begin(),bmpinf,DIB_RGB_COLORS,SRCCOPY);
01225         }
01226                         
01227         char temp[100];
01228         char Name[256]; Name[0] = 0;
01229         if (InfoTexte==2)
01230         {
01231                 _splitpath(pth.c_str(),temp,temp,Name,temp);            
01232         }
01233         else if (InfoTexte==3)
01234         {
01235                 __int64 mask = 1;
01236         }
01237         else if (InfoTexte==1)
01238         {
01239                 sprintf(Name,"%d",i->id);
01240         }
01241         rect_txt.top = pt.y + sizetile_y + spacing_tile_text;
01242         rect_txt.bottom += rect_txt.top + sizetext_y;
01243         rect_txt.left -= spacing_x;
01244         pDC->DrawText(Name,strlen(Name),&rect_txt,DT_CENTER | DT_SINGLELINE);
01245 
01246         // Restore the device context
01247         pDC->SetBkColor( clrBk );
01248         pDC->SetTextColor( clrText );
01249 
01250         if (i->Selected&3)
01251         {
01252                 CRect rc;
01253                 rc.left = pt.x; rc.top = pt.y; rc.right = rc.left + sizetile_x; rc.bottom = rc.top + sizetile_y;
01254                 ShadeRect(pDC,rc);
01255         }
01256 
01257         // Invalidate flag button
01258         Browse *parent = (Browse*)this->GetParent();
01259         parent->UpdateFlags ();
01260 
01261         // Release the font
01262         pDC->SelectObject(pOldFont);
01263 }
01264 
01265 void CTView::ShadeRect( CDC *pDC, CRect& rect )
01266 
01267 {
01268      // Bit pattern for a monochrome brush with every
01269      // other pixel turned off
01270      WORD Bits[8] = { 0x0055, 0x00aa, 0x0055, 0x00aa,
01271                       0x0055, 0x00aa, 0x0055, 0x00aa };
01272 
01273         CBitmap bmBrush;
01274         CBrush brush; // (GetSysColor(COLOR_3DFACE));
01275 
01276         // Need a monochrome pattern bitmap
01277         bmBrush.CreateBitmap( 8, 8, 1, 1, &Bits );
01278 
01279         // Create the pattern brush
01280         brush.CreatePatternBrush( &bmBrush );
01281 
01282         CBrush *pOldBrush = pDC->SelectObject( &brush );
01283 
01284         // Turn every other pixel to black
01285         COLORREF clrBk = pDC->SetBkColor( GetSysColor(COLOR_3DFACE) );
01286         COLORREF clrText = pDC->SetTextColor( RGB(0,0,0) );
01287         // 0x00A000C9 is the ROP code to AND the brush with the destination
01288         pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), 
01289                  (DWORD)0x00A000C9);                     //DPa - raster code
01290 
01291         pDC->SetBkColor( clrBk );
01292         pDC->SetTextColor( clrText );
01293         clrBk = pDC->SetBkColor( RGB(0,0,0) );
01294         clrText = pDC->SetTextColor( GetSysColor(COLOR_HIGHLIGHT) );
01295         // 0x00FA0089 is the ROP code to OR the brush with the destination
01296         pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), 
01297                  (DWORD)0x00FA0089);                     //DPo - raster code
01298 
01299         // Restore the device context
01300         pDC->SelectObject( pOldBrush );
01301         pDC->SetBkColor( clrBk );
01302         pDC->SetTextColor( clrText );
01303 }
01304 
01305 
01306 //code modifie des sources des MFC =)))
01307 void CTView::DrawDragRect(CDC *pDC,LPCRECT lpRect, SIZE size,
01308         LPCRECT lpRectLast, SIZE sizeLast, CBrush* pBrush, CBrush* pBrushLast)
01309 {
01310         // first, determine the update region and select it
01311         CRgn rgnNew;
01312         CRgn rgnOutside, rgnInside;
01313         CRect rect;
01314         if (lpRect)
01315         {
01316                 rgnOutside.CreateRectRgnIndirect(lpRect);
01317                 rect = *lpRect;
01318                 rect.InflateRect(-size.cx, -size.cy);
01319                 rect.IntersectRect(rect, lpRect);
01320                 rgnInside.CreateRectRgnIndirect(rect);
01321                 rgnNew.CreateRectRgn(0, 0, 0, 0);
01322                 rgnNew.CombineRgn(&rgnOutside, &rgnInside, RGN_XOR);
01323         }
01324 
01325         CBrush* pBrushOld = NULL;
01326         if (pBrush == NULL)
01327                 pBrush = CDC::GetHalftoneBrush();
01328         if (pBrushLast == NULL)
01329                 pBrushLast = pBrush;
01330 
01331         CRgn rgnLast, rgnUpdate;
01332         if (lpRectLast != NULL/* && lpRect!=NULL*/)
01333         {
01334                 // find difference between new region and old region
01335                 rgnLast.CreateRectRgn(0, 0, 0, 0);
01336                 if (lpRect==NULL) rgnOutside.CreateRectRgnIndirect(lpRectLast);
01337                 else rgnOutside.SetRectRgn(lpRectLast);
01338                 rect = *lpRectLast;
01339                 rect.InflateRect(-sizeLast.cx, -sizeLast.cy);
01340                 rect.IntersectRect(rect, lpRectLast);
01341                 if (lpRect==NULL) rgnInside.CreateRectRgnIndirect(rect);
01342                 else rgnInside.SetRectRgn(rect);
01343                 rgnLast.CombineRgn(&rgnOutside, &rgnInside, RGN_XOR);
01344 
01345                 // only diff them if brushes are the same
01346                 if (pBrush->m_hObject == pBrushLast->m_hObject)
01347                 {
01348                         rgnUpdate.CreateRectRgn(0, 0, 0, 0);
01349                         rgnUpdate.CombineRgn(&rgnLast, &rgnNew, RGN_XOR);
01350                 }
01351         }
01352         if (!lpRect || (pBrush->m_hObject != pBrushLast->m_hObject && lpRectLast != NULL))
01353         {
01354                 // brushes are different -- erase old region first
01355                 pDC->SelectClipRgn(&rgnLast);
01356                 pDC->GetClipBox(&rect);
01357                 pBrushOld = pDC->SelectObject(pBrushLast);
01358                 pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATINVERT);
01359                 pDC->SelectObject(pBrushOld);
01360         }
01361         // draw into the update/new region
01362         if (lpRect)
01363         {
01364                 pDC->SelectClipRgn(rgnUpdate.m_hObject != NULL ? &rgnUpdate : &rgnNew);
01365                 pDC->GetClipBox(&rect);
01366                 pBrushOld = pDC->SelectObject(pBrush);
01367                 pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATINVERT);
01368                 pDC->SelectObject(pBrushOld);
01369         }
01370         pDC->SelectClipRgn(NULL);
01371 }
01372 
01373 void CTView::RemoveSelection(int n)
01374 {
01375         for (tilelist::iterator p = InfoList.theList[n].begin();p!=InfoList.theList[n].end();++p)
01376         {
01377                 p->Selected = 0;
01378         }
01379 }
01380 
01381 void CTView::InsertItemInCtrlList(tilelist::iterator iFirst,tilelist::iterator iLast)
01382 {
01383 /*      int iItem = InfoList.GetSize();
01384         for (tilelist::iterator i=iFirst;i!=iLast;++i)
01385         {
01386                 char num[10];
01387                 sprintf(num,"%d",iItem);
01388                 InsertItem(iItem,num,0);
01389                 SetItemData(iItem++,(DWORD)(*i));
01390         }*/
01391 }
01392 
01393 
01394 
01395 LRESULT CTView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
01396 {
01397         // TODO: Add your specialized code here and/or call the base class
01398         if (message==WM_MOUSEMOVE)
01399         {
01400                 MousePos.x = LOWORD(lParam);
01401                 MousePos.y = HIWORD(lParam);
01402         }
01403         if (message==WM_DRAWITEM)
01404         {
01405                 int toto=0;
01406         }
01407         if (message==WM_CLOSE) Delete();
01408         if (message==WM_COMMAND) //The user had selected an item on popup menu
01409         {
01410                 Browse *parent = (Browse*)this->GetParent();
01411                 int id=LOWORD(wParam);
01412                 if (id==ID_MENU_SUPR_TILE)
01413                 {
01414                         for (int i=0; i<InfoList.GetSize(parent->m_128x128); i++)
01415                         {
01416                                 if (InfoList.Get(i, parent->m_128x128)->Selected)
01417                                 {
01418                                         switch (parent->m_128x128)
01419                                         {
01420                                         case 0:
01421                                                 InfoList.removeTile128 (i);
01422                                                 break;
01423                                         case 1:
01424                                                 InfoList.removeTile256 (i);
01425                                                 break;
01426                                         default:
01427                                                 nlassert (0); // no!
01428                                         }
01429                                 }
01430                         }
01431                         bPopup = 0;
01432                 }
01433                 else if (id==ID_MENU_SUPR_BITMAP)
01434                 {
01435                         for (int i=0; i<InfoList.GetSize(parent->m_128x128); i++)
01436                         {
01437                                 if (InfoList.Get(i, parent->m_128x128)->Selected)
01438                                 {
01439                                         switch (parent->m_128x128)
01440                                         {
01441                                         case 0:
01442                                                 InfoList.clearTile128 (i, (CTile::TBitmap)(Texture-1));
01443                                                 break;
01444                                         case 1:
01445                                                 InfoList.clearTile256 (i, (CTile::TBitmap)(Texture-1));
01446                                                 break;
01447                                         case 2:
01448                                                 InfoList.clearTransition (i, (CTile::TBitmap)(Texture-1));
01449                                                 break;
01450                                         case 3:
01451                                                 InfoList.clearDisplacement (i);
01452                                                 break;
01453                                         default:
01454                                                 nlassert (0); // no!
01455                                         }
01456                                 }
01457                         }
01458                         bPopup = 0;
01459                 }
01460                 else if ((id==ID_MENU_ADD)||(id==ID_MENU_REPLACE))
01461                 {
01462                         _chdir (LastPath.c_str());
01463                         CFileDialog load(true, NULL, LastPath.c_str(), OFN_ENABLESIZING | OFN_ALLOWMULTISELECT,
01464                                 "Targa bitmap (*.tga)|*.tga|All files (*.*)|*.*||",NULL);
01465                         load.m_ofn.lpstrFile = new char[10000]; //le buffer contient la list de tous les noms de fichier
01466                         load.m_ofn.lpstrFile[0] = 0;
01467                         //avec 10ko on devrait tranquille ... si l'ensemble des noms des fichiers depassent 10000 cara, l'insertion n'a pas lieu
01468                         load.m_ofn.nMaxFile = 10000-1;
01469                         if (load.DoModal()==IDOK)
01470                         {
01471                                 //AfxMessageBox ("toto");
01472                                 POSITION p = load.GetStartPosition(); //la doc dit que p=NULL quand il n'y a pas de selection : c'est faux, genial les MFC
01473                                 while (p)
01474                                 {
01475                                         CString str = load.GetNextPathName(p);
01476 
01477                                         char sDrive[256];
01478                                         char sPath[256];
01479                                         _splitpath (str, sDrive, sPath, NULL, NULL);
01480                                         LastPath=string (sDrive)+string (sPath);
01481 
01482                                         if (str!=CString(""))
01483                                         {
01484                                                 int index=0;
01485                                                 const char *pathname = (LPCTSTR)str;
01486 
01487                                                 // Add mode, to the end of the list
01488                                                 if (id==ID_MENU_ADD)
01489                                                 {
01490                                                         // Index of the new tile
01491                                                         int index;
01492 
01493                                                         // Index in the list
01494                                                         switch (parent->m_128x128)
01495                                                         {
01496                                                         case 0:
01497                                                                 // Add a 128 tile
01498                                                                 index=InfoList.addTile128 ();
01499 
01500                                                                 // Set the tile
01501                                                                 if (!InfoList.setTile128 (index, pathname, Texture==1?CTile::diffuse:(Texture==2?CTile::additive:CTile::alpha)))
01502                                                                         // If prb, remove it
01503                                                                         InfoList.removeTile128 (index);
01504 
01505                                                                 break;
01506                                                         case 1:
01507                                                                 // Add a 128 tile
01508                                                                 index=InfoList.addTile256 ();
01509 
01510                                                                 // Set the tile
01511                                                                 if (!InfoList.setTile256 (index, pathname, Texture==1?CTile::diffuse:(Texture==2?CTile::additive:CTile::alpha)))
01512                                                                         // If prb, remove it
01513                                                                         InfoList.removeTile256 (index);
01514 
01515                                                                 break;
01516                                                         case 2:
01517                                                                 nlassert (0);           // no, can't add transition
01518                                                                 break;
01519                                                         }
01520                                                 }
01521                                                 else
01522                                                 {
01523                                                         // Must be a replace mode.
01524                                                         nlassert (id==ID_MENU_REPLACE);
01525 
01526                                                         // For each tile
01527                                                         for (int index=0; index<InfoList.GetSize(parent->m_128x128); index++)
01528                                                         {
01529                                                                 // If selected
01530                                                                 if (InfoList.Get(index, parent->m_128x128)->Selected)
01531                                                                 {
01532                                                                         switch (parent->m_128x128)
01533                                                                         {
01534                                                                         case 0:
01535                                                                                 // Set the 128 tile
01536                                                                                 InfoList.setTile128 (index, pathname, Texture==1?CTile::diffuse:(Texture==2?CTile::additive:CTile::alpha));
01537                                                                                 break;
01538                                                                         case 1:
01539                                                                                 // Set the 256 tile
01540                                                                                 InfoList.setTile256 (index, pathname, Texture==1?CTile::diffuse:(Texture==2?CTile::additive:CTile::alpha));
01541                                                                                 break;
01542                                                                         case 2:
01543                                                                                 // Alpha texture ?
01544                                                                                 if (Texture!=3)
01545                                                                                 {
01546                                                                                         InfoList.setTileTransition (index, pathname, Texture==1?CTile::diffuse:(Texture==2?CTile::additive:CTile::alpha));
01547                                                                                 }
01548                                                                                 // Alpha!
01549                                                                                 else
01550                                                                                 {
01551                                                                                         // Select rotation
01552                                                                                         SelectRotation selectRotation;
01553                                                                                         if (selectRotation.DoModal()==IDOK)
01554                                                                                         {
01555                                                                                                 // Set the alpha tile with the good rotation
01556                                                                                                 InfoList.setTileTransitionAlpha (index, pathname, selectRotation.RotSelected);
01557                                                                                         }
01558                                                                                 }
01559                                                                                 break;
01560                                                                         case 3:
01561                                                                                 // Displacement
01562                                                                                 InfoList.setDisplacement (index, pathname);
01563                                                                                 break;
01564                                                                         default:
01565                                                                                 nlassert (0); // no!
01566                                                                         }
01567                                                                 }
01568 
01569                                                                 // Reload last inserted tile
01570                                                                 InfoList.Reload(index, 1, parent->m_128x128);
01571                                                                 InfoList.Get(index, parent->m_128x128)->Selected = 0;
01572                                                         }
01573                                                 }
01574                                         }
01575                                 }
01576                         }
01577                         delete load.m_ofn.lpstrFile;
01578                         //InfoList.Sort();
01579                         bPopup = 0;
01580                 }
01581                 else if (id==12)
01582                 {
01583                         ViewTileMode = 1;
01584                         Browse *parent = (Browse*) this->GetParent();
01585                         parent->SendMessage(WM_PAINT,0,0);
01586                         bPopup = 0;
01587                 }
01588                 this->RedrawWindow();
01589         }
01590         return CStatic::WindowProc(message, wParam, lParam);
01591 }
01592         
01593 
01594 void CTView::OnRButtonDown(UINT nFlags, CPoint point) 
01595 {       
01596         // TODO: Add your message handler code here and/or call default
01597         if (!lockInsertion)
01598         {
01599                 Browse *parent = (Browse*)this->GetParent();
01600                 RECT wndpos; CMenu popup;
01601                 GetParent()->GetWindowRect(&wndpos);
01602                 popup.CreatePopupMenu();
01603 
01604                 int c = 0;
01605                 for (tilelist::iterator p = InfoList.GetFirst(parent->m_128x128);p!=InfoList.GetLast(parent->m_128x128);++p)
01606                 {
01607                         if (p->Selected) c++;
01608                 }               
01609                                 
01610                 if (!ViewTileMode)
01611                 {
01612                         popup.AppendMenu(parent->m_128x128<2 ? MF_STRING : MF_STRING | MF_GRAYED, ID_MENU_ADD,"Add...");
01613                         popup.AppendMenu(c>0 ? MF_STRING : MF_STRING | MF_GRAYED, ID_MENU_REPLACE, "Replace...");
01614                         popup.AppendMenu(c>0 ? MF_STRING : MF_STRING | MF_GRAYED, ID_MENU_SUPR_BITMAP, "Del bitmap");
01615                         popup.AppendMenu((c>0 && parent->m_128x128<2) ? MF_STRING : MF_STRING | MF_GRAYED, ID_MENU_SUPR_TILE, "Del tile");
01616                 }
01617                 else
01618                 {
01619                 }
01620                 bPopup = 1;
01621                 popup.TrackPopupMenu(TPM_LEFTALIGN,MousePos.x+wndpos.left,MousePos.y+wndpos.top,GetParent(),NULL);
01622         }
01623         CStatic::OnRButtonDown(nFlags, point);
01624 }
01625 
01626 void CTView::OnLButtonDown(UINT nFlags, CPoint point) 
01627 {
01628         // TODO: Add your message handler code here and/or call default
01629         SendMessage(WM_PAINT,0,0);
01630         CStatic::OnLButtonDown(nFlags, point);
01631 }
01632 
01633 
01634 void CTView::OnLButtonDblClk(UINT nFlags, CPoint point) 
01635 {
01636         // TODO: Add your message handler code here and/or call default
01637 }
01638