openMSX
ImGuiManager.hh
Go to the documentation of this file.
1#ifndef IMGUI_MANAGER_HH
2#define IMGUI_MANAGER_HH
3
5#include "ImGuiUtils.hh"
6#include "Shortcuts.hh"
7
8#include "EventListener.hh"
9#include "FilenameSetting.hh"
10#include "IntegerSetting.hh"
11#include "Reactor.hh"
12#include "RomTypes.hh"
13#include "TclObject.hh"
14
15#include "Observer.hh"
16#include "strCat.hh"
17#include "StringReplacer.hh"
18
19#include <functional>
20#include <optional>
21#include <string_view>
22#include <vector>
23
24struct ImFont;
25struct ImGuiTextBuffer;
26
27namespace openmsx {
28
29class CliComm;
30class ImGuiBitmapViewer;
31class ImGuiBreakPoints;
32class ImGuiCharacter;
33class ImGuiCheatFinder;
34class ImGuiConnector;
35class ImGuiConsole;
36class ImGuiDebugger;
37class ImGuiDiskManipulator;
38class ImGuiHelp;
39class ImGuiKeyboard;
40class ImGuiMachine;
41class ImGuiMedia;
42class ImGuiMessages;
43class ImGuiOpenFile;
44class ImGuiPalette;
45class ImGuiOsdIcons;
46class ImGuiReverseBar;
47class ImGuiSettings;
48class ImGuiSoundChip;
49class ImGuiSpriteViewer;
50class ImGuiSymbols;
51class ImGuiTools;
52class ImGuiTrainer;
53class ImGuiVdpRegs;
54class ImGuiWatchExpr;
55class RomInfo;
56class SettingsConfig;
57
58class ImGuiManager final : public ImGuiPartInterface, private EventListener, private Observer<Setting>
59{
60public:
61 ImGuiManager(const ImGuiManager&) = delete;
63
64 explicit ImGuiManager(Reactor& reactor_, SettingsConfig& config_);
66
69
70 [[nodiscard]] Reactor& getReactor() { return reactor; }
71 [[nodiscard]] Shortcuts& getShortcuts() { return shortcuts; }
72 [[nodiscard]] Interpreter& getInterpreter();
73 [[nodiscard]] CliComm& getCliComm();
74 std::optional<TclObject> execute(TclObject command);
75 void executeDelayed(std::function<void()> action);
76 void executeDelayed(TclObject command,
77 const std::function<void(const TclObject&)>& ok,
78 const std::function<void(const std::string&)>& error);
79 void executeDelayed(TclObject command,
80 const std::function<void(const TclObject&)>& ok = {});
81
82 void printError(std::string_view message);
83 template<typename... Ts> void printError(Ts&&... ts) {
84 printError(static_cast<std::string_view>(tmpStrCat(std::forward<Ts>(ts)...)));
85 }
86
87 void preNewFrame();
88 void paintImGui();
89
90 void storeWindowPosition(gl::ivec2 pos) { windowPos = pos; }
91 [[nodiscard]] gl::ivec2 retrieveWindowPosition() const { return windowPos; }
92
93private:
94 void initializeImGui();
95 [[nodiscard]] ImFont* addFont(zstring_view filename, int fontSize);
96 void loadFont();
97 void reloadFont();
98
99 // ImGuiPartInterface
100 [[nodiscard]] zstring_view iniName() const override { return "manager"; }
101 void save(ImGuiTextBuffer& buf) override;
102 void loadLine(std::string_view name, zstring_view value) override;
103 void loadEnd() override;
104
105 // EventListener
106 int signalEvent(const Event& event) override;
107
108 // Observer<Setting>
109 void update(const Setting& setting) noexcept override;
110
111 // ini handler callbacks
112 void iniReadInit();
113 void* iniReadOpen(std::string_view name);
114 void loadLine(void* entry, const char* line) const;
115 void iniApplyAll();
116 void iniWriteAll(ImGuiTextBuffer& buf);
117
118 void updateParts();
119
120private:
121 Reactor& reactor;
122 std::vector<ImGuiPartInterface*> parts;
123 std::vector<ImGuiPartInterface*> toBeAddedParts;
124 bool removeParts = false;
125 Shortcuts shortcuts;
126
127public:
132 ImFont* fontProp = nullptr;
133 ImFont* fontMono = nullptr;
134
135 std::unique_ptr<ImGuiMachine> machine;
136 std::unique_ptr<ImGuiDebugger> debugger;
137 std::unique_ptr<ImGuiBreakPoints> breakPoints;
138 std::unique_ptr<ImGuiSymbols> symbols;
139 std::unique_ptr<ImGuiWatchExpr> watchExpr;
140 std::unique_ptr<ImGuiBitmapViewer> bitmap;
141 std::unique_ptr<ImGuiCharacter> character;
142 std::unique_ptr<ImGuiSpriteViewer> sprite;
143 std::unique_ptr<ImGuiVdpRegs> vdpRegs;
144 std::unique_ptr<ImGuiPalette> palette;
145 std::unique_ptr<ImGuiReverseBar> reverseBar;
146 std::unique_ptr<ImGuiHelp> help;
147 std::unique_ptr<ImGuiOsdIcons> osdIcons;
148 std::unique_ptr<ImGuiOpenFile> openFile;
149 std::unique_ptr<ImGuiMedia> media;
150 std::unique_ptr<ImGuiConnector> connector;
151 std::unique_ptr<ImGuiTools> tools;
152 std::unique_ptr<ImGuiTrainer> trainer;
153 std::unique_ptr<ImGuiCheatFinder> cheatFinder;
154 std::unique_ptr<ImGuiDiskManipulator> diskManipulator;
155 std::unique_ptr<ImGuiSettings> settings;
156 std::unique_ptr<ImGuiSoundChip> soundChip;
157 std::unique_ptr<ImGuiKeyboard> keyboard;
158 std::unique_ptr<ImGuiConsole> console;
159 std::unique_ptr<ImGuiMessages> messages;
160
161 bool menuFade = true;
162 bool needReloadFont = false;
163 std::string loadIniFile;
164
165private:
166 std::vector<std::function<void()>> delayedActionQueue;
167 float menuAlpha = 1.0f;
168
169 std::string droppedFile;
170 std::string insertedInfo;
171 std::vector<std::string> selectList;
172 std::string selectedMedia;
173 const RomInfo* romInfo = nullptr;
174 RomType selectedRomType = ROM_UNKNOWN;
175 float insertedInfoTimeout = 0.0f;
176 gl::ivec2 windowPos;
177 bool mainMenuBarUndocked = false;
178 bool handleDropped = false;
179 bool openInsertedInfo = false;
180 bool guiActive = false;
181
182 static constexpr auto persistentElements = std::tuple{
183 PersistentElement{"mainMenuBarUndocked", &ImGuiManager::mainMenuBarUndocked},
184 PersistentElement{"mainMenuBarFade", &ImGuiManager::menuFade},
185 PersistentElement{"windowPos", &ImGuiManager::windowPos}
186 };
187};
188
189// Parse machine or extension config files:
190// store config-name, display-name and meta-data
191template<typename InfoType>
192std::vector<InfoType> parseAllConfigFiles(ImGuiManager& manager, std::string_view type, std::initializer_list<std::string_view> topics)
193{
194 static constexpr auto replacer = StringReplacer::create(
195 "name", "Name",
196 "manufacturer", "Manufacturer",
197 "code", "Product code",
198 "region", "Region",
199 "release_year", "Release year",
200 "description", "Description",
201 "type", "Type");
202
203 std::vector<InfoType> result;
204
205 const auto& configs = Reactor::getHwConfigs(type);
206 result.reserve(configs.size());
207 for (const auto& config : configs) {
208 auto& info = result.emplace_back();
209 info.configName = config;
210
211 // get machine meta-data
212 auto& configInfo = info.configInfo;
213 if (auto r = manager.execute(makeTclList("openmsx_info", type, config))) {
214 auto first = r->begin();
215 auto last = r->end();
216 while (first != last) {
217 auto desc = *first++;
218 if (first == last) break; // shouldn't happen
219 auto value = *first++;
220 if (!value.empty()) {
221 configInfo.emplace_back(std::string(replacer(desc)),
222 std::string(value));
223 }
224 }
225 }
226
227 // Based on the above meta-data, try to construct a more
228 // readable name Unfortunately this new name is no
229 // longer guaranteed to be unique, we'll address this
230 // below.
231 auto& display = info.displayName;
232 for (const auto topic : topics) {
233 if (const auto* value = getOptionalDictValue(configInfo, topic)) {
234 if (!value->empty()) {
235 if (!display.empty()) strAppend(display, ' ');
236 strAppend(display, *value);
237 }
238 }
239 }
240 if (display.empty()) display = config;
241 }
242
243 ranges::sort(result, StringOp::caseless{}, &InfoType::displayName);
244
245 // make 'displayName' unique again
246 auto sameDisplayName = [](InfoType& x, InfoType& y) {
248 return cmp(x.displayName, y.displayName);
249 };
250 chunk_by(result, sameDisplayName, [](auto first, auto last) {
251 if (std::distance(first, last) == 1) return; // no duplicate name
252 for (auto it = first; it != last; ++it) {
253 strAppend(it->displayName, " (", it->configName, ')');
254 }
255 ranges::sort(first, last, StringOp::caseless{}, &InfoType::displayName);
256 });
257
258 return result;
259}
260
261} // namespace openmsx
262
263#endif
BaseSetting * setting
ImGuiManager & operator=(const ImGuiManager &)=delete
std::unique_ptr< ImGuiMachine > machine
void registerPart(ImGuiPartInterface *part)
std::unique_ptr< ImGuiBreakPoints > breakPoints
void printError(std::string_view message)
std::unique_ptr< ImGuiVdpRegs > vdpRegs
std::unique_ptr< ImGuiCheatFinder > cheatFinder
std::unique_ptr< ImGuiTrainer > trainer
std::unique_ptr< ImGuiDiskManipulator > diskManipulator
IntegerSetting fontMonoSize
std::unique_ptr< ImGuiWatchExpr > watchExpr
std::unique_ptr< ImGuiPalette > palette
std::unique_ptr< ImGuiConnector > connector
std::optional< TclObject > execute(TclObject command)
std::unique_ptr< ImGuiKeyboard > keyboard
std::unique_ptr< ImGuiHelp > help
IntegerSetting fontPropSize
std::unique_ptr< ImGuiSpriteViewer > sprite
Interpreter & getInterpreter()
void storeWindowPosition(gl::ivec2 pos)
ImGuiManager(const ImGuiManager &)=delete
Shortcuts & getShortcuts()
std::unique_ptr< ImGuiConsole > console
std::unique_ptr< ImGuiSoundChip > soundChip
std::unique_ptr< ImGuiReverseBar > reverseBar
std::unique_ptr< ImGuiMedia > media
gl::ivec2 retrieveWindowPosition() const
std::unique_ptr< ImGuiMessages > messages
std::unique_ptr< ImGuiOpenFile > openFile
std::unique_ptr< ImGuiOsdIcons > osdIcons
FilenameSetting fontPropFilename
std::unique_ptr< ImGuiBitmapViewer > bitmap
std::unique_ptr< ImGuiSettings > settings
std::unique_ptr< ImGuiDebugger > debugger
std::unique_ptr< ImGuiCharacter > character
void unregisterPart(ImGuiPartInterface *part)
std::unique_ptr< ImGuiTools > tools
void executeDelayed(std::function< void()> action)
FilenameSetting fontMonoFilename
void printError(Ts &&... ts)
std::unique_ptr< ImGuiSymbols > symbols
A Setting with an integer value.
Generic Gang-of-Four Observer class, templatized edition.
Definition Observer.hh:10
Contains the main loop of openMSX.
Definition Reactor.hh:72
static std::vector< std::string > getHwConfigs(std::string_view type)
Definition Reactor.cc:349
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
constexpr auto create(Args ...args)
This file implemented 3 utility functions:
Definition Autofire.cc:11
const std::string * getOptionalDictValue(const std::vector< std::pair< std::string, std::string > > &info, std::string_view key)
std::vector< InfoType > parseAllConfigFiles(ImGuiManager &manager, std::string_view type, std::initializer_list< std::string_view > topics)
@ ROM_UNKNOWN
Definition RomTypes.hh:94
std::variant< KeyUpEvent, KeyDownEvent, MouseMotionEvent, MouseButtonUpEvent, MouseButtonDownEvent, MouseWheelEvent, JoystickAxisMotionEvent, JoystickHatEvent, JoystickButtonUpEvent, JoystickButtonDownEvent, OsdControlReleaseEvent, OsdControlPressEvent, WindowEvent, TextEvent, FileDropEvent, QuitEvent, FinishFrameEvent, CliCommandEvent, GroupEvent, BootEvent, FrameDrawnEvent, BreakEvent, SwitchRendererEvent, TakeReverseSnapshotEvent, AfterTimedEvent, MachineLoadedEvent, MachineActivatedEvent, MachineDeactivatedEvent, MidiInReaderEvent, MidiInWindowsEvent, MidiInCoreMidiEvent, MidiInCoreMidiVirtualEvent, MidiInALSAEvent, Rs232TesterEvent, Rs232NetEvent, ImGuiDelayedActionEvent, ImGuiActiveEvent > Event
Definition Event.hh:444
TclObject makeTclList(Args &&... args)
Definition TclObject.hh:293
constexpr void sort(RandomAccessRange &&range)
Definition ranges.hh:49
TemporaryString tmpStrCat(Ts &&... ts)
Definition strCat.hh:742
void strAppend(std::string &result, Ts &&...ts)
Definition strCat.hh:752