bevy_flash 0.1.2

A Bevy plugin for Flash Animation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
use std::collections::btree_map::ValuesMut;
use std::sync::Arc;

use bevy::{
    asset::{Assets, Handle, RenderAssetUsages},
    image::Image,
    log::warn_once,
    math::{IVec2, UVec2},
    platform::collections::HashMap,
    render::render_resource::TextureFormat,
};
use swf::{BlendMode, CharacterId, ColorTransform, Depth, Rectangle, Twips};

use crate::RenderContext;

use super::{
    character::Character, filter::Filter, graphic::Graphic, matrix::Matrix,
    morph_shape::MorphShape, movie_clip::MovieClip, tag_utils::SwfMovie, transform::Transform,
};

pub(crate) type FrameNumber = u16;

type ImageCaches = HashMap<(u16, u16), Handle<Image>>;

#[derive(Debug, Clone, Default)]
pub struct ImageCacheInfo {
    width: u16,
    height: u16,
    caches: ImageCaches,
}
impl ImageCacheInfo {
    pub fn handle(&self) -> Handle<Image> {
        self.caches.get(&(self.width, self.height)).unwrap().clone()
    }
    pub fn size(&self) -> UVec2 {
        UVec2::new(self.width as u32, self.height as u32)
    }
    pub fn has_cache(&mut self, width: u16, height: u16) -> bool {
        if self.caches.contains_key(&(width, height)) {
            // 缓存命中
            self.width = width;
            self.height = height;
            true
        } else {
            false
        }
    }
}

#[derive(Debug, Clone, Default)]
pub struct ImageCache {
    /// 此缓存上次使用的 `Matrix.a` 值
    matrix_a: f32,
    /// 此缓存上次使用的 `Matrix.b` 值
    matrix_b: f32,
    /// 此缓存上次使用的 `Matrix.c` 值
    matrix_c: f32,
    /// 此缓存上次使用的 `Matrix.d` 值
    matrix_d: f32,

    /// 原始图像的宽度,应用滤镜之前。
    source_width: u16,
    /// 原始图像的高度,应用滤镜之前。
    source_height: u16,

    /// 缓存的图像
    image: Option<ImageCacheInfo>,

    /// 用于绘制最终位图的偏移量(即如果滤镜增加了位图大小的情况下)。
    draw_offset: IVec2,

    dirty: bool,
}

impl ImageCache {
    pub fn is_dirty(&mut self, other: &Matrix, source_width: u16, source_height: u16) -> bool {
        self.dirty = self.matrix_a != other.a
            || self.matrix_b != other.b
            || self.matrix_c != other.c
            || self.matrix_d != other.d
            || self.source_width != source_width
            || self.source_height != source_height
            || self.image.is_none();
        self.dirty
    }

    #[allow(clippy::too_many_arguments)]
    pub fn update(
        &mut self,
        images: &mut Assets<Image>,
        matrix: &Matrix,
        width: u16,
        height: u16,
        actual_width: u16,
        actual_height: u16,
        swf_version: u8,
        draw_offset: IVec2,
    ) {
        self.matrix_a = matrix.a;
        self.matrix_b = matrix.b;
        self.matrix_c = matrix.c;
        self.matrix_d = matrix.d;
        self.source_width = width;
        self.source_height = height;
        self.draw_offset = draw_offset;

        if let Some(current) = self.image.as_mut()
            && current.has_cache(actual_width, actual_height)
        {
            // 缓存命中,不需要重新渲染
            return;
        }
        let acceptable_size = if swf_version > 9 {
            let total = actual_width as u32 * actual_height as u32;
            actual_width < 8191 && actual_height < 8191 && total < 16777216
        } else {
            actual_width < 2880 && actual_height < 2880
        };
        if actual_width > 0 && actual_height > 0 && acceptable_size {
            let mut image = Image::new_target_texture(
                actual_width as u32,
                actual_height as u32,
                TextureFormat::Rgba8Unorm,
            );
            image.asset_usage = RenderAssetUsages::RENDER_WORLD;
            if let Some(image_cache_info) = &mut self.image {
                image_cache_info.width = actual_width;
                image_cache_info.height = actual_height;
                image_cache_info
                    .caches
                    .insert((actual_width, actual_height), images.add(image));
            } else {
                let mut caches = HashMap::new();
                caches.insert((actual_width, actual_height), images.add(image));
                self.image = Some(ImageCacheInfo {
                    width: actual_width,
                    height: actual_height,
                    caches,
                });
            }
        } else {
            self.image = None;
        }
    }

    /// 显式清除缓存值并释放所有资源。
    /// 此操作仅应在无法渲染到缓存且需要暂时禁用缓存的情况下使用。
    pub fn clear(&mut self) {
        self.image = None;
    }

    pub fn image_info(&self) -> Option<ImageCacheInfo> {
        self.image.clone()
    }
}

#[derive(Debug, Clone, Default)]
pub struct DisplayObjectBase {
    name: Option<Box<str>>,
    place_frame: FrameNumber,
    depth: Depth,
    clip_depth: Depth,
    transform: Transform,
    filters: Vec<Filter>,
    blend_mode: BlendMode,
    as_bitmap_cached: bool,
    cache_dirty: bool,
}
impl DisplayObjectBase {
    fn set_matrix(&mut self, matrix: Matrix) {
        self.transform.matrix = matrix;
    }

    pub fn set_color_transform(&mut self, color_transform: ColorTransform) {
        self.transform.color_transform = color_transform;
    }

    pub fn set_blend_mode(&mut self, blend_mode: BlendMode) {
        self.blend_mode = blend_mode;
    }

    fn set_filters(&mut self, filters: Vec<Filter>) -> bool {
        if filters != self.filters {
            self.filters = filters;
            true
        } else {
            false
        }
    }

    fn recheck_cache(
        &self,
        shape_depth_layer: &str,
        image_caches: &mut HashMap<String, ImageCache>,
    ) {
        if !self.filters.is_empty() && image_caches.get(shape_depth_layer).is_none()
            || self.as_bitmap_cached
        {
            image_caches.insert(shape_depth_layer.to_owned(), ImageCache::default());
        }
    }

    fn invalidate_cached_bitmap(&mut self) {
        self.cache_dirty = true;
    }

    fn set_name(&mut self, name: Option<Box<str>>) {
        self.name = name;
    }

    pub fn transform(&self) -> &Transform {
        &self.transform
    }

    pub fn filters(&self) -> Vec<Filter> {
        self.filters.clone()
    }

    pub fn blend_mode(&self) -> BlendMode {
        self.blend_mode
    }
}

pub(crate) trait TDisplayObject: Clone + Into<DisplayObject> {
    fn base(&self) -> &DisplayObjectBase;
    fn base_mut(&mut self) -> &mut DisplayObjectBase;

    fn set_place_frame(&mut self, frame: u16) {
        self.base_mut().place_frame = frame;
    }

    fn set_depth(&mut self, depth: Depth) {
        self.base_mut().depth = depth;
    }

    fn set_clip_depth(&mut self, clip_depth: Depth) {
        self.base_mut().clip_depth = clip_depth;
    }

    fn set_matrix(&mut self, matrix: Matrix) {
        self.base_mut().set_matrix(matrix);
    }

    fn set_color_transform(&mut self, color_transform: ColorTransform) {
        self.base_mut().set_color_transform(color_transform);
    }

    fn set_blend_mode(&mut self, blend_mode: BlendMode) {
        self.base_mut().set_blend_mode(blend_mode);
    }

    fn set_filters(&mut self, filters: Vec<Filter>) {
        if self.base_mut().set_filters(filters) {
            self.invalidate_cached_bitmap();
        }
    }

    fn invalidate_cached_bitmap(&mut self) {
        self.base_mut().invalidate_cached_bitmap();
    }

    fn recheck_cache(
        &self,
        shape_depth_layer: &str,
        image_caches: &mut HashMap<String, ImageCache>,
    ) {
        self.base().recheck_cache(shape_depth_layer, image_caches);
    }

    fn set_name(&mut self, name: Option<Box<str>>) {
        self.base_mut().set_name(name);
    }

    fn movie(&self) -> Arc<SwfMovie>;

    fn id(&self) -> CharacterId;

    fn depth(&self) -> Depth {
        self.base().depth
    }

    fn clip_depth(&self) -> Depth {
        self.base().clip_depth
    }

    fn place_frame(&self) -> FrameNumber {
        self.base().place_frame
    }

    fn filters(&self) -> Vec<Filter> {
        self.base().filters()
    }

    fn blend_mode(&self) -> BlendMode {
        self.base().blend_mode()
    }

    fn matrix(&self) -> &Matrix {
        &self.base().transform().matrix
    }

    fn transform(&self) -> &Transform {
        self.base().transform()
    }

    fn swf_version(&self) -> u8 {
        self.movie().version()
    }

    fn cache_dirty(&self) -> bool {
        self.base().cache_dirty
    }

    /// 该对象未经过变换的固有边界框。这些边界不包含子显示对象(DisplayObjects)。
    /// 叶节点显示对象应返回自身的边界。仅包含子对象的复合显示对象应返回 &Default::default ()。
    fn self_bounds(&mut self, context: &mut RenderContext) -> Rectangle<Twips>;

    /// 获取此对象及其所有子对象的渲染边界。该边界与向 Flash 暴露的边界主要有两点不同:
    /// - 若应用了会增大显示内容尺寸的滤镜,渲染边界可能会更大
    /// - 不考虑滚动矩形
    fn render_bounds_with_transform(
        &mut self,
        matrix: &Matrix,
        include_own_filters: bool,
        context: &mut RenderContext,
    ) -> Rectangle<Twips> {
        let scale = context.scale;
        let mut bounds = *matrix * self.self_bounds(context);
        if let Some(children) = self.children_mut() {
            for child in children {
                let matrix = *matrix * *child.matrix();
                bounds = bounds.union(&child.render_bounds_with_transform(&matrix, true, context));
            }
        }

        if include_own_filters {
            let filters = self.base().filters();
            for mut filter in filters {
                filter.scale(scale.x, scale.y);
                bounds = filter.calculate_dest_rect(bounds);
            }
        }
        bounds
    }

    fn apply_place_object(&mut self, place_object: &swf::PlaceObject, version: u8) {
        if let Some(matrix) = place_object.matrix {
            self.set_matrix(matrix.into());
        }
        if let Some(color_transform) = &place_object.color_transform {
            self.set_color_transform(*color_transform);
        }
        if let Some(ratio) = place_object.ratio
            && let Some(morph_shape) = self.as_morph_shape()
        {
            morph_shape.set_ratio(ratio);
        }
        if let Some(blend_mode) = place_object.blend_mode {
            self.set_blend_mode(blend_mode);
        }
        if let Some(clip_depth) = place_object.clip_depth {
            self.set_clip_depth(clip_depth);
        }
        if let Some(is_bitmap_cached) = place_object.is_bitmap_cached {
            self.base_mut().as_bitmap_cached = is_bitmap_cached;
        }
        if version >= 11 {
            if let Some(_visible) = place_object.is_visible {
                //TODO:
                warn_once!("visible is not supported. id: {}. TODO!", self.id());
            }
            if let Some(_color) = place_object.background_color {}
        }
        if let Some(filters) = &place_object.filters {
            self.set_filters(filters.iter().map(Filter::from).collect())
        }
    }

    fn enter_frame(&mut self, _characters: &HashMap<u16, Character>) {}

    fn render_self(&mut self, _context: &mut RenderContext, _blend_mode: BlendMode) {}

    fn replace_with(&mut self, _id: CharacterId, _characters: &HashMap<CharacterId, Character>) {}

    fn children_mut(&mut self) -> Option<ValuesMut<'_, u16, DisplayObject>> {
        None
    }

    fn allow_as_mask(&self) -> bool {
        true
    }

    fn as_morph_shape(&mut self) -> Option<&mut MorphShape> {
        None
    }
}

#[derive(Debug, Clone)]
pub enum DisplayObject {
    Graphic(Graphic),
    MovieClip(MovieClip),
    MorphShape(MorphShape),
}

impl TDisplayObject for DisplayObject {
    fn base(&self) -> &DisplayObjectBase {
        match self {
            Self::Graphic(g) => g.base(),
            Self::MovieClip(m) => m.base(),
            Self::MorphShape(m) => m.base(),
        }
    }

    fn base_mut(&mut self) -> &mut DisplayObjectBase {
        match self {
            Self::Graphic(g) => g.base_mut(),
            Self::MovieClip(m) => m.base_mut(),
            Self::MorphShape(m) => m.base_mut(),
        }
    }

    fn movie(&self) -> Arc<SwfMovie> {
        match self {
            Self::Graphic(g) => g.movie(),
            Self::MovieClip(m) => m.movie(),
            Self::MorphShape(m) => m.movie(),
        }
    }

    fn enter_frame(&mut self, characters: &HashMap<CharacterId, Character>) {
        match self {
            Self::Graphic(g) => g.enter_frame(characters),
            Self::MovieClip(m) => m.enter_frame(characters),
            Self::MorphShape(m) => m.enter_frame(characters),
        }
    }

    fn replace_with(&mut self, id: CharacterId, characters: &HashMap<CharacterId, Character>) {
        match self {
            Self::Graphic(g) => g.replace_with(id, characters),
            Self::MovieClip(m) => m.replace_with(id, characters),
            Self::MorphShape(m) => m.replace_with(id, characters),
        }
    }

    fn self_bounds(&mut self, context: &mut RenderContext) -> Rectangle<Twips> {
        match self {
            Self::Graphic(g) => g.self_bounds(context),
            Self::MovieClip(m) => m.self_bounds(context),
            Self::MorphShape(m) => m.self_bounds(context),
        }
    }

    fn children_mut(&mut self) -> Option<ValuesMut<'_, u16, DisplayObject>> {
        match self {
            Self::MovieClip(m) => m.children_mut(),
            _ => None,
        }
    }

    fn id(&self) -> CharacterId {
        match self {
            Self::Graphic(g) => g.id(),
            Self::MovieClip(m) => m.id(),
            Self::MorphShape(m) => m.id(),
        }
    }

    fn as_morph_shape(&mut self) -> Option<&mut MorphShape> {
        match self {
            Self::MorphShape(m) => Some(m),
            _ => None,
        }
    }
}