OpenTTD Source 20260208-master-g43af8e94d0
hotkeys.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef HOTKEYS_H
11#define HOTKEYS_H
12
13#include "gfx_type.h"
14#include "window_type.h"
15#include "string_type.h"
16
21struct Hotkey {
22 Hotkey(uint16_t default_keycode, const std::string &name, int num);
23 Hotkey(const std::vector<uint16_t> &default_keycodes, const std::string &name, int num);
24
25 void AddKeycode(uint16_t keycode);
26
27 const std::string name;
28 int num;
29 std::set<uint16_t> keycodes;
30};
31
32struct IniFile;
33
37struct HotkeyList {
38 typedef EventState (*GlobalHotkeyHandlerFunc)(int hotkey);
39
40 HotkeyList(const std::string &ini_group, const std::vector<Hotkey> &items, GlobalHotkeyHandlerFunc global_hotkey_handler = nullptr);
41 ~HotkeyList();
42
43 void Load(const IniFile &ini);
44 void Save(IniFile &ini) const;
45
46 int CheckMatch(uint16_t keycode, bool global_only = false) const;
47
48 GlobalHotkeyHandlerFunc global_hotkey_handler;
49private:
50 const std::string ini_group;
51 std::vector<Hotkey> items;
52
57 HotkeyList(const HotkeyList &other);
58};
59
60bool IsQuitKey(uint16_t keycode);
61
64
65void HandleGlobalHotkeys(char32_t key, uint16_t keycode);
66
67static constexpr int SPECIAL_HOTKEY_BIT = 30;
68
74inline bool IsSpecialHotkey(const int &hotkey)
75{
76 return HasBit(hotkey, SPECIAL_HOTKEY_BIT);
77}
78
89
99template<class ItemType, class ListType>
100std::tuple<size_t, size_t> GetListIndexStep(SpecialListHotkeys hotkey, const ListType &list, const ItemType &current_item)
101{
102 /* Don't use -1, because how % is implemented for negative numbers. */
103 size_t step_back = list.size() - 1;
104 auto get_relative_index_step = [list, current_item](size_t step) -> std::tuple<size_t, size_t> {
105 size_t index = std::distance(list.begin(), std::ranges::find(list, current_item));
106 return {(index + step) % list.size(), step};
107 };
108
109 switch (hotkey) {
110 default: NOT_REACHED();
112 return {0, 1};
113
115 return {list.size() - 1, step_back};
116
118 return get_relative_index_step(step_back);
119
121 return get_relative_index_step(1);
122 }
123}
124
125#endif /* HOTKEYS_H */
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Types related to the graphics and/or input devices.
static constexpr int SPECIAL_HOTKEY_BIT
Bit which denotes that hotkey isn't bound to UI button.
Definition hotkeys.h:67
void LoadHotkeysFromConfig()
Load the hotkeys from the config file.
Definition hotkeys.cpp:332
std::tuple< size_t, size_t > GetListIndexStep(SpecialListHotkeys hotkey, const ListType &list, const ItemType &current_item)
Gets the first index in the list for given hotkey and the step to look for another if first is invali...
Definition hotkeys.h:100
bool IsQuitKey(uint16_t keycode)
Does the given keycode match one of the keycodes bound to 'quit game'?
Definition main_gui.cpp:541
void SaveHotkeysToConfig()
Save the hotkeys to the config file.
Definition hotkeys.cpp:338
SpecialListHotkeys
Indexes for special hotkeys to navigate in lists.
Definition hotkeys.h:83
@ NextItem
Hotkey to select next item in the list.
Definition hotkeys.h:85
@ FirstItem
Hotkey to select first item in the list.
Definition hotkeys.h:86
@ LastItem
Hotkey to select last item in the list.
Definition hotkeys.h:87
@ PreviousItem
Hotkey to select previous item in the list.
Definition hotkeys.h:84
bool IsSpecialHotkey(const int &hotkey)
Checks if hotkey index is special or not.
Definition hotkeys.h:74
Types for strings.
void Save(IniFile &ini) const
Save HotkeyList to IniFile.
Definition hotkeys.cpp:286
HotkeyList(const HotkeyList &other)
Dummy private copy constructor to prevent compilers from copying the structure, which fails due to _h...
int CheckMatch(uint16_t keycode, bool global_only=false) const
Check if a keycode is bound to something.
Definition hotkeys.cpp:301
void Load(const IniFile &ini)
Load HotkeyList from IniFile.
Definition hotkeys.cpp:269
Hotkey(uint16_t default_keycode, const std::string &name, int num)
Create a new Hotkey object with a single default keycode.
Definition hotkeys.cpp:221
void AddKeycode(uint16_t keycode)
Add a keycode to this hotkey, from now that keycode will be matched in addition to any previously add...
Definition hotkeys.cpp:248
Ini file that supports both loading and saving.
Definition ini_type.h:86
Types related to windows.
EventState
State of handling an event.