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, int32_t 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 if (m_locations.empty()) {
00099 return;
00100 }
00101
00102 std::vector<Location>::const_iterator locit = m_locations.begin();
00103 for (; locit != m_locations.end(); locit++) {
00104 const Location loc = *locit;
00105 if (layer != loc.getLayer()) {
00106 continue;
00107 }
00108
00109 CellGrid* cg = layer->getCellGrid();
00110 if (!cg) {
00111 FL_WARN(_log, "No cellgrid assigned to layer, cannot draw selection");
00112 continue;
00113 }
00114
00115 std::vector<ExactModelCoordinate> vertices;
00116 cg->getVertices(vertices, loc.getLayerCoordinates());
00117 std::vector<ExactModelCoordinate>::const_iterator it = vertices.begin();
00118 ScreenPoint firstpt = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
00119 Point pt1(firstpt.x, firstpt.y);
00120 Point pt2;
00121 ++it;
00122 for (; it != vertices.end(); it++) {
00123 ScreenPoint pts = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
00124 pt2.x = pts.x; pt2.y = pts.y;
00125 Point cpt1 = pt1;
00126 Point cpt2 = pt2;
00127 m_renderbackend->drawLine(cpt1, cpt2, m_color.r, m_color.g, m_color.b);
00128 pt1 = pt2;
00129 }
00130 m_renderbackend->drawLine(pt2, Point(firstpt.x, firstpt.y), m_color.r, m_color.g, m_color.b);
00131 }
00132 }
00133
00134 void CellSelectionRenderer::setColor(uint8_t r, uint8_t g, uint8_t b) {
00135 m_color.r = r;
00136 m_color.g = g;
00137 m_color.b = b;
00138 }
00139 }