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(Reactor& reactor_, SettingsConfig& config_);
62 ImGuiManager(const ImGuiManager&) = delete;
67
70
71 [[nodiscard]] Reactor& getReactor() { return reactor; }
72 [[nodiscard]] Shortcuts& getShortcuts() { return shortcuts; }
73 [[nodiscard]] Interpreter& getInterpreter();
74 [[nodiscard]] CliComm& getCliComm();
75 std::optional<TclObject> execute(TclObject command);
76 void executeDelayed(std::function<void()> action);
77 void executeDelayed(TclObject command,
78 const std::function<void(const TclObject&)>& ok,
79 const std::function<void(const std::string&)>& error);
80 void executeDelayed(TclObject command,
81 const std::function<void(const TclObject&)>& ok = {});
82
83 void printError(std::string_view message);
84 template<typename... Ts> void printError(Ts&&... ts) {
85 printError(static_cast<std::string_view>(tmpStrCat(std::forward<Ts>(ts)...)));
86 }
87
88 void preNewFrame();
89 void paintImGui();
90
91 void storeWindowPosition(gl::ivec2 pos) { windowPos = pos; }
92 [[nodiscard]] gl::ivec2 retrieveWindowPosition() const { return windowPos; }
93
94private:
95 void initializeImGui();
96 [[nodiscard]] ImFont* addFont(zstring_view filename, int fontSize);
97 void loadFont();
98 void reloadFont();
99
100 // ImGuiPartInterface
101 [[nodiscard]] zstring_view iniName() const override { return "manager"; }
102 void save(ImGuiTextBuffer& buf) override;
103 void loadLine(std::string_view name, zstring_view value) override;
104 void loadEnd() override;
105
106 // EventListener
107 int signalEvent(const Event& event) override;
108
109 // Observer<Setting>
110 void update(const Setting& setting) noexcept override;
111
112 // ini handler callbacks
113 void iniReadInit();
114 void* iniReadOpen(std::string_view name);
115 void loadLine(void* entry, const char* line) const;
116 void iniApplyAll();
117 void iniWriteAll(ImGuiTextBuffer& buf);
118
119 void updateParts();
120
121private:
122 Reactor& reactor;
123 std::vector<ImGuiPartInterface*> parts;
124 std::vector<ImGuiPartInterface*> toBeAddedParts;
125 bool removeParts = false;
126 Shortcuts shortcuts;
127
128public:
133 ImFont* fontProp = nullptr;
134 ImFont* fontMono = nullptr;
135
136 std::unique_ptr<ImGuiMachine> machine;
137 std::unique_ptr<ImGuiDebugger> debugger;
138 std::unique_ptr<ImGuiBreakPoints> breakPoints;
139 std::unique_ptr<ImGuiSymbols> symbols;
140 std::unique_ptr<ImGuiWatchExpr> watchExpr;
141 std::unique_ptr<ImGuiBitmapViewer> bitmap;
142 std::unique_ptr<ImGuiCharacter> character;
143 std::unique_ptr<ImGuiSpriteViewer> sprite;
144 std::unique_ptr<ImGuiVdpRegs> vdpRegs;
145 std::unique_ptr<ImGuiPalette> palette;
146 std::unique_ptr<ImGuiReverseBar> reverseBar;
147 std::unique_ptr<ImGuiHelp> help;
148 std::unique_ptr<ImGuiOsdIcons> osdIcons;
149 std::unique_ptr<ImGuiOpenFile> openFile;
150 std::unique_ptr<ImGuiMedia> media;
151 std::unique_ptr<ImGuiConnector> connector;
152 std::unique_ptr<ImGuiTools> tools;
153 std::unique_ptr<ImGuiTrainer> trainer;
154 std::unique_ptr<ImGuiCheatFinder> cheatFinder;
155 std::unique_ptr<ImGuiDiskManipulator> diskManipulator;
156 std::unique_ptr<ImGuiSettings> settings;
157 std::unique_ptr<ImGuiSoundChip> soundChip;
158 std::unique_ptr<ImGuiKeyboard> keyboard;
159 std::unique_ptr<ImGuiConsole> console;
160 std::unique_ptr<ImGuiMessages> messages;
161
162 bool menuFade = true;
163 bool needReloadFont = false;
164 std::string loadIniFile;
165
166private:
167 std::vector<std::function<void()>> delayedActionQueue;
168 float menuAlpha = 1.0f;
169
170 std::string droppedFile;
171 std::string insertedInfo;
172 std::vector<std::string> selectList;
173 std::string selectedMedia;
174 const RomInfo* romInfo = nullptr;
175 RomType selectedRomType = ROM_UNKNOWN;
176 float insertedInfoTimeout = 0.0f;
177 gl::ivec2 windowPos;
178 bool mainMenuBarUndocked = false;
179 bool handleDropped = false;
180 bool openInsertedInfo = false;
181 bool guiActive = false;
182
183 static constexpr auto persistentElements = std::tuple{
184 PersistentElement{"mainMenuBarUndocked", &ImGuiManager::mainMenuBarUndocked},
185 PersistentElement{"mainMenuBarFade", &ImGuiManager::menuFade},
186 PersistentElement{"windowPos", &ImGuiManager::windowPos}
187 };
188};
189
190// Parse machine or extension config files:
191// store config-name, display-name and meta-data
192template<typename InfoType>
193std::vector<InfoType> parseAllConfigFiles(ImGuiManager& manager, std::string_view type, std::initializer_list<std::string_view> topics)
194{
195 static constexpr auto replacer = StringReplacer::create(
196 "name", "Name",
197 "manufacturer", "Manufacturer",
198 "code", "Product code",
199 "region", "Region",
200 "release_year", "Release year",
201 "description", "Description",
202 "type", "Type");
203
204 std::vector<InfoType> result;
205
206 const auto& configs = Reactor::getHwConfigs(type);
207 result.reserve(configs.size());
208 for (const auto& config : configs) {
209 auto& info = result.emplace_back();
210 info.configName = config;
211
212 // get machine meta-data
213 auto& configInfo = info.configInfo;
214 if (auto r = manager.execute(makeTclList("openmsx_info", type, config))) {
215 auto first = r->begin();
216 auto last = r->end();
217 while (first != last) {
218 auto desc = *first++;
219 if (first == last) break; // shouldn't happen
220 auto value = *first++;
221 if (!value.empty()) {
222 configInfo.emplace_back(std::string(replacer(desc)),
223 std::string(value));
224 }
225 }
226 }
227
228 // Based on the above meta-data, try to construct a more
229 // readable name Unfortunately this new name is no
230 // longer guaranteed to be unique, we'll address this
231 // below.
232 auto& display = info.displayName;
233 for (const auto topic : topics) {
234 if (const auto* value = getOptionalDictValue(configInfo, topic)) {
235 if (!value->empty()) {
236 if (!display.empty()) strAppend(display, ' ');
237 strAppend(display, *value);
238 }
239 }
240 }
241 if (display.empty()) display = config;
242 }
243
244 ranges::sort(result, StringOp::caseless{}, &InfoType::displayName);
245
246 // make 'displayName' unique again
247 auto sameDisplayName = [](InfoType& x, InfoType& y) {
249 return cmp(x.displayName, y.displayName);
250 };
251 chunk_by(result, sameDisplayName, [](auto first, auto last) {
252 if (std::distance(first, last) == 1) return; // no duplicate name
253 for (auto it = first; it != last; ++it) {
254 strAppend(it->displayName, " (", it->configName, ')');
255 }
256 ranges::sort(first, last, StringOp::caseless{}, &InfoType::displayName);
257 });
258
259 return result;
260}
261
262} // namespace openmsx
263
264#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
ImGuiManager(ImGuiManager &&)=delete
ImGuiManager & operator=(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:74
static std::vector< std::string > getHwConfigs(std::string_view type)
Definition Reactor.cc:348
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:446
TclObject makeTclList(Args &&... args)
Definition TclObject.hh:293
constexpr void sort(RandomAccessRange &&range)
Definition ranges.hh:51
TemporaryString tmpStrCat(Ts &&... ts)
Definition strCat.hh:742
void strAppend(std::string &result, Ts &&...ts)
Definition strCat.hh:752