PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
interaction_sequence_ops.h
1#pragma once
2
3#include "pixelbullet/scene/interaction_sequence_types.h"
4
5#include <algorithm>
6#include <optional>
7#include <string>
8#include <string_view>
9#include <utility>
10
11namespace pixelbullet
12{
13inline void NormalizeInteractionSequenceEditorGraphLayout(InteractionSequenceEditorGraphLayout& layout)
14{
15 std::vector<InteractionSequenceEditorGraphNodeLayout> normalized;
16 normalized.reserve(layout.nodes.size());
17 for (InteractionSequenceEditorGraphNodeLayout& node : layout.nodes)
18 {
19 if (node.node_id.empty())
20 {
21 continue;
22 }
23
24 const auto existing = std::find_if(normalized.begin(), normalized.end(),
26 { return candidate.node_id == node.node_id; });
27 if (existing == normalized.end())
28 {
29 normalized.push_back(std::move(node));
30 }
31 else
32 {
33 *existing = std::move(node);
34 }
35 }
36
37 layout.nodes = std::move(normalized);
38}
39
40inline void NormalizeInteractionSequenceAsset(InteractionSequenceAsset& asset)
41{
42 if (asset.editor_graph)
43 {
44 NormalizeInteractionSequenceEditorGraphLayout(*asset.editor_graph);
45 if (asset.editor_graph->Empty())
46 {
47 asset.editor_graph.reset();
48 }
49 }
50
51 if (asset.initial_node_id.empty() && !asset.nodes.empty())
52 {
53 asset.initial_node_id = asset.nodes.front().id;
54 }
55}
56
57[[nodiscard]] inline std::optional<std::size_t> FindInteractionSequenceNodeIndex(const InteractionSequenceAsset& asset,
58 const std::string_view node_id)
59{
60 for (std::size_t index = 0; index < asset.nodes.size(); ++index)
61 {
62 if (asset.nodes[index].id == node_id)
63 {
64 return index;
65 }
66 }
67
68 return std::nullopt;
69}
70
71[[nodiscard]] inline const InteractionSequenceNode* FindInteractionSequenceNode(const InteractionSequenceAsset& asset,
72 const std::string_view node_id)
73{
74 const auto index = FindInteractionSequenceNodeIndex(asset, node_id);
75 return index ? &asset.nodes[*index] : nullptr;
76}
77
78[[nodiscard]] inline InteractionSequenceNode* FindInteractionSequenceNode(InteractionSequenceAsset& asset,
79 const std::string_view node_id)
80{
81 const auto index = FindInteractionSequenceNodeIndex(asset, node_id);
82 return index ? &asset.nodes[*index] : nullptr;
83}
84
85[[nodiscard]] inline const InteractionSequenceEditorGraphNodeLayout*
86FindInteractionSequenceEditorGraphNodeLayout(const InteractionSequenceAsset& asset, const std::string_view node_id)
87{
88 if (!asset.editor_graph)
89 {
90 return nullptr;
91 }
92
93 const auto it = std::find_if(asset.editor_graph->nodes.begin(), asset.editor_graph->nodes.end(),
94 [&](const InteractionSequenceEditorGraphNodeLayout& node) { return node.node_id == node_id; });
95 return it == asset.editor_graph->nodes.end() ? nullptr : &(*it);
96}
97
99FindInteractionSequenceEditorGraphNodeLayout(InteractionSequenceAsset& asset, const std::string_view node_id)
100{
101 if (!asset.editor_graph)
102 {
103 return nullptr;
104 }
105
106 const auto it = std::find_if(asset.editor_graph->nodes.begin(), asset.editor_graph->nodes.end(),
107 [&](const InteractionSequenceEditorGraphNodeLayout& node) { return node.node_id == node_id; });
108 return it == asset.editor_graph->nodes.end() ? nullptr : &(*it);
109}
110
111inline InteractionSequenceEditorGraphNodeLayout& EnsureInteractionSequenceEditorGraphNodeLayout(InteractionSequenceAsset& asset,
112 std::string node_id)
113{
114 if (!asset.editor_graph)
115 {
116 asset.editor_graph = InteractionSequenceEditorGraphLayout{};
117 }
118
119 if (InteractionSequenceEditorGraphNodeLayout* existing = FindInteractionSequenceEditorGraphNodeLayout(asset, node_id))
120 {
121 return *existing;
122 }
123
124 asset.editor_graph->nodes.push_back(InteractionSequenceEditorGraphNodeLayout{
125 .node_id = std::move(node_id),
126 .position = glm::vec2(0.0f),
127 });
128 return asset.editor_graph->nodes.back();
129}
130} // namespace pixelbullet
Definition interaction_sequence_types.h:54
Definition interaction_sequence_types.h:42
Definition interaction_sequence_types.h:34
Definition interaction_sequence_types.h:22