layer.h

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 #ifndef FIFE_LAYER_H
00023 #define FIFE_LAYER_H
00024 
00025 // Standard C++ library includes
00026 #include <algorithm>
00027 #include <string>
00028 #include <vector>
00029 #include <set>
00030 
00031 // 3rd party library includes
00032 
00033 // FIFE includes
00034 // These includes are split up in two parts, separated by one empty line
00035 // First block: files included from the FIFE root src directory
00036 // Second block: files included from the same folder
00037 #include "util/base/fifeclass.h"
00038 #include "util/structures/rect.h"
00039 #include "model/metamodel/modelcoords.h"
00040 #include "model/metamodel/object.h"
00041 
00042 #include "instance.h"
00043 
00044 namespace FIFE {
00045 
00046     class Map;
00047     class Selection;
00048     class CellGrid;
00049     class Object;
00050     class InstanceTree;
00051 
00058     enum PathingStrategy {
00059         CELL_EDGES_ONLY,
00060         CELL_EDGES_AND_DIAGONALS,
00061         FREEFORM
00062     };
00063 
00066     class LayerChangeListener {
00067     public:
00068         virtual ~LayerChangeListener() {};
00069 
00075         virtual void onLayerChanged(Layer* layer, std::vector<Instance*>& changedInstances) = 0;
00076 
00081         virtual void onInstanceCreate(Layer* layer, Instance* instance) = 0;
00082 
00088         virtual void onInstanceDelete(Layer* layer, Instance* instance) = 0;
00089     };
00090 
00091 
00094     class Layer : public FifeClass {
00095         public:
00100             Layer(const std::string& identifier, Map* map, CellGrid* grid);
00101 
00104             ~Layer();
00105 
00108             const std::string& getId() const { return m_id; }
00109 
00112             void setId(const std::string& id) { m_id = id; }
00113 
00116             Map* getMap() const { return m_map; }
00117 
00121             CellGrid* getCellGrid() const { return m_grid; }
00122 
00125             void setCellGrid(CellGrid* grid) { m_grid = grid; }
00126 
00130             InstanceTree* getInstanceTree(void) const { return m_instanceTree; }
00131 
00135             bool hasInstances() const;
00136 
00139             Instance* createInstance(Object* object, const ModelCoordinate& p, const std::string& id="");
00140 
00143             Instance* createInstance(Object* object, const ExactModelCoordinate& p, const std::string& id="");
00144 
00148             bool addInstance(Instance* instance, const ExactModelCoordinate& p);
00149 
00152             void deleteInstance(Instance* object);
00153 
00156             const std::vector<Instance*>& getInstances() const { return m_instances; }
00157 
00160             std::vector<Instance*> getInstances(const std::string& id);
00161 
00166             std::vector<Instance*> getInstancesAt(Location& loc, bool use_exactcoordinates=false);
00167 
00171             std::list<Instance*> getInstancesIn(Rect& rec);
00172 
00175             Instance* getInstance(const std::string& identifier);
00176 
00179             void setInstancesVisible(bool vis);
00180 
00184             void setLayerTransparency(uint8_t transparency);
00185 
00188             uint8_t getLayerTransparency();
00189 
00195             void getMinMaxCoordinates(ModelCoordinate& min, ModelCoordinate& max, const Layer* layer = 0) const;
00196 
00202             bool cellContainsBlockingInstance(const ModelCoordinate& cellCoordinate);
00203 
00207             void toggleInstancesVisible();
00208 
00212             bool areInstancesVisible() const { return m_instances_visibility; }
00213 
00217             bool update();
00218 
00222             void setPathingStrategy(PathingStrategy strategy) { m_pathingstrategy = strategy; }
00223 
00227             PathingStrategy getPathingStrategy() const { return m_pathingstrategy; }
00228 
00232             void addChangeListener(LayerChangeListener* listener);
00233 
00237             void removeChangeListener(LayerChangeListener* listener);
00238 
00241             bool isChanged() { return m_changed; }
00242 
00246             std::vector<Instance*>& getChangedInstances() { return m_changedinstances; }
00247 
00248             void setInstanceActivityStatus(Instance* instance, bool active);
00249 
00250         protected:
00251             std::string m_id;
00252 
00253             Map* m_map;
00254 
00255             bool m_instances_visibility;
00256 
00257             uint8_t m_transparency;
00258 
00259             // all the instances on this layer
00260             std::vector<Instance*> m_instances;
00261 
00262             // all the active instances on this layer
00263             std::set<Instance*> m_active_instances;
00264 
00265             //The instance tree
00266             InstanceTree* m_instanceTree;
00267 
00268             // layer's cellgrid
00269             CellGrid* m_grid;
00270 
00271             // pathing strategy for the layer
00272             PathingStrategy m_pathingstrategy;
00273 
00274             // listeners for layer changes
00275             std::vector<LayerChangeListener*> m_changelisteners;
00276 
00277             // holds changed instances after each update
00278             std::vector<Instance*> m_changedinstances;
00279 
00280             // true if layer (or it's instance) information was changed during previous update round
00281             bool m_changed;
00282     };
00283 
00284 } // FIFE
00285 
00286 #endif