Package Things :: Module BagOfStuff :: Class BagOfStuff
[frames] | no frames]

Class BagOfStuff

object --+    
         |    
      dict --+
             |
            BagOfStuff

Loads and holds stuff: Images, Fonts and SVG files.

Example

bos = BagOfStuff(); bos.add("apic.jpg", "logo")

Then you can use bos["logo"].pixbuf to get the pixel buffer for Python-Cairo.

Fonts note

Make one bag in your app, before you create your main AllThings object. This way it can install your fonts before GTK starts. Once GTK starts, there's no way to add new fonts to your app.

Use

bos = BagOfStuff()

bos.add('some file','some_key')

Once you have an instance, you can extract/use stuff:

  1. Bitmaps: bos["key"].pixbuf - supplies a pixmap.
  2. Vectors: bos["key"].draw(context)
Instance Methods
new empty dictionary

__init__(self)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
 
__getitem__(self, key)
x[y]
 
add(self, paf, key)
Add resources to your "bag".

Inherited from dict: __cmp__, __contains__, __delitem__, __eq__, __ge__, __getattribute__, __gt__, __iter__, __le__, __len__, __lt__, __ne__, __new__, __repr__, __setitem__, __sizeof__, clear, copy, fromkeys, get, has_key, items, iteritems, iterkeys, itervalues, keys, pop, popitem, setdefault, update, values

Inherited from object: __delattr__, __format__, __reduce__, __reduce_ex__, __setattr__, __str__, __subclasshook__

Class Variables
  imagetypes = ['png', 'jpg', 'jpeg']
  fonttypes = ['ttf', 'otf']
  vectortypes = ['svg']

Inherited from dict: __hash__

Properties

Inherited from object: __class__

Method Details

__init__(self)
(Constructor)

 

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Returns:
new empty dictionary

Overrides: object.__init__
(inherited documentation)

__getitem__(self, key)
(Indexing operator)

 

x[y]

Overrides: dict.__getitem__
(inherited documentation)

add(self, paf, key)

 

Add resources to your "bag". This is a simple way to get SVG (Inkscape) files and images and fonts into your animation; and then refer to them by keys.

Use

bos = BagOfStuff()

bos.add(filename, key)

This adds stuff to your bag of ..well.. stuff -- think of it as a repository of elements for your scenes and animations.

Each item in a bag has a variety of methods, like bos['alien'].draw(). See the classes Sprite, Loop, Path, Mask as well as .. .. ..

SVG files

NB: Within your SVG file, ensure the main elements have their own UNIQUE ids.

Keys

Each id (in the SVG file) will be joined to the key (given to the add method) to form the master key for this bag.

Example: bos.add('some.svg','walking'). Thereafter, stuff within the bag will be keyed as 'walking:funnywalk', 'walking:run' assuming you have ids within the SVG called 'funnywalk' and 'run'.

You set ids on objects in Inkscape by using the Object Properties dialog.

LAYOUT OF YOUR SVG FILES

This is quite important to get right. Use only these layer names and rules:

sprites

A 'sprite' is a GROUP of shapes. Use as many sprites on this layer as you need. Each a unique group with and id.

Clones: Create and use freely within each sprite. Just watch complexity because that slows things down.

Groups: You can use sub-groups within a sprite. They do not need ids.

All other Inkscape tools can be used -- effects like blur may not work.

loops

A 'loop' is a SUB-LAYER of GROUPS. The idea is that you would draw animations like walk-cycles, or explosions -- frame-by-frame stuff -- in a "loop". Given a sub-layer called "walking", you would have individual groups within that called "walk1", "walk2" and so on.

Each sub group must be given an incremental id: walk1, walk2, walk3 etc. These will be pulled-out and keyed by that id. All other rules the same as for sprites.

masks

A 'mask' is a simple, closed path that can be used for clipping or hit-testing. Do NOT group these vectors. Draw a single path (and convert to curves) and give it an id.

paths

A 'path' is a simple open path (convert to curves). Do NOT group it. Paths are used for making Things follow along them. (You could also simply draw them to the screen.)

SUB-LAYERS

Important.

On each MAIN LAYER, you *must* add SUB LAYERS (use Inkscape's layer dialog) and keep each of your objects on their OWN LAYER.

For example: In your 'sprites' layer, you have "redaliens" and "bluealiens" layers under it.

In the "redalien" sub-layer, you have a group with id="redalien1". You can also add other sprites, let's add id="boss_monster".

In your "bluealiens" sub-layer (hide the redaliens one) and draw a group with id="bluebug".

Now you have:

 sprites
  |____redaliens
  |       |_______redalien1
  |       |
  |       |_______boss_monster
  |
  |____bluealiens
          |_______bluebug

 .

This has several advantages, not least of which is that you can draw many things in one svg file and use the visibility tool to focus on one thing at a time.

HIDING THINGS

Suppose you use a rectangle as a means to guide your drawing, but don't want it in the final animation: In Inkscape, under the object properties dialog, give the rectangle a Label of hidden and it will be.

Adding images

You can load png, jpg and jpeg files.

Use: bos.add('path/to/somepic.png','my_big_ugly_logo')

See class AnImage for more info.

Adding fonts

At the moment you can only use ttf files.

Use: bos.add('path/to/somefont.ttf','key')

In the case of fonts, the key is not used, but you must pass it. You use fonts via pango, and that takes a family name.

Adding sounds

I suggest that you practice using lips, tongue and bits of shrubbery to make the sounds you expect to hear. Try to time this with the animation. This will have to do until I get a clue about how-to include sound, doo-bee-doo.

The add() method

Parameters:
  • paf - Path and filename. The path can be relative; it's convenient to have a directory of data in the same place that your Python code is running; if it were called 'repo' then your path would be 'repo/something.???' pointing to a picture, an svg, or whatever.
  • key - A unique key for accessing the stuff you are adding to the bag. If you repeat keynames, complaints will not be scarce.