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