forked from ruffle-rs/ruffle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive.rs
More file actions
861 lines (757 loc) · 30.4 KB
/
interactive.rs
File metadata and controls
861 lines (757 loc) · 30.4 KB
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
//! Interactive object enumtrait
use crate::avm1::Avm1;
use crate::avm1::Value as Avm1Value;
use crate::avm2::activation::Activation as Avm2Activation;
use crate::avm2::{Avm2, EventObject as Avm2EventObject, EventObject, Value as Avm2Value};
use crate::backend::ui::MouseCursor;
use crate::context::UpdateContext;
use crate::display_object::avm1_button::Avm1Button;
use crate::display_object::avm2_button::Avm2Button;
use crate::display_object::container::DisplayObjectContainer;
use crate::display_object::edit_text::EditText;
use crate::display_object::loader_display::LoaderDisplay;
use crate::display_object::movie_clip::MovieClip;
use crate::display_object::stage::Stage;
use crate::display_object::{
BoundsMode, DisplayObject, DisplayObjectBase, TDisplayObject, TDisplayObjectContainer,
};
use crate::events::{ClipEvent, ClipEventResult, MouseButton};
use crate::string::AvmString;
use bitflags::bitflags;
use either::Either;
use gc_arena::barrier::unlock;
use gc_arena::lock::Lock;
use gc_arena::{Collect, Gc, Mutation};
use ruffle_common::utils::HasPrefixField;
use ruffle_macros::{enum_trait_object, istr};
use std::cell::Cell;
use std::fmt::Debug;
use swf::{Point, Rectangle, Twips};
/// Find the lowest common ancestor between the display objects in `from` and
/// `to`.
///
/// If no such common ancestor exists, this returns `None`.
fn lowest_common_ancestor<'gc>(
from: DisplayObject<'gc>,
to: DisplayObject<'gc>,
) -> Option<DisplayObject<'gc>> {
let mut from_parents = vec![];
let mut us = Some(from);
while let Some(parent) = us {
from_parents.push(parent);
us = parent.parent();
}
let mut to_parents = vec![];
let mut them = Some(to);
while let Some(parent) = them {
to_parents.push(parent);
them = parent.parent();
}
let mut hca = None;
for (us_parent, them_parent) in from_parents
.into_iter()
.rev()
.zip(to_parents.into_iter().rev())
{
if DisplayObject::ptr_eq(us_parent, them_parent) {
hca = Some(us_parent);
} else {
break;
}
}
hca
}
bitflags! {
/// Boolean state flags used by `InteractiveObject`.
#[derive(Clone, Copy)]
struct InteractiveObjectFlags: u8 {
/// Whether this `InteractiveObject` accepts mouse and other user
/// events.
const MOUSE_ENABLED = 1 << 0;
/// Whether this `InteractiveObject` accepts double-clicks.
const DOUBLE_CLICK_ENABLED = 1 << 1;
/// Whether this `InteractiveObject` is currently focused.
const HAS_FOCUS = 1 << 2;
}
}
#[derive(Collect, Clone, HasPrefixField)]
#[collect(no_drop)]
#[repr(C, align(8))]
pub struct InteractiveObjectBase<'gc> {
pub base: DisplayObjectBase<'gc>,
context_menu: Lock<Avm2Value<'gc>>,
#[collect(require_static)]
tab_index: Cell<Option<i32>>,
#[collect(require_static)]
flags: Cell<InteractiveObjectFlags>,
#[collect(require_static)]
tab_enabled: Cell<Option<bool>>,
/// Specifies whether this object displays a yellow rectangle when focused.
focus_rect: Cell<Option<bool>>,
}
impl Default for InteractiveObjectBase<'_> {
fn default() -> Self {
Self {
base: Default::default(),
flags: Cell::new(InteractiveObjectFlags::MOUSE_ENABLED),
context_menu: Lock::new(Avm2Value::Null),
tab_enabled: Cell::new(None),
tab_index: Cell::new(None),
focus_rect: Cell::new(None),
}
}
}
impl<'gc> InteractiveObjectBase<'gc> {
fn contains_flag(&self, flag: InteractiveObjectFlags) -> bool {
self.flags.get().contains(flag)
}
fn set_flag(&self, flag: InteractiveObjectFlags, value: bool) {
let mut flags = self.flags.get();
flags.set(flag, value);
self.flags.set(flags);
}
}
#[enum_trait_object(
#[derive(Clone, Collect, Copy, Debug)]
#[collect(no_drop)]
pub enum InteractiveObject<'gc> {
Stage(Stage<'gc>),
Avm1Button(Avm1Button<'gc>),
Avm2Button(Avm2Button<'gc>),
MovieClip(MovieClip<'gc>),
EditText(EditText<'gc>),
LoaderDisplay(LoaderDisplay<'gc>),
}
)]
pub trait TInteractiveObject<'gc>:
'gc + Clone + Copy + Collect<'gc> + Debug + Into<InteractiveObject<'gc>>
{
fn raw_interactive(self) -> Gc<'gc, InteractiveObjectBase<'gc>>;
fn as_displayobject(self) -> DisplayObject<'gc>;
/// Check if the interactive object accepts user input.
#[no_dynamic]
fn mouse_enabled(self) -> bool {
self.raw_interactive()
.contains_flag(InteractiveObjectFlags::MOUSE_ENABLED)
}
/// Set if the interactive object accepts user input.
#[no_dynamic]
fn set_mouse_enabled(self, value: bool) {
self.raw_interactive()
.set_flag(InteractiveObjectFlags::MOUSE_ENABLED, value)
}
/// Check if the interactive object accepts double-click events.
#[no_dynamic]
fn double_click_enabled(self) -> bool {
self.raw_interactive()
.contains_flag(InteractiveObjectFlags::DOUBLE_CLICK_ENABLED)
}
// Set if the interactive object accepts double-click events.
#[no_dynamic]
fn set_double_click_enabled(self, value: bool) {
self.raw_interactive()
.set_flag(InteractiveObjectFlags::DOUBLE_CLICK_ENABLED, value)
}
#[no_dynamic]
fn has_focus(self) -> bool {
self.raw_interactive()
.contains_flag(InteractiveObjectFlags::HAS_FOCUS)
}
#[no_dynamic]
fn set_has_focus(self, value: bool) {
self.raw_interactive()
.set_flag(InteractiveObjectFlags::HAS_FOCUS, value)
}
#[no_dynamic]
fn context_menu(self) -> Avm2Value<'gc> {
self.raw_interactive().context_menu.get()
}
#[no_dynamic]
fn set_context_menu(self, mc: &Mutation<'gc>, value: Avm2Value<'gc>) {
let write = Gc::write(mc, self.raw_interactive());
unlock!(write, InteractiveObjectBase, context_menu).set(value);
}
/// Get the boolean flag which determines whether objects display a glowing border
/// when they have focus.
#[no_dynamic]
fn focus_rect(self) -> Option<bool> {
self.raw_interactive().focus_rect.get()
}
/// Set the boolean flag which determines whether objects display a glowing border
/// when they have focus.
#[no_dynamic]
fn set_focus_rect(self, value: Option<bool>) {
self.raw_interactive().focus_rect.set(value);
}
/// Filter the incoming clip event.
///
/// If this returns `Handled`, then the rest of the event handling
/// machinery should run. Otherwise, the event will not be handled, neither
/// by this interactive object nor it's children. The event will be passed
/// onto other siblings of the display object instead.
fn filter_clip_event(
self,
_context: &mut UpdateContext<'gc>,
event: ClipEvent,
) -> ClipEventResult;
/// Propagate the event to children.
///
/// If this function returns `Handled`, then further event processing will
/// terminate, including the event default.
fn propagate_to_children(
self,
context: &mut UpdateContext<'gc>,
event: ClipEvent<'gc>,
) -> ClipEventResult {
if event.propagates()
&& let Some(container) = self.as_displayobject().as_container()
{
// Mouse events fire in reverse order (high depth to low depth).
// Button and key events fire in render list order (low depth to high depth).
let children = if event.is_mouse_event() {
Either::Left(container.iter_render_list().rev())
} else {
Either::Right(container.iter_render_list())
};
for child in children {
if let Some(interactive) = child.as_interactive()
&& interactive.handle_clip_event(context, event) == ClipEventResult::Handled
{
return ClipEventResult::Handled;
}
}
}
ClipEventResult::NotHandled
}
/// Dispatch the event to script event handlers.
///
/// This function only runs if the clip event has not been filtered and
/// none of the interactive object's children handled the event. It
/// ultimately determines if this display object will handle the event, or
/// if the event will be passed onto siblings and parents.
fn event_dispatch(
self,
_context: &mut UpdateContext<'gc>,
_event: ClipEvent<'gc>,
) -> ClipEventResult;
/// Convert the clip event into an AVM2 event and dispatch it into the
/// AVM2 side of this object.
///
/// This is only intended to be called for events defined by
/// `InteractiveObject` itself. Display object impls that have their own
/// event types should dispatch them in `event_dispatch`.
#[no_dynamic]
fn event_dispatch_to_avm2(
self,
context: &mut UpdateContext<'gc>,
event: ClipEvent<'gc>,
) -> ClipEventResult {
if !self.as_displayobject().movie().is_action_script_3() {
return ClipEventResult::NotHandled;
}
// Flash appears to not fire events *at all* for a targeted EditText
// that was originally created by the timeline. Normally, one of the ancestors
// of the TextField would get targeted, but instead, the event isn't fired
// (not even the Stage receives the event)
if let Some(text) = self.as_displayobject().as_edit_text()
&& text.is_selectable()
&& text.was_static()
{
return ClipEventResult::NotHandled;
}
let target = if let Some(target) = self.as_displayobject().object2() {
target.into()
} else {
return ClipEventResult::NotHandled;
};
let mut activation = Avm2Activation::from_nothing(context);
match event {
ClipEvent::Press { .. } | ClipEvent::RightPress | ClipEvent::MiddlePress => {
let button = match event {
ClipEvent::Press { .. } => MouseButton::Left,
ClipEvent::RightPress => MouseButton::Right,
ClipEvent::MiddlePress => MouseButton::Middle,
_ => unreachable!(),
};
let avm2_event = Avm2EventObject::mouse_event_down(
&mut activation,
self.as_displayobject(),
button,
);
let handled = Avm2::dispatch_event(activation.context, avm2_event, target);
if handled {
ClipEventResult::Handled
} else {
// When there are any click handlers, the down event is considered handled.
let avm2_event = Avm2EventObject::mouse_event_click(
&mut activation,
self.as_displayobject(),
button,
);
Avm2::simulate_event_dispatch(activation.context, avm2_event, target).into()
}
}
ClipEvent::MouseUpInside
| ClipEvent::RightMouseUpInside
| ClipEvent::MiddleMouseUpInside => {
let avm2_event = Avm2EventObject::mouse_event_up(
&mut activation,
self.as_displayobject(),
match event {
ClipEvent::MouseUpInside => MouseButton::Left,
ClipEvent::RightMouseUpInside => MouseButton::Right,
ClipEvent::MiddleMouseUpInside => MouseButton::Middle,
_ => unreachable!(),
},
);
Avm2::dispatch_event(activation.context, avm2_event, target).into()
}
ClipEvent::Release { index } => {
let is_double_click = index % 2 != 0;
let double_click_enabled = self
.raw_interactive()
.contains_flag(InteractiveObjectFlags::DOUBLE_CLICK_ENABLED);
if is_double_click && double_click_enabled {
let string_double_click = istr!("doubleClick");
let avm2_event = Avm2EventObject::mouse_event(
&mut activation,
string_double_click,
self.as_displayobject(),
None,
0,
true,
MouseButton::Left,
);
Avm2::dispatch_event(activation.context, avm2_event, target).into()
} else {
let avm2_event = Avm2EventObject::mouse_event_click(
&mut activation,
self.as_displayobject(),
MouseButton::Left,
);
Avm2::dispatch_event(activation.context, avm2_event, target).into()
}
}
ClipEvent::RightRelease | ClipEvent::MiddleRelease => {
let avm2_event = Avm2EventObject::mouse_event_click(
&mut activation,
self.as_displayobject(),
match event {
ClipEvent::RightRelease => MouseButton::Right,
ClipEvent::MiddleRelease => MouseButton::Middle,
_ => unreachable!(),
},
);
Avm2::dispatch_event(activation.context, avm2_event, target).into()
}
ClipEvent::ReleaseOutside => {
let string_release_outside = istr!("releaseOutside");
let avm2_event = Avm2EventObject::mouse_event(
&mut activation,
string_release_outside,
self.as_displayobject(),
None,
0,
true,
MouseButton::Left,
);
Avm2::dispatch_event(activation.context, avm2_event, target).into()
}
ClipEvent::RollOut { to } | ClipEvent::DragOut { to } => {
let string_mouse_out = istr!("mouseOut");
let avm2_event = Avm2EventObject::mouse_event(
&mut activation,
string_mouse_out,
self.as_displayobject(),
to,
0,
true,
MouseButton::Left,
);
let mut handled = Avm2::dispatch_event(activation.context, avm2_event, target);
let lca = lowest_common_ancestor(
self.as_displayobject(),
to.map(|t| t.as_displayobject())
.unwrap_or_else(|| activation.context.stage.into()),
);
let mut rollout_target = Some(self.as_displayobject());
while let Some(tgt) = rollout_target {
if DisplayObject::option_ptr_eq(rollout_target, lca) {
break;
}
let string_roll_out = istr!("rollOut");
let avm2_event = Avm2EventObject::mouse_event(
&mut activation,
string_roll_out,
tgt,
to,
0,
false,
MouseButton::Left,
);
if let Some(avm2_target) = tgt.object2() {
handled = Avm2::dispatch_event(
activation.context,
avm2_event,
avm2_target.into(),
) || handled;
}
rollout_target = tgt.parent();
}
handled.into()
}
ClipEvent::RollOver { from } | ClipEvent::DragOver { from } => {
let lca = lowest_common_ancestor(
self.as_displayobject(),
from.map(|t| t.as_displayobject())
.unwrap_or_else(|| activation.context.stage.into()),
);
let mut handled = false;
let mut rollover_target = Some(self.as_displayobject());
while let Some(tgt) = rollover_target {
if DisplayObject::option_ptr_eq(rollover_target, lca) {
break;
}
let string_roll_over = istr!("rollOver");
let avm2_event = Avm2EventObject::mouse_event(
&mut activation,
string_roll_over,
tgt,
from,
0,
false,
MouseButton::Left,
);
if let Some(avm2_target) = tgt.object2() {
handled = Avm2::dispatch_event(
activation.context,
avm2_event,
avm2_target.into(),
) || handled;
}
rollover_target = tgt.parent();
}
let string_mouse_over = istr!("mouseOver");
let avm2_event = Avm2EventObject::mouse_event(
&mut activation,
string_mouse_over,
self.as_displayobject(),
from,
0,
true,
MouseButton::Left,
);
handled = Avm2::dispatch_event(activation.context, avm2_event, target) || handled;
handled.into()
}
ClipEvent::MouseWheel { delta } => {
let string_mouse_wheel = istr!("mouseWheel");
let avm2_event = Avm2EventObject::mouse_event(
&mut activation,
string_mouse_wheel,
self.as_displayobject(),
None,
delta.lines() as i32,
true,
MouseButton::Left,
);
Avm2::dispatch_event(activation.context, avm2_event, target).into()
}
ClipEvent::MouseMoveInside => {
let string_mouse_move = istr!("mouseMove");
let avm2_event = Avm2EventObject::mouse_event(
&mut activation,
string_mouse_move,
self.as_displayobject(),
None,
0,
true,
MouseButton::Left,
);
Avm2::dispatch_event(activation.context, avm2_event, target).into()
}
_ => ClipEventResult::NotHandled,
}
}
/// Executes and propagates the given clip event.
/// Events execute inside-out; the deepest child will react first, followed
/// by its parent, and so forth.
#[no_dynamic]
fn handle_clip_event(
self,
context: &mut UpdateContext<'gc>,
event: ClipEvent<'gc>,
) -> ClipEventResult {
if !self.mouse_enabled() {
return ClipEventResult::NotHandled;
}
if self.filter_clip_event(context, event) == ClipEventResult::NotHandled {
return ClipEventResult::NotHandled;
}
if self.propagate_to_children(context, event) == ClipEventResult::Handled {
return ClipEventResult::Handled;
}
self.event_dispatch(context, event)
}
/// Determine the bottom-most interactive display object under the given
/// mouse cursor.
///
/// Only objects capable of handling mouse input should flag themselves as
/// mouse-pickable, as doing so will make them eligible to receive targeted
/// mouse events. As a result of this, the returned object will always be
/// an `InteractiveObject`.
fn mouse_pick_avm1(
self,
_context: &mut UpdateContext<'gc>,
_point: Point<Twips>,
_require_button_mode: bool,
) -> Option<InteractiveObject<'gc>> {
None
}
fn mouse_pick_avm2(
self,
_context: &mut UpdateContext<'gc>,
_point: Point<Twips>,
_require_button_mode: bool,
) -> Avm2MousePick<'gc> {
Avm2MousePick::Miss
}
/// The cursor to use when this object is the hovered element under a mouse.
fn mouse_cursor(self, _context: &mut UpdateContext<'gc>) -> MouseCursor {
MouseCursor::Hand
}
/// Whether this object is focusable for keyboard input.
fn is_focusable(self, _context: &mut UpdateContext<'gc>) -> bool {
// By default, all interactive objects are focusable.
true
}
/// Whether this object is focusable using a pointer device,
/// i.e. whether the focus should be updated when it's clicked.
///
/// The default behavior is following:
/// * in AVM1 objects cannot be focused by mouse,
/// * in AVM2 objects can be focused by mouse when they are tab enabled.
fn is_focusable_by_mouse(self, context: &mut UpdateContext<'gc>) -> bool {
let self_do = self.as_displayobject();
self_do.movie().is_action_script_3() && self.tab_enabled(context)
}
/// Called whenever the focus tracker has deemed this display object worthy, or no longer worthy,
/// of being the currently focused object.
/// This should only be called by the focus manager. To change a focus, go through that.
fn on_focus_changed(
self,
_context: &mut UpdateContext<'gc>,
_focused: bool,
_other: Option<InteractiveObject<'gc>>,
) {
}
/// If this object has focus, this method drops it.
#[no_dynamic]
fn drop_focus(self, context: &mut UpdateContext<'gc>) {
if self.has_focus() {
let tracker = context.focus_tracker;
tracker.set(None, context);
}
}
#[no_dynamic]
fn call_focus_handler(
self,
context: &mut UpdateContext<'gc>,
focused: bool,
other: Option<InteractiveObject<'gc>>,
) {
let self_do = self.as_displayobject();
if let Some(object) = self_do.object1() {
let other = other
.map(|d| d.as_displayobject().object1_or_null())
.unwrap_or(Avm1Value::Null);
let method_name = if focused {
AvmString::new_ascii_static(context.gc(), b"onSetFocus")
} else {
AvmString::new_ascii_static(context.gc(), b"onKillFocus")
};
Avm1::run_stack_frame_for_method(self_do, object, method_name, &[other], context);
} else if let Some(object) = self_do.object2() {
let mut activation = Avm2Activation::from_nothing(context);
let event_name = if focused { "focusIn" } else { "focusOut" };
let event = EventObject::focus_event(&mut activation, event_name, false, other, 0);
Avm2::dispatch_event(activation.context, event, object.into());
}
}
/// Whether this object may be highlighted when focused.
fn is_highlightable(self, context: &mut UpdateContext<'gc>) -> bool {
self.is_highlight_enabled(context)
}
/// Whether highlight is enabled for this object.
///
/// Note: This value does not mean that a highlight should actually be rendered,
/// for that see [`Self::is_highlightable()`].
#[no_dynamic]
fn is_highlight_enabled(self, context: &mut UpdateContext<'gc>) -> bool {
if self.as_displayobject().movie().version() >= 6 {
self.focus_rect()
.unwrap_or_else(|| context.stage.stage_focus_rect())
} else {
context.stage.stage_focus_rect()
}
}
/// Get the bounds of the focus highlight.
fn highlight_bounds(self) -> Rectangle<Twips> {
self.as_displayobject().world_bounds(BoundsMode::Engine)
}
/// Whether this object is included in tab ordering.
fn is_tabbable(self, context: &mut UpdateContext<'gc>) -> bool {
self.tab_enabled(context)
}
/// Sets whether tab ordering is enabled for this object.
///
/// Some objects may be excluded from tab ordering
/// even if it's enabled, see [`Self::is_tabbable()`].
#[no_dynamic]
fn tab_enabled(self, context: &mut UpdateContext<'gc>) -> bool {
if self.as_displayobject().movie().is_action_script_3() {
self.raw_interactive()
.tab_enabled
.get()
.unwrap_or_else(|| self.tab_enabled_default(context))
} else {
self.as_displayobject().get_avm1_boolean_property(
istr!(context, "tabEnabled"),
context,
|context| self.tab_enabled_default(context),
)
}
}
fn tab_enabled_default(self, _context: &mut UpdateContext<'gc>) -> bool {
false
}
#[no_dynamic]
fn set_tab_enabled(self, context: &mut UpdateContext<'gc>, value: bool) {
if self.as_displayobject().movie().is_action_script_3() {
self.raw_interactive().tab_enabled.set(Some(value))
} else {
self.as_displayobject().set_avm1_property(
istr!(context, "tabEnabled"),
value.into(),
context,
);
}
}
/// Used to customize tab ordering.
/// When not `None`, a custom ordering is used, and
/// objects are ordered according to this value.
#[no_dynamic]
fn tab_index(self) -> Option<i32> {
self.raw_interactive().tab_index.get()
}
#[no_dynamic]
fn set_tab_index(self, value: Option<i32>) {
// tabIndex = -1 is always equivalent to unset tabIndex
let value = if matches!(value, Some(-1)) {
None
} else {
value
};
self.raw_interactive().tab_index.set(value)
}
/// Whether event handlers (e.g. onKeyUp, onPress) should be fired for the given event.
#[no_dynamic]
fn should_fire_event_handlers(
self,
context: &mut UpdateContext<'gc>,
event: ClipEvent,
) -> bool {
// Event handlers are supported only by SWF6+.
if self.as_displayobject().movie().version() < 6 {
return false;
}
// Non-keyboard events are always handled.
if !event.is_key_event() {
return true;
}
// Keyboard events don't fire their methods unless the object has focus (#2120).
// The focus highlight also has to be active (see test focus_keyboard_press).
self.has_focus() && context.focus_tracker.highlight().is_active()
}
}
#[derive(Copy, Clone, Collect)]
#[collect(no_drop)]
pub enum Avm2MousePick<'gc> {
Hit(InteractiveObject<'gc>),
PropagateToParent,
Miss,
}
impl Debug for Avm2MousePick<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Avm2MousePick::Hit(target) => write!(f, "Hit({:?})", target.as_displayobject().name()),
Avm2MousePick::PropagateToParent => write!(f, "PropagateToParent"),
Avm2MousePick::Miss => write!(f, "Miss"),
}
}
}
impl<'gc> Avm2MousePick<'gc> {
/// Modifies this result to account for the parent's `mouseEnabled` and `mouseChildren` properties
#[must_use]
pub fn combine_with_parent(&self, parent: DisplayObjectContainer<'gc>) -> Avm2MousePick<'gc> {
let parent_int = DisplayObject::from(parent).as_interactive().unwrap();
match self {
Avm2MousePick::Hit(target) => {
// If the parent has `mouseChildren=true` then propagate the existing
// Avm2MousePick::Hit, leaving the target unchanged. This is unaffected
// by the parent `mouseEnabled` property.
// However, the root object of a loader or stage is never a valid target of hit
// events (even if moved out of the loader's hierarchy).
if parent.raw_container().mouse_children() && !target.as_displayobject().is_root() {
*self
// If the parent has `mouseChildren=false`, then the eventual
// MouseEvent (if it gets fired) will *not* have a `target`
// set to the original child.
} else {
// If the parent has `mouseChildren=false` and `mouseEnabled=true`,
// then the event from the child gets converted into an event
// targeting the parent - it 'absorbs' child events.
if parent_int.mouse_enabled() {
Avm2MousePick::Hit(parent_int)
// If the parent has `mouseChildren=false` and `mouseEnabled=true`,
// we have a weird case. The event can propagate through this 'fully disabled'
// parent - if it reaches an ancestor with `mouseEnabled=true`, it will get
// 'absorbed' by that ancestor. Otherwise, no event will be fired.
} else {
Avm2MousePick::PropagateToParent
}
}
}
Avm2MousePick::PropagateToParent => {
// If the parent has `mouseEnabled=true`, then 'absorb'
// the event that was propagated up from some child. Note that
// the `mouseChildren` setting plays no role here.
if parent_int.mouse_enabled() {
Avm2MousePick::Hit(parent_int)
// Otherwise, continue propagating the event up the tree.
} else {
Avm2MousePick::PropagateToParent
}
}
// A miss in a child always stays a miss, regardless of parent settings.
Avm2MousePick::Miss => Avm2MousePick::Miss,
}
}
}
impl<'gc> InteractiveObject<'gc> {
pub fn ptr_eq<T: TInteractiveObject<'gc>>(a: T, b: T) -> bool {
std::ptr::eq(a.as_displayobject().as_ptr(), b.as_displayobject().as_ptr())
}
pub fn option_ptr_eq(
a: Option<InteractiveObject<'gc>>,
b: Option<InteractiveObject<'gc>>,
) -> bool {
a.map(|o| o.as_displayobject().as_ptr()) == b.map(|o| o.as_displayobject().as_ptr())
}
}
impl PartialEq for InteractiveObject<'_> {
fn eq(&self, other: &Self) -> bool {
InteractiveObject::ptr_eq(*self, *other)
}
}
impl Eq for InteractiveObject<'_> {}