aboutsummaryrefslogtreecommitdiff
path: root/core/src/Model/Workflow/Workflow.hpp
blob: 3dc6f3879cedc280251671e0db61ab7dd46674cd (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#pragma once

#include "Model/Assets.hpp"
#include "Model/Workflow/Value.hpp"
#include "Utils/Vector.hpp"
#include "cplt_fwd.hpp"

#include <imgui_node_editor.h>
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <functional>
#include <iosfwd>
#include <limits>
#include <memory>
#include <string>
#include <variant>
#include <vector>

namespace ImNodes = ax::NodeEditor;

class WorkflowConnection
{
public:
	static constexpr auto kInvalidId = std::numeric_limits<uint32_t>::max();

	uint32_t Id;
	uint32_t SourceNode;
	uint32_t SourcePin;
	uint32_t DestinationNode;
	uint32_t DestinationPin;

public:
	WorkflowConnection();

	bool IsValid() const;

	/// Used for `LinkId` when interfacing with imgui node editor. Runtime only (not saved to disk and generated when loading).
	ImNodes::LinkId GetLinkId() const;

	void DrawDebugInfo() const;
	void ReadFrom(std::istream& stream);
	void WriteTo(std::ostream& stream);
};

class WorkflowNode
{
public:
	static constexpr auto kInvalidId = std::numeric_limits<uint32_t>::max();
	static constexpr auto kInvalidPinId = std::numeric_limits<uint32_t>::max();

	enum Type
	{
		InputType,
		TransformType,
		OutputType,
	};

	enum Kind
	{
		KD_NumericAddition,
		KD_NumericSubtraction,
		KD_NumericMultiplication,
		KD_NumericDivision,
		KD_NumericExpression,
		KD_TextFormatting,
		KD_DocumentTemplateExpansion,
		KD_FormInput,
		KD_DatabaseRowsInput,

		InvalidKind,
		KindCount = InvalidKind,
	};

	struct InputPin
	{
		uint32_t Connection = WorkflowConnection::kInvalidId;
		BaseValue::Kind MatchingType = BaseValue::InvalidKind;
		bool ConnectionToConst = false;

		/// A constant connection connects from a user-specified constant value, feeding to a valid \c DestinationNode and \c DestinationPin (i.e. input pins).
		bool IsConstantConnection() const;
		bool IsConnected() const;
		BaseValue::Kind GetMatchingType() const;
	};

	struct OutputPin
	{
		uint32_t Connection = WorkflowConnection::kInvalidId;
		BaseValue::Kind MatchingType = BaseValue::InvalidKind;

		bool IsConnected() const;
		BaseValue::Kind GetMatchingType() const;
	};

protected:
	friend class Workflow;
	friend class WorkflowEvaluationContext;

	Workflow* mWorkflow;
	std::vector<InputPin> mInputs;
	std::vector<OutputPin> mOutputs;
	Vec2i mPosition;
	uint32_t mId;
	Kind mKind;
	int mDepth;
	bool mLocked;

public:
	static const char* FormatKind(Kind kind);
	static const char* FormatType(Type type);
	static std::unique_ptr<WorkflowNode> CreateByKind(Kind kind);

	static bool IsInstance(const WorkflowNode* node);

	WorkflowNode(Kind kind, bool locked);
	virtual ~WorkflowNode() = default;

	WorkflowNode(const WorkflowNode&) = delete;
	WorkflowNode& operator=(const WorkflowNode&) = delete;
	WorkflowNode(WorkflowNode&&) = default;
	WorkflowNode& operator=(WorkflowNode&&) = default;

	void SetPosition(const Vec2i& position);
	Vec2i GetPosition() const;

	uint32_t GetId() const;
	/// Used for `NodeId` when interfacing with imgui node editor. Runtime only (not saved to disk and generated when loading).
	ImNodes::NodeId GetNodeId() const;
	Kind GetKind() const;
	int GetDepth() const;
	bool IsLocked() const;

	Type GetType() const;
	bool IsInputNode() const;
	bool IsOutputNode() const;

	void ConnectInput(uint32_t pinId, WorkflowNode& srcNode, uint32_t srcPinId);
	void DisconnectInput(uint32_t pinId);

	void DrawInputPinDebugInfo(uint32_t pinId) const;
	const InputPin& GetInputPin(uint32_t pinId) const;
	ImNodes::PinId GetInputPinUniqueId(uint32_t pinId) const;

	void ConnectOutput(uint32_t pinId, WorkflowNode& dstNode, uint32_t dstPinId);
	void DisconnectOutput(uint32_t pinId);

	void DrawOutputPinDebugInfo(uint32_t pinId) const;
	const OutputPin& GetOutputPin(uint32_t pinId) const;
	ImNodes::PinId GetOutputPinUniqueId(uint32_t pinId) const;

	virtual void Evaluate(WorkflowEvaluationContext& ctx) = 0;

	void Draw();
	virtual void DrawExtra() {}

	void DrawDebugInfo() const;
	virtual void DrawExtraDebugInfo() const {}

	virtual void ReadFrom(std::istream& istream);
	virtual void WriteTo(std::ostream& ostream);

protected:
	InputPin& InsertInputPin(int atIdx);
	void RemoveInputPin(int pin);
	void SwapInputPin(int a, int b);
	OutputPin& InsertOutputPin(int atIdx);
	void RemoveOutputPin(int pin);
	void SwapOutputPin(int a, int b);

	/* For \c Workflow to invoke, override by implementations */

	void OnAttach(Workflow& workflow, uint32_t newId);
	void OnDetach();
};

class Workflow : public Asset
{
public:
	using CategoryType = WorkflowAssetList;
	static constinit const WorkflowAssetList Category;

private:
	friend class WorkflowNode;
	friend class WorkflowEvaluationContext;

	std::vector<WorkflowConnection> mConnections;
	std::vector<std::unique_ptr<WorkflowNode>> mNodes;
	std::vector<std::unique_ptr<BaseValue>> mConstants;
	std::vector<std::vector<uint32_t>> mDepthGroups;
	int mConnectionCount;
	int mNodeCount;
	int mConstantCount;
	bool mDepthsDirty = true;

public:
	/* Graph access */

	const std::vector<WorkflowConnection>& GetConnections() const;
	std::vector<WorkflowConnection>& GetConnections();
	const std::vector<std::unique_ptr<WorkflowNode>>& GetNodes() const;
	std::vector<std::unique_ptr<WorkflowNode>>& GetNodes();
	const std::vector<std::unique_ptr<BaseValue>>& GetConstants() const;
	std::vector<std::unique_ptr<BaseValue>>& GetConstants();

	WorkflowConnection* GetConnectionById(uint32_t id);
	WorkflowConnection* GetConnectionByLinkId(ImNodes::LinkId linkId);
	WorkflowNode* GetNodeById(uint32_t id);
	WorkflowNode* GetNodeByNodeId(ImNodes::NodeId nodeId);
	BaseValue* GetConstantById(uint32_t id);

	struct GlobalPinId
	{
		WorkflowNode* Node;
		uint32_t PinId;
		/// true => input pin
		/// false => output pin
		bool IsOutput;
	};

	/// `pinId` should be the `UniqueId` of a pin from a node that's within this workflow.
	GlobalPinId DisassembleGlobalPinId(ImNodes::PinId id);
	ImNodes::PinId FabricateGlobalPinId(const WorkflowNode& node, uint32_t pinId, bool isOutput) const;

	const std::vector<std::vector<uint32_t>>& GetDepthGroups() const;
	bool DoesDepthNeedsUpdate() const;

	/* Graph mutation */

	void AddNode(std::unique_ptr<WorkflowNode> step);
	void RemoveNode(uint32_t id);

	void RemoveConnection(uint32_t id);

	bool Connect(WorkflowNode& sourceNode, uint32_t sourcePin, WorkflowNode& destinationNode, uint32_t destinationPin);
	bool DisconnectBySource(WorkflowNode& sourceNode, uint32_t sourcePin);
	bool DisconnectByDestination(WorkflowNode& destinationNode, uint32_t destinationPin);

	/* Graph rebuild */

	enum GraphUpdateResult
	{
		/// Successfully rebuilt graph dependent data.
		/// Details: nothing is written.
		GUR_Success,
		/// Nothing has changed since last time UpdateGraph() was called.
		/// Details: nothing is written.
		GUR_NoWorkToDo,
		/// Details: list of nodes is written.
		GUR_UnsatisfiedDependencies,
		/// Details: list of nodes is written.
		GUR_UnreachableNodes,
	};

	using GraphUpdateDetails = std::variant<
		// Case: nothing
		std::monostate,
		// Case: list of nodes (ids)
		std::vector<uint32_t>>;

	GraphUpdateResult UpdateGraph(GraphUpdateDetails* details = nullptr);

	/* Serialization */

	enum ReadResult
	{
		RR_Success,
		RR_InvalidVersion,
	};
	ReadResult ReadFrom(std::istream& stream);
	void WriteTo(std::ostream& stream);

private:
	std::pair<WorkflowConnection&, uint32_t> AllocWorkflowConnection();
	std::pair<std::unique_ptr<WorkflowNode>&, uint32_t> AllocWorkflowStep();
};

class WorkflowAssetList final : public AssetListTyped<Workflow>
{
private:
	// AC = Asset Creator
	std::string mACNewName;
	NameSelectionError mACNewNameError = NameSelectionError::Empty;

public:
	virtual void DiscoverFiles(const std::function<void(SavedAsset)>& callback) const override;

	virtual std::string RetrieveNameFromFile(const std::filesystem::path& file) const override;
	virtual uuids::uuid RetrieveUuidFromFile(const std::filesystem::path& file) const override;
	virtual std::filesystem::path RetrievePathFromAsset(const SavedAsset& asset) const override;

	virtual void SaveEmptyInstance(const SavedAsset& asset) const override;
	virtual Workflow* CreateEmptyInstance(const SavedAsset& diskForm) const override;

	virtual Workflow* LoadImpl(const SavedAsset& diskForm) const override;

	virtual void DisplayAssetCreator(ListState& state) override;

	virtual void SetupDetailsTable(const char* tableId) const override;
	virtual void DrawBigIcon(ListState& state, const SavedAsset& asset) const override;
	virtual void DrawDetailsTableRow(ListState& state, const SavedAsset& asset) const override;
};