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