00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <pgprogressbar.h>
00019 #include "../paradialog.h"
00020 #include "cargowidget.h"
00021
00022 #include "../clipboard.h"
00023 #include "../edselfnt.h"
00024 #include "../edmisc.h"
00025
00026
00027
00028 class CargoEditor : public PG_Window {
00029 ContainerBase* container;
00030 HighLightingManager highLightingManager;
00031 int unitColumnCount;
00032 CargoWidget* cargoWidget;
00033 PG_ProgressBar* bgw;
00034 static int stack;
00035
00036 TemporaryContainerStorage tus;
00037
00038 bool addUnit()
00039 {
00040 addCargo( container );
00041 cargoWidget->redrawAll();
00042 updateGraph();
00043 return true;
00044 }
00045
00046 bool remove()
00047 {
00048 if ( cargoWidget->getMarkedUnit() ) {
00049 delete cargoWidget->getMarkedUnit();
00050 cargoWidget->redrawAll();
00051 updateGraph();
00052 return true;
00053 } else
00054 return false;
00055 }
00056
00057 bool copyUnit()
00058 {
00059 if ( cargoWidget->getMarkedUnit() ) {
00060 ClipBoard::Instance().clear();
00061 ClipBoard::Instance().addUnit( cargoWidget->getMarkedUnit() );
00062 return true;
00063 } else
00064 return false;
00065 }
00066
00067 bool pasteUnit()
00068 {
00069 Vehicle* veh = ClipBoard::Instance().pasteUnit();
00070 if ( !veh )
00071 return false;
00072
00073 if ( container->vehicleFit( veh )) {
00074 container->addToCargo( veh );
00075 cargoWidget->redrawAll();
00076 updateGraph();
00077 return true;
00078 } else {
00079 delete veh;
00080 return false;
00081 }
00082 }
00083
00084 bool editUnit()
00085 {
00086 changeunitvalues( cargoWidget->getMarkedUnit() );
00087 updateGraph();
00088 return true;
00089 }
00090
00091 bool editUnitCargo()
00092 {
00093 cargoEditor( cargoWidget->getMarkedUnit() );
00094 updateGraph();
00095 return true;
00096 }
00097
00098 bool ok()
00099 {
00100 QuitModal();
00101 return true;
00102 }
00103
00104 bool cancel()
00105 {
00106 tus.restore();
00107 QuitModal();
00108 return true;
00109 }
00110
00111 void updateGraph()
00112 {
00113 if ( container->baseType->maxLoadableWeight > 0 ) {
00114 bgw->SetProgress( float( container->cargoWeight()) / container->baseType->maxLoadableWeight * 100 );
00115 bgw->Update();
00116 }
00117 }
00118
00119 public:
00120 CargoEditor( PG_Widget* parent, ContainerBase* my_container ) : PG_Window( parent, PG_Rect( 50 + stack * 20, 30 + stack * 20, 500, 400 ), "Cargo Editor" ), container( my_container ), tus( container, true)
00121 {
00122 bgw = new PG_ProgressBar( this, PG_Rect( 10, 35, Width() - 20, 15 ) );
00123
00124 updateGraph();
00125
00126 ++stack;
00127 cargoWidget = new CargoWidget( this, PG_Rect( 10, 60, Width()-20, Height() - 120 ), container, true);
00128
00129 int buttonLine = Height() - 110 + 60;
00130 int buttonHeight = 30;
00131 PG_Button* add = new PG_Button( this, PG_Rect( 10, buttonLine, 50, buttonHeight), "+" );
00132 add->sigClick.connect( SigC::slot( *this, &CargoEditor::addUnit ));
00133
00134 PG_Button* rem = new PG_Button( this, PG_Rect( 70, buttonLine, 50, buttonHeight), "-" );
00135 rem->sigClick.connect( SigC::slot( *this, &CargoEditor::remove ));
00136
00137 PG_Button* copy = new PG_Button( this, PG_Rect( 130, buttonLine, 50, buttonHeight), "copy" );
00138 copy->sigClick.connect( SigC::slot( *this, &CargoEditor::copyUnit ));
00139
00140 PG_Button* paste = new PG_Button( this, PG_Rect( 190, buttonLine, 50, buttonHeight), "paste" );
00141 paste->sigClick.connect( SigC::slot( *this, &CargoEditor::pasteUnit ));
00142
00143 PG_Button* edit = new PG_Button( this, PG_Rect( 250, buttonLine, 50, buttonHeight), "edit" );
00144 edit->sigClick.connect( SigC::slot( *this, &CargoEditor::editUnit ));
00145
00146 PG_Button* cargo = new PG_Button( this, PG_Rect( 310, buttonLine, 50, buttonHeight), "cargo" );
00147 cargo->sigClick.connect( SigC::slot( *this, &CargoEditor::editUnitCargo ));
00148
00149 PG_Button* cancel = new PG_Button( this, PG_Rect( 370, buttonLine, 50, buttonHeight), "cancel" );
00150 cancel->sigClick.connect( SigC::slot( *this, &CargoEditor::cancel ));
00151
00152 PG_Button* ok = new PG_Button( this, PG_Rect( 430, buttonLine, 50, buttonHeight), "ok" );
00153 ok->sigClick.connect( SigC::slot( *this, &CargoEditor::ok ));
00154 }
00155
00156 bool eventKeyDown(const SDL_KeyboardEvent* key)
00157 {
00158 int mod = SDL_GetModState() & ~(KMOD_NUM | KMOD_CAPS | KMOD_MODE);
00159
00160 if ( !mod ) {
00161 if ( key->keysym.sym == SDLK_ESCAPE )
00162 return cancel();
00163
00164 if ( key->keysym.sym == SDLK_RETURN || key->keysym.sym == SDLK_KP_ENTER )
00165 return ok();
00166
00167 if ( key->keysym.sym == SDLK_c )
00168 return editUnitCargo();
00169
00170 if ( key->keysym.sym == SDLK_p )
00171 return editUnit();
00172
00173 if ( key->keysym.sym == SDLK_PLUS || key->keysym.sym == SDLK_KP_PLUS)
00174 return addUnit();
00175
00176 if ( key->keysym.sym == SDLK_MINUS || key->keysym.sym == SDLK_DELETE || key->keysym.sym == SDLK_KP_MINUS)
00177 return remove();
00178
00179 }
00180
00181 if ( mod & KMOD_CTRL ) {
00182 if ( key->keysym.sym == SDLK_c )
00183 return copyUnit();
00184
00185 if ( key->keysym.sym == SDLK_v )
00186 return pasteUnit();
00187
00188 }
00189 return false;
00190 };
00191
00192 ~CargoEditor()
00193 {
00194 --stack;
00195 }
00196 };
00197
00198
00199 int CargoEditor::stack = 0;
00200
00201 void cargoEditor( ContainerBase* container )
00202 {
00203 if ( container && container->baseType->maxLoadableUnits ) {
00204 CargoEditor ce ( NULL, container );
00205 ce.Show();
00206 ce.RunModal();
00207 }
00208 }
00209