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 constructor(value: Array<ImageFrameInfo>) { 19 super(value); 20 } 21 static identity: Symbol = Symbol('imageAnimatorImages'); 22 applyPeer(node: KNode, reset: boolean): void { 23 if (reset) { 24 getUINativeModule().imageAnimator.resetImages(node); 25 } else { 26 let arkImageFrame: ArkImageFrameInfoToArray = this.convertImageFrames(this.value); 27 if (!arkImageFrame) { 28 getUINativeModule().imageAnimator.resetImages(node); 29 } else { 30 getUINativeModule().imageAnimator.setImages(node, arkImageFrame.arrSrc, 31 arkImageFrame.arrWidth, arkImageFrame.arrHeight, arkImageFrame.arrTop, arkImageFrame.arrLeft, 32 arkImageFrame.arrDuration, arkImageFrame.arrSrc.length); 33 } 34 } 35 } 36 37 checkObjectDiff(): boolean { 38 let checkDiff = true; 39 if (this.value && this.value.length > 0 && 40 this.stageValue && this.stageValue.length > 0 && 41 this.value.length === this.stageValue.length) { 42 let checkItemEqual: Boolean = false; 43 44 for (let i: number = 0; i < this.value.length; i++) { 45 checkItemEqual = this.isEqual(this.stageValue[i], this.value[i]); 46 if (!checkItemEqual) { 47 checkDiff = !checkItemEqual; 48 break; 49 } 50 } 51 } 52 return checkDiff; 53 } 54 55 isEqual(one: ImageFrameInfo, another: ImageFrameInfo): boolean { 56 if (!(one.width === another.width && 57 one.height === another.height && 58 one.top === another.top && 59 one.left === another.left && 60 one.duration === another.duration)) { 61 return true; 62 } else { 63 return !isBaseOrResourceEqual(one.src, another.src); 64 } 65 } 66 67 convertImageFrames(value: Array<ImageFrameInfo>): ArkImageFrameInfoToArray { 68 if (value && value.length > 0) { 69 let isFlag: Boolean = true; 70 for (let item of value) { 71 if (item.src === undefined || item.src === null) { 72 isFlag = false; 73 break; 74 } 75 } 76 if (isFlag) { 77 let array: ArkImageFrameInfoToArray = new ArkImageFrameInfoToArray(); 78 for (let item of value) { 79 array.arrSrc.push(<string>item.src); 80 array.arrWidth.push((item.width === undefined || item.width === null) ? 0 : item.width); 81 array.arrHeight.push((item.height === undefined || item.height === null) ? 0 : item.height); 82 array.arrTop.push((item.top === undefined || item.top === null) ? 0 : item.top); 83 array.arrLeft.push((item.left === undefined || item.left === null) ? 0 : item.left); 84 array.arrDuration.push((item.duration === undefined || item.duration === null) ? 0 : item.duration); 85 } 86 return array; 87 } else { 88 return undefined; 89 } 90 } else { 91 return undefined; 92 } 93 } 94} 95 96class ImageAnimatorDurationModifier extends ModifierWithKey<number> { 97 constructor(value: number) { 98 super(value); 99 } 100 static identity: Symbol = Symbol('imageAnimatorDuration'); 101 applyPeer(node: KNode, reset: boolean): void { 102 if (reset) { 103 getUINativeModule().imageAnimator.resetDuration(node); 104 } else { 105 getUINativeModule().imageAnimator.setDuration(node, this.value); 106 } 107 } 108 checkObjectDiff(): boolean { 109 return this.stageValue !== this.value; 110 } 111} 112 113class ImageAnimatorReverseModifier extends ModifierWithKey<boolean> { 114 constructor(value: boolean) { 115 super(value); 116 } 117 static identity: Symbol = Symbol('imageAnimatorReverse'); 118 applyPeer(node: KNode, reset: boolean): void { 119 if (reset) { 120 getUINativeModule().imageAnimator.resetReverse(node); 121 } else { 122 getUINativeModule().imageAnimator.setReverse(node, this.value); 123 } 124 } 125 checkObjectDiff(): boolean { 126 return this.stageValue !== this.value; 127 } 128} 129 130class ImageAnimatorStateModifier extends ModifierWithKey<AnimationStatus> { 131 constructor(value: AnimationStatus) { 132 super(value); 133 } 134 static identity: Symbol = Symbol('imageAnimatorState'); 135 applyPeer(node: KNode, reset: boolean): void { 136 if (reset) { 137 getUINativeModule().imageAnimator.resetState(node); 138 } else { 139 getUINativeModule().imageAnimator.setState(node, this.value); 140 } 141 } 142 checkObjectDiff(): boolean { 143 return this.stageValue !== this.value; 144 } 145} 146 147class ImageAnimatorFixedSizeModifier extends ModifierWithKey<boolean> { 148 constructor(value: boolean) { 149 super(value); 150 } 151 static identity: Symbol = Symbol('imageAnimatorFixedSize'); 152 applyPeer(node: KNode, reset: boolean): void { 153 if (reset) { 154 getUINativeModule().imageAnimator.resetFixedSize(node); 155 } else { 156 getUINativeModule().imageAnimator.setFixedSize(node, this.value); 157 } 158 } 159 checkObjectDiff(): boolean { 160 return this.stageValue !== this.value; 161 } 162} 163 164class ImageAnimatorFillModeModifier extends ModifierWithKey<FillMode> { 165 constructor(value: FillMode) { 166 super(value); 167 } 168 static identity: Symbol = Symbol('imageAnimatorFillMode'); 169 applyPeer(node: KNode, reset: boolean): void { 170 if (reset) { 171 getUINativeModule().imageAnimator.resetFillMode(node); 172 } else { 173 getUINativeModule().imageAnimator.setFillMode(node, this.value); 174 } 175 } 176 checkObjectDiff(): boolean { 177 return this.stageValue !== this.value; 178 } 179} 180 181class ImageAnimatorIterationsModeModifier extends ModifierWithKey<number> { 182 constructor(value: number) { 183 super(value); 184 } 185 static identity: Symbol = Symbol('imageAnimatorIterationsMode'); 186 applyPeer(node: KNode, reset: boolean): void { 187 if (reset) { 188 getUINativeModule().imageAnimator.resetIterations(node); 189 } else { 190 getUINativeModule().imageAnimator.setIterations(node, this.value); 191 } 192 } 193 checkObjectDiff(): boolean { 194 return this.stageValue !== this.value; 195 } 196} 197 198class ImageAnimatorAutoMonitorInvisibleAreaModeModifier extends ModifierWithKey<boolean> { 199 constructor(value: boolean) { 200 super(value); 201 } 202 static identity: Symbol = Symbol('autoMonitorInvisibleAreaMode'); 203 applyPeer(node: KNode, reset: boolean): void { 204 if (reset) { 205 getUINativeModule().imageAnimator.setAutoMonitorInvisibleAreaMode(node, false); 206 } else { 207 getUINativeModule().imageAnimator.setAutoMonitorInvisibleAreaMode(node, this.value); 208 } 209 } 210 checkObjectDiff(): boolean { 211 return this.stageValue !== this.value; 212 } 213} 214 215declare type OnStart = () => void; 216class ImageAnimatorOnStartModifier extends ModifierWithKey<OnStart> { 217 constructor(value: OnStart) { 218 super(value); 219 } 220 static identity: Symbol = Symbol('imageAnimatorOnStart'); 221 applyPeer(node: KNode, reset: boolean): void { 222 if (reset) { 223 getUINativeModule().imageAnimator.resetImageAnimatorOnStart(node); 224 } else { 225 getUINativeModule().imageAnimator.setImageAnimatorOnStart(node, this.value); 226 } 227 } 228} 229 230declare type OnPause = () => void; 231class ImageAnimatorOnPauseModifier extends ModifierWithKey<OnPause> { 232 constructor(value: OnPause) { 233 super(value); 234 } 235 static identity: Symbol = Symbol('imageAnimatorOnPause'); 236 applyPeer(node: KNode, reset: boolean): void { 237 if (reset) { 238 getUINativeModule().imageAnimator.resetImageAnimatorOnPause(node); 239 } else { 240 getUINativeModule().imageAnimator.setImageAnimatorOnPause(node, this.value); 241 } 242 } 243} 244 245declare type OnRepeat = () => void; 246class ImageAnimatorOnRepeatModifier extends ModifierWithKey<OnRepeat> { 247 constructor(value: OnRepeat) { 248 super(value); 249 } 250 static identity: Symbol = Symbol('imageAnimatorOnRepeat'); 251 applyPeer(node: KNode, reset: boolean): void { 252 if (reset) { 253 getUINativeModule().imageAnimator.resetImageAnimatorOnRepeat(node); 254 } else { 255 getUINativeModule().imageAnimator.setImageAnimatorOnRepeat(node, this.value); 256 } 257 } 258} 259 260declare type OnCancel = () => void; 261class ImageAnimatorOnCancelModifier extends ModifierWithKey<OnCancel> { 262 constructor(value: OnCancel) { 263 super(value); 264 } 265 static identity: Symbol = Symbol('imageAnimatorOnCancel'); 266 applyPeer(node: KNode, reset: boolean): void { 267 if (reset) { 268 getUINativeModule().imageAnimator.resetImageAnimatorOnCancel(node); 269 } else { 270 getUINativeModule().imageAnimator.setImageAnimatorOnCancel(node, this.value); 271 } 272 } 273} 274 275declare type OnFinish = () => void; 276class ImageAnimatorOnFinishModifier extends ModifierWithKey<OnFinish> { 277 constructor(value: OnFinish) { 278 super(value); 279 } 280 static identity: Symbol = Symbol('imageAnimatorOnFinish'); 281 applyPeer(node: KNode, reset: boolean): void { 282 if (reset) { 283 getUINativeModule().imageAnimator.resetImageAnimatorOnFinish(node); 284 } else { 285 getUINativeModule().imageAnimator.setImageAnimatorOnFinish(node, this.value); 286 } 287 } 288} 289 290class ArkImageAnimatorComponent extends ArkComponent implements CommonMethod<ImageAnimatorAttribute> { 291 constructor(nativePtr: KNode, classType?: ModifierType) { 292 super(nativePtr, classType); 293 } 294 images(value: Array<ImageFrameInfo>): ImageAnimatorAttribute { 295 modifierWithKey(this._modifiersWithKeys, ImageAnimatorImagesModifier.identity, 296 ImageAnimatorImagesModifier, value); 297 return this; 298 } 299 state(value: AnimationStatus): ImageAnimatorAttribute { 300 modifierWithKey(this._modifiersWithKeys, ImageAnimatorStateModifier.identity, 301 ImageAnimatorStateModifier, value); 302 return this; 303 } 304 duration(value: number): ImageAnimatorAttribute { 305 modifierWithKey(this._modifiersWithKeys, ImageAnimatorDurationModifier.identity, 306 ImageAnimatorDurationModifier, value); 307 return this; 308 } 309 reverse(value: boolean): ImageAnimatorAttribute { 310 modifierWithKey(this._modifiersWithKeys, ImageAnimatorReverseModifier.identity, 311 ImageAnimatorReverseModifier, value); 312 return this; 313 } 314 fixedSize(value: boolean): ImageAnimatorAttribute { 315 modifierWithKey(this._modifiersWithKeys, ImageAnimatorFixedSizeModifier.identity, 316 ImageAnimatorFixedSizeModifier, value); 317 return this; 318 } 319 preDecode(value: number): ImageAnimatorAttribute { 320 throw new Error('Method not implemented.'); 321 } 322 fillMode(value: FillMode): ImageAnimatorAttribute { 323 modifierWithKey(this._modifiersWithKeys, ImageAnimatorFillModeModifier.identity, 324 ImageAnimatorFillModeModifier, value); 325 return this; 326 } 327 iterations(value: number): ImageAnimatorAttribute { 328 modifierWithKey(this._modifiersWithKeys, ImageAnimatorIterationsModeModifier.identity, 329 ImageAnimatorIterationsModeModifier, value); 330 return this; 331 } 332 monitorInvisibleAreaMode(value: boolean): ImageAnimatorAttribute { 333 modifierWithKey(this._modifiersWithKeys, ImageAnimatorAutoMonitorInvisibleAreaModeModifier.identity, 334 ImageAnimatorAutoMonitorInvisibleAreaModeModifier, value); 335 return this; 336 } 337 onStart(event: () => void): ImageAnimatorAttribute { 338 modifierWithKey(this._modifiersWithKeys, ImageAnimatorOnStartModifier.identity, ImageAnimatorOnIncModifier, event); 339 return this; 340 } 341 onPause(event: () => void): ImageAnimatorAttribute { 342 modifierWithKey(this._modifiersWithKeys, ImageAnimatorOnPauseModifier.identity, ImageAnimatorOnPauseModifier, event); 343 return this; 344 } 345 onRepeat(event: () => void): ImageAnimatorAttribute { 346 modifierWithKey(this._modifiersWithKeys, ImageAnimatorOnRepeatModifier.identity, ImageAnimatorOnRepeatModifier, event); 347 return this; 348 } 349 onCancel(event: () => void): ImageAnimatorAttribute { 350 modifierWithKey(this._modifiersWithKeys, ImageAnimatorOnCancelModifier.identity, ImageAnimatorOnCancelModifier, event); 351 return this; 352 } 353 onFinish(event: () => void): ImageAnimatorAttribute { 354 modifierWithKey(this._modifiersWithKeys, ImageAnimatorOnFinishModifier.identity, ImageAnimatorOnFinishModifier, event); 355 return this; 356 } 357} 358// @ts-ignore 359globalThis.ImageAnimator.attributeModifier = function (modifier: ArkComponent): void { 360 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 361 return new ArkImageAnimatorComponent(nativePtr); 362 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 363 return new modifierJS.ImageAnimatorModifier(nativePtr, classType); 364 }); 365}; 366