00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef edselfntH
00022 #define edselfntH
00023
00024 #include <algorithm>
00025
00026 #include <paragui.h>
00027 #include <pgwidget.h>
00028 #include <pgwidgetlist.h>
00029 #include <pgwindow.h>
00030 #include <pgapplication.h>
00031
00032 #include "dialogs/selectionwindow.h"
00033 #include "ed_mapcomponent.h"
00034 #include "edglobal.h"
00035
00036 #include "events.h"
00037 #include "itemrepository.h"
00038 #include "objects.h"
00039
00040
00041 extern void selcargo( PG_Window* parentWindow, ContainerBase* container );
00042 extern void selbuildingproduction( Building* eht );
00043
00044 extern void sortItems( vector<Vehicletype*>& vec );
00045 extern void sortItems( vector<BuildingType*>& vec );
00046 extern void sortItems( vector<ObjectType*>& vec );
00047 extern void sortItems( vector<TerrainType*>& vec );
00048 extern void sortItems( vector<MineType*>& vec );
00049
00050
00051
00052
00055 template <class MapItemType>
00056 class MapItemTypeWidget : public SelectionWidget {
00057 const MapItemType* item;
00058 public:
00059 typedef MapItemType ItemType;
00060
00061 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 )
00062 {
00063
00064 };
00065
00066 ASCString getName() const
00067 {
00068 return item->getName();
00069 }
00070
00071 const ItemType* getItem() const
00072 {
00073 return item;
00074 };
00075
00076 void display( SDL_Surface * surface, const PG_Rect & src, const PG_Rect & dst ) {
00077 typename ItemTypeSelector<MapItemType>::type mapComponent( item );
00078 mapComponent.displayClip( this, surface, src, dst );
00079 };
00080
00081 };
00082
00083
00090 template <class MapItemWidget>
00091 class BaseMapItemTypeWidgetFactory : public SelectionItemFactory {
00092 protected:
00093 typedef typename MapItemWidget::ItemType ItemType;
00094 typedef vector<ItemType*> Items;
00095 typename Items::iterator it;
00096
00097
00098
00099 virtual bool isFiltered( const ItemType& item ) {
00100 return ItemFiltrationSystem::isFiltered( &item );
00101 };
00102
00103 virtual void registerItem( MapItemWidget* item ) {};
00104
00105 private:
00106 const ItemRepository<ItemType>& repository;
00107 Items items;
00108
00109 public:
00110 BaseMapItemTypeWidgetFactory( const ItemRepository<ItemType>& itemRepository ) : repository( itemRepository ) {
00111 for ( size_t i = 0; i < repository.getNum(); ++i ) {
00112 ItemType* item = repository.getObject_byPos(i);
00113 if ( item )
00114 items.push_back(item);
00115 }
00116 sortItems( items );
00117 restart();
00118 };
00119
00120 void restart()
00121 {
00122 it = items.begin();
00123 };
00124
00125 SelectionWidget* spawnNextItem( PG_Widget* parent, const PG_Point& pos )
00126 {
00127 while ( it != items.end() && isFiltered( **it) )
00128 ++it;
00129
00130 if ( it != items.end() ) {
00131 MapItemWidget* myItem = new MapItemWidget( parent, pos, *(it++) );
00132 registerItem( myItem );
00133 return myItem;
00134 } else
00135 return NULL;
00136 };
00137 };
00138
00139
00141 template <class MapItemWidget>
00142 class MapItemTypeWidgetFactory : public BaseMapItemTypeWidgetFactory<MapItemWidget> {
00143 protected:
00144 typedef typename MapItemWidget::ItemType ItemType;
00145 public:
00146
00147 MapItemTypeWidgetFactory( const ItemRepository<ItemType>& itemRepository ) : BaseMapItemTypeWidgetFactory<MapItemWidget>( itemRepository ) { };
00148
00149 void itemSelected( const SelectionWidget* widget, bool mouse )
00150 {
00151 if ( !widget )
00152 return;
00153
00154 const MapItemWidget* mapItemWidget = dynamic_cast<const MapItemWidget*>(widget);
00155 assert( mapItemWidget );
00156 if ( mapItemWidget->getItem() ) {
00157 typename ItemTypeSelector<typename MapItemWidget::ItemType>::type item ( mapItemWidget->getItem() );
00158 selection.setSelection( item );
00159 }
00160 }
00161 };
00162
00163
00165 template <class MapItemWidget>
00166 class MapItemTypeWidgetFactory_IDSelection : public BaseMapItemTypeWidgetFactory<MapItemWidget> {
00167 typedef typename MapItemWidget::ItemType ItemType;
00168 int& itemID;
00169 int defaultItemID;
00170 std::map<int,SelectionWidget*> cache;
00171 protected:
00172 virtual void registerItem( MapItemWidget* itemWidget )
00173 {
00174 cache[itemWidget->getItem()->getID()] = itemWidget;
00175 };
00176
00177 public:
00178
00179 MapItemTypeWidgetFactory_IDSelection( const ItemRepository<ItemType>& itemRepository, int& id ) : BaseMapItemTypeWidgetFactory<MapItemWidget>( itemRepository ), itemID( id ), defaultItemID(id)
00180 {
00181 };
00182
00183 SelectionWidget* getDefaultItem()
00184 {
00185 if ( cache.find( defaultItemID ) != cache.end() )
00186 return cache[defaultItemID];
00187 else
00188 return NULL;
00189 }
00190
00191
00192 void itemSelected( const SelectionWidget* widget, bool mouse )
00193 {
00194 if ( !widget )
00195 return;
00196
00197 const MapItemWidget* mapItemWidget = dynamic_cast<const MapItemWidget*>(widget);
00198 assert( mapItemWidget );
00199 if ( mapItemWidget->getItem() )
00200 itemID = mapItemWidget->getItem()->id;
00201 }
00202 };
00203
00204
00209 template <class ItemType>
00210 bool selectItemID( int& id, const ItemRepository<ItemType>& itemRepository )
00211 {
00212 ItemSelectorWindow isw( NULL, PG_Rect( 300, 50, 280, PG_Application::GetScreenHeight()-100), "select item", new MapItemTypeWidgetFactory_IDSelection< MapItemTypeWidget<ItemType> >(itemRepository, id) );
00213 isw.Show();
00214 isw.RunModal();
00215 return true;
00216 }
00217
00218
00220 extern void addCargo( ContainerBase* container );
00221
00222 extern void editProduction( ContainerBase* container );
00223
00224
00225
00226 #endif