Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

DialogTeleport.cpp

Go to the documentation of this file.
00001 /*
00002  * DialogTeleport.cpp
00003  *
00004  * Part of Fly! Legacy project
00005  *
00006  * Copyright 2003 Chris Wallace
00007  *
00008  * Fly! Legacy is free software; you can redistribute it and/or modify
00009  *   it under the terms of the GNU General Public License as published by
00010  *   the Free Software Foundation; either version 2 of the License, or
00011  *   (at your option) any later version.
00012  *
00013  * Fly! Legacy is distributed in the hope that it will be useful,
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *   GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  *   along with Fly! Legacy; if not, write to the Free Software
00020  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00024 
00025 #include "../Include/FlyLegacy.h"
00026 #include "../Include/Globals.h"
00027 #include "../Include/Ui.h"
00028 #include "../Include/Terrain.h"
00029 #include "../Include/Situation.h"
00030 
00031 
00032 char *lathemi_list[] =
00033 {
00034   "S",
00035   "N",
00036   NULL
00037 };
00038 
00039 char *lonhemi_list[] =
00040 {
00041   "E",
00042   "W",
00043   NULL
00044 };
00045 
00046 
00047 typedef struct {
00048   puDialogBox*  dialog;
00049   puFrame*    frame;
00050   puText*     title;
00051   puInput*    icaofaa;
00052   puOneShot*    aplookup;
00053   puText*     apname;
00054   puSelectBox*  lathemi;
00055   puInput*    latdeg;
00056   puInput*    latmin;
00057   puInput*    latsec;
00058   puSelectBox*  lonhemi;
00059   puInput*    londeg;
00060   puInput*    lonmin;
00061   puInput*    lonsec;
00062   puInput*    alt;
00063   puOneShot*    teleport;
00064   puOneShot*    ok;
00065   puOneShot*    cancel;
00066 
00067   char      apName[80];
00068 } STeleportDialogData;
00069 
00070 
00071 static STeleportDialogData    *data = NULL;
00072 
00073 
00074 void teleport_dlg_kill (void)
00075 {
00076   if (data == NULL) return;
00077 
00078   // Deleting the PUI dialog will delete all child widgets as well
00079   delete data->dialog;
00080 
00081   // Delete dialog data structure
00082   delete data;
00083   data = NULL;
00084 }
00085 
00086 static void teleport_dlg_sync (void)
00087 {
00088   if (data == NULL) return;
00089 
00090   int i;
00091   float f;
00092 
00093   // Update airport name
00094   data->apname->setLabel (data->apName);
00095 
00096   // Validate latitude degrees
00097   i = data->latdeg->getIntegerValue ();
00098   if (i < 0) i = -i;
00099   if (i > 85) i = 85;
00100   data->latdeg->setValue (i);
00101 
00102   // Validate latitude minutes
00103   i = data->latmin->getIntegerValue ();
00104   if (i < 0) i = 0;
00105   if (i > 59) i = 59;
00106   data->latmin->setValue (i);
00107 
00108   // Validate latitude seconds
00109   f = data->latsec->getFloatValue ();
00110   if (f < 0) f = 0;
00111   if (f > 59.999) f = 59.999;
00112   data->latsec->setValue (f);
00113 
00114   // Validate longitude degrees
00115   i = data->londeg->getIntegerValue ();
00116   if (i < 0) i = 0;
00117   if (i > 179) i = 179;
00118   data->londeg->setValue (i);
00119 
00120   // Validate longitude minutes
00121   i = data->lonmin->getIntegerValue ();
00122   if (i < 0) i = 0;
00123   if (i > 59) i = 59;
00124   data->lonmin->setValue (i);
00125 
00126   // Validate longitude seconds
00127   f = data->lonsec->getFloatValue ();
00128   if (f < 0) f = 0;
00129   if (f > 59.999) f = 59.999;
00130   data->lonsec->setValue (f);
00131 }
00132 
00133 static void do_teleport (void)
00134 {
00135   // Get new position
00136   SPosition pos;
00137   pos.lat = (data->latdeg->getIntegerValue() * 3600) +
00138           (data->latmin->getIntegerValue() * 60) +
00139         data->latsec->getFloatValue();
00140   int hemi = data->lathemi->getCurrentItem ();
00141   if (hemi == 0) {
00142     // Southern hemisphere
00143     pos.lat = -pos.lat;
00144   }
00145 
00146   pos.lon = (data->londeg->getIntegerValue() * 3600) +
00147           (data->lonmin->getIntegerValue() * 60) +
00148         data->lonsec->getFloatValue();
00149   hemi = data->lonhemi->getCurrentItem ();
00150   if (hemi == 1) {
00151     // Western hemisphere
00152     pos.lon = (360 * 3600) - pos.lon;
00153   }
00154 
00156   pos.alt = data->alt->getFloatValue();
00157 
00158   // Set new user aircraft position
00159   CVehicleObject *user = globals->sit->GetUserVehicle ();
00160   user->SetPosition (pos);
00161 }
00162 
00163 
00164 static void init_position (SPosition pos)
00165 {
00166   // Initialize latitude fields
00167   if (pos.lat > 0) {
00168     // Northern hemisphere
00169     data->lathemi->setCurrentItem (1);
00170   } else {
00171     // Southern hemisphere
00172     data->lathemi->setCurrentItem (0);
00173     pos.lat = -pos.lat;
00174   }
00175   data->latdeg->setValue ((int)(pos.lat / 3600));
00176   pos.lat = fmod (pos.lat, 3600);
00177   data->latmin->setValue ((int)(pos.lat / 60));
00178   pos.lat = fmod (pos.lat, 60);
00179   data->latsec->setValue ((float)(pos.lat));
00180 
00181   // Initialize longitude fields
00182   if (pos.lon > (180 * 3600)) {
00183     // Western hemisphere
00184     data->lonhemi->setCurrentItem (1);
00185     pos.lon = (360 * 3600) - pos.lon;
00186   } else {
00187     // Eastern hemisphere
00188     data->lonhemi->setCurrentItem (0);
00189   }
00190   data->londeg->setValue ((int)(pos.lon / 3600));
00191   pos.lon = fmod (pos.lon, 3600);
00192   data->lonmin->setValue ((int)(pos.lon / 60));
00193   pos.lon = fmod (pos.lon, 60);
00194   data->lonsec->setValue ((int)(pos.lon));
00195 
00196   data->alt->setValue ((float)pos.alt);
00197 }
00198 
00199 
00200 static void teleport_teleport_cb (puObject* obj)
00201 {
00202   // Teleport, but don't close window
00203   do_teleport ();
00204 }
00205 
00206 static void teleport_ok_cb (puObject* obj)
00207 {
00208   // Teleport and kill dialog box
00209   do_teleport ();
00210   teleport_dlg_kill ();
00211 }
00212 
00213 static void teleport_cancel_cb (puObject* obj)
00214 {
00215   // Close dialog box without applying new date/time
00216   teleport_dlg_kill ();
00217 }
00218 
00219 static void teleport_aplookup_cb (puObject* obj)
00220 {
00221   // Get key lookup from input box
00222   char key[80];
00223   data->icaofaa->getValue (key);
00224   for (unsigned int i=0; i<strlen(key); i++) key[i] = toupper(key[i]);
00225   // Non-portable function for upper case conversion
00226   // _strupr (key);
00227 
00228   SAirport *ap;
00229   if (SearchAirportsByICAO(key, &ap) != 0) {
00230     // Found a match on ICAO identifier
00231     strcpy (data->apName, ap->name);
00232     ap->pos.alt = data->alt->getFloatValue();
00233     init_position (ap->pos);
00234   } else if (SearchAirportsByFAA(key, &ap) != 0) {
00235     // Found a match on FAA identifier
00236     strcpy (data->apName, ap->name);
00237     ap->pos.alt = data->alt->getFloatValue();
00238     init_position (ap->pos);
00239   }
00240   FreeAirport (ap);
00241 
00242   teleport_dlg_sync ();
00243 }
00244 
00245 static void teleport_sync_cb (puObject* obj)
00246 {
00247   teleport_dlg_sync ();
00248 }
00249 
00250 void teleport_dlg_create (void)
00251 {
00252   if (data != NULL) return;
00253 
00254   // Instantiate data structure for dialog contents
00255   data = new STeleportDialogData;
00256   strcpy (data->apName, "");
00257 
00258   // Create dialog box
00259   data->dialog = new puDialogBox (20, 300);
00260   {
00261     // Frame
00262     data->frame = new puFrame (0, 0, 380, 250);
00263 
00264     data->title = new puText (10, 220);
00265     data->title->setLabel ("TELEPORT");
00266 
00267     // Airport lookup label
00268     puText *aplabel = new puText (10, 200);
00269     aplabel->setLabel ("ICAO/FAA:");
00270     
00271     // Airport ID for lookup
00272     data->icaofaa = new puInput (100, 200, 150, 220);
00273     data->icaofaa->setStyle (PUSTYLE_BOXED);
00274 
00275     // Lookup airport ID
00276     data->aplookup = new puOneShot (280, 200, 340, 220);
00277     data->aplookup->setLegend ("Lookup");
00278     data->aplookup->setStyle (PUSTYLE_SMALL_SHADED);
00279     data->aplookup->setCallback (teleport_aplookup_cb);
00280 
00281     // Airport name label
00282     puText *apnamelabel = new puText (10, 180);
00283     apnamelabel->setLabel ("Airport:");
00284     
00285     // Airport name (following lookup)
00286     data->apname = new puText (100, 180);
00287     data->apname->setLabel (data->apName);
00288     data->apname->setStyle (PUSTYLE_BOXED);
00289 
00290     // Latitude label
00291     puText *latlabel = new puText (10, 140);
00292     latlabel->setLabel ("Latitude:");
00293 
00294     // Latitude hemisphere
00295     data->lathemi = new puSelectBox (100, 140, 150, 160, lathemi_list);
00296     data->lathemi->setLabelPlace (PUPLACE_CENTERED_LEFT);
00297     data->lathemi->setStyle (PUSTYLE_BOXED);
00298 
00299     // Latitude degrees
00300     data->latdeg = new puInput (160, 140, 220, 160);
00301     data->latdeg->setLabel ("Deg");
00302     data->latdeg->setLabelPlace (PUPLACE_TOP_CENTERED);
00303     data->latdeg->setStyle (PUSTYLE_BOXED);
00304     data->latdeg->setCallback (teleport_sync_cb);
00305 
00306     // Latitude minutes
00307     data->latmin = new puInput (230, 140, 290, 160);
00308     data->latmin->setLabel ("Min");
00309     data->latmin->setLabelPlace (PUPLACE_TOP_CENTERED);
00310     data->latmin->setStyle (PUSTYLE_BOXED);
00311     data->latmin->setCallback (teleport_sync_cb);
00312 
00313     // Latitude seconds
00314     data->latsec = new puInput (300, 140, 360, 160);
00315     data->latsec->setLabel ("sec");
00316     data->latsec->setLabelPlace (PUPLACE_TOP_CENTERED);
00317     data->latsec->setStyle (PUSTYLE_BOXED);
00318     data->latsec->setCallback (teleport_sync_cb);
00319 
00320     // Longitude label
00321     puText *lonlabel = new puText (10, 100);
00322     lonlabel->setLabel ("Longitude:");
00323 
00324     // Longitude hemisphere
00325     data->lonhemi = new puSelectBox (100, 100, 150, 120, lonhemi_list);
00326     data->lonhemi->setLabelPlace (PUPLACE_CENTERED_LEFT);
00327     data->lonhemi->setStyle (PUSTYLE_BOXED);
00328 
00329     // Longitude degrees
00330     data->londeg = new puInput (160, 100, 220, 120);
00331     data->londeg->setLabel ("Deg");
00332     data->londeg->setLabelPlace (PUPLACE_TOP_CENTERED);
00333     data->londeg->setStyle (PUSTYLE_BOXED);
00334     data->londeg->setCallback (teleport_sync_cb);
00335 
00336     // Longitude minutes
00337     data->lonmin = new puInput (230, 100, 290, 120);
00338     data->lonmin->setLabel ("Min");
00339     data->lonmin->setLabelPlace (PUPLACE_TOP_CENTERED);
00340     data->lonmin->setStyle (PUSTYLE_BOXED);
00341     data->lonmin->setCallback (teleport_sync_cb);
00342 
00343     // Longitude seconds
00344     data->lonsec = new puInput (300, 100, 360, 120);
00345     data->lonsec->setLabel ("Sec");
00346     data->lonsec->setLabelPlace (PUPLACE_TOP_CENTERED);
00347     data->lonsec->setStyle (PUSTYLE_BOXED);
00348     data->lonsec->setCallback (teleport_sync_cb);
00349 
00350     // Altitude label
00351     puText *altlabel = new puText (10, 60);
00352     altlabel->setLabel ("Altitude:");
00353 
00354     // Altitude AGL
00355     data->alt = new puInput (100, 60, 150, 80);
00356     data->alt->setLabel ("Feet AGL");
00357     data->alt->setLabelPlace (PUPLACE_CENTERED_RIGHT);
00358     data->alt->setStyle (PUSTYLE_BOXED);
00359 
00360     // Teleport button
00361     data->teleport = new puOneShot (20, 20, 100, 40);
00362     data->teleport->setLegend ("Teleport");
00363     data->teleport->makeReturnDefault (true);
00364     data->teleport->setStyle (PUSTYLE_SMALL_SHADED);
00365     data->teleport->setCallback (teleport_teleport_cb);
00366 
00367     // OK button
00368     data->ok = new puOneShot (200, 20, 260, 40);
00369     data->ok->setLegend ("OK");
00370     data->ok->makeReturnDefault (true);
00371     data->ok->setStyle (PUSTYLE_SMALL_SHADED);
00372     data->ok->setCallback (teleport_ok_cb);
00373 
00374     // Cancel button
00375     data->cancel = new puOneShot (280, 20, 340, 40);
00376     data->cancel->setLegend ("Cancel");
00377     data->cancel->setStyle (PUSTYLE_SMALL_SHADED);
00378     data->cancel->setCallback (teleport_cancel_cb);
00379 
00380     // Get current user aircraft position
00381     CVehicleObject *user = globals->sit->GetUserVehicle ();
00382     SPosition pos = user->GetPosition();
00383     init_position (pos);
00384 
00385     // Synchronize data
00386     teleport_dlg_sync ();
00387   }
00388   data->dialog->close ();
00389   data->dialog->reveal ();
00390 }
00391 
SourceForge.net Logo Documentation generated by doxygen