From d3a9d7565b18404b2042c9dc025cc690250cb50c Mon Sep 17 00:00:00 2001 From: rtk0c Date: Sun, 13 Jun 2021 17:16:46 -0700 Subject: Move UI_Workflows.cpp > WorkflowCategory to be a part of WorkflowNode's RTTI (cherry picked from commit f45084562cfc95392b4dfb968dd215c25be3fc67) --- core/src/UI/UI_Workflows.cpp | 143 +++---------------------------------------- 1 file changed, 9 insertions(+), 134 deletions(-) (limited to 'core/src/UI') diff --git a/core/src/UI/UI_Workflows.cpp b/core/src/UI/UI_Workflows.cpp index 68fd3ee..e61e934 100644 --- a/core/src/UI/UI_Workflows.cpp +++ b/core/src/UI/UI_Workflows.cpp @@ -21,126 +21,10 @@ namespace ImNodes = ax::NodeEditor; namespace { -enum class WorkflowCategory -{ - Numeric, - Text, - Documents, - UserInput, - SystemInput, - Output, -}; - -class WorkflowDatabase -{ -public: - using WorkflowNodeConstructor = std::unique_ptr (*)(); - - struct Candidate - { - WorkflowNodeConstructor Constructor; - std::string Name; - WorkflowCategory Category; - }; - -private: - std::vector mCandidates; - - int mTextOffset; - int mDocumentOffset; - int mUserInputOffset; - int mSystemInputNodes; - int mOutputOffset; - -public: - // clang-format off - std::span GetNumericNodes() { return { &mCandidates[0], (size_t)(mTextOffset - 0) }; }; - std::span GetTextNodes() { return { &mCandidates[mTextOffset], (size_t)(mDocumentOffset - mTextOffset) }; }; - std::span GetDocumentNodes() { return { &mCandidates[mDocumentOffset], (size_t)(mUserInputOffset - mDocumentOffset) }; }; - std::span GetUserInputNodes() { return { &mCandidates[mUserInputOffset], (size_t)(mSystemInputNodes - mUserInputOffset) }; }; - std::span GetSystemInputNodes() { return { &mCandidates[mSystemInputNodes], (size_t)(mOutputOffset - mSystemInputNodes) }; }; - std::span GetOutputNodes() { return { &mCandidates[mOutputOffset], (size_t)(mCandidates.size() - mOutputOffset) }; }; - // clang-format on - -public: - static WorkflowDatabase& GetInstance() - { - static WorkflowDatabase database; - return database; - } - -public: - WorkflowDatabase() - { - // Numeric nodes offset start at 0 - mCandidates.push_back(Candidate{ - .Constructor = []() -> std::unique_ptr { return std::make_unique(NumericOperationNode::Addition); }, - .Name = I18N_TEXT("Add", L10N_WORKFLOW_ADD), - .Category = WorkflowCategory::Numeric, - }); - mCandidates.push_back(Candidate{ - .Constructor = []() -> std::unique_ptr { return std::make_unique(NumericOperationNode::Subtraction); }, - .Name = I18N_TEXT("Subtract", L10N_WORKFLOW_SUB), - .Category = WorkflowCategory::Numeric, - }); - mCandidates.push_back(Candidate{ - .Constructor = []() -> std::unique_ptr { return std::make_unique(NumericOperationNode::Multiplication); }, - .Name = I18N_TEXT("Multiply", L10N_WORKFLOW_MUL), - .Category = WorkflowCategory::Numeric, - }); - mCandidates.push_back(Candidate{ - .Constructor = []() -> std::unique_ptr { return std::make_unique(NumericOperationNode::Division); }, - .Name = I18N_TEXT("Divide", L10N_WORKFLOW_DIV), - .Category = WorkflowCategory::Numeric, - }); - mCandidates.push_back(Candidate{ - .Constructor = []() -> std::unique_ptr { return std::make_unique(); }, - .Name = I18N_TEXT("Evaluate expression", L10N_WORKFLOW_EVAL), - .Category = WorkflowCategory::Numeric, - }); - - mTextOffset = mCandidates.size(); - mCandidates.push_back(Candidate{ - .Constructor = []() -> std::unique_ptr { return std::make_unique(); }, - .Name = I18N_TEXT("Format text", L10N_WORKFLOW_FMT), - .Category = WorkflowCategory::Text, - }); - - mDocumentOffset = mCandidates.size(); - mCandidates.push_back(Candidate{ - .Constructor = []() -> std::unique_ptr { return std::make_unique(); }, - .Name = I18N_TEXT("Expand template", L10N_WORKFLOW_INSTANTIATE_TEMPLATE), - .Category = WorkflowCategory::Documents, - }); - - /* Inputs */ - - mUserInputOffset = mCandidates.size(); - mCandidates.push_back(Candidate{ - .Constructor = []() -> std::unique_ptr { return std::make_unique(); }, - .Name = I18N_TEXT("Form input", L10N_WORKFLOW_FORM_INPUT), - .Category = WorkflowCategory::UserInput, - }); - - mCandidates.push_back(Candidate{ - .Constructor = []() -> std::unique_ptr { return std::make_unique(); }, - .Name = I18N_TEXT("Database input", L10N_WORKFLOW_DB_INPUT), - .Category = WorkflowCategory::UserInput, - }); - - mSystemInputNodes = mCandidates.size(); - - /* Outputs */ - - mOutputOffset = mCandidates.size(); - } -}; - class WorkflowUI { private: std::unique_ptr mWorkflow; - WorkflowDatabase* mWorkflowDb; ImNodes::EditorContext* mContext; @@ -152,7 +36,6 @@ public: WorkflowUI(std::unique_ptr workflow) : mWorkflow{ std::move(workflow) } { - mWorkflowDb = &WorkflowDatabase::GetInstance(); mContext = ImNodes::CreateEditor(); } @@ -325,25 +208,21 @@ public: } if (ImGui::BeginPopup("CreateNodeCtxMenu")) { - auto DisplayCandidatesCategory = [&](const char* name, std::span candidates) { - if (ImGui::BeginMenu(name)) { - for (auto& candidate : candidates) { - if (ImGui::MenuItem(candidate.Name.c_str())) { + for (int i = WorkflowNode::CG_Numeric; i < WorkflowNode::InvalidCategory; ++i) { + auto category = (WorkflowNode::Category)i; + auto members = WorkflowNode::QueryCategoryMembers(category); + + if (ImGui::BeginMenu(WorkflowNode::FormatCategory(category))) { + for (auto member : members) { + if (ImGui::MenuItem(WorkflowNode::FormatKind(member))) { // Create node - auto uptr = candidate.Constructor(); + auto uptr = WorkflowNode::CreateByKind(member); mWorkflow->AddNode(std::move(uptr)); } } ImGui::EndMenu(); } - }; - - DisplayCandidatesCategory(I18N_TEXT("Numeric", L10N_WORKFLOW_CATEGORY_NUMERIC), mWorkflowDb->GetNumericNodes()); - DisplayCandidatesCategory(I18N_TEXT("Text", L10N_WORKFLOW_CATEGORY_TEXT), mWorkflowDb->GetTextNodes()); - DisplayCandidatesCategory(I18N_TEXT("Document", L10N_WORKFLOW_CATEGORY_DOCUMENT), mWorkflowDb->GetDocumentNodes()); - DisplayCandidatesCategory(I18N_TEXT("User input", L10N_WORKFLOW_CATEGORY_USER_INPUT), mWorkflowDb->GetUserInputNodes()); - DisplayCandidatesCategory(I18N_TEXT("System input", L10N_WORKFLOW_CATEGORY_SYS_INPUT), mWorkflowDb->GetSystemInputNodes()); - DisplayCandidatesCategory(I18N_TEXT("Output", L10N_WORKFLOW_CATEGORY_OUTPUT), mWorkflowDb->GetOutputNodes()); + } ImGui::EndPopup(); } @@ -356,10 +235,6 @@ public: ImNodes::End(); } - - void DisplayNodeList() - { - } }; } // namespace -- cgit v1.2.3-70-g09d2