Skip to content

Commit

Permalink
text: Store background and border colors as swf::Color
Browse files Browse the repository at this point in the history
Instead of `u32`.
  • Loading branch information
relrelb committed Sep 8, 2022
1 parent ece4b9a commit 7ec5873
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 56 deletions.
11 changes: 7 additions & 4 deletions core/src/avm1/globals/text_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::font::round_down_to_pixel;
use crate::html::TextFormat;
use crate::string::{AvmString, WStr};
use gc_arena::{GcCell, MutationContext};
use swf::Color;

macro_rules! tf_method {
($fn:expr) => {
Expand Down Expand Up @@ -396,7 +397,7 @@ pub fn background_color<'gc>(
this: EditText<'gc>,
_activation: &mut Activation<'_, 'gc, '_>,
) -> Result<Value<'gc>, Error<'gc>> {
Ok(this.background_color().into())
Ok(this.background_color().to_rgb().into())
}

pub fn set_background_color<'gc>(
Expand All @@ -405,7 +406,8 @@ pub fn set_background_color<'gc>(
value: Value<'gc>,
) -> Result<(), Error<'gc>> {
let rgb = value.coerce_to_u32(activation)?;
this.set_background_color(activation.context.gc_context, rgb & 0xFFFFFF);
let color = Color::from_rgb(rgb, 255);
this.set_background_color(activation.context.gc_context, color);
Ok(())
}

Expand All @@ -430,7 +432,7 @@ pub fn border_color<'gc>(
this: EditText<'gc>,
_activation: &mut Activation<'_, 'gc, '_>,
) -> Result<Value<'gc>, Error<'gc>> {
Ok(this.border_color().into())
Ok(this.border_color().to_rgb().into())
}

pub fn set_border_color<'gc>(
Expand All @@ -439,7 +441,8 @@ pub fn set_border_color<'gc>(
value: Value<'gc>,
) -> Result<(), Error<'gc>> {
let rgb = value.coerce_to_u32(activation)?;
this.set_border_color(activation.context.gc_context, rgb & 0xFFFFFF);
let color = Color::from_rgb(rgb, 255);
this.set_border_color(activation.context.gc_context, color);
Ok(())
}

Expand Down
21 changes: 11 additions & 10 deletions core/src/avm2/globals/flash/text/textfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::string::AvmString;
use crate::tag_utils::SwfMovie;
use gc_arena::{GcCell, MutationContext};
use std::sync::Arc;
use swf::Color;

/// Implements `flash.text.TextField`'s instance constructor.
pub fn instance_init<'gc>(
Expand Down Expand Up @@ -139,7 +140,7 @@ pub fn background_color<'gc>(
.and_then(|this| this.as_display_object())
.and_then(|this| this.as_edit_text())
{
return Ok((this.background_color()).into());
return Ok(this.background_color().to_rgb().into());
}

Ok(Value::Undefined)
Expand All @@ -154,12 +155,12 @@ pub fn set_background_color<'gc>(
.and_then(|this| this.as_display_object())
.and_then(|this| this.as_edit_text())
{
let new_color = args
let rgb = args
.get(0)
.cloned()
.unwrap_or(Value::Undefined)
.unwrap_or(&Value::Undefined)
.coerce_to_u32(activation)?;
this.set_background_color(activation.context.gc_context, new_color);
let color = Color::from_rgb(rgb, 255);
this.set_background_color(activation.context.gc_context, color);
}

Ok(Value::Undefined)
Expand Down Expand Up @@ -209,7 +210,7 @@ pub fn border_color<'gc>(
.and_then(|this| this.as_display_object())
.and_then(|this| this.as_edit_text())
{
return Ok(this.border_color().into());
return Ok(this.border_color().to_rgb().into());
}

Ok(Value::Undefined)
Expand All @@ -224,12 +225,12 @@ pub fn set_border_color<'gc>(
.and_then(|this| this.as_display_object())
.and_then(|this| this.as_edit_text())
{
let border_color = args
let rgb = args
.get(0)
.cloned()
.unwrap_or(Value::Undefined)
.unwrap_or(&Value::Undefined)
.coerce_to_u32(activation)?;
this.set_border_color(activation.context.gc_context, border_color);
let color = Color::from_rgb(rgb, 255);
this.set_border_color(activation.context.gc_context, color);
}

Ok(Value::Undefined)
Expand Down
79 changes: 37 additions & 42 deletions core/src/display_object/edit_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use gc_arena::{Collect, Gc, GcCell, MutationContext};
use ruffle_render::shape_utils::DrawCommand;
use ruffle_render::transform::Transform;
use std::{cell::Ref, cell::RefMut, sync::Arc};
use swf::Twips;
use swf::{Color, Twips};

/// The kind of autosizing behavior an `EditText` should have, if any
#[derive(Copy, Clone, Debug, Collect, PartialEq, Eq)]
Expand Down Expand Up @@ -74,10 +74,12 @@ pub struct EditTextData<'gc> {
text_spans: FormatSpans,

/// The color of the background fill. Only applied when has_border and has_background.
background_color: u32,
#[collect(require_static)]
background_color: Color,

/// The color of the border.
border_color: u32,
#[collect(require_static)]
border_color: Color,

/// The current border drawing.
drawing: Drawing,
Expand Down Expand Up @@ -213,9 +215,6 @@ impl<'gc> EditText<'gc> {
);
let line_data = get_line_data(&layout);

let background_color = 0xFFFFFF; // Default is white
let border_color = 0; // Default is black

let mut base = InteractiveObjectBase::default();

base.base.matrix_mut().tx = bounds.x_min;
Expand Down Expand Up @@ -254,8 +253,8 @@ impl<'gc> EditText<'gc> {
},
),
flags,
background_color,
border_color,
background_color: Color::WHITE,
border_color: Color::BLACK,
drawing: Drawing::new(),
object: None,
layout,
Expand Down Expand Up @@ -468,11 +467,15 @@ impl<'gc> EditText<'gc> {
self.redraw_border(gc_context);
}

pub fn background_color(self) -> u32 {
self.0.read().background_color
pub fn background_color(self) -> Color {
self.0.read().background_color.clone()
}

pub fn set_background_color(self, gc_context: MutationContext<'gc, '_>, background_color: u32) {
pub fn set_background_color(
self,
gc_context: MutationContext<'gc, '_>,
background_color: Color,
) {
self.0.write(gc_context).background_color = background_color;
self.redraw_border(gc_context);
}
Expand All @@ -489,11 +492,11 @@ impl<'gc> EditText<'gc> {
self.redraw_border(gc_context);
}

pub fn border_color(self) -> u32 {
self.0.read().border_color
pub fn border_color(self) -> Color {
self.0.read().border_color.clone()
}

pub fn set_border_color(self, gc_context: MutationContext<'gc, '_>, border_color: u32) {
pub fn set_border_color(self, gc_context: MutationContext<'gc, '_>, border_color: Color) {
self.0.write(gc_context).border_color = border_color;
self.redraw_border(gc_context);
}
Expand Down Expand Up @@ -544,7 +547,7 @@ impl<'gc> EditText<'gc> {
/// This `text_transform` is separate from and relative to the base
/// transform that this `EditText` automatically gets by virtue of being a
/// `DisplayObject`.
pub fn text_transform(self, color: swf::Color, baseline_adjustment: Twips) -> Transform {
pub fn text_transform(self, color: Color, baseline_adjustment: Twips) -> Transform {
let mut transform: Transform = Default::default();
transform.color_transform.set_mult_color(&color);

Expand Down Expand Up @@ -632,43 +635,35 @@ impl<'gc> EditText<'gc> {
.flags
.intersects(EditTextFlag::BORDER | EditTextFlag::HAS_BACKGROUND)
{
let bounds = write.bounds.clone();
let border_color = write.border_color;
let background_color = write.background_color;

if write.flags.contains(EditTextFlag::BORDER) {
write.drawing.set_line_style(Some(
swf::LineStyle::new()
.with_width(Twips::new(1))
.with_color(swf::Color::from_rgb(border_color, 0xFF)),
));
} else {
write.drawing.set_line_style(None);
}
if write.flags.contains(EditTextFlag::HAS_BACKGROUND) {
write
.drawing
.set_fill_style(Some(swf::FillStyle::Color(swf::Color::from_rgb(
background_color,
0xFF,
))));
} else {
write.drawing.set_fill_style(None);
}
let line_style = write.flags.contains(EditTextFlag::BORDER).then_some(
swf::LineStyle::new()
.with_width(Twips::new(1))
.with_color(write.border_color.clone()),
);
write.drawing.set_line_style(line_style);

let fill_style = write
.flags
.contains(EditTextFlag::HAS_BACKGROUND)
.then_some(swf::FillStyle::Color(write.background_color.clone()));
write.drawing.set_fill_style(fill_style);

let width = write.bounds.width();
let height = write.bounds.height();
write.drawing.draw_command(DrawCommand::MoveTo {
x: Twips::ZERO,
y: Twips::ZERO,
});
write.drawing.draw_command(DrawCommand::LineTo {
x: Twips::ZERO,
y: bounds.y_max - bounds.y_min,
y: height,
});
write.drawing.draw_command(DrawCommand::LineTo {
x: bounds.x_max - bounds.x_min,
y: bounds.y_max - bounds.y_min,
x: width,
y: height,
});
write.drawing.draw_command(DrawCommand::LineTo {
x: bounds.x_max - bounds.x_min,
x: width,
y: Twips::ZERO,
});
write.drawing.draw_command(DrawCommand::LineTo {
Expand Down

0 comments on commit 7ec5873

Please sign in to comment.