-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
178 lines (152 loc) · 5.43 KB
/
Copy pathmain.cpp
File metadata and controls
178 lines (152 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <ftxui/component/component.hpp>
#include <ftxui/component/screen_interactive.hpp>
#include <ftxui/dom/elements.hpp>
#include <ftxui/screen/color.hpp>
#include "src/renderer.hpp"
#include "src/segtree.hpp"
#include "src/state.hpp"
#include "src/ui.hpp"
#include "src/utils.hpp"
using namespace ftxui;
using namespace segtree;
int main() {
SegmentTree tree;
VisualizationState visState;
TreeRenderer renderer;
// 默认预设数据
std::string arrayInput = "1 5 4 2 3 9 7";
std::string queryLeft = "2";
std::string queryRight = "4";
std::string updateValue = "2";
// 启动时弹窗选项
bool showStartupPopup = true;
// 构建树
auto doBuild = [&]() {
visState.clearError();
auto arr = parseArray(arrayInput);
if (arr.empty()) {
visState.setError("错误: 数组为空, 请输入用空格分隔的数字");
return;
}
visState.startOperation(OperationType::Build);
tree.build(arr, [&](int p, int l, int r, const std::string& msg) {
visState.recordStep(p, l, r, msg, tree.snapshot());
});
visState.reset();
};
// 区间查询
auto doQuery = [&]() {
visState.clearError();
if (tree.getN() == 0) {
visState.setError("错误: 请先构建树");
return;
}
int ql = parseInt(queryLeft, 1);
int qr = parseInt(queryRight, tree.getN());
if (ql < 1 || qr > tree.getN() || ql > qr) {
visState.setError(std::format("错误: 无效的范围 [{},{}], 有效范围为 [1,{}]", ql, qr, tree.getN()));
return;
}
visState.startOperation(OperationType::Query);
tree.query(ql, qr, [&](int p, int l, int r, const std::string& msg, int v = 0) {
visState.recordStep(p, l, r, msg, tree.snapshot(), v);
});
visState.reset();
};
// 区间更新
auto doUpdate = [&]() {
visState.clearError();
if (tree.getN() == 0) {
visState.setError("错误: 请先构建树");
return;
}
int ql = parseInt(queryLeft, 1);
int qr = parseInt(queryRight, tree.getN());
int val = parseInt(updateValue, 0);
if (ql < 1 || qr > tree.getN() || ql > qr) {
visState.setError(std::format("错误: 无效的范围 [{},{}], 有效范围为 [1,{}]", ql, qr, tree.getN()));
return;
}
visState.startOperation(OperationType::Update);
tree.update(ql, qr, val, [&](int p, int l, int r, const std::string& msg) {
visState.recordStep(p, l, r, msg, tree.snapshot());
});
visState.reset();
};
// 清空树
auto doClear = [&]() {
tree = SegmentTree();
visState.clear();
};
auto onPrev = [&]() { visState.prev(); };
auto onNext = [&]() { visState.next(); };
auto onReset = [&]() { visState.reset(); };
auto onEnd = [&]() { visState.goToEnd(); };
auto closeStartupPopup = [&]() { showStartupPopup = false; };
auto inputComp = InputSection(arrayInput, queryLeft, queryRight, updateValue);
auto opComp = OperationPanel(doBuild, doQuery, doUpdate, doClear);
auto stepComp = StepNavigator(onPrev, onNext, onReset, onEnd);
auto popupComp = PopupContent(closeStartupPopup);
auto mainLayout = Container::Horizontal({inputComp, opComp, stepComp, popupComp});
// 主布局
auto mainComponent = Renderer(mainLayout, [&]() {
// 启动时弹窗
if (showStartupPopup) return vbox(popupComp->Render()) | border | bgcolor(Color::Black) | center;
// 顶栏
auto topbarArea = hbox(
inputComp->Render(),
filler(),
vbox(
opComp->Render(),
stepComp->Render()
)
);
// 演示区
auto legend = renderer.renderLegend();
auto stepInfo = renderer.renderStepInfo(visState);
auto treeView = renderer.render(tree, visState);
// 说明框应处于图层顶层
auto demoArea = dbox(
// 树保持上下及左右居中
vbox(
stepInfo,
filler(),
treeView | flex | center,
filler()
),
legend | clear_under | TopLeft // 需要先清除下层渲染再置左上方
) | flex | border;
return vbox(topbarArea, demoArea);
});
// 键盘事件监听
mainComponent = CatchEvent(mainComponent, [&](Event event) {
if (showStartupPopup) {
if (event == Event::Return || event == Event::Escape) {
showStartupPopup = false;
return true;
}
return false;
}
if (event == Event::ArrowLeft) {
visState.prev();
return true;
}
if (event == Event::ArrowRight) {
visState.next();
return true;
}
if (event == Event::Home) {
visState.reset();
return true;
}
if (event == Event::End) {
visState.goToEnd();
return true;
}
return false;
});
// 应用启动
auto screen = ScreenInteractive::Fullscreen();
screen.Loop(mainComponent);
return 0;
}