-
-
Notifications
You must be signed in to change notification settings - Fork 817
/
graphics.rs
1713 lines (1498 loc) · 58.7 KB
/
graphics.rs
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! `flash.display.Graphics` builtin/prototype
use crate::avm2::activation::Activation;
use crate::avm2::error::{argument_error, make_error_2008};
use crate::avm2::globals::flash::geom::transform::object_to_matrix;
use crate::avm2::object::{Object, TObject, VectorObject};
use crate::avm2::parameters::ParametersExt;
use crate::avm2::value::Value;
use crate::avm2::vector::VectorStorage;
use crate::avm2::{ArrayStorage, Error};
use crate::avm2_stub_method;
use crate::display_object::TDisplayObject;
use crate::drawing::Drawing;
use crate::string::{AvmString, WStr};
use ruffle_render::shape_utils::{DrawCommand, GradientType};
use std::f64::consts::FRAC_1_SQRT_2;
use swf::{
Color, FillStyle, Fixed16, Fixed8, Gradient, GradientInterpolation, GradientRecord,
GradientSpread, LineCapStyle, LineJoinStyle, LineStyle, Matrix, Point, Twips,
};
/// Convert an RGB `color` and `alpha` argument pair into a `swf::Color`.
/// `alpha` is normalized from 0.0 - 1.0.
fn color_from_args(rgb: u32, alpha: f64) -> Color {
Color::from_rgb(rgb, (alpha * 255.0) as u8)
}
/// Implements `Graphics.beginFill`.
pub fn begin_fill<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(this) = this.as_display_object() {
let color = args.get_u32(activation, 0)?;
let alpha = args.get_f64(activation, 1)?;
if let Some(mut draw) = this.as_drawing(activation.context.gc_context) {
draw.set_fill_style(Some(FillStyle::Color(color_from_args(color, alpha))));
}
}
Ok(Value::Undefined)
}
/// Implements `Graphics.beginBitmapFill`.
pub fn begin_bitmap_fill<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(this) = this.as_display_object() {
let bitmap = args
.get_object(activation, 0, "bitmap")?
.as_bitmap_data()
.expect("Bitmap argument is ensured to be a BitmapData from actionscript");
let matrix = if let Some(matrix) = args.try_get_object(activation, 1) {
Matrix::from(object_to_matrix(matrix, activation)?)
} else {
// Users can explicitly pass in `null` to mean identity matrix
Matrix::IDENTITY
};
let is_repeating = args.get_bool(2);
let is_smoothed = args.get_bool(3);
let handle =
bitmap.bitmap_handle(activation.context.gc_context, activation.context.renderer);
let bitmap = ruffle_render::bitmap::BitmapInfo {
handle,
width: bitmap.width() as u16,
height: bitmap.height() as u16,
};
let scale_matrix = Matrix::scale(
(Twips::TWIPS_PER_PIXEL as i16).into(),
(Twips::TWIPS_PER_PIXEL as i16).into(),
);
if let Some(mut draw) = this.as_drawing(activation.context.gc_context) {
let id = draw.add_bitmap(bitmap);
draw.set_fill_style(Some(FillStyle::Bitmap {
id,
matrix: matrix * scale_matrix,
is_smoothed,
is_repeating,
}));
}
}
Ok(Value::Undefined)
}
/// Implements `Graphics.beginGradientFill`.
pub fn begin_gradient_fill<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(this) = this.as_display_object() {
let gradient_type = args.get_string(activation, 0)?;
let gradient_type = parse_gradient_type(activation, gradient_type)?;
let colors = args.get_object(activation, 1, "colors")?;
let alphas = args.get_object(activation, 2, "alphas")?;
let ratios = args.get_object(activation, 3, "ratios")?;
let records = build_gradient_records(
activation,
&colors.as_array_storage().expect("Guaranteed by AS"),
&alphas.as_array_storage().expect("Guaranteed by AS"),
&ratios.as_array_storage().expect("Guaranteed by AS"),
)?;
let matrix = if let Some(matrix) = args.try_get_object(activation, 4) {
Matrix::from(object_to_matrix(matrix, activation)?)
} else {
// Users can explicitly pass in `null` to mean identity matrix
Matrix::IDENTITY
};
let spread = args.get_string(activation, 5);
let spread = parse_spread_method(spread?);
let interpolation = args.get_string(activation, 6);
let interpolation = parse_interpolation_method(interpolation?);
let focal_point = args.get_f64(activation, 7)?;
if let Some(mut draw) = this.as_drawing(activation.context.gc_context) {
match gradient_type {
GradientType::Linear => {
draw.set_fill_style(Some(FillStyle::LinearGradient(Gradient {
matrix,
spread,
interpolation,
records,
})))
}
GradientType::Radial if focal_point == 0.0 => {
draw.set_fill_style(Some(FillStyle::RadialGradient(Gradient {
matrix,
spread,
interpolation,
records,
})))
}
_ => draw.set_fill_style(Some(FillStyle::FocalGradient {
gradient: Gradient {
matrix,
spread,
interpolation,
records,
},
focal_point: Fixed8::from_f64(focal_point),
})),
}
}
}
Ok(Value::Undefined)
}
fn build_gradient_records<'gc>(
activation: &mut Activation<'_, 'gc>,
colors: &ArrayStorage<'gc>,
alphas: &ArrayStorage<'gc>,
ratios: &ArrayStorage<'gc>,
) -> Result<Vec<GradientRecord>, Error<'gc>> {
let length = colors.length().min(alphas.length()).min(ratios.length());
let mut records = Vec::with_capacity(length);
for i in 0..length {
let color = colors
.get(i)
.expect("Length should be guaranteed")
.coerce_to_u32(activation)?;
let alpha = alphas
.get(i)
.expect("Length should be guaranteed")
.coerce_to_number(activation)? as f32;
let ratio = ratios
.get(i)
.expect("Length should be guaranteed")
.coerce_to_u32(activation)?;
records.push(GradientRecord {
ratio: ratio.clamp(0, 255) as u8,
color: Color::from_rgb(color, (alpha * 255.0) as u8),
})
}
Ok(records)
}
fn parse_gradient_type<'gc>(
activation: &mut Activation<'_, 'gc>,
gradient_type: AvmString<'gc>,
) -> Result<GradientType, Error<'gc>> {
if &gradient_type == b"linear" {
Ok(GradientType::Linear)
} else if &gradient_type == b"radial" {
Ok(GradientType::Radial)
} else {
Err(make_error_2008(activation, "type"))
}
}
fn parse_interpolation_method(gradient_type: AvmString) -> GradientInterpolation {
if &gradient_type == b"linearRGB" {
GradientInterpolation::LinearRgb
} else {
GradientInterpolation::Rgb
}
}
fn parse_spread_method(spread_method: AvmString) -> GradientSpread {
if &spread_method == b"repeat" {
GradientSpread::Repeat
} else if &spread_method == b"reflect" {
GradientSpread::Reflect
} else {
GradientSpread::Pad
}
}
/// Implements `Graphics.clear`
pub fn clear<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(this) = this.as_display_object() {
if let Some(mut draw) = this.as_drawing(activation.context.gc_context) {
draw.clear()
}
}
Ok(Value::Undefined)
}
/// Implements `Graphics.curveTo`.
pub fn curve_to<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(this) = this.as_display_object() {
let control_x = args.get_f64(activation, 0)?;
let control_y = args.get_f64(activation, 1)?;
let anchor_x = args.get_f64(activation, 2)?;
let anchor_y = args.get_f64(activation, 3)?;
if let Some(mut draw) = this.as_drawing(activation.context.gc_context) {
draw.draw_command(DrawCommand::QuadraticCurveTo {
control: Point::from_pixels(control_x, control_y),
anchor: Point::from_pixels(anchor_x, anchor_y),
});
}
}
Ok(Value::Undefined)
}
/// Implements `Graphics.endFill`.
pub fn end_fill<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(this) = this.as_display_object() {
if let Some(mut draw) = this.as_drawing(activation.context.gc_context) {
draw.set_fill_style(None);
}
}
Ok(Value::Undefined)
}
fn caps_to_cap_style(caps: Option<AvmString>) -> LineCapStyle {
if let Some(caps) = caps {
if &caps == b"none" {
LineCapStyle::None
} else if &caps == b"square" {
LineCapStyle::Square
} else {
LineCapStyle::Round
}
} else {
LineCapStyle::Round
}
}
fn joints_to_join_style(joints: Option<AvmString>, miter_limit: f64) -> LineJoinStyle {
if let Some(joints) = joints {
if &joints == b"miter" {
LineJoinStyle::Miter(Fixed8::from_f64(miter_limit))
} else if &joints == b"bevel" {
LineJoinStyle::Bevel
} else {
LineJoinStyle::Round
}
} else {
LineJoinStyle::Round
}
}
fn scale_mode_to_allow_scale_bits<'gc>(scale_mode: &WStr) -> Result<(bool, bool), Error<'gc>> {
if scale_mode == b"none" {
Ok((false, false))
} else if scale_mode == b"horizontal" {
Ok((true, false))
} else if scale_mode == b"vertical" {
Ok((false, true))
} else {
Ok((true, true))
}
}
/// Implements `Graphics.lineStyle`.
pub fn line_style<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(this) = this.as_display_object() {
let thickness = args.get_f64(activation, 0)?;
if thickness.is_nan() {
if let Some(mut draw) = this.as_drawing(activation.context.gc_context) {
draw.set_line_style(None);
}
} else {
let color = args.get_u32(activation, 1)?;
let alpha = args.get_f64(activation, 2)?;
let is_pixel_hinted = args.get_bool(3);
let scale_mode = args.get_string(activation, 4)?;
let caps = caps_to_cap_style(args.try_get_string(activation, 5)?);
let joints = args.try_get_string(activation, 6)?;
let miter_limit = args.get_f64(activation, 7)?;
let width = Twips::from_pixels(thickness.clamp(0.0, 255.0));
let color = color_from_args(color, alpha);
let join_style = joints_to_join_style(joints, miter_limit);
let (allow_scale_x, allow_scale_y) = scale_mode_to_allow_scale_bits(&scale_mode)?;
let line_style = LineStyle::new()
.with_width(width)
.with_color(color)
.with_start_cap(caps)
.with_end_cap(caps)
.with_join_style(join_style)
.with_allow_scale_x(allow_scale_x)
.with_allow_scale_y(allow_scale_y)
.with_is_pixel_hinted(is_pixel_hinted)
.with_allow_close(false);
if let Some(mut draw) = this.as_drawing(activation.context.gc_context) {
draw.set_line_style(Some(line_style));
}
}
}
Ok(Value::Undefined)
}
/// Implements `Graphics.lineTo`.
pub fn line_to<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(this) = this.as_display_object() {
let x = Twips::from_pixels(args.get_f64(activation, 0)?);
let y = Twips::from_pixels(args.get_f64(activation, 1)?);
if let Some(mut draw) = this.as_drawing(activation.context.gc_context) {
draw.draw_command(DrawCommand::LineTo(Point::new(x, y)));
}
}
Ok(Value::Undefined)
}
/// Implements `Graphics.moveTo`.
pub fn move_to<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(this) = this.as_display_object() {
let x = Twips::from_pixels(args.get_f64(activation, 0)?);
let y = Twips::from_pixels(args.get_f64(activation, 1)?);
if let Some(mut draw) = this.as_drawing(activation.context.gc_context) {
draw.draw_command(DrawCommand::MoveTo(Point::new(x, y)));
}
}
Ok(Value::Undefined)
}
/// Implements `Graphics.drawRect`.
pub fn draw_rect<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(this) = this.as_display_object() {
let x = Twips::from_pixels(args.get_f64(activation, 0)?);
let y = Twips::from_pixels(args.get_f64(activation, 1)?);
let width = Twips::from_pixels(args.get_f64(activation, 2)?);
let height = Twips::from_pixels(args.get_f64(activation, 3)?);
if let Some(mut draw) = this.as_drawing(activation.context.gc_context) {
draw.draw_command(DrawCommand::MoveTo(Point::new(x, y)));
draw.draw_command(DrawCommand::LineTo(Point::new(x + width, y)));
draw.draw_command(DrawCommand::LineTo(Point::new(x + width, y + height)));
draw.draw_command(DrawCommand::LineTo(Point::new(x, y + height)));
draw.draw_command(DrawCommand::LineTo(Point::new(x, y)));
}
}
Ok(Value::Undefined)
}
/// Length between two points on a unit circle that are 45 degrees apart from
/// one another.
///
/// This constant is `H`, short for 'hypotenuse', because it is also the length
/// of the hypotenuse formed from the control point triangle of any quadratic
/// Bezier curve approximating a 45-degree unit circle arc.
///
/// The derivation of this constant - or a similar constant for any other arc
/// angle hypotenuse - is as follows:
///
/// 1. Call the arc angle `alpha`. In this special case, `alpha` is 45 degrees,
/// or one-quarter `PI`.
/// 2. Consider the triangle formed by the center of the circle and the two
/// points at the start and end of the arc. The two other angles will be
/// equal, and it and `alpha` sum to 180 degrees. We'll call this angle
/// `beta`, and it is equal to `alpha` minus 180 degrees, divided by 2.
/// 3. Using the law of sines, we know that the sine of `alpha` divided by `H`
/// is equal to the sine of `beta` divided by `r`, where `r` is the radius
/// of the circle. We can solve for `H` to get the result. Note that since
/// this is a unit circle, you won't see a radius term in this constant.
//const H:f64 = (PI * 0.25).sin() / (PI * 0.375).sin();
/// Length between two control points of a quadratic Bezier curve approximating
/// a 45-degree arc of a unit circle.
///
/// This constant is critical to calculating the off-curve point of the control
/// point triangle. We do so by taking the tangents at each on-curve point,
/// which point in the direction of the off-curve points. Then, we scale one of
/// those tangent vectors by `A_B` and add it to the on-curve point to get the
/// off-curve point, constructing our Bezier.
///
/// The derivation of this constant - or a similar constant for any other arc
/// angle Bezier - is as follows:
///
/// 1. Start with the value of `H` for the given arc angle `alpha`.
/// 2. Consider the triangle formed by the three control points of our desired
/// Bezier curve. We'll call the angle at the off-curve control point
/// `delta`, and the two other angles of this triangle are `gamma`.
/// 3. Because two of the lines of this triangle are tangent lines of the
/// circle, they will form a right angle with the normal, which is the same
/// as the line between the center of the circle and the point.
/// Coincidentally, this right angle is shared between `beta`, meaning that
/// we can subtract it from 90 degrees to obtain `gamma`. Or, after some
/// elementary algebra, just take half of `alpha`.
/// 4. We can then derive the value of `delta` by subtracting out the other two
/// `gamma`s from 180 degrees. This, again, can be simplified to just
/// 180 degrees minus `alpha`.
/// 5. By the law of sines, the sine of `delta` divided by `H` is equal to
/// the sine of `gamma` divided by `A_B`. We can then rearrange this to get
/// `H` times the sine of `gamma`, divided by the sine of `delta`; which is
/// our `A_B` constant.
//const A_B:f64 = H * (PI * 0.125).sin() / (PI * 0.75).sin();
/// A list of five quadratic Bezier control points, intended to approximate the
/// bottom-right quadrant of a unit circle.
///
/// Through coordinate reflections we can obtain the rest of the circle; and
/// with translations and scaling we can obtain any ellipse on the plane.
///
/// Points are stored in counter-clockwise order from 0 degrees to 90 degrees.
const UNIT_CIRCLE_POINTS: [(f64, f64); 5] = [
(1.0, 0.0),
(1.0, 0.41421356237309503),
(FRAC_1_SQRT_2, FRAC_1_SQRT_2),
(0.4142135623730951, 1.0),
(0.00000000000000006123233995736766, 1.0),
];
/* [
((PI * 0.0).cos(), (PI * 0.0).sin()),
((PI * 0.0).cos() + *A_B * (PI * 0.0).sin() * -1.0,
(PI * 0.0).sin() + *A_B * (PI * 0.0).cos()),
((PI * 0.25).cos(), (PI * 0.25).sin()),
((PI * 0.25).cos() + *A_B * (PI * 0.25).sin() * -1.0,
(PI * 0.25).sin() + *A_B * (PI * 0.25).cos()),
((PI * 0.5).cos(), (PI * 0.5).sin()),
]; */
/// Draw a roundrect.
fn draw_round_rect_internal(
draw: &mut Drawing,
x: f64,
y: f64,
width: f64,
height: f64,
mut ellipse_width: f64,
mut ellipse_height: f64,
) {
if ellipse_height.is_nan() {
ellipse_height = ellipse_width;
}
//Clamp the ellipse sizes to the size of the rectangle.
if ellipse_width > width {
ellipse_width = width;
}
if ellipse_height > height {
ellipse_height = height;
}
// We'll start from the bottom-right corner of the rectangle,
// because that's what Flash Player does.
let ucp = UNIT_CIRCLE_POINTS;
let line_width = width - ellipse_width;
let line_height = height - ellipse_height;
let br_ellipse_center_x = x + ellipse_width / 2.0 + line_width;
let br_ellipse_center_y = y + ellipse_height / 2.0 + line_height;
let br_point_x = br_ellipse_center_x + ellipse_width / 2.0 * ucp[2].0;
let br_point_y = br_ellipse_center_y + ellipse_height / 2.0 * ucp[2].1;
let br_point = Point::from_pixels(br_point_x, br_point_y);
draw.draw_command(DrawCommand::MoveTo(br_point));
let br_b_curve_x = br_ellipse_center_x + ellipse_width / 2.0 * ucp[3].0;
let br_b_curve_y = br_ellipse_center_y + ellipse_height / 2.0 * ucp[3].1;
let br_b_curve = Point::from_pixels(br_b_curve_x, br_b_curve_y);
let right_b_point_x = br_ellipse_center_x + ellipse_width / 2.0 * ucp[4].0;
let right_b_point_y = br_ellipse_center_y + ellipse_height / 2.0 * ucp[4].1;
let right_b_point = Point::from_pixels(right_b_point_x, right_b_point_y);
draw.draw_command(DrawCommand::QuadraticCurveTo {
control: br_b_curve,
anchor: right_b_point,
});
// Oh, since we're drawing roundrects, we also need to draw lines
// in between each ellipse. This is the bottom line.
let tl_ellipse_center_x = x + ellipse_width / 2.0;
let tl_ellipse_center_y = y + ellipse_height / 2.0;
let left_b_point_x = tl_ellipse_center_x + ellipse_width / -2.0 * ucp[4].0;
let left_b_point_y = br_ellipse_center_y + ellipse_height / 2.0 * ucp[4].1;
let left_b_point = Point::from_pixels(left_b_point_x, left_b_point_y);
draw.draw_command(DrawCommand::LineTo(left_b_point));
// Bottom-left ellipse
let b_bl_curve_x = tl_ellipse_center_x + ellipse_width / -2.0 * ucp[3].0;
let b_bl_curve_y = br_ellipse_center_y + ellipse_height / 2.0 * ucp[3].1;
let b_bl_curve = Point::from_pixels(b_bl_curve_x, b_bl_curve_y);
let bl_point_x = tl_ellipse_center_x + ellipse_width / -2.0 * ucp[2].0;
let bl_point_y = br_ellipse_center_y + ellipse_height / 2.0 * ucp[2].1;
let bl_point = Point::from_pixels(bl_point_x, bl_point_y);
draw.draw_command(DrawCommand::QuadraticCurveTo {
control: b_bl_curve,
anchor: bl_point,
});
let bl_l_curve_x = tl_ellipse_center_x + ellipse_width / -2.0 * ucp[1].0;
let bl_l_curve_y = br_ellipse_center_y + ellipse_height / 2.0 * ucp[1].1;
let bl_l_curve = Point::from_pixels(bl_l_curve_x, bl_l_curve_y);
let bottom_l_point_x = tl_ellipse_center_x + ellipse_width / -2.0 * ucp[0].0;
let bottom_l_point_y = br_ellipse_center_y + ellipse_height / 2.0 * ucp[0].1;
let bottom_l_point = Point::from_pixels(bottom_l_point_x, bottom_l_point_y);
draw.draw_command(DrawCommand::QuadraticCurveTo {
control: bl_l_curve,
anchor: bottom_l_point,
});
// Left side
let top_l_point_x = tl_ellipse_center_x + ellipse_width / -2.0 * ucp[0].0;
let top_l_point_y = tl_ellipse_center_y + ellipse_height / -2.0 * ucp[0].1;
let top_l_point = Point::from_pixels(top_l_point_x, top_l_point_y);
draw.draw_command(DrawCommand::LineTo(top_l_point));
// Top-left ellipse
let l_tl_curve_x = tl_ellipse_center_x + ellipse_width / -2.0 * ucp[1].0;
let l_tl_curve_y = tl_ellipse_center_y + ellipse_height / -2.0 * ucp[1].1;
let l_tl_curve = Point::from_pixels(l_tl_curve_x, l_tl_curve_y);
let tl_point_x = tl_ellipse_center_x + ellipse_width / -2.0 * ucp[2].0;
let tl_point_y = tl_ellipse_center_y + ellipse_height / -2.0 * ucp[2].1;
let tl_point = Point::from_pixels(tl_point_x, tl_point_y);
draw.draw_command(DrawCommand::QuadraticCurveTo {
control: l_tl_curve,
anchor: tl_point,
});
let tl_t_curve_x = tl_ellipse_center_x + ellipse_width / -2.0 * ucp[3].0;
let tl_t_curve_y = tl_ellipse_center_y + ellipse_height / -2.0 * ucp[3].1;
let tl_t_curve = Point::from_pixels(tl_t_curve_x, tl_t_curve_y);
let left_t_point_x = tl_ellipse_center_x + ellipse_width / -2.0 * ucp[4].0;
let left_t_point_y = tl_ellipse_center_y + ellipse_height / -2.0 * ucp[4].1;
let left_t_point = Point::from_pixels(left_t_point_x, left_t_point_y);
draw.draw_command(DrawCommand::QuadraticCurveTo {
control: tl_t_curve,
anchor: left_t_point,
});
// Top side
let right_t_point_x = br_ellipse_center_x + ellipse_width / 2.0 * ucp[4].0;
let right_t_point_y = tl_ellipse_center_y + ellipse_height / -2.0 * ucp[4].1;
let right_t_point = Point::from_pixels(right_t_point_x, right_t_point_y);
draw.draw_command(DrawCommand::LineTo(right_t_point));
// Top-right ellipse
let t_tr_curve_x = br_ellipse_center_x + ellipse_width / 2.0 * ucp[3].0;
let t_tr_curve_y = tl_ellipse_center_y + ellipse_height / -2.0 * ucp[3].1;
let t_tr_curve = Point::from_pixels(t_tr_curve_x, t_tr_curve_y);
let tr_point_x = br_ellipse_center_x + ellipse_width / 2.0 * ucp[2].0;
let tr_point_y = tl_ellipse_center_y + ellipse_height / -2.0 * ucp[2].1;
let tr_point = Point::from_pixels(tr_point_x, tr_point_y);
draw.draw_command(DrawCommand::QuadraticCurveTo {
control: t_tr_curve,
anchor: tr_point,
});
let tr_r_curve_x = br_ellipse_center_x + ellipse_width / 2.0 * ucp[1].0;
let tr_r_curve_y = tl_ellipse_center_y + ellipse_height / -2.0 * ucp[1].1;
let tr_r_curve = Point::from_pixels(tr_r_curve_x, tr_r_curve_y);
let top_r_point_x = br_ellipse_center_x + ellipse_width / 2.0 * ucp[0].0;
let top_r_point_y = tl_ellipse_center_y + ellipse_height / -2.0 * ucp[0].1;
let top_r_point = Point::from_pixels(top_r_point_x, top_r_point_y);
draw.draw_command(DrawCommand::QuadraticCurveTo {
control: tr_r_curve,
anchor: top_r_point,
});
// Right side & other half of bottom-right ellipse
let bottom_r_point_x = br_ellipse_center_x + ellipse_width / 2.0 * ucp[0].0;
let bottom_r_point_y = br_ellipse_center_y + ellipse_height / 2.0 * ucp[0].1;
let bottom_r_point = Point::from_pixels(bottom_r_point_x, bottom_r_point_y);
draw.draw_command(DrawCommand::LineTo(bottom_r_point));
let r_br_curve_x = br_ellipse_center_x + ellipse_width / 2.0 * ucp[1].0;
let r_br_curve_y = br_ellipse_center_y + ellipse_height / 2.0 * ucp[1].1;
let r_br_curve = Point::from_pixels(r_br_curve_x, r_br_curve_y);
draw.draw_command(DrawCommand::QuadraticCurveTo {
control: r_br_curve,
anchor: br_point,
});
}
/// Implements `Graphics.drawRoundRect`.
pub fn draw_round_rect<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(this) = this.as_display_object() {
let x = args.get_f64(activation, 0)?;
let y = args.get_f64(activation, 1)?;
let width = args.get_f64(activation, 2)?;
let height = args.get_f64(activation, 3)?;
let ellipse_width = args.get_f64(activation, 4)?;
let ellipse_height = args.get_f64(activation, 5)?;
if let Some(mut draw) = this.as_drawing(activation.context.gc_context) {
draw_round_rect_internal(
&mut draw,
x,
y,
width,
height,
ellipse_width,
ellipse_height,
);
}
}
Ok(Value::Undefined)
}
/// Implements `Graphics.drawCircle`.
pub fn draw_circle<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(this) = this.as_display_object() {
let x = args.get_f64(activation, 0)?;
let y = args.get_f64(activation, 1)?;
let radius = args.get_f64(activation, 2)?;
if let Some(mut draw) = this.as_drawing(activation.context.gc_context) {
draw_round_rect_internal(
&mut draw,
x - radius,
y - radius,
radius * 2.0,
radius * 2.0,
radius * 2.0,
radius * 2.0,
);
}
}
Ok(Value::Undefined)
}
/// Implements `Graphics.drawEllipse`.
pub fn draw_ellipse<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(this) = this.as_display_object() {
let x = args.get_f64(activation, 0)?;
let y = args.get_f64(activation, 1)?;
let width = args.get_f64(activation, 2)?;
let height = args.get_f64(activation, 3)?;
if let Some(mut draw) = this.as_drawing(activation.context.gc_context) {
draw_round_rect_internal(&mut draw, x, y, width, height, width, height)
}
}
Ok(Value::Undefined)
}
/// Implements `Graphics.lineGradientStyle`
pub fn line_gradient_style<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(this) = this.as_display_object() {
let gradient_type = args.get_string(activation, 0);
let gradient_type = parse_gradient_type(activation, gradient_type?)?;
let colors = args.get_object(activation, 1, "colors")?;
let alphas = args.get_object(activation, 2, "alphas")?;
let ratios = args.get_object(activation, 3, "ratios")?;
let records = build_gradient_records(
activation,
&colors.as_array_storage().expect("Guaranteed by AS"),
&alphas.as_array_storage().expect("Guaranteed by AS"),
&ratios.as_array_storage().expect("Guaranteed by AS"),
)?;
let matrix = if let Some(matrix) = args.try_get_object(activation, 4) {
Matrix::from(object_to_matrix(matrix, activation)?)
} else {
// Users can explicitly pass in `null` to mean identity matrix
Matrix::IDENTITY
};
let spread = args.get_string(activation, 5);
let spread = parse_spread_method(spread?);
let interpolation = args.get_string(activation, 6);
let interpolation = parse_interpolation_method(interpolation?);
let focal_point = args.get_f64(activation, 7)?;
if let Some(mut draw) = this.as_drawing(activation.context.gc_context) {
match gradient_type {
GradientType::Linear => {
draw.set_line_fill_style(FillStyle::LinearGradient(Gradient {
matrix,
spread,
interpolation,
records,
}))
}
GradientType::Radial if focal_point == 0.0 => {
draw.set_line_fill_style(FillStyle::RadialGradient(Gradient {
matrix,
spread,
interpolation,
records,
}))
}
_ => draw.set_line_fill_style(FillStyle::FocalGradient {
gradient: Gradient {
matrix,
spread,
interpolation,
records,
},
focal_point: Fixed8::from_f64(focal_point),
}),
}
}
}
Ok(Value::Undefined)
}
/// Implements `Graphics.cubicCurveTo`
pub fn cubic_curve_to<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(this) = this.as_display_object() {
let control_a_x = args.get_f64(activation, 0)?;
let control_a_y = args.get_f64(activation, 1)?;
let control_b_x = args.get_f64(activation, 2)?;
let control_b_y = args.get_f64(activation, 3)?;
let anchor_x = args.get_f64(activation, 4)?;
let anchor_y = args.get_f64(activation, 5)?;
if let Some(mut draw) = this.as_drawing(activation.context.gc_context) {
draw.draw_command(DrawCommand::CubicCurveTo {
control_a: Point::from_pixels(control_a_x, control_a_y),
control_b: Point::from_pixels(control_b_x, control_b_y),
anchor: Point::from_pixels(anchor_x, anchor_y),
});
}
}
Ok(Value::Undefined)
}
/// Implements `Graphics.copyFrom`
pub fn copy_from<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(this) = this.as_display_object() {
let source = args
.get_object(activation, 0, "sourceGraphics")?
.as_display_object()
.expect("Bad sourceGraphics");
let source = source
.as_drawing(activation.context.gc_context)
.expect("Missing drawing for sourceGraphics");
let mut target_drawing = this
.as_drawing(activation.context.gc_context)
.expect("Missing drawing for target");
target_drawing.copy_from(&source);
}
Ok(Value::Undefined)
}
/// Implements `Graphics.drawPath`
pub fn draw_path<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
let this = this.as_display_object().unwrap();
let mut drawing = this.as_drawing(activation.context.gc_context).unwrap();
let commands = args.get_object(activation, 0, "commands")?;
let data = args.get_object(activation, 1, "data")?;
// FIXME - implement winding, and fill behavior described in the Flash docs
// (which is different from just running each command sequentially on `Graphics`)
let _winding = args.get_string(activation, 2)?;
avm2_stub_method!(
activation,
"flash.display.Graphics",
"drawPath",
"winding and fill behavior"
);
let commands = commands
.as_vector_storage()
.expect("commands is not a Vector");
let data = data.as_vector_storage().expect("data is not a Vector");
process_commands(activation, &mut drawing, &commands, &data)?;
Ok(Value::Undefined)
}
/// Implements `Graphics.drawRoundRectComplex`
pub fn draw_round_rect_complex<'gc>(
activation: &mut Activation<'_, 'gc>,
_this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
avm2_stub_method!(activation, "flash.display.Graphics", "drawRoundRectComplex");
Ok(Value::Undefined)
}
/// Implements `Graphics.drawTriangles`
pub fn draw_triangles<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(this) = this.as_display_object() {
if let Some(mut drawing) = this.as_drawing(activation.context.gc_context) {
let vertices = args.get_object(activation, 0, "vertices")?;
let indices = args.try_get_object(activation, 1);
let uvt_data = args.try_get_object(activation, 2);
if uvt_data.is_some() {
avm2_stub_method!(
activation,
"flash.display.Graphics",
"drawTriangles",
"with uvt data"
);
}
let culling = {
let culling = args.get_string(activation, 3)?;
culling_to_triangle_culling(activation, culling)?
};
draw_triangles_internal(
activation,
&mut drawing,
&vertices,
indices.as_ref(),
uvt_data.as_ref(),
culling,
)?;
}
}
Ok(Value::Undefined)
}
fn draw_triangles_internal<'gc>(
activation: &mut Activation<'_, 'gc>,
drawing: &mut Drawing,
vertices: &Object<'gc>,
indices: Option<&Object<'gc>>,
_uvt_data: Option<&Object<'gc>>,
culling: TriangleCulling,
) -> Result<(), Error<'gc>> {
let vertices = vertices
.as_vector_storage()
.expect("vertices is not a Vector");
if let Some(indices) = indices {
let indices = indices
.as_vector_storage()
.expect("indices is not a Vector");
fn read_point<'gc>(
vertices: &VectorStorage<'gc>,
index: usize,
activation: &mut Activation<'_, 'gc>,
) -> Result<Point<Twips>, Error<'gc>> {
let x = {
let x = vertices
.get(2 * index, activation)?
.coerce_to_number(activation)?;
Twips::from_pixels(x)
};
let y = {
let y = vertices
.get(2 * index + 1, activation)?
.coerce_to_number(activation)?;
Twips::from_pixels(y)
};
Ok(Point::new(x, y))
}
fn next_triangle<'gc>(
vertices: &VectorStorage<'gc>,
indices: &mut impl Iterator<Item = Value<'gc>>,
activation: &mut Activation<'_, 'gc>,
) -> Result<Option<Triangle>, Error<'gc>> {
match (indices.next(), indices.next(), indices.next()) {
(Some(i0), Some(i1), Some(i2)) => {
let i0 = i0.coerce_to_u32(activation)? as usize;
let i1 = i1.coerce_to_u32(activation)? as usize;
let i2 = i2.coerce_to_u32(activation)? as usize;
let p0 = read_point(vertices, i0, activation)?;
let p1 = read_point(vertices, i1, activation)?;
let p2 = read_point(vertices, i2, activation)?;
Ok(Some((p0, p1, p2)))
}
_ => Ok(None),
}
}