TerraForge3D  2.3.1
3D Terrain And Landscape Generator
CurveNode.cpp
1#include "Generators/CPUNodeEditor/Nodes/CurveNode.h"
2#include "Base/ImGuiShapes.h"
3#include "Generators/CPUNodeEditor/CPUNodeEditor.h"
4#include <iostream>
5
6#include <mutex>
7#include "Base/ImGuiCurveEditor.h"
8
9// Temporary
10
11template <typename t>
12static void ReserveVector(std::vector<t> &vec, int amount)
13{
14 int size = vec.size();
15
16 for (int i = size; i <= amount; i++)
17 {
18 vec.push_back(t());
19 }
20}
21
22static const char *axises[3] = {"X", "Y", "Z"};
23
24NodeOutput CurveNode::Evaluate(NodeInputParam input, NodeEditorPin *pin)
25{
26 float result = 0;
27 float axisVal = 0;
28 float axisMax = 0;
29
30 switch (axis)
31 {
32 case 0 :
33 axisVal = input.x;
34 axisMax = input.maxX;
35 break;
36
37 case 1:
38 axisVal = input.y;
39 axisMax = input.maxY;
40 break;
41
42 case 2:
43 axisVal = input.z;
44 axisMax = input.maxZ;
45 break;
46
47 default:
48 break;
49 }
50
51 if (axisMax == 0)
52 {
53 axisMax = 1;
54 }
55
56 return NodeOutput({ ImGui::CurveValueSmooth((axisVal/axisMax), maxPoints, curve.data()) });
57}
58
59void CurveNode::Load(nlohmann::json data)
60{
61 maxPoints = data["maxPoints"];
62 ReserveVector(curve, (maxPoints > data["curveSize"].get<int>() ? maxPoints : data["curveSize"].get<int>()));
63
64 for (nlohmann::json tmp : data["curve"])
65 {
66 curve[tmp["index"]] = ImVec2(tmp["x"], tmp["y"]);
67 }
68
69 axis = data["axis"];
70}
71
72nlohmann::json CurveNode::Save()
73{
74 nlohmann::json data;
76 data["type"] = MeshNodeEditor::MeshNodeType::Curve;
77 int i = 0;
78
79 for (ImVec2 point : curve)
80 {
81 nlohmann::json tmp2;
82 tmp2["x"] = point.x;
83 tmp2["y"] = point.y;
84 tmp2["index"] = i++;
85 tmp.push_back(tmp2);
86 }
87
88 data["curve"] = tmp;
89 data["curveSize"] = curve.size();
90 data["maxPoints"] = maxPoints;
91 data["axis"] = axis;
92 return data;
93}
94
95void CurveNode::OnRender()
96{
97 DrawHeader("Curve Editor");
98 ImGui::Dummy(ImVec2(150, 10));
99 ImGui::SameLine();
100 ImGui::Text("Out");
101 outputPins[0]->Render();
102 ImGui::Text("Max Points");
103 ImGui::PushItemWidth(100);
104
105 if (ImGui::DragInt(("##dI" + std::to_string(id)).c_str(), &maxPoints, 1, 10, 256))
106 {
107 ReserveVector(curve, maxPoints);
108 }
109
110 ImGui::PopItemWidth();
111 ImGui::NewLine();
112
113 if (ImGui::Curve(("##" + std::to_string(id)).c_str(), ImVec2(200, 200), maxPoints, curve.data()))
114 {
115 }
116
117 ImGui::Text("Current Axis: ");
118 ImGui::SameLine();
119 ImGui::Text(axises[axis]);
120
121 if (ImGui::Button(("Change Axis##" + std::to_string(id)).c_str()))
122 {
123 axis++;
124
125 if (axis == 3)
126 {
127 axis = 0;
128 }
129 }
130}
131
132CurveNode::CurveNode()
133{
134 outputPins.push_back(new NodeEditorPin(NodeEditorPinType::Output));
135 headerColor = ImColor(OP_NODE_COLOR);
136 maxPoints = 10;
137 ReserveVector(curve, maxPoints);
138 curve[0].x = -1;
139 axis = 0;
140}
size_type size() const noexcept
returns the number of elements
Definition: json.hpp:22912
void push_back(basic_json &&val)
add an object to an array
Definition: json.hpp:23148
a class to store JSON values
Definition: json.hpp:17860