FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
triggercontroller.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 
24 // 3rd party library includes
25 
26 // FIFE includes
27 // These includes are split up in two parts, separated by one empty line
28 // First block: files included from the FIFE root src directory
29 // Second block: files included from the same folder
31 #include "util/log/logger.h"
32 
33 #include "cell.h"
34 #include "cellcache.h"
35 #include "map.h"
36 #include "trigger.h"
37 #include "triggercontroller.h"
38 
39 namespace FIFE {
40 
41  static Logger _log(LM_STRUCTURES);
42 
44  m_map(map) {
45  }
46 
49  for (; it != m_triggerNameMap.end(); ++it) {
50  delete it->second;
51  }
52  }
53 
54  Trigger* TriggerController::createTrigger(const std::string& triggerName) {
55  //assert(!exists(triggerName));
56 
57  Trigger* trigger = new Trigger(triggerName);
58 
59  std::pair<TriggerNameMapIterator, bool> returnValue;
60  returnValue = m_triggerNameMap.insert ( TriggerNameMapPair(triggerName, trigger));
61 
62  if (!returnValue.second) {
63  delete trigger;
64  FL_WARN(_log, LMsg("TriggerController::createTrigger() - ") << "Trigger " << triggerName << " already exists.... ignoring.");
65  }
66 
67  return returnValue.first->second;
68  }
69 
70  Trigger* TriggerController::createTriggerOnCoordinate(const std::string& triggerName, Layer* layer, const ModelCoordinate& pt) {
71  assert(layer);
72 
73  Trigger* trigger = createTrigger(triggerName);
74  trigger->assign(layer, pt);
75  return trigger;
76  }
77 
78  Trigger* TriggerController::createTriggerOnCoordinates(const std::string& triggerName, Layer* layer, const std::vector<ModelCoordinate>& coords) {
79  assert(layer);
80 
81  Trigger* trigger = createTrigger(triggerName);
82  for (std::vector<ModelCoordinate>::const_iterator it = coords.begin(); it != coords.end(); ++it) {
83  trigger->assign(layer, *it);
84  }
85  return trigger;
86  }
87 
88  Trigger* TriggerController::createTriggerOnRect(const std::string& triggerName, Layer* layer, const Rect& rec) {
89  assert(layer);
90  assert(layer->getCellCache());
91 
92  Trigger* trigger = createTrigger(triggerName);
93  std::vector<Cell*> cells = layer->getCellCache()->getCellsInRect(rec);
94  for (std::vector<Cell*>::iterator it = cells.begin(); it != cells.end(); ++it) {
95  trigger->assign(*it);
96  }
97  return trigger;
98  }
99 
100  Trigger* TriggerController::createTriggerOnLocation(const std::string& triggerName, const Location& loc) {
101  assert(loc.getLayer());
102  assert(loc.getLayer()->getCellCache());
103 
104  Trigger* trigger = createTrigger(triggerName);
105  trigger->assign(loc.getLayer(), loc.getLayerCoordinates());
106  return trigger;
107  }
108 
109  Trigger* TriggerController::createTriggerOnLocations(const std::string& triggerName, const std::vector<Location>& locs) {
110  Trigger* trigger = createTrigger(triggerName);
111  for (std::vector<Location>::const_iterator it = locs.begin(); it != locs.end(); ++it) {
112  trigger->assign((*it).getLayer(), (*it).getLayerCoordinates());
113  }
114  return trigger;
115  }
116 
117  Trigger* TriggerController::createTriggerOnCell(const std::string& triggerName, Cell* cell) {
118  assert(cell);
119 
120  Trigger* trigger = createTrigger(triggerName);
121  trigger->assign(cell);
122  return trigger;
123  }
124 
125  Trigger* TriggerController::createTriggerOnCells(const std::string& triggerName, const std::vector<Cell*>& cells) {
126  Trigger* trigger = createTrigger(triggerName);
127  for (std::vector<Cell*>::const_iterator it = cells.begin(); it != cells.end(); ++it) {
128  trigger->assign(*it);
129  }
130  return trigger;
131  }
132 
133  Trigger* TriggerController::createTriggerOnInstance(const std::string& triggerName, Instance* instance) {
134  assert(instance);
135 
136  Trigger* trigger = createTrigger(triggerName);
137  trigger->attach(instance);
138  return trigger;
139  }
140 
141  Trigger* TriggerController::getTrigger(const std::string& triggerName) {
142  TriggerNameMapIterator it = m_triggerNameMap.find(triggerName);
143  if (it != m_triggerNameMap.end()) {
144  return it->second;
145  }
146  return NULL;
147  }
148 
149  void TriggerController::deleteTrigger(const std::string& triggerName) {
150  TriggerNameMapIterator it = m_triggerNameMap.find(triggerName);
151  if (it != m_triggerNameMap.end()) {
152  delete it->second;
153  m_triggerNameMap.erase(it);
154  }
155  }
156 
157  void TriggerController::removeTriggerFromCoordinate(const std::string& triggerName, Layer* layer, const ModelCoordinate& pt) {
158  TriggerNameMapIterator it = m_triggerNameMap.find(triggerName);
159  if (it != m_triggerNameMap.end()) {
160  it->second->remove(layer, pt);
161  }
162  }
163 
164  void TriggerController::removeTriggerFromCoordinates(const std::string& triggerName, Layer* layer, const std::vector<ModelCoordinate>& coords) {
165  TriggerNameMapIterator it = m_triggerNameMap.find(triggerName);
166  if (it != m_triggerNameMap.end()) {
167  for (std::vector<ModelCoordinate>::const_iterator cit = coords.begin(); cit != coords.end(); ++cit) {
168  it->second->remove(layer, *cit);
169  }
170  }
171  }
172 
173  void TriggerController::removeTriggerFromRect(const std::string& triggerName, Layer* layer, const Rect& rec) {
174  TriggerNameMapIterator it = m_triggerNameMap.find(triggerName);
175  if (it != m_triggerNameMap.end()) {
176  std::vector<Cell*> cells = layer->getCellCache()->getCellsInRect(rec);
177  for (std::vector<Cell*>::iterator cit = cells.begin(); cit != cells.end(); ++cit) {
178  it->second->remove(*cit);
179  }
180  }
181  }
182 
183  void TriggerController::removeTriggerFromLocation(const std::string& triggerName, const Location& loc) {
184  TriggerNameMapIterator it = m_triggerNameMap.find(triggerName);
185  if (it != m_triggerNameMap.end()) {
186  it->second->remove(loc.getLayer(), loc.getLayerCoordinates());
187  }
188  }
189 
190  void TriggerController::removeTriggerFromLocations(const std::string& triggerName, const std::vector<Location>& locs) {
191  TriggerNameMapIterator it = m_triggerNameMap.find(triggerName);
192  if (it != m_triggerNameMap.end()) {
193  for (std::vector<Location>::const_iterator cit = locs.begin(); cit != locs.end(); ++cit) {
194  it->second->remove((*cit).getLayer(), (*cit).getLayerCoordinates());
195  }
196  }
197  }
198 
199  void TriggerController::removeTriggerFromCell(const std::string& triggerName, Cell* cell) {
200  TriggerNameMapIterator it = m_triggerNameMap.find(triggerName);
201  if (it != m_triggerNameMap.end()) {
202  it->second->remove(cell);
203  }
204  }
205 
206  void TriggerController::removeTriggerFromCells(const std::string& triggerName, const std::vector<Cell*>& cells) {
207  TriggerNameMapIterator it = m_triggerNameMap.find(triggerName);
208  if (it != m_triggerNameMap.end()) {
209  for (std::vector<Cell*>::const_iterator cit = cells.begin(); cit != cells.end(); ++cit) {
210  it->second->remove(*cit);
211  }
212  }
213  }
214 
215  void TriggerController::removeTriggerFromInstance(const std::string& triggerName, Instance* instance) {
216  TriggerNameMapIterator it = m_triggerNameMap.find(triggerName);
217  if (it != m_triggerNameMap.end()) {
218  if (it->second->getAttached() == instance) {
219  it->second->detach();
220  }
221  }
222  }
223 
224  std::vector<Trigger*> TriggerController::getAllTriggers() {
225  std::vector<Trigger*> triggers;
227  for (; it != m_triggerNameMap.end(); ++it) {
228  triggers.push_back(it->second);
229  }
230  return triggers;
231  }
232 
233  std::vector<std::string> TriggerController::getAllTriggerNames() {
234  std::vector<std::string> names;
236  for (; it != m_triggerNameMap.end(); ++it) {
237  names.push_back(it->first);
238  }
239  return names;
240  }
241 
242  bool TriggerController::exists(const std::string& name) {
244  if (it != m_triggerNameMap.end()) {
245  return true;
246  }
247 
248  return false;
249  }
250 
251 } //FIFE
#define FL_WARN(logger, msg)
Definition: logger.h:72
Helper class to create log strings out from separate parts Usage: LMsg("some text") << variable << "...
Definition: logger.h:82
void attach(Instance *instance)
Attaches the trigger to the given instance.
Definition: trigger.cpp:286
Trigger * createTriggerOnRect(const std::string &triggerName, Layer *layer, const Rect &rec)
Creates and adds a trigger to the cells on the specified layer and rect.
void removeTriggerFromCoordinate(const std::string &triggerName, Layer *layer, const ModelCoordinate &pt)
Removes a trigger from the cell on the specified layer and coordinate.
std::vector< Cell * > getCellsInRect(const Rect &rec)
Returns all cells in the rect.
Definition: cellcache.cpp:883
Trigger * createTriggerOnInstance(const std::string &triggerName, Instance *instance)
Creates and attach a trigger to the specified instance.
CellCache * getCellCache()
Returns the CellCache of this layer.
Definition: layer.cpp:573
Trigger * createTriggerOnCoordinate(const std::string &triggerName, Layer *layer, const ModelCoordinate &pt)
Creates and adds a trigger to the cell on the specified layer and coordinate.
Trigger * createTriggerOnCoordinates(const std::string &triggerName, Layer *layer, const std::vector< ModelCoordinate > &coords)
Creates and adds a trigger to the cells on the specified layer and coordinates.
static Logger _log(LM_AUDIO)
virtual ~TriggerController()
Destructor.
void removeTriggerFromCell(const std::string &triggerName, Cell *cell)
Removes a trigger from the specified cell.
Layer * getLayer() const
Gets the layer where this location is pointing to.
Definition: location.cpp:83
Trigger * createTriggerOnLocation(const std::string &triggerName, const Location &loc)
Creates and adds a trigger to the cell on the specified location.
ModelCoordinate getLayerCoordinates() const
Gets cell precision layer coordinates set to this location.
Definition: location.cpp:113
std::vector< Trigger * > getAllTriggers()
Returns a vector with all trigger pointers.
std::pair< std::string, Trigger * > TriggerNameMapPair
std::vector< std::string > getAllTriggerNames()
Returns a vector with all trigger names.
void removeTriggerFromCells(const std::string &triggerName, const std::vector< Cell * > &cells)
Removes a trigger from the specified cells.
A basic layer on a map.
Definition: layer.h:99
Trigger * getTrigger(const std::string &triggerName)
Returns a pointer to the trigger with the given name.
void assign(Layer *layer, const ModelCoordinate &pt)
Assigns trigger on given layer and position.
Definition: trigger.cpp:242
std::map< std::string, Trigger * >::iterator TriggerNameMapIterator
A basic cell on a CellCache.
Definition: cell.h:136
Trigger * createTriggerOnCell(const std::string &triggerName, Cell *cell)
Creates and adds a trigger to the specified cell.
void removeTriggerFromInstance(const std::string &triggerName, Instance *instance)
Detach a trigger from the specified instance.
void deleteTrigger(const std::string &triggerName)
Deletes a trigger with the given name.
void removeTriggerFromRect(const std::string &triggerName, Layer *layer, const Rect &rec)
Removes a trigger from the cell on the specified layer and coordinates.
A 3D Point.
Definition: point.h:205
void removeTriggerFromLocations(const std::string &triggerName, const std::vector< Location > &locs)
Removes a trigger from the cell on the specified locations.
Trigger * createTrigger(const std::string &triggerName)
Creates a trigger.
Trigger get triggered when a specific set of criteria are met.
Definition: trigger.h:83
Trigger * createTriggerOnLocations(const std::string &triggerName, const std::vector< Location > &locs)
Creates and adds a trigger to the cell on the specified locations.
Trigger * createTriggerOnCells(const std::string &triggerName, const std::vector< Cell * > &cells)
Creates and adds a trigger to the specified cells.
bool exists(const std::string &name)
Checks to see if the trigger name already exists.
A container of Layer(s).
Definition: map.h:88
TriggerNameMap m_triggerNameMap
Trigger name map.
void removeTriggerFromCoordinates(const std::string &triggerName, Layer *layer, const std::vector< ModelCoordinate > &coords)
Removes a trigger from the cell on the specified layer and coordinates.
void removeTriggerFromLocation(const std::string &triggerName, const Location &loc)
Removes a trigger from the cell on the specified location.
An Instance is an "instantiation" of an Object at a Location.
Definition: instance.h:101
TriggerController(Map *map)
Constructor.