Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text: Introduce layout coordinate space and transforms #18448

Merged
merged 5 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions core/src/display_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -969,15 +969,9 @@ pub fn render_base<'gc>(this: DisplayObject<'gc>, context: &mut RenderContext<'_
true,
&context.stage.view_matrix(),
);
context.commands.draw_rect(
background,
Matrix::create_box(
bounds.width().to_pixels() as f32,
bounds.height().to_pixels() as f32,
bounds.x_min,
bounds.y_min,
),
);
context
.commands
.draw_rect(background, Matrix::create_box_from_rectangle(&bounds));
}
apply_standard_mask_and_scroll(this, context, |context| this.render_self(context));
}
Expand Down
51 changes: 26 additions & 25 deletions core/src/display_object/edit_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,28 @@ impl<'gc> EditText<'gc> {
.set(flag, value);
}

/// Returns the matrix for transforming from layout
/// coordinate space into this object's local space.
fn layout_to_local_matrix(self, data: &EditTextData) -> Matrix {
Matrix::translate(
data.bounds.x_min + Self::GUTTER - Twips::from_pixels(data.hscroll),
data.bounds.y_min + Self::GUTTER - data.vertical_scroll_offset(),
)
}

/// Returns the matrix for transforming from this object's
/// local space into its layout coordinate space.
fn local_to_layout_matrix(self, data: &EditTextData) -> Matrix {
// layout_to_local contains only a translation,
// no need to inverse the matrix generically.
let Matrix { tx, ty, .. } = self.layout_to_local_matrix(data);
Matrix::translate(-tx, -ty)
}

fn local_to_layout(&self, data: &EditTextData, local: Point<Twips>) -> Point<Twips> {
self.local_to_layout_matrix(data) * local
}

pub fn replace_text(
self,
from: usize,
Expand Down Expand Up @@ -1316,11 +1338,8 @@ impl<'gc> EditText<'gc> {

pub fn screen_position_to_index(self, position: Point<Twips>) -> Option<usize> {
let text = self.0.read();
let mut position = self.global_to_local(position)?;
position.x += Twips::from_pixels(text.hscroll) - Self::GUTTER;
position.y += text.vertical_scroll_offset() - Self::GUTTER;
position.x -= text.bounds.x_min;
position.y -= text.bounds.y_min;
let position = self.global_to_local(position)?;
let position = self.local_to_layout(&text, position);

// TODO We can use binary search for both y and x here

Expand Down Expand Up @@ -2329,32 +2348,16 @@ impl<'gc> TDisplayObject<'gc> for EditText<'gc> {
}
}

context.transform_stack.push(&Transform {
matrix: Matrix::translate(edit_text.bounds.x_min, edit_text.bounds.y_min),
..Default::default()
});

context.commands.push_mask();
let mask = Matrix::create_box(
edit_text.bounds.width().to_pixels() as f32,
edit_text.bounds.height().to_pixels() as f32,
Twips::ZERO,
Twips::ZERO,
);
let mask = Matrix::create_box_from_rectangle(&edit_text.bounds);
context.commands.draw_rect(
Color::WHITE,
context.transform_stack.transform().matrix * mask,
);
context.commands.activate_mask();

let scroll_offset = edit_text.vertical_scroll_offset();
// TODO: Where does this come from? How is this different than INTERNAL_PADDING? Does this apply to y as well?
// If this is actually right, offset the border in `redraw_border` instead of doing an extra push.
context.transform_stack.push(&Transform {
matrix: Matrix::translate(
Self::GUTTER - Twips::from_pixels(edit_text.hscroll),
Self::GUTTER - scroll_offset,
),
matrix: self.layout_to_local_matrix(&edit_text),
..Default::default()
});

Expand All @@ -2378,8 +2381,6 @@ impl<'gc> TDisplayObject<'gc> for EditText<'gc> {
context.transform_stack.transform().matrix * mask,
);
context.commands.pop_mask();

context.transform_stack.pop();
}

fn allow_as_mask(&self) -> bool {
Expand Down
9 changes: 9 additions & 0 deletions render/src/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ impl Matrix {
}
}

pub fn create_box_from_rectangle(rect: &Rectangle<Twips>) -> Self {
Self::create_box(
rect.width().to_pixels() as f32,
rect.height().to_pixels() as f32,
rect.x_min,
rect.y_min,
)
}

pub fn create_gradient_box(
width: f32,
height: f32,
Expand Down