/* Copyright (c) 2018-2020, Arm Limited and Contributors * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 the "License"; * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include #include #include "scene_graph/components/light.h" #include "scene_graph/components/texture.h" namespace vkb { namespace sg { class Node; class Component; class SubMesh; /// @brief A collection of nodes organized in a tree structure. /// It can contain more than one root node. class Scene { public: Scene() = default; Scene(const std::string &name); void set_name(const std::string &name); const std::string &get_name() const; void set_nodes(std::vector> &&nodes); void add_node(std::unique_ptr &&node); void add_child(Node &child); std::unique_ptr get_model(uint32_t index = 0); void add_component(std::unique_ptr &&component); void add_component(std::unique_ptr &&component, Node &node); /** * @brief Set list of components for the given type * @param type_info The type of the component * @param components The list of components (retained) */ void set_components(const std::type_index &type_info, std::vector> &&components); /** * @brief Set list of components casted from the given template type */ template void set_components(std::vector> &&components) { std::vector> result(components.size()); std::transform(components.begin(), components.end(), result.begin(), [](std::unique_ptr &component) -> std::unique_ptr { return std::unique_ptr(std::move(component)); }); set_components(typeid(T), std::move(result)); } /** * @brief Clears a list of components */ template void clear_components() { set_components(typeid(T), {}); } /** * @return List of pointers to components casted to the given template type */ template std::vector get_components() const { std::vector result; if (has_component(typeid(T))) { auto &scene_components = get_components(typeid(T)); result.resize(scene_components.size()); std::transform(scene_components.begin(), scene_components.end(), result.begin(), [](const std::unique_ptr &component) -> T * { return dynamic_cast(component.get()); }); } return result; } /** * @return List of components for the given type */ const std::vector> &get_components(const std::type_index &type_info) const; template bool has_component() const { return has_component(typeid(T)); } bool has_component(const std::type_index &type_info) const; Node *find_node(const std::string &name); void set_root_node(Node &node); Node &get_root_node(); private: std::string name; /// List of all the nodes std::vector> nodes; Node *root{nullptr}; std::unordered_map>> components; }; } // namespace sg } // namespace vkb