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(true);
00048     }
00049 
00050     CellSelectionRenderer::CellSelectionRenderer(const CellSelectionRenderer& old):
00051         RendererBase(old) {
00052         setEnabled(true);
00053     }
00054 
00055     RendererBase* CellSelectionRenderer::clone() {
00056         return new CellSelectionRenderer(*this);
00057     }
00058 
00059     CellSelectionRenderer::~CellSelectionRenderer() {
00060     }
00061 
00062     CellSelectionRenderer* CellSelectionRenderer::getInstance(IRendererContainer* cnt) {
00063         return dynamic_cast<CellSelectionRenderer*>(cnt->getRenderer("CellSelectionRenderer"));
00064     }
00065     
00066     void CellSelectionRenderer::reset() {
00067         m_locations.clear();
00068     }
00069 
00070     void CellSelectionRenderer::selectLocation(const Location* loc) {
00071         if (loc) {
00072             std::vector<Location>::const_iterator it = m_locations.begin();
00073             for (; it != m_locations.end(); it++) {
00074                 if (*it == *loc) return;
00075             }
00076 
00077             m_locations.push_back(Location(*loc));
00078         }
00079     }
00080 
00081     void CellSelectionRenderer::deselectLocation(const Location* loc) {
00082         if (loc) {
00083             std::vector<Location>::iterator it = m_locations.begin();
00084             for (; it != m_locations.end(); it++) {
00085                 if (*it == *loc) {
00086                     m_locations.erase(it);
00087                     break;
00088                 }
00089             }
00090         }
00091     }
00092 
00093     void CellSelectionRenderer::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances) {
00094         std::vector<Location>::const_iterator locit = m_locations.begin();
00095 
00096         for (; locit != m_locations.end(); locit++) {
00097             const Location loc = *locit;
00098             if (layer != loc.getLayer()) {
00099                 continue;
00100             }
00101             
00102             CellGrid* cg = layer->getCellGrid();
00103             if (!cg) {
00104                 FL_WARN(_log, "No cellgrid assigned to layer, cannot draw selection");
00105                 continue;
00106             }
00107 
00108             std::vector<ExactModelCoordinate> vertices;
00109             cg->getVertices(vertices, loc.getLayerCoordinates());
00110             std::vector<ExactModelCoordinate>::const_iterator it = vertices.begin();
00111             ScreenPoint firstpt = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
00112             Point pt1(firstpt.x, firstpt.y);
00113             Point pt2;
00114             ++it;
00115             for (; it != vertices.end(); it++) {
00116                 ScreenPoint pts = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
00117                 pt2.x = pts.x; pt2.y = pts.y;
00118                 Point cpt1 = pt1;
00119                 Point cpt2 = pt2;
00120                 m_renderbackend->drawLine(cpt1, cpt2, 255, 0, 0);
00121                 pt1 = pt2;
00122             }
00123             m_renderbackend->drawLine(pt2, Point(firstpt.x, firstpt.y), 255, 0, 0);
00124         }
00125     }
00126 }
Generated by  doxygen 1.6.2-20100208