1 // winlocations.cpp
2 //
3 // Copyright (C) 2002, Chris Laurel <claurel@shatters.net>
4 //
5 // Miscellaneous utilities for Locations UI implementation.
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 
12 #include "winlocations.h"
13 #include "res/resource.h"
14 #include <celutil/winutil.h>
15 
16 using namespace std;
17 
18 static const int FeatureSizeSliderRange = 100;
19 static const float MinFeatureSize = 1.0f;
20 static const float MaxFeatureSize = 100.0f;
21 
22 static const uint32 FilterOther = ~(Location::City |
23                                     Location::Observatory |
24                                     Location::LandingSite |
25                                     Location::Crater |
26                                     Location::Mons |
27                                     Location::Terra |
28                                     Location::EruptiveCenter |
29                                     Location::Vallis |
30                                     Location::Mare);
31 
LocationsProc(HWND hDlg,UINT message,UINT wParam,LONG lParam)32 static BOOL APIENTRY LocationsProc(HWND hDlg,
33                                    UINT message,
34                                    UINT wParam,
35                                    LONG lParam)
36 {
37     LocationsDialog* dlg = reinterpret_cast<LocationsDialog*>(GetWindowLong(hDlg, DWL_USER));
38 
39     switch (message)
40     {
41     case WM_INITDIALOG:
42         {
43             dlg = reinterpret_cast<LocationsDialog*>(lParam);
44             if (dlg == NULL)
45                 return EndDialog(hDlg, 0);
46             SetWindowLong(hDlg, DWL_USER, lParam);
47 
48             // Store original settings in case user cancels the dialog
49             // dlg->initialLocationFlags = dlg->appCore->getSimulation()->getActiveObserver();
50             dlg->initialLocationFlags = 0;
51             dlg->initialFeatureSize = dlg->appCore->getRenderer()->getMinimumFeatureSize();
52 
53             // Set dialog controls to reflect current label and render modes
54             dlg->SetControls(hDlg);
55 
56             return(TRUE);
57         }
58         break;
59 
60     case WM_COMMAND:
61     {
62         Observer* obs = dlg->appCore->getSimulation()->getActiveObserver();
63         uint32 locationFilter = obs->getLocationFilter();
64 
65         switch (LOWORD(wParam))
66         {
67         case IDC_SHOW_CITIES:
68             obs->setLocationFilter(locationFilter ^ Location::City);
69             break;
70         case IDC_SHOW_OBSERVATORIES:
71             obs->setLocationFilter(locationFilter ^ Location::Observatory);
72             break;
73         case IDC_SHOW_LANDING_SITES:
74             obs->setLocationFilter(locationFilter ^ Location::LandingSite);
75             break;
76         case IDC_SHOW_MONTES:
77             obs->setLocationFilter(locationFilter ^ Location::Mons);
78             break;
79         case IDC_SHOW_MARIA:
80             obs->setLocationFilter(locationFilter ^ Location::Mare);
81             break;
82         case IDC_SHOW_CRATERS:
83             obs->setLocationFilter(locationFilter ^ Location::Crater);
84             break;
85         case IDC_SHOW_VALLES:
86             obs->setLocationFilter(locationFilter ^ Location::Vallis);
87             break;
88         case IDC_SHOW_TERRAE:
89             obs->setLocationFilter(locationFilter ^ Location::Terra);
90             break;
91         case IDC_SHOW_VOLCANOES:
92             obs->setLocationFilter(locationFilter ^ Location::EruptiveCenter);
93             break;
94         case IDC_SHOW_OTHERS:
95             obs->setLocationFilter(locationFilter ^ FilterOther);
96             break;
97         case IDC_LABELFEATURES:
98             {
99                 Renderer* renderer = dlg->appCore->getRenderer();
100                 uint32 labelMode = renderer->getLabelMode();
101                 renderer->setLabelMode(labelMode ^ Renderer::LocationLabels);
102                 break;
103             }
104         case IDOK:
105             if (dlg != NULL && dlg->parent != NULL)
106             {
107                 SendMessage(dlg->parent, WM_COMMAND, IDCLOSE,
108                             reinterpret_cast<LPARAM>(dlg));
109             }
110             EndDialog(hDlg, 0);
111             return TRUE;
112         case IDCANCEL:
113             if (dlg != NULL && dlg->parent != NULL)
114             {
115                 // Reset render flags, label mode, and hud detail to
116                 // initial values
117                 dlg->RestoreSettings(hDlg);
118                 SendMessage(dlg->parent, WM_COMMAND, IDCLOSE,
119                             reinterpret_cast<LPARAM>(dlg));
120             }
121             EndDialog(hDlg, 0);
122             return TRUE;
123         }
124         break;
125     }
126 
127     case WM_DESTROY:
128         if (dlg != NULL && dlg->parent != NULL)
129         {
130             SendMessage(dlg->parent, WM_COMMAND, IDCLOSE,
131                         reinterpret_cast<LPARAM>(dlg));
132         }
133         return TRUE;
134 
135     case WM_HSCROLL:
136         {
137             WORD sbValue = LOWORD(wParam);
138             LRESULT sliderPos;
139 
140             if (sbValue == SB_THUMBTRACK)
141                 sliderPos = HIWORD(wParam);
142             else
143                 sliderPos = SendMessage(GetDlgItem(hDlg, IDC_SLIDER_FEATURE_SIZE), TBM_GETPOS, 0, 0);
144 
145             char val[16];
146             HWND hwnd = GetDlgItem(hDlg, IDC_EDIT_FEATURE_SIZE);
147             float featureSize = (float) sliderPos / (float) FeatureSizeSliderRange;
148             featureSize = MinFeatureSize + (MaxFeatureSize - MinFeatureSize) * featureSize;
149             sprintf(val, "%d", (int) featureSize);
150             SetWindowText(hwnd, val);
151 
152             dlg->appCore->getRenderer()->setMinimumFeatureSize(featureSize);
153         }
154     }
155 
156     return FALSE;
157 }
158 
159 
LocationsDialog(HINSTANCE appInstance,HWND _parent,CelestiaCore * _appCore)160 LocationsDialog::LocationsDialog(HINSTANCE appInstance,
161                                  HWND _parent,
162                                  CelestiaCore* _appCore) :
163     CelestiaWatcher(*_appCore),
164     appCore(_appCore),
165     parent(_parent)
166 {
167     hwnd = CreateDialogParam(appInstance,
168                              MAKEINTRESOURCE(IDD_LOCATIONS),
169                              parent,
170                              LocationsProc,
171                              reinterpret_cast<LONG>(this));
172 }
173 
174 
dlgCheck(HWND hDlg,WORD item,uint32 flags,uint32 f)175 static void dlgCheck(HWND hDlg, WORD item, uint32 flags, uint32 f)
176 {
177     SendDlgItemMessage(hDlg, item, BM_SETCHECK,
178                        ((flags & f) != 0) ? BST_CHECKED : BST_UNCHECKED, 0);
179 }
180 
181 
SetControls(HWND hDlg)182 void LocationsDialog::SetControls(HWND hDlg)
183 {
184     Observer* obs = appCore->getSimulation()->getActiveObserver();
185     uint32 locFilter = obs->getLocationFilter();
186 
187     dlgCheck(hDlg, IDC_SHOW_CITIES,        locFilter, Location::City);
188     dlgCheck(hDlg, IDC_SHOW_OBSERVATORIES, locFilter, Location::Observatory);
189     dlgCheck(hDlg, IDC_SHOW_LANDING_SITES, locFilter, Location::LandingSite);
190     dlgCheck(hDlg, IDC_SHOW_MONTES,        locFilter, Location::Mons);
191     dlgCheck(hDlg, IDC_SHOW_MARIA,         locFilter, Location::Mare);
192     dlgCheck(hDlg, IDC_SHOW_CRATERS,       locFilter, Location::Crater);
193     dlgCheck(hDlg, IDC_SHOW_VALLES,        locFilter, Location::Vallis);
194     dlgCheck(hDlg, IDC_SHOW_TERRAE,        locFilter, Location::Terra);
195     dlgCheck(hDlg, IDC_SHOW_VOLCANOES,        locFilter, Location::EruptiveCenter);
196     dlgCheck(hDlg, IDC_SHOW_OTHERS,        locFilter, Location::Other);
197 
198     uint32 labelMode = appCore->getRenderer()->getLabelMode();
199     dlgCheck(hDlg, IDC_LABELFEATURES,     labelMode, Renderer::LocationLabels);
200 
201     // Set up feature size slider
202     SendDlgItemMessage(hDlg,
203                        IDC_SLIDER_FEATURE_SIZE,
204                        TBM_SETRANGE,
205                        (WPARAM)TRUE,
206                        (LPARAM) MAKELONG(0, FeatureSizeSliderRange));
207     float featureSize = appCore->getRenderer()->getMinimumFeatureSize();
208     int sliderPos = (int) (FeatureSizeSliderRange *
209                            (featureSize - MinFeatureSize) /
210                            (MaxFeatureSize - MinFeatureSize));
211     SendDlgItemMessage(hDlg,
212                        IDC_SLIDER_FEATURE_SIZE,
213                        TBM_SETPOS,
214                        (WPARAM) TRUE,
215                        (LPARAM) sliderPos);
216 
217     char val[16];
218     HWND hwnd = GetDlgItem(hDlg, IDC_EDIT_FEATURE_SIZE);
219     sprintf(val, "%d", (int) featureSize);
220     SetWindowText(hwnd, val);
221 }
222 
223 
RestoreSettings(HWND hDlg)224 void LocationsDialog::RestoreSettings(HWND hDlg)
225 {
226 }
227 
notifyChange(CelestiaCore *,int)228 void LocationsDialog::notifyChange(CelestiaCore*, int)
229 {
230     if (parent != NULL)
231         SetControls(hwnd);
232 }
233