00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "vehicletypeselector.h"
00019 #include "selectionwindow.h"
00020 #include "unitinfodialog.h"
00021
00022 #include "../vehicletype.h"
00023 #include "../iconrepository.h"
00024 #include "../spfst.h"
00025 #include "../unitset.h"
00026
00027 VehicleTypeBaseWidget :: VehicleTypeBaseWidget( PG_Widget* parent, const PG_Point& pos, int width, const VehicleType* vehicletype, const Player& player ) : SelectionWidget( parent, PG_Rect( pos.x, pos.y, width, fieldsizey+10 )), vt( vehicletype ), actplayer(player)
00028 {
00029 int col1 = 50;
00030 int lineheight = 20;
00031
00032 int sw = (width - col1 - 10) / 6;
00033
00034 PG_Label* lbl1 = new PG_Label( this, PG_Rect( col1, 0, 3 * sw, lineheight ), vt->name );
00035 lbl1->SetFontSize( lbl1->GetFontSize() -2 );
00036
00037 PG_Label* lbl2 = new PG_Label( this, PG_Rect( col1, lineheight, 3 * sw, lineheight ), vt->description );
00038 lbl2->SetFontSize( lbl2->GetFontSize() -2 );
00039
00040 PG_Button* b = new PG_Button( this, PG_Rect( buttonXPos(width, 0 ), Height()/2-lineheight, 2*lineheight, 2*lineheight ));
00041 b->SetIcon( IconRepository::getIcon( "blue-i.png").getBaseSurface() );
00042 b->sigClick.connect( SigC::slot( *this, &VehicleTypeBaseWidget::info ));
00043
00044 SetTransparency( 255 );
00045 };
00046
00047 int VehicleTypeBaseWidget::buttonXPos( int width, int num )
00048 {
00049 int col1 = 50;
00050 int sw = (width - col1 - 10) / 6;
00051 return col1 + 3 * sw + (10 + sw) * num;
00052 }
00053
00054
00055
00056 bool VehicleTypeBaseWidget::info()
00057 {
00058 unitInfoDialog( vt );
00059 return true;
00060 }
00061
00062
00063 ASCString VehicleTypeBaseWidget::getName() const
00064 {
00065 return vt->getName();
00066 };
00067
00068 void VehicleTypeBaseWidget::display( SDL_Surface * surface, const PG_Rect & src, const PG_Rect & dst )
00069 {
00070 if ( !getClippingSurface().valid() )
00071 getClippingSurface() = Surface::createSurface( fieldsizex + 10, fieldsizey + 10, 32, 0 );
00072
00073 getClippingSurface().Fill(0);
00074
00075 vt->paint( getClippingSurface(), SPoint(5,5), actplayer.getPlayerColor(), 0 );
00076 PG_Draw::BlitSurface( getClippingSurface().getBaseSurface(), src, surface, dst);
00077 }
00078
00079 Surface VehicleTypeBaseWidget::clippingSurface;
00080
00081 VehicleBaseWidget :: VehicleBaseWidget( PG_Widget* parent, const PG_Point& pos, int width, const Vehicle* vehicle, const Player& player ) : SelectionWidget( parent, PG_Rect( pos.x, pos.y, width, fieldsizey+10 )), v( vehicle ), actplayer(player)
00082 {
00083 int col1 = 50;
00084 int lineheight = 18;
00085
00086 int sw = (width - col1 - 10) / 6;
00087
00088 PG_Label* lbl1 = new PG_Label( this, PG_Rect( col1, 0, 3 * sw, lineheight ), v->typ->name );
00089 lbl1->SetFontSize( lbl1->GetFontSize() -2 );
00090
00091 PG_Label* lbl2 = new PG_Label( this, PG_Rect( col1, lineheight, 3 * sw, lineheight ), v->typ->description );
00092 lbl2->SetFontSize( lbl2->GetFontSize() -3 );
00093
00094 if ( v->getName() != v->typ->name ) {
00095 PG_Label* lbl3 = new PG_Label( this, PG_Rect( col1, lineheight * 2, 3 * sw, lineheight), '>'+v->getName()+'<');
00096 lbl3->SetFontSize( lbl3->GetFontSize() -3 );
00097 lbl3->SetFontColor(0xff0000);
00098 }
00099
00100 PG_Button* b = new PG_Button( this, PG_Rect( buttonXPos(width, 0 ), Height()/2-lineheight, 2*lineheight, 2*lineheight ));
00101 b->SetIcon( IconRepository::getIcon( "blue-i.png").getBaseSurface() );
00102 b->sigClick.connect( SigC::slot( *this, &VehicleBaseWidget::info ));
00103
00104 SetTransparency( 255 );
00105 };
00106
00107 int VehicleBaseWidget::buttonXPos( int width, int num )
00108 {
00109 int col1 = 50;
00110 int sw = (width - col1 - 10) / 6;
00111 return col1 + 3 * sw + (10 + sw) * num;
00112 }
00113
00114
00115
00116 bool VehicleBaseWidget::info()
00117 {
00118 unitInfoDialog( v->typ );
00119 return true;
00120 }
00121
00122
00123 ASCString VehicleBaseWidget::getName() const
00124 {
00125 return v->getName();
00126 };
00127
00128 void VehicleBaseWidget::display( SDL_Surface * surface, const PG_Rect & src, const PG_Rect & dst )
00129 {
00130 if ( !getClippingSurface().valid() )
00131 getClippingSurface() = Surface::createSurface( fieldsizex + 10, fieldsizey + 10, 32, 0 );
00132
00133 getClippingSurface().Fill(0);
00134
00135 v->typ->paint( getClippingSurface(), SPoint(5,5), actplayer.getPlayerColor(), 0 );
00136 PG_Draw::BlitSurface( getClippingSurface().getBaseSurface(), src, surface, dst);
00137 }
00138
00139 Surface VehicleBaseWidget::clippingSurface;
00140
00141
00142
00143 VehicleTypeResourceWidget::VehicleTypeResourceWidget( PG_Widget* parent, const PG_Point& pos, int width, const VehicleType* vehicletype, int lackingResources, const Resources& cost, const Player& player )
00144 : VehicleTypeBaseWidget( parent,pos, width, vehicletype, player )
00145 {
00146 int col1 = 50;
00147 int lineheight = 20;
00148
00149 int sw = (width - col1 - 10) / 6;
00150
00151 static const char* filenames[3] = { "energy.png", "material.png", "fuel.png" };
00152
00153 for ( int i = 0; i < 3; ++i ) {
00154 new PG_Image ( this, PG_Point( col1 + 2 + 5 * sw, i * 12 + 5), IconRepository::getIcon( filenames[i] ).getBaseSurface(), false );
00155 PG_Label* lbl = new PG_Label( this, PG_Rect( col1 + 3 * sw, i * 12, sw * 2, lineheight ), ASCString::toString(cost.resource(i)) );
00156 lbl->SetAlignment( PG_Label::RIGHT );
00157 lbl->SetFontSize( lbl->GetFontSize() - 3 );
00158 if ( lackingResources & (1<<i) )
00159 lbl->SetFontColor( 0xff0000);
00160 }
00161 }
00162
00163
00164 VehicleTypeCountWidget::VehicleTypeCountWidget( PG_Widget* parent, const PG_Point& pos, int width, const VehicleType* vehicletype, const Player& player, int number )
00165 : VehicleTypeBaseWidget( parent,pos, width, vehicletype, player )
00166 {
00167 int col1 = 50;
00168 int lineheight = 20;
00169
00170 int sw = (width - col1 - 10) / 6;
00171
00172 PG_Label* lbl = new PG_Label( this, PG_Rect( col1 + 4 * sw, 0, sw * 2, lineheight*2 ), ASCString::toString(number) );
00173 lbl->SetAlignment( PG_Label::RIGHT );
00174 lbl->SetFontSize( lbl->GetFontSize() + 5 );
00175 }
00176
00177 VehicleTypeCountLocateWidget::VehicleTypeCountLocateWidget( PG_Widget* parent, const PG_Point& pos, int width, const VehicleType* vehicletype, const Player& player, int number )
00178 : VehicleTypeCountWidget( parent, pos, width, vehicletype, player, number )
00179 {
00180 int lineheight = 20;
00181
00182 PG_Button* b = new PG_Button( this, PG_Rect( buttonXPos(width, 1 ), Height()/2-lineheight, 2*lineheight, 2*lineheight ));
00183 b->SetIcon( IconRepository::getIcon( "magnifier.png").getBaseSurface() );
00184 b->sigClick.connect( SigC::slot( *this, &VehicleTypeCountLocateWidget::locate ));
00185
00186 }
00187
00188 bool VehicleTypeCountLocateWidget::locate()
00189 {
00190 locateVehicles( getVehicletype() );
00191 return true;
00192 }
00193
00194
00195 VehicleTypeSelectionItemFactory :: VehicleTypeSelectionItemFactory( Resources plantResources, const Container& types, const Player& player )
00196 : actplayer(player), showResourcesForUnit(true), original_items( types )
00197 {
00198 restart();
00199 setAvailableResource( plantResources );
00200 };
00201
00202 VehicleTypeSelectionItemFactory :: VehicleTypeSelectionItemFactory( const Container& types, const Player& player )
00203 : actplayer(player), showResourcesForUnit(false), original_items( types )
00204 {
00205 restart();
00206 };
00207
00208
00209
00210 void VehicleTypeSelectionItemFactory::restart()
00211 {
00212 items = original_items;
00213 sort( items.begin(), items.end(), vehicleComp );
00214 it = items.begin();
00215 };
00216
00217
00218 SelectionWidget* VehicleTypeSelectionItemFactory::spawnNextItem( PG_Widget* parent, const PG_Point& pos )
00219 {
00220 if ( it != items.end() ) {
00221 const VehicleType* v = *(it++);
00222 if ( showResourcesForUnit ) {
00223 Resources cost = getCost(v);
00224
00225 int lackingResources = 0;
00226 for ( int r = 0; r < 3; ++r )
00227 if ( plantResources.resource(r) < cost.resource(r))
00228 lackingResources |= 1 << r;
00229 return new VehicleTypeResourceWidget( parent, pos, parent->Width() - 15, v, lackingResources, cost, actplayer );
00230 } else
00231 return new VehicleTypeBaseWidget( parent, pos, parent->Width() - 15, v, actplayer );
00232 } else
00233 return NULL;
00234 };
00235
00236 SigC::Signal1<void,const VehicleType*> VehicleTypeSelectionItemFactory::showVehicleInfo;
00237
00238
00239 void VehicleTypeSelectionItemFactory::itemSelected( const SelectionWidget* widget, bool mouse )
00240 {
00241 if ( !widget )
00242 return;
00243
00244 const VehicleTypeBaseWidget* fw = dynamic_cast<const VehicleTypeBaseWidget*>(widget);
00245 assert( fw );
00246
00247 showVehicleInfo( fw->getVehicletype() );
00248
00249 vehicleTypeSelected( fw->getVehicletype(), mouse );
00250 }
00251