OXYGENHUB
◆ main openmsx / src / imgui / ImGuiConsole.hh
3.1 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef IMGUI_CONSOLE_HH
2
#define IMGUI_CONSOLE_HH
3
4
#include "ImGuiPart.hh"
5
#include "ImGuiUtils.hh"
6
7
#include "BooleanSetting.hh"
8
#include "CompletionCandidate.hh"
9
#include "ConsoleLine.hh"
10
#include "InterpreterOutput.hh"
11
12
#include "Observer.hh"
13
#include "circular_buffer.hh"
14
#include "function_ref.hh"
15
16
#include <string>
17
18
struct ImGuiInputTextCallbackData;
19
20
namespace openmsx {
21
22
class ImGuiConsole final : public ImGuiPart
23
                         , private InterpreterOutput
24
                         , private Observer<Setting>
25
{
26
public:
27
	explicit ImGuiConsole(ImGuiManager& manager);
28
	~ImGuiConsole();
29
30
	[[nodiscard]] zstring_view iniName() const override { return "console"; }
31
	void save(ImGuiTextBuffer& buf) override;
32
	void loadLine(std::string_view name, zstring_view value) override;
33
	void paint(MSXMotherBoard* motherBoard) override;
34
35
public:
36
	bool show = false;
37
38
	struct CompletionPopupLayout {
39
		std::vector<float> columnWidths = {};
40
		gl::vec2 size = {};
41
		bool needXScrollBar = false;
42
		bool needYScrollBar = false;
43
	};
44
45
private:
46
	void print(std::string_view text, imColor color = imColor::TEXT);
47
	void newLineConsole(ConsoleLine line);
48
	static int textEditCallbackStub(ImGuiInputTextCallbackData* data);
49
	int textEditCallback(ImGuiInputTextCallbackData* data);
50
	void doEdit(ImGuiInputTextCallbackData* data);
51
	void tabEdit(ImGuiInputTextCallbackData* data, function_ref<std::string(std::string_view)> action);
52
	void colorize(std::string_view line);
53
	void putHistory(std::string command);
54
	void saveHistory();
55
	void loadHistory();
56
57
	// InterpreterOutput
58
	void output(std::string_view text) override;
59
	[[nodiscard]] unsigned getOutputColumns() const override;
60
	void setCompletions(std::vector<CompletionCandidate> completions) override;
61
62
	// Observer
63
	void update(const Setting& setting) noexcept override;
64
65
private:
66
	BooleanSetting consoleSetting;
67
68
	circular_buffer<std::string> history;
69
	std::string historyBackupLine;
70
	int historyPos = -1; // -1: new line, 0..history.size()-1 browsing history.
71
72
	circular_buffer<ConsoleLine> lines; // output lines
73
74
	std::string commandBuffer; // collect multi-line commands
75
76
	std::string prompt;
77
	std::string inputBuf;
78
	ConsoleLine coloredInputBuf;
79
80
	unsigned columns = 80; // gets recalculated
81
	bool scrollToBottom = false;
82
	bool wasShown = false;
83
	bool wrap = true;
84
85
	// completion popup variables
86
	// -- these are needed while popup is open
87
	std::vector<CompletionCandidate> completions;
88
	CompletionPopupLayout popupLayout;
89
	int completionIndex = -1;
90
	// -- these are only needed between 2 frames
91
	std::vector<ImWchar> replayInput; // replay input typed in popup in text field
92
	CompletionCandidate completionReplacement; // apply the selection from popup in text field
93
	float textCursorScrnPosX = 0.0f; // last drawn text cursor X position
94
	bool replayBackspace = false; // whether to replay a backspace in text field
95
	bool completionPopupOpen = false; // was popup open in last frame
96
97
	static constexpr auto persistentElements = std::tuple{
98
		PersistentElement{"show", &ImGuiConsole::show},
99
		PersistentElement{"wrap", &ImGuiConsole::wrap}
100
	};
101
};
102
103
} // namespace openmsx
104
105
#endif