[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / alpha-function-helper.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
18 // CLASS HEADER
19 #include <dali-scene3d/public-api/loader/alpha-function-helper.h>
20
21 // EXTERNAL INCLUDES
22 #include <unordered_map>
23
24 namespace Dali::Scene3D::Loader
25 {
26 namespace
27 {
28 // clang-format off
29 #define DALI_ALPHA_FUNCTION_ENTRY(x) { #x, AlphaFunction::x }
30 // clang-format on
31
32 std::unordered_map<std::string, AlphaFunction>& GetFunctions()
33 {
34   static std::unordered_map<std::string, AlphaFunction> sFunctions{
35     DALI_ALPHA_FUNCTION_ENTRY(DEFAULT),
36     DALI_ALPHA_FUNCTION_ENTRY(LINEAR),
37     DALI_ALPHA_FUNCTION_ENTRY(REVERSE),
38     DALI_ALPHA_FUNCTION_ENTRY(EASE_IN_SQUARE),
39     DALI_ALPHA_FUNCTION_ENTRY(EASE_OUT_SQUARE),
40     DALI_ALPHA_FUNCTION_ENTRY(EASE_IN),
41     DALI_ALPHA_FUNCTION_ENTRY(EASE_OUT),
42     DALI_ALPHA_FUNCTION_ENTRY(EASE_IN_OUT),
43     DALI_ALPHA_FUNCTION_ENTRY(EASE_IN_SINE),
44     DALI_ALPHA_FUNCTION_ENTRY(EASE_OUT_SINE),
45     DALI_ALPHA_FUNCTION_ENTRY(EASE_IN_OUT_SINE),
46     DALI_ALPHA_FUNCTION_ENTRY(BOUNCE),
47     DALI_ALPHA_FUNCTION_ENTRY(SIN),
48     DALI_ALPHA_FUNCTION_ENTRY(EASE_OUT_BACK),
49   };
50   return sFunctions;
51 }
52
53 #undef DALI_ALPHA_FUNCTION_ENTRY
54
55 } // namespace
56
57 AlphaFunction GetAlphaFunction(const std::string& name, bool* found)
58 {
59   auto iFind   = GetFunctions().find(name);
60   bool success = iFind != GetFunctions().end();
61   if(found)
62   {
63     *found = success;
64   }
65   return success ? iFind->second : AlphaFunction(AlphaFunction::DEFAULT);
66 }
67
68 void RegisterAlphaFunction(const std::string& name, AlphaFunction alphaFn)
69 {
70   DALI_ASSERT_ALWAYS(GetFunctions().insert({name, alphaFn}).second &&
71                      "Function with given key already exists.");
72 }
73
74 } // namespace Dali::Scene3D::Loader