2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/event/modeling/model-impl.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/integration-api/resource-cache.h>
27 #include <dali/integration-api/platform-abstraction.h>
28 #include <dali/internal/event/common/thread-local-storage.h>
29 #include <dali/internal/event/modeling/model-factory.h>
30 #include <dali/internal/event/modeling/model-logger.h>
31 #include <dali/internal/event/modeling/model-data-impl.h>
32 #include <dali/public-api/modeling/model-animation-map.h>
33 #include <dali/public-api/object/type-registry.h>
35 #include <dali/internal/event/resources/resource-client.h>
36 #include <dali/internal/update/resources/resource-manager.h>
41 using Internal::ModelDataPtr;
42 using Integration::ModelResourceType;
43 using Integration::PlatformAbstraction;
53 const char* const SIGNAL_MODEL_LOADING_FINISHED = "model-loading-finished";
54 const char* const SIGNAL_MODEL_SAVING_FINISHED = "model-saving-finished";
56 TypeRegistration mType( typeid( Dali::Model ), typeid( Dali::BaseHandle ), NULL );
58 SignalConnectorType signalConnector1( mType, SIGNAL_MODEL_LOADING_FINISHED, &Model::DoConnectSignal );
59 SignalConnectorType signalConnector2( mType, SIGNAL_MODEL_SAVING_FINISHED, &Model::DoConnectSignal );
61 } // unnamed namespace
65 ModelPtr Model::New(const std::string& name)
67 ModelPtr model(new Model(name));
69 model->RegisterObject();
74 Model::Model(const std::string& name)
76 DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
78 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
79 mResourceClient = &tls.GetResourceClient();
81 ModelFactory& modelFactory = tls.GetModelFactory();
83 mTicket = modelFactory.Load(name);
84 mTicket->AddObserver(*this);
89 DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
90 mTicket->RemoveObserver(*this);
95 bool Model::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
97 bool connected( true );
98 Model* model = dynamic_cast<Model*>(object);
100 if( 0 == strcmp( signalName.c_str(), SIGNAL_MODEL_LOADING_FINISHED ) )
102 model->LoadingFinishedSignal().Connect( tracker, functor );
104 else if( 0 == strcmp( signalName.c_str(), SIGNAL_MODEL_SAVING_FINISHED ) )
106 model->SavingFinishedSignal().Connect( tracker, functor );
110 // signalName does not match any signal
117 void Model::ResourceLoadingFailed(const ResourceTicket& ticket)
119 DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
121 mLoadingFinished.Emit( Dali::Model( this ) );
124 void Model::ResourceLoadingSucceeded(const ResourceTicket& ticket)
126 DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
128 // Generate resource tickets for meshes
129 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
130 ResourceManager& resourceManager = tls.GetResourceManager();
131 ModelDataPtr modelData = resourceManager.GetModelData( GetResourceId() );
132 modelData->Unpack( *mResourceClient );
134 mLoadingFinished.Emit( Dali::Model( this ) );
137 void Model::ResourceUploaded(const ResourceTicket& ticket)
139 // do nothing - not a GL resource
142 void Model::ResourceSavingFailed(const ResourceTicket& ticket)
144 DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
146 mSavingFinished.Emit( Dali::Model( this ), false );
149 void Model::ResourceSavingSucceeded(const ResourceTicket& ticket)
151 DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
153 mSavingFinished.Emit( Dali::Model( this ), true );
156 ResourceId Model::GetResourceId() const
158 DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
159 DALI_ASSERT_DEBUG(mTicket);
161 return mTicket->GetId();
164 /********************************************************************************
165 * Write the model data to standard output
169 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
170 ResourceManager& resourceManager = tls.GetResourceManager();
171 ModelDataPtr modelData = resourceManager.GetModelData(GetResourceId());
175 ModelLogger modelLogger(*(modelData.Get()));
176 modelLogger.mWriteVertices = false; // TODO - add a public traits class for model logger
182 fprintf(stderr, "%s: Model not loaded yet\n", __PRETTY_FUNCTION__);
184 // printf to stderr doesnt always show in browser console window
185 printf("%s: Model not loaded yet\n", __PRETTY_FUNCTION__);
190 /********************************************************************************
191 * Write the model data to a url
193 void Model::Save(const std::string& url)
195 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
196 ResourceManager& resourceManager = tls.GetResourceManager();
197 if( resourceManager.GetModelData(GetResourceId()) )
199 mResourceClient->SaveResource(mTicket, url);
203 /********************************************************************************
206 unsigned int Model::NumberOfAnimations() const
208 DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
210 unsigned int numAnimations = 0;
211 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
212 ResourceManager& resourceManager = tls.GetResourceManager();
213 ModelDataPtr modelData = resourceManager.GetModelData(GetResourceId());
217 numAnimations = modelData->NumberOfAnimationMaps();
220 return numAnimations;
223 bool Model::FindAnimation(const std::string& animationName, unsigned int & animationIndex)
227 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
228 ResourceManager& resourceManager = tls.GetResourceManager();
229 ModelDataPtr modelData = resourceManager.GetModelData(GetResourceId());
233 found = modelData->FindAnimation(animationName, animationIndex);
238 /********************************************************************************
239 * Return the model data, if it's finished loading.
241 ModelDataPtr Model::GetModelData() const
243 DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
245 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
246 ResourceManager& resourceManager = tls.GetResourceManager();
247 ModelDataPtr modelData = resourceManager.GetModelData(GetResourceId());
251 } // namespace Internal