OXYGENHUB
◆ main openmsx / src / imgui / DebuggableEditor.hh
6.1 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef DEBUGGABLE_EDITOR_HH
2
#define DEBUGGABLE_EDITOR_HH
3
4
// Based on:
5
//     Mini memory editor for Dear ImGui (to embed in your game/tools)
6
//
7
//     Get latest version at http://www.github.com/ocornut/imgui_club
8
//     (See there for documentation and the (old) Changelog).
9
//
10
//     Licence: MIT
11
12
#include "ImGuiPart.hh"
13
14
#include <imgui.h>
15
16
#include <optional>
17
#include <string>
18
#include <vector>
19
20
namespace openmsx {
21
22
class Debuggable;
23
class ImGuiManager;
24
class SymbolManager;
25
26
class DebuggableEditor final : public ImGuiPart
27
{
28
	struct Sizes {
29
		int addrDigitsCount = 0;
30
		float lineHeight = 0.0f;
31
		float glyphWidth = 0.0f;
32
		float hexCellWidth = 0.0f;
33
		float spacingBetweenMidCols = 0.0f;
34
		float posHexStart = 0.0f;
35
		float posAsciiStart = 0.0f;
36
		float posAsciiEnd = 0.0f;
37
		float windowWidth = 0.0f;
38
	};
39
40
public:
41
	explicit DebuggableEditor(ImGuiManager& manager_, std::string_view debuggableName, size_t index);
42
	[[nodiscard]] std::string_view getDebuggableName() const { return {title.data(), debuggableNameSize}; }
43
44
	void makeSnapshot(MSXMotherBoard& motherBoard);
45
	void makeSnapshot(Debuggable& debuggable);
46
47
	// ImGuiPart
48
	[[nodiscard]] zstring_view iniName() const override { return title; }
49
	void save(ImGuiTextBuffer& buf) override;
50
	void loadLine(std::string_view name, zstring_view value) override;
51
	void loadEnd() override;
52
	void paint(MSXMotherBoard* motherBoard) override;
53
54
public:
55
	bool open = true;
56
57
private:
58
	[[nodiscard]] Sizes calcSizes(unsigned memSize) const;
59
	[[nodiscard]] std::string formatAddr(const Sizes& s, unsigned addr) const;
60
	void setStrings(const Sizes& s, Debuggable& debuggable);
61
	bool setAddr(const Sizes& s, Debuggable& debuggable, unsigned memSize, unsigned addr);
62
	void scrollAddr(const Sizes& s, Debuggable& debuggable, unsigned memSize, unsigned addr, bool forceScroll);
63
64
	void drawExport(const Sizes& s, Debuggable& debuggable);
65
	void drawContents(const Sizes& s, Debuggable& debuggable, unsigned memSize);
66
	void drawSearch(const Sizes& s, Debuggable& debuggable, unsigned memSize);
67
	void parseSearchString(std::string_view str);
68
	void search(const Sizes& s, Debuggable& debuggable, unsigned memSize);
69
	[[nodiscard]] bool match(Debuggable& debuggable, unsigned memSize, unsigned addr);
70
	void drawPreviewLine(const Sizes& s, Debuggable& debuggable, unsigned memSize);
71
72
private:
73
	enum AddressMode : int {CURSOR, EXPRESSION};
74
	enum class SearchType : int {HEX, ASCII};
75
	enum class SearchHighlight : int {NONE, SINGLE, ALL};
76
	enum class SearchDirection : int {FWD, BWD};
77
	enum PreviewEndianess : int {LE, BE};
78
79
	enum ExportRange : int {RANGE_ALL, RANGE_RANGE};
80
	enum ExportFormatted : int {EXPORT_RAW, EXPORT_FORMATTED};
81
	enum ExportFormat : int {FORMAT_BIN, FORMAT_DEC, FORMAT_HEX, FORMAT_ASCII, FORMAT_CUSTOM};
82
	enum ExportDestination : int {OUTPUT_CLIPBOARD, OUTPUT_FILE};
83
84
	SymbolManager& symbolManager;
85
	size_t debuggableNameSize;
86
	std::string title; // debuggableName + suffix
87
88
	// Settings
89
	static constexpr int MAX_COLUMNS = 64;
90
	int  columns = 16;            // number of columns to display.
91
	bool showAscii = true;        // display ASCII representation on the right side.
92
	bool showAddress = true;      // display the address bar (e.g. on small views it can make sense to hide this)
93
	bool showSearch = false;      // display search functionality
94
	bool showDataPreview = false; // display a footer previewing the decimal/binary/hex/float representation of the currently selected bytes.
95
	bool showSymbolInfo = false;  // display symbol information and highlight known symbols in hex view
96
	bool greyOutZeroes = true;    // display null/zero bytes using the TextDisabled color.
97
	std::string addrExpr;
98
	std::string searchString;
99
	unsigned currentAddr = 0;
100
	int addrMode = CURSOR;
101
	int searchType = static_cast<int>(SearchType::HEX);
102
	int searchHighlight = static_cast<int>(SearchHighlight::SINGLE);
103
	int searchDirection = static_cast<int>(SearchDirection::FWD);
104
	int previewEndianess = LE;
105
	ImGuiDataType previewDataType = ImGuiDataType_U8;
106
107
	bool showExportWindow = false;
108
	int exportRange = RANGE_ALL;
109
	int exportFormatted = EXPORT_FORMATTED;
110
	int exportFormat = FORMAT_HEX;
111
	int exportDestination = OUTPUT_CLIPBOARD;
112
	int exportColumns = 16;
113
	std::string exportBegin, exportEnd;
114
	std::string exportPrefix;
115
	std::string exportSuffix;
116
	std::string exportCustomFormat = "#%02X";
117
	std::string exportFilename;
118
	std::string exportStatus;
119
	float exportStatusTimeout = 0.0f;
120
121
	static constexpr auto persistentElements = std::tuple{
122
		PersistentElement   {"open",             &DebuggableEditor::open},
123
		PersistentElementMax{"columns",          &DebuggableEditor::columns, MAX_COLUMNS + 1},
124
		PersistentElement   {"showAscii",        &DebuggableEditor::showAscii},
125
		PersistentElement   {"showAddress",      &DebuggableEditor::showAddress},
126
		PersistentElement   {"showSearch",       &DebuggableEditor::showSearch},
127
		PersistentElement   {"showDataPreview",  &DebuggableEditor::showDataPreview},
128
		PersistentElement   {"showSymbolInfo",   &DebuggableEditor::showSymbolInfo},
129
		PersistentElement   {"greyOutZeroes",    &DebuggableEditor::greyOutZeroes},
130
		PersistentElement   {"addrExpr",         &DebuggableEditor::addrExpr},
131
		PersistentElement   {"searchString",     &DebuggableEditor::searchString},
132
		PersistentElement   {"currentAddr",      &DebuggableEditor::currentAddr},
133
		PersistentElementMax{"addrMode",         &DebuggableEditor::addrMode, 2},
134
		PersistentElementMax{"searchType",       &DebuggableEditor::searchType, 2},
135
		PersistentElementMax{"searchHighlight",  &DebuggableEditor::searchHighlight, 2},
136
		PersistentElementMax{"searchDirection",  &DebuggableEditor::searchDirection, 2},
137
		PersistentElementMax{"previewEndianess", &DebuggableEditor::previewEndianess, 2},
138
		PersistentElementMax{"previewDataType",  &DebuggableEditor::previewDataType, 8}
139
	};
140
141
	// [Internal State]
142
	std::string dataInput;
143
	std::string addrStr;
144
	std::optional<std::vector<uint8_t>> searchPattern;
145
	std::optional<unsigned> searchResult;
146
	enum EditType : uint8_t { HEX, ASCII };
147
	EditType dataEditingActive = HEX;
148
	bool dataEditingTakeFocus = false;
149
	bool updateAddr = false;
150
	bool switchedTab = false;
151
	bool resetCursor = false;
152
153
	std::vector<uint8_t> snapshot;
154
};
155
156
} // namespace openmsx
157
158
#endif