Advanced Strategic Command
edselfnt.h
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 #ifndef edselfntH
22 #define edselfntH
23 
24 #include <algorithm>
25 
26 #include <paragui.h>
27 #include <pgwidget.h>
28 #include <pgwidgetlist.h>
29 #include <pgwindow.h>
30 #include <pgapplication.h>
31 
33 #include "ed_mapcomponent.h"
34 #include "edglobal.h"
35 
36 #include "events.h"
37 #include "itemrepository.h"
38 #include "objects.h"
39 
40 
41 extern void selcargo( PG_Window* parentWindow, ContainerBase* container );
42 extern void selbuildingproduction( Building* eht );
43 
44 extern void sortItems( vector<VehicleType*>& vec );
45 extern void sortItems( vector<BuildingType*>& vec );
46 extern void sortItems( vector<ObjectType*>& vec );
47 extern void sortItems( vector<TerrainType*>& vec );
48 extern void sortItems( vector<MineType*>& vec );
49 
50 
51 
52 
55 template <class MapItemType>
57  const MapItemType* item;
58  public:
60 
61  MapItemTypeWidget ( PG_Widget* parent, const PG_Point& pos, const MapItemType* mapItemType ) : SelectionWidget( parent, PG_Rect( pos.x, pos.y, ItemTypeSelector<ItemType>::type::Width(), ItemTypeSelector<ItemType>::type::Height() + MapComponent::fontHeight )), item( mapItemType )
62  {
63 
64  };
65 
66  ASCString getName() const
67  {
68  return item->getName();
69  }
70 
71  const ItemType* getItem() const
72  {
73  return item;
74  };
75 
76  void display( SDL_Surface * surface, const PG_Rect & src, const PG_Rect & dst ) {
77  typename ItemTypeSelector<MapItemType>::type mapComponent( item );
78  mapComponent.displayClip( this, surface, src, dst );
79  };
80 
81 };
82 
83 
90 template <class MapItemWidget>
92  protected:
93  typedef typename MapItemWidget::ItemType ItemType;
94  typedef vector<ItemType*> Items;
95  typename Items::iterator it;
96 
97 
98 
99  virtual bool isFiltered( const ItemType& item ) {
100  return ItemFiltrationSystem::isFiltered( &item );
101  };
102 
103  virtual void registerItem( MapItemWidget* item ) {};
104 
105  private:
106  const ItemRepository<ItemType>& repository;
107  Items items;
108 
109  public:
110  BaseMapItemTypeWidgetFactory( const ItemRepository<ItemType>& itemRepository ) : repository( itemRepository ) {
111  for ( size_t i = 0; i < repository.getNum(); ++i ) {
112  ItemType* item = repository.getObject_byPos(i);
113  if ( item )
114  items.push_back(item);
115  }
116  sortItems( items );
117  restart();
118  };
119 
120  void restart()
121  {
122  it = items.begin();
123  };
124 
125  SelectionWidget* spawnNextItem( PG_Widget* parent, const PG_Point& pos )
126  {
127  while ( it != items.end() && isFiltered( **it) )
128  ++it;
129 
130  if ( it != items.end() ) {
131  MapItemWidget* myItem = new MapItemWidget( parent, pos, *(it++) );
132  registerItem( myItem );
133  return myItem;
134  } else
135  return NULL;
136  };
137 };
138 
139 
141 template <class MapItemWidget>
143  protected:
144  typedef typename MapItemWidget::ItemType ItemType;
145  public:
146 
147  MapItemTypeWidgetFactory( const ItemRepository<ItemType>& itemRepository ) : BaseMapItemTypeWidgetFactory<MapItemWidget>( itemRepository ) { };
148 
149  void itemSelected( const SelectionWidget* widget, bool mouse )
150  {
151  if ( !widget )
152  return;
153 
154  const MapItemWidget* mapItemWidget = dynamic_cast<const MapItemWidget*>(widget);
155  assert( mapItemWidget );
156  if ( mapItemWidget->getItem() ) {
157  typename ItemTypeSelector<typename MapItemWidget::ItemType>::type item ( mapItemWidget->getItem() );
158  selection.setSelection( item );
159  }
160  }
161 };
162 
163 
165 template <class MapItemWidget>
167  typedef typename MapItemWidget::ItemType ItemType;
168  int& itemID;
169  int defaultItemID;
170  std::map<int,SelectionWidget*> cache;
171  protected:
172  virtual void registerItem( MapItemWidget* itemWidget )
173  {
174  cache[itemWidget->getItem()->getID()] = itemWidget;
175  };
176 
177  public:
178 
179  MapItemTypeWidgetFactory_IDSelection( const ItemRepository<ItemType>& itemRepository, int& id ) : BaseMapItemTypeWidgetFactory<MapItemWidget>( itemRepository ), itemID( id ), defaultItemID(id)
180  {
181  };
182 
184  {
185  if ( cache.find( defaultItemID ) != cache.end() )
186  return cache[defaultItemID];
187  else
188  return NULL;
189  }
190 
191 
192  void itemSelected( const SelectionWidget* widget, bool mouse )
193  {
194  if ( !widget )
195  return;
196 
197  const MapItemWidget* mapItemWidget = dynamic_cast<const MapItemWidget*>(widget);
198  assert( mapItemWidget );
199  if ( mapItemWidget->getItem() )
200  itemID = mapItemWidget->getItem()->id;
201  }
202 };
203 
204 
209 template <class ItemType>
210 bool selectItemID( int& id, const ItemRepository<ItemType>& itemRepository )
211 {
212  ItemSelectorWindow isw( NULL, PG_Rect( 300, 50, 280, PG_Application::GetScreenHeight()-100), "select item", new MapItemTypeWidgetFactory_IDSelection< MapItemTypeWidget<ItemType> >(itemRepository, id) );
213  isw.Show();
214  isw.RunModal();
215  return true;
216 }
217 
218 
220 extern void addCargo( ContainerBase* container );
221 
222 extern void editProduction( ContainerBase* container );
223 
224 
225 
226 #endif
MapItemTypeWidget(PG_Widget *parent, const PG_Point &pos, const MapItemType *mapItemType)
Definition: edselfnt.h:61
void selbuildingproduction(Building *eht)
The BaseMapItemTypeWidgetFactory iterates through all items of an ItemRepository and spawns a MapItem...
Definition: edselfnt.h:91
void itemSelected(const SelectionWidget *widget, bool mouse)
Definition: edselfnt.h:192
virtual void registerItem(MapItemWidget *item)
Definition: edselfnt.h:103
MapItemTypeWidgetFactory(const ItemRepository< ItemType > &itemRepository)
Definition: edselfnt.h:147
virtual void registerItem(MapItemWidget *itemWidget)
Definition: edselfnt.h:172
vector< ItemType * > Items
Definition: edselfnt.h:94
MapItemWidget::ItemType ItemType
Definition: edselfnt.h:144
void editProduction(ContainerBase *container)
Definition: edselfnt.cpp:556
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
BaseMapItemTypeWidgetFactory(const ItemRepository< ItemType > &itemRepository)
Definition: edselfnt.h:110
size_t getNum() const
void setSelection(const Placeable &component)
Definition: edglobal.cpp:191
T * getObject_byPos(int pos) const
void selcargo(PG_Window *parentWindow, ContainerBase *container)
The class describing properties that are common to all vehicles of a certain kind.
Definition: vehicletype.h:177
void display(SDL_Surface *surface, const PG_Rect &src, const PG_Rect &dst)
Definition: edselfnt.h:76
A MapComponent represents any kind of item that can be placed on the map by the user.
The base class for everything that can be placed on a map field.
Definition: mapitemtype.h:16
Items::iterator it
Definition: edselfnt.h:95
static bool isFiltered(Category cat, int id)
A MapItemTypeWidget show any kind of MapItemType in a SelectionWidget It is typically generated by a ...
Definition: edselfnt.h:56
MapItemTypeWidgetFactory_IDSelection(const ItemRepository< ItemType > &itemRepository, int &id)
Definition: edselfnt.h:179
virtual ASCString getName() const =0
void addCargo(ContainerBase *container)
Lets the user choose a unit and adds it to the cargo of container.
Definition: edselfnt.cpp:358
bool selectItemID(int &id, const ItemRepository< ItemType > &itemRepository)
opens a window in which the user can select an item of the given type ( template parameter ItemType )...
Definition: edselfnt.h:210
SelectionHolder selection
Definition: edglobal.cpp:189
Adds selection logic to a BaseMapItemTypeWidgetFactory. Selected items are registered as being the ac...
Definition: edselfnt.h:142
Mine and Objects which can be placed on a map field.
const ItemType * getItem() const
Definition: edselfnt.h:71
void itemSelected(const SelectionWidget *widget, bool mouse)
Definition: edselfnt.h:149
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
An actual building on the map, which references a BuildingType Buildings have an owner,.
Definition: buildings.h:38
MapItemWidget::ItemType ItemType
Definition: edselfnt.h:93
ASCString getName() const
Definition: edselfnt.h:66
void sortItems(vector< VehicleType * > &vec)
Definition: edselfnt.cpp:75
SelectionWidget * spawnNextItem(PG_Widget *parent, const PG_Point &pos)
Definition: edselfnt.h:125
SelectionWidget * getDefaultItem()
Definition: edselfnt.h:183
Adds selection logic to a BaseMapItemTypeWidgetFactory. The ID is of the selected item is obtained an...
Definition: edselfnt.h:166
MapItemType ItemType
Definition: edselfnt.h:59
virtual bool isFiltered(const ItemType &item)
Definition: edselfnt.h:99