FIFE  2008.0
cellselectionrenderer.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
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
30 #include "video/renderbackend.h"
31 #include "util/math/fife_math.h"
32 #include "util/log/logger.h"
33 #include "model/metamodel/grids/cellgrid.h"
34 #include "model/structures/instance.h"
35 #include "model/structures/layer.h"
36 #include "model/structures/location.h"
37 
38 #include "view/camera.h"
39 #include "cellselectionrenderer.h"
40 
41 
42 namespace FIFE {
43  static Logger _log(LM_VIEWVIEW);
44 
46  RendererBase(renderbackend, position) {
47  setEnabled(false);
48  m_color.r = 255;
49  m_color.g = 0;
50  m_color.b = 0;
51  }
52 
54  RendererBase(old),
55  m_color(old.m_color) {
56  setEnabled(false);
57  }
58 
60  return new CellSelectionRenderer(*this);
61  }
62 
64  }
65 
67  return dynamic_cast<CellSelectionRenderer*>(cnt->getRenderer("CellSelectionRenderer"));
68  }
69 
71  m_locations.clear();
72  }
73 
74  void CellSelectionRenderer::selectLocation(const Location* loc) {
75  if (loc) {
76  std::vector<Location>::const_iterator it = m_locations.begin();
77  for (; it != m_locations.end(); it++) {
78  if (*it == *loc) return;
79  }
80 
81  m_locations.push_back(Location(*loc));
82  }
83  }
84 
85  void CellSelectionRenderer::deselectLocation(const Location* loc) {
86  if (loc) {
87  std::vector<Location>::iterator it = m_locations.begin();
88  for (; it != m_locations.end(); it++) {
89  if (*it == *loc) {
90  m_locations.erase(it);
91  break;
92  }
93  }
94  }
95  }
96 
97  void CellSelectionRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
98  if (m_locations.empty()) {
99  return;
100  }
101 
102  std::vector<Location>::const_iterator locit = m_locations.begin();
103  for (; locit != m_locations.end(); locit++) {
104  const Location loc = *locit;
105  if (layer != loc.getLayer()) {
106  continue;
107  }
108 
109  CellGrid* cg = layer->getCellGrid();
110  if (!cg) {
111  FL_WARN(_log, "No cellgrid assigned to layer, cannot draw selection");
112  continue;
113  }
114 
115  std::vector<ExactModelCoordinate> vertices;
116  cg->getVertices(vertices, loc.getLayerCoordinates());
117  std::vector<ExactModelCoordinate>::const_iterator it = vertices.begin();
118  ScreenPoint firstpt = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
119  Point pt1(firstpt.x, firstpt.y);
120  Point pt2;
121  ++it;
122  for (; it != vertices.end(); it++) {
123  ScreenPoint pts = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
124  pt2.x = pts.x; pt2.y = pts.y;
125  Point cpt1 = pt1;
126  Point cpt2 = pt2;
127  m_renderbackend->drawLine(cpt1, cpt2, m_color.r, m_color.g, m_color.b);
128  pt1 = pt2;
129  }
130  m_renderbackend->drawLine(pt2, Point(firstpt.x, firstpt.y), m_color.r, m_color.g, m_color.b);
131  }
132  }
133 
134  void CellSelectionRenderer::setColor(uint8_t r, uint8_t g, uint8_t b) {
135  m_color.r = r;
136  m_color.g = g;
137  m_color.b = b;
138  }
139 }
virtual RendererBase * getRenderer(const std::string &renderername)=0
void deselectLocation(const Location *loc)
static CellSelectionRenderer * getInstance(IRendererContainer *cnt)
CellGrid * getCellGrid() const
Definition: layer.h:121
CellSelectionRenderer(RenderBackend *renderbackend, int32_t position)
virtual void drawLine(const Point &p1, const Point &p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
virtual void setEnabled(bool enabled)
void selectLocation(const Location *loc)
ScreenPoint toScreenCoordinates(const ExactModelCoordinate &map_coords)
Definition: camera.cpp:331
void render(Camera *cam, Layer *layer, RenderList &instances)
credit to phoku for his NodeDisplay example which the visitor code is adapted from ( he coded the qua...
Definition: soundclip.cpp:39