wingotodlg.cpp
上传用户:center1979
上传日期:2022-07-26
资源大小:50633k
文件大小:6k
源码类别:

OpenGL

开发平台:

Visual C++

  1. // wingotodlg.cpp
  2. // 
  3. // Copyright (C) 2001, Chris Laurel <claurel@shatters.net>
  4. //
  5. // Goto object dialog for Windows.
  6. //
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU General Public License
  9. // as published by the Free Software Foundation; either version 2
  10. // of the License, or (at your option) any later version.
  11. #include <windows.h>
  12. #include "wingotodlg.h"
  13. #include "celutil/winutil.h"
  14. #include "res/resource.h"
  15. using namespace std;
  16. static bool GetDialogFloat(HWND hDlg, int id, float& f)
  17. {
  18.     char buf[128];
  19.     
  20.     if (GetDlgItemText(hDlg, id, buf, sizeof buf) > 0 &&
  21.         sscanf(buf, " %f", &f) == 1)
  22.         return true;
  23.     else
  24.         return false;
  25. }
  26. static bool SetDialogFloat(HWND hDlg, int id, char* format, float f)
  27. {
  28.     char buf[128];
  29.     sprintf(buf, format, f);
  30.     return (SetDlgItemText(hDlg, id, buf) == TRUE);
  31. }
  32. static BOOL APIENTRY GotoObjectProc(HWND hDlg,
  33.                                     UINT message,
  34.                                     UINT wParam,
  35.                                     LONG lParam)
  36. {
  37.     GotoObjectDialog* gotoDlg = reinterpret_cast<GotoObjectDialog*>(GetWindowLong(hDlg, DWL_USER));
  38.     switch (message)
  39.     {
  40.     case WM_INITDIALOG:
  41.         {
  42.             GotoObjectDialog* gotoDlg = reinterpret_cast<GotoObjectDialog*>(lParam);
  43.             if (gotoDlg == NULL)
  44.                 return EndDialog(hDlg, 0);
  45.             SetWindowLong(hDlg, DWL_USER, lParam);
  46.             CheckRadioButton(hDlg,
  47.                              IDC_RADIO_KM, IDC_RADIO_RADII,
  48.                              IDC_RADIO_KM);
  49.             //Initialize name, distance, latitude and longitude edit boxes with current values.
  50.             Simulation* sim = gotoDlg->appCore->getSimulation();
  51.             double distance, longitude, latitude;
  52.             sim->getSelectionLongLat(distance, longitude, latitude);
  53.             //Display information in format appropriate for object
  54.             if (sim->getSelection().body() != NULL)
  55.             {
  56.                 distance = distance - (double) sim->getSelection().body()->getRadius();
  57.                 SetDialogFloat(hDlg, IDC_EDIT_DISTANCE, "%.1f", (float)distance);
  58.                 SetDialogFloat(hDlg, IDC_EDIT_LONGITUDE, "%.5f", (float)longitude);
  59.                 SetDialogFloat(hDlg, IDC_EDIT_LATITUDE, "%.5f", (float)latitude);
  60.                 SetDlgItemText(hDlg, IDC_EDIT_OBJECTNAME,
  61.                  const_cast<char*>(UTF8ToCurrentCP(sim->getSelection().body()->getName(true)).c_str()));
  62.             }
  63. //            else if (sim->getSelection().star != NULL)
  64. //            {
  65. //                //Code to obtain searchable star name
  66. //               SetDlgItemText(hDlg, IDC_EDIT_OBJECTNAME, (char*)sim->getSelection().star->);
  67. //            }
  68.             return(TRUE);
  69.         }
  70.         break;
  71.     case WM_COMMAND:
  72.         if (LOWORD(wParam) == IDC_BUTTON_GOTO)
  73.         {
  74.             char buf[1024], out[1024];
  75.             wchar_t wbuff[1024];
  76.             int len = GetDlgItemText(hDlg, IDC_EDIT_OBJECTNAME, buf, sizeof buf);
  77.             Simulation* sim = gotoDlg->appCore->getSimulation();
  78.             Selection sel;
  79.             if (len > 0) 
  80.             {
  81.                 int wlen = MultiByteToWideChar(CP_ACP, 0, buf, -1, wbuff, sizeof(wbuff));
  82.                 WideCharToMultiByte(CP_UTF8, 0, wbuff, wlen, out, sizeof(out), NULL, NULL);
  83.                 sel = sim->findObjectFromPath(string(out), true);
  84.             }
  85.             
  86.             if (!sel.empty())
  87.             {
  88.                 sim->setSelection(sel);
  89.                 sim->geosynchronousFollow();
  90.                 float distance = (float) (sel.radius() * 5);
  91.                 if (GetDialogFloat(hDlg, IDC_EDIT_DISTANCE, distance))
  92.                 {
  93.                     if (IsDlgButtonChecked(hDlg, IDC_RADIO_AU) == BST_CHECKED)
  94.                         distance = astro::AUtoKilometers(distance);
  95.                     else if (IsDlgButtonChecked(hDlg, IDC_RADIO_RADII) == BST_CHECKED)
  96.                         distance = distance * (float) sel.radius();
  97.                     
  98.                     distance += (float) sel.radius();
  99.                 }
  100.                 distance = astro::kilometersToLightYears(distance);
  101.                 float longitude, latitude;
  102.                 if (GetDialogFloat(hDlg, IDC_EDIT_LONGITUDE, longitude) &&
  103.                     GetDialogFloat(hDlg, IDC_EDIT_LATITUDE, latitude))
  104.                 {
  105.                     sim->gotoSelectionLongLat(5.0,
  106.                                               distance,
  107.                                               degToRad(longitude),
  108.                                               degToRad(latitude),
  109.                                               Vec3f(0, 1, 0));
  110.                 }
  111.                 else
  112.                 {
  113.                     sim->gotoSelection(5.0,
  114.                                        distance,
  115.                                        Vec3f(0, 1, 0),
  116.                                        ObserverFrame::ObserverLocal);
  117.                 }
  118.             }
  119.             return TRUE;
  120.         }
  121.         else if (LOWORD(wParam) == IDCANCEL)
  122.         {
  123.             if (gotoDlg != NULL && gotoDlg->parent != NULL)
  124.             {
  125.                 SendMessage(gotoDlg->parent, WM_COMMAND, IDCLOSE,
  126.                             reinterpret_cast<LPARAM>(gotoDlg));
  127.             }
  128.             EndDialog(hDlg, 0);
  129.             return TRUE;
  130.         }
  131.         break;
  132.     case WM_DESTROY:
  133.         if (gotoDlg != NULL && gotoDlg->parent != NULL)
  134.         {
  135.             SendMessage(gotoDlg->parent, WM_COMMAND, IDCLOSE,
  136.                         reinterpret_cast<LPARAM>(gotoDlg));
  137.         }
  138.         EndDialog(hDlg, 0);
  139.         return TRUE;
  140.     }
  141.     return FALSE;
  142. }
  143. GotoObjectDialog::GotoObjectDialog(HINSTANCE appInstance,
  144.                                    HWND _parent,
  145.                                    CelestiaCore* _appCore) :
  146.     appCore(_appCore),
  147.     parent(_parent)
  148. {
  149.     hwnd = CreateDialogParam(appInstance,
  150.                              MAKEINTRESOURCE(IDD_GOTO_OBJECT),
  151.                              parent,
  152.                              GotoObjectProc,
  153.                              reinterpret_cast<LONG>(this));
  154. }