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 "stl.hh"
8#include "zstring_view.hh"
9
10#include <optional>
11#include <string_view>
12
13namespace openmsx {
14
16{
17public:
32 enum class Type {
33 LOCAL,
34 GLOBAL,
37 };
38 struct Shortcut {
39 ImGuiKeyChord keyChord = ImGuiKey_None;
41
42 [[nodiscard]] bool operator==(const Shortcut& other) const = default;
43 };
45 ImGuiKeyChord keyChord = ImGuiKey_None;
47 bool repeat = false;
48 };
49
50public:
51 Shortcuts();
52 Shortcuts(const Shortcuts&) = delete;
53 Shortcuts(Shortcuts&&) = delete;
54 Shortcuts& operator=(const Shortcuts&) = delete;
56 ~Shortcuts() = default;
57
58 [[nodiscard]] static bool getShortcutRepeat(ID id);
59 [[nodiscard]] static zstring_view getShortcutName(ID id);
60 [[nodiscard]] static zstring_view getShortcutDescription(ID id);
61 [[nodiscard]] static std::optional<ID> parseShortcutName(std::string_view name);
62 [[nodiscard]] static std::optional<Type> parseType(std::string_view name);
63 [[nodiscard]] static const Shortcut& getDefaultShortcut(ID id);
64
65 [[nodiscard]] const Shortcut& getShortcut(ID id) const;
66 void setShortcut(ID id, const Shortcut& shortcut);
67
69
70 [[nodiscard]] bool checkShortcut(const ShortcutWithRepeat& shortcut) const;
71 [[nodiscard]] bool checkShortcut(ID id) const;
72
73 template<typename XmlStream>
74 void saveShortcuts(XmlStream& xml) const
75 {
76 xml.with_tag("shortcuts", [&]{
77 for (auto [id_, shortcut_] : enumerate(shortcuts)) {
78 auto id = static_cast<ID>(id_);
79 const auto& shortcut = shortcut_; // clang-15 workaround
80 if (shortcut == getDefaultShortcut(id)) continue;
81 xml.with_tag("shortcut", [&]{
82 xml.attribute("key", getKeyChordName(shortcut.keyChord));
83 // don't save the 'ALWAYS_xxx' values
84 if (shortcut.type == Type::GLOBAL) xml.attribute("type", "global");
85 xml.data(getShortcutName(id));
86 });
87 }
88 });
89 }
90
91private:
93};
94
95} // namespace openmsx
96
97#endif
static zstring_view getShortcutName(ID id)
Definition Shortcuts.cc:111
~Shortcuts()=default
Shortcuts(const Shortcuts &)=delete
void setDefaultShortcuts()
Definition Shortcuts.cc:81
Shortcuts & operator=(const Shortcuts &)=delete
void saveShortcuts(XmlStream &xml) const
Definition Shortcuts.hh:74
static const Shortcut & getDefaultShortcut(ID id)
Definition Shortcuts.cc:86
static zstring_view getShortcutDescription(ID id)
Definition Shortcuts.cc:130
bool checkShortcut(const ShortcutWithRepeat &shortcut) const
Definition Shortcuts.cc:135
static std::optional< ID > parseShortcutName(std::string_view name)
Definition Shortcuts.cc:116
static std::optional< Type > parseType(std::string_view name)
Definition Shortcuts.cc:123
Shortcuts & operator=(Shortcuts &&)=delete
void setShortcut(ID id, const Shortcut &shortcut)
Definition Shortcuts.cc:96
const Shortcut & getShortcut(ID id) const
Definition Shortcuts.cc:91
static bool getShortcutRepeat(ID id)
Definition Shortcuts.cc:106
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