1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
/***************************************************************************
cargowidget.cpp - description
-------------------
begin : Tue Oct 24 2000
copyright : (C) 2000 by Martin Bickel
email : bickel@asc-hq.org
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <pgprogressbar.h>
#include "../paradialog.h"
#include "cargowidget.h"
#include "../clipboard.h"
#include "../edselfnt.h"
#include "../edmisc.h"
class CargoEditor : public PG_Window {
ContainerBase* container;
HighLightingManager highLightingManager;
int unitColumnCount;
CargoWidget* cargoWidget;
PG_ProgressBar* bgw;
static int stack;
TemporaryContainerStorage tus;
bool addUnit()
{
addCargo( container );
cargoWidget->redrawAll();
updateGraph();
return true;
}
bool remove()
{
if ( cargoWidget->getMarkedUnit() ) {
delete cargoWidget->getMarkedUnit();
cargoWidget->redrawAll();
updateGraph();
return true;
} else
return false;
}
bool copyUnit()
{
if ( cargoWidget->getMarkedUnit() ) {
ClipBoard::Instance().clear();
ClipBoard::Instance().addUnit( cargoWidget->getMarkedUnit() );
return true;
} else
return false;
}
bool pasteUnit()
{
Vehicle* veh = ClipBoard::Instance().pasteUnit();
if ( !veh )
return false;
if ( container->vehicleFit( veh )) {
container->addToCargo( veh );
cargoWidget->redrawAll();
updateGraph();
return true;
} else {
delete veh;
return false;
}
}
bool editUnit()
{
changeunitvalues( cargoWidget->getMarkedUnit() );
updateGraph();
return true;
}
bool editUnitCargo()
{
cargoEditor( cargoWidget->getMarkedUnit() );
updateGraph();
return true;
}
bool ok()
{
QuitModal();
return true;
}
bool cancel()
{
tus.restore();
QuitModal();
return true;
}
void updateGraph()
{
if ( container->baseType->maxLoadableWeight > 0 ) {
bgw->SetProgress( float( container->cargoWeight()) / container->baseType->maxLoadableWeight * 100 );
bgw->Update();
}
}
public:
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)
{
bgw = new PG_ProgressBar( this, PG_Rect( 10, 35, Width() - 20, 15 ) );
updateGraph();
++stack;
cargoWidget = new CargoWidget( this, PG_Rect( 10, 60, Width()-20, Height() - 120 ), container, true);
int buttonLine = Height() - 110 + 60;
int buttonHeight = 30;
PG_Button* add = new PG_Button( this, PG_Rect( 10, buttonLine, 50, buttonHeight), "+" );
add->sigClick.connect( SigC::slot( *this, &CargoEditor::addUnit ));
PG_Button* rem = new PG_Button( this, PG_Rect( 70, buttonLine, 50, buttonHeight), "-" );
rem->sigClick.connect( SigC::slot( *this, &CargoEditor::remove ));
PG_Button* copy = new PG_Button( this, PG_Rect( 130, buttonLine, 50, buttonHeight), "copy" );
copy->sigClick.connect( SigC::slot( *this, &CargoEditor::copyUnit ));
PG_Button* paste = new PG_Button( this, PG_Rect( 190, buttonLine, 50, buttonHeight), "paste" );
paste->sigClick.connect( SigC::slot( *this, &CargoEditor::pasteUnit ));
PG_Button* edit = new PG_Button( this, PG_Rect( 250, buttonLine, 50, buttonHeight), "edit" );
edit->sigClick.connect( SigC::slot( *this, &CargoEditor::editUnit ));
PG_Button* cargo = new PG_Button( this, PG_Rect( 310, buttonLine, 50, buttonHeight), "cargo" );
cargo->sigClick.connect( SigC::slot( *this, &CargoEditor::editUnitCargo ));
PG_Button* cancel = new PG_Button( this, PG_Rect( 370, buttonLine, 50, buttonHeight), "cancel" );
cancel->sigClick.connect( SigC::slot( *this, &CargoEditor::cancel ));
PG_Button* ok = new PG_Button( this, PG_Rect( 430, buttonLine, 50, buttonHeight), "ok" );
ok->sigClick.connect( SigC::slot( *this, &CargoEditor::ok ));
}
bool eventKeyDown(const SDL_KeyboardEvent* key)
{
int mod = SDL_GetModState() & ~(KMOD_NUM | KMOD_CAPS | KMOD_MODE);
if ( !mod ) {
if ( key->keysym.sym == SDLK_ESCAPE )
return cancel();
if ( key->keysym.sym == SDLK_RETURN || key->keysym.sym == SDLK_KP_ENTER )
return ok();
if ( key->keysym.sym == SDLK_c )
return editUnitCargo();
if ( key->keysym.sym == SDLK_p )
return editUnit();
if ( key->keysym.sym == SDLK_PLUS || key->keysym.sym == SDLK_KP_PLUS)
return addUnit();
if ( key->keysym.sym == SDLK_MINUS || key->keysym.sym == SDLK_DELETE || key->keysym.sym == SDLK_KP_MINUS)
return remove();
}
if ( mod & KMOD_CTRL ) {
if ( key->keysym.sym == SDLK_c )
return copyUnit();
if ( key->keysym.sym == SDLK_v )
return pasteUnit();
}
return false;
};
~CargoEditor()
{
--stack;
}
};
int CargoEditor::stack = 0;
void cargoEditor( ContainerBase* container )
{
if ( container && container->baseType->maxLoadableUnits ) {
CargoEditor ce ( NULL, container );
ce.Show();
ce.RunModal();
}
}
|