2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://floralicense.org/license/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 * @file FUiEffects_RuntimeEffectModel.cpp
19 * @brief This file contains an implementation of EffectModel class methods
24 #include <FBaseSysLog.h>
25 #include <FBaseErrors.h>
26 #include "../FUiEffects_EffectErrorMessages.h"
27 #include <FUiEffectsTypes.h>
28 #include <FUiEffects_LoggingProfiler.h>
29 #include "FUiEffects_RuntimeEffectModel.h"
30 #include "FUiEffects_RuntimeModelSurface.h"
31 #include "FUiEffects_RuntimeGraphicalSurface.h"
32 #include "FUiEffects_RuntimeRenderDataScene.h"
34 using namespace Tizen::Ui::Effects;
35 using namespace Tizen::Ui::Effects::_PhysicsEngine;
36 using namespace Tizen::Ui::Effects::_Utils;
39 namespace Tizen { namespace Ui { namespace Effects { namespace _Runtime
43 ModelSurfacesCollectionDeleter::operator()(TypeMSurfacesCollection* pMSurfacesCollection)
45 TypeMSurfacesCollection::iterator it;
46 for (it = pMSurfacesCollection->begin(); it != pMSurfacesCollection->end(); ++it)
50 delete pMSurfacesCollection;
53 EffectModel::EffectModel(long effectID_,
54 const std::string& name,
55 const char* pPathToScript,
56 IEffectModelListener* pEMListener,
57 EffectType effectType_,
58 TypeTimeEffect effectDuration_)
60 , __pEMListener(pEMListener)
61 , __state(_EFFECT_STATE_INITIALIZING)
62 , __effectType(effectType_)
64 , __pMapElements(null)
65 , __pMapGSurface(null)
66 , __pMSurfacesCollection(null)
67 , __effectID(effectID_)
69 , __effectTimePassed(0)
70 , __effectDuration(effectDuration_)
71 , __pRenderDataScene(null)
73 __state = _EFFECT_STATE_RUNTIME_ERROR;
75 unique_ptr<ScriptProcessing> pScript(new (std::nothrow) ScriptProcessing(pPathToScript, this));
76 SysTryReturnVoidResult(NID_UI_EFFECT, pScript.get() != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
77 result res = GetLastResult();
78 SysTryReturnVoidResult(NID_UI_EFFECT, res == E_SUCCESS, res, GetErrorMessage(res));
80 unique_ptr<TypeMSurfacesCollection, ModelSurfacesCollectionDeleter> pMSurfacesCollection(new (std::nothrow) TypeMSurfacesCollection, ModelSurfacesCollectionDeleter());
81 SysTryReturnVoidResult(NID_UI_EFFECT, pMSurfacesCollection.get() != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
83 unique_ptr<TypeMapElements> pMapElements(new (std::nothrow) TypeMapElements);
84 SysTryReturnVoidResult(NID_UI_EFFECT, pMapElements.get() != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
86 unique_ptr<TypeMapGSurface> pMapGSurface(new (std::nothrow) TypeMapGSurface);
87 SysTryReturnVoidResult(NID_UI_EFFECT, pMapGSurface.get() != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
89 unique_ptr<RenderDataScene> pRenderDataScene(new (std::nothrow) RenderDataScene());
90 SysTryReturnVoidResult(NID_UI_EFFECT, pRenderDataScene.get() != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
92 __state = _EFFECT_STATE_INITIALIZING;
94 __pScript = std::move(pScript);
95 __pMSurfacesCollection = std::move(pMSurfacesCollection);
96 __pMapElements = std::move(pMapElements);
97 __pMapGSurface = std::move(pMapGSurface);
98 __pRenderDataScene = std::move(pRenderDataScene);
100 if (pEMListener == null)
102 SysLogException(NID_UI_EFFECT, E_SYSTEM, "The pointer to EffectModelListener is null!");
103 __state = _EFFECT_STATE_RUNTIME_ERROR;
107 EffectModel::~EffectModel(void)
113 EffectModel::AddModelSurface(ModelSurface* pMSurface)
115 if (__state == _EFFECT_STATE_INITIALIZING)
117 __pMSurfacesCollection->push_back(pMSurface);
123 EffectModel::EffectInitialize(void)
125 SysAssertf(__state == _EFFECT_STATE_FINISHING || __state == _EFFECT_STATE_INITIALIZING, _UiEffectError::INTERNAL_ERROR);
127 __state = _EFFECT_STATE_INITIALIZING;
129 TypeMSurfacesCollection::iterator it;
130 for (it = __pMSurfacesCollection->begin(); it != __pMSurfacesCollection->end(); ++it)
140 EffectModel::Start(const EffectsVector<float>& effectStartInfo)
142 if (__state != _EFFECT_STATE_RUNNABLE)
147 __state = _EFFECT_STATE_STARTING;
149 START_PROFILER(EFFLOG_SCRIPT)
150 if (!__pScript->OnEffectStart(effectStartInfo))
155 END_PROFILER(EFFLOG_SCRIPT)
157 //requesting a bitmap for each graphical surface to effect impl
158 RenderDataSurfaceCollection::const_iterator it;
159 for (it = __pRenderDataScene->GetRenderDataSurfaceCollection().begin(); it != __pRenderDataScene->GetRenderDataSurfaceCollection().end(); ++it)
161 START_PROFILER(EFFLOG_BITMAP)
162 if (!__pEMListener->OnBitmapRequested((*it)->bitmapId))
167 END_PROFILER(EFFLOG_BITMAP)
170 //sending event to effect impl about effect started
171 PROFILER_EXPR(__pEMListener->OnEffectStarted();, EFFLOG_OnEffectStarted);
173 __state = _EFFECT_STATE_RUNNING;
178 EffectModel::Stop(bool isBreakdown, bool isInterruptedFromApplication)
182 if (__state != _EFFECT_STATE_RUNNING)
187 __state = _EFFECT_STATE_FINISHING;
188 if (isInterruptedFromApplication)
190 __effectResult.effectResult = EFFECT_RESULT_INTERRUPTED;
191 __effectResult.lastShownBitmapsId = LastShownBitmapIdCollection(1, -1);
193 __pEMListener->OnEffectFinished(__effectResult.effectResult, __effectResult.lastShownBitmapsId);
198 __state = _EFFECT_STATE_RUNTIME_ERROR;
199 __effectResult.effectResult = EFFECT_RESULT_ERROR;
200 __effectResult.lastShownBitmapsId = LastShownBitmapIdCollection(1, -1);
201 __pEMListener->OnEffectFinished(__effectResult.effectResult, __effectResult.lastShownBitmapsId);
207 EffectModel::Calculate(float dt)
209 if (__state != _EFFECT_STATE_RUNNING)
214 __effectTimePassed += dt;
215 if (__effectTimePassed > __effectDuration && __effectDuration > std::numeric_limits<TypeTimeEffect>::epsilon())
217 __effectTimePassed = __effectDuration;
220 START_PROFILER_AV(Calculate_calc_script, 50);
221 if (!__pScript->OnEffectCalculate(dt))
226 END_PROFILER_AV(Calculate_calc_script);
228 TypeMSurfacesCollection::iterator it;
229 START_PROFILER_AV(Calculate_calc_model, 50);
230 for (it = __pMSurfacesCollection->begin(); it != __pMSurfacesCollection->end(); ++it)
232 //physics calculations
233 (*it)->Calculate(dt);
235 END_PROFILER_AV(Calculate_calc_model);
237 if (__effectType == EFFECT_TYPE_INTERACTIVE)
239 __effectResult = __pScript->IsEffectFinished();
241 else if (__effectTimePassed >= __effectDuration && __effectDuration > std::numeric_limits<TypeTimeEffect>::epsilon())
243 __effectResult.effectResult = EFFECT_RESULT_FINISHED;
244 __effectResult.lastShownBitmapsId = LastShownBitmapIdCollection(1, 1);
247 switch (__effectResult.effectResult)
249 case EFFECT_RESULT_FINISHED:
252 case EFFECT_RESULT_ERROR:
262 EffectModel::FeedTouchPressEvent(const TouchEventScript &pos3)
264 SysTryReturnVoidResult(NID_UI_EFFECT, __state == _EFFECT_STATE_RUNNING, E_INVALID_STATE, _UiEffectError::FEED_TOUCH_IS_BLOCKED, __effectID, __name.c_str());
265 SysTryReturnVoidResult(NID_UI_EFFECT, __effectType == EFFECT_TYPE_INTERACTIVE, E_OPERATION_FAILED, _UiEffectError::FEED_TOUCH_IS_BLOCKED, __effectID, __name.c_str());
267 if (!__pScript->OnTouchPressed(pos3))
270 SysLogException(NID_UI_EFFECT, E_OPERATION_FAILED, "Runtime error in OnEffectsTouchPressed Script for Effect with Id = %i and name \"%s\" is appeared. Effect is blocked!", __effectID, __name.c_str());
276 EffectModel::FeedTouchMoveEvent(const TouchEventScript &pos3)
278 SysTryReturnVoidResult(NID_UI_EFFECT, __state == _EFFECT_STATE_RUNNING, E_INVALID_STATE, _UiEffectError::FEED_TOUCH_IS_BLOCKED, __effectID, __name.c_str());
279 SysTryReturnVoidResult(NID_UI_EFFECT, __effectType == EFFECT_TYPE_INTERACTIVE, E_OPERATION_FAILED, _UiEffectError::FEED_TOUCH_IS_BLOCKED, __effectID, __name.c_str());
281 if (!__pScript->OnTouchMoved(pos3))
284 SysLogException(NID_UI_EFFECT, E_OPERATION_FAILED, "Runtime error in OnEffectsTouchMoved Script for Effect with Id = %i and name \"%s\" is appeared. Effect is blocked!", __effectID, __name.c_str());
290 EffectModel::FeedTouchReleaseEvent(const TouchEventScript &pos3)
292 SysTryReturnVoidResult(NID_UI_EFFECT, __state == _EFFECT_STATE_RUNNING, E_INVALID_STATE, _UiEffectError::FEED_TOUCH_IS_BLOCKED, __effectID, __name.c_str());
293 SysTryReturnVoidResult(NID_UI_EFFECT, __effectType == EFFECT_TYPE_INTERACTIVE, E_OPERATION_FAILED, _UiEffectError::FEED_TOUCH_IS_BLOCKED, __effectID, __name.c_str());
295 if (!__pScript->OnTouchReleased(pos3))
298 SysLogException(NID_UI, E_OPERATION_FAILED, "Runtime error in OnEffectsTouchReleased Script for Effect with Id = %i and name \"%s\" is appeared. Effect is blocked!", __effectID, __name.c_str());
304 EffectModel::FeedTouchDoublePressEvent(const TouchEventScript &pos3)
306 SysTryReturnVoidResult(NID_UI_EFFECT, __state == _EFFECT_STATE_RUNNING, E_INVALID_STATE, _UiEffectError::FEED_TOUCH_IS_BLOCKED, __effectID, __name.c_str());
307 SysTryReturnVoidResult(NID_UI_EFFECT, __effectType == EFFECT_TYPE_INTERACTIVE, E_OPERATION_FAILED, _UiEffectError::FEED_TOUCH_IS_BLOCKED, __effectID, __name.c_str());
309 if (!__pScript->OnTouchDoublePressed(pos3))
312 SysLogException(NID_UI_EFFECT, E_OPERATION_FAILED, "Runtime error in OnEffectsTouchDoublePressed Script for Effect with Id = %i and name \"%s\" is appeared. Effect is blocked!", __effectID, __name.c_str());
318 EffectModel::GetCalculationPeriodForTimer(void)const
320 return __effectTimePassed;
324 EffectModel::GetState(void)const
330 EffectModel::GetType(void)const
336 EffectModel::ScriptSetProperty(long objID, ElementProperty property, bool value)
338 TypeMapElements::iterator it;
339 if (!FindElement(objID, it))
343 return it->second->SetProperty(property, value);
347 EffectModel::ScriptSetProperty(long objID, ElementProperty property, LUA_NUMBER value)
354 case N_EFFECT_DURATION:
355 __effectDuration = value;
364 TypeMapElements::iterator it;
365 if (!FindElement(objID, it))
369 return it->second->SetProperty(property, value);
373 EffectModel::ScriptSetProperty(long objID, ElementProperty property, const Vec3f &value)
375 TypeMapElements::iterator it;
376 if (!FindElement(objID, it))
380 return it->second->SetProperty(property, value);
384 EffectModel::ScriptSetPropertyGroup(long modelSurfaceID, GroupElements group, ElementProperty property, LUA_NUMBER value)
386 TypeMSurfacesCollection::iterator it;
387 for (it = __pMSurfacesCollection->begin(); it != __pMSurfacesCollection->end(); ++it)
389 if ((*it)->GetId() == modelSurfaceID)
391 return (*it)->SetPropertyGroup(group, property, value);
398 EffectModel::ScriptGetDistanceFromGround(long modelSurfaceID) const
400 PropertyCast propCast = {PropertyCast::NO_PROPERTY, {0} };
401 TypeMSurfacesCollection::iterator it;
402 for (it = __pMSurfacesCollection->begin(); it != __pMSurfacesCollection->end(); ++it)
404 if ((*it)->GetId() == modelSurfaceID)
406 propCast.type = PropertyCast::NUMBER;
407 propCast.value.numberValue = (*it)->GetDistanceFromGround();
411 propCast.type = PropertyCast::NO_PROPERTY;
416 EffectModel::ScriptGetProperty(long objID, ElementProperty property) const
420 PropertyCast propCast = {PropertyCast::NO_PROPERTY, {0}};
423 case N_EFFECT_TIME_PASSED:
424 propCast.type = PropertyCast::NUMBER;
425 propCast.value.numberValue = __effectTimePassed;
427 case N_EFFECT_DURATION:
428 propCast.type = PropertyCast::NUMBER;
429 propCast.value.numberValue = __effectDuration;
432 propCast.type = PropertyCast::NO_PROPERTY;
438 TypeMapElements::iterator it;
439 if (!FindElement(objID, it))
441 PropertyCast propCast = {PropertyCast::NO_PROPERTY, {0} };
442 propCast.type = PropertyCast::NO_PROPERTY;
447 return it->second->GetProperty(property);
452 EffectModel::ScriptUpdateBitmap(long graphicalSurfaceId, long bitmapId)
454 TypeMapGSurface::iterator it;
455 if (!FindGraphicalSurface(graphicalSurfaceId, it))
461 it->second->SetBitmapId(bitmapId);
463 return __pEMListener->OnBitmapRequested(bitmapId);
467 EffectModel::GetRenderingData(void) const
469 return __pRenderDataScene.get();
473 EffectModel::ScriptGetNearestPointsIds(long graphicalSurfaceId, Vec3f &position) const
475 TypeMapGSurface::iterator it;
476 if (!FindGraphicalSurface(graphicalSurfaceId, it))
478 PropertyCast propCast = {PropertyCast::BOOL, {false}};
481 return it->second->GetNearestPointsIds(position);
485 EffectModel::Construct(void)
487 if (__state != _EFFECT_STATE_INITIALIZING)
492 TypeMSurfacesCollection::iterator it;
493 for (it = __pMSurfacesCollection->begin(); it != __pMSurfacesCollection->end(); ++it)
495 (*it)->Construct(__pMapElements.get(), __pMapGSurface.get(), &(__pRenderDataScene->__renderDataSurfaceCollection));
503 EffectModel::UpdateGraphicalSurfaces(void)
505 if (__state == _EFFECT_STATE_RUNNING || __state == _EFFECT_STATE_STARTING || __state == _EFFECT_STATE_FINISHING)
507 TypeMSurfacesCollection::iterator it;
508 for ( it = __pMSurfacesCollection->begin(); it != __pMSurfacesCollection->end(); ++it )
510 (*it)->UpdateGraphicalSurface();
517 EffectModel::ScriptRotateSurface(long graphicalSurfaceId, float angle, _Axis axis, _Point point,
518 float ax, float ay, float az, float x0, float y0, float z0)
520 TypeMapGSurface::iterator it;
521 if (!FindGraphicalSurface(graphicalSurfaceId, it))
527 return it->second->RotateSurface(angle, axis, point,
528 ax, ay, az, x0, y0, z0);
533 EffectModel::ScriptScaleSurface(long graphicalSurfaceId, float ax, float ay, float az, _Point point,
534 float x0, float y0, float z0)
536 TypeMapGSurface::iterator it;
537 if (!FindGraphicalSurface(graphicalSurfaceId, it))
543 return it->second->ScaleSurface(ax, ay, az, point,
549 EffectModel::ScriptMoveSurface(long graphicalSurfaceId, float x0, float y0, float z0)
551 TypeMapGSurface::iterator it;
552 if (!FindGraphicalSurface(graphicalSurfaceId, it))
558 it->second->MoveSurface(x0, y0, z0);
564 EffectModel::ScriptResetSurfaceTransformation(long graphicalSurfaceID)
566 TypeMapGSurface::iterator it;
567 if (!FindGraphicalSurface(graphicalSurfaceID, it))
573 it->second->ResetSurfaceTransformation();
579 EffectModel::ScriptSetTransformationMatrix(long graphicalSurfaceID, const LuaMatrix4& luaMatrix4)
581 TypeMapGSurface::iterator it;
582 if (!FindGraphicalSurface(graphicalSurfaceID, it))
588 it->second->SetTransformationMatrix(luaMatrix4);
594 EffectModel::FindElement(long objID, TypeMapElements::iterator &it) const
596 it = __pMapElements->find(objID);
597 return (it != __pMapElements->end());
601 EffectModel::FindGraphicalSurface(long objID, TypeMapGSurface::iterator &it) const
603 it = __pMapGSurface->find(objID);
604 return (it != __pMapGSurface->end());
608 EffectModel::InitEffectModel(void)
610 __effectTimePassed = 0;
611 __effectResult.effectResult = static_cast<EffectResult>(0);
612 __effectResult.lastShownBitmapsId = LastShownBitmapIdCollection(1, 1);
614 if (!__pScript->Initialize())
616 __state = _EFFECT_STATE_RUNTIME_ERROR;
620 __state = _EFFECT_STATE_RUNNABLE;
627 EffectModel::ScriptAddUnitLight(UnitLight& unitLight)
629 return __pRenderDataScene->AddUnitLight(unitLight);
633 EffectModel::ScriptRemoveUnitLight(const string& name)
635 return __pRenderDataScene->RemoveUnitLight(name);
639 EffectModel::ScriptRemoveAllUnitsLightType(TypeUnitLight typeUnitLight)
641 return __pRenderDataScene->RemoveAllUnitsLightType(typeUnitLight);
645 EffectModel::ScriptGetUnitLight(const string& name) const
647 return __pRenderDataScene->GetUnitLight(name);
651 EffectModel::ScriptSetLightAmbientColour(const Vec3f& ambientColour)
653 __pRenderDataScene->SetLightAmbientColour(ambientColour);
658 EffectModel::ScriptSetLightAmbientColour(float red, float green, float blue)
660 __pRenderDataScene->SetLightAmbientColour(red, green, blue);
665 EffectModel::ScriptSetLightIntensity(float intensity)
667 __pRenderDataScene->SetLightIntensity(intensity);
672 EffectModel::ScriptSetLightAttenuation(float attenuation)
674 __pRenderDataScene->SetLightAttenuation(attenuation);
679 EffectModel::ScriptGetLightAmbientColour(void) const
681 return __pRenderDataScene->GetLightAmbientColour();
685 EffectModel::ScriptGetLightIntensity(void) const
687 return __pRenderDataScene->GetLightIntensity();
691 EffectModel::ScriptGetLightAttenuation(void) const
693 return __pRenderDataScene->GetLightAttenuation();
695 ///////////////////////////////////////////////////
697 EffectModel::GetId(void) const
703 EffectModel::GetName(void) const
708 } } } } // Tizen::Ui::Effects::_Runtime