Advanced Strategic Command
buildingtypeselector.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 "buildingtypeselector.h"
19 #include "selectionwindow.h"
20 #include "unitinfodialog.h"
21 
22 #include "../buildingtype.h"
23 #include "../iconrepository.h"
24 #include "../spfst.h"
25 #include "../unitset.h"
26 
27 
28 
30 const int buildingWidth = 4 * fielddistx;
31 
33 {
34  int miny = maxint;
35  int maxy = minint;
36  for ( int y = 0; y < 6; ++y )
37  for ( int x = 0; x < 4; ++x )
38  if ( type->fieldExists( BuildingType::LocalCoordinate( x, y ))) {
39  miny = min( miny, y );
40  maxy = max( maxy, y );
41  }
42 
43  return (maxy - miny) * fielddisty + fieldsizey;
44 }
45 
46 
47 BuildingTypeBaseWidget :: BuildingTypeBaseWidget( PG_Widget* parent, const PG_Point& pos, int width, const BuildingType* buildingType, const Player& player ) : SelectionWidget( parent, PG_Rect( pos.x, pos.y, width, getBuildingHeight(buildingType)+10 )), vt( buildingType ), actplayer(player)
48 {
49 
50  new PG_Label( this, PG_Rect( buildingWidth, 20, Width() - buildingWidth - 10, 25 ), vt->name );
51 
52  SetTransparency( 255 );
53 };
54 
55 
56 
58 {
59  return vt->getName();
60 };
61 
62 void BuildingTypeBaseWidget::display( SDL_Surface * surface, const PG_Rect & src, const PG_Rect & dst )
63 {
64  if ( !getClippingSurface().valid() )
65  getClippingSurface() = Surface::createSurface( buildingWidth + 10, buildingHeight + 10, 32, 0 );
66 
67  getClippingSurface().Fill(0);
68 
69  int yoffs = maxint;
70  int xoffs = maxint;
71  for ( int y = 0; y < 6; ++y )
72  for ( int x = 0; x < 4; ++x )
73  if ( vt->fieldExists( BuildingType::LocalCoordinate( x, y ))) {
74  yoffs = min( yoffs, y * fielddisty );
75  xoffs = min( xoffs, x * fielddistx + (y&1)*fielddisthalfx);
76  }
77 
78  for ( int y = 0; y < 6; ++y )
79  for ( int x = 0; x < 4; ++x )
80  if ( vt->fieldExists( BuildingType::LocalCoordinate( x, y )))
81  vt->paintSingleField( getClippingSurface(), SPoint(5 - xoffs, 5 - yoffs), BuildingType::LocalCoordinate(x,y), actplayer.getPlayerColor() );
82 
83  // vt->paint( getClippingSurface(), SPoint(5,5), actplayer, 0 );
84  PG_Draw::BlitSurface( getClippingSurface().getBaseSurface(), src, surface, dst);
85 }
86 
87 Surface BuildingTypeBaseWidget::clippingSurface;
88 
89 
90 
91 BuildingTypeResourceWidget::BuildingTypeResourceWidget( PG_Widget* parent, const PG_Point& pos, int width, const BuildingType* BuildingType, int lackingResources, const Resources& cost, const Player& player )
92  : BuildingTypeBaseWidget( parent,pos, width, BuildingType, player )
93 {
94  int col1 = 50;
95  int lineheight = 20;
96 
97  int sw = (width - col1 - 10) / 6;
98 
99  static const char* filenames[3] = { "energy.png", "material.png", "fuel.png" };
100 
101  for ( int i = 0; i < 3; ++i ) {
102  new PG_Image ( this, PG_Point( col1 + 2 + 5 * sw, i * 12 + 5), IconRepository::getIcon( filenames[i] ).getBaseSurface(), false );
103  PG_Label* lbl = new PG_Label( this, PG_Rect( col1 + 3 * sw, i * 12, sw * 2, lineheight ), ASCString::toString(cost.resource(i)) );
104  lbl->SetAlignment( PG_Label::RIGHT );
105  lbl->SetFontSize( lbl->GetFontSize() - 3 );
106  if ( lackingResources & (1<<i) )
107  lbl->SetFontColor( 0xff0000);
108  }
109 }
110 
111 
112 BuildingTypeCountWidget::BuildingTypeCountWidget( PG_Widget* parent, const PG_Point& pos, int width, const BuildingType* BuildingType, const Player& player, int number )
113  : BuildingTypeBaseWidget( parent,pos, width, BuildingType, player )
114 {
115  int col1 = 50;
116  int lineheight = 20;
117 
118  int sw = (width - col1 - 10) / 6;
119 
120  PG_Label* lbl = new PG_Label( this, PG_Rect( col1 + 4 * sw, 0, sw * 2, lineheight*2 ), ASCString::toString(number) );
121  lbl->SetAlignment( PG_Label::RIGHT );
122  lbl->SetFontSize( lbl->GetFontSize() + 5 );
123 }
124 
125 
126 
127 
128 
129 
130 BuildingTypeSelectionItemFactory :: BuildingTypeSelectionItemFactory( Resources plantResources, const Container& types, const Player& player ) : actplayer(player), original_items( types )
131 {
132  restart();
133  setAvailableResource( plantResources );
134 };
135 
136 
137 bool BuildingComp ( const BuildingType* v1, const BuildingType* v2 )
138 {
139  int id1 = getUnitSetID(v1);
140  int id2 = getUnitSetID(v2);
141  return (id1 < id2) ||
142  (id1 == id2 && v1->name < v2->name);
143 };
144 
145 
147 {
148  items = original_items;
149  sort( items.begin(), items.end(), BuildingComp );
150  it = items.begin();
151 };
152 
153 
154 SelectionWidget* BuildingTypeSelectionItemFactory::spawnNextItem( PG_Widget* parent, const PG_Point& pos )
155 {
156  if ( it != items.end() ) {
157  const BuildingType* v = *(it++);
158  Resources cost = getCost(v);
159 
160  int lackingResources = 0;
161  for ( int r = 0; r < 3; ++r )
162  if ( plantResources.resource(r) < cost.resource(r))
163  lackingResources |= 1 << r;
164  return new BuildingTypeResourceWidget( parent, pos, parent->Width() - 15, v, lackingResources, cost, actplayer );
165  } else
166  return NULL;
167 };
168 
170 {
171  return type->productionCost;
172 }
173 
174 
175 
177 {
178  if ( !widget )
179  return;
180 
181  const BuildingTypeResourceWidget* fw = dynamic_cast<const BuildingTypeResourceWidget*>(widget);
182  assert( fw );
183 
184  // showBuildingInfo( fw->getBuildingType() );
185 
187 }
188 
void paintSingleField(Surface &s, SPoint pos, const LocalCoordinate &localCoordinate, const PlayerColor &player, int weather=0, int constructionStep=0) const
void setAvailableResource(const Resources &plantResources)
void display(SDL_Surface *surface, const PG_Rect &src, const PG_Rect &dst)
const int buildingHeight
void itemSelected(const SelectionWidget *widget, bool mouse)
A local coordinate referencing a single field that a building covers.
Definition: buildingtype.h:57
PlayerColor getPlayerColor() const
Definition: player.cpp:268
virtual Resources getCost(const BuildingType *type)
static Surface createSurface(int width, int height, SDLmm::Color color=255)
Definition: surface.cpp:387
virtual void BuildingTypeSelected(const BuildingType *type)=0
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
#define fieldsizey
Definition: typen.h:441
#define fielddisthalfx
Definition: typen.h:435
bool fieldExists(const LocalCoordinate &localCoordinate) const
returns whether this building covers the given field
Definition: buildingtype.h:108
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
BuildingTypeResourceWidget(PG_Widget *parent, const PG_Point &pos, int width, const BuildingType *BuildingType, int lackingResources, const Resources &cost, const Player &player)
The class describing properties that are common to all buildings of a certain kind.
Definition: buildingtype.h:35
bool BuildingComp(const BuildingType *v1, const BuildingType *v2)
const BuildingType * getBuildingType() const
int getUnitSetID(const VehicleType *veh)
Definition: unitset.cpp:216
BuildingTypeSelectionItemFactory(Resources plantResources, const Container &types, const Player &player)
SelectionWidget * spawnNextItem(PG_Widget *parent, const PG_Point &pos)
SDLmm::SPoint SPoint
Definition: surface.h:27
const int buildingWidth
vector< const BuildingType * > Container
#define fielddistx
Definition: typen.h:433
int & resource(int type)
Definition: typen.h:105
BuildingTypeCountWidget(PG_Widget *parent, const PG_Point &pos, int width, const BuildingType *BuildingType, const Player &player, int number)
static Surface & getIcon(const ASCString &name)
ASCString getName() const
#define maxint
Definition: typen.h:462
#define minint
Definition: typen.h:463
const T & max(const T &a, const T &b, const T &c)
Definition: misc.h:97
const T & min(const T &a, const T &b, const T &c)
Definition: misc.h:80
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 int getBuildingHeight(const BuildingType *type)
ASCString name
a short name, for example B-52
BuildingTypeBaseWidget(PG_Widget *parent, const PG_Point &pos, int width, const BuildingType *BuildingType, const Player &player)
#define fielddisty
Definition: typen.h:434