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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
/*
This file is part of Advanced Strategic Command; http://www.asc-hq.de
Copyright (C) 1994-1999 Martin Bickel and Marc Schellenberger
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
#ifndef edselfntH
#define edselfntH
#include <algorithm>
#include <paragui.h>
#include <pgwidget.h>
#include <pgwidgetlist.h>
#include <pgwindow.h>
#include <pgapplication.h>
#include "dialogs/selectionwindow.h"
#include "ed_mapcomponent.h"
#include "edglobal.h"
#include "events.h"
#include "itemrepository.h"
#include "objects.h"
extern void selcargo( PG_Window* parentWindow, ContainerBase* container );
extern void selbuildingproduction( Building* eht );
extern void sortItems( vector<Vehicletype*>& vec );
extern void sortItems( vector<BuildingType*>& vec );
extern void sortItems( vector<ObjectType*>& vec );
extern void sortItems( vector<TerrainType*>& vec );
extern void sortItems( vector<MineType*>& vec );
/** A MapItemTypeWidget show any kind of MapItemType in a SelectionWidget
It is typically generated by a BaseMapItemTypeWidgetFactory */
template <class MapItemType>
class MapItemTypeWidget : public SelectionWidget {
const MapItemType* item;
public:
typedef MapItemType ItemType;
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 )
{
};
ASCString getName() const
{
return item->getName();
}
const ItemType* getItem() const
{
return item;
};
void display( SDL_Surface * surface, const PG_Rect & src, const PG_Rect & dst ) {
typename ItemTypeSelector<MapItemType>::type mapComponent( item );
mapComponent.displayClip( this, surface, src, dst );
};
};
/** The BaseMapItemTypeWidgetFactory iterates through all items of an ItemRepository and spawns a MapItemTypeWidget for it.
It is will belong to a SelectionWidget , which will control the MapItemTypeWidget generation.
This class does nothing if a widget is selected. This behaviour must be added by derived classes.
*/
template <class MapItemWidget>
class BaseMapItemTypeWidgetFactory : public SelectionItemFactory {
protected:
typedef typename MapItemWidget::ItemType ItemType;
typedef vector<ItemType*> Items;
typename Items::iterator it;
virtual bool isFiltered( const ItemType& item ) {
return ItemFiltrationSystem::isFiltered( &item );
};
virtual void registerItem( MapItemWidget* item ) {};
private:
const ItemRepository<ItemType>& repository;
Items items;
public:
BaseMapItemTypeWidgetFactory( const ItemRepository<ItemType>& itemRepository ) : repository( itemRepository ) {
for ( size_t i = 0; i < repository.getNum(); ++i ) {
ItemType* item = repository.getObject_byPos(i);
if ( item )
items.push_back(item);
}
sortItems( items );
restart();
};
void restart()
{
it = items.begin();
};
SelectionWidget* spawnNextItem( PG_Widget* parent, const PG_Point& pos )
{
while ( it != items.end() && isFiltered( **it) )
++it;
if ( it != items.end() ) {
MapItemWidget* myItem = new MapItemWidget( parent, pos, *(it++) );
registerItem( myItem );
return myItem;
} else
return NULL;
};
};
//! Adds selection logic to a BaseMapItemTypeWidgetFactory. Selected items are registered as being the active "brush" for editing the map
template <class MapItemWidget>
class MapItemTypeWidgetFactory : public BaseMapItemTypeWidgetFactory<MapItemWidget> {
protected:
typedef typename MapItemWidget::ItemType ItemType;
public:
MapItemTypeWidgetFactory( const ItemRepository<ItemType>& itemRepository ) : BaseMapItemTypeWidgetFactory<MapItemWidget>( itemRepository ) { };
void itemSelected( const SelectionWidget* widget, bool mouse )
{
if ( !widget )
return;
const MapItemWidget* mapItemWidget = dynamic_cast<const MapItemWidget*>(widget);
assert( mapItemWidget );
if ( mapItemWidget->getItem() ) {
typename ItemTypeSelector<typename MapItemWidget::ItemType>::type item ( mapItemWidget->getItem() );
selection.setSelection( item );
}
}
};
//! Adds selection logic to a BaseMapItemTypeWidgetFactory. The ID is of the selected item is obtained and written to the variable which was passed on construction
template <class MapItemWidget>
class MapItemTypeWidgetFactory_IDSelection : public BaseMapItemTypeWidgetFactory<MapItemWidget> {
typedef typename MapItemWidget::ItemType ItemType;
int& itemID;
int defaultItemID;
std::map<int,SelectionWidget*> cache;
protected:
virtual void registerItem( MapItemWidget* itemWidget )
{
cache[itemWidget->getItem()->getID()] = itemWidget;
};
public:
MapItemTypeWidgetFactory_IDSelection( const ItemRepository<ItemType>& itemRepository, int& id ) : BaseMapItemTypeWidgetFactory<MapItemWidget>( itemRepository ), itemID( id ), defaultItemID(id)
{
};
SelectionWidget* getDefaultItem()
{
if ( cache.find( defaultItemID ) != cache.end() )
return cache[defaultItemID];
else
return NULL;
}
void itemSelected( const SelectionWidget* widget, bool mouse )
{
if ( !widget )
return;
const MapItemWidget* mapItemWidget = dynamic_cast<const MapItemWidget*>(widget);
assert( mapItemWidget );
if ( mapItemWidget->getItem() )
itemID = mapItemWidget->getItem()->id;
}
};
/** opens a window in which the user can select an item of the given type ( template parameter ItemType ).
\param id The value of this variable will set the item that is selected when opening the window. The selected item will also be written back here.
\param itemRepository the repository of items from which the user can chose one
*/
template <class ItemType>
bool selectItemID( int& id, const ItemRepository<ItemType>& itemRepository )
{
ItemSelectorWindow isw( NULL, PG_Rect( 300, 50, 280, PG_Application::GetScreenHeight()-100), "select item", new MapItemTypeWidgetFactory_IDSelection< MapItemTypeWidget<ItemType> >(itemRepository, id) );
isw.Show();
isw.RunModal();
return true;
}
//! Lets the user chose a unit and adds it to the cargo of container.
extern void addCargo( ContainerBase* container );
extern void editProduction( ContainerBase* container );
#endif
|