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