Merge "Combine Internal::ProxyObject & Internal::Object" into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / modeling / model-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/modeling/model-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstdio>
23
24 // INTERNAL INCLUDES
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>
34
35 #include <dali/internal/event/resources/resource-client.h>
36 #include <dali/internal/update/resources/resource-manager.h>
37
38 namespace Dali
39 {
40
41 using Internal::ModelDataPtr;
42 using Integration::ModelResourceType;
43 using Integration::PlatformAbstraction;
44
45 namespace Internal
46 {
47
48 namespace
49 {
50
51 // Signals
52
53 const char* const SIGNAL_MODEL_LOADING_FINISHED = "model-loading-finished";
54 const char* const SIGNAL_MODEL_SAVING_FINISHED =  "model-saving-finished";
55
56 TypeRegistration mType( typeid( Dali::Model ), typeid( Dali::BaseHandle ), NULL );
57
58 SignalConnectorType signalConnector1( mType, SIGNAL_MODEL_LOADING_FINISHED, &Model::DoConnectSignal );
59 SignalConnectorType signalConnector2( mType, SIGNAL_MODEL_SAVING_FINISHED,  &Model::DoConnectSignal );
60
61 } // unnamed namespace
62
63 using Dali::Vector4;
64
65 ModelPtr Model::New(const std::string& name)
66 {
67   ModelPtr model(new Model(name));
68
69   model->RegisterObject();
70
71   return model;
72 }
73
74 Model::Model(const std::string& name)
75 {
76   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
77
78   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
79   mResourceClient = &tls.GetResourceClient();
80
81   ModelFactory& modelFactory = tls.GetModelFactory();
82
83   mTicket = modelFactory.Load(name);
84   mTicket->AddObserver(*this);
85 }
86
87 Model::~Model()
88 {
89   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
90   mTicket->RemoveObserver(*this);
91
92   UnregisterObject();
93 }
94
95 bool Model::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
96 {
97   bool connected( true );
98   Model* model = dynamic_cast<Model*>(object);
99
100   if( 0 == strcmp( signalName.c_str(), SIGNAL_MODEL_LOADING_FINISHED ) )
101   {
102     model->LoadingFinishedSignal().Connect( tracker, functor );
103   }
104   else if( 0 == strcmp( signalName.c_str(), SIGNAL_MODEL_SAVING_FINISHED ) )
105   {
106     model->SavingFinishedSignal().Connect( tracker, functor );
107   }
108   else
109   {
110     // signalName does not match any signal
111     connected = false;
112   }
113
114   return connected;
115 }
116
117 void Model::ResourceLoadingFailed(const ResourceTicket& ticket)
118 {
119   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
120
121   mLoadingFinished.Emit( Dali::Model( this ) );
122 }
123
124 void Model::ResourceLoadingSucceeded(const ResourceTicket& ticket)
125 {
126   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
127
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 );
133
134   mLoadingFinished.Emit( Dali::Model( this ) );
135 }
136
137 void Model::ResourceUploaded(const ResourceTicket& ticket)
138 {
139   // do nothing - not a GL resource
140 }
141
142 void Model::ResourceSavingFailed(const ResourceTicket& ticket)
143 {
144   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
145
146   mSavingFinished.Emit( Dali::Model( this ), false );
147 }
148
149 void Model::ResourceSavingSucceeded(const ResourceTicket& ticket)
150 {
151   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
152
153   mSavingFinished.Emit( Dali::Model( this ), true );
154 }
155
156 ResourceId Model::GetResourceId() const
157 {
158   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
159   DALI_ASSERT_DEBUG(mTicket);
160
161   return mTicket->GetId();
162 }
163
164 /********************************************************************************
165  * Write the model data to standard output
166  */
167 void Model::Write()
168 {
169   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
170   ResourceManager& resourceManager = tls.GetResourceManager();
171   ModelDataPtr modelData = resourceManager.GetModelData(GetResourceId());
172
173   if(modelData != 0)
174   {
175     ModelLogger modelLogger(*(modelData.Get()));
176     modelLogger.mWriteVertices = false; // TODO - add a public traits class for model logger
177     modelLogger.Write();
178   }
179   else
180   {
181 #ifndef EMSCRIPTEN
182     fprintf(stderr, "%s: Model not loaded yet\n", __PRETTY_FUNCTION__);
183 #else
184     // printf to stderr doesnt always show in browser console window
185     printf("%s: Model not loaded yet\n", __PRETTY_FUNCTION__);
186 #endif
187   }
188 }
189
190 /********************************************************************************
191  * Write the model data to a url
192  */
193 void Model::Save(const std::string& url)
194 {
195   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
196   ResourceManager& resourceManager = tls.GetResourceManager();
197   if( resourceManager.GetModelData(GetResourceId()) )
198   {
199     mResourceClient->SaveResource(mTicket, url);
200   }
201 }
202
203 /********************************************************************************
204  *
205  */
206 unsigned int Model::NumberOfAnimations() const
207 {
208   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
209
210   unsigned int numAnimations = 0;
211   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
212   ResourceManager& resourceManager = tls.GetResourceManager();
213   ModelDataPtr modelData = resourceManager.GetModelData(GetResourceId());
214
215   if (modelData)
216   {
217     numAnimations = modelData->NumberOfAnimationMaps();
218   }
219
220   return numAnimations;
221 }
222
223 bool Model::FindAnimation(const std::string& animationName, unsigned int & animationIndex)
224 {
225   bool found = false;
226
227   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
228   ResourceManager& resourceManager = tls.GetResourceManager();
229   ModelDataPtr modelData = resourceManager.GetModelData(GetResourceId());
230
231   if (modelData)
232   {
233     found = modelData->FindAnimation(animationName, animationIndex);
234   }
235   return found;
236 }
237
238 /********************************************************************************
239  * Return the model data, if it's finished loading.
240  */
241 ModelDataPtr Model::GetModelData() const
242 {
243   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
244
245   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
246   ResourceManager& resourceManager = tls.GetResourceManager();
247   ModelDataPtr modelData = resourceManager.GetModelData(GetResourceId());
248   return modelData;
249 }
250
251 } // namespace Internal
252 } // namespace Dali