Advanced Strategic Command
vehicletypeselector.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  cargodialog.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 "vehicletypeselector.h"
19 #include "selectionwindow.h"
20 #include "unitinfodialog.h"
21 
22 #include "../vehicletype.h"
23 #include "../iconrepository.h"
24 #include "../spfst.h"
25 #include "../unitset.h"
26 
27 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)
28 {
29  int col1 = 50;
30  int lineheight = 20;
31 
32  int sw = (width - col1 - 10) / 6;
33 
34  PG_Label* lbl1 = new PG_Label( this, PG_Rect( col1, 0, 3 * sw, lineheight ), vt->name );
35  lbl1->SetFontSize( lbl1->GetFontSize() -2 );
36 
37  PG_Label* lbl2 = new PG_Label( this, PG_Rect( col1, lineheight, 3 * sw, lineheight ), vt->description );
38  lbl2->SetFontSize( lbl2->GetFontSize() -2 );
39 
40  PG_Button* b = new PG_Button( this, PG_Rect( buttonXPos(width, 0 ), Height()/2-lineheight, 2*lineheight, 2*lineheight ));
41  b->SetIcon( IconRepository::getIcon( "blue-i.png").getBaseSurface() );
42  b->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &VehicleTypeBaseWidget::info )));
43 
44  SetTransparency( 255 );
45 };
46 
47 int VehicleTypeBaseWidget::buttonXPos( int width, int num )
48 {
49  int col1 = 50;
50  int sw = (width - col1 - 10) / 6;
51  return col1 + 3 * sw + (10 + sw) * num;
52 }
53 
54 
55 
56 bool VehicleTypeBaseWidget::info()
57 {
58  unitInfoDialog( vt );
59  return true;
60 }
61 
62 
64 {
65  return vt->getName();
66 };
67 
68 void VehicleTypeBaseWidget::display( SDL_Surface * surface, const PG_Rect & src, const PG_Rect & dst )
69 {
70  if ( !getClippingSurface().valid() )
71  getClippingSurface() = Surface::createSurface( fieldsizex + 10, fieldsizey + 10, 32, 0 );
72 
73  getClippingSurface().Fill(0);
74 
75  vt->paint( getClippingSurface(), SPoint(5,5), actplayer.getPlayerColor(), 0 );
76  PG_Draw::BlitSurface( getClippingSurface().getBaseSurface(), src, surface, dst);
77 }
78 
79 Surface VehicleTypeBaseWidget::clippingSurface;
80 
81 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)
82 {
83  int col1 = 50;
84  int lineheight = 18;
85 
86  int sw = (width - col1 - 10) / 6;
87 
88  PG_Label* lbl1 = new PG_Label( this, PG_Rect( col1, 0, 3 * sw, lineheight ), v->typ->name );
89  lbl1->SetFontSize( lbl1->GetFontSize() -2 );
90 
91  PG_Label* lbl2 = new PG_Label( this, PG_Rect( col1, lineheight, 3 * sw, lineheight ), v->typ->description );
92  lbl2->SetFontSize( lbl2->GetFontSize() -3 );
93 
94  if ( v->getName() != v->typ->name ) {
95  PG_Label* lbl3 = new PG_Label( this, PG_Rect( col1, lineheight * 2, 3 * sw, lineheight), '>'+v->getName()+'<');
96  lbl3->SetFontSize( lbl3->GetFontSize() -3 );
97  lbl3->SetFontColor(0xff0000);
98  }
99 
100  PG_Button* b = new PG_Button( this, PG_Rect( buttonXPos(width, 0 ), Height()/2-lineheight, 2*lineheight, 2*lineheight ));
101  b->SetIcon( IconRepository::getIcon( "blue-i.png").getBaseSurface() );
102  b->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &VehicleBaseWidget::info )));
103 
104  SetTransparency( 255 );
105 };
106 
107 int VehicleBaseWidget::buttonXPos( int width, int num )
108 {
109  int col1 = 50;
110  int sw = (width - col1 - 10) / 6;
111  return col1 + 3 * sw + (10 + sw) * num;
112 }
113 
114 
115 
116 bool VehicleBaseWidget::info()
117 {
118  unitInfoDialog( v->typ );
119  return true;
120 }
121 
122 
124 {
125  return v->getName();
126 };
127 
128 void VehicleBaseWidget::display( SDL_Surface * surface, const PG_Rect & src, const PG_Rect & dst )
129 {
130  if ( !getClippingSurface().valid() )
131  getClippingSurface() = Surface::createSurface( fieldsizex + 10, fieldsizey + 10, 32, 0 );
132 
133  getClippingSurface().Fill(0);
134 
135  v->typ->paint( getClippingSurface(), SPoint(5,5), actplayer.getPlayerColor(), 0 );
136  PG_Draw::BlitSurface( getClippingSurface().getBaseSurface(), src, surface, dst);
137 }
138 
139 Surface VehicleBaseWidget::clippingSurface;
140 
141 
142 
143 VehicleTypeResourceWidget::VehicleTypeResourceWidget( PG_Widget* parent, const PG_Point& pos, int width, const VehicleType* vehicletype, int lackingResources, const Resources& cost, const Player& player )
144  : VehicleTypeBaseWidget( parent,pos, width, vehicletype, player )
145 {
146  int col1 = 50;
147  int lineheight = 20;
148 
149  int sw = (width - col1 - 10) / 6;
150 
151  static const char* filenames[3] = { "energy.png", "material.png", "fuel.png" };
152 
153  for ( int i = 0; i < 3; ++i ) {
154  new PG_Image ( this, PG_Point( col1 + 2 + 5 * sw, i * 12 + 5), IconRepository::getIcon( filenames[i] ).getBaseSurface(), false );
155  PG_Label* lbl = new PG_Label( this, PG_Rect( col1 + 3 * sw, i * 12, sw * 2, lineheight ), ASCString::toString(cost.resource(i)) );
156  lbl->SetAlignment( PG_Label::RIGHT );
157  lbl->SetFontSize( lbl->GetFontSize() - 3 );
158  if ( lackingResources & (1<<i) )
159  lbl->SetFontColor( 0xff0000);
160  }
161 }
162 
163 
164 VehicleTypeCountWidget::VehicleTypeCountWidget( PG_Widget* parent, const PG_Point& pos, int width, const VehicleType* vehicletype, const Player& player, int number )
165  : VehicleTypeBaseWidget( parent,pos, width, vehicletype, player )
166 {
167  int col1 = 50;
168  int lineheight = 20;
169 
170  int sw = (width - col1 - 10) / 6;
171 
172  PG_Label* lbl = new PG_Label( this, PG_Rect( col1 + 4 * sw, 0, sw * 2, lineheight*2 ), ASCString::toString(number) );
173  lbl->SetAlignment( PG_Label::RIGHT );
174  lbl->SetFontSize( lbl->GetFontSize() + 5 );
175 }
176 
177 VehicleTypeCountLocateWidget::VehicleTypeCountLocateWidget( PG_Widget* parent, const PG_Point& pos, int width, const VehicleType* vehicletype, const Player& player, int number )
178  : VehicleTypeCountWidget( parent, pos, width, vehicletype, player, number )
179 {
180  int lineheight = 20;
181 
182  PG_Button* b = new PG_Button( this, PG_Rect( buttonXPos(width, 1 ), Height()/2-lineheight, 2*lineheight, 2*lineheight ));
183  b->SetIcon( IconRepository::getIcon( "magnifier.png").getBaseSurface() );
184  b->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &VehicleTypeCountLocateWidget::locate )));
185 
186 }
187 
188 bool VehicleTypeCountLocateWidget::locate()
189 {
191  return true;
192 }
193 
194 
196  : actplayer(player), showResourcesForUnit(true), original_items( types )
197 {
198  restart();
199  setAvailableResource( plantResources );
200 };
201 
203  : actplayer(player), showResourcesForUnit(false), original_items( types )
204 {
205  restart();
206 };
207 
208 
209 
211 {
212  items = original_items;
213  sort( items.begin(), items.end(), vehicleComp );
214  it = items.begin();
215 };
216 
217 
218 SelectionWidget* VehicleTypeSelectionItemFactory::spawnNextItem( PG_Widget* parent, const PG_Point& pos )
219 {
220  if ( it != items.end() ) {
221  const VehicleType* v = *(it++);
222  if ( showResourcesForUnit ) {
223  Resources cost = getCost(v);
224 
225  int lackingResources = 0;
226  for ( int r = 0; r < 3; ++r )
227  if ( plantResources.resource(r) < cost.resource(r))
228  lackingResources |= 1 << r;
229  return new VehicleTypeResourceWidget( parent, pos, parent->Width() - 15, v, lackingResources, cost, actplayer );
230  } else
231  return new VehicleTypeBaseWidget( parent, pos, parent->Width() - 15, v, actplayer );
232  } else
233  return NULL;
234 };
235 
236 sigc::signal<void,const VehicleType*> VehicleTypeSelectionItemFactory::showVehicleInfo;
237 
238 
240 {
241  if ( !widget )
242  return;
243 
244  const VehicleTypeBaseWidget* fw = dynamic_cast<const VehicleTypeBaseWidget*>(widget);
245  assert( fw );
246 
248 
249  vehicleTypeSelected( fw->getVehicletype(), mouse );
250 }
251 
VehicleTypeCountLocateWidget(PG_Widget *parent, const PG_Point &pos, int width, const VehicleType *vehicletype, const Player &player, int number)
sigc::signal< void, const VehicleType * > locateVehicles
PlayerColor getPlayerColor() const
Definition: player.cpp:268
bool vehicleComp(const VehicleType *v1, const VehicleType *v2)
Definition: unitset.cpp:234
ASCString getName() const
static Surface createSurface(int width, int height, SDLmm::Color color=255)
Definition: surface.cpp:387
SelectionWidget * spawnNextItem(PG_Widget *parent, const PG_Point &pos)
VehicleTypeBaseWidget(PG_Widget *parent, const PG_Point &pos, int width, const VehicleType *vehicletype, const Player &player)
ASCString getName() const
ASCString getName() const
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
ASCString description
short description of the units role, for example "strategic bomber"
void display(SDL_Surface *surface, const PG_Rect &src, const PG_Rect &dst)
void setAvailableResource(const Resources &plantResources)
#define fieldsizey
Definition: typen.h:441
The class describing properties that are common to all vehicles of a certain kind.
Definition: vehicletype.h:177
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
vector< const VehicleType * > Container
ASCString getName() const
returns the units name or, if it does not exist, the unit type's name or description ...
Definition: vehicle.cpp:1569
VehicleTypeResourceWidget(PG_Widget *parent, const PG_Point &pos, int width, const VehicleType *vehicletype, int lackingResources, const Resources &cost, const Player &player)
#define fieldsizex
Definition: typen.h:440
SDLmm::SPoint SPoint
Definition: surface.h:27
VehicleTypeSelectionItemFactory(Resources plantResources, const Container &types, const Player &player)
const VehicleType * getVehicletype() const
static int buttonXPos(int width, int num)
int & resource(int type)
Definition: typen.h:105
const VehicleType * typ
Definition: vehicle.h:83
static Surface & getIcon(const ASCString &name)
virtual void vehicleTypeSelected(const VehicleType *type, bool mouse)
VehicleBaseWidget(PG_Widget *parent, const PG_Point &pos, int width, const Vehicle *vehicle, const Player &player)
void paint(Surface &s, SPoint pos, const PlayerColor &player, int direction=0) const
void itemSelected(const SelectionWidget *widget, bool mouse)
void display(SDL_Surface *surface, const PG_Rect &src, const PG_Rect &dst)
the different players in ASC. There may be 8 players (0..7) and neutral units (8) ...
Definition: player.h:99
Resources are basically the currency of ASC.
Definition: typen.h:97
static sigc::signal< void, const VehicleType * > showVehicleInfo
void unitInfoDialog(const VehicleType *vt)
static int buttonXPos(int width, int num)
VehicleTypeCountWidget(PG_Widget *parent, const PG_Point &pos, int width, const VehicleType *vehicletype, const Player &player, int number)
ASCString name
a short name, for example B-52
virtual Resources getCost(const VehicleType *type)