core: Keep track of cursor position in drawings
This commit is contained in:
parent
99574cfa72
commit
1f8abc92fd
|
@ -2,7 +2,7 @@ use crate::backend::render::ShapeHandle;
|
||||||
use crate::bounding_box::BoundingBox;
|
use crate::bounding_box::BoundingBox;
|
||||||
use crate::context::{RenderContext, UpdateContext};
|
use crate::context::{RenderContext, UpdateContext};
|
||||||
use crate::shape_utils::{DistilledShape, DrawCommand, DrawPath};
|
use crate::shape_utils::{DistilledShape, DrawCommand, DrawPath};
|
||||||
use swf::{FillStyle, LineStyle};
|
use swf::{FillStyle, LineStyle, Twips};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Drawing {
|
pub struct Drawing {
|
||||||
|
@ -14,6 +14,7 @@ pub struct Drawing {
|
||||||
lines: Vec<(LineStyle, Vec<DrawCommand>)>,
|
lines: Vec<(LineStyle, Vec<DrawCommand>)>,
|
||||||
current_fill: Option<(FillStyle, Vec<DrawCommand>)>,
|
current_fill: Option<(FillStyle, Vec<DrawCommand>)>,
|
||||||
current_line: Option<(LineStyle, Vec<DrawCommand>)>,
|
current_line: Option<(LineStyle, Vec<DrawCommand>)>,
|
||||||
|
cursor: (Twips, Twips),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drawing {
|
impl Drawing {
|
||||||
|
@ -27,6 +28,7 @@ impl Drawing {
|
||||||
lines: Vec::new(),
|
lines: Vec::new(),
|
||||||
current_fill: None,
|
current_fill: None,
|
||||||
current_line: None,
|
current_line: None,
|
||||||
|
cursor: (Twips::zero(), Twips::zero()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,7 +39,13 @@ impl Drawing {
|
||||||
self.fills.push(existing);
|
self.fills.push(existing);
|
||||||
}
|
}
|
||||||
if let Some(style) = style {
|
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;
|
self.dirty = true;
|
||||||
|
@ -51,6 +59,7 @@ impl Drawing {
|
||||||
self.edge_bounds = BoundingBox::default();
|
self.edge_bounds = BoundingBox::default();
|
||||||
self.shape_bounds = BoundingBox::default();
|
self.shape_bounds = BoundingBox::default();
|
||||||
self.dirty = true;
|
self.dirty = true;
|
||||||
|
self.cursor = (Twips::zero(), Twips::zero());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_line_style(&mut self, style: Option<LineStyle>) {
|
pub fn set_line_style(&mut self, style: Option<LineStyle>) {
|
||||||
|
@ -58,7 +67,13 @@ impl Drawing {
|
||||||
self.lines.push(existing);
|
self.lines.push(existing);
|
||||||
}
|
}
|
||||||
if let Some(style) = style {
|
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;
|
self.dirty = true;
|
||||||
|
@ -83,6 +98,8 @@ impl Drawing {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.cursor = command.end_point();
|
||||||
|
|
||||||
if let Some((_, commands)) = &mut self.current_line {
|
if let Some((_, commands)) = &mut self.current_line {
|
||||||
commands.push(command.clone());
|
commands.push(command.clone());
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
struct Point {
|
struct Point {
|
||||||
x: Twips,
|
x: Twips,
|
||||||
|
|
|
@ -72,6 +72,11 @@ impl Twips {
|
||||||
Self(twips.into())
|
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.
|
/// Returns the number of twips.
|
||||||
pub fn get(self) -> i32 {
|
pub fn get(self) -> i32 {
|
||||||
self.0
|
self.0
|
||||||
|
|
Loading…
Reference in New Issue