~zanneth/bedside

4f6bd3490a6585c56ec0cc1c5e4f6550b26556d1 — Charles Magahern 1 year, 2 months ago 1dea38b
Fix stupid bugs with path component
3 files changed, 30 insertions(+), 20 deletions(-)

M src/components/path.rs
M src/engine.rs
M src/main.rs
M src/components/path.rs => src/components/path.rs +20 -3
@@ 6,7 6,7 @@ use crate::render_context::RenderContext;
use crate::scene::Scene;
use crate::vertex_array::*;

use bytemuck::bytes_of;
use bytemuck::{bytes_of, cast_slice};
use std::mem::size_of;
use ultraviolet::{Vec2, Vec4};



@@ 53,6 53,22 @@ impl PathComponent {
                DrawingMode::Fill => false,
            };

            // upload vertices into position buffer
            let position_buffer = GraphicsBuffer::new(BufferTarget::Array, BufferAttribute {
                components_per_vertex:  2,
                component_type:         ComponentType::Float,
                normalized:             false,
                stride:                 size_of::<Vec2>() as isize,
                offset:                 0
            });
            let primitives: Vec<f32> = mvertices
                .iter()
                .map(|x| x.as_array().to_owned())
                .flatten()
                .collect();
            position_buffer.load_data(&BufferUsage::static_draw(), cast_slice(&primitives));
            varray.set_position_buffer(position_buffer);

            subpaths.push(Subpath {
                varray,
                vertices: mvertices,


@@ 113,7 129,6 @@ impl PathComponent {

    fn fill_subpath(&self, subpath: &Subpath, _render_ctx: &RenderContext) {
        subpath.varray.bind();
        // NB: this is probably wrong
        unsafe { gl::DrawArrays(gl::TRIANGLE_FAN, 0, subpath.vertices.len() as i32); }
    }
}


@@ 127,6 142,7 @@ pub fn path_system(scene: &mut Scene, render_ctx: &mut RenderContext, _: &mut In
    }
}

#[derive(Copy, Clone, Debug, PartialEq)]
enum DrawingCommand {
    MoveTo { x: f32, y: f32 },
    LineTo { x: f32, y: f32 },


@@ 139,6 155,7 @@ enum DrawingCommand {
    },
}

#[derive(Clone, Debug, PartialEq)]
pub struct PathBuilder {
    commands: Vec<DrawingCommand>,
}


@@ 186,7 203,7 @@ impl PathBuilder {
    }

    pub fn fill(self) -> PathComponent {
        let mut subpaths = vec![vec![]];
        let mut subpaths = vec![];
        for cmd in &self.commands {
            use DrawingCommand::*;
            match cmd {

M src/engine.rs => src/engine.rs +1 -1
@@ 27,10 27,10 @@ impl Engine {
            scene: None,
            systems: vec![
                // N.B. ordering matters here.
                Box::new(touch_system),
                Box::new(camera_system),
                Box::new(transform_system),
                Box::new(text_system),
                Box::new(touch_system),
                Box::new(quad_system),
                Box::new(path_system),
                Box::new(material_system),

M src/main.rs => src/main.rs +9 -16
@@ 134,33 134,26 @@ fn create_button(pos: Vec3, color: Color) -> Entity {
        width: 250.0,
        height: 100.0
    };
    let mut button = Entity::new();
    button.name = Some("button".into());
    button.transform = Some(TransformComponent::with_position(&pos));
    button.path = Some(PathBuilder::new()
    let pathb = PathBuilder::new()
        .move_to(0.0, 0.0)
        .line_to(button_rect.width, 0.0)
        .line_to(button_rect.width, button_rect.height)
        .line_to(0.0, button_rect.height)
        .line_to(0.0, 0.0)
        .stroke()
    );
        .line_to(0.0, 0.0);

    let mut button = Entity::new();
    button.name = Some("button".into());
    button.transform = Some(TransformComponent::with_position(&pos));
    button.path = Some(pathb.clone().stroke());
    button.material = Some(MaterialComponent::with_color(&color));
    button.touch = Some(TouchComponent::new(&button_rect));
    button.set_event_handler(move |button, event| {
        let pathb = PathBuilder::new()
            .move_to(0.0, 0.0)
            .line_to(button_rect.width, 0.0)
            .line_to(button_rect.width, button_rect.height)
            .line_to(0.0, button_rect.height)
            .line_to(0.0, 0.0);

        match event {
            Event::Touch(t) => {
                if t.phase == TouchPhase::Down {
                    button.path = Some(pathb.fill());
                    button.path = Some(pathb.clone().fill());
                } else {
                    button.path = Some(pathb.stroke());
                    button.path = Some(pathb.clone().stroke());
                }
            },
        }