OpenTTD Source 20260311-master-g511d3794ce
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 "core/enum_type.hpp"
14#include "gfx_type.h"
15#include "window_type.h"
16#include "string_type.h"
17
22struct Hotkey {
23 Hotkey(uint16_t default_keycode, const std::string &name, int num);
24 Hotkey(const std::vector<uint16_t> &default_keycodes, const std::string &name, int num);
25
27 template <typename EnumType> requires is_scoped_enum_v<EnumType>
28 Hotkey(uint16_t default_keycode, const std::string &name, EnumType num) : Hotkey(default_keycode, name, to_underlying(num)) {}
29
31 template <typename EnumType> requires is_scoped_enum_v<EnumType>
32 Hotkey(const std::vector<uint16_t> &default_keycodes, const std::string &name, EnumType num) : Hotkey(default_keycodes, name, to_underlying(num)) {}
33
34 void AddKeycode(uint16_t keycode);
35
36 const std::string name;
37 int num;
38 std::set<uint16_t> keycodes;
39};
40
41struct IniFile;
42
46struct HotkeyList {
47 typedef EventState (*GlobalHotkeyHandlerFunc)(int hotkey);
48
49 HotkeyList(const std::string &ini_group, const std::vector<Hotkey> &items, GlobalHotkeyHandlerFunc global_hotkey_handler = nullptr);
51
52 void Load(const IniFile &ini);
53 void Save(IniFile &ini) const;
54
55 int CheckMatch(uint16_t keycode, bool global_only = false) const;
56
57 GlobalHotkeyHandlerFunc global_hotkey_handler;
58private:
59 const std::string ini_group;
60 std::vector<Hotkey> items;
61
67 HotkeyList(const HotkeyList &other);
68};
69
70bool IsQuitKey(uint16_t keycode);
71
74
75void HandleGlobalHotkeys(char32_t key, uint16_t keycode);
76
77static constexpr int SPECIAL_HOTKEY_BIT = 30;
78
84inline bool IsSpecialHotkey(const int &hotkey)
85{
86 return HasBit(hotkey, SPECIAL_HOTKEY_BIT);
87}
88
99
109template<class ItemType, class ListType>
110std::tuple<size_t, size_t> GetListIndexStep(SpecialListHotkeys hotkey, const ListType &list, const ItemType &current_item)
111{
112 /* Don't use -1, because how % is implemented for negative numbers. */
113 size_t step_back = list.size() - 1;
114 auto get_relative_index_step = [list, current_item](size_t step) -> std::tuple<size_t, size_t> {
115 size_t index = std::distance(list.begin(), std::ranges::find(list, current_item));
116 return {(index + step) % list.size(), step};
117 };
118
119 switch (hotkey) {
120 default: NOT_REACHED();
122 return {0, 1};
123
125 return {list.size() - 1, step_back};
126
128 return get_relative_index_step(step_back);
129
131 return get_relative_index_step(1);
132 }
133}
134
135#endif /* HOTKEYS_H */
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Type (helpers) for enums.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
constexpr bool is_scoped_enum_v
Implementation of std::is_scoped_enum_v (from C++23).
Definition enum_type.hpp:24
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:77
void LoadHotkeysFromConfig()
Load the hotkeys from the config file.
Definition hotkeys.cpp:333
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:110
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:339
SpecialListHotkeys
Indexes for special hotkeys to navigate in lists.
Definition hotkeys.h:93
@ NextItem
Hotkey to select next item in the list.
Definition hotkeys.h:95
@ FirstItem
Hotkey to select first item in the list.
Definition hotkeys.h:96
@ LastItem
Hotkey to select last item in the list.
Definition hotkeys.h:97
@ PreviousItem
Hotkey to select previous item in the list.
Definition hotkeys.h:94
bool IsSpecialHotkey(const int &hotkey)
Checks if hotkey index is special or not.
Definition hotkeys.h:84
Types for strings.
void Save(IniFile &ini) const
Save HotkeyList to IniFile.
Definition hotkeys.cpp:287
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:302
void Load(const IniFile &ini)
Load HotkeyList from IniFile.
Definition hotkeys.cpp:270
~HotkeyList()
Remove outselves from the global hotkey list.
Definition hotkeys.cpp:261
Hotkey(uint16_t default_keycode, const std::string &name, EnumType num)
Create a new Hotkey object with a single default keycode.
Definition hotkeys.h:28
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
Hotkey(const std::vector< uint16_t > &default_keycodes, const std::string &name, EnumType num)
Create a new Hotkey object with multiple default keycodes.
Definition hotkeys.h:32
Ini file that supports both loading and saving.
Definition ini_type.h:87
Types related to windows.
EventState
State of handling an event.