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