-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathsave_load.cpp
206 lines (167 loc) · 5.72 KB
/
save_load.cpp
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
#include "node_editor.h"
#include <imnodes.h>
#include <imgui.h>
#include <SDL_keycode.h>
#include <algorithm>
#include <cassert>
#include <fstream>
#include <ios> // for std::streamsize
#include <stddef.h>
#include <vector>
namespace example
{
namespace
{
struct Node
{
int id;
float value;
Node() = default;
Node(const int i, const float v) : id(i), value(v) {}
};
struct Link
{
int id;
int start_attr, end_attr;
};
class SaveLoadEditor
{
public:
SaveLoadEditor() : nodes_(), links_(), current_id_(0) {}
void show()
{
ImGui::Begin("Save & load example");
ImGui::TextUnformatted("A -- add node");
ImGui::TextUnformatted(
"Close the executable and rerun it -- your nodes should be exactly "
"where you left them!");
ImNodes::BeginNodeEditor();
if (ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows) &&
ImNodes::IsEditorHovered() && ImGui::IsKeyReleased(ImGuiKey_A))
{
const int node_id = ++current_id_;
ImNodes::SetNodeScreenSpacePos(node_id, ImGui::GetMousePos());
nodes_.push_back(Node(node_id, 0.f));
}
for (Node& node : nodes_)
{
ImNodes::BeginNode(node.id);
ImNodes::BeginNodeTitleBar();
ImGui::TextUnformatted("node");
ImNodes::EndNodeTitleBar();
ImNodes::BeginInputAttribute(node.id << 8);
ImGui::TextUnformatted("input");
ImNodes::EndInputAttribute();
ImNodes::BeginStaticAttribute(node.id << 16);
ImGui::PushItemWidth(120.f);
ImGui::DragFloat("value", &node.value, 0.01f);
ImGui::PopItemWidth();
ImNodes::EndStaticAttribute();
ImNodes::BeginOutputAttribute(node.id << 24);
const float text_width = ImGui::CalcTextSize("output").x;
ImGui::Indent(120.f + ImGui::CalcTextSize("value").x - text_width);
ImGui::TextUnformatted("output");
ImNodes::EndOutputAttribute();
ImNodes::EndNode();
}
for (const Link& link : links_)
{
ImNodes::Link(link.id, link.start_attr, link.end_attr);
}
ImNodes::EndNodeEditor();
{
Link link;
if (ImNodes::IsLinkCreated(&link.start_attr, &link.end_attr))
{
link.id = ++current_id_;
links_.push_back(link);
}
}
{
int link_id;
if (ImNodes::IsLinkDestroyed(&link_id))
{
auto iter =
std::find_if(links_.begin(), links_.end(), [link_id](const Link& link) -> bool {
return link.id == link_id;
});
assert(iter != links_.end());
links_.erase(iter);
}
}
ImGui::End();
}
void save()
{
// Save the internal imnodes state
ImNodes::SaveCurrentEditorStateToIniFile("save_load.ini");
// Dump our editor state as bytes into a file
std::fstream fout(
"save_load.bytes", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
// copy the node vector to file
const size_t num_nodes = nodes_.size();
fout.write(
reinterpret_cast<const char*>(&num_nodes),
static_cast<std::streamsize>(sizeof(size_t)));
fout.write(
reinterpret_cast<const char*>(nodes_.data()),
static_cast<std::streamsize>(sizeof(Node) * num_nodes));
// copy the link vector to file
const size_t num_links = links_.size();
fout.write(
reinterpret_cast<const char*>(&num_links),
static_cast<std::streamsize>(sizeof(size_t)));
fout.write(
reinterpret_cast<const char*>(links_.data()),
static_cast<std::streamsize>(sizeof(Link) * num_links));
// copy the current_id to file
fout.write(
reinterpret_cast<const char*>(¤t_id_), static_cast<std::streamsize>(sizeof(int)));
}
void load()
{
// Load the internal imnodes state
ImNodes::LoadCurrentEditorStateFromIniFile("save_load.ini");
// Load our editor state into memory
std::fstream fin("save_load.bytes", std::ios_base::in | std::ios_base::binary);
if (!fin.is_open())
{
return;
}
// copy nodes into memory
size_t num_nodes;
fin.read(reinterpret_cast<char*>(&num_nodes), static_cast<std::streamsize>(sizeof(size_t)));
nodes_.resize(num_nodes);
fin.read(
reinterpret_cast<char*>(nodes_.data()),
static_cast<std::streamsize>(sizeof(Node) * num_nodes));
// copy links into memory
size_t num_links;
fin.read(reinterpret_cast<char*>(&num_links), static_cast<std::streamsize>(sizeof(size_t)));
links_.resize(num_links);
fin.read(
reinterpret_cast<char*>(links_.data()),
static_cast<std::streamsize>(sizeof(Link) * num_links));
// copy current_id into memory
fin.read(reinterpret_cast<char*>(¤t_id_), static_cast<std::streamsize>(sizeof(int)));
}
private:
std::vector<Node> nodes_;
std::vector<Link> links_;
int current_id_;
};
static SaveLoadEditor editor;
} // namespace
void NodeEditorInitialize()
{
ImNodes::GetIO().LinkDetachWithModifierClick.Modifier = &ImGui::GetIO().KeyCtrl;
ImNodes::PushAttributeFlag(ImNodesAttributeFlags_EnableLinkDetachWithDragClick);
editor.load();
}
void NodeEditorShow() { editor.show(); }
void NodeEditorShutdown()
{
ImNodes::PopAttributeFlag();
editor.save();
}
} // namespace example