3737
3838#include " render_pipeline/rppanda/showbase/loader.hpp"
3939
40+ #include < nodePathCollection.h>
4041#include < audioManager.h>
4142#include < loader.h>
4243#include < audioLoadRequest.h>
4647#include < dynamicTextFont.h>
4748#include < texturePool.h>
4849#include < shaderPool.h>
50+ #include < modelFlattenRequest.h>
4951
5052#include < fmt/ostream.h>
5153
@@ -64,6 +66,12 @@ class Loader::Impl
6466 void pre_load_model (LoaderOptions& this_options, bool & this_ok_missing,
6567 boost::optional<bool > no_cache, bool allow_instance, boost::optional<bool > ok_missing);
6668
69+ /* *
70+ * The asynchronous flatten operation has completed; quietly
71+ * drop in the new models.
72+ */
73+ void async_flatten_done (const std::vector<NodePath>& models, const CallbackType& callback, std::vector<NodePath>& orig_model_list);
74+
6775 /* *
6876 * A model or sound file or some such thing has just been
6977 * loaded asynchronously by the sub-thread. Add it to the list
@@ -116,6 +124,26 @@ void Loader::Impl::pre_load_model(LoaderOptions& this_options, bool& this_ok_mis
116124 this_options.set_flags (this_options.get_flags () | LoaderOptions::LF_allow_instance);
117125}
118126
127+ void Loader::Impl::async_flatten_done (const std::vector<NodePath>& models, const CallbackType& callback, std::vector<NodePath>& orig_model_list)
128+ {
129+ rppanda_showbase_cat.debug () << " async_flatten_done" << std::endl;
130+
131+ if (models.size () != orig_model_list.size ())
132+ rppanda_showbase_cat.error () << " async_flatten_done: Size is not same." << std::endl;
133+
134+ for (size_t k = 0 , k_end = models.size (); k < k_end; ++k)
135+ {
136+ orig_model_list[k].get_children ().detach ();
137+ auto orig = orig_model_list[k].node ();
138+ auto flat = models[k].node ();
139+ orig->copy_all_properties (flat);
140+ flat->replace_node (orig);
141+ }
142+
143+ if (callback)
144+ callback (orig_model_list);
145+ }
146+
119147void Loader::Impl::got_async_object (const Event* ev)
120148{
121149 if (ev->get_num_parameters () != 1 )
@@ -152,8 +180,7 @@ void Loader::Impl::got_async_object(const Event* ev)
152180
153181// ************************************************************************************************
154182
155- Loader::Callback::Callback (Loader* loader, int num_objects,
156- const std::function<void (std::vector<NodePath>&)>& callback) : loader_(loader), callback_(callback)
183+ Loader::Callback::Callback (Loader* loader, int num_objects, const CallbackType& callback) : loader_(loader), callback_(callback)
157184{
158185 objects_.resize (num_objects);
159186}
@@ -265,17 +292,15 @@ std::vector<NodePath> Loader::load_model(const std::vector<Filename>& model_list
265292
266293std::shared_ptr<Loader::Callback> Loader::load_model_async (const Filename& model_path, const LoaderOptions& loader_options,
267294 boost::optional<bool > no_cache, bool allow_instance, boost::optional<bool > ok_missing,
268- const std::function<void (std::vector<NodePath>&)>& callback,
269- boost::optional<int> priority)
295+ const CallbackType& callback, boost::optional<int > priority)
270296{
271297 return load_model_async (std::vector<Filename>{model_path}, loader_options, no_cache,
272298 allow_instance, ok_missing, callback, priority);
273299}
274300
275301std::shared_ptr<Loader::Callback> Loader::load_model_async (const std::vector<Filename>& model_list, const LoaderOptions& loader_options,
276302 boost::optional<bool > no_cache, bool allow_instance, boost::optional<bool > ok_missing,
277- const std::function<void (std::vector<NodePath>&)>& callback,
278- boost::optional<int> priority)
303+ const CallbackType& callback, boost::optional<int > priority)
279304{
280305 rppanda_showbase_cat.debug () << " Loading model: " << join_to_string (model_list) << std::endl;
281306
@@ -654,4 +679,35 @@ void Loader::unload_shader(const Filename& shader_path)
654679 ShaderPool::release_shader (shader_path);
655680}
656681
682+ auto Loader::async_flatten_strong (NodePath model, const CallbackType& callback) -> std::shared_ptr<Callback>
683+ {
684+ return async_flatten_strong (std::vector<NodePath>{model}, callback);
685+ }
686+
687+ auto Loader::async_flatten_strong (const std::vector<NodePath>& model_list, const CallbackType& callback) -> std::shared_ptr<Callback>
688+ {
689+ auto cb = std::make_shared<Callback>(this , model_list.size (), callback);
690+
691+ size_t i = 0 ;
692+ for (const auto & model_path : model_list)
693+ {
694+ PT (ModelFlattenRequest) request = new ModelFlattenRequest (model_path.node ());
695+ request->set_done_event (impl_->hook_ );
696+ impl_->loader_ ->load_async (request);
697+ cb->requests_ .insert (request);
698+ cb->request_list_ .push_back (request);
699+ impl_->requests_ .insert ({ request.p (),{ cb, i } });
700+ i += 1 ;
701+ }
702+
703+ return cb;
704+ }
705+
706+ auto Loader::async_flatten_strong_in_place (std::vector<NodePath>& model_list, const CallbackType& callback) -> std::shared_ptr<Callback>
707+ {
708+ const auto & const_model_list = model_list;
709+ CallbackType new_callback = std::bind (&Impl::async_flatten_done, impl_.get (), std::placeholders::_1, callback, model_list);
710+ return async_flatten_strong (const_model_list, new_callback);
711+ }
712+
657713}
0 commit comments