forked from ruffle-rs/ruffle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
focus_tracker.rs
210 lines (179 loc) · 7.09 KB
/
focus_tracker.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
use crate::avm1::Avm1;
use crate::avm1::Value;
use crate::context::{RenderContext, UpdateContext};
pub use crate::display_object::{
DisplayObject, TDisplayObject, TDisplayObjectContainer, TextSelection,
};
use crate::drawing::Drawing;
use either::Either;
use gc_arena::barrier::unlock;
use gc_arena::lock::Lock;
use gc_arena::{Collect, Gc, Mutation};
use ruffle_render::shape_utils::DrawCommand;
use std::cell::RefCell;
use swf::{Color, LineJoinStyle, Point, Twips};
#[derive(Collect)]
#[collect(no_drop)]
pub struct FocusTrackerData<'gc> {
focus: Lock<Option<DisplayObject<'gc>>>,
highlight: RefCell<Highlight>,
}
enum Highlight {
Inactive,
Active(Drawing),
}
#[derive(Clone, Copy, Collect)]
#[collect(no_drop)]
pub struct FocusTracker<'gc>(Gc<'gc, FocusTrackerData<'gc>>);
impl<'gc> FocusTracker<'gc> {
const HIGHLIGHT_WIDTH: Twips = Twips::from_pixels_i32(3);
const HIGHLIGHT_COLOR: Color = Color::YELLOW;
// Although at 3px width Round and Miter are similar
// to each other, it seems that FP uses Round.
const HIGHLIGHT_LINE_JOIN_STYLE: LineJoinStyle = LineJoinStyle::Round;
pub fn new(mc: &Mutation<'gc>) -> Self {
Self(Gc::new(
mc,
FocusTrackerData {
focus: Lock::new(None),
highlight: RefCell::new(Highlight::Inactive),
},
))
}
pub fn reset_highlight(&self) {
self.0.highlight.replace(Highlight::Inactive);
}
pub fn get(&self) -> Option<DisplayObject<'gc>> {
self.0.focus.get()
}
pub fn set(
&self,
focused_element: Option<DisplayObject<'gc>>,
context: &mut UpdateContext<'_, 'gc>,
) {
let old = self.0.focus.get();
// Check if the focused element changed.
if old.map(|o| o.as_ptr()) != focused_element.map(|o| o.as_ptr()) {
let focus = unlock!(Gc::write(context.gc(), self.0), FocusTrackerData, focus);
focus.set(focused_element);
// The highlight always follows the focus.
self.update_highlight();
if let Some(old) = old {
old.on_focus_changed(context, false, focused_element);
}
if let Some(new) = focused_element {
new.on_focus_changed(context, true, old);
}
tracing::info!("Focus is now on {:?}", focused_element);
if let Some(level0) = context.stage.root_clip() {
Avm1::notify_system_listeners(
level0,
context,
"Selection".into(),
"onSetFocus".into(),
&[
old.map(|v| v.object()).unwrap_or(Value::Null),
focused_element.map(|v| v.object()).unwrap_or(Value::Null),
],
);
}
}
// This applies even if the focused element hasn't changed.
if let Some(text_field) = focused_element.and_then(|e| e.as_edit_text()) {
if text_field.is_editable() {
if !text_field.movie().is_action_script_3() {
let length = text_field.text_length();
text_field
.set_selection(Some(TextSelection::for_range(0, length)), context.gc());
}
context.ui.open_virtual_keyboard();
}
}
}
pub fn cycle(&self, context: &mut UpdateContext<'_, 'gc>, reverse: bool) {
let stage = context.stage;
let mut tab_order = vec![];
stage.fill_tab_order(&mut tab_order, context);
let custom_ordering = tab_order.iter().any(|o| o.tab_index().is_some());
if custom_ordering {
Self::order_custom(&mut tab_order);
} else {
Self::order_automatic(&mut tab_order);
};
let mut tab_order = if reverse {
Either::Left(tab_order.iter().rev())
} else {
Either::Right(tab_order.iter())
}
.peekable();
let first = tab_order.peek().copied();
let next = if let Some(current_focus) = self.get() {
// Find the next object which should take the focus.
tab_order
.skip_while(|o| o.as_ptr() != current_focus.as_ptr())
.nth(1)
.or(first)
} else {
// If no focus is present, we start from the beginning.
first
};
if next.is_some() {
self.set(next.copied(), context);
self.update_highlight();
}
}
fn update_highlight(&self) {
self.0.highlight.replace(self.redraw_highlight());
}
fn redraw_highlight(&self) -> Highlight {
let Some(focus) = self.get() else {
return Highlight::Inactive;
};
if !focus.is_highlight_enabled() {
return Highlight::Inactive;
}
let bounds = focus.world_bounds().grow(-Self::HIGHLIGHT_WIDTH / 2);
let mut drawing = Drawing::new();
drawing.set_line_style(Some(
swf::LineStyle::new()
.with_width(Self::HIGHLIGHT_WIDTH)
.with_color(Self::HIGHLIGHT_COLOR)
.with_join_style(Self::HIGHLIGHT_LINE_JOIN_STYLE),
));
drawing.draw_command(DrawCommand::MoveTo(Point::new(bounds.x_min, bounds.y_min)));
drawing.draw_command(DrawCommand::LineTo(Point::new(bounds.x_min, bounds.y_max)));
drawing.draw_command(DrawCommand::LineTo(Point::new(bounds.x_max, bounds.y_max)));
drawing.draw_command(DrawCommand::LineTo(Point::new(bounds.x_max, bounds.y_min)));
drawing.draw_command(DrawCommand::LineTo(Point::new(bounds.x_min, bounds.y_min)));
Highlight::Active(drawing)
}
pub fn render_highlight(&self, context: &mut RenderContext<'_, 'gc>) {
if let Highlight::Active(ref highlight) = *self.0.highlight.borrow() {
highlight.render(context);
};
}
fn order_custom(tab_order: &mut Vec<DisplayObject>) {
// Custom ordering disables automatic ordering and
// ignores all objects without tabIndex.
tab_order.retain(|o| o.tab_index().is_some());
// Then, items are sorted according to their tab indices.
// TODO When two objects have the same index, the behavior is undefined.
// We should analyze and match FP's behavior here if possible.
tab_order.sort_by_key(|o| o.tab_index());
}
// TODO This ordering is yet far from being perfect.
// FP actually has some weird ordering logic, which
// sometimes jumps up, sometimes even ignores some objects.
fn order_automatic(tab_order: &mut Vec<DisplayObject>) {
fn key_extractor(o: &DisplayObject) -> (Twips, Twips) {
let bounds = o.world_bounds();
(bounds.y_min, bounds.x_min)
}
// The automatic order is mainly dependent on
// the position of the top-left bound corner.
tab_order.sort_by_cached_key(key_extractor);
// Duplicated positions are removed,
// only the first element is retained.
tab_order.dedup_by_key(|o| key_extractor(o));
}
}