Class: Totem
Defined in: | src/totem.coffee |
Overview
avatarChange
on being notified that a build, equip, or colour change has occurred on an Avatar being drawn. Data sent by the event will be of the form avatar:<Avatar>, event:<String>
. You can listen to this event using $(totem).on "avatarChange", (e, data) ->
, or a similar method.
startedDrawing
and finishedDrawing
on being notified that a DrawingService has started or finished drawing on a canvas. Data sent by the event will be of the form avatar:<Avatar>
canvas:<Object>
.
The Totem class provides an API for working with avatar builder applications. It's intended that you normally use Totem to create Avatars, Inventories and other collaborating objects rather than creating them yourself. Totem will then manage their lifecycles for you, including attaching relevant events, caching, etc.
Totem is not intended to replace an MVC framework; you may want to wrap objects like Avatars using your preferred model layer, so that you can find and sort them according to your application's requirements.
Instance Method Summary
- - (void) constructor(params) Constructor Create a new Totem object.
- - (void) config(key = null, value) Get and set Totem's configuration.
- - (Object) authenticate(type = "client", data = {}, bootstrap = false) Authenticate with a HAPI instance.
- - (Boolean) userIsAuthorized() Whether an authorized user is available.
- - (void) logout() Log out of the application.
- - (Object) bootstrap() Load all data necessary to bootstrap an app.
- - (AbstractUser) user() Get the active user - either a GuestUser or an AuthorizedUser.
- - (Object) categories() Retrieve the categories used by the avatar.
- - (Object) goods() Retrieve all available goods.
- - (Object) purchase(good, options = {}) Purchase a good on behalf of the logged-in user.
- - (Object) purchaseDefaultGoods() Ensure that the user owns any {#defaultGoods default goods}.
- - (Object) assets() Retrieve assets available to a user, indexed by good.
- - (Object) inventory() Retrieve a complete Inventory.
- - (Object) refresh() Refresh the inventory.
- - (Avatar) avatar(representation = null, attributes = {}) Create a new Avatar.
- - (Object) avatars(options = {}) Get an Avatar or set of Avatars belonging to the User.
- - (Object) randomise(avatar, options = {}, inventory = null) Randomise the assets equipped to an Avatar.
- - (Avatar) reset(avatar, inventory = null) Reset the given Avatar's assets to the {#defaultAssets defaults}.
- - (Object) drawOn(avatar, canvas, options = {}, service = null) Draw an Avatar on an HTMLCanvasElement.
Constructor Details
- (void) constructor(params)
Create a new Totem object.
Instance Method Details
- (Object) config()
- (Object) config(key)
- (Object) config(params)
- (Object) config(key, value)
Get and set Totem's configuration.
Possible keys are:
- representation
Default avatar representation
- palette
Default palette -- A palette arranged by category: global:
key if you want to use the palette globally. Unset keys will be silently ignored
- globalPalette
Whether to use the palette globally
- defaultGoods
An array of labels defining default goods to be purchased by the AuthorizedUser at bootstrap
- defaultAssets
An array of labels defining default assets to be used when resetting an avatar
- drawingAdaptor
Define an adaptor type which will be used to draw avatars. should be given in the form type:<String>, options:<Object>
. type
should be a requirable class in the "./drawing/adaptor" folder. options
is an object passed to the class constructor. Default is type: "canvasElement"
, which will create a new {CanvasElementAdaptor CanvasElementAdaptor}
- authService
A service which will be used for authentication. Default is a new AuthService
- (Object) authenticate(type = "client", data = {}, bootstrap = false)
Authenticate with a HAPI instance.
Examples:
`totem.authenticate "code", code: 5496`
or
`totem.authenticate "code", (callback) -> callback code: 5496`
- (Boolean) userIsAuthorized()
Whether an authorized user is available.
- (void) logout()
Log out of the application.
- (Object) bootstrap()
Load all data necessary to bootstrap an app.
- (AbstractUser) user()
Get the active user - either a GuestUser or an AuthorizedUser.
- (Object) categories()
Retrieve the categories used by the avatar.
- (Object) goods()
Retrieve all available goods.
- (Object) purchase(good, options = {})
- (Object) purchaseDefaultGoods()
Ensure that the user owns any {#defaultGoods default goods}.
- (Object) assets()
Retrieve assets available to a user, indexed by good. Delegates to User.
- (Object) inventory()
Retrieve a complete Inventory.
- (Object) refresh()
Refresh the inventory. This will reload assets available to the user and add any new ones to the inventory.
- (Avatar) avatar(representation = null, attributes = {})
Create a new Avatar. If {#defaultAssets defaultAssets} have been set, the avatar will be built using them.
- (Object) avatars(options = {})
Get an Avatar or set of Avatars belonging to the
User.
If the default representation is not set, and avatars retrieved
do not have representations set, avatars will be returned
without renderable assets, and must be built using
randomise or Avatar#build; you
can force 'bare' avatars to be returned using the option
assets: false
.
Image urls can still be retrieved from non-renderable avatars.
- (Object) randomise(avatar, options = {}, inventory = null)
Randomise the assets equipped to an Avatar. This operation will use the representation set on an avatar, or the {#representation default representation}. If no representation can be set using these values, an error will be thrown.
- (Avatar) reset(avatar, inventory = null)
Reset the given Avatar's assets to the {#defaultAssets defaults}. This operation will use the representation set on an avatar, or the {#representation default representation}. If no representation can be set using these values, an error will be thrown.
- (Object) drawOn(avatar, canvas, options = {}, service = null)
Draw an Avatar on an HTMLCanvasElement.
The autoDraw
option, true
by default, will keep drawing the avatar
on the given canvas whenever a change is made to it (a build,
equip or colour change), until a different
Avatar is drawn on the canvas.