Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major redesign #713

Closed
bvssvni opened this issue Nov 29, 2014 · 6 comments

Comments

@bvssvni
Copy link
Member

commented Nov 29, 2014

Example:

graphics::Rectangle::new(color)
    .draw(rect, c, g);

I want to investigate an alternative design, where each drawable shape is a type, such that it can be used without transformation context.

  • Add drawable shapes with pub fields
  • Context should only contain view and transform matrix

Drawable shapes:

  • Ellipse
  • Image
  • Line
  • Polygon
  • Rectangle

Todo:

  • Create a new branch "newdesign"
@bvssvni

This comment has been minimized.

Copy link
Member Author

commented Nov 29, 2014

Perhaps we could use the Get and Modifier convention from piston-current. This can be implemented directly on the drawable shapes. It is possible to use .set as immutable operation if the type is Copy.

@bvssvni

This comment has been minimized.

Copy link
Member Author

commented Nov 30, 2014

This reminds me of PistonDevelopers/piston#736 where you blur out the distinction between state and function.

@bvssvni

This comment has been minimized.

Copy link
Member Author

commented Nov 30, 2014

One approach is to use context types, but with defaults set to concrete types. The drawback is that you have to use traits:

#![feature(default_type_params)]

pub struct Image<C = [f32, ..4], SR = [i32, ..4]> {
    pub color: C,
    pub source_rectangle: SR
}

pub trait DrawImage {
    fn draw(&self);
}

impl DrawImage for Image {
    fn draw(&self) {}
}

impl DrawImage for Image<(), ()> {
    fn draw(&self) {}
}

fn main() {
    Image { color: (), source_rectangle: () }.draw();
}
@bvssvni

This comment has been minimized.

Copy link
Member Author

commented Nov 30, 2014

Another approach is to use Option which lets you use the builder pattern:

pub struct Image {
    pub color: Option<[f32, ..4]>,
    pub source_rectangle: Option<[i32, ..4]>
}

impl Image {
    pub fn draw(&self) {}
}

fn main() {
    Image { color: None, source_rectangle: None }.draw();
}

This could be used like this:

Image::new().set(Color(...)).set(SourceRectangle(...)).draw(texture, c, g);
@bvssvni

This comment has been minimized.

Copy link
Member Author

commented Nov 30, 2014

One benefit by separating the shape is that you can reuse and modify how you draw in multiple ways and it does not depend on the type. For example, you can change the color by adding your own ways of modifying it in an ad-hoc way. You can also write a different draw method for another texture type.

@bvssvni

This comment has been minimized.

Copy link
Member Author

commented Dec 1, 2014

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.