|
| 1 | +#include "gui/input/HotkeySettings.h" |
| 2 | +#include <config/ActiveSettings.h> |
| 3 | +#include <gui/guiWrapper.h> |
| 4 | + |
| 5 | +extern WindowInfo g_window_info; |
| 6 | +wxFrame* s_main_window = nullptr; |
| 7 | + |
| 8 | +static auto& cfg_hotkeys = GetConfig().hotkeys; |
| 9 | +static const std::unordered_map<uHotkey*, std::function<void(void)>> cfg_hotkey_to_func_map{ |
| 10 | + {&cfg_hotkeys.toggle_fullscreen, [](void) {s_main_window->ShowFullScreen(!s_main_window->IsFullScreen());}}, |
| 11 | + {&cfg_hotkeys.toggle_fullscreen_alt, [](void) {s_main_window->ShowFullScreen(!s_main_window->IsFullScreen());}}, |
| 12 | + {&cfg_hotkeys.exit_fullscreen, [](void) {s_main_window->ShowFullScreen(false);}}, |
| 13 | + {&cfg_hotkeys.take_screenshot, [](void) {g_window_info.has_screenshot_request = true;}}, |
| 14 | +}; |
| 15 | + |
| 16 | +struct HotkeyEntry |
| 17 | +{ |
| 18 | + std::unique_ptr<wxStaticText> name; |
| 19 | + std::unique_ptr<wxButton> key_input; |
| 20 | + uHotkey& hotkey; |
| 21 | + |
| 22 | + HotkeyEntry(wxStaticText* name, wxButton* key_input, uHotkey& hotkey) |
| 23 | + : name(name), key_input(key_input), hotkey(hotkey) { |
| 24 | + key_input->SetClientData(&hotkey); |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +HotkeySettings::HotkeySettings(wxWindow* parent) |
| 29 | + : wxFrame(parent, wxID_ANY, "Hotkey Settings") |
| 30 | +{ |
| 31 | + m_panel = new wxPanel(this); |
| 32 | + m_sizer = new wxFlexGridSizer(0, 2, wxSize(0, 0)); |
| 33 | + |
| 34 | + m_panel->SetSizer(m_sizer); |
| 35 | + Center(); |
| 36 | + |
| 37 | + create_hotkey("Toggle fullscreen", cfg_hotkeys.toggle_fullscreen); |
| 38 | + create_hotkey("Take screenshot", cfg_hotkeys.take_screenshot); |
| 39 | + |
| 40 | + m_sizer->SetSizeHints(this); |
| 41 | +} |
| 42 | + |
| 43 | +HotkeySettings::~HotkeySettings() |
| 44 | +{ |
| 45 | + if (m_need_to_save) |
| 46 | + { |
| 47 | + g_config.Save(); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +void HotkeySettings::init(wxFrame* main_window_frame) |
| 52 | +{ |
| 53 | + hotkey_to_func_map.reserve(cfg_hotkey_to_func_map.size()); |
| 54 | + for (const auto& [cfg_hotkey, func] : cfg_hotkey_to_func_map) |
| 55 | + { |
| 56 | + auto hotkey_raw = cfg_hotkey->raw; |
| 57 | + if (hotkey_raw > 0) |
| 58 | + { |
| 59 | + hotkey_to_func_map[hotkey_raw] = func; |
| 60 | + } |
| 61 | + } |
| 62 | + s_main_window = main_window_frame; |
| 63 | +} |
| 64 | + |
| 65 | +void HotkeySettings::create_hotkey(const wxString& label, uHotkey& hotkey) |
| 66 | +{ |
| 67 | + /* add new hotkey */ |
| 68 | + { |
| 69 | + auto* name = new wxStaticText(m_panel, wxID_ANY, label, wxDefaultPosition, wxSize(240, 30), wxALIGN_CENTER); |
| 70 | + auto* key_input = new wxButton(m_panel, wxID_ANY, hotkey_to_wxstring(hotkey), wxDefaultPosition, wxSize(120, 30), wxWANTS_CHARS); |
| 71 | + |
| 72 | + key_input->Bind(wxEVT_BUTTON, &HotkeySettings::on_hotkey_input_click, this); |
| 73 | + |
| 74 | + auto flags = wxSizerFlags().Expand(); |
| 75 | + m_sizer->Add(name, flags); |
| 76 | + m_sizer->Add(key_input, flags); |
| 77 | + |
| 78 | + m_hotkeys.emplace_back(name, key_input, hotkey); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +void HotkeySettings::on_hotkey_input_click(wxCommandEvent& event) |
| 83 | +{ |
| 84 | + if (active_hotkey_input_btn) |
| 85 | + { |
| 86 | + wxKeyEvent revert_event{}; |
| 87 | + revert_event.SetEventObject(active_hotkey_input_btn); |
| 88 | + on_key_up(revert_event); |
| 89 | + } |
| 90 | + auto* obj = static_cast<wxButton*>(event.GetEventObject()); |
| 91 | + obj->Bind(wxEVT_KEY_UP, &HotkeySettings::on_key_up, this); |
| 92 | + obj->SetLabelText('_'); |
| 93 | + active_hotkey_input_btn = obj; |
| 94 | +} |
| 95 | + |
| 96 | +void HotkeySettings::on_key_up(wxKeyEvent& event) |
| 97 | +{ |
| 98 | + active_hotkey_input_btn = nullptr; |
| 99 | + auto* obj = static_cast<wxButton*>(event.GetEventObject()); |
| 100 | + obj->Unbind(wxEVT_KEY_UP, &HotkeySettings::on_key_up, this); |
| 101 | + auto& cfg_hotkey = *static_cast<uHotkey*>(obj->GetClientData()); |
| 102 | + if (auto keycode = event.GetKeyCode(); is_valid_keycode_up(keycode)) |
| 103 | + { |
| 104 | + auto old_hotkey = cfg_hotkey; |
| 105 | + uHotkey new_hotkey{}; |
| 106 | + new_hotkey.key = keycode; |
| 107 | + new_hotkey.alt = event.AltDown(); |
| 108 | + new_hotkey.ctrl = event.ControlDown(); |
| 109 | + new_hotkey.shift = event.ShiftDown(); |
| 110 | + if ((new_hotkey.raw != old_hotkey.raw) && |
| 111 | + (hotkey_to_func_map.find(new_hotkey.raw) == hotkey_to_func_map.end())) |
| 112 | + { |
| 113 | + m_need_to_save |= true; |
| 114 | + cfg_hotkey = new_hotkey; |
| 115 | + hotkey_to_func_map.erase(old_hotkey.raw); |
| 116 | + hotkey_to_func_map[cfg_hotkey.raw] = cfg_hotkey_to_func_map.at(&cfg_hotkey); |
| 117 | + } |
| 118 | + } |
| 119 | + obj->SetLabelText(hotkey_to_wxstring(cfg_hotkey)); |
| 120 | +} |
| 121 | + |
| 122 | +bool HotkeySettings::is_valid_keycode_up(int keycode) |
| 123 | +{ |
| 124 | + switch (keycode) |
| 125 | + { |
| 126 | + case WXK_NONE: |
| 127 | + case WXK_ESCAPE: |
| 128 | + case WXK_ALT: |
| 129 | + case WXK_CONTROL: |
| 130 | + case WXK_SHIFT: |
| 131 | + return false; |
| 132 | + default: |
| 133 | + return true; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +wxString HotkeySettings::hotkey_to_wxstring(uHotkey hotkey) |
| 138 | +{ |
| 139 | + if (hotkey.raw <= 0) |
| 140 | + { |
| 141 | + return ""; |
| 142 | + } |
| 143 | + wxString ret_val; |
| 144 | + if (hotkey.alt) |
| 145 | + { |
| 146 | + ret_val.append("ALT + "); |
| 147 | + } |
| 148 | + if (hotkey.ctrl) |
| 149 | + { |
| 150 | + ret_val.append("CTRL + "); |
| 151 | + } |
| 152 | + if (hotkey.shift) |
| 153 | + { |
| 154 | + ret_val.append("SHIFT + "); |
| 155 | + } |
| 156 | + ret_val.append(wxAcceleratorEntry(0, hotkey.key).ToString()); |
| 157 | + return ret_val; |
| 158 | +} |
0 commit comments