GitHub Viewer
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: E02-MockGUI.cpp
@Time: 2023/9/16 21:22
@Desc: 本example主要展示,在不修改之前任何功能(node中的逻辑)的情况下,
通过aspect和event功能,在界面上展示当前正在执行的node,
模拟 GUI 中node的变亮(或者变暗)的操作。
***************************/
#include
#include
#include
#include "CGraph.h"
using namespace CGraph;
const static char* EXAMPLE_PARAM_KEY = "example-param-key";
const static char* EXAMPLE_EVENT_KEY = "example-event-key";
struct ProcessGParam : public GParam {
CVoid change(const std::string& name, bool isBegin) {
if (isBegin) {
running_elements_.insert(name);
} else {
running_elements_.erase(name);
}
}
CVoid print() {
std::cout getName(), false);
}
notify(EXAMPLE_EVENT_KEY, GEventType::SYNC);
}
};
class ActionGNode : public GNode {
CStatus run() override {
int ms = std::abs((int)std::random_device{}()) % 4000 + 1000;
CGRAPH_SLEEP_MILLISECOND(ms); // 一个算子,随机休息一段时间,时长 1000~5000 ms
return CStatus();
}
};
void example_mock_gui() {
GElementPtr a, b, c, d, e, f, g = nullptr;
auto pipeline = GPipelineFactory::create();
pipeline->registerGElement(&a, {}, "nodeA");
pipeline->registerGElement(&b, {a}, "nodeB");
pipeline->registerGElement(&c, {a}, "nodeC");
pipeline->registerGElement(&d, {b}, "nodeD");
pipeline->registerGElement(&e, {b, c}, "nodeE");
pipeline->registerGElement(&f, {d, e}, "nodeF");
pipeline->registerGElement(&g, {e}, "nodeG");
pipeline->createGParam(EXAMPLE_PARAM_KEY);
pipeline->addGEvent(EXAMPLE_EVENT_KEY);
pipeline->addGAspect(); // 在每个node,开始和结束的时候,去触发 EXAMPLE_EVENT_KEY 事件,显示当前正在running的node信息
pipeline->process();
GPipelineFactory::clear();
}
int main() {
example_mock_gui();
return 0;
}