ORTS

SimplePathfinderWidget.C

Go to the documentation of this file.
00001 // $Id $
00002 
00003 // SimpleTerrain Widget Implementation
00004 #include "SimplePathfinderWidget.H"
00005 #include "SimplePathfinder.H"
00006 #include "GfxModule.H"
00007 #include "GfxGlobal.H"
00008 #include "Profiler.H"
00009 #include "PathfindTask.H"
00010 
00011 #include "Game.H"
00012 #include "GameStateModule.H"
00013 
00014 using namespace std;
00015 using boost::shared_ptr;
00016 
00017 typedef TerrainBase::Loc Loc;
00018 
00019 //===================================================================
00020 
00021 SimplePathfinderOverlay::SimplePathfinderOverlay(const SimplePathfinder &terrain, GfxModule &gfxm_)
00022   : dot(gfxm_), gfxm(&gfxm_)
00023 {
00024   enabled = true;
00025   terrain_p = &terrain;
00026 }
00027 
00028 //-------------------------------------------------------------------
00029 
00030 bool SimplePathfinderOverlay::handle_event(const Event &e)
00031 {
00032   if (e.get_who() == GfxModule::FROM) {
00033     if (e.get_what() == GfxModule::DRAW_MSG) {
00034       draw();
00035       return true;
00036     }
00037   }
00038   return false;
00039 }
00040 
00041 //-------------------------------------------------------------------
00042 
00043 void SimplePathfinderOverlay::draw()
00044 {
00045   PROFILE("TerrainOverlay");
00046   //glDisable(GL_TEXTURE_2D);
00047 
00048   if (!enabled) return;
00049   
00050   //bool draw_air = false;
00051   
00052   if(!terrain_p->initiated) return;
00053   //   shared_ptr<const ST_Terrain::PFEngine::Map> map;
00054   //   if (forcefield_p){
00055   //     if (!forcefield_p->pfEngine) return;
00056   //     map = draw_air ? forcefield_p->pfEngine->get_air_map() : forcefield_p->pfEngine->get_ground_map();
00057   //   }
00058   //   else{
00059   //     if (!terrain_p->pfEngine) return;
00060   //     map =  draw_air ? terrain_p->pfEngine->get_air_map() : terrain_p->pfEngine->get_ground_map();
00061   //   }
00062     
00063   
00064   //if (!st_terrain->pfEngine) return;
00065 
00066   //shared_ptr<const ST_Terrain::PFEngine::Map> map = draw_air ? st_terrain->pfEngine->get_air_map() : st_terrain->pfEngine->get_ground_map();
00067 
00068   sint4 map_width = terrain_p->tiles_x * terrain_p->tile_points;
00069   sint4 map_height = terrain_p->tiles_y * terrain_p->tile_points;
00070   
00071   sint4 gw = gfxm->get_game().get_map().get_width();
00072   sint4 gh = gfxm->get_game().get_map().get_height();
00073   
00074   real4 dx = (real4)gw / (real4)(map_width);
00075   real4 dy = (real4)gh / (real4)(map_height);
00076 
00077 //   {
00078 //     PROFILE("draw tiles");
00079 // #if 1
00080 //     dot.begin_squares();
00081 //     FORS (xi, map_width) {
00082 //       FORS (yi, map_height) {
00083 //         if ((*map)(xi, yi).max_size > 0 || (*map)(xi, yi).cost != 0 ) {
00084           
00085 //           Vec3<real4> c;
00086 //           real4 v = (*map)(xi, yi).max_size / 20.0;
00087 //           c = Vec3<real4>(v, 0, 0);
00088 //             //          if ((*map)(xi, yi).max_size < 1) c = Vec3<real4>(1, 0, 0);
00089 //             //          if ((*map)(xi, yi).cost > 0)     c = Vec3<real4>(0, 1, 1);
00090 
00091 //           real4 x1 = (xi-0.5) * dx;
00092 //           real4 x2 = (xi+0.5) * dx;
00093 //           real4 y1 = (yi-0.5) * dy;
00094 //           real4 y2 = (yi+0.5) * dy;
00095           
00096 //           dot.draw_square(x1, y1, x2, y2, c);
00097 //         }
00098 //       }
00099 //     }
00100 //     dot.end_squares();
00101 // #endif
00102 //   }
00103  
00104   {
00105     PROFILE("draw lines");
00106  
00107     const std::list<PathfindTask> &tasks = terrain_p->tasks;
00108     
00109     Vec3<real4> c(1,0,0);
00110       
00111     dot.begin_lines();
00112  
00113     FORALL (tasks, j) {
00114       if (j->isPending) continue;
00115       //const UnitPath &upath = *j->path;
00116       const Vector<Loc> &path = j->path.locs;
00117       
00118       real4 x1, y1;
00119       if (path.size() > 1) {
00120     x1 = path[0].x;
00121     y1 = path[0].y;
00122     x1 *= dx;
00123     y1 *= dy;
00124 
00125     for (uint4 i = 1; i < path.size(); ++i) {
00126       real4 x2 = path[i].x;
00127       real4 y2 = path[i].y;
00128       x2 *= dx;
00129       y2 *= dy;
00130       //  std::cout << "drawing lines\n";
00131       dot.draw_line(x1, y1, x2, y2, c, 1.0);
00132           
00133       x1 = x2;
00134       y1 = y2;
00135     }
00136       }
00137     }
00138  
00139     
00140     dot.end_lines();
00141       
00142   }
00143     
00144   //glEnable(GL_TEXTURE_2D);
00145 }
00146 
00147 //===================================================================
00148 //===================================================================
00149 
00150 SimplePathfinderWidget::SimplePathfinderWidget(const SimplePathfinder &terrain, GfxModule *gfxm)
00151 {
00152   if (gfxm) {
00153     overlay = new SimplePathfinderOverlay(terrain, *gfxm);
00154     gfxm->add_handler(overlay);
00155     overlay->enable();
00156   } else {
00157     overlay = 0;
00158   }
00159 
00160   draw_tiles = true;
00161   draw_air = false;
00162   wwidth = 0;
00163   wheight = 0;
00164 
00165   set_name("Path Widget");
00166   terrain_p = &terrain;
00167   
00168 }
00169 
00170 
00171 //-------------------------------------------------------------------
00172 
00173 SimplePathfinderWidget::~SimplePathfinderWidget()
00174 {
00175   if (overlay) delete overlay;
00176 }
00177 
00178 //===================================================================
00179 
00180 stype SimplePathfinderWidget::event_key_down(stype key, stype /*mod*/, stype x, stype y)
00181 {
00182   if (is_outside(x,y)) return stype();
00183   cout << "event_key_down" << endl;
00184   
00185 //   shared_ptr<const ST_Terrain::PFEngine::Map> map;
00186 
00187 //   if (forcefield_p){  
00188 //     map = draw_air ? forcefield_p->pfEngine->get_air_map() : forcefield_p->pfEngine->get_ground_map();
00189 //   }
00190 //   else{
00191 //     map =  draw_air ? terrain_p->pfEngine->get_air_map() : terrain_p->pfEngine->get_ground_map();
00192 //   }
00193     
00194   
00195  //  shared_ptr<const ST_Terrain::PFEngine::Map> map = draw_air ? st_terrain->pfEngine->get_air_map() : st_terrain->pfEngine->get_ground_map();
00196 
00197   char index = (char)key.to_int();
00198   
00199   switch(index){
00200     
00201   case '\t':
00202     if (overlay) {
00203       if (overlay->is_enabled()) {
00204         overlay->disable();
00205       } else {
00206         overlay->enable();
00207       }
00208     }
00209     return stype(1);
00210 
00211   case 't':
00212     draw_tiles = !draw_tiles;
00213     return stype(1);
00214     
00215   case 'a':
00216     draw_air = !draw_air;
00217     //    map = draw_air ? air_map : gnd_map;
00218     return stype(1);
00219     
00220 //   case 'p':
00221 //     FORS (y, map->get_h()) {
00222 //       FORS (x, map->get_w()) {
00223 //         if ((*map)(x,y).max_size >= 0) cout << " ";
00224 //         cout << (int)((*map)(x, y).max_size);
00225 //       }
00226 //       cout << endl;
00227 //     }
00228 //   case 'c':
00229 //     FORS (y, map->get_h()) {
00230 //       FORS (x, map->get_w()) {
00231 //         cout << " ";
00232 //         cout << (int)((*map)(x, y).cost);
00233 //       }
00234 //       cout << endl;
00235 //     }
00236 //     return stype(1);
00237     
00238   default:
00239     break;
00240   }
00241 
00242   return stype();
00243 }
00244 
00245 //===================================================================
00246 
00247 stype SimplePathfinderWidget::draw()
00248 {
00249   if (!!off) return stype();
00250   
00251   // shared_ptr<const ST_Terrain::PFEngine::Map> map;
00252   
00253    if (!terrain_p->initiated) return stype();
00254 
00255 //   if (forcefield_p){
00256 //     if(!forcefield_p->pfEngine) return stype();
00257 //     map = draw_air ? forcefield_p->pfEngine->get_air_map() : forcefield_p->pfEngine->get_ground_map();
00258 //   }
00259 //   else{
00260 //     if(!terrain_p->pfEngine) return stype();
00261 //     map =  draw_air ? terrain_p->pfEngine->get_air_map() : terrain_p->pfEngine->get_ground_map();
00262 //   }
00263   
00264   //shared_ptr<const ST_Terrain::PFEngine::Map> map = draw_air ? st_terrain->pfEngine->get_air_map() : st_terrain->pfEngine->get_ground_map();
00265 
00266   wwidth = terrain_p->tiles_x * terrain_p->tile_points;
00267   wheight = terrain_p ->tiles_x * terrain_p->tile_points;
00268   
00269   sint4 x = this->x.to_int();
00270   sint4 y = this->y.to_int();
00271   sint4 w = this->w.to_int();
00272   sint4 h = this->h.to_int();
00273 
00274   real8 xd = (real8)w/(real8)wwidth;
00275   real8 yd = (real8)h/(real8)wheight;
00276 
00277   //draw_grid(wwidth, wheight);
00278   
00279   //   FORS (xi, wwidth) {
00280   //     FORS (yi, wheight) {
00281   //       if ((*map)(xi, yi).max_size <= 0 || (*map)(xi, yi).cost != 0 ) {
00282   //         if ((*map)(xi, yi).max_size < 1) glColor4f(1, 0, 0,  1.0);
00283   //         if ((*map)(xi, yi).cost > 0)     glColor4f(0, 1, 1,  1.0);
00284   //         glBegin(GL_QUADS);
00285         
00286   //         glVertex2f( x+xi*xd    , y+h-yi*yd   -yd );
00287   //         glVertex2f( x+xi*xd+xd , y+h-yi*yd   -yd );
00288   //         glVertex2f( x+xi*xd+xd , y+h-yi*yd+yd-yd );
00289   //         glVertex2f( x+xi*xd    , y+h-yi*yd+yd-yd );
00290         
00291   //         glEnd();
00292   //       }
00293   //     }
00294   //   }
00295   
00296 #if 1
00297   
00298   const std::list<PathfindTask> &tasks = terrain_p->tasks;
00299   FORALL (tasks, j) {
00300     if (j->isPending) continue;
00301 
00302     const Vector<Loc> &path = j->path.locs;
00303    
00304     glColor4f(1, 1, 1, 1);
00305     glBegin(GL_LINE_STRIP);
00306 
00307     FORALL (path, i) {
00308       sint4 wix = i->x;
00309       sint4 wiy = i->y;
00310       glVertex2f((real8)x+         ((real8)wix * xd),
00311          (real8)y+(real8)h-((real8)wiy * yd));
00312     }
00313 
00314     glEnd();
00315     
00316     //unit
00317     const Object *obj = *(j->task.objs.begin());
00318     sint4 ux, uy;
00319     obj->get_center(ux, uy);
00320     
00321     glColor4f(1, 1, 1, 1);
00322     glBegin(GL_QUADS);
00323 
00324     //sint4 wux = ux;
00325     //sint4 wuy = uy;
00326     
00327     glVertex2f(x+(ux*xd),    y+h-(uy*yd)-yd);
00328     glVertex2f(x+(ux*xd)+xd, y+h-(uy*yd)-yd);
00329     glVertex2f(x+(ux*xd)+xd, y+h-(uy*yd)   );
00330     glVertex2f(x+(ux*xd),    y+h-(uy*yd)   );
00331     
00332     glEnd();
00333   }
00334 
00335   
00336 
00337 #endif
00338     
00339   glEnable(GL_TEXTURE_2D);
00340   
00341   return stype();
00342 }
00343 
00344 
00345 //===================================================================
00346 
00347 


Generated on Fri May 18 2012 03:02:46 for ORTS by Doxygen1.7.3