Debian Codegraph @

code/controls/LightingPresets.cpp

raw
1 /*
2 Copyright (C) 2009-2011 wxLauncher Team
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include "controls/LightingPresets.h"
20
21 #include "apis/ProfileProxy.h"
22 #include "apis/TCManager.h"
23
24 #include "controls/ModList.h"
25
26 #include "global/ids.h"
27 #include "global/MemoryDebugging.h"
28 #include "global/ModDefaults.h"
29
30 #include "generated/configure_launcher.h"
31
32 #include <wx/gbsizer.h>
33 #include <wx/hyperlink.h>
34
35 Preset::Preset(const wxString& name, const int buttonId, const wxString& flagSet):
36 name(name), buttonId(buttonId), flagSet(flagSet) {
37 }
38
39 void Preset::SetFlagSet(const wxString& flagSet) {
40 wxCHECK_RET(!flagSet.IsEmpty(), _T("SetFlagSet() given empty flag set!"));
41
42 this->flagSet = flagSet;
43 }
44
45 // vertical spacing between radio buttons is platform-specific
46 #if IS_WIN32
47 const int RADIOBUTTON_SPACING = 20;
48 #elif IS_LINUX
49 const int RADIOBUTTON_SPACING = 5;
50 #else
51 const int RADIOBUTTON_SPACING = 10;
52 #endif
53
54 const int DEFAULT_PRESET_ID = ID_PRESETS_OFF;
55 PresetHashMap LightingPresets::presets;
56
57 LightingPresets::LightingPresets(wxWindow* parent) : wxPanel(parent, wxID_ANY) {
58 if (presets.size() == 0) {
59 InitializePresets();
60 }
61
62 TCManager::RegisterTCActiveModChanged(this);
63 ProfileProxy::GetProxy()->RegisterProxyFlagDataReady(this);
64
65 wxStaticBox* lightingPresetsBox = new wxStaticBox(this, wxID_ANY, _("Lighting presets"));
66
67 wxHyperlinkCtrl* presetDescsUrl =
68 new wxHyperlinkCtrl(this, wxID_ANY, _T("Preset descriptions"),
69 _T("http://www.hard-light.net/wiki/index.php/Sample_Lighting_Settings"));
70 wxButton* customFlagsCopyButton =
71 new wxButton(this, ID_COPY_PRESET_BUTTON, _T("Copy selected preset to custom flags"));
72 wxRadioButton* radioButton1 = new wxRadioButton (this, ID_PRESETS_OFF, _T("Presets off"),
73 wxDefaultPosition, wxDefaultSize, wxRB_GROUP);
74 wxRadioButton* radioButton2 =
75 new wxRadioButton (this, ID_PRESET_BASELINE,
76 DEFAULT_MOD_RECOMMENDED_LIGHTING_NAME);
77 wxRadioButton* radioButton3 = new wxRadioButton (this, ID_PRESET_DABRAIN, _T("DaBrain's"));
78 wxRadioButton* radioButton4 = new wxRadioButton (this, ID_PRESET_HERRA_TOHTORI, _T("Herra Tohtori's"));
79 wxRadioButton* radioButton5 = new wxRadioButton (this, ID_PRESET_CKID, _T("CKid's"));
80 wxRadioButton* radioButton6 = new wxRadioButton (this, ID_PRESET_COLECAMPBELL666, _T("ColeCampbell666's"));
81 wxRadioButton* radioButton7 = new wxRadioButton (this, ID_PRESET_CASTOR, _T("Castor's"));
82 wxRadioButton* radioButton8 = new wxRadioButton (this, ID_PRESET_SPIDEY, _T("Spidey's"));
83 wxRadioButton* radioButton9 = new wxRadioButton (this, ID_PRESET_WOOLIE_WOOL, _T("Woolie Wool's"));
84
85 this->Initialize();
86
87 wxGridBagSizer* lightingInsideSizer = new wxGridBagSizer();
88 lightingInsideSizer->Add(presetDescsUrl, wxGBPosition(0,0), wxGBSpan(1,1),
89 wxALIGN_CENTER_HORIZONTAL|wxBOTTOM, RADIOBUTTON_SPACING);
90 lightingInsideSizer->Add(customFlagsCopyButton, wxGBPosition(1,0), wxGBSpan(1,1),
91 wxALIGN_CENTER_HORIZONTAL|wxBOTTOM, RADIOBUTTON_SPACING + 5);
92 lightingInsideSizer->Add(radioButton1, wxGBPosition(2,0), wxGBSpan(1,1), wxBOTTOM, RADIOBUTTON_SPACING);
93 lightingInsideSizer->Add(radioButton2, wxGBPosition(3,0), wxGBSpan(1,1), wxEXPAND|wxBOTTOM, RADIOBUTTON_SPACING);
94 lightingInsideSizer->Add(radioButton3, wxGBPosition(4,0), wxGBSpan(1,1), wxBOTTOM, RADIOBUTTON_SPACING);
95 lightingInsideSizer->Add(radioButton4, wxGBPosition(5,0), wxGBSpan(1,1), wxBOTTOM, RADIOBUTTON_SPACING);
96 lightingInsideSizer->Add(radioButton5, wxGBPosition(6,0), wxGBSpan(1,1), wxBOTTOM, RADIOBUTTON_SPACING);
97 lightingInsideSizer->Add(radioButton6, wxGBPosition(7,0), wxGBSpan(1,1), wxBOTTOM, RADIOBUTTON_SPACING);
98 lightingInsideSizer->Add(radioButton7, wxGBPosition(8,0), wxGBSpan(1,1), wxBOTTOM, RADIOBUTTON_SPACING);
99 lightingInsideSizer->Add(radioButton8, wxGBPosition(9,0), wxGBSpan(1,1), wxBOTTOM, RADIOBUTTON_SPACING);
100 lightingInsideSizer->Add(radioButton9, wxGBPosition(10,0), wxGBSpan(1,1));
101
102 wxStaticBoxSizer* lightingPresetsSizer = new wxStaticBoxSizer(lightingPresetsBox, wxHORIZONTAL);
103 lightingPresetsSizer->Add(lightingInsideSizer, wxSizerFlags().Expand().Border(wxALL, 5));
104
105 this->SetSizer(lightingPresetsSizer);
106 }
107
108 void LightingPresets::InitializePresets() {
109 wxASSERT_MSG(presets.size() == 0, _T("presets have already been initialized"));
110
111 presets[ID_PRESETS_OFF] = Preset(_T("Off"), ID_PRESETS_OFF, wxEmptyString);
112 presets[ID_PRESET_BASELINE] =
113 Preset(_T("BaselineRecommended"), ID_PRESET_BASELINE,
114 DEFAULT_MOD_RECOMMENDED_LIGHTING_FLAGSET);
115 presets[ID_PRESET_DABRAIN] =
116 Preset(_T("DaBrain"), ID_PRESET_DABRAIN,
117 _T("-ambient_factor 10 -no_emissive_light -spec_exp 7.0 -spec_point 8.6 -spec_static 12.8 -spec_tube 5.0"));
118 presets[ID_PRESET_HERRA_TOHTORI] =
119 Preset(_T("HerraTohtori"), ID_PRESET_HERRA_TOHTORI,
120 _T("-ambient_factor 35 -no_emissive_light -spec_exp 15 -spec_point 1.2 -spec_static 1.5 -spec_tube 1.5 -ogl_spec 20"));
121 presets[ID_PRESET_CKID] =
122 Preset(_T("CKid"), ID_PRESET_CKID,
123 _T("-ambient_factor 35 -no_emissive_light -spec_exp 16.7 -spec_point 0.6 -spec_static 0.9 -spec_tube 1"));
124 presets[ID_PRESET_COLECAMPBELL666] =
125 Preset(_T("ColeCampbell666"), ID_PRESET_COLECAMPBELL666,
126 _T("-ambient_factor 0 -no_emissive_light -spec_exp 11 -spec_point .6 -spec_static .8 -spec_tube .4 -ogl_spec 80"));
127 presets[ID_PRESET_CASTOR] =
128 Preset(_T("Castor"), ID_PRESET_CASTOR,
129 _T("-ambient_factor 75 -spec_exp 7.0 -spec_point 8.6 -spec_static 3.0 -spec_tube 5.0"));
130 presets[ID_PRESET_SPIDEY] =
131 Preset(_T("Spidey"), ID_PRESET_SPIDEY,
132 _T("-ambient_factor 5 -spec_exp 15 -spec_point 1.2 -spec_static 1.7 -spec_tube 1.5 -ogl_spec 50"));
133 presets[ID_PRESET_WOOLIE_WOOL] =
134 Preset(_T("WoolieWool"), ID_PRESET_WOOLIE_WOOL,
135 _T("-ambient_factor 105 -no_emissive_light -spec_exp 9 -spec_point 0.3 -spec_static 0.8 -spec_tube 0.7 -ogl_spec 120"));
136 }
137
138 BEGIN_EVENT_TABLE(LightingPresets, wxPanel)
139 EVT_BUTTON(ID_COPY_PRESET_BUTTON, LightingPresets::OnCopyLightingPreset)
140 EVT_RADIOBUTTON(ID_PRESETS_OFF, LightingPresets::OnSelectLightingPreset)
141 EVT_RADIOBUTTON(ID_PRESET_BASELINE, LightingPresets::OnSelectLightingPreset)
142 EVT_RADIOBUTTON(ID_PRESET_DABRAIN, LightingPresets::OnSelectLightingPreset)
143 EVT_RADIOBUTTON(ID_PRESET_HERRA_TOHTORI, LightingPresets::OnSelectLightingPreset)
144 EVT_RADIOBUTTON(ID_PRESET_CKID, LightingPresets::OnSelectLightingPreset)
145 EVT_RADIOBUTTON(ID_PRESET_COLECAMPBELL666, LightingPresets::OnSelectLightingPreset)
146 EVT_RADIOBUTTON(ID_PRESET_CASTOR, LightingPresets::OnSelectLightingPreset)
147 EVT_RADIOBUTTON(ID_PRESET_SPIDEY, LightingPresets::OnSelectLightingPreset)
148 EVT_RADIOBUTTON(ID_PRESET_WOOLIE_WOOL, LightingPresets::OnSelectLightingPreset)
149 EVT_COMMAND(wxID_NONE, EVT_TC_ACTIVE_MOD_CHANGED, LightingPresets::OnActiveModChanged)
150 EVT_COMMAND(wxID_NONE, EVT_PROXY_FLAG_DATA_READY, LightingPresets::OnProxyFlagDataReady)
151 END_EVENT_TABLE()
152
153 void LightingPresets::OnSelectLightingPreset(wxCommandEvent &event) {
154 int id = event.GetId();
155 const wxString& presetName = PresetButtonIdToPresetName(id);
156
157 wxLogDebug(_T("lighting preset %s selected"), presetName.c_str());
158
159 wxButton* copyPresetButton =
160 dynamic_cast<wxButton*>(wxWindow::FindWindowById(ID_COPY_PRESET_BUTTON, this));
161 wxCHECK_RET( copyPresetButton != NULL, _T("Unable to find copy lighting preset button"));
162
163 if (id == ID_PRESETS_OFF) {
164 copyPresetButton->Disable();
165 } else {
166 copyPresetButton->Enable();
167 }
168
169 ProfileProxy::GetProxy()->SetLightingPreset(presetName);
170 }
171
172 void LightingPresets::OnCopyLightingPreset(wxCommandEvent &WXUNUSED(event)) {
173 wxASSERT_MSG(presets.size() != 0, _T("presets have not been initialized"));
174
175 wxCHECK_RET(ProfileProxy::GetProxy()->HasLightingPreset(),
176 _T("copy lighting preset button pressed with no preset stored in profile"));
177
178 wxString presetName(ProfileProxy::GetProxy()->GetLightingPresetName());
179
180 wxCHECK_RET(presetName != presets[ID_PRESETS_OFF].GetName(),
181 _T("copy lighting preset button pressed when 'presets off' is selected"));
182
183 wxLogDebug(_T("attempting to copy preset named %s to custom flags"), presetName.c_str());
184
185 ProfileProxy::GetProxy()->CopyPresetToCustomFlags();
186
187 this->Reset();
188 }
189
190 void LightingPresets::OnActiveModChanged(wxCommandEvent &WXUNUSED(event)) {
191 const ModItem* activeMod = ModList::GetActiveMod();
192 wxCHECK_RET(activeMod != NULL,
193 _T("LightingPresets::OnActiveModChanged(): activeMod is NULL!"));
194
195 presets[ID_PRESET_BASELINE].SetFlagSet(activeMod->recommendedlightingflagset);
196
197 wxRadioButton* radioButtonRecommended = dynamic_cast<wxRadioButton*>(
198 wxWindow::FindWindowById(ID_PRESET_BASELINE, this));
199 wxCHECK_RET(radioButtonRecommended != NULL,
200 _T("Could not find recommended preset radio button"));
201
202 radioButtonRecommended->SetLabel(activeMod->recommendedlightingname);
203 }
204
205 void LightingPresets::OnProxyFlagDataReady(wxCommandEvent &WXUNUSED(event)) {
206 this->Initialize();
207 }
208
209 const wxString& LightingPresets::PresetNameToPresetFlagSet(const wxString& presetName) {
210 if (presets.size() == 0) { // for registry_helper, so that it can write cmdline_fso.cfg
211 InitializePresets();
212 }
213
214 for (PresetHashMap::iterator it = presets.begin(), end = presets.end(); it != end; ++it) {
215 if (it->second.GetName() == presetName) {
216 return it->second.GetFlagSet();
217 }
218 }
219
220 wxLogWarning(_T("PresetNameToPresetFlagSet: unknown preset name %s, returning default (%s)"),
221 presetName.c_str(), presets[DEFAULT_PRESET_ID].GetName().c_str());
222 return presets[DEFAULT_PRESET_ID].GetFlagSet();
223 }
224
225 void LightingPresets::Initialize() {
226 wxString presetName;
227
228 if (ProfileProxy::GetProxy()->HasLightingPreset()) {
229 presetName = ProfileProxy::GetProxy()->GetLightingPresetName();
230 } else {
231 presetName = PresetButtonIdToPresetName(DEFAULT_PRESET_ID);
232 }
233
234 int presetButtonId = PresetNameToPresetButtonId(presetName);
235
236 wxRadioButton *selectedPresetButton =
237 dynamic_cast<wxRadioButton*>(wxWindow::FindWindowById(presetButtonId, this));
238 wxCHECK_RET( selectedPresetButton != NULL, _T("Cannot find selected preset button"));
239 selectedPresetButton->SetValue(true);
240
241 wxCommandEvent presetSelectionEvent(wxEVT_COMMAND_RADIOBUTTON_SELECTED, presetButtonId);
242 this->OnSelectLightingPreset(presetSelectionEvent);
243 }
244
245 void LightingPresets::Reset() {
246 wxRadioButton* presetsOffButton =
247 dynamic_cast<wxRadioButton*>(wxWindow::FindWindowById(ID_PRESETS_OFF, this));
248 wxCHECK_RET( presetsOffButton != NULL, _T("Unable to find lighting presets off radio button"));
249 presetsOffButton->SetValue(true);
250
251 wxCommandEvent resetEvent(wxEVT_COMMAND_RADIOBUTTON_SELECTED, ID_PRESETS_OFF);
252 this->OnSelectLightingPreset(resetEvent);
253 }
254
255 int LightingPresets::PresetNameToPresetButtonId(const wxString& presetName) {
256 wxASSERT_MSG(presets.size() != 0, _T("presets have not been initialized"));
257
258 for (PresetHashMap::iterator it = presets.begin(), end = presets.end(); it != end; ++it) {
259 if (it->second.GetName() == presetName) {
260 return it->second.GetButtonId();
261 }
262 }
263
264 wxLogWarning(_T("PresetNameToPresetButtonId: unknown preset name %s, returning default (%s)"),
265 presetName.c_str(), presets[DEFAULT_PRESET_ID].GetName().c_str());
266 return DEFAULT_PRESET_ID;
267 }
268
269 const wxString& LightingPresets::PresetButtonIdToPresetName(int buttonId) {
270 wxASSERT_MSG(presets.size() != 0, _T("presets have not been initialized"));
271
272 PresetHashMap::iterator it = presets.find(buttonId);
273
274 wxCHECK_MSG(it != presets.end(), presets[DEFAULT_PRESET_ID].GetName(),
275 wxString::Format(_T("PresetButtonIdToPresetName given invalid button ID %d"), buttonId));
276
277 return it->second.GetName();
278 }