FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
cellselectionrenderer.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
30 #include "video/renderbackend.h"
31 #include "util/math/fife_math.h"
32 #include "util/log/logger.h"
35 #include "model/structures/layer.h"
37 
38 #include "view/camera.h"
39 #include "cellselectionrenderer.h"
40 
41 
42 namespace FIFE {
46  static Logger _log(LM_VIEWVIEW);
47 
49  RendererBase(renderbackend, position) {
50  setEnabled(false);
51  m_color.r = 255;
52  m_color.g = 0;
53  m_color.b = 0;
54  }
55 
57  RendererBase(old),
58  m_color(old.m_color) {
59  setEnabled(false);
60  }
61 
63  return new CellSelectionRenderer(*this);
64  }
65 
67  }
68 
70  return dynamic_cast<CellSelectionRenderer*>(cnt->getRenderer("CellSelectionRenderer"));
71  }
72 
74  m_locations.clear();
75  }
76 
78  if (loc) {
79  std::vector<Location>::const_iterator it = m_locations.begin();
80  for (; it != m_locations.end(); it++) {
81  if ((*it).getLayerCoordinates() == loc->getLayerCoordinates()) {
82  return;
83  }
84  }
85 
86  m_locations.push_back(Location(*loc));
87  }
88  }
89 
91  if (loc) {
92  std::vector<Location>::iterator it = m_locations.begin();
93  for (; it != m_locations.end(); it++) {
94  if ((*it).getLayerCoordinates() == loc->getLayerCoordinates()) {
95  m_locations.erase(it);
96  break;
97  }
98  }
99  }
100  }
101 
102  void CellSelectionRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
103  if (m_locations.empty()) {
104  return;
105  }
106 
107  std::vector<Location>::const_iterator locit = m_locations.begin();
108  for (; locit != m_locations.end(); locit++) {
109  const Location loc = *locit;
110  if (layer != loc.getLayer()) {
111  continue;
112  }
113 
114  CellGrid* cg = layer->getCellGrid();
115  if (!cg) {
116  FL_WARN(_log, "No cellgrid assigned to layer, cannot draw selection");
117  continue;
118  }
119 
120  std::vector<ExactModelCoordinate> vertices;
121  cg->getVertices(vertices, loc.getLayerCoordinates());
122  std::vector<ExactModelCoordinate>::const_iterator it = vertices.begin();
123  ScreenPoint firstpt = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
124  Point pt1(firstpt.x, firstpt.y);
125  Point pt2;
126  ++it;
127  for (; it != vertices.end(); it++) {
128  ScreenPoint pts = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
129  pt2.x = pts.x; pt2.y = pts.y;
130  Point cpt1 = pt1;
131  Point cpt2 = pt2;
132  m_renderbackend->drawLine(cpt1, cpt2, m_color.r, m_color.g, m_color.b);
133  pt1 = pt2;
134  }
135  m_renderbackend->drawLine(pt2, Point(firstpt.x, firstpt.y), m_color.r, m_color.g, m_color.b);
136  }
137  }
138 
140  m_color.r = r;
141  m_color.g = g;
142  m_color.b = b;
143  }
144 }
#define FL_WARN(logger, msg)
Definition: logger.h:72
SDL_Color m_color
currently used color
Abstract interface for all the renderbackends.
std::vector< RenderItem * > RenderList
Definition: renderitem.h:130
RendererBase * clone()
Makes copy of this renderer.
Interface to class owning the renderers Used to get correct subclass of renderer in scripting side (v...
Definition: rendererbase.h:66
static Logger _log(LM_AUDIO)
virtual RendererBase * getRenderer(const std::string &renderername)=0
Returns renderer with given name.
Camera describes properties of a view port shown in the main screen Main screen can have multiple cam...
Definition: camera.h:59
Layer * getLayer() const
Gets the layer where this location is pointing to.
Definition: location.cpp:83
void deselectLocation(const Location *loc)
Deselects given location on map.
ModelCoordinate getLayerCoordinates() const
Gets cell precision layer coordinates set to this location.
Definition: location.cpp:113
virtual void getVertices(std::vector< ExactModelCoordinate > &vtx, const ModelCoordinate &cell)=0
Fills given point vector with vertices from selected cell.
std::vector< Location > m_locations
selected locations
RenderBackend * m_renderbackend
Definition: rendererbase.h:171
unsigned char uint8_t
Definition: core.h:38
PointType2D< int32_t > Point
Definition: point.h:195
static CellSelectionRenderer * getInstance(IRendererContainer *cnt)
Gets instance for interface access.
virtual ~CellSelectionRenderer()
Destructor.
Base class for all view renderers View renderer renders one aspect of the view shown on screen...
Definition: rendererbase.h:78
A basic layer on a map.
Definition: layer.h:99
CellSelectionRenderer(RenderBackend *renderbackend, int32_t position)
Constructor.
virtual void drawLine(const Point &p1, const Point &p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Draws line between given points with given RGBA.
virtual void setEnabled(bool enabled)
Enables renderer.
CellSelectionRenderer renders a frame around selected cells.
CellGrid * getCellGrid() const
Get the Cellgrid.
Definition: layer.cpp:93
void reset()
Deselects all locations.
void selectLocation(const Location *loc)
Selects given location on map.
ScreenPoint toScreenCoordinates(const ExactModelCoordinate &map_coords)
Transforms given point from map coordinates to screen coordinates.
Definition: camera.cpp:428
A 3D Point.
Definition: point.h:205
ExactModelCoordinate toMapCoordinates(const ModelCoordinate &layer_coords)
Transforms given point from layer coordinates to map coordinates.
Definition: cellgrid.cpp:75
void render(Camera *cam, Layer *layer, RenderList &instances)
This method is called by the view to ask renderer to draw its rendering aspect based on given paramet...
void setColor(uint8_t r, uint8_t g, uint8_t b)
Changes the used color.