unitguidedialog.cpp

Go to the documentation of this file.
00001 #include <sigc++/sigc++.h>
00002 #include <sstream>
00003 
00004 #include "unitguidedialog.h"
00005 
00006 #include "../typen.h"
00007 #include "../terraintype.h"
00008 #include "../vehicletype.h"
00009 #include "../spfst.h"
00010 #include "../spfst-legacy.h"
00011 #include "../gameoptions.h"
00012 #include "../iconrepository.h"
00013 #include "../unitset.h"
00014 #include <iostream>
00015 #include <boost/algorithm/string/trim.hpp>
00016 
00025 class DataTab : public PG_Widget
00026 {
00027         public:
00028                 enum Page { movement, weapons1, weapons2, production, transport, general, terrainAccess1, terrainAccess2 };
00029                 
00030                 DataTab( PG_Widget* parent, const PG_Rect& pos, bool showDescription );
00031                 ~DataTab();
00032                 void setPage( Page page );
00033                 void setUnit( VehicleType* type );
00034                 void setTab0( DataTab* tab0 );
00035                 void setCompare( bool compare );
00036                 void setData();
00037                 void HideAllLocal();
00038                 
00039                 
00040         private:
00041                 Page currentPage;
00042                 VehicleType* currentUnit;
00043                 
00044                 PG_Rect parentPos;
00045                 DataTab* tabCompare;
00046                 bool compareData;
00047                 bool showDescriptionOnly;
00048                 
00049                 //std::vector< PG_Widget* > children;
00050                 PG_Widget *movementTab, *weapons1Tab, *weapons2Tab, *productionTab, *transportTab, *generalTab, *terrainAccessTab1, *terrainAccessTab2;
00051                 PG_Label *nameGeneral, *iconGeneral;//, *categoryIcon;
00052                 PG_Label *id, *category, *armor, *view, *jamming, *maxWind, *tankEnery, *tankMat, *tankFuel, *fuelConsuption, *maxSpeed, *weight, *autoRepairRate, *productionCostEnergy, *productionCostMaterial, *productionCostFuel;
00053                 
00054                 PG_Label **movementIcons, *jumpRange, *jumpCost;
00055                 PG_Label **weaponNames, **weaponPunch, **weaponRange, **weaponRechargeCost;
00056                 PG_Label **terrainAccessLabel;
00057 };
00058 
00059 DataTab::DataTab( PG_Widget* parent, const PG_Rect& pos, bool showDescription ) : PG_Widget( parent, pos )
00060 {
00061         currentPage = general;
00062         currentUnit = NULL;
00063         parentPos = pos;
00064         compareData = false;
00065         tabCompare = NULL;
00066         showDescriptionOnly = showDescription;
00067         
00068         int xPos = 10;
00069         int yPos = 10;
00070         int width = parentPos.Width() - 20;
00071         int height = parentPos.Height() - 20;
00072         int lineHeight = 12;
00073         int labelHeight = 12;
00074         //int heightLevelWidth = 20;
00075         int fontSize = 10;
00076         
00077         iconGeneral = new PG_Label( this, PG_Rect( xPos, yPos, 48, 48 ) );
00078         yPos += 55;
00079         nameGeneral = new PG_Label( this, PG_Rect( xPos, yPos, width, labelHeight ) );
00080         yPos += lineHeight;
00081 
00082         movementTab = new PG_Widget( this, PG_Rect( xPos, yPos, width, height ) );
00083         weapons1Tab = new PG_Widget( this, PG_Rect( xPos, yPos, width, height ) );
00084         weapons2Tab = new PG_Widget( this, PG_Rect( xPos, yPos, width, height ) );
00085         productionTab = new PG_Widget( this, PG_Rect( xPos, yPos, width, height ) );
00086         transportTab = new PG_Widget( this, PG_Rect( xPos, yPos, width, height ) );
00087         generalTab = new PG_Widget( this, PG_Rect( xPos, yPos, width, height ) );
00088         terrainAccessTab1 = new PG_Widget( this, PG_Rect( xPos, yPos, width, height ) );
00089         terrainAccessTab2 = new PG_Widget( this, PG_Rect( xPos, yPos, width, height ) );
00090         
00091         HideAllLocal();
00092         generalTab->Show();
00093         
00094         
00095         xPos = 0;
00096         yPos = 0;
00097         
00098         id = new PG_Label( generalTab, PG_Rect( xPos, yPos, width, labelHeight ) );
00099         yPos += lineHeight;
00100         //categoryIcon = new PG_Label( generalTab, PG_Rect( xPos, yPos, 20, 20 ) );
00101         category = new PG_Label ( generalTab, PG_Rect( xPos/* + 30*/, yPos, width - 30, labelHeight ) );
00102         yPos += lineHeight;
00103         armor = new PG_Label( generalTab, PG_Rect( xPos, yPos, width, labelHeight ) );
00104         yPos += lineHeight;
00105         view = new PG_Label( generalTab, PG_Rect( xPos, yPos, width, labelHeight ) );
00106         yPos += lineHeight;
00107         jamming = new PG_Label( generalTab, PG_Rect( xPos, yPos, width, labelHeight ) );
00108         yPos += lineHeight;
00109         maxWind = new PG_Label( generalTab, PG_Rect( xPos, yPos, width, labelHeight ) );
00110         yPos += lineHeight;
00111         tankEnery = new PG_Label( generalTab, PG_Rect( xPos, yPos, width, labelHeight ) );
00112         yPos += lineHeight;
00113         tankMat = new PG_Label( generalTab, PG_Rect( xPos, yPos, width, labelHeight ) );
00114         yPos += lineHeight;
00115         tankFuel = new PG_Label( generalTab, PG_Rect( xPos, yPos, width, labelHeight ) );
00116         yPos += lineHeight;
00117         fuelConsuption = new PG_Label( generalTab, PG_Rect( xPos, yPos, width, labelHeight ) );
00118         yPos += lineHeight;
00119         maxSpeed = new PG_Label( generalTab, PG_Rect( xPos, yPos, width, labelHeight ) );
00120         yPos += lineHeight;
00121         weight = new PG_Label( generalTab, PG_Rect( xPos, yPos, width, labelHeight ) );
00122         yPos += lineHeight;
00123         autoRepairRate = new PG_Label( generalTab, PG_Rect( xPos, yPos, width, labelHeight ) );
00124         yPos += lineHeight;
00125         productionCostEnergy = new PG_Label( generalTab, PG_Rect( xPos, yPos, width, labelHeight ) );
00126         yPos += lineHeight;
00127         productionCostMaterial = new PG_Label( generalTab, PG_Rect( xPos, yPos, width, labelHeight ) );
00128         yPos += lineHeight;
00129         productionCostFuel = new PG_Label( generalTab, PG_Rect( xPos, yPos, width, labelHeight ) );
00130         yPos += lineHeight;
00131         
00132         xPos = 0;
00133         yPos = 0;
00134         
00135         movementIcons =  new PG_Label*[ choehenstufennum ];
00136         
00137         for( int i=0; i<choehenstufennum; i++ )
00138         {
00139                 movementIcons[ i ] = new PG_Label( movementTab, PG_Rect( xPos, yPos, width, labelHeight ) );
00140                 yPos += lineHeight;
00141         }
00142         jumpRange = new PG_Label( movementTab, PG_Rect( xPos, yPos, width, labelHeight ) );
00143         yPos += lineHeight;
00144         jumpCost = new PG_Label( movementTab, PG_Rect( xPos, yPos, width, labelHeight ) ); 
00145         yPos += lineHeight;
00146         
00147         xPos = 0;
00148         yPos = 0;
00149         
00150         weaponNames =  new PG_Label*[ 16 ];
00151         weaponPunch =  new PG_Label*[ 16 ];
00152         weaponRange =  new PG_Label*[ 16 ];
00153         weaponRechargeCost =  new PG_Label*[ 16 ];
00154         for( int i=0; i<8; i++ )
00155         {
00156                 weaponNames[ i ] = new PG_Label( weapons1Tab, PG_Rect( xPos, yPos, width, labelHeight ) );
00157                 yPos += lineHeight;
00158                 weaponPunch[ i ] = new PG_Label( weapons1Tab, PG_Rect( xPos, yPos, width, labelHeight ) );
00159                 yPos += lineHeight;
00160                 weaponRange[ i ] = new PG_Label( weapons1Tab, PG_Rect( xPos, yPos, width, labelHeight ) );
00161                 yPos += lineHeight;
00162                 weaponRechargeCost[ i ] = new PG_Label( weapons1Tab, PG_Rect( xPos, yPos, width, labelHeight ) );
00163                 yPos += lineHeight;
00164         }
00165 
00166         xPos = 0;
00167         yPos = 0;
00168         
00169         for( int i=8; i<16; i++ )
00170         {
00171                 weaponNames[ i ] = new PG_Label( weapons2Tab, PG_Rect( xPos, yPos, width, labelHeight ) );
00172                 yPos += lineHeight;
00173                 weaponPunch[ i ] = new PG_Label( weapons2Tab, PG_Rect( xPos, yPos, width, labelHeight ) );
00174                 yPos += lineHeight;
00175                 weaponRange[ i ] = new PG_Label( weapons2Tab, PG_Rect( xPos, yPos, width, labelHeight ) );
00176                 yPos += lineHeight;
00177                 weaponRechargeCost[ i ] = new PG_Label( weapons2Tab, PG_Rect( xPos, yPos, width, labelHeight ) );
00178                 yPos += lineHeight;
00179         }
00180 
00181         xPos = 0;
00182         yPos = 0;
00183         
00184         terrainAccessLabel =  new PG_Label*[ terrainPropertyNum ];
00185         for( int i=0; i<30; i++ )
00186         {
00187                 terrainAccessLabel[ i ] = new PG_Label( terrainAccessTab1, PG_Rect( xPos, yPos, width, labelHeight ) );
00188                 yPos += lineHeight;
00189         }
00190 
00191         xPos = 0;
00192         yPos = 0;
00193         
00194         for( int i=30; i<terrainPropertyNum; i++ )
00195         {
00196                 terrainAccessLabel[ i ] = new PG_Label( terrainAccessTab2, PG_Rect( xPos, yPos, width, labelHeight ) );
00197                 yPos += lineHeight;
00198         }
00199 
00200         xPos = 0;
00201         yPos = 0;
00202         
00203         SetFontSize( fontSize, true );
00204 }
00205 
00206 DataTab::~DataTab()
00207 {
00208         delete movementIcons;
00209 }
00210 
00211 void DataTab::setTab0( DataTab* tab0 )
00212 {
00213         tabCompare = tab0;
00214 }
00215 
00216 void DataTab::setCompare( bool compare )
00217 {
00218         compareData = compare;
00219         setData();
00220 }
00221 
00222 void DataTab::HideAllLocal()
00223 {
00224         movementTab->Hide();
00225         weapons1Tab->Hide();
00226         weapons2Tab->Hide();
00227         productionTab->Hide();
00228         transportTab->Hide();
00229         terrainAccessTab1->Hide();
00230         terrainAccessTab2->Hide();
00231         generalTab->Hide();
00232 }
00233 
00234 void DataTab::setPage( Page page )
00235 {
00236         currentPage = page;
00237         HideAllLocal();
00238         switch( page )
00239         {
00240                 case movement:
00241                         movementTab->Show();
00242                         break;
00243                 case weapons1:
00244                         weapons1Tab->Show();
00245                         break;
00246                 case weapons2:
00247                         weapons2Tab->Show();
00248                         break;
00249                 case production:
00250                         productionTab->Show();
00251                         break;
00252                 case transport:
00253                         transportTab->Show();
00254                         break;
00255                 case terrainAccess1:
00256                         terrainAccessTab1->Show();
00257                         break;
00258                 case terrainAccess2:
00259                         terrainAccessTab2->Show();
00260                         break;
00261                 default:
00262                         generalTab->Show();
00263                         break;
00264         }
00265 }
00266 
00267 void DataTab::setUnit( VehicleType* type )
00268 {
00269         currentUnit = type;
00270         setData();
00271 }
00272 
00273 void DataTab::setData()
00274 {
00275         
00276         
00277         if( currentUnit == NULL && ! showDescriptionOnly )
00278                 return;
00279         
00280         VehicleType* compareType = NULL;
00281         if( compareData && tabCompare != NULL )
00282         {
00283                 compareType = tabCompare->currentUnit;
00284         }
00285         
00286         char buffer[ 500 ];
00287         std::string value = "";
00288 
00289         if( showDescriptionOnly )
00290         {
00291                 iconGeneral->SetText( "Icon" );
00292                 nameGeneral->SetText( "Name" );
00293                 id->SetText( "ID" );
00294                 category->SetText( "Category" );
00295                 armor->SetText( "Armor" );
00296                 view->SetText( "View" );
00297                 jamming->SetText( "Jamming" );
00298                 maxWind->SetText( "Max Wind Resistance" );
00299                 tankEnery->SetText( "Tank Energy ASC|BI" );
00300                 tankMat->SetText( "Tank Material ASC|BI" );
00301                 tankFuel->SetText( "Tank Fuel ASC|BI" );
00302                 fuelConsuption->SetText( "Fuel Consumption" );
00303                 maxSpeed->SetText( "Max Speed" );
00304                 weight->SetText( "Weight" );
00305                 autoRepairRate->SetText( "Auto Repair Rate" );
00306                 productionCostEnergy->SetText( "Production Cost Energy" );
00307                 productionCostMaterial->SetText( "Production Cost Material" );
00308                 productionCostFuel->SetText( "Production Cost Fuel" );
00309                 
00310                 for( int i=0; i<choehenstufennum; i++ )
00311                 {
00312                         std::string iconName = "height-a";
00313                         char buffer[ 5 ];
00314                         itoa( i, buffer, 10 );
00315                         iconName += buffer;
00316                         iconName += ".png";
00317                         movementIcons[ i ]->SetIcon( IconRepository::getIcon( iconName ).getBaseSurface() );
00318                 }
00319                 
00320                 for( int i=0; i<16; i++ )
00321                 {
00322                         value = "Weapon ";
00323                         itoa( i, buffer, 10 );
00324                         value += buffer;
00325                         value += " Name";
00326                         weaponNames[ i ]->SetText( value );
00327                         weaponPunch[ i ]->SetText( "Punch" );
00328                         weaponRange[ i ]->SetText( "Range" );
00329                         weaponRechargeCost[ i ]->SetText( "Laser Recharge" );
00330                 }
00331                 
00332                 for( int i=0; i<terrainPropertyNum; i++ )
00333                 {
00334                         terrainAccessLabel[ i ]->SetText( terrainProperty[ i ] );
00335                 }
00336                 
00337                 jumpRange->SetText( "Jump Range" );
00338                 jumpCost->SetText( "Jump Cost" );
00339                 
00340         }else
00341         {
00342                 iconGeneral->SetIcon( currentUnit->getImage().getBaseSurface() );
00343                 nameGeneral->SetText( currentUnit->getName() );
00344                 itoa( currentUnit->getID(), buffer, 10 );
00345                 id->SetText( buffer );
00346                 //categoryIcon->SetIcon( IconRepository::getIcon( moveMaliTypeIcons[ currentUnit->movemalustyp ] ).getBaseSurface() );
00347                 category->SetText( cmovemalitypes[ currentUnit->movemalustyp ] );               
00348 
00349                 {
00350                         int armor = currentUnit->armor;
00351                         if( compareType != NULL )
00352                         {
00353                                 armor = armor - compareType->armor;
00354                         }
00355                         itoa( armor, buffer, 10 );
00356                         this->armor->SetText( buffer );
00357                 }
00358                 
00359                 {
00360                         int view = currentUnit->view;
00361                         if( compareType != NULL )
00362                         {
00363                                 view = view - compareType->view;
00364                         }
00365                         itoa( view, buffer, 10 );
00366                         this->view->SetText( buffer );
00367                 }
00368                 
00369                 {
00370                         int jamming = currentUnit->jamming;
00371                         if( compareType != NULL )
00372                         {
00373                                 jamming = jamming - compareType->jamming;
00374                         }
00375                         itoa( jamming, buffer, 10 );
00376                         this->jamming->SetText( buffer );
00377                 }
00378 
00379                 {
00380                         int maxwindspeedonwater = currentUnit->maxwindspeedonwater;
00381                         if( compareType != NULL )
00382                         {
00383                                 maxwindspeedonwater = maxwindspeedonwater - compareType->maxwindspeedonwater;
00384                         }
00385                         itoa( maxwindspeedonwater, buffer, 10 );
00386                         this->maxWind->SetText( buffer );
00387                 }
00388                 
00389                 {
00390                         Resources biTank = currentUnit->getStorageCapacity( 1 );
00391                         Resources ascTank = currentUnit->getStorageCapacity( 0 );
00392                         value = "";
00393 
00394                         int tank = ascTank.resource( 0 );
00395                         if( compareType != NULL )
00396                         {
00397                                 tank = tank - compareType->getStorageCapacity( 0 ).resource( 0 );
00398                         }
00399                         itoa( tank, buffer, 10 );
00400                         
00401                         value += buffer;
00402                         value += " | ";
00403                         
00404                         tank = biTank.resource( 0 );
00405                         if( compareType != NULL )
00406                         {
00407                                 tank = tank - compareType->getStorageCapacity( 1 ).resource( 0 );
00408                         }
00409                         itoa( tank, buffer, 10 );
00410                         
00411                         value += buffer;
00412                         this->tankEnery->SetText( value );
00413                         
00414                         value = "";
00415                         tank = ascTank.resource( 1 );
00416                         if( compareType != NULL )
00417                         {
00418                                 tank = tank - compareType->getStorageCapacity( 0 ).resource( 1 );
00419                         }
00420                         itoa( tank, buffer, 10 );
00421                         
00422                         value += buffer;
00423                         value += " | ";
00424                         
00425                         tank = biTank.resource( 1 );
00426                         if( compareType != NULL )
00427                         {
00428                                 tank = tank - compareType->getStorageCapacity( 1 ).resource( 1 );
00429                         }
00430                         itoa( tank, buffer, 10 );
00431                         
00432                         value += buffer;
00433                         this->tankMat->SetText( value );
00434 
00435                         
00436                         value = "";
00437                         tank = ascTank.resource( 2 );
00438                         if( compareType != NULL )
00439                         {
00440                                 tank = tank - compareType->getStorageCapacity( 0 ).resource( 2 );
00441                         }
00442                         itoa( tank, buffer, 10 );
00443                         
00444                         value += buffer;
00445                         value += " | ";
00446                         
00447                         tank = biTank.resource( 2 );
00448                         if( compareType != NULL )
00449                         {
00450                                 tank = tank - compareType->getStorageCapacity( 1 ).resource( 2 );
00451                         }
00452                         itoa( tank, buffer, 10 );
00453                         
00454                         value += buffer;
00455                         this->tankFuel->SetText( value );
00456                 }
00457 
00458                 {
00459                         int fuelConsumption = currentUnit->fuelConsumption;
00460                         if( compareType != NULL )
00461                         {
00462                                 fuelConsumption = fuelConsumption - compareType->fuelConsumption;
00463                         }
00464                         itoa( fuelConsumption, buffer, 10 );
00465                         this->fuelConsuption->SetText( buffer );
00466                 }
00467 
00468                 {
00469                         int movement = currentUnit->maxSpeed();
00470                         if( compareType != NULL )
00471                         {
00472                                 movement = movement - compareType->maxSpeed();
00473                         }
00474                         itoa( movement, buffer, 10 );
00475                         this->maxSpeed->SetText( buffer );
00476                 }
00477 
00478                 {
00479                         int weight = currentUnit->weight;
00480                         if( compareType != NULL )
00481                         {
00482                                 weight = weight - compareType->weight;
00483                         }
00484                         itoa( weight, buffer, 10 );
00485                         this->weight->SetText( buffer );
00486                 }
00487 
00488                 {
00489                         int autorepairrate = currentUnit->autorepairrate;
00490                         if( compareType != NULL )
00491                         {
00492                                 autorepairrate = autorepairrate - compareType->autorepairrate;
00493                         }
00494                         itoa( autorepairrate, buffer, 10 );
00495                         this->autoRepairRate->SetText( buffer );
00496                 }
00497 
00498                 
00499                 {
00500                         int tank = currentUnit->calcProductionCost().resource( 0 );
00501                         if( compareType != NULL )
00502                         {
00503                                 tank = tank - compareType->calcProductionCost().resource( 0 );
00504                         }
00505                         itoa( tank, buffer, 10 );
00506                         
00507                         this->productionCostEnergy->SetText( buffer );
00508                 }
00509 
00510                 {
00511                         int tank = currentUnit->calcProductionCost().resource( 1 );
00512                         if( compareType != NULL )
00513                         {
00514                                 tank = tank - compareType->calcProductionCost().resource( 1 );
00515                         }
00516                         itoa( tank, buffer, 10 );
00517                         
00518                         this->productionCostMaterial->SetText( buffer );
00519                 }
00520 
00521                 {
00522                         int tank = currentUnit->calcProductionCost().resource( 2 );
00523                         if( compareType != NULL )
00524                         {
00525                                 tank = tank - compareType->calcProductionCost().resource( 2 );
00526                         }
00527                         itoa( tank, buffer, 10 );
00528                         
00529                         this->productionCostFuel->SetText( buffer );
00530                 }
00531 
00532                 for( int i=0; i<choehenstufennum; i++ )
00533                 {
00534                         int movement = currentUnit->movement[ i ];
00535                         if( compareType != NULL )
00536                         {
00537                                 movement = movement - compareType->movement[ i ];
00538                         }
00539                         itoa( movement, buffer, 10 );
00540                         value = buffer;
00541                         if( currentUnit->jumpDrive.height & (1<<i) )
00542                                 value += " Jump";
00543                         movementIcons[ i ]->SetText( value );
00544                 }
00545                 
00546                 {
00547                         int maxDistance = currentUnit->jumpDrive.maxDistance;
00548                         if( compareType != NULL )
00549                         {
00550                                 maxDistance = maxDistance - compareType->jumpDrive.maxDistance;
00551                         }
00552                         maxDistance /= 10;
00553                         itoa( maxDistance, buffer, 10 );
00554                         this->jumpRange->SetText( buffer );
00555                 }
00556                 
00557                 for( int i=0; i<16; i++ )
00558                 {
00559                         weaponNames[ i ]->SetText( "N/A" );
00560                         weaponPunch[ i ]->SetText( "0-0" );
00561                         weaponRange[ i ]->SetText( "0-0" );
00562                         weaponRechargeCost[ i ]->SetText( "0x 0|0|0" );
00563                 }
00564                 
00565                 for( int i=0; i<currentUnit->weapons.count; i++ )
00566                 {
00567                         weaponNames[ i ]->SetText( currentUnit->weapons.weapon[ i ].name );
00568                         
00569                         value = "";
00570                         int maxPunch = currentUnit->weapons.weapon[ i ].maxstrength;
00571                         if( compareType != NULL )
00572                         {
00573                                 maxPunch = maxPunch - compareType->weapons.weapon[ i ].maxstrength;
00574                         }
00575                         itoa( maxPunch, buffer, 10 );
00576                         value = buffer;
00577                         value += "-";
00578                         int minPunch = currentUnit->weapons.weapon[ i ].minstrength;
00579                         if( compareType != NULL )
00580                         {
00581                                 minPunch = minPunch - compareType->weapons.weapon[ i ].minstrength;
00582                         }
00583                         itoa( minPunch, buffer, 10 );
00584                         value += buffer;
00585                         weaponPunch[ i ]->SetText( value );
00586                         
00587                         value = "";
00588                         int minRange = currentUnit->weapons.weapon[ i ].mindistance;
00589                         if( compareType != NULL )
00590                         {
00591                                 minRange = minRange - compareType->weapons.weapon[ i ].mindistance;
00592                         }
00593                         minRange /= 10;
00594                         itoa( minRange, buffer, 10 );
00595                         value = buffer;
00596                         value += "-";
00597                         int maxRange = currentUnit->weapons.weapon[ i ].maxdistance;
00598                         if( compareType != NULL )
00599                         {
00600                                 maxRange = maxRange - compareType->weapons.weapon[ i ].maxdistance;
00601                         }
00602                         maxRange /= 10;
00603                         itoa( maxRange, buffer, 10 );
00604                         value += buffer;
00605                         weaponRange[ i ]->SetText( value );
00606                         
00607                         value = "";
00608                         int rechargeRate = currentUnit->weapons.weapon[ i ].laserRechargeRate;
00609                         if( compareType != NULL )
00610                         {
00611                                 rechargeRate = rechargeRate - compareType->weapons.weapon[ i ].laserRechargeRate;
00612                         }
00613                         itoa( rechargeRate, buffer, 10 );
00614                         value = buffer;
00615                         value += "x  ";
00616                         int costEnergy = currentUnit->weapons.weapon[ i ].laserRechargeCost.resource( 0 );
00617                         if( compareType != NULL )
00618                         {
00619                                 costEnergy = costEnergy - compareType->weapons.weapon[ i ].laserRechargeCost.resource( 0 );
00620                         }
00621                         itoa( costEnergy, buffer, 10 );
00622                         value += buffer;
00623                         value += "|";
00624                         int costMaterial = currentUnit->weapons.weapon[ i ].laserRechargeCost.resource( 1 );
00625                         if( compareType != NULL )
00626                         {
00627                                 costMaterial = costMaterial - compareType->weapons.weapon[ i ].laserRechargeCost.resource( 1 );
00628                         }
00629                         itoa( costMaterial, buffer, 10 );
00630                         value += buffer;
00631                         value += "|";
00632                         int costFuel = currentUnit->weapons.weapon[ i ].laserRechargeCost.resource( 2 );
00633                         if( compareType != NULL )
00634                         {
00635                                 costFuel = costFuel - compareType->weapons.weapon[ i ].laserRechargeCost.resource( 2 );
00636                         }
00637                         itoa( costFuel, buffer, 10 );
00638                         value += buffer;
00639                         weaponRechargeCost[ i ]->SetText( value );
00640                 }
00641 
00642                 for( int i=0; i<terrainPropertyNum; i++ )
00643                 {
00644                         if( currentUnit->terrainaccess.terrainkill[ i ] )
00645                         {
00646                                 terrainAccessLabel[ i ]->SetText( "kill" );
00647                         }else if( currentUnit->terrainaccess.terrainnot[ i ] )
00648                         {
00649                                 terrainAccessLabel[ i ]->SetText( "not accessible" );
00650                         }else if( currentUnit->terrainaccess.terrainreq[ i ] )
00651                         {
00652                                 terrainAccessLabel[ i ]->SetText( "required" );
00653                         }else if( currentUnit->terrainaccess.terrain[ i ] )
00654                         {
00655                                 terrainAccessLabel[ i ]->SetText( "yes" );
00656                         }else
00657                         {
00658                                 terrainAccessLabel[ i ]->SetText( "" );
00659                         }
00660                         
00661                         // narf!
00662                         if( currentUnit->jumpDrive.height != 0 )
00663                         {
00664                                 if( currentUnit->jumpDrive.targetterrain.terrainreq[ i ] )
00665                                 {
00666                                         value = terrainAccessLabel[ i ]->GetText();
00667                                         value += " JumpReq";
00668                                         terrainAccessLabel[ i ]->SetText( value );
00669                                 }else if( currentUnit->jumpDrive.targetterrain.terrain[ i ] )
00670                                 {
00671                                         value = terrainAccessLabel[ i ]->GetText();
00672                                         value += " Jump";
00673                                         terrainAccessLabel[ i ]->SetText( value );
00674                                 }
00675                         }
00676                 }
00677                 
00678                 {
00679                         value = "";
00680                         int cost = currentUnit->jumpDrive.consumption.resource( 0 );
00681                         if( compareType != NULL )
00682                         {
00683                                 cost = cost - compareType->jumpDrive.consumption.resource( 0 );
00684                         }
00685                         itoa( cost, buffer, 10 );
00686                         value += buffer;
00687                         value += "|";
00688                         
00689                         cost = currentUnit->jumpDrive.consumption.resource( 1 );
00690                         if( compareType != NULL )
00691                         {
00692                                 cost = cost - compareType->jumpDrive.consumption.resource( 1 );
00693                         }
00694                         itoa( cost, buffer, 10 );
00695                         value += buffer;
00696                         value += "|";
00697                         
00698                         cost = currentUnit->jumpDrive.consumption.resource( 2 );
00699                         if( compareType != NULL )
00700                         {
00701                                 cost = cost - compareType->jumpDrive.consumption.resource( 2 );
00702                         }
00703                         itoa( cost, buffer, 10 );
00704                         value += buffer;
00705                         
00706                         jumpCost->SetText( value );
00707                 }
00708         }
00709                 
00710         
00711 }
00712 
00713 
00714 
00715 class UnitGuideWidget : public PG_Widget
00716 {
00717         private:
00718     DropDownSelector* mainCategory;
00719     DropDownSelector* subCategory;
00720                 
00721     DropDownSelector* unitSet;
00722     DropDownSelector* displayTab;
00723     DropDownSelector* unit;
00724     DropDownSelector* page;
00725                 
00726                 PG_Button* display;
00727                 PG_CheckButton* showDifference;
00728 
00729                 PG_Label* mainCategoryLabel;
00730                 PG_Label* subCategoryLabel;
00731                 PG_Label* unitSetLabel;
00732                 PG_Label* displayTabLabel;
00733                 PG_Label* unitLabel;
00734                 PG_Label* showDifferenceLabel;
00735                 
00736                 PG_ScrollArea* content;
00737                 
00738                 DataTab* descriptionTab;
00739                 //--------------------------
00740                 struct TSubCategory{
00741                         int category;
00742                         std::string name;
00743                 };
00744                 typedef std::vector< TSubCategory > TSubCategories;
00745                 
00746                 struct TMainCategory{
00747                         std::string name;
00748                         TSubCategories subCategories;
00749                 };
00750                 typedef std::vector< TMainCategory > TMainCategories;
00751                 
00752                 TMainCategories mainCategories;
00753                 //--------------------------
00754                 
00755                 void loadCategories();
00756                 
00757                 void selectUnits();
00758                 
00759                 // signal connectors
00760                 void mainCategoryChanged( int i );
00761                 void subCategoryChanged( int i );
00762                 void unitSetChanged( int i );
00763                 void pageChanged( int i );
00764                 bool displayUnit();
00765                 bool showDifferenceTrigger( bool pressed );
00766                 //---------------------------
00767                 typedef std::vector< VehicleType* > TVehicleTypes;
00768                 TVehicleTypes selectedUnits;
00769                 
00770                 typedef std::vector< DataTab* > TDataTabs;
00771                 TDataTabs dataTabs;
00772                 
00773         public:
00774                 UnitGuideWidget( PG_Widget* parent, const PG_Rect& pos, int tabs );
00775 };
00776 
00777 
00778 
00779 UnitGuideWidget::UnitGuideWidget( PG_Widget* parent, const PG_Rect& pos, int tabs ) : PG_Widget( parent, pos )
00780 {
00781         loadCategories();
00782         
00783         int xPos = 20;
00784         int yPos = 20;
00785         
00786         mainCategoryLabel = new PG_Label ( this, PG_Rect( xPos, yPos, 100, 20 ), "Main Category" );
00787         xPos += 120;
00788         
00789         mainCategory = new DropDownSelector( this, PG_Rect( xPos, yPos, 200, 20 ) );
00790         for( int i=0; i<mainCategories.size(); i++ )
00791         {
00792                 mainCategory->AddItem( mainCategories[ i ].name );
00793         }
00794         mainCategory->selectionSignal.connect( SigC::slot( *this, &UnitGuideWidget::mainCategoryChanged ) );
00795         xPos += 220;
00796         
00797         unitSetLabel = new PG_Label ( this, PG_Rect( xPos, yPos, 100, 20 ), "Unit Set" );
00798         xPos += 120;
00799         
00800         unitSet = new DropDownSelector( this, PG_Rect( xPos, yPos, 200, 20 ) );
00801         unitSet->AddItem( "All Sets" );
00802         for( int i=0; i<unitSets.size(); i++ )
00803         {
00804                 unitSet->AddItem( unitSets[ i ]->name );
00805         }
00806         unitSet->selectionSignal.connect( SigC::slot( *this, &UnitGuideWidget::unitSetChanged ) );
00807         xPos = 20;
00808         yPos += 25;
00809         
00810         
00811         subCategoryLabel = new PG_Label ( this, PG_Rect( xPos, yPos, 100, 20 ), "Sub Category" );
00812         xPos += 120;
00813         
00814         subCategory = new DropDownSelector( this, PG_Rect( xPos, yPos, 200, 20 ) );
00815         subCategory->selectionSignal.connect( SigC::slot( *this, &UnitGuideWidget::subCategoryChanged ) );
00816         xPos += 220;
00817         
00818         unitLabel = new PG_Label ( this, PG_Rect( xPos, yPos, 100, 20 ), "Unit" );
00819         xPos += 120;
00820         
00821         unit = new DropDownSelector( this, PG_Rect( xPos, yPos, 200, 20 ) );
00822         xPos = 20;
00823         yPos += 25;
00824         
00825         displayTabLabel = new PG_Label ( this, PG_Rect( xPos, yPos, 100, 20 ), "Display on tab" );
00826         xPos += 120;
00827         
00828         displayTab = new DropDownSelector( this, PG_Rect( xPos, yPos, 50, 20 ) );
00829         for( int i=0; i<tabs; i++ )
00830         {
00831                 std::stringstream nameStream;
00832                 nameStream << i;
00833                 displayTab->AddItem( nameStream.str() );
00834         }
00835         xPos += 70;
00836         
00837         showDifferenceLabel = new PG_Label ( this, PG_Rect( xPos, yPos, 100, 20 ), "show difference" );
00838         xPos += 120;
00839         
00840         showDifference = new PG_CheckButton( this, PG_Rect( xPos, yPos, 20, 20 ) );
00841   showDifference->sigClick.connect( SigC::slot( *this, &UnitGuideWidget::showDifferenceTrigger ));
00842         xPos += 40;
00843         
00844         display = new PG_Button( this, PG_Rect( xPos, yPos, 100, 20 ), "display" );
00845   display->sigClick.connect( SigC::slot( *this, &UnitGuideWidget::displayUnit ));
00846         xPos += 120;
00847         
00848         page = new DropDownSelector( this, PG_Rect( xPos, yPos, 200, 20 ) );
00849         page->AddItem( "General" );
00850         page->AddItem( "Weapons 1" );
00851         page->AddItem( "Weapons 2" );
00852         page->AddItem( "Movement" );
00853         page->AddItem( "Transport" );
00854         page->AddItem( "TerrainAccess 1" );
00855         page->AddItem( "TerrainAccess 2" );
00856         page->AddItem( "Production" );
00857         page->selectionSignal.connect( SigC::slot( *this, &UnitGuideWidget::pageChanged ) );
00858         
00859         xPos = 0;
00860         yPos += 25;
00861         /*
00862         content = new PG_ScrollArea( this, PG_Rect( xPos, yPos, pos.Width(), pos.Height() - yPos - 10 ) );
00863         content->ScrollTo( 0, 0 );
00864         */
00865         descriptionTab = new DataTab( this, PG_Rect( xPos, yPos, 200, pos.Height() - yPos - 10 ), true );
00866         descriptionTab->setData();
00867 
00868         xPos += 200;
00869         for( int i=0; i<tabs; i++ )
00870         {
00871                 DataTab* dataTab = new DataTab( this, PG_Rect( xPos, yPos, 150, pos.Height() - yPos - 10 ), false );
00872                 dataTabs.push_back( dataTab );
00873                 xPos += 150;
00874         }
00875         for( int i=1; i<tabs; i++ )
00876         {
00877                 dataTabs[ i ]->setTab0( dataTabs[ 0 ] );
00878         }
00879         mainCategory->SelectFirstItem();
00880         unitSet->SelectFirstItem();
00881         
00882 }
00883 
00884 bool UnitGuideWidget::showDifferenceTrigger( bool pressed )
00885 {
00886         //Hide();
00887         for( int i=1; i<dataTabs.size(); i++ )
00888         {
00889                 dataTabs[ i ]->setCompare( pressed );
00890         }
00891         //Show();
00892         return true;
00893 }
00894 
00895 void UnitGuideWidget::pageChanged( int i )
00896 {
00897         //Hide();
00898         switch( i )
00899         {
00900                 case 0: 
00901                         for( int tab=0; tab<dataTabs.size(); tab++ )
00902                                 dataTabs[ tab ]->setPage( DataTab::general );
00903                         descriptionTab->setPage( DataTab::general );
00904                         break;
00905                 case 1:
00906                         for( int tab=0; tab<dataTabs.size(); tab++ )
00907                                 dataTabs[ tab ]->setPage( DataTab::weapons1 );
00908                         descriptionTab->setPage( DataTab::weapons1 );
00909                         break;
00910                 case 2:
00911                         for( int tab=0; tab<dataTabs.size(); tab++ )
00912                                 dataTabs[ tab ]->setPage( DataTab::weapons2 );
00913                         descriptionTab->setPage( DataTab::weapons2 );
00914                         break;
00915                 case 3:
00916                         for( int tab=0; tab<dataTabs.size(); tab++ )
00917                                 dataTabs[ tab ]->setPage( DataTab::movement );
00918                         descriptionTab->setPage( DataTab::movement );
00919                         break;
00920                 case 4:
00921                         for( int tab=0; tab<dataTabs.size(); tab++ )
00922                                 dataTabs[ tab ]->setPage( DataTab::transport );
00923                         descriptionTab->setPage( DataTab::transport );
00924                         break;
00925                 case 5:
00926                         for( int tab=0; tab<dataTabs.size(); tab++ )
00927                                 dataTabs[ tab ]->setPage( DataTab::terrainAccess1 );
00928                         descriptionTab->setPage( DataTab::terrainAccess1 );
00929                         break;
00930                 case 6:
00931                         for( int tab=0; tab<dataTabs.size(); tab++ )
00932                                 dataTabs[ tab ]->setPage( DataTab::terrainAccess2 );
00933                         descriptionTab->setPage( DataTab::terrainAccess2 );
00934                         break;
00935                 case 7:
00936                         for( int tab=0; tab<dataTabs.size(); tab++ )
00937                                 dataTabs[ tab ]->setPage( DataTab::production );
00938                         descriptionTab->setPage( DataTab::production );
00939                         break;
00940                 default:
00941                         for( int tab=0; tab<dataTabs.size(); tab++ )
00942                                 dataTabs[ tab ]->setPage( DataTab::general );
00943                         descriptionTab->setPage( DataTab::general );
00944                         break;
00945         }
00946         //Show();
00947 }
00948 
00949 bool UnitGuideWidget::displayUnit()
00950 {
00951 //      Hide();
00952         dataTabs[ displayTab->GetSelectedItemIndex() ]->setUnit( selectedUnits[ unit->GetSelectedItemIndex() ] );
00953         
00954         if( showDifference->GetPressed() && displayTab->GetSelectedItemIndex() == 0 )
00955         {
00956                 for( int i=1; i<dataTabs.size(); i++ )
00957                         dataTabs[ i ]->setData();
00958         }
00959         
00960 //      Show();
00961         return true;
00962 }
00963 
00964 void UnitGuideWidget::selectUnits()
00965 {
00966         selectedUnits.clear();
00967         int allUnits = actmap->getVehicleTypeNum();
00968         int filter = mainCategories[ mainCategory->GetSelectedItemIndex() ].subCategories[ subCategory->GetSelectedItemIndex() ].category;
00969         
00970         SingleUnitSet* selectedUnitSet = NULL;
00971         if( unitSet->GetSelectedItemIndex() > 0 )
00972                 selectedUnitSet = unitSets[ unitSet->GetSelectedItemIndex() - 1 ];
00973         
00974         
00975         for( int i=0; i<allUnits; i++ )
00976         {
00977                 VehicleType *type = actmap->getvehicletype_bypos( i );
00978                 
00979                 if( selectedUnitSet != NULL )
00980                 {
00981                         if( ! selectedUnitSet->isMember( type->id, SingleUnitSet::unit ) )
00982                                 continue;
00983                 }
00984                 
00985                 for( int j=0; j<type->guideSortHelp.size(); j++ )
00986                 {
00987                         if( type->guideSortHelp[ j ] == filter )
00988                         {
00989                                 // yay!
00990                                 selectedUnits.push_back( type );
00991                                 
00992                                 break;
00993                         }
00994                 }
00995         }
00996         
00997         unit->DeleteAll();
00998         for( int i=0; i<selectedUnits.size(); i++ )
00999         {
01000                 unit->AddItem( selectedUnits[ i ]->getName() );
01001         }
01002         unit->SelectFirstItem();
01003 }
01004 
01005 void UnitGuideWidget::mainCategoryChanged( int i )
01006 {
01007         subCategory->DeleteAll();
01008         
01009         TSubCategories subCategories = mainCategories[ i ].subCategories;
01010         for( int j=0; j<subCategories.size(); j++ )
01011         {
01012                 subCategory->AddItem( subCategories[ j ].name );
01013         }
01014         subCategory->SelectFirstItem();
01015 }
01016 
01017 void UnitGuideWidget::subCategoryChanged( int i )
01018 {
01019         selectUnits();
01020 }
01021 
01022 void UnitGuideWidget::unitSetChanged( int i )
01023 {
01024         selectUnits();
01025 }
01026 
01027 
01028 void UnitGuideWidget::loadCategories()
01029 {
01030         ASCString lineBuffer;
01031         tnfilestream file( "guidesorthelp.properties", tnstream::reading );
01032 
01033         TMainCategory mainCategory;
01034         while( file.readTextString( lineBuffer ) )
01035         {
01036                 std::string line = lineBuffer;
01037                 boost::algorithm::trim( line );
01038 
01039                 if( line.size() == 0 ) 
01040                         continue;
01041                 
01042                 if( line[ 0 ] == '[' )
01043                 {
01044                         if( mainCategory.subCategories.size() > 0 )
01045                         {
01046                                 mainCategories.push_back( mainCategory );
01047                         }
01048                         mainCategory.name = line.substr( 1, line.length() - 2 );
01049                         mainCategory.subCategories.clear();
01050                 }else
01051                 {
01052                         if( line.find( "=" ) > 0 )
01053                         {
01054                                 std::string key = line.substr( 0, line.find( "=" ) );
01055                                 line = line.substr( line.find( "=" ) + 1 );
01056                                 boost::algorithm::trim( key );
01057                                 boost::algorithm::trim( line );
01058                                 TSubCategory subCat;
01059                                 subCat.name = line;
01060                                 subCat.category = atoi( key );
01061                                 mainCategory.subCategories.push_back( subCat );
01062                         }
01063                 }
01064                 
01065         }
01066         if( mainCategory.subCategories.size() > 0 )
01067         {
01068                 mainCategories.push_back( mainCategory );
01069         }
01070         /*
01071         TSubCategory subCategory;
01072         subCategory.name = "Heavy Trooper";
01073         subCategory.category = 203;
01074         
01075         TSubCategories infantry;
01076         infantry.push_back( subCategory );
01077 
01078         subCategory.name = "jumps";
01079         subCategory.category = 505;
01080         infantry.push_back( subCategory );
01081         
01082         TMainCategory mainCategory;
01083         mainCategory.name = "Infantry";
01084         mainCategory.subCategories = infantry;
01085         
01086         mainCategories.push_back( mainCategory );
01087 
01088         TSubCategories tanks;
01089         subCategory.name = "A-Sat Multi";
01090         subCategory.category = 232;
01091         tanks.push_back( subCategory );
01092 
01093         mainCategory.name = "Sats";
01094         mainCategory.subCategories = tanks;
01095         
01096         mainCategories.push_back( mainCategory );
01097         */
01098 }
01099 
01100 class UnitGuideWindow : public ASC_PG_Dialog
01101 {
01102         private:
01103                 UnitGuideWidget *w;
01104         public: 
01105                 UnitGuideWindow( PG_Widget* parent, const PG_Rect& pos, int tabs );
01106                 ~UnitGuideWindow();
01107 };
01108 
01109 UnitGuideWindow::UnitGuideWindow( PG_Widget* parent, const PG_Rect& pos, int tabs ) : ASC_PG_Dialog( parent, pos, "UnitGuide" )
01110 {
01111         w = new UnitGuideWidget( this, PG_Rect( 0, 0, pos.Width(), pos.Height() ), tabs );
01112 }
01113 
01114 UnitGuideWindow::~UnitGuideWindow()
01115 {
01116         delete w;
01117 }
01118 
01119 void unitGuideWindow( int tabs )
01120 {
01121    try {
01122       UnitGuideWindow w( NULL, PG_Rect( 50, 50, 760, 600 ), tabs );
01123       w.Show();
01124       w.RunModal();
01125    } catch ( tfileerror err ) {
01126       errorMessage("error loading file " + err.getFileName() );
01127    }
01128 }

Generated on Mon May 21 01:26:39 2012 for Advanced Strategic Command by  doxygen 1.5.1