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