1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16
17#include "node/node_transition.h"
18
19#include "base/utils/utils.h"
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25ArkUI_TransitionEffect* OH_ArkUI_CreateOpacityTransitionEffect(float opacity)
26{
27    ArkUI_TransitionEffect* option = new ArkUI_TransitionEffect { ARKUI_TRANSITION_EFFECT_OPACITY };
28    option->translate = nullptr;
29    option->scale = nullptr;
30    option->rotate = nullptr;
31    option->appear = nullptr;
32    option->disappear = nullptr;
33    option->combine = nullptr;
34    option->animation = nullptr;
35    option->opacity = std::clamp(opacity, 0.0f, 1.0f);
36    return option;
37}
38
39ArkUI_TransitionEffect* OH_ArkUI_CreateTranslationTransitionEffect(ArkUI_TranslationOptions* translate)
40{
41    CHECK_NULL_RETURN(translate, nullptr);
42    ArkUI_TransitionEffect* option = new ArkUI_TransitionEffect { ARKUI_TRANSITION_EFFECT_TRANSLATE };
43    option->scale = nullptr;
44    option->rotate = nullptr;
45    option->appear = nullptr;
46    option->disappear = nullptr;
47    option->combine = nullptr;
48    option->animation = nullptr;
49    option->translate = translate;
50    return option;
51}
52
53ArkUI_TransitionEffect* OH_ArkUI_CreateScaleTransitionEffect(ArkUI_ScaleOptions* scale)
54{
55    CHECK_NULL_RETURN(scale, nullptr);
56    ArkUI_TransitionEffect* option = new ArkUI_TransitionEffect { ARKUI_TRANSITION_EFFECT_SCALE };
57    option->translate = nullptr;
58    option->rotate = nullptr;
59    option->appear = nullptr;
60    option->disappear = nullptr;
61    option->combine = nullptr;
62    option->animation = nullptr;
63    option->scale = scale;
64    return option;
65}
66
67ArkUI_TransitionEffect* OH_ArkUI_CreateRotationTransitionEffect(ArkUI_RotationOptions* rotate)
68{
69    CHECK_NULL_RETURN(rotate, nullptr);
70    ArkUI_TransitionEffect* option = new ArkUI_TransitionEffect { ARKUI_TRANSITION_EFFECT_ROTATE };
71    option->translate = nullptr;
72    option->scale = nullptr;
73    option->appear = nullptr;
74    option->disappear = nullptr;
75    option->combine = nullptr;
76    option->animation = nullptr;
77    option->rotate = rotate;
78    return option;
79}
80
81ArkUI_TransitionEffect* OH_ArkUI_CreateMovementTransitionEffect(ArkUI_TransitionEdge move)
82{
83    if (move < ARKUI_TRANSITION_EDGE_TOP || move > ARKUI_TRANSITION_EDGE_END) {
84        move = ARKUI_TRANSITION_EDGE_START;
85    }
86    ArkUI_TransitionEffect* option = new ArkUI_TransitionEffect { ARKUI_TRANSITION_EFFECT_MOVE };
87    option->translate = nullptr;
88    option->scale = nullptr;
89    option->rotate = nullptr;
90    option->appear = nullptr;
91    option->disappear = nullptr;
92    option->combine = nullptr;
93    option->animation = nullptr;
94    option->move = move;
95    return option;
96}
97
98ArkUI_TransitionEffect* OH_ArkUI_CreateAsymmetricTransitionEffect(
99    ArkUI_TransitionEffect* appear, ArkUI_TransitionEffect* disappear)
100{
101    if (!appear && !disappear) {
102        return nullptr;
103    }
104    ArkUI_TransitionEffect* option = new ArkUI_TransitionEffect { ARKUI_TRANSITION_EFFECT_ASYMMETRIC };
105    option->translate = nullptr;
106    option->scale = nullptr;
107    option->rotate = nullptr;
108    option->combine = nullptr;
109    option->animation = nullptr;
110    option->appear = appear;
111    option->disappear = disappear;
112    return option;
113}
114
115void OH_ArkUI_TransitionEffect_Dispose(ArkUI_TransitionEffect* option)
116{
117    CHECK_NULL_VOID(option);
118    if (option->toEffectOption) {
119        delete option->toEffectOption;
120        option->toEffectOption = nullptr;
121    }
122    delete option;
123}
124
125int32_t OH_ArkUI_TransitionEffect_Combine(ArkUI_TransitionEffect* option, ArkUI_TransitionEffect* combine)
126{
127    CHECK_NULL_RETURN(option, ARKUI_ERROR_CODE_PARAM_INVALID);
128    auto* currentOption = option;
129    while (currentOption->combine) {
130        currentOption = currentOption->combine;
131    }
132    currentOption->combine = combine;
133    return ARKUI_ERROR_CODE_NO_ERROR;
134}
135
136int32_t OH_ArkUI_TransitionEffect_SetAnimation(ArkUI_TransitionEffect* option, ArkUI_AnimateOption* animation)
137{
138    CHECK_NULL_RETURN(option, ARKUI_ERROR_CODE_PARAM_INVALID);
139    option->animation = animation;
140    return ARKUI_ERROR_CODE_NO_ERROR;
141}
142#ifdef __cplusplus
143};
144#endif