~vpzom/savg

15509944e810334199c8a5699eec34f4cee1a7ad — Colin Reeder 5 years ago b466162
Add translation
4 files changed, 49 insertions(+), 2 deletions(-)

M src/document.rs
M src/draw.rs
M src/main.rs
M src/types.rs
M src/document.rs => src/document.rs +7 -0
@@ 72,6 72,13 @@ impl Element {
            Element::Rect(rect) => Some(rect),
        }
    }
    pub fn translate(&mut self, diff: &Size2) {
        match self {
            Element::Rect(rect) => {
                rect.position += &diff;
            }
        }
    }
}

pub trait ScalableElement {

M src/draw.rs => src/draw.rs +7 -1
@@ 8,13 8,19 @@ use std::borrow::Cow;
pub fn draw_elem(elem: &Element, gr: &cairo::Context, ctx: DrawContext, drag_state: &Option<DragState>) {
    match elem {
        Element::Rect(rect) => {
            let mut pos = Cow::Borrowed(&rect.position);
            if let Some(DragState::TranslateBoth { start_pos }) = drag_state {
                let diff = ctx.mouse_pos - start_pos;
                pos = Cow::Owned(pos.as_ref() + &diff);
            }

            let mut size = Cow::Borrowed(&rect.size);
            if let Some(DragState::ScaleBoth { start_pos }) = drag_state {
                let diff = ctx.mouse_pos - start_pos;
                size = Cow::Owned(size.as_ref() + &diff);
            }

            gr.rectangle(rect.position.x, rect.position.y, size.width, size.height);
            gr.rectangle(pos.x, pos.y, size.width, size.height);
            if let Some(ref fill) = rect.presentation.fill {
                match fill {
                    Paint::Color(ref color) => match color {

M src/main.rs => src/main.rs +28 -1
@@ 271,7 271,9 @@ fn main() {
                            }

                            if bbox.contains(&mouse_pos) {
                                // already selected
                                *instance.drag_state.write().unwrap() = Some(DragState::TranslateBoth {
                                    start_pos: mouse_pos,
                                });

                                return Inhibit(true);
                            }


@@ 332,6 334,28 @@ fn main() {
                            }
                        }
                    },
                    DragState::TranslateBoth {
                        start_pos,
                    } => {
                        let pos = Point2 {
                            x: pos_x,
                            y: pos_y,
                        };
                        let diff = &pos - start_pos;

                        let selection = *instance.selection.read().unwrap();

                        if let Some(selection) = selection {
                            let mut document = instance.document.write().unwrap();
                            for elem in &mut document.content {
                                if elem.id() == selection {
                                    elem.as_mut().translate(&diff);
                                    instance.queue_repaint();
                                    break;
                                }
                            }
                        }
                    },
                }
            }



@@ 469,6 493,9 @@ pub enum DragState {
    ScaleBoth {
        start_pos: Point2
    },
    TranslateBoth {
        start_pos: Point2,
    }
}

pub struct DrawContext<'a> {

M src/types.rs => src/types.rs +7 -0
@@ 15,6 15,13 @@ impl<'a> std::ops::Add<&'a Size2> for &'a Point2 {
    }
}

impl<'a> std::ops::AddAssign<&'a Size2> for Point2 {
    fn add_assign(&mut self, other: &Size2) {
        self.x += other.width;
        self.y += other.height;
    }
}

impl<'a> std::ops::Sub for &'a Point2 {
    type Output = Size2;