openMSX
Shortcuts.hh
Go to the documentation of this file.
1#ifndef IMGUI_SHORTCUTS_HH
2#define IMGUI_SHORTCUTS_HH
3
4#include "ImGuiUtils.hh"
5
6#include "enumerate.hh"
7#include "zstring_view.hh"
8
9#include <array>
10#include <optional>
11#include <string_view>
12
13namespace openmsx {
14
16{
17public:
30 enum class Type {
31 LOCAL,
32 GLOBAL,
35 };
36 struct Shortcut {
37 ImGuiKeyChord keyChord = ImGuiKey_None;
39
40 [[nodiscard]] bool operator==(const Shortcut& other) const = default;
41 };
43 ImGuiKeyChord keyChord = ImGuiKey_None;
45 bool repeat = false;
46 };
47
48public:
49 Shortcuts();
50 Shortcuts(const Shortcuts&) = delete;
51 Shortcuts(Shortcuts&&) = delete;
52 Shortcuts& operator=(const Shortcuts&) = delete;
54 ~Shortcuts() = default;
55
56 [[nodiscard]] static bool getShortcutRepeat(ID id);
57 [[nodiscard]] static zstring_view getShortcutName(ID id);
58 [[nodiscard]] static zstring_view getShortcutDescription(ID id);
59 [[nodiscard]] static std::optional<ID> parseShortcutName(std::string_view name);
60 [[nodiscard]] static std::optional<Type> parseType(std::string_view name);
61 [[nodiscard]] static const Shortcut& getDefaultShortcut(ID id);
62
63 [[nodiscard]] const Shortcut& getShortcut(ID id) const;
64 void setShortcut(ID id, const Shortcut& shortcut);
65
67
68 [[nodiscard]] bool checkShortcut(const ShortcutWithRepeat& shortcut) const;
69 [[nodiscard]] bool checkShortcut(ID id) const;
70
71 template<typename XmlStream>
72 void saveShortcuts(XmlStream& xml) const
73 {
74 xml.with_tag("shortcuts", [&]{
75 for (auto [id_, shortcut_] : enumerate(shortcuts)) {
76 auto id = static_cast<ID>(id_);
77 const auto& shortcut = shortcut_; // clang-15 workaround
78 if (shortcut == getDefaultShortcut(id)) continue;
79 xml.with_tag("shortcut", [&]{
80 xml.attribute("key", getKeyChordName(shortcut.keyChord));
81 // don't save the 'ALWAYS_xxx' values
82 if (shortcut.type == Type::GLOBAL) xml.attribute("type", "global");
83 xml.data(getShortcutName(id));
84 });
85 }
86 });
87 }
88
89private:
90 std::array<Shortcut, ID::NUM_SHORTCUTS> shortcuts;
91};
92
93} // namespace openmsx
94
95#endif
static zstring_view getShortcutName(ID id)
Definition Shortcuts.cc:109
~Shortcuts()=default
Shortcuts(const Shortcuts &)=delete
void setDefaultShortcuts()
Definition Shortcuts.cc:75
Shortcuts & operator=(const Shortcuts &)=delete
void saveShortcuts(XmlStream &xml) const
Definition Shortcuts.hh:72
static const Shortcut & getDefaultShortcut(ID id)
Definition Shortcuts.cc:80
static zstring_view getShortcutDescription(ID id)
Definition Shortcuts.cc:129
bool checkShortcut(const ShortcutWithRepeat &shortcut) const
Definition Shortcuts.cc:135
static std::optional< ID > parseShortcutName(std::string_view name)
Definition Shortcuts.cc:115
static std::optional< Type > parseType(std::string_view name)
Definition Shortcuts.cc:122
Shortcuts & operator=(Shortcuts &&)=delete
void setShortcut(ID id, const Shortcut &shortcut)
Definition Shortcuts.cc:92
const Shortcut & getShortcut(ID id) const
Definition Shortcuts.cc:86
static bool getShortcutRepeat(ID id)
Definition Shortcuts.cc:103
Shortcuts(Shortcuts &&)=delete
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
constexpr auto enumerate(Iterable &&iterable)
Heavily inspired by Nathan Reed's blog post: Python-Like enumerate() In C++17 http://reedbeta....
Definition enumerate.hh:28
This file implemented 3 utility functions:
Definition Autofire.cc:11
std::string getKeyChordName(ImGuiKeyChord keyChord)
bool operator==(const Shortcut &other) const =default