00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <sigc++/sigc++.h>
00022
00023 #include "ammotransferdialog.h"
00024
00025 #include "../unitctrl.h"
00026 #include "../containercontrols.h"
00027 #include "../gameoptions.h"
00028 #include "../actions/servicing.h"
00029
00030
00031 class TransferWidget : public PG_Widget {
00032 private:
00033 Transferrable* trans;
00034 PG_Slider* slider;
00035
00036 bool updatePos( long a = 0 )
00037 {
00038 if ( slider )
00039 slider->SetPosition( trans->getAmount( trans->getDstContainer() ));
00040 return true;
00041 }
00042
00043 bool updateRange()
00044 {
00045 int min = trans->getMin( trans->getDstContainer(), false );
00046
00047 int max;
00048 if ( trans->getMax( trans->getDstContainer(), true ) * 3 < trans->getMax( trans->getDstContainer(), false ))
00049 max = trans->getMax( trans->getDstContainer(), true );
00050 else
00051 max = trans->getMax( trans->getDstContainer(), false );
00052
00053 if ( slider )
00054 slider->SetRange( min, max );
00055
00056 updatePos();
00057 return true;
00058 }
00059
00060
00061 public:
00062 TransferWidget ( PG_Widget* parent, const PG_Rect& pos, Transferrable* transferrable, TransferHandler& handler ) : PG_Widget( parent,pos ), trans( transferrable ), slider(NULL)
00063 {
00064 if ( transferrable->isExchangable() ) {
00065 slider = new PG_Slider( this, PG_Rect( 0, 25, pos.w, 15 ), PG_ScrollBar::HORIZONTAL );
00066
00067 updateRange();
00068
00069 slider->sigSlide.connect( SigC::slot( *transferrable, &Transferrable::setDestAmount ));
00070 slider->sigSlideEnd.connect( SigC::slot( *this, &TransferWidget::updatePos));
00071
00072 handler.updateRanges.connect( SigC::slot( *this, &TransferWidget::updateRange));
00073 }
00074
00075
00076 PG_Rect labels = PG_Rect( 0, 0, pos.w, 20 );
00077 PG_Label* l = new PG_Label ( this, labels, transferrable->getName() );
00078 l->SetAlignment( PG_Label::CENTER );
00079
00080 l = new PG_Label ( this, labels );
00081 l->SetAlignment( PG_Label::LEFT );
00082 transferrable->sigSourceAmount.connect( SigC::slot( *l, &PG_Label::SetText ));
00083
00084 l = new PG_Label ( this, labels );
00085 l->SetAlignment( PG_Label::RIGHT );
00086 transferrable->sigDestAmount.connect( SigC::slot( *l, &PG_Label::SetText ));
00087 };
00088 };
00089
00090
00091 class AmmoTransferWindow : public ASC_PG_Dialog {
00092 private:
00093 ContainerBase* first;
00094 ContainerBase* second;
00095 TransferHandler handler;
00096
00097
00098 Surface img1,img2;
00099
00100 bool ok()
00101 {
00102 handler.commit();
00103 QuitModal();
00104 return true;
00105 }
00106
00107 public:
00108 AmmoTransferWindow ( ContainerBase* source, ContainerBase* destination, PG_Widget* parent );
00109
00110 bool somethingToTransfer() { return handler.getTransfers().size(); };
00111
00112 bool eventKeyDown(const SDL_KeyboardEvent* key)
00113 {
00114 if ( key->keysym.sym == SDLK_ESCAPE ) {
00115 QuitModal();
00116 return true;
00117 }
00118 return false;
00119 }
00120
00121 };
00122
00123
00124 AmmoTransferWindow :: AmmoTransferWindow ( ContainerBase* source, ContainerBase* destination, PG_Widget* parent ) : ASC_PG_Dialog( NULL, PG_Rect( 30, 30, 400, 400 ), "Transfer" ), first (source), second( destination ), handler( source, destination )
00125 {
00126 int ypos = 30;
00127 int border = 10;
00128
00129
00130 img1 = source->getImage();
00131 img2 = destination->getImage();
00132
00133 const int singleTransferHeight = 60;
00134
00135 int expectedHeight = handler.getTransfers().size() * singleTransferHeight;
00136 if ( handler.ammoProductionPossible() )
00137 expectedHeight += 30;
00138
00139 int newHeight = min( PG_Application::GetScreen()->h - 60, expectedHeight + 130 );
00140 SizeWidget( w, newHeight );
00141
00142
00143 PG_ScrollWidget* area = new PG_ScrollWidget ( this, PG_Rect( border, ypos, w - border, h - 80 ));
00144 area->SetTransparency( 255 );
00145
00146 (new PG_ThemeWidget( area, PG_Rect( 5,3, fieldsizex, fieldsizey)))->SetBackground( img1.getBaseSurface(), PG_Draw::STRETCH );
00147 (new PG_ThemeWidget( area, PG_Rect( area->Width() - 5 - fieldsizex, 3, fieldsizex, fieldsizey)))->SetBackground( img2.getBaseSurface(), PG_Draw::STRETCH );
00148
00149
00150 ypos = fieldsizex + 5;
00151 if ( handler.ammoProductionPossible() ) {
00152 PG_CheckButton* production = new PG_CheckButton( area, PG_Rect( border, ypos, area->w - 30, 20 ), "allow ammo production" );
00153 if ( CGameOptions::Instance()->autoproduceammunition )
00154 production->SetPressed( );
00155 production->sigClick.connect( SigC::slot( handler, &TransferHandler::allowAmmoProduction ));
00156 ypos += 30;
00157 }
00158
00159 for ( TransferHandler::Transfers::iterator i = handler.getTransfers().begin(); i != handler.getTransfers().end(); ++i ) {
00160 new TransferWidget( area, PG_Rect( 0, ypos, area->w - 30, 50 ), *i, handler );
00161 ypos += singleTransferHeight;
00162 }
00163
00164 int buttonWidth = 150;
00165 PG_Button* b = new PG_Button( this, PG_Rect( w - buttonWidth - border, h - 30 - border, buttonWidth, 30), "OK" );
00166 b->sigClick.connect( SigC::slot( *this, &AmmoTransferWindow::ok ));
00167
00168 for ( TransferHandler::Transfers::iterator i = handler.getTransfers().begin(); i != handler.getTransfers().end(); ++i )
00169 (*i)->showAll();
00170 }
00171
00172 void ammoTransferWindow ( ContainerBase* source, ContainerBase* destination )
00173 {
00174 AmmoTransferWindow atw( source, destination, NULL );
00175 if ( atw.somethingToTransfer() ) {
00176 atw.Show();
00177 atw.RunModal();
00178 }
00179 }
00180
00181 void ammoTransferWindow ( VehicleService* serviceAction, ContainerBase* destination )
00182 {
00183
00184 }