Skip to content

Commit 5be7f42

Browse files
adrian17Herschel
authored andcommitted
avm2: Migrate all Events to AS, remove EventData.
1 parent 9e96b07 commit 5be7f42

34 files changed

+582
-1741
lines changed

core/src/avm2.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use crate::avm2::class::AllocatorFn;
44
use crate::avm2::globals::SystemClasses;
55
use crate::avm2::method::{Method, NativeMethodImpl};
6-
use crate::avm2::object::EventObject;
76
use crate::avm2::script::{Script, TranslationUnit};
87
use crate::context::UpdateContext;
98
use crate::string::AvmString;
@@ -48,10 +47,10 @@ mod vtable;
4847
pub use crate::avm2::activation::Activation;
4948
pub use crate::avm2::array::ArrayStorage;
5049
pub use crate::avm2::domain::Domain;
51-
pub use crate::avm2::events::{Event, EventData};
5250
pub use crate::avm2::names::{Namespace, QName};
5351
pub use crate::avm2::object::{
54-
ArrayObject, ClassObject, Object, ScriptObject, SoundChannelObject, StageObject, TObject,
52+
ArrayObject, ClassObject, EventObject, Object, ScriptObject, SoundChannelObject, StageObject,
53+
TObject,
5554
};
5655
pub use crate::avm2::value::Value;
5756

@@ -156,16 +155,12 @@ impl<'gc> Avm2<'gc> {
156155
/// The `bool` parameter reads true if the event was cancelled.
157156
pub fn dispatch_event(
158157
context: &mut UpdateContext<'_, 'gc, '_>,
159-
event: Event<'gc>,
158+
event: Object<'gc>,
160159
target: Object<'gc>,
161160
) -> Result<bool, Error> {
162161
use crate::avm2::events::dispatch_event;
163-
164162
let mut activation = Activation::from_nothing(context.reborrow());
165-
166-
let event_object = EventObject::from_event(&mut activation, event)?;
167-
168-
dispatch_event(&mut activation, target, event_object)
163+
dispatch_event(&mut activation, target, event)
169164
}
170165

171166
/// Add an object to the broadcast list.
@@ -209,10 +204,12 @@ impl<'gc> Avm2<'gc> {
209204
/// new broadcast type, you must add it to the `BROADCAST_WHITELIST` first.
210205
pub fn broadcast_event(
211206
context: &mut UpdateContext<'_, 'gc, '_>,
212-
event: Event<'gc>,
207+
event: Object<'gc>,
213208
on_type: ClassObject<'gc>,
214209
) -> Result<(), Error> {
215-
let event_name = event.event_type();
210+
let base_event = event.as_event().unwrap(); // TODO: unwrap?
211+
let event_name = base_event.event_type();
212+
drop(base_event);
216213
if !BROADCAST_WHITELIST
217214
.iter()
218215
.any(|x| AvmString::from(*x) == event_name)
@@ -240,7 +237,7 @@ impl<'gc> Avm2<'gc> {
240237
let mut activation = Activation::from_nothing(context.reborrow());
241238

242239
if object.is_of_type(on_type, &mut activation)? {
243-
Avm2::dispatch_event(&mut activation.context, event.clone(), object)?;
240+
Avm2::dispatch_event(&mut activation.context, event, object)?;
244241
}
245242
}
246243
}

core/src/avm2/events.rs

Lines changed: 2 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ use crate::avm2::names::{Namespace, QName};
55
use crate::avm2::object::{Object, TObject};
66
use crate::avm2::value::Value;
77
use crate::avm2::Error;
8-
use crate::context::UpdateContext;
9-
use crate::display_object::{DisplayObject, InteractiveObject, TDisplayObject};
10-
use crate::events::KeyCode;
8+
use crate::display_object::TDisplayObject;
119
use crate::string::AvmString;
12-
use bitflags::bitflags;
1310
use fnv::FnvHashMap;
1411
use gc_arena::Collect;
1512
use std::collections::BTreeMap;
@@ -45,101 +42,6 @@ pub enum PropagationMode {
4542
StopImmediate,
4643
}
4744

48-
bitflags! {
49-
/// Keyboard modifiers.
50-
#[derive(Collect, Default)]
51-
#[collect(require_static)]
52-
pub struct KeyModifiers: u8 {
53-
const CTRL = 0b00000001;
54-
const ALT = 0b00000010;
55-
const SHIFT = 0b00000100;
56-
const COMMAND = 0b00001000;
57-
}
58-
}
59-
60-
impl KeyModifiers {
61-
fn from_current_keys<'gc>(context: &mut UpdateContext<'_, 'gc, '_>) -> Self {
62-
let mut keymods = KeyModifiers::default();
63-
64-
if context.input.is_key_down(KeyCode::Control) {
65-
keymods.insert(KeyModifiers::CTRL);
66-
}
67-
68-
if context.input.is_key_down(KeyCode::Alt) {
69-
keymods.insert(KeyModifiers::ALT);
70-
}
71-
72-
if context.input.is_key_down(KeyCode::Shift) {
73-
keymods.insert(KeyModifiers::SHIFT);
74-
}
75-
76-
//TODO: We don't have a UI keycode for ⌘.
77-
78-
keymods
79-
}
80-
}
81-
82-
/// The data for a dispatched event.
83-
///
84-
/// This roughly corresponds to properties provided on specific AS3 `Event`
85-
/// subclasses.
86-
#[derive(Clone, Collect, Debug)]
87-
#[collect(no_drop)]
88-
pub enum EventData<'gc> {
89-
Empty,
90-
Error {
91-
text: AvmString<'gc>,
92-
error_id: i32,
93-
},
94-
FullScreen {
95-
full_screen: bool,
96-
interactive: bool,
97-
},
98-
IOError {
99-
text: AvmString<'gc>,
100-
error_id: i32,
101-
},
102-
Mouse {
103-
local_x: f64,
104-
local_y: f64,
105-
movement_x: f64,
106-
movement_y: f64,
107-
related_object: Option<InteractiveObject<'gc>>,
108-
modifiers: KeyModifiers,
109-
button_down: bool,
110-
delta: i32,
111-
},
112-
SecurityError {
113-
text: AvmString<'gc>,
114-
error_id: i32,
115-
},
116-
Text {
117-
text: AvmString<'gc>,
118-
},
119-
}
120-
121-
impl<'gc> EventData<'gc> {
122-
pub fn mouse_event(
123-
context: &mut UpdateContext<'_, 'gc, '_>,
124-
target: DisplayObject<'gc>,
125-
related_object: Option<InteractiveObject<'gc>>,
126-
delta: i32,
127-
) -> Self {
128-
let local_pos = target.global_to_local(*context.mouse_position);
129-
130-
Self::Mouse {
131-
local_x: local_pos.0.to_pixels(),
132-
local_y: local_pos.1.to_pixels(),
133-
movement_x: 0.0, //TODO: Implement mouselocking.
134-
movement_y: 0.0,
135-
related_object,
136-
modifiers: KeyModifiers::from_current_keys(context),
137-
button_down: context.input.is_mouse_down(),
138-
delta,
139-
}
140-
}
141-
}
142-
14345
/// Represents data fields of an event that can be fired on an object that
14446
/// implements `IEventDispatcher`.
14547
#[derive(Clone, Collect, Debug)]
@@ -170,14 +72,11 @@ pub struct Event<'gc> {
17072

17173
/// The name of the event being triggered.
17274
event_type: AvmString<'gc>,
173-
174-
/// The event's data set.
175-
event_data: EventData<'gc>,
17675
}
17776

17877
impl<'gc> Event<'gc> {
17978
/// Construct a new event of a given type.
180-
pub fn new<S>(event_type: S, event_data: EventData<'gc>) -> Self
79+
pub fn new<S>(event_type: S) -> Self
18180
where
18281
S: Into<AvmString<'gc>>,
18382
{
@@ -190,7 +89,6 @@ impl<'gc> Event<'gc> {
19089
event_phase: EventPhase::AtTarget,
19190
target: None,
19291
event_type: event_type.into(),
193-
event_data,
19492
}
19593
}
19694

@@ -272,18 +170,6 @@ impl<'gc> Event<'gc> {
272170
pub fn set_current_target(&mut self, current_target: Object<'gc>) {
273171
self.current_target = Some(current_target)
274172
}
275-
276-
pub fn event_data(&self) -> &EventData<'gc> {
277-
&self.event_data
278-
}
279-
280-
pub fn event_data_mut(&mut self) -> &mut EventData<'gc> {
281-
&mut self.event_data
282-
}
283-
284-
pub fn set_event_data(&mut self, event_data: EventData<'gc>) {
285-
self.event_data = event_data;
286-
}
287173
}
288174

289175
/// A set of handlers organized by event type, priority, and order added.

core/src/avm2/globals.rs

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -452,11 +452,6 @@ pub fn load_player_globals<'gc>(
452452
flash::events::eventdispatcher::create_class(mc),
453453
script,
454454
)?;
455-
class(
456-
activation,
457-
flash::events::eventphase::create_class(mc),
458-
script,
459-
)?;
460455

461456
// package `flash.utils`
462457
avm2_system_class!(
@@ -691,59 +686,6 @@ pub fn load_player_globals<'gc>(
691686
// this call.
692687
load_playerglobal(activation, domain)?;
693688

694-
// These are event definitions, which need to be able to
695-
// load "flash.events.Event", which is defined in our playerglobal.
696-
// Therefore, they need to come after "load_playerglobal"
697-
// FIXME: Convert all of these event classes to ActionScript,
698-
// which will allow us to remove all of these calls.
699-
700-
avm2_system_class!(
701-
mouseevent,
702-
activation,
703-
flash::events::mouseevent::create_class(mc),
704-
script
705-
);
706-
avm2_system_class!(
707-
textevent,
708-
activation,
709-
flash::events::textevent::create_class(mc),
710-
script
711-
);
712-
avm2_system_class!(
713-
errorevent,
714-
activation,
715-
flash::events::errorevent::create_class(mc),
716-
script
717-
);
718-
avm2_system_class!(
719-
securityerrorevent,
720-
activation,
721-
flash::events::securityerrorevent::create_class(mc),
722-
script
723-
);
724-
avm2_system_class!(
725-
ioerrorevent,
726-
activation,
727-
flash::events::ioerrorevent::create_class(mc),
728-
script
729-
);
730-
class(
731-
activation,
732-
flash::events::keyboardevent::create_class(mc),
733-
script,
734-
)?;
735-
class(
736-
activation,
737-
flash::events::progressevent::create_class(mc),
738-
script,
739-
)?;
740-
avm2_system_class!(
741-
fullscreenevent,
742-
activation,
743-
flash::events::fullscreenevent::create_class(mc),
744-
script
745-
);
746-
747689
Ok(())
748690
}
749691

@@ -803,6 +745,12 @@ fn load_playerglobal<'gc>(
803745
[
804746
("flash.display", "Scene", scene),
805747
("flash.events", "Event", event),
748+
("flash.events", "TextEvent", textevent),
749+
("flash.events", "ErrorEvent", errorevent),
750+
("flash.events", "SecurityErrorEvent", securityerrorevent),
751+
("flash.events", "IOErrorEvent", ioerrorevent),
752+
("flash.events", "MouseEvent", mouseevent),
753+
("flash.events", "FullScreenEvent", fullscreenevent),
806754
]
807755
);
808756

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
//! `flash.events` namespace
22
3-
pub mod errorevent;
43
pub mod event;
54
pub mod eventdispatcher;
6-
pub mod eventphase;
7-
pub mod fullscreenevent;
85
pub mod ieventdispatcher;
9-
pub mod ioerrorevent;
10-
pub mod keyboardevent;
11-
pub mod mouseevent;
12-
pub mod progressevent;
13-
pub mod securityerrorevent;
14-
pub mod textevent;
6+
pub mod mouse_event;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package flash.events {
2+
public class ErrorEvent extends TextEvent {
3+
4+
public static const ERROR:String = "error";
5+
6+
private var _errorID:int;
7+
8+
public function ErrorEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false, text:String = "", id:int = 0)
9+
{
10+
super(type,bubbles,cancelable,text);
11+
this._errorID = id;
12+
}
13+
14+
public function get errorID() : int
15+
{
16+
return this._errorID;
17+
}
18+
19+
override public function clone() : Event
20+
{
21+
return new ErrorEvent(this.type,this.bubbles,this.cancelable,this.text,this._errorID);
22+
}
23+
24+
override public function toString() : String
25+
{
26+
return this.formatToString("ErrorEvent","type","bubbles","cancelable","eventPhase","text");
27+
}
28+
}
29+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package flash.events {
2+
public final class EventPhase {
3+
public static const CAPTURING_PHASE:uint = 1;
4+
public static const AT_TARGET:uint = 2;
5+
public static const BUBBLING_PHASE:uint = 3;
6+
}
7+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package flash.events {
2+
public class FullScreenEvent extends ActivityEvent {
3+
4+
public static const FULL_SCREEN:String = "fullScreen";
5+
public static const FULL_SCREEN_INTERACTIVE_ACCEPTED:String = "fullScreenInteractiveAccepted";
6+
7+
8+
private var _fullScreen:Boolean;
9+
private var _interactive:Boolean;
10+
11+
public function FullScreenEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false, fullScreen:Boolean = false, interactive:Boolean = false)
12+
{
13+
super(type,bubbles,cancelable);
14+
this._fullScreen = fullScreen;
15+
this._interactive = interactive;
16+
}
17+
18+
override public function clone() : Event
19+
{
20+
return new FullScreenEvent(this.type,this.bubbles,this.cancelable,this.fullScreen,this.interactive);
21+
}
22+
23+
override public function toString() : String
24+
{
25+
return this.formatToString("FullScreenEvent","type","bubbles","cancelable","eventPhase","fullScreen","interactive");
26+
}
27+
28+
public function get fullScreen() : Boolean
29+
{
30+
return this._fullScreen;
31+
}
32+
33+
public function get interactive() : Boolean
34+
{
35+
return this._interactive;
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)