1/* 2 * Copyright (c) 2023 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/// <reference path='./import.ts' /> 17class ImageAnimatorImagesModifier extends ModifierWithKey<Array<ImageFrameInfo>> { 18 static identity: Symbol = Symbol('imageAnimatorImages'); 19 applyPeer(node: KNode, reset: boolean): void { 20 if (reset) { 21 getUINativeModule().imageAnimator.resetImages(node); 22 } else { 23 let arkImageFrame: ArkImageFrameInfoToArray = this.convertImageFrames(this.value); 24 if (!arkImageFrame) { 25 getUINativeModule().imageAnimator.resetImages(node); 26 } else { 27 getUINativeModule().imageAnimator.setImages(node, arkImageFrame.arrSrc, 28 arkImageFrame.arrWidth, arkImageFrame.arrHeight, arkImageFrame.arrTop, arkImageFrame.arrLeft, 29 arkImageFrame.arrDuration, arkImageFrame.arrSrc.length); 30 } 31 } 32 } 33 34 checkObjectDiff(): boolean { 35 let checkDiff = true; 36 if (this.value && this.value.length > 0 && 37 this.stageValue && this.stageValue.length > 0 && 38 this.value.length === this.stageValue.length) { 39 let checkItemEqual: Boolean = false; 40 41 for (let i: number = 0; i < this.value.length; i++) { 42 checkItemEqual = this.isEqual(this.stageValue[i], this.value[i]); 43 if (!checkItemEqual) { 44 checkDiff = !checkItemEqual; 45 break; 46 } 47 } 48 } 49 return checkDiff; 50 } 51 52 isEqual(one: ImageFrameInfo, another: ImageFrameInfo): boolean { 53 if (!(one.width === another.width && 54 one.height === another.height && 55 one.top === another.top && 56 one.left === another.left && 57 one.duration === another.duration)) { 58 return true; 59 } else { 60 return !isBaseOrResourceEqual(one.src, another.src); 61 } 62 } 63 64 convertImageFrames(value: Array<ImageFrameInfo>): ArkImageFrameInfoToArray { 65 if (value && value.length > 0) { 66 let isFlag: Boolean = true; 67 for (let item of value) { 68 if (item.src === undefined || item.src === null) { 69 isFlag = false; 70 break; 71 } 72 } 73 if (isFlag) { 74 let array: ArkImageFrameInfoToArray = new ArkImageFrameInfoToArray(); 75 for (let item of value) { 76 array.arrSrc.push(<string>item.src); 77 array.arrWidth.push((item.width === undefined || item.width === null) ? 0 : item.width); 78 array.arrHeight.push((item.height === undefined || item.height === null) ? 0 : item.height); 79 array.arrTop.push((item.top === undefined || item.top === null) ? 0 : item.top); 80 array.arrLeft.push((item.left === undefined || item.left === null) ? 0 : item.left); 81 array.arrDuration.push((item.duration === undefined || item.duration === null) ? 0 : item.duration); 82 } 83 return array; 84 } else { 85 return undefined; 86 } 87 } else { 88 return undefined; 89 } 90 } 91} 92 93class ImageAnimatorDurationModifier extends ModifierWithKey<number> { 94 static identity: Symbol = Symbol('imageAnimatorDuration'); 95 applyPeer(node: KNode, reset: boolean): void { 96 if (reset) { 97 getUINativeModule().imageAnimator.resetDuration(node); 98 } else { 99 getUINativeModule().imageAnimator.setDuration(node, this.value); 100 } 101 } 102 checkObjectDiff(): boolean { 103 return this.stageValue !== this.value; 104 } 105} 106 107class ImageAnimatorReverseModifier extends ModifierWithKey<boolean> { 108 static identity: Symbol = Symbol('imageAnimatorReverse'); 109 applyPeer(node: KNode, reset: boolean): void { 110 if (reset) { 111 getUINativeModule().imageAnimator.resetReverse(node); 112 } else { 113 getUINativeModule().imageAnimator.setReverse(node, this.value); 114 } 115 } 116 checkObjectDiff(): boolean { 117 return this.stageValue !== this.value; 118 } 119} 120 121class ImageAnimatorStateModifier extends ModifierWithKey<AnimationStatus> { 122 static identity: Symbol = Symbol('imageAnimatorState'); 123 applyPeer(node: KNode, reset: boolean): void { 124 if (reset) { 125 getUINativeModule().imageAnimator.resetState(node); 126 } else { 127 getUINativeModule().imageAnimator.setState(node, this.value); 128 } 129 } 130 checkObjectDiff(): boolean { 131 return this.stageValue !== this.value; 132 } 133} 134 135class ImageAnimatorFixedSizeModifier extends ModifierWithKey<boolean> { 136 static identity: Symbol = Symbol('imageAnimatorFixedSize'); 137 applyPeer(node: KNode, reset: boolean): void { 138 if (reset) { 139 getUINativeModule().imageAnimator.resetFixedSize(node); 140 } else { 141 getUINativeModule().imageAnimator.setFixedSize(node, this.value); 142 } 143 } 144 checkObjectDiff(): boolean { 145 return this.stageValue !== this.value; 146 } 147} 148 149class ImageAnimatorFillModeModifier extends ModifierWithKey<FillMode> { 150 static identity: Symbol = Symbol('imageAnimatorFillMode'); 151 applyPeer(node: KNode, reset: boolean): void { 152 if (reset) { 153 getUINativeModule().imageAnimator.resetFillMode(node); 154 } else { 155 getUINativeModule().imageAnimator.setFillMode(node, this.value); 156 } 157 } 158 checkObjectDiff(): boolean { 159 return this.stageValue !== this.value; 160 } 161} 162 163class ImageAnimatorIterationsModeModifier extends ModifierWithKey<number> { 164 static identity: Symbol = Symbol('imageAnimatorIterationsMode'); 165 applyPeer(node: KNode, reset: boolean): void { 166 if (reset) { 167 getUINativeModule().imageAnimator.resetIterations(node); 168 } else { 169 getUINativeModule().imageAnimator.setIterations(node, this.value); 170 } 171 } 172 checkObjectDiff(): boolean { 173 return this.stageValue !== this.value; 174 } 175} 176 177class ArkImageAnimatorComponent extends ArkComponent implements CommonMethod<ImageAnimatorAttribute> { 178 constructor(nativePtr: KNode, classType?: ModifierType) { 179 super(nativePtr, classType); 180 } 181 images(value: Array<ImageFrameInfo>): ImageAnimatorAttribute { 182 modifierWithKey(this._modifiersWithKeys, ImageAnimatorImagesModifier.identity, 183 ImageAnimatorImagesModifier, value); 184 return this; 185 } 186 state(value: AnimationStatus): ImageAnimatorAttribute { 187 modifierWithKey(this._modifiersWithKeys, ImageAnimatorStateModifier.identity, 188 ImageAnimatorStateModifier, value); 189 return this; 190 } 191 duration(value: number): ImageAnimatorAttribute { 192 modifierWithKey(this._modifiersWithKeys, ImageAnimatorDurationModifier.identity, 193 ImageAnimatorDurationModifier, value); 194 return this; 195 } 196 reverse(value: boolean): ImageAnimatorAttribute { 197 modifierWithKey(this._modifiersWithKeys, ImageAnimatorReverseModifier.identity, 198 ImageAnimatorReverseModifier, value); 199 return this; 200 } 201 fixedSize(value: boolean): ImageAnimatorAttribute { 202 modifierWithKey(this._modifiersWithKeys, ImageAnimatorFixedSizeModifier.identity, 203 ImageAnimatorFixedSizeModifier, value); 204 return this; 205 } 206 preDecode(value: number): ImageAnimatorAttribute { 207 throw new Error('Method not implemented.'); 208 } 209 fillMode(value: FillMode): ImageAnimatorAttribute { 210 modifierWithKey(this._modifiersWithKeys, ImageAnimatorFillModeModifier.identity, 211 ImageAnimatorFillModeModifier, value); 212 return this; 213 } 214 iterations(value: number): ImageAnimatorAttribute { 215 modifierWithKey(this._modifiersWithKeys, ImageAnimatorIterationsModeModifier.identity, 216 ImageAnimatorIterationsModeModifier, value); 217 return this; 218 } 219 onStart(event: () => void): ImageAnimatorAttribute { 220 throw new Error('Method not implemented.'); 221 } 222 onPause(event: () => void): ImageAnimatorAttribute { 223 throw new Error('Method not implemented.'); 224 } 225 onRepeat(event: () => void): ImageAnimatorAttribute { 226 throw new Error('Method not implemented.'); 227 } 228 onCancel(event: () => void): ImageAnimatorAttribute { 229 throw new Error('Method not implemented.'); 230 } 231 onFinish(event: () => void): ImageAnimatorAttribute { 232 throw new Error('Method not implemented.'); 233 } 234} 235// @ts-ignore 236globalThis.ImageAnimator.attributeModifier = function (modifier: ArkComponent): void { 237 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 238 return new ArkImageAnimatorComponent(nativePtr); 239 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 240 return new modifierJS.ImageAnimatorModifier(nativePtr, classType); 241 }); 242}; 243