00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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 }