File: buildingtypeselector.cpp

package info (click to toggle)
asc 2.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 75,080 kB
  • ctags: 24,943
  • sloc: cpp: 155,023; sh: 8,829; ansic: 6,890; makefile: 650; perl: 138
file content (188 lines) | stat: -rw-r--r-- 6,459 bytes parent folder | download | duplicates (8)
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
/***************************************************************************
                          cargodialog.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 "buildingtypeselector.h"
#include "selectionwindow.h"
#include "unitinfodialog.h"

#include "../buildingtype.h"
#include "../iconrepository.h"
#include "../spfst.h"
#include "../unitset.h"



const int buildingHeight = 5 * fielddisty + fieldsizey;
const int buildingWidth = 4 * fielddistx;

int BuildingTypeBaseWidget :: getBuildingHeight( const BuildingType* type )
{
   int miny = maxint;
   int maxy = minint;
   for ( int y = 0; y < 6; ++y )
      for ( int x = 0; x < 4; ++x )
         if ( type->fieldExists( BuildingType::LocalCoordinate( x, y ))) {
            miny = min( miny, y );
            maxy = max( maxy, y );
         }

   return (maxy - miny) * fielddisty + fieldsizey;
}


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)
{

   new PG_Label( this, PG_Rect( buildingWidth, 20, Width() - buildingWidth - 10, 25 ), vt->name );

   SetTransparency( 255 );
};



ASCString BuildingTypeBaseWidget::getName() const
{
   return vt->getName();
};

void BuildingTypeBaseWidget::display( SDL_Surface * surface, const PG_Rect & src, const PG_Rect & dst )
{
   if ( !getClippingSurface().valid() )
      getClippingSurface() = Surface::createSurface( buildingWidth + 10, buildingHeight + 10, 32, 0 );

   getClippingSurface().Fill(0);

   int yoffs = maxint;
   int xoffs = maxint;
   for ( int y = 0; y < 6; ++y )
      for ( int x = 0; x < 4; ++x )
         if ( vt->fieldExists( BuildingType::LocalCoordinate( x, y ))) {
            yoffs = min( yoffs, y * fielddisty );
            xoffs = min( xoffs, x * fielddistx + (y&1)*fielddisthalfx);
         }

   for ( int y = 0; y < 6; ++y )
      for ( int x = 0; x < 4; ++x )
         if ( vt->fieldExists( BuildingType::LocalCoordinate( x, y )))
            vt->paintSingleField( getClippingSurface(), SPoint(5 - xoffs, 5 - yoffs), BuildingType::LocalCoordinate(x,y), actplayer.getPlayerColor() );

   // vt->paint( getClippingSurface(), SPoint(5,5), actplayer, 0 );
   PG_Draw::BlitSurface( getClippingSurface().getBaseSurface(), src, surface, dst);
}

Surface BuildingTypeBaseWidget::clippingSurface;



BuildingTypeResourceWidget::BuildingTypeResourceWidget( PG_Widget* parent, const PG_Point& pos, int width, const BuildingType* BuildingType, int lackingResources, const Resources& cost, const Player& player )
   : BuildingTypeBaseWidget( parent,pos, width, BuildingType, player )
{
   int col1 = 50;
   int lineheight  = 20;

   int sw = (width - col1 - 10) / 6;

   static const char* filenames[3] = { "energy.png", "material.png", "fuel.png" };

   for ( int i = 0; i < 3; ++i ) {
      new PG_Image ( this, PG_Point( col1 + 2 + 5 * sw, i * 12 + 5), IconRepository::getIcon( filenames[i] ).getBaseSurface(), false  );
      PG_Label* lbl = new PG_Label( this, PG_Rect( col1 + 3 * sw, i * 12, sw * 2, lineheight ), ASCString::toString(cost.resource(i)) );
      lbl->SetAlignment( PG_Label::RIGHT );
      lbl->SetFontSize( lbl->GetFontSize() - 3 );
      if ( lackingResources & (1<<i) )
         lbl->SetFontColor( 0xff0000);
   }
}
      

BuildingTypeCountWidget::BuildingTypeCountWidget( PG_Widget* parent, const PG_Point& pos, int width, const BuildingType* BuildingType, const Player& player, int number )
   : BuildingTypeBaseWidget( parent,pos, width, BuildingType, player )
{
   int col1 = 50;
   int lineheight  = 20;

   int sw = (width - col1 - 10) / 6;

   PG_Label* lbl = new PG_Label( this, PG_Rect( col1 + 4 * sw, 0, sw * 2, lineheight*2 ), ASCString::toString(number) );
   lbl->SetAlignment( PG_Label::RIGHT );
   lbl->SetFontSize( lbl->GetFontSize() + 5 );
}






BuildingTypeSelectionItemFactory :: BuildingTypeSelectionItemFactory( Resources plantResources, const Container& types, const Player& player ) : actplayer(player), original_items( types )
{
   restart();
   setAvailableResource( plantResources );
};


bool BuildingComp ( const BuildingType* v1, const BuildingType* v2 )
{
   int id1 = getUnitSetID(v1);
   int id2 = getUnitSetID(v2);
   return (id1 <  id2) ||
          (id1 == id2 && v1->name < v2->name);
};


void BuildingTypeSelectionItemFactory::restart()
{
   items = original_items;
   sort( items.begin(), items.end(), BuildingComp );
   it = items.begin();
};


SelectionWidget* BuildingTypeSelectionItemFactory::spawnNextItem( PG_Widget* parent, const PG_Point& pos )
{
   if ( it != items.end() ) {
      const BuildingType* v = *(it++);
      Resources cost  = getCost(v);

      int lackingResources = 0;
      for ( int r = 0; r < 3; ++r )
         if ( plantResources.resource(r) < cost.resource(r))
            lackingResources |= 1 << r;
      return new BuildingTypeResourceWidget( parent, pos, parent->Width() - 15, v, lackingResources, cost, actplayer );
   } else
      return NULL;
};

Resources BuildingTypeSelectionItemFactory::getCost( const BuildingType* type )
{
   return type->productionCost;
}



void BuildingTypeSelectionItemFactory::itemSelected( const SelectionWidget* widget, bool mouse )
{
   if ( !widget )
      return;

   const BuildingTypeResourceWidget* fw = dynamic_cast<const BuildingTypeResourceWidget*>(widget);
   assert( fw );

   // showBuildingInfo( fw->getBuildingType() );
   
   BuildingTypeSelected( fw->getBuildingType() );
}