[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / internal / physics-adaptor-impl.cpp
1 /*
2  * Copyright (c) 2023 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 // Class Header
18 #include <dali-physics/internal/physics-adaptor-impl.h>
19
20 // External Headers
21 #include <memory>
22 #include <utility>
23
24 // Internal Headers
25 #include <dali-physics/internal/physics-world-impl.h>
26 #include <dali/dali.h>
27 #include <dali/devel-api/threading/mutex.h>
28 #include <dali/integration-api/debug.h>
29 #include <dali/public-api/actors/drawable-actor.h>
30
31 namespace
32 {
33 #if defined(DEBUG_ENABLED)
34 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, false, "LOG_PHYSICS");
35 #endif
36
37 } // namespace
38
39 namespace Dali::Toolkit::Physics::Internal
40 {
41 PhysicsAdaptor::PhysicsAdaptor()
42 : mSlotDelegate(this)
43 {
44 }
45
46 PhysicsAdaptor::~PhysicsAdaptor()
47 {
48 }
49
50 void PhysicsAdaptor::Initialize(const Dali::Matrix& transform, Uint16Pair worldSize)
51 {
52   // Create an actor that can handle mouse events.
53   // @todo Enable this to be fully configured / provided
54   mRootActor                                 = Layer::New();
55   mRootActor[Actor::Property::NAME]          = "PhysicsRootLayer";
56   mRootActor[Layer::Property::BEHAVIOR]      = Layer::LAYER_3D;
57   mRootActor[Layer::Property::DEPTH_TEST]    = true;
58   mRootActor[Actor::Property::SIZE]          = Vector2(worldSize.GetWidth(), worldSize.GetHeight());
59   mRootActor[Actor::Property::ANCHOR_POINT]  = Dali::AnchorPoint::CENTER;
60   mRootActor[Actor::Property::PARENT_ORIGIN] = Dali::ParentOrigin::CENTER;
61
62   // Initialize derived adaptor (and world)
63   OnInitialize(transform, worldSize);
64 }
65
66 void PhysicsAdaptor::SetTimestep(float timestep)
67 {
68   mPhysicsWorld->SetTimestep(timestep);
69 }
70
71 float PhysicsAdaptor::GetTimestep() const
72 {
73   return mPhysicsWorld->GetTimestep();
74 }
75
76 Physics::PhysicsAdaptor::ScopedPhysicsAccessorPtr PhysicsAdaptor::GetPhysicsAccessor()
77 {
78   return std::unique_ptr<Physics::PhysicsAdaptor::ScopedPhysicsAccessor>(new Physics::PhysicsAdaptor::ScopedPhysicsAccessor(*mPhysicsWorld.get()));
79 }
80
81 void PhysicsAdaptor::SetIntegrationState(Physics::PhysicsAdaptor::IntegrationState state)
82 {
83   mPhysicsWorld->SetIntegrationState(state);
84 }
85
86 Physics::PhysicsAdaptor::IntegrationState PhysicsAdaptor::GetIntegrationState() const
87 {
88   return mPhysicsWorld->GetIntegrationState();
89 }
90
91 void PhysicsAdaptor::SetDebugState(Physics::PhysicsAdaptor::DebugState state)
92 {
93   mPhysicsWorld->SetDebugState(state);
94 }
95
96 Physics::PhysicsAdaptor::DebugState PhysicsAdaptor::GetDebugState() const
97 {
98   return mPhysicsWorld->GetDebugState();
99 }
100
101 Dali::Actor PhysicsAdaptor::GetRootActor() const
102 {
103   return mRootActor;
104 }
105
106 void PhysicsAdaptor::OnUpdateActors(Dali::UpdateProxy* updateProxy)
107 {
108   for(auto&& actor : mPhysicsActors)
109   {
110     // Get position, orientation from physics world.
111     Vector3 position = actor.second->GetActorPosition();
112     updateProxy->BakePosition(actor.first, position);
113     Quaternion rotation = actor.second->GetActorRotation();
114     updateProxy->BakeOrientation(actor.first, rotation);
115   }
116 }
117
118 void PhysicsAdaptor::Queue(std::function<void()> function)
119 {
120   mPhysicsWorld->Queue(function);
121 }
122
123 void PhysicsAdaptor::CreateSyncPoint()
124 {
125   mPhysicsWorld->CreateSyncPoint();
126 }
127
128 std::unique_ptr<PhysicsWorld>& PhysicsAdaptor::GetPhysicsWorld()
129 {
130   return mPhysicsWorld;
131 }
132
133 } // namespace Dali::Toolkit::Physics::Internal