FIFE  2008.0
cellselectionrenderer.cpp
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 // Standard C++ library includes
00023 
00024 // 3rd party library includes
00025 
00026 // FIFE includes
00027 // These includes are split up in two parts, separated by one empty line
00028 // First block: files included from the FIFE root src directory
00029 // Second block: files included from the same folder
00030 #include "video/renderbackend.h"
00031 #include "util/math/fife_math.h"
00032 #include "util/log/logger.h"
00033 #include "model/metamodel/grids/cellgrid.h"
00034 #include "model/structures/instance.h"
00035 #include "model/structures/layer.h"
00036 #include "model/structures/location.h"
00037 
00038 #include "view/camera.h"
00039 #include "cellselectionrenderer.h"
00040 
00041 
00042 namespace FIFE {
00043     static Logger _log(LM_VIEWVIEW);
00044 
00045     CellSelectionRenderer::CellSelectionRenderer(RenderBackend* renderbackend, int position):
00046         RendererBase(renderbackend, position) {
00047         setEnabled(false);
00048         m_color.r = 255;
00049         m_color.g = 0;
00050         m_color.b = 0;
00051     }
00052 
00053     CellSelectionRenderer::CellSelectionRenderer(const CellSelectionRenderer& old):
00054         RendererBase(old),
00055         m_color(old.m_color) {
00056         setEnabled(false);
00057     }
00058 
00059     RendererBase* CellSelectionRenderer::clone() {
00060         return new CellSelectionRenderer(*this);
00061     }
00062 
00063     CellSelectionRenderer::~CellSelectionRenderer() {
00064     }
00065 
00066     CellSelectionRenderer* CellSelectionRenderer::getInstance(IRendererContainer* cnt) {
00067         return dynamic_cast<CellSelectionRenderer*>(cnt->getRenderer("CellSelectionRenderer"));
00068     }
00069     
00070     void CellSelectionRenderer::reset() {
00071         m_locations.clear();
00072     }
00073 
00074     void CellSelectionRenderer::selectLocation(const Location* loc) {
00075         if (loc) {
00076             std::vector<Location>::const_iterator it = m_locations.begin();
00077             for (; it != m_locations.end(); it++) {
00078                 if (*it == *loc) return;
00079             }
00080 
00081             m_locations.push_back(Location(*loc));
00082         }
00083     }
00084 
00085     void CellSelectionRenderer::deselectLocation(const Location* loc) {
00086         if (loc) {
00087             std::vector<Location>::iterator it = m_locations.begin();
00088             for (; it != m_locations.end(); it++) {
00089                 if (*it == *loc) {
00090                     m_locations.erase(it);
00091                     break;
00092                 }
00093             }
00094         }
00095     }
00096 
00097     void CellSelectionRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
00098         std::vector<Location>::const_iterator locit = m_locations.begin();
00099 
00100         for (; locit != m_locations.end(); locit++) {
00101             const Location loc = *locit;
00102             if (layer != loc.getLayer()) {
00103                 continue;
00104             }
00105             
00106             CellGrid* cg = layer->getCellGrid();
00107             if (!cg) {
00108                 FL_WARN(_log, "No cellgrid assigned to layer, cannot draw selection");
00109                 continue;
00110             }
00111 
00112             m_renderbackend->disableLighting();
00113 
00114             std::vector<ExactModelCoordinate> vertices;
00115             cg->getVertices(vertices, loc.getLayerCoordinates());
00116             std::vector<ExactModelCoordinate>::const_iterator it = vertices.begin();
00117             ScreenPoint firstpt = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
00118             Point pt1(firstpt.x, firstpt.y);
00119             Point pt2;
00120             ++it;
00121             for (; it != vertices.end(); it++) {
00122                 ScreenPoint pts = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
00123                 pt2.x = pts.x; pt2.y = pts.y;
00124                 Point cpt1 = pt1;
00125                 Point cpt2 = pt2;
00126                 m_renderbackend->drawLine(cpt1, cpt2, m_color.r, m_color.g, m_color.b);
00127                 pt1 = pt2;
00128             }
00129             m_renderbackend->drawLine(pt2, Point(firstpt.x, firstpt.y), m_color.r, m_color.g, m_color.b);
00130             m_renderbackend->enableLighting();
00131         }
00132     }
00133 
00134     void CellSelectionRenderer::setColor(Uint8 r, Uint8 g, Uint8 b) {
00135         m_color.r = r;
00136         m_color.g = g;
00137         m_color.b = b;
00138     }
00139 }