InputConfigDiag.cpp 32 KB

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