pub struct PaperdollFactory {
    pub meta: Meta,
    /* private fields */
}
Expand description

A factory helps you manage the paperdoll project.

It stores all the data used in the project, and generates images for rendering.

Examples

use paperdoll::PaperdollFactory;

let mut factory = PaperdollFactory::default();

// Creates a new fragment.
let fragment_id = factory.add_fragment().unwrap();

// Stores a single black pixel in this fragment.
let fragment = factory.get_fragment_mut(fragment_id).unwrap();
fragment.image.width = 1;
fragment.image.height = 1;
fragment.image.pixels = vec![0, 0, 0, 255];

// Creates a new slot.
let slot_id = factory.add_slot().unwrap();

// Adds previous fragment to candidates of this slot.
let slot = factory.get_slot_mut(slot_id).unwrap();
slot.candidates.push(fragment_id);

// Creates a new doll.
let doll_id = factory.add_doll().unwrap();

// Resizes this doll and adds previous slot to this doll.
let doll = factory.get_doll_mut(doll_id).unwrap();
doll.width = 1;
doll.height = 1;
doll.slots.push(slot_id);

// Creates a Paperdoll struct which uses
let paperdoll = factory.builder()
    .doll(doll_id)
    .set_slot(slot_id, fragment_id)
    .build();

// Gets the image data for rendering this paperdoll.
let image_data = factory.render_paperdoll(&paperdoll).unwrap();

Fields§

§meta: Meta

The meta data of the project.

Implementations§

source§

impl PaperdollFactory

source

pub fn new( meta: Meta, doll_list: Vec<Doll>, slot_list: Vec<Slot>, fragment_list: Vec<Fragment> ) -> Result<Self>

Creates a paper doll factory. Will create an empty doll if there is no doll in doll_list.

Errors
  • Will return an error if there are duplicated ids for dolls, slots, or fragments.
source

pub fn from_manifest(manifest: Manifest) -> Result<Self>

Creates a paper doll factory from the given manifest.

Calls Self::new under the hood.

source

pub fn add_doll(&mut self) -> Result<u32>

Adds a new doll to the factory.

Returns the id of the doll.

Errors
  • Will return an error if the id pool is full.
source

pub fn add_fragment(&mut self) -> Result<u32>

Adds a new fragment to the factory.

Returns the id of the fragment.

Errors
  • Will return an error if the id pool is full.
source

pub fn add_slot(&mut self) -> Result<u32>

Adds a new slot to the factory.

Returns the id of the slot.

Errors
  • Will return an error if the id pool is full.
source

pub fn analyse( &self, doll: u32, slot_map: &HashMap<u32, u32>, only_id: bool ) -> Result<RenderMaterial>

Returns the structure of the paper doll. Can be used later for drawing the paper doll as a replacement for Self::render if you want to handle the rendering process yourself.

Arguments
  • doll: The id of the doll to be displayed.
  • slot_map: A map with the id of slot as key and the id of fragment which is used in this slot as value.
  • only_id: Whether the result RenderMaterial needs to contain the pixel data of the images? If true, the pixel data will be cloned. It’s recommended to set this to false if you do not rely on pixels returning here for rendering, eg. you have stored the pixel data elsewhere.
source

pub fn analyse_paperdoll( &self, paperdoll: &Paperdoll, only_id: bool ) -> Result<RenderMaterial>

Returns the structure of the given paperdoll.

Calls Self::analyse under the hood.

source

pub fn builder(&self) -> PaperdollBuilder<'_>

Returns a builder to construct Paperdoll.

source

pub fn dolls(&self) -> Iter<'_, u32, Doll>

Returns an iterator over all ids of dolls.

source

pub fn fragments(&self) -> Iter<'_, u32, Fragment>

Returns an iterator over all ids of fragments.

source

pub fn get_doll(&self, id: u32) -> Option<&Doll>

Returns a reference to the doll with the given id.

source

pub fn get_doll_mut(&mut self, id: u32) -> Option<&mut Doll>

Returns a mutable reference to the doll with the given id.

source

pub fn get_fragment(&self, id: u32) -> Option<&Fragment>

Returns a reference to the fragment with the given id.

source

pub fn get_fragment_mut(&mut self, id: u32) -> Option<&mut Fragment>

Returns a mutable reference to the fragment with the given id.

source

pub fn get_slot(&self, id: u32) -> Option<&Slot>

Returns a reference to the slot with the given id.

source

pub fn get_slot_mut(&mut self, id: u32) -> Option<&mut Slot>

Returns a mutable reference to the slot with the given id.

source

pub fn remove_doll(&mut self, id: u32) -> Option<Doll>

Removes the doll with the given id from the factory.

Returns the removed doll if it was previously in the factory, otherwise returns None.

source

pub fn remove_fragment(&mut self, id: u32) -> Option<Fragment>

Removes the fragment with the given id from the factory.

Returns the removed fragment if it was previously in the factory, otherwise returns None.

source

pub fn remove_slot(&mut self, id: u32) -> Option<Slot>

Removes the slot with the given id from the factory.

Returns the removed slot if it was previously in the factory, otherwise returns None.

source

pub fn render( &self, doll: u32, slot_map: &HashMap<u32, u32> ) -> Result<ImageData>

Returns the image data for rendering purpose.

Arguments
  • doll: The id of the doll to be displayed.
  • slot_map: A map with the id of slot as key and the id of fragment which is used in this slot as value.
source

pub fn render_paperdoll(&self, paperdoll: &Paperdoll) -> Result<ImageData>

Returns the image data to render the given paperdoll.

Calls Self::render under the hood.

source

pub fn slots(&self) -> Iter<'_, u32, Slot>

Returns an iterator over all ids of slots.

source

pub fn to_manifest(&self) -> Manifest

Creates a manifest based on the data of this factory.

Trait Implementations§

source§

impl Default for PaperdollFactory

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.