forked from dolphin-emu/dolphin
/
InputConfigDiag.cpp
1089 lines (870 loc) · 32.6 KB
/
InputConfigDiag.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
#include <algorithm>
#include <cctype>
#include <cstddef>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
#include <wx/app.h>
#include <wx/arrstr.h>
#include <wx/bitmap.h>
#include <wx/button.h>
#include <wx/chartype.h>
#include <wx/checkbox.h>
#include <wx/choice.h>
#include <wx/combobox.h>
#include <wx/control.h>
#include <wx/dcmemory.h>
#include <wx/defs.h>
#include <wx/dialog.h>
#include <wx/event.h>
#include <wx/font.h>
#include <wx/gdicmn.h>
#include <wx/listbox.h>
#include <wx/msgdlg.h>
#include <wx/notebook.h>
#include <wx/panel.h>
#include <wx/sizer.h>
#include <wx/slider.h>
#include <wx/spinctrl.h>
#include <wx/statbmp.h>
#include <wx/stattext.h>
#include <wx/string.h>
#include <wx/textctrl.h>
#include <wx/timer.h>
#include <wx/toplevel.h>
#include <wx/translation.h>
#include <wx/unichar.h>
#include "Common/FileSearch.h"
#include "Common/FileUtil.h"
#include "Common/IniFile.h"
#include "Common/MsgHandler.h"
#include "Core/HW/Wiimote.h"
#include "DolphinWX/InputConfigDiag.h"
#include "DolphinWX/WxUtils.h"
#include "InputCommon/ControllerEmu.h"
#include "InputCommon/InputConfig.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"
#include "InputCommon/ControllerInterface/Device.h"
#include "InputCommon/ControllerInterface/ExpressionParser.h"
using namespace ciface::ExpressionParser;
void GamepadPage::ConfigExtension(wxCommandEvent& event)
{
ControllerEmu::Extension* const ex = ((ExtensionButton*)event.GetEventObject())->extension;
// show config diag, if "none" isn't selected
if (ex->switch_extension)
{
wxDialog dlg(this, -1,
wxGetTranslation(StrToWxStr(ex->attachments[ex->switch_extension]->GetName())));
wxBoxSizer* const main_szr = new wxBoxSizer(wxVERTICAL);
const std::size_t orig_size = control_groups.size();
ControlGroupsSizer* const szr = new ControlGroupsSizer(ex->attachments[ex->switch_extension].get(), &dlg, this, &control_groups);
main_szr->Add(szr, 0, wxLEFT, 5);
main_szr->Add(dlg.CreateButtonSizer(wxOK), 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 5);
dlg.SetSizerAndFit(main_szr);
dlg.Center();
dlg.ShowModal();
// remove the new groups that were just added, now that the window closed
control_groups.resize(orig_size);
}
}
PadSettingExtension::PadSettingExtension(wxWindow* const parent, ControllerEmu::Extension* const ext)
: PadSetting(new wxChoice(parent, -1))
, extension(ext)
{
for (auto& attachment : extension->attachments)
{
((wxChoice*)wxcontrol)->Append(wxGetTranslation(StrToWxStr(attachment->GetName())));
}
UpdateGUI();
}
void PadSettingExtension::UpdateGUI()
{
((wxChoice*)wxcontrol)->Select(extension->switch_extension);
}
void PadSettingExtension::UpdateValue()
{
extension->switch_extension = ((wxChoice*)wxcontrol)->GetSelection();
}
PadSettingCheckBox::PadSettingCheckBox(wxWindow* const parent, ControllerEmu::ControlGroup::Setting* const _setting)
: PadSetting(new wxCheckBox(parent, -1, wxGetTranslation(StrToWxStr(_setting->name))))
, setting(_setting)
{
UpdateGUI();
}
void PadSettingCheckBox::UpdateGUI()
{
((wxCheckBox*)wxcontrol)->SetValue(!!setting->GetValue());
}
void PadSettingCheckBox::UpdateValue()
{
// 0.01 so its saved to the ini file as just 1. :(
setting->SetValue(0.01 * ((wxCheckBox*)wxcontrol)->GetValue());
}
void PadSettingSpin::UpdateGUI()
{
((wxSpinCtrl*)wxcontrol)->SetValue((int)(setting->GetValue() * 100));
}
void PadSettingSpin::UpdateValue()
{
setting->SetValue(ControlState(((wxSpinCtrl*)wxcontrol)->GetValue()) / 100);
}
ControlDialog::ControlDialog(GamepadPage* const parent, InputConfig& config, ControllerInterface::ControlReference* const ref)
: wxDialog(parent, -1, _("Configure Control"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
, control_reference(ref)
, m_config(config)
, m_parent(parent)
{
m_devq = m_parent->controller->default_device;
// GetStrings() sounds slow :/
//device_cbox = new wxComboBox(this, -1, StrToWxStr(ref->device_qualifier.ToString()), wxDefaultPosition, wxSize(256,-1), parent->device_cbox->GetStrings(), wxTE_PROCESS_ENTER);
device_cbox = new wxComboBox(this, -1, StrToWxStr(m_devq.ToString()), wxDefaultPosition, wxSize(256,-1), parent->device_cbox->GetStrings(), wxTE_PROCESS_ENTER);
device_cbox->Bind(wxEVT_COMBOBOX, &ControlDialog::SetDevice, this);
device_cbox->Bind(wxEVT_TEXT_ENTER, &ControlDialog::SetDevice, this);
wxStaticBoxSizer* const control_chooser = CreateControlChooser(parent);
wxStaticBoxSizer* const d_szr = new wxStaticBoxSizer(wxVERTICAL, this, _("Device"));
d_szr->Add(device_cbox, 0, wxEXPAND|wxALL, 5);
wxBoxSizer* const szr = new wxBoxSizer(wxVERTICAL);
szr->Add(d_szr, 0, wxEXPAND|wxLEFT|wxRIGHT|wxTOP, 5);
szr->Add(control_chooser, 1, wxEXPAND|wxALL, 5);
SetSizerAndFit(szr); // needed
UpdateGUI();
SetFocus();
}
ControlButton::ControlButton(wxWindow* const parent, ControllerInterface::ControlReference* const _ref, const unsigned int width, const std::string& label)
: wxButton(parent, -1, "", wxDefaultPosition, wxSize(width,20))
, control_reference(_ref)
{
if (label.empty())
SetLabel(StrToWxStr(_ref->expression));
else
SetLabel(StrToWxStr(label));
}
void InputConfigDialog::UpdateProfileComboBox()
{
std::string pname(File::GetUserPath(D_CONFIG_IDX));
pname += PROFILES_PATH;
pname += m_config.profile_name;
CFileSearch::XStringVector exts;
exts.push_back("*.ini");
CFileSearch::XStringVector dirs;
dirs.push_back(pname);
CFileSearch cfs(exts, dirs);
const CFileSearch::XStringVector& sv = cfs.GetFileNames();
wxArrayString strs;
for (auto si = sv.cbegin(); si != sv.cend(); ++si)
{
std::string str(si->begin() + si->find_last_of('/') + 1 , si->end() - 4) ;
strs.push_back(StrToWxStr(str));
}
for (GamepadPage* page : m_padpages)
{
page->profile_cbox->Clear();
page->profile_cbox->Append(strs);
}
}
void InputConfigDialog::UpdateControlReferences()
{
for (GamepadPage* page : m_padpages)
{
page->controller->UpdateReferences(g_controller_interface);
}
}
void InputConfigDialog::ClickSave(wxCommandEvent& event)
{
m_config.SaveConfig();
event.Skip();
}
void ControlDialog::UpdateListContents()
{
control_lbox->Clear();
ciface::Core::Device* const dev = g_controller_interface.FindDevice(m_devq);
if (dev)
{
if (control_reference->is_input)
{
for (ciface::Core::Device::Input* input : dev->Inputs())
{
control_lbox->Append(StrToWxStr(input->GetName()));
}
}
else // It's an output
{
for (ciface::Core::Device::Output* output : dev->Outputs())
{
control_lbox->Append(StrToWxStr(output->GetName()));
}
}
}
}
void ControlDialog::SelectControl(const std::string& name)
{
//UpdateGUI();
const int f = control_lbox->FindString(StrToWxStr(name));
if (f >= 0)
control_lbox->Select(f);
}
void ControlDialog::UpdateGUI()
{
// update textbox
textctrl->SetValue(StrToWxStr(control_reference->expression));
// updates the "bound controls:" label
m_bound_label->SetLabel(wxString::Format(_("Bound Controls: %lu"),
(unsigned long)control_reference->BoundCount()));
switch (control_reference->parse_error)
{
case EXPRESSION_PARSE_SYNTAX_ERROR:
m_error_label->SetLabel(_("Syntax error"));
break;
case EXPRESSION_PARSE_NO_DEVICE:
m_error_label->SetLabel(_("Device not found"));
break;
default:
m_error_label->SetLabel("");
}
};
void GamepadPage::UpdateGUI()
{
device_cbox->SetValue(StrToWxStr(controller->default_device.ToString()));
for (ControlGroupBox* cgBox : control_groups)
{
for (ControlButton* button : cgBox->control_buttons)
{
wxString expr = StrToWxStr(button->control_reference->expression);
expr.Replace("&", "&&");
button->SetLabel(expr);
}
for (PadSetting* padSetting : cgBox->options)
{
padSetting->UpdateGUI();
}
}
}
void GamepadPage::ClearAll(wxCommandEvent&)
{
// just load an empty ini section to clear everything :P
IniFile::Section section;
controller->LoadConfig(§ion);
// no point in using the real ControllerInterface i guess
ControllerInterface face;
std::lock_guard<std::recursive_mutex> lk(m_config.controls_lock);
controller->UpdateReferences(face);
UpdateGUI();
}
void GamepadPage::LoadDefaults(wxCommandEvent&)
{
controller->LoadDefaults(g_controller_interface);
std::lock_guard<std::recursive_mutex> lk(m_config.controls_lock);
controller->UpdateReferences(g_controller_interface);
UpdateGUI();
}
bool ControlDialog::Validate()
{
control_reference->expression = WxStrToStr(textctrl->GetValue());
std::lock_guard<std::recursive_mutex> lk(m_config.controls_lock);
g_controller_interface.UpdateReference(control_reference, m_parent->controller->default_device);
UpdateGUI();
return (control_reference->parse_error == EXPRESSION_PARSE_SUCCESS);
}
void GamepadPage::SetDevice(wxCommandEvent&)
{
controller->default_device.FromString(WxStrToStr(device_cbox->GetValue()));
// show user what it was validated as
device_cbox->SetValue(StrToWxStr(controller->default_device.ToString()));
// this will set all the controls to this default device
controller->UpdateDefaultDevice();
// update references
std::lock_guard<std::recursive_mutex> lk(m_config.controls_lock);
controller->UpdateReferences(g_controller_interface);
}
void ControlDialog::SetDevice(wxCommandEvent&)
{
m_devq.FromString(WxStrToStr(device_cbox->GetValue()));
// show user what it was validated as
device_cbox->SetValue(StrToWxStr(m_devq.ToString()));
// update gui
UpdateListContents();
}
void ControlDialog::ClearControl(wxCommandEvent&)
{
control_reference->expression.clear();
std::lock_guard<std::recursive_mutex> lk(m_config.controls_lock);
g_controller_interface.UpdateReference(control_reference, m_parent->controller->default_device);
UpdateGUI();
}
inline bool IsAlphabetic(wxString &str)
{
for (wxUniChar c : str)
if (!isalpha(c))
return false;
return true;
}
inline void GetExpressionForControl(wxString &expr,
wxString &control_name,
ciface::Core::DeviceQualifier *control_device = nullptr,
ciface::Core::DeviceQualifier *default_device = nullptr)
{
expr = "";
// non-default device
if (control_device && default_device && !(*control_device == *default_device))
{
expr += control_device->ToString();
expr += ":";
}
// append the control name
expr += control_name;
if (!IsAlphabetic(expr))
expr = wxString::Format("`%s`", expr);
}
bool ControlDialog::GetExpressionForSelectedControl(wxString &expr)
{
const int num = control_lbox->GetSelection();
if (num < 0)
return false;
wxString control_name = control_lbox->GetString(num);
GetExpressionForControl(expr,
control_name,
&m_devq,
&m_parent->controller->default_device);
return true;
}
void ControlDialog::SetSelectedControl(wxCommandEvent&)
{
wxString expr;
if (!GetExpressionForSelectedControl(expr))
return;
textctrl->WriteText(expr);
control_reference->expression = textctrl->GetValue();
std::lock_guard<std::recursive_mutex> lk(m_config.controls_lock);
g_controller_interface.UpdateReference(control_reference, m_parent->controller->default_device);
UpdateGUI();
}
void ControlDialog::AppendControl(wxCommandEvent& event)
{
wxString device_expr, expr;
const wxString lbl = ((wxButton*)event.GetEventObject())->GetLabel();
char op = lbl[0];
if (!GetExpressionForSelectedControl(device_expr))
return;
// Unary ops (that is, '!') are a special case. When there's a selection,
// put parens around it and prepend it with a '!', but when there's nothing,
// just add a '!device'.
if (op == '!')
{
wxString selection = textctrl->GetStringSelection();
if (selection == "")
expr = wxString::Format("%c%s", op, device_expr);
else
expr = wxString::Format("%c(%s)", op, selection);
}
else
{
expr = wxString::Format(" %c %s", op, device_expr);
}
textctrl->WriteText(expr);
control_reference->expression = textctrl->GetValue();
std::lock_guard<std::recursive_mutex> lk(m_config.controls_lock);
g_controller_interface.UpdateReference(control_reference, m_parent->controller->default_device);
UpdateGUI();
}
void GamepadPage::AdjustSetting(wxCommandEvent& event)
{
std::lock_guard<std::recursive_mutex> lk(m_config.controls_lock);
((PadSetting*)((wxControl*)event.GetEventObject())->GetClientData())->UpdateValue();
}
void GamepadPage::AdjustSettingUI(wxCommandEvent& event)
{
m_iterate = !m_iterate;
std::lock_guard<std::recursive_mutex> lk(m_config.controls_lock);
((PadSetting*)((wxControl*)event.GetEventObject())->GetClientData())->UpdateValue();
}
void GamepadPage::AdjustControlOption(wxCommandEvent&)
{
std::lock_guard<std::recursive_mutex> lk(m_config.controls_lock);
m_control_dialog->control_reference->range = (ControlState)(m_control_dialog->range_slider->GetValue()) / SLIDER_TICK_COUNT;
}
void GamepadPage::ConfigControl(wxEvent& event)
{
m_control_dialog = new ControlDialog(this, m_config, ((ControlButton*)event.GetEventObject())->control_reference);
m_control_dialog->ShowModal();
m_control_dialog->Destroy();
// update changes that were made in the dialog
UpdateGUI();
}
void GamepadPage::ClearControl(wxEvent& event)
{
ControlButton* const btn = (ControlButton*)event.GetEventObject();
btn->control_reference->expression.clear();
btn->control_reference->range = 1.0;
std::lock_guard<std::recursive_mutex> lk(m_config.controls_lock);
controller->UpdateReferences(g_controller_interface);
// update changes
UpdateGUI();
}
void ControlDialog::DetectControl(wxCommandEvent& event)
{
wxButton* const btn = (wxButton*)event.GetEventObject();
const wxString lbl = btn->GetLabel();
ciface::Core::Device* const dev = g_controller_interface.FindDevice(m_devq);
if (dev)
{
btn->SetLabel(_("[ waiting ]"));
// This makes the "waiting" text work on Linux. true (only if needed) prevents crash on Windows
wxTheApp->Yield(true);
std::lock_guard<std::recursive_mutex> lk(m_config.controls_lock);
ciface::Core::Device::Control* const ctrl = control_reference->Detect(DETECT_WAIT_TIME, dev);
// if we got input, select it in the list
if (ctrl)
SelectControl(ctrl->GetName());
btn->SetLabel(lbl);
}
}
void GamepadPage::DetectControl(wxCommandEvent& event)
{
ControlButton* btn = (ControlButton*)event.GetEventObject();
if (DetectButton(btn) && m_iterate == true)
{
auto it = std::find(control_buttons.begin(), control_buttons.end(), btn);
// std find will never return end since btn will always be found in control_buttons
++it;
for (; it != control_buttons.end(); ++it)
{
if (!DetectButton(*it))
break;
}
}
}
bool GamepadPage::DetectButton(ControlButton* button)
{
bool success = false;
// find device :/
ciface::Core::Device* const dev = g_controller_interface.FindDevice(controller->default_device);
if (dev)
{
button->SetLabel(_("[ waiting ]"));
// This makes the "waiting" text work on Linux. true (only if needed) prevents crash on Windows
wxTheApp->Yield(true);
std::lock_guard<std::recursive_mutex> lk(m_config.controls_lock);
ciface::Core::Device::Control* const ctrl = button->control_reference->Detect(DETECT_WAIT_TIME, dev);
// if we got input, update expression and reference
if (ctrl)
{
wxString control_name = ctrl->GetName();
wxString expr;
GetExpressionForControl(expr, control_name);
button->control_reference->expression = expr;
g_controller_interface.UpdateReference(button->control_reference, controller->default_device);
success = true;
}
}
UpdateGUI();
return success;
}
wxStaticBoxSizer* ControlDialog::CreateControlChooser(GamepadPage* const parent)
{
wxStaticBoxSizer* const main_szr = new wxStaticBoxSizer(wxVERTICAL, this, control_reference->is_input ? _("Input") : _("Output"));
textctrl = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition, wxSize(-1, 48), wxTE_MULTILINE | wxTE_RICH2);
wxFont font = textctrl->GetFont();
font.SetFamily(wxFONTFAMILY_MODERN);
textctrl->SetFont(font);
wxButton* const detect_button = new wxButton(this, -1, control_reference->is_input ? _("Detect") : _("Test"));
wxButton* const clear_button = new wxButton(this, -1, _("Clear"));
wxButton* const select_button = new wxButton(this, -1, _("Select"));
select_button->Bind(wxEVT_BUTTON, &ControlDialog::SetSelectedControl, this);
wxButton* const or_button = new wxButton(this, -1, _("| OR"));
or_button->Bind(wxEVT_BUTTON, &ControlDialog::AppendControl, this);
control_lbox = new wxListBox(this, -1, wxDefaultPosition, wxSize(-1, 64));
wxBoxSizer* const button_sizer = new wxBoxSizer(wxVERTICAL);
button_sizer->Add(detect_button, 1, 0, 5);
button_sizer->Add(select_button, 1, 0, 5);
button_sizer->Add(or_button, 1, 0, 5);
if (control_reference->is_input)
{
// TODO: check if && is good on other OS
wxButton* const and_button = new wxButton(this, -1, _("&& AND"));
wxButton* const not_button = new wxButton(this, -1, _("! NOT"));
wxButton* const add_button = new wxButton(this, -1, _("+ ADD"));
and_button->Bind(wxEVT_BUTTON, &ControlDialog::AppendControl, this);
not_button->Bind(wxEVT_BUTTON, &ControlDialog::AppendControl, this);
add_button->Bind(wxEVT_BUTTON, &ControlDialog::AppendControl, this);
button_sizer->Add(and_button, 1, 0, 5);
button_sizer->Add(not_button, 1, 0, 5);
button_sizer->Add(add_button, 1, 0, 5);
}
range_slider = new wxSlider(this, -1, SLIDER_TICK_COUNT, -SLIDER_TICK_COUNT * 5, SLIDER_TICK_COUNT * 5, wxDefaultPosition, wxDefaultSize, wxSL_TOP | wxSL_LABELS /*| wxSL_AUTOTICKS*/);
range_slider->SetValue((int)(control_reference->range * SLIDER_TICK_COUNT));
detect_button->Bind(wxEVT_BUTTON, &ControlDialog::DetectControl, this);
clear_button->Bind(wxEVT_BUTTON, &ControlDialog::ClearControl, this);
range_slider->Bind(wxEVT_SCROLL_CHANGED, &GamepadPage::AdjustControlOption, parent);
wxStaticText* const range_label = new wxStaticText(this, -1, _("Range"));
m_bound_label = new wxStaticText(this, -1, "");
m_error_label = new wxStaticText(this, -1, "");
wxBoxSizer* const range_sizer = new wxBoxSizer(wxHORIZONTAL);
range_sizer->Add(range_label, 0, wxCENTER|wxLEFT, 5);
range_sizer->Add(range_slider, 1, wxEXPAND|wxLEFT, 5);
wxBoxSizer* const ctrls_sizer = new wxBoxSizer(wxHORIZONTAL);
ctrls_sizer->Add(control_lbox, 1, wxEXPAND, 0);
ctrls_sizer->Add(button_sizer, 0, wxEXPAND, 0);
wxSizer* const bottom_btns_sizer = CreateButtonSizer(wxOK|wxAPPLY);
bottom_btns_sizer->Prepend(clear_button, 0, wxLEFT, 5);
main_szr->Add(range_sizer, 0, wxEXPAND|wxLEFT|wxRIGHT, 5);
main_szr->Add(ctrls_sizer, 0, wxEXPAND|wxLEFT|wxRIGHT|wxBOTTOM, 5);
main_szr->Add(textctrl, 1, wxEXPAND|wxLEFT|wxRIGHT|wxBOTTOM, 5);
main_szr->Add(bottom_btns_sizer, 0, wxEXPAND|wxBOTTOM|wxRIGHT, 5);
main_szr->Add(m_bound_label, 0, wxCENTER, 0);
main_szr->Add(m_error_label, 0, wxCENTER, 0);
UpdateListContents();
return main_szr;
}
void GamepadPage::GetProfilePath(std::string& path)
{
const wxString& name = profile_cbox->GetValue();
if (!name.empty())
{
// TODO: check for dumb characters maybe
path = File::GetUserPath(D_CONFIG_IDX);
path += PROFILES_PATH;
path += m_config.profile_name;
path += '/';
path += WxStrToStr(profile_cbox->GetValue());
path += ".ini";
}
}
void GamepadPage::LoadProfile(wxCommandEvent&)
{
std::string fname;
GamepadPage::GetProfilePath(fname);
if (!File::Exists(fname))
return;
IniFile inifile;
inifile.Load(fname);
std::lock_guard<std::recursive_mutex> lk(m_config.controls_lock);
controller->LoadConfig(inifile.GetOrCreateSection("Profile"));
controller->UpdateReferences(g_controller_interface);
UpdateGUI();
}
void GamepadPage::SaveProfile(wxCommandEvent&)
{
std::string fname;
GamepadPage::GetProfilePath(fname);
File::CreateFullPath(fname);
if (!fname.empty())
{
IniFile inifile;
controller->SaveConfig(inifile.GetOrCreateSection("Profile"));
inifile.Save(fname);
m_config_dialog->UpdateProfileComboBox();
}
else
{
WxUtils::ShowErrorDialog(_("You must enter a valid profile name."));
}
}
void GamepadPage::DeleteProfile(wxCommandEvent&)
{
std::string fname;
GamepadPage::GetProfilePath(fname);
const char* const fnamecstr = fname.c_str();
if (File::Exists(fnamecstr) &&
AskYesNoT("Are you sure you want to delete \"%s\"?",
WxStrToStr(profile_cbox->GetValue()).c_str()))
{
File::Delete(fnamecstr);
m_config_dialog->UpdateProfileComboBox();
}
}
void InputConfigDialog::UpdateDeviceComboBox()
{
ciface::Core::DeviceQualifier dq;
for (GamepadPage* page : m_padpages)
{
page->device_cbox->Clear();
for (ciface::Core::Device* d : g_controller_interface.Devices())
{
dq.FromDevice(d);
page->device_cbox->Append(StrToWxStr(dq.ToString()));
}
page->device_cbox->SetValue(StrToWxStr(page->controller->default_device.ToString()));
}
}
void GamepadPage::RefreshDevices(wxCommandEvent&)
{
std::lock_guard<std::recursive_mutex> lk(m_config.controls_lock);
// refresh devices
g_controller_interface.Reinitialize();
// update all control references
m_config_dialog->UpdateControlReferences();
// update device cbox
m_config_dialog->UpdateDeviceComboBox();
}
ControlGroupBox::~ControlGroupBox()
{
for (PadSetting* padSetting : options)
delete padSetting;
}
ControlGroupBox::ControlGroupBox(ControllerEmu::ControlGroup* const group, wxWindow* const parent, GamepadPage* const eventsink)
: wxBoxSizer(wxVERTICAL)
, control_group(group)
{
static_bitmap = nullptr;
const std::vector<std::string> exclude_buttons = {"Mic", "Modifier"};
const std::vector<std::string> exclude_groups = {"IR", "Swing", "Tilt", "Shake", "UDP Wiimote", "Extension", "Rumble"};
wxFont m_SmallFont(7, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
for (auto& control : group->controls)
{
wxStaticText* const label = new wxStaticText(parent, -1, wxGetTranslation(StrToWxStr(control->name)));
ControlButton* const control_button = new ControlButton(parent, control->control_ref.get(), 80);
control_button->SetFont(m_SmallFont);
control_buttons.push_back(control_button);
if (std::find(exclude_groups.begin(), exclude_groups.end(), control_group->name) == exclude_groups.end() &&
std::find(exclude_buttons.begin(), exclude_buttons.end(), control->name) == exclude_buttons.end())
eventsink->control_buttons.push_back(control_button);
if (control->control_ref->is_input)
{
control_button->SetToolTip(_("Left-click to detect input.\nMiddle-click to clear.\nRight-click for more options."));
control_button->Bind(wxEVT_BUTTON, &GamepadPage::DetectControl, eventsink);
}
else
{
control_button->SetToolTip(_("Left/Right-click for more options.\nMiddle-click to clear."));
control_button->Bind(wxEVT_BUTTON, &GamepadPage::ConfigControl, eventsink);
}
control_button->Bind(wxEVT_MIDDLE_DOWN, &GamepadPage::ClearControl, eventsink);
control_button->Bind(wxEVT_RIGHT_UP, &GamepadPage::ConfigControl, eventsink);
wxBoxSizer* const control_sizer = new wxBoxSizer(wxHORIZONTAL);
control_sizer->AddStretchSpacer(1);
control_sizer->Add(label, 0, wxCENTER | wxRIGHT, 3);
control_sizer->Add(control_button, 0, 0, 0);
Add(control_sizer, 0, wxEXPAND|wxLEFT|wxRIGHT, 3);
}
wxMemoryDC dc;
switch (group->type)
{
case GROUP_TYPE_STICK:
case GROUP_TYPE_TILT:
case GROUP_TYPE_CURSOR:
case GROUP_TYPE_FORCE:
{
wxBitmap bitmap(64, 64);
dc.SelectObject(bitmap);
dc.Clear();
static_bitmap = new wxStaticBitmap(parent, -1, bitmap, wxDefaultPosition, wxDefaultSize, wxBITMAP_TYPE_BMP);
wxBoxSizer* const szr = new wxBoxSizer(wxVERTICAL);
for (auto& groupSetting : group->settings)
{
PadSettingSpin* setting = new PadSettingSpin(parent, groupSetting.get());
setting->wxcontrol->Bind(wxEVT_SPINCTRL, &GamepadPage::AdjustSetting, eventsink);
options.push_back(setting);
szr->Add(new wxStaticText(parent, -1, wxGetTranslation(StrToWxStr(groupSetting->name))));
szr->Add(setting->wxcontrol, 0, wxLEFT, 0);
}
wxBoxSizer* const h_szr = new wxBoxSizer(wxHORIZONTAL);
h_szr->Add(szr, 1, 0, 5);
h_szr->Add(static_bitmap, 0, wxALL|wxCENTER, 3);
Add(h_szr, 0, wxEXPAND|wxLEFT|wxCENTER|wxTOP, 3);
}
break;
case GROUP_TYPE_BUTTONS:
{
wxBitmap bitmap(int(12*group->controls.size()+1), 12);
dc.SelectObject(bitmap);
dc.Clear();
static_bitmap = new wxStaticBitmap(parent, -1, bitmap, wxDefaultPosition, wxDefaultSize, wxBITMAP_TYPE_BMP);
PadSettingSpin* const threshold_cbox = new PadSettingSpin(parent, group->settings[0].get());
threshold_cbox->wxcontrol->Bind(wxEVT_SPINCTRL, &GamepadPage::AdjustSetting, eventsink);
threshold_cbox->wxcontrol->SetToolTip(_("Adjust the analog control pressure required to activate buttons."));
options.push_back(threshold_cbox);
wxBoxSizer* const szr = new wxBoxSizer(wxHORIZONTAL);
szr->Add(new wxStaticText(parent, -1, wxGetTranslation(StrToWxStr(group->settings[0]->name))),
0, wxCENTER|wxRIGHT, 3);
szr->Add(threshold_cbox->wxcontrol, 0, wxRIGHT, 3);
Add(szr, 0, wxALL|wxCENTER, 3);
Add(static_bitmap, 0, wxALL|wxCENTER, 3);
}
break;
case GROUP_TYPE_MIXED_TRIGGERS:
case GROUP_TYPE_TRIGGERS:
case GROUP_TYPE_SLIDER:
{
int height = (int)(12 * group->controls.size());
int width = 64;
if (GROUP_TYPE_MIXED_TRIGGERS == group->type)
width = 64+12+1;
if (GROUP_TYPE_TRIGGERS != group->type)
height /= 2;
wxBitmap bitmap(width, height+1);
dc.SelectObject(bitmap);
dc.Clear();
static_bitmap = new wxStaticBitmap(parent, -1, bitmap, wxDefaultPosition, wxDefaultSize, wxBITMAP_TYPE_BMP);
for (auto& groupSetting : group->settings)
{
PadSettingSpin* setting = new PadSettingSpin(parent, groupSetting.get());
setting->wxcontrol->Bind(wxEVT_SPINCTRL, &GamepadPage::AdjustSetting, eventsink);
options.push_back(setting);
wxBoxSizer* const szr = new wxBoxSizer(wxHORIZONTAL);
szr->Add(new wxStaticText(parent, -1, wxGetTranslation(StrToWxStr(groupSetting->name))), 0, wxCENTER|wxRIGHT, 3);
szr->Add(setting->wxcontrol, 0, wxRIGHT, 3);
Add(szr, 0, wxALL|wxCENTER, 3);
}
Add(static_bitmap, 0, wxALL|wxCENTER, 3);
}
break;
case GROUP_TYPE_EXTENSION:
{
PadSettingExtension* const attachments = new PadSettingExtension(parent, (ControllerEmu::Extension*)group);
wxButton* const configure_btn = new ExtensionButton(parent, (ControllerEmu::Extension*)group);
options.push_back(attachments);
attachments->wxcontrol->Bind(wxEVT_CHOICE, &GamepadPage::AdjustSetting, eventsink);
configure_btn->Bind(wxEVT_BUTTON, &GamepadPage::ConfigExtension, eventsink);
Add(attachments->wxcontrol, 0, wxTOP|wxLEFT|wxRIGHT|wxEXPAND, 3);
Add(configure_btn, 0, wxALL|wxEXPAND, 3);
}
break;
default:
{
//options
for (auto& groupSetting : group->settings)
{
if (groupSetting->high == DEFAULT_HIGH_VALUE)
{
PadSettingCheckBox* setting_cbox = new PadSettingCheckBox(parent, groupSetting.get());
if (groupSetting->is_iterate == true)
{
setting_cbox->wxcontrol->Bind(wxEVT_CHECKBOX, &GamepadPage::AdjustSettingUI, eventsink);
groupSetting->value = 0;
}
else
{
setting_cbox->wxcontrol->Bind(wxEVT_CHECKBOX, &GamepadPage::AdjustSetting, eventsink);
}
options.push_back(setting_cbox);
Add(setting_cbox->wxcontrol, 0, wxALL | wxLEFT, 5);
}
else
{
PadSettingSpin* setting = new PadSettingSpin(parent, groupSetting.get());
setting->wxcontrol->Bind(wxEVT_SPINCTRL, &GamepadPage::AdjustSetting, eventsink);
options.push_back(setting);
wxBoxSizer* const szr = new wxBoxSizer(wxHORIZONTAL);
szr->Add(new wxStaticText(parent, -1, wxGetTranslation(StrToWxStr(groupSetting->name))), 0, wxCENTER | wxRIGHT, 3);
szr->Add(setting->wxcontrol, 0, wxRIGHT, 3);
Add(szr, 0, wxALL | wxCENTER, 3);
}
}
}
break;
}
dc.SelectObject(wxNullBitmap);
//AddStretchSpacer(0);
}
ControlGroupsSizer::ControlGroupsSizer(ControllerEmu* const controller, wxWindow* const parent, GamepadPage* const eventsink, std::vector<ControlGroupBox*>* groups)
: wxBoxSizer(wxHORIZONTAL)
{
size_t col_size = 0;
wxBoxSizer* stacked_groups = nullptr;
for (auto& group : controller->groups)
{
ControlGroupBox* control_group_box = new ControlGroupBox(group.get(), parent, eventsink);
wxStaticBoxSizer *control_group =
new wxStaticBoxSizer(wxVERTICAL, parent, wxGetTranslation(StrToWxStr(group->name)));
control_group->Add(control_group_box);
const size_t grp_size = group->controls.size() + group->settings.size();
col_size += grp_size;
if (col_size > 8 || nullptr == stacked_groups)
{
if (stacked_groups)
Add(stacked_groups, 0, /*wxEXPAND|*/wxBOTTOM|wxRIGHT, 5);
stacked_groups = new wxBoxSizer(wxVERTICAL);
stacked_groups->Add(control_group, 0, wxEXPAND);
col_size = grp_size;
}
else
{
stacked_groups->Add(control_group, 0, wxEXPAND);
}
if (groups)
groups->push_back(control_group_box);
}
if (stacked_groups)
Add(stacked_groups, 0, /*wxEXPAND|*/wxBOTTOM|wxRIGHT, 5);
}
GamepadPage::GamepadPage(wxWindow* parent, InputConfig& config, const unsigned int pad_num, InputConfigDialog* const config_dialog)
: wxPanel(parent, wxID_ANY)
,controller(config.controllers[pad_num])
, m_config_dialog(config_dialog)
, m_config(config)
{
wxBoxSizer* control_group_sizer = new ControlGroupsSizer(m_config.controllers[pad_num], this, this, &control_groups);
wxStaticBoxSizer* profile_sbox = new wxStaticBoxSizer(wxHORIZONTAL, this, _("Profile"));
// device chooser
wxStaticBoxSizer* const device_sbox = new wxStaticBoxSizer(wxHORIZONTAL, this, _("Device"));
device_cbox = new wxComboBox(this, -1, "", wxDefaultPosition, wxSize(64,-1));
device_cbox->ToggleWindowStyle(wxTE_PROCESS_ENTER);