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_extened.h"
18
19#include "base/utils/utils.h"
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25ArkUI_DrawableDescriptor* OH_ArkUI_DrawableDescriptor_CreateFromPixelMap(OH_PixelmapNativeHandle pixelMap)
26{
27    CHECK_NULL_RETURN(pixelMap, nullptr);
28    ArkUI_DrawableDescriptor* drawableDescriptor =
29        new ArkUI_DrawableDescriptor { nullptr, nullptr, 0, nullptr, nullptr, nullptr, nullptr };
30    drawableDescriptor->pixelMap = pixelMap;
31    drawableDescriptor->drawableDescriptor =
32        std::make_shared<OHOS::Ace::Napi::DrawableDescriptor>(pixelMap->GetInnerPixelmap());
33    return drawableDescriptor;
34}
35
36ArkUI_DrawableDescriptor* OH_ArkUI_DrawableDescriptor_CreateFromAnimatedPixelMap(
37    OH_PixelmapNativeHandle* array, int32_t size)
38{
39    CHECK_NULL_RETURN(array, nullptr);
40    ArkUI_DrawableDescriptor* drawableDescriptor =
41        new ArkUI_DrawableDescriptor { nullptr, nullptr, 0, nullptr, nullptr, nullptr, nullptr };
42    drawableDescriptor->pixelMapArray = array;
43    drawableDescriptor->size = size;
44    std::vector<std::shared_ptr<OHOS::Media::PixelMap>> pixelMapList;
45    for (int32_t index = 0; index < size; index++) {
46        if (!array[index]) {
47            continue;
48        }
49        pixelMapList.push_back(array[index]->GetInnerPixelmap());
50    }
51    int32_t duration = -1;
52    int32_t iteration = 1;
53    drawableDescriptor->animatedDrawableDescriptor =
54        std::make_shared<OHOS::Ace::Napi::AnimatedDrawableDescriptor>(pixelMapList, duration, iteration);
55    return drawableDescriptor;
56}
57
58void OH_ArkUI_DrawableDescriptor_Dispose(ArkUI_DrawableDescriptor* drawableDescriptor)
59{
60    delete drawableDescriptor;
61}
62
63OH_PixelmapNativeHandle OH_ArkUI_DrawableDescriptor_GetStaticPixelMap(ArkUI_DrawableDescriptor* drawableDescriptor)
64{
65    CHECK_NULL_RETURN(drawableDescriptor, nullptr);
66    return drawableDescriptor->pixelMap;
67}
68
69OH_PixelmapNativeHandle* OH_ArkUI_DrawableDescriptor_GetAnimatedPixelMapArray(
70    ArkUI_DrawableDescriptor* drawableDescriptor)
71{
72    CHECK_NULL_RETURN(drawableDescriptor, nullptr);
73    return drawableDescriptor->pixelMapArray;
74}
75
76int32_t OH_ArkUI_DrawableDescriptor_GetAnimatedPixelMapArraySize(ArkUI_DrawableDescriptor* drawableDescriptor)
77{
78    CHECK_NULL_RETURN(drawableDescriptor, 0);
79    return drawableDescriptor->size;
80}
81
82void OH_ArkUI_DrawableDescriptor_SetAnimationDuration(ArkUI_DrawableDescriptor* drawableDescriptor, int32_t duration)
83{
84    CHECK_NULL_VOID(drawableDescriptor);
85    CHECK_NULL_VOID(drawableDescriptor->animatedDrawableDescriptor);
86    drawableDescriptor->animatedDrawableDescriptor->SetDuration(duration);
87}
88
89int32_t OH_ArkUI_DrawableDescriptor_GetAnimationDuration(ArkUI_DrawableDescriptor* drawableDescriptor)
90{
91    CHECK_NULL_RETURN(drawableDescriptor, -1);
92    CHECK_NULL_RETURN(drawableDescriptor->animatedDrawableDescriptor, -1);
93    return drawableDescriptor->animatedDrawableDescriptor->GetDuration();
94}
95
96void OH_ArkUI_DrawableDescriptor_SetAnimationIteration(
97    ArkUI_DrawableDescriptor* drawableDescriptor, int32_t iteration)
98{
99    CHECK_NULL_VOID(drawableDescriptor);
100    CHECK_NULL_VOID(drawableDescriptor->animatedDrawableDescriptor);
101    drawableDescriptor->animatedDrawableDescriptor->SetIterations(iteration);
102}
103
104int32_t OH_ArkUI_DrawableDescriptor_GetAnimationIteration(ArkUI_DrawableDescriptor* drawableDescriptor)
105{
106    CHECK_NULL_RETURN(drawableDescriptor, 1);
107    CHECK_NULL_RETURN(drawableDescriptor->animatedDrawableDescriptor, 1);
108    return drawableDescriptor->animatedDrawableDescriptor->GetIterations();
109}
110
111#ifdef __cplusplus
112};
113#endif
114