Pioneer
Loading...
Searching...
No Matches
ActionBinder.h
Go to the documentation of this file.
1// Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
2// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4#pragma once
5
6#include "imgui/imgui.h"
7
8#include <sigc++/sigc++.h>
9
10#include <map>
11#include <string>
12#include <variant>
13
14namespace Editor {
15
16 // Represents an action the user can execute within an editor context
17 // that should be associated with some window-global shortcut.
18 // The predicate will be evaluated to determine if the entry is enabled.
19 struct ActionEntry {
20 template<typename Functor>
21 ActionEntry(std::string_view label, ImGuiKeyChord shortcut, Functor f) :
22 label(label),
24 action(f)
25 {}
26
27 template<typename Predicate, typename Functor>
28 ActionEntry(std::string_view label, ImGuiKeyChord shortcut, Predicate p, Functor f) :
29 label(label),
31 predicate(p),
32 action(f)
33 {}
34
35 template<typename Functor>
36 ActionEntry(std::string_view label, const char *icon, ImGuiKeyChord shortcut, Functor f) :
37 label(label),
38 fontIcon(icon),
40 action(f)
41 {}
42
43 template<typename Predicate, typename Functor>
44 ActionEntry(std::string_view label, const char *icon, ImGuiKeyChord shortcut, Predicate p, Functor f) :
45 label(label),
46 fontIcon(icon),
48 predicate(p),
49 action(f)
50 {}
51
52 std::string label;
53 const char *fontIcon;
54 ImGuiKeyChord shortcut;
55
56 sigc::slot<bool ()> predicate;
57 sigc::slot<void ()> action;
58 };
59
61 public:
64
65 struct Group;
66
67 using GroupEntry = std::variant<Group *, ActionEntry *>;
68 struct Group {
69 Group(std::string_view name, bool menu) :
70 label(std::string(name)),
71 isMenu(menu)
72 {}
73
74 std::string label;
75 std::vector<GroupEntry> entries;
76 bool isMenu;
77 };
78
79 // process all actions and determine if their shortcuts are activated
80 void Update();
81
82 // Draw debug window displaying all registered actions
83 void DrawOverview(const char *title, bool *pOpen = nullptr);
84
85 // Draw all groups registered in this ActionBinder as a main menu bar
86 void DrawMenuBar();
87
88 // draw the GroupEntry named by 'id' in the context of an existing
89 // dropdown menu (i.e. do not submit a top-level BeginMenu)
90 void DrawGroup(std::string id) { DrawMenuInternal(m_groupStorage.at(id), false); }
91
92 // draw the GroupEntry named by 'id' as a submenu (with a top-level BeginMenu)
93 void DrawMenu(std::string id) { DrawMenuInternal(m_groupStorage.at(id), true); }
94
95 // draw the GroupEntry named by 'id' in the context of a button toolbar
96 // void DrawToolbar(std::string id)
97
98 static std::string FormatShortcut(ImGuiKeyChord shortcut);
99
100 // Begin a group or menu with the given ID. ID is a qualified domain
101 // name relative to the current ID stack.
102 ActionBinder &BeginGroup(std::string id) { BeginInternal(id, false); return *this; }
103 ActionBinder &BeginMenu(std::string id) { BeginInternal(id, true); return *this; }
104 void EndGroup() { EndInternal(); }
105 void EndMenu() { EndInternal(); }
106
107 // Add the given action to the currently open group. Fails if no group
108 // is open. The action can be referenced by the fully-qualified id
109 // 'group-id[.group-id[...]].action-id'.
110 ActionBinder &AddAction(std::string id, ActionEntry &&entry) { AddInternal(id, std::move(entry)); return *this; }
111
112 std::vector<std::string> &GetGroups() { return m_topLevelGroups; }
113
114 ActionEntry *GetAction(std::string id);
115 void TriggerAction(std::string id);
116
117 private:
118 void BeginInternal(std::string id, bool menu);
119 void EndInternal();
120
121 void AddInternal(std::string id, ActionEntry &&entry);
122
123 void DrawMenuInternal(Group &group, bool menuHeading);
124
125 void DrawGroupOverviewEntry(Group &group);
126
127 std::map<std::string, ActionEntry> m_actionStorage;
128 std::map<std::string, Group> m_groupStorage;
129
130 std::vector<std::string> m_topLevelGroups;
131
132 std::vector<std::pair<std::string, Group *>> m_activeGroupStack;
133 };
134
135}
Definition: ActionBinder.h:60
std::vector< std::string > & GetGroups()
Definition: ActionBinder.h:112
void DrawGroup(std::string id)
Definition: ActionBinder.h:90
std::variant< Group *, ActionEntry * > GroupEntry
Definition: ActionBinder.h:67
ActionBinder & BeginGroup(std::string id)
Definition: ActionBinder.h:102
ActionBinder & BeginMenu(std::string id)
Definition: ActionBinder.h:103
void DrawMenuBar()
Definition: ActionBinder.cpp:93
void EndMenu()
Definition: ActionBinder.h:105
ActionBinder()
Definition: ActionBinder.cpp:22
void TriggerAction(std::string id)
Definition: ActionBinder.cpp:202
~ActionBinder()
Definition: ActionBinder.cpp:26
ActionEntry * GetAction(std::string id)
Definition: ActionBinder.cpp:194
void DrawMenu(std::string id)
Definition: ActionBinder.h:93
void Update()
Definition: ActionBinder.cpp:39
ActionBinder & AddAction(std::string id, ActionEntry &&entry)
Definition: ActionBinder.h:110
void DrawOverview(const char *title, bool *pOpen=nullptr)
Definition: ActionBinder.cpp:83
static std::string FormatShortcut(ImGuiKeyChord shortcut)
Definition: ActionBinder.cpp:31
void EndGroup()
Definition: ActionBinder.h:104
Definition: ActionBinder.h:14
Definition: ActionBinder.h:68
std::string label
Definition: ActionBinder.h:74
bool isMenu
Definition: ActionBinder.h:76
std::vector< GroupEntry > entries
Definition: ActionBinder.h:75
Group(std::string_view name, bool menu)
Definition: ActionBinder.h:69
Definition: ActionBinder.h:19
ActionEntry(std::string_view label, ImGuiKeyChord shortcut, Functor f)
Definition: ActionBinder.h:21
std::string label
Definition: ActionBinder.h:52
ImGuiKeyChord shortcut
Definition: ActionBinder.h:54
ActionEntry(std::string_view label, ImGuiKeyChord shortcut, Predicate p, Functor f)
Definition: ActionBinder.h:28
ActionEntry(std::string_view label, const char *icon, ImGuiKeyChord shortcut, Functor f)
Definition: ActionBinder.h:36
sigc::slot< void()> action
Definition: ActionBinder.h:57
const char * fontIcon
Definition: ActionBinder.h:53
ActionEntry(std::string_view label, const char *icon, ImGuiKeyChord shortcut, Predicate p, Functor f)
Definition: ActionBinder.h:44
sigc::slot< bool()> predicate
Definition: ActionBinder.h:56