Struct paperdoll::PaperdollFactory
source · 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
impl PaperdollFactory
sourcepub fn new(
meta: Meta,
doll_list: Vec<Doll>,
slot_list: Vec<Slot>,
fragment_list: Vec<Fragment>
) -> Result<Self>
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.
sourcepub fn from_manifest(manifest: Manifest) -> Result<Self>
pub fn from_manifest(manifest: Manifest) -> Result<Self>
Creates a paper doll factory from the given manifest.
Calls Self::new
under the hood.
sourcepub fn add_doll(&mut self) -> Result<u32>
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.
sourcepub fn add_fragment(&mut self) -> Result<u32>
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.
sourcepub fn add_slot(&mut self) -> Result<u32>
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.
sourcepub fn analyse(
&self,
doll: u32,
slot_map: &HashMap<u32, u32>,
only_id: bool
) -> Result<RenderMaterial>
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 resultRenderMaterial
needs to contain the pixel data of the images? Iftrue
, the pixel data will be cloned. It’s recommended to set this tofalse
if you do not rely on pixels returning here for rendering, eg. you have stored the pixel data elsewhere.
sourcepub fn analyse_paperdoll(
&self,
paperdoll: &Paperdoll,
only_id: bool
) -> Result<RenderMaterial>
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.
sourcepub fn builder(&self) -> PaperdollBuilder<'_>
pub fn builder(&self) -> PaperdollBuilder<'_>
Returns a builder to construct Paperdoll
.
sourcepub fn fragments(&self) -> Iter<'_, u32, Fragment>
pub fn fragments(&self) -> Iter<'_, u32, Fragment>
Returns an iterator over all ids of fragments.
sourcepub fn get_doll(&self, id: u32) -> Option<&Doll>
pub fn get_doll(&self, id: u32) -> Option<&Doll>
Returns a reference to the doll with the given id.
sourcepub fn get_doll_mut(&mut self, id: u32) -> Option<&mut Doll>
pub fn get_doll_mut(&mut self, id: u32) -> Option<&mut Doll>
Returns a mutable reference to the doll with the given id.
sourcepub fn get_fragment(&self, id: u32) -> Option<&Fragment>
pub fn get_fragment(&self, id: u32) -> Option<&Fragment>
Returns a reference to the fragment with the given id.
sourcepub fn get_fragment_mut(&mut self, id: u32) -> Option<&mut Fragment>
pub fn get_fragment_mut(&mut self, id: u32) -> Option<&mut Fragment>
Returns a mutable reference to the fragment with the given id.
sourcepub fn get_slot(&self, id: u32) -> Option<&Slot>
pub fn get_slot(&self, id: u32) -> Option<&Slot>
Returns a reference to the slot with the given id.
sourcepub fn get_slot_mut(&mut self, id: u32) -> Option<&mut Slot>
pub fn get_slot_mut(&mut self, id: u32) -> Option<&mut Slot>
Returns a mutable reference to the slot with the given id.
sourcepub fn remove_doll(&mut self, id: u32) -> Option<Doll>
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
.
sourcepub fn remove_fragment(&mut self, id: u32) -> Option<Fragment>
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
.
sourcepub fn remove_slot(&mut self, id: u32) -> Option<Slot>
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
.
sourcepub fn render(
&self,
doll: u32,
slot_map: &HashMap<u32, u32>
) -> Result<ImageData>
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.
sourcepub fn render_paperdoll(&self, paperdoll: &Paperdoll) -> Result<ImageData>
pub fn render_paperdoll(&self, paperdoll: &Paperdoll) -> Result<ImageData>
Returns the image data to render the given paperdoll.
Calls Self::render
under the hood.
sourcepub fn to_manifest(&self) -> Manifest
pub fn to_manifest(&self) -> Manifest
Creates a manifest based on the data of this factory.