1 
2 #include <wx/sizer.h>
3 #include <wx/statbox.h>
4 #include <wx/checkbox.h>
5 #include <wx/slider.h>
6 #include <wx/combobox.h>
7 #include <wx/stattext.h>
8 #include <wx/choicdlg.h>
9 #include <wx/defs.h>
10 #include <wx/intl.h>
11 #include <wx/button.h>
12 #include <wx/tipwin.h>
13 #include <wx/tooltip.h>
14 #include <wx/textctrl.h>
15 #include <map>
16 
17 #include "utils/controls.h"
18 #include "utils/conversion.h"
19 #include <lslutils/misc.h>
20 #include <lslutils/conversion.h>
21 #include <lslunitsync/optionswrapper.h>
22 #include <lslunitsync/unitsync.h>
23 #include "battle.h"
24 #include "gui/spinctl/spinctrl.h"
25 #include "utils/customdialogs.h"
26 #include "server.h"
27 #include "settings.h"
28 #include "ui.h"
29 #include "aui/auimanager.h"
30 
31 
32 const char sep = *("_");
33 const wxString wxsep = _T("_");
34 
35 
36 // note that the SpinCtrlDouble change is hadnled explicitly via the control calling the right handler
BEGIN_EVENT_TABLE_TEMPLATE1(BattleroomMMOptionsTab,wxPanel,BattleType)37 BEGIN_EVENT_TABLE_TEMPLATE1( BattleroomMMOptionsTab, wxPanel, BattleType)
38 	EVT_COMBOBOX					(wxID_ANY, BattleroomMMOptionsTab::OnComBoxChange)
39 	EVT_CHECKBOX					(wxID_ANY, BattleroomMMOptionsTab::OnChkBoxChange)
40 	EVT_TEXT_ENTER					(wxID_ANY,  BattleroomMMOptionsTab::OnTextCtrlChange)
41 
42 //  EVT_BUTTON( BOPTS_LOADPRES, BattleroomMMOptionsTab::OnLoadPreset )
43 //  EVT_BUTTON( BOPTS_SAVEPRES, BattleroomMMOptionsTab::OnSavePreset )
44 //  EVT_BUTTON( BOPTS_DELETEPRES, BattleroomMMOptionsTab::OnDeletePreset )
45 //  EVT_BUTTON( BOPTS_SETDEFAULTPRES, BattleroomMMOptionsTab::OnSetModDefaultPreset )
46 
47   EVT_BUTTON( wxID_ANY, BattleroomMMOptionsTab::OnButton )
48 END_EVENT_TABLE()
49 
50 template < class BattleType >
51 BattleroomMMOptionsTab<BattleType>::BattleroomMMOptionsTab(  BattleType* battle, wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style )
52 : wxScrolledWindow( parent, id, pos, size, style | wxHSCROLL ),m_battle(battle)
53 {
54   GetAui().manager->AddPane( this, wxLEFT, _T("battleroommmoptionstab") );
55 
56 	m_main_sizer = new wxBoxSizer( wxVERTICAL );
57 
58   wxStaticBoxSizer* m_preset_sizer;
59   m_preset_sizer = new wxStaticBoxSizer( new wxStaticBox( this, -1, _("Manage Presets") ), wxHORIZONTAL );
60 
61   m_options_preset_sel = new wxComboBox( this, BOPTS_CHOSEPRES, _T(""), wxDefaultPosition, wxDefaultSize,  sett().GetPresetList(), wxCB_READONLY );
62   m_options_preset_sel->SetToolTip(TE(_("Set name.")));
63 
64   m_preset_sizer->Add( m_options_preset_sel, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
65 
66   m_load_btn = new wxButton( this, BOPTS_LOADPRES, _("Load..."), wxDefaultPosition, wxDefaultSize, 0 );
67   m_load_btn->SetToolTip( TE(_("Load a saved set of options.")) );
68 
69   m_preset_sizer->Add( m_load_btn, 0, wxALL, 5 );
70 
71   m_save_btn = new wxButton( this, BOPTS_SAVEPRES, _("Save..."), wxDefaultPosition, wxDefaultSize, 0 );
72   m_save_btn->SetToolTip( TE(_("Save a set of options.")) );
73 
74   m_preset_sizer->Add( m_save_btn, 0, wxALL, 5 );
75 
76   m_delete_btn = new wxButton( this, BOPTS_DELETEPRES, _("Delete..."), wxDefaultPosition, wxDefaultSize, 0 );
77   m_delete_btn->SetToolTip( TE(_("Delete a set of options.")) );
78 
79   m_preset_sizer->Add( m_delete_btn, 0, wxALL, 5 );
80 
81   m_default_btn = new wxButton( this, BOPTS_SETDEFAULTPRES, _("Set default..."), wxDefaultPosition, wxDefaultSize, 0 );
82   m_default_btn->SetToolTip( TE(_("Use the current set of options as game's default.")) );
83 
84   m_preset_sizer->Add( m_default_btn, 0, wxALL, 5 );
85 
86   m_preset_sizer->Add( 0, 0, 1, wxEXPAND, 0 );
87 
88     m_map_mod_container = new wxBoxSizer( wxVERTICAL );
89 
90 	m_mod_options_sizer = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, _("Game Options") ), wxVERTICAL );
91 	m_mod_layout = new wxBoxSizer( wxVERTICAL);
92     setupOptionsSizer(m_mod_layout, LSL::OptionsWrapper::ModOption);
93 	m_mod_options_sizer->Add( m_mod_layout, 1, wxEXPAND, 5 );
94 
95 	m_map_options_sizer = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, _("Map Options") ), wxVERTICAL );
96 	m_map_layout = new wxBoxSizer( wxVERTICAL);
97     setupOptionsSizer(m_map_layout, LSL::OptionsWrapper::MapOption);
98 	m_map_options_sizer->Add( m_map_layout, 1, wxEXPAND, 5 );
99 
100 
101     m_main_sizer->Add( m_preset_sizer, 0, wxALL|wxEXPAND, 5 );
102 	m_map_mod_container->Add( m_mod_options_sizer, 0, wxALL|wxEXPAND, 5 );
103 	m_map_mod_container->Add( m_map_options_sizer, 0, wxALL|wxEXPAND, 5 );
104 	m_main_sizer->Add( m_map_mod_container, 1, wxALL|wxEXPAND, 5 );
105 
106 
107   //m_main_sizer->FitInside(this);
108 
109   SetBattle( battle );
110 
111     SetScrollRate( SCROLL_RATE, SCROLL_RATE );
112 	SetSizer( m_main_sizer );
113 	Layout();
114 
115 
116 }
117 
118 template < class BattleType >
~BattleroomMMOptionsTab()119 BattleroomMMOptionsTab<BattleType>::~BattleroomMMOptionsTab()
120 {
121     if(GetAui().manager)GetAui().manager->DetachPane( this );
122 }
123 
124 template < class BattleType >
setupOptionsSizer(wxBoxSizer * parent_sizer,LSL::OptionsWrapper::GameOption optFlag)125 void BattleroomMMOptionsTab<BattleType>::setupOptionsSizer( wxBoxSizer* parent_sizer,
126                                                             LSL::OptionsWrapper::GameOption optFlag )
127 {
128     if ( !m_battle )
129         return;
130     unsigned int num_options = 0;
131     for (const auto& it : m_battle->CustomBattleOptions().m_opts[optFlag].section_map)
132     {
133         wxStaticBoxSizer* section_sizer = new wxStaticBoxSizer(
134                     new wxStaticBox( this, wxID_ANY, TowxString(it.second.name)), wxVERTICAL );
135         //only add non-empty sizer
136         if ( setupOptionsSectionSizer( it.second, section_sizer, optFlag ) ) {
137             parent_sizer->Add( section_sizer, 0 , wxALL, section_sizer->GetChildren().size() > 0 ? 5 : 0 );
138             num_options++;
139         }
140         else
141             delete section_sizer;
142     }
143 
144     //adds options with no asociated section
145     LSL::mmOptionSection dummy;
146     wxBoxSizer* section_sizer = new wxBoxSizer( wxVERTICAL );
147     if ( setupOptionsSectionSizer( dummy, section_sizer, optFlag ) == 0 ) {
148         if ( num_options == 0 ) {
149 			wxString name = wxFormat( _T("%d%sno_opts") ) %optFlag % wxsep;
150             wxStaticText* none_found = new wxStaticText( this, wxID_ANY, _("no options available"),
151                                             wxDefaultPosition, wxDefaultSize, 0, name  );
152             m_statictext_map[name] = none_found;
153             parent_sizer->Add( none_found, 0, wxALL, 3 );
154         }
155     }
156     else {
157         wxStaticBoxSizer* other_section = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, _("other") ), wxVERTICAL );
158         other_section->Add( section_sizer, 0 , wxALL, section_sizer->GetChildren().size() > 0 ? 5 : 0 );
159         parent_sizer->Add( other_section, 0 , wxALL, 0 );
160     }
161 
162 }
163 
164 template < class BattleType >
getButton(const wxWindowID id,const wxString & name)165 wxButton* BattleroomMMOptionsTab<BattleType>::getButton( const wxWindowID id, const wxString& name )
166 {
167 		if ( !m_battle ) return 0;
168     m_button_map[name] = new wxButton(this, id + BUTTON_ID_OFFSET, _T("?"), wxDefaultPosition, wxDefaultSize, wxNO_BORDER|wxBU_EXACTFIT, wxDefaultValidator, name );
169     return m_button_map[name];
170 }
171 
172 template < class BattleType >
setupOptionsSectionSizer(const LSL::mmOptionSection & section,wxBoxSizer * parent_sizer,LSL::OptionsWrapper::GameOption optFlag)173 int BattleroomMMOptionsTab<BattleType>::setupOptionsSectionSizer(const LSL::mmOptionSection& section,
174     wxBoxSizer* parent_sizer, LSL::OptionsWrapper::GameOption optFlag)
175 {
176 	if ( !m_battle ) return -1;
177     const int col_gap = 35;
178 	wxString pref = wxFormat( _T("%d%s") ) % optFlag % wxsep;
179     LSL::OptionsWrapper optWrap = m_battle->CustomBattleOptions();
180 	bool enable = m_battle->IsFounderMe();
181 	wxFlexGridSizer* cbxSizer =  new wxFlexGridSizer( 4, 2, 10, 10 );
182 	wxFlexGridSizer* spinSizer =  new wxFlexGridSizer( 4, 10, 10 );
183 	wxFlexGridSizer* textSizer =  new wxFlexGridSizer( 4, 10, 10 );
184 	wxFlexGridSizer* chkSizer = new wxFlexGridSizer( 4, 10, 10 );
185 
186     const int b_gap = 1;
187 
188     int total_count = 0;
189 	int ctrl_count = 0;
190     for (const auto i : optWrap.m_opts[optFlag].bool_map)
191     {
192         if ( i.second.section == section.key )
193         {
194             LSL::mmOptionBool current = i.second;
195             wxCheckBox* temp = new wxCheckBox(this,BOOL_START_ID+ctrl_count, TowxString(current.name));
196             temp->SetToolTip(TES(current.description));
197             m_name_info_map[pref+TowxString(current.key)] = TowxString(current.description);
198             temp->SetName(pref+TowxString(current.key));
199             m_chkbox_map[pref+TowxString(current.key)] = temp;
200             temp->SetValue(current.value);
201 			temp->Enable(enable);
202 			wxBoxSizer* ct_sizer = new wxBoxSizer( wxHORIZONTAL );
203 			ct_sizer->Add(temp, 0, wxRIGHT| wxALIGN_CENTER_VERTICAL, b_gap);
204             ct_sizer->Add(getButton(BOOL_START_ID+ctrl_count,pref+TowxString(current.key)), 0, wxRIGHT| wxALIGN_CENTER_VERTICAL, col_gap);
205 			chkSizer->Add( ct_sizer );
206 			ctrl_count++;
207         }
208 	}
209 
210     total_count += ctrl_count;
211 	ctrl_count = 0;
212     for ( const auto it : optWrap.m_opts[optFlag].float_map)
213 	{
214 //	    wxString seckey = it.second.section;
215 //	    wxString kkey = section.key ;
216         if ( it.second.section == section.key )
217         {
218             const LSL::mmOptionFloat current = it.second;
219 			SlSpinCtrlDouble<ThisType>* tempspin = new SlSpinCtrlDouble<ThisType>();
220 			tempspin->Create(this, FLOAT_START_ID+ctrl_count, _T(""),
221 					wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, double(current.min), double(current.max),
222                     double(current.value),double(current.stepping), TowxString(current.key));
223             tempspin->SetSnapToTicks( true );
224             tempspin->SetToolTip(TES(current.description));
225             const wxString pref_key = pref + TowxString(current.key);
226             m_name_info_map[pref_key] = TowxString(current.description);
227 			tempspin->Enable(enable);
228             tempspin->SetName(pref_key);
229             m_spinctrl_map[pref_key] = tempspin;
230              wxStaticText* tempst = new wxStaticText(this,-1, TowxString(current.name));
231              m_statictext_map[pref_key] = tempst;
232 			spinSizer->Add(tempst,0, wxALIGN_CENTER_VERTICAL);
233 			wxBoxSizer* ct_sizer = new wxBoxSizer( wxHORIZONTAL );
234 			ct_sizer->Add(tempspin, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, b_gap);
235             ct_sizer->Add(getButton(FLOAT_START_ID+ctrl_count,pref_key), 0, wxRIGHT , col_gap);
236 			spinSizer->Add( ct_sizer );
237 			ctrl_count++;
238         }
239 	}
240 
241     total_count += ctrl_count;
242 	ctrl_count = 0;
243     for (const auto it : optWrap.m_opts[optFlag].list_map)
244 	{
245         if ( it.second.section == section.key )
246         {
247             LSL::mmOptionList current = it.second;
248 
249             const int temp = int(current.cbx_choices.size()-1);
250             const int index = LSL::Util::Clamp(current.cur_choice_index,0,temp);
251             wxComboBox* tempchoice = new wxComboBox(this, LIST_START_ID+ctrl_count,
252                                                     TowxString(current.cbx_choices[index]), wxDefaultPosition,
253                                                     wxDefaultSize,
254                                                     LSL::Util::vectorToArrayString(current.cbx_choices),
255                                                     wxCB_READONLY, wxDefaultValidator);
256             wxString tooltip = TowxString(current.description + std::string("\n"));
257             for ( const auto itor : current.listitems)
258             {
259                 tooltip+= TowxString(std::string("\n") + itor.name + std::string(": ") + itor.desc);
260             }
261             tempchoice->SetToolTip(TE(tooltip));
262             const wxString pref_key = pref + TowxString(current.key);
263             m_name_info_map[pref_key] = tooltip;
264             tempchoice->SetName(pref_key);
265             tempchoice->Enable(enable);
266             m_combox_map[pref_key] = tempchoice;
267             wxStaticText* tempst = new wxStaticText(this,-1, TowxString(current.name));
268             m_statictext_map[pref_key] = tempst;
269             cbxSizer->Add(tempst,0, wxALIGN_CENTER_VERTICAL);
270             wxBoxSizer* ct_sizer = new wxBoxSizer( wxHORIZONTAL );
271             ct_sizer->Add(tempchoice, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, b_gap);
272             ct_sizer->Add(getButton(LIST_START_ID+ctrl_count,pref_key), 0,  wxRIGHT, col_gap);
273             cbxSizer->Add( ct_sizer );
274 
275             ctrl_count++;
276         }
277 	}
278 
279     total_count += ctrl_count;
280 	ctrl_count = 0;
281     for (const auto it : optWrap.m_opts[optFlag].string_map)
282 	{
283         if ( it.second.section == section.key )
284         {
285             const LSL::mmOptionString current = it.second;
286             wxTextCtrl* temptext = new wxTextCtrl(this, STRING_START_ID+ctrl_count, TowxString(current.value), wxDefaultPosition,
287                     wxDefaultSize, 0, wxDefaultValidator, TowxString(current.key));
288             temptext->SetToolTip(TES(current.description));
289             const wxString pref_key = pref + TowxString(current.key);
290             m_name_info_map[pref_key] = TowxString(current.description);
291             temptext->SetName(pref_key);
292             temptext->Enable(enable);
293             m_textctrl_map[pref_key] = temptext;
294             wxStaticText* tempst = new wxStaticText(this,-1,TowxString(current.name));
295             m_statictext_map[pref_key] = tempst;
296             textSizer->Add(tempst,0, wxALIGN_CENTER_VERTICAL);
297             wxBoxSizer* ct_sizer = new wxBoxSizer( wxHORIZONTAL );
298             ct_sizer->Add(temptext,0, wxALIGN_CENTER_VERTICAL | wxRIGHT, b_gap);
299             ct_sizer->Add(getButton(STRING_START_ID+ctrl_count,pref_key),0, wxRIGHT, col_gap);
300             textSizer->Add( ct_sizer );
301 
302             ctrl_count++;
303         }
304 	}
305     total_count += ctrl_count;
306 
307 	parent_sizer->Add( chkSizer, 0, wxALL, chkSizer->GetChildren().size() > 0 ? 5 : 0 );
308 	parent_sizer->Add( spinSizer, 0, wxALL, spinSizer->GetChildren().size() > 0 ? 5 : 0 );
309 	parent_sizer->Add( cbxSizer, 0, wxALL, cbxSizer->GetChildren().size() > 0 ? 5 : 0 );
310 	parent_sizer->Add( textSizer, 0, wxALL, textSizer->GetChildren().size() > 0 ? 5 : 0 );
311 
312     return total_count;
313 
314 }
315 
316 template < class BattleType >
OnChkBoxChange(wxCommandEvent & event)317 void BattleroomMMOptionsTab<BattleType>::OnChkBoxChange(wxCommandEvent& event)
318 {
319 	if ( !m_battle ) return;
320 	wxCheckBox* box = (wxCheckBox*) event.GetEventObject();
321     const wxString key = (box->GetName()).AfterFirst(sep);
322 	long gameoption ;
323 	box->GetName().BeforeFirst(sep).ToLong(&gameoption);
324 
325     if( m_battle->CustomBattleOptions().setSingleOption( STD_STRING(key), (box->GetValue() ? "1" : "0"),
326                                                          (LSL::OptionsWrapper::GameOption)gameoption ) )
327 	{
328         if (m_battle->IsFounderMe())
329         {
330           m_battle->SendHostInfo( (wxString()<<gameoption) + wxsep + key );
331         }
332 	}
333 }
334 
335 template < class BattleType >
OnComBoxChange(wxCommandEvent & event)336 void BattleroomMMOptionsTab<BattleType>::OnComBoxChange(wxCommandEvent& event)
337 {
338 	if ( !m_battle ) return;
339 	wxComboBox* box = (wxComboBox*) event.GetEventObject();
340 
341     const auto key = STD_STRING((box->GetName()).AfterFirst(sep));
342 	long gameoption;
343 	box->GetName().BeforeFirst(sep).ToLong(&gameoption);
344     const auto itemKey = m_battle->CustomBattleOptions().GetNameListOptItemKey(key, STD_STRING(box->GetValue()),
345                                                                              (LSL::OptionsWrapper::GameOption)gameoption );
346 
347     if(m_battle->CustomBattleOptions().setSingleOption( key, itemKey,
348                                                         (LSL::OptionsWrapper::GameOption)gameoption ) )
349 	{
350         if (m_battle->IsFounderMe())
351         {
352           m_battle->SendHostInfo( (wxString()<< gameoption) + wxsep + TowxString(key) );
353         }
354 	}
355 }
356 
357 template < class BattleType >
OnTextCtrlChange(wxCommandEvent & event)358 void BattleroomMMOptionsTab<BattleType>::OnTextCtrlChange(wxCommandEvent& event)
359 {
360 	if ( !m_battle ) return;
361 	wxTextCtrl* box = (wxTextCtrl*) event.GetEventObject();
362     const auto key = STD_STRING((box->GetName()).AfterFirst(sep));
363 	long gameoption;
364 	box->GetName().BeforeFirst(sep).ToLong(&gameoption);
365     if(m_battle->CustomBattleOptions().setSingleOption( key, STD_STRING(box->GetValue()),
366                                                         (LSL::OptionsWrapper::GameOption)gameoption ) )
367 	{
368 		if (m_battle->IsFounderMe())
369 		{
370           m_battle->SendHostInfo(  (wxString()<< gameoption) + wxsep + TowxString(key));
371 		}
372 
373 	}
374 }
375 
376 template < class BattleType >
OnSpinCtrlDoubleChange(SlSpinDoubleEvent & event)377 void BattleroomMMOptionsTab<BattleType>::OnSpinCtrlDoubleChange(SlSpinDoubleEvent& event)
378 {
379 	if ( !m_battle ) return;
380 	SlSpinCtrlDouble<ThisType>* box = (SlSpinCtrlDouble<ThisType>*) event.GetEventObject();
381     const auto key = STD_STRING((box->GetName()).AfterFirst(sep));
382 	long gameoption;
383 	box->GetName().BeforeFirst(sep).ToLong(&gameoption);
384     if(m_battle->CustomBattleOptions().setSingleOption( key,wxFormat( _T("%f") ) % box->GetValue(),
385                                                         (LSL::OptionsWrapper::GameOption)gameoption ) )
386 	{
387 		if (m_battle->IsFounderMe())
388 		{
389           m_battle->SendHostInfo(  (wxString()<< gameoption) + wxsep + TowxString(key) );
390 		}
391 	}
392 }
393 
394 template < class BattleType >
UpdateOptControls(wxString controlName)395 void BattleroomMMOptionsTab<BattleType>::UpdateOptControls(wxString controlName)
396 {
397 	if ( !m_battle ) return;
398 	long gameoption;
399 	controlName.BeforeFirst(sep).ToLong(&gameoption);
400     const auto optKey = STD_STRING(controlName.AfterFirst(sep));
401 
402     if ( gameoption == LSL::OptionsWrapper::PrivateOptions )
403 	{
404     if ( optKey == "mapname" ) OnReloadControls( LSL::OptionsWrapper::MapOption );
405     if ( optKey == "modname" ) OnReloadControls( LSL::OptionsWrapper::ModOption );
406     return;
407 	}
408 
409 	if ( m_chkbox_map.find(controlName) != m_chkbox_map.end() )
410 	{
411         const long value = LSL::Util::FromString<long>(
412                     m_battle->CustomBattleOptions().getSingleValue( optKey, (LSL::OptionsWrapper::GameOption)gameoption ));
413         wxCheckBox* cur = m_chkbox_map[controlName] ;
414         cur->SetValue(value);
415 	}
416 
417 	 if ( m_combox_map.find(controlName) != m_combox_map.end() )
418     {
419 		wxComboBox* cur = m_combox_map[controlName];
420         cur->SetValue(TowxString((m_battle->CustomBattleOptions()
421                                  .GetNameListOptValue( optKey, (LSL::OptionsWrapper::GameOption)gameoption))));
422 	}
423 
424 	 if ( m_textctrl_map.find(controlName) != m_textctrl_map.end() )
425 	{
426         const wxString value = TowxString(m_battle->CustomBattleOptions()
427                                     .getSingleValue( optKey, (LSL::OptionsWrapper::GameOption)gameoption));
428 		wxTextCtrl* cur = m_textctrl_map[controlName];
429 		cur->SetValue(value);
430 	}
431 
432 	 if ( m_spinctrl_map.find(controlName) != m_spinctrl_map.end() )
433 	{
434          const long value = LSL::Util::FromString<long>(
435                      m_battle->CustomBattleOptions().getSingleValue( optKey, (LSL::OptionsWrapper::GameOption)gameoption ));
436 		SlSpinCtrlDouble<ThisType>* cur = m_spinctrl_map[controlName] ;
437         cur->SetValue(value);
438 	}
439 
440 }
441 
442 template<class T>
RemovePrefixed(T & v,wxString pref)443 void RemovePrefixed(T &v, wxString pref){
444   typename T::iterator it = v.begin();
445   while(it != v.end())
446   {
447     typename T::iterator next = it;
448     ++next;
449     wxString key = it->first;
450     if (key.StartsWith(pref))
451     {
452       delete it->second;
453       v.erase(it);
454     }
455     it=next;
456   }
457 }
458 
459 template < class BattleType >
OnReloadControls(LSL::OptionsWrapper::GameOption flag)460 void BattleroomMMOptionsTab<BattleType>::OnReloadControls(LSL::OptionsWrapper::GameOption flag)
461 {
462 	if ( !m_battle ) return;
463 	wxString pref = wxFormat( _T("%d%s") ) % flag % wxsep;
464 
465 	//purgin existing keys from map
466 	RemovePrefixed(m_chkbox_map,pref);
467 	RemovePrefixed(m_spinctrl_map,pref);
468 	RemovePrefixed(m_textctrl_map,pref);
469 	RemovePrefixed(m_combox_map,pref);
470 	RemovePrefixed(m_statictext_map,pref);
471 	RemovePrefixed(m_button_map,pref);
472 
473 	//reloading the controls
474 	switch (flag)
475 	{
476         case LSL::OptionsWrapper::ModOption:
477 			m_mod_options_sizer->Remove(m_mod_layout);
478 			m_mod_layout = new wxBoxSizer( wxVERTICAL);
479             setupOptionsSizer(m_mod_layout, LSL::OptionsWrapper::ModOption);
480 			//m_mod_options_sizer->Add( m_mod_options_sizer, 1, wxEXPAND, 5 );
481 			m_mod_options_sizer->Add( m_mod_layout, 1, wxALL|wxEXPAND, 5 );
482 			break;
483         case LSL::OptionsWrapper::MapOption:
484 			m_map_options_sizer->Remove(m_map_layout);
485 			m_map_layout = new wxBoxSizer( wxVERTICAL);
486             setupOptionsSizer(m_map_layout, LSL::OptionsWrapper::MapOption);
487 			m_map_options_sizer->Add( m_map_layout, 1, wxALL|wxEXPAND, 5 );
488 			break;
489 		default:
490 			break;
491 	}
492 
493 
494 	//this->SetSizer( m_main_sizer, true );
495 	m_main_sizer->FitInside(this);
496 
497 	this->Layout();
498     SetScrollbars( 10, 10, 62, 62 );
499 }
500 
501 template < class BattleType >
OnReloadControls()502 void BattleroomMMOptionsTab<BattleType>::OnReloadControls()
503 {
504 		if ( !m_battle ) return;
505     for ( unsigned int i = 0; i < LSL::OptionsWrapper::LastOption; i++)
506         OnReloadControls( (LSL::OptionsWrapper::GameOption) i );
507 }
508 
509 template < class BattleType >
OnLoadPreset(wxCommandEvent &)510 void BattleroomMMOptionsTab<BattleType>::OnLoadPreset( wxCommandEvent& /*unused*/ )
511 {
512 	if ( !m_battle ) return;
513   wxString presetname = m_options_preset_sel->GetValue();
514   if ( presetname.IsEmpty() )
515   {
516      customMessageBoxNoModal( SL_MAIN_ICON , _("Cannot load an options set without a name\nPlease select one from the list and try again."), _("error"), wxICON_EXCLAMATION|wxOK );
517      return;
518   }
519   m_battle->LoadOptionsPreset( presetname );
520   m_battle->SendHostInfo( IBattle::HI_Send_All_opts );
521   OnReloadControls( );
522 }
523 
524 
525 template < class BattleType >
OnSavePreset(wxCommandEvent &)526 void BattleroomMMOptionsTab<BattleType>::OnSavePreset( wxCommandEvent& /*unused*/ )
527 {
528 	if ( !m_battle ) return;
529     wxString presetname;
530 	if ( !ui().AskText( _("Enter preset name"), _("Enter a name to save the current set of options\nIf a preset with the same name already exist, it will be overwritten"), presetname ) )
531         return;
532     if ( presetname.IsEmpty() )
533     {
534         customMessageBoxNoModal( SL_MAIN_ICON , _("Cannot save an options set without a name."), _("error"), wxICON_EXCLAMATION|wxOK );
535         return;
536     }
537     m_battle->SaveOptionsPreset( presetname );
538 }
539 
540 
541 template < class BattleType >
OnDeletePreset(wxCommandEvent &)542 void BattleroomMMOptionsTab<BattleType>::OnDeletePreset( wxCommandEvent& /*unused*/ )
543 {
544 	if ( !m_battle ) return;
545   wxArrayString choices = m_battle->GetPresetList();
546 	int result = wxGetSingleChoiceIndex(_("Pick an existing option set from the list"),_("Delete preset"), choices );
547 	if ( result < 0 ) return;
548   m_battle->DeletePreset( choices[result] );
549 }
550 
551 template < class BattleType >
OnSetModDefaultPreset(wxCommandEvent &)552 void BattleroomMMOptionsTab<BattleType>::OnSetModDefaultPreset( wxCommandEvent& /*unused*/ )
553 {
554 	if ( !m_battle ) return;
555   wxArrayString choices = m_battle->GetPresetList();
556 	int result = wxGetSingleChoiceIndex(_("Pick an existing option set from the list"),_("Set game default preset"), choices );
557 	if ( result < 0 ) return;
558   sett().SetModDefaultPresetName( m_battle->GetHostModName(), choices[result] );
559 }
560 
561 
562 template < class BattleType >
UpdatePresetList()563 void BattleroomMMOptionsTab<BattleType>::UpdatePresetList()
564 {
565 		if ( !m_battle ) return;
566     m_options_preset_sel->Clear();
567     m_options_preset_sel->Append(sett().GetPresetList());
568     m_options_preset_sel->SetStringSelection(  m_battle->GetCurrentPreset() );
569 }
570 
571 template < class BattleType >
OnButton(wxCommandEvent & event)572 void BattleroomMMOptionsTab<BattleType>::OnButton( wxCommandEvent& event )
573 {
574     switch ( event.GetId() ) {
575         case BOPTS_LOADPRES: OnLoadPreset ( event ); break;
576         case BOPTS_SAVEPRES: OnSavePreset ( event ); break;
577         case BOPTS_DELETEPRES: OnDeletePreset ( event ); break;
578         case BOPTS_SETDEFAULTPRES: OnSetModDefaultPreset ( event ); break;
579         default: OnInfoButton( event ); break;
580 
581     }
582 
583 }
584 
585 template < class BattleType >
OnInfoButton(wxCommandEvent & event)586 void BattleroomMMOptionsTab<BattleType>::OnInfoButton( wxCommandEvent& event )
587 {
588 		if ( !m_battle ) return;
589     #ifdef wxUSE_TIPWINDOW
590     wxWindow* button = (wxWindow*) event.GetEventObject();
591     if ( button ) {
592         nameInfoMap::const_iterator iter = m_name_info_map.find( button->GetName() );
593         if ( iter != m_name_info_map.end() ) {
594             //needs to be moved a little away from cursor pos
595             wxPoint pos =  wxGetMousePosition();
596             wxTipWindow* tip = new wxTipWindow ( this, iter->second , 1000 );
597             tip->Move( pos.x, pos.y - tip->GetSize().GetHeight() );
598         }
599     }
600     #endif
601 }
602 
603 template < class BattleType >
SetBattle(BattleType * battle)604 void BattleroomMMOptionsTab<BattleType>::SetBattle( BattleType* battle )
605 {
606 	m_battle = battle;
607 
608 
609 	m_options_preset_sel->Enable(m_battle);
610 	m_load_btn->Enable(m_battle);
611 	m_save_btn->Enable(m_battle);
612 	m_delete_btn->Enable(m_battle);
613 	m_default_btn->Enable(m_battle);
614 	for ( chkBoxMap::iterator itor = m_chkbox_map.begin(); itor != m_chkbox_map.end(); ++itor ) itor->second->Enable(m_battle);
615 	for ( comboBoxMap::iterator itor = m_combox_map.begin(); itor != m_combox_map.end(); ++itor ) itor->second->Enable(m_battle);
616 	for ( typename spinCtrlMap::iterator itor = m_spinctrl_map.begin(); itor != m_spinctrl_map.end(); ++itor ) itor->second->Enable(m_battle);
617 	for ( textCtrlMap::iterator itor = m_textctrl_map.begin(); itor != m_textctrl_map.end(); ++itor ) itor->second->Enable(m_battle);
618 	for ( buttonMap::iterator itor = m_button_map.begin(); itor != m_button_map.end(); ++itor ) itor->second->Enable(m_battle);
619 
620 	if ( m_battle ) {
621 		m_options_preset_sel->SetStringSelection( sett().GetModDefaultPresetName( m_battle->GetHostModName() ) );
622 		if ( !m_battle->IsFounderMe() ) {
623 			m_options_preset_sel->Disable();
624 			m_load_btn->Disable();
625 			m_save_btn->Disable();
626 			m_delete_btn->Disable();
627 			m_default_btn->Disable();
628 		} else {
629 			OnReloadControls();
630 		}
631 	}
632 }
633 
634 template < class BattleType >
GetBattle()635 BattleType* BattleroomMMOptionsTab<BattleType>::GetBattle()
636 {
637 	return m_battle;
638 }
639 
640