core: Keep track of cursor position in drawings

This commit is contained in:
Nathan Adams 2020-05-21 16:45:22 +02:00
parent 99574cfa72
commit 1f8abc92fd
3 changed files with 35 additions and 3 deletions

View File

@ -2,7 +2,7 @@ use crate::backend::render::ShapeHandle;
use crate::bounding_box::BoundingBox;
use crate::context::{RenderContext, UpdateContext};
use crate::shape_utils::{DistilledShape, DrawCommand, DrawPath};
use swf::{FillStyle, LineStyle};
use swf::{FillStyle, LineStyle, Twips};
#[derive(Clone, Debug)]
pub struct Drawing {
@ -14,6 +14,7 @@ pub struct Drawing {
lines: Vec<(LineStyle, Vec<DrawCommand>)>,
current_fill: Option<(FillStyle, Vec<DrawCommand>)>,
current_line: Option<(LineStyle, Vec<DrawCommand>)>,
cursor: (Twips, Twips),
}
impl Drawing {
@ -27,6 +28,7 @@ impl Drawing {
lines: Vec::new(),
current_fill: None,
current_line: None,
cursor: (Twips::zero(), Twips::zero()),
}
}
@ -37,7 +39,13 @@ impl Drawing {
self.fills.push(existing);
}
if let Some(style) = style {
self.current_fill = Some((style, Vec::new()));
self.current_fill = Some((
style,
vec![DrawCommand::MoveTo {
x: self.cursor.0,
y: self.cursor.1,
}],
));
}
self.dirty = true;
@ -51,6 +59,7 @@ impl Drawing {
self.edge_bounds = BoundingBox::default();
self.shape_bounds = BoundingBox::default();
self.dirty = true;
self.cursor = (Twips::zero(), Twips::zero());
}
pub fn set_line_style(&mut self, style: Option<LineStyle>) {
@ -58,7 +67,13 @@ impl Drawing {
self.lines.push(existing);
}
if let Some(style) = style {
self.current_line = Some((style, Vec::new()));
self.current_line = Some((
style,
vec![DrawCommand::MoveTo {
x: self.cursor.0,
y: self.cursor.1,
}],
));
}
self.dirty = true;
@ -83,6 +98,8 @@ impl Drawing {
}
}
self.cursor = command.end_point();
if let Some((_, commands)) = &mut self.current_line {
commands.push(command.clone());
}

View File

@ -116,6 +116,16 @@ pub enum DrawCommand {
},
}
impl DrawCommand {
pub fn end_point(&self) -> (Twips, Twips) {
match self {
DrawCommand::MoveTo { x, y } => (*x, *y),
DrawCommand::LineTo { x, y } => (*x, *y),
DrawCommand::CurveTo { x2, y2, .. } => (*x2, *y2),
}
}
}
#[derive(Debug, Copy, Clone)]
struct Point {
x: Twips,

View File

@ -72,6 +72,11 @@ impl Twips {
Self(twips.into())
}
/// Creates a new `Twips` object set to the value of 0.
pub fn zero() -> Self {
Self(0)
}
/// Returns the number of twips.
pub fn get(self) -> i32 {
self.0