This is a documentation for Board Game Arena: play board games online !

Khác biệt giữa bản sửa đổi của “Stock”

Từ Board Game Arena
Bước tới điều hướng Bước tới tìm kiếm
 
(Không hiển thị 25 phiên bản của 7 người dùng ở giữa)
Dòng 1: Dòng 1:
'''Stock''' is a javascript component that you can use in your game interface to display a set of elements of the same size that need to be arranged in single or multiple lines.


"Stock" is a javascript component that you can use on your game interface to display a set of elements of the same size that need to be arranged in one or several lines.
Stock is very flexible and is the most used component in BGA games.


Stock is very flexible and is the most used component in BGA games.
Examples of stock use cases:


Stock is used for example:
* Display a set of cards, typically hands (examples: ''Hearts'', ''Seasons'', ''The Boss'', ''Race for the Galaxy'').
* To display set of cards, typically hands (ex: in Hearts, Seasons, The Boss, Race for the Galaxy, ...).
* Display items in player panels (examples: ''Takenoko'', ''Amyitis'', ...)
* To display items in player panels (ex: Takenoko, Amyitis, ...)
* ... Many other situations. For example, black dice and cubes on cards in ''Troyes'' are displayed with stock components.
* ... in many other situations. For example, black dice and cubes on cards in Troyes are displayed with stock components.


Using stock:
Using stock:
* Your items are arranged nicely and sorted by type.
* Your items are arranged nicely and sorted by type.
* When adding (or removing) items to the set. All items slide smoothly to their new position in the set to host the new one.
* When adding or removing items to a set, all items slide smoothly to their new position in the set.
* Select/unselect items is a built-in functionnality.
* Selecting and unselecting items are built-in functions.
* You don't have to care about inserting/removing HTML piece of code: the entire life of the stock is managed by the component.
* You don't have to worry about inserting/removing HTML code; the entire life cycle of the stock is managed by the component.


== Using stock: a simple example ==
== Using stock: a simple example ==


Let's have a look on how the stock is used in game "Hearts" to display a hand of standard cards.
Let's have a look on how the stock is used in the game ''Hearts'' to display a hand of standard cards.
 
First, don't forget to add "ebg/stock" as a dependency in your js file:
 
<pre>
define([
    "dojo","dojo/_base/declare",
    "ebg/core/gamegui",
    "ebg/counter",
    "ebg/stock"    /// <==== HERE
],
</pre>
 
The stock is initialized in the Javascript "setup" method like this:


The stock is initialized in Javascript "setup" method like this:
<pre>
<pre>
     // Player hand
     // Player hand
Dòng 28: Dòng 41:
Explanations:
Explanations:
* We create a new stock object for the player hand.
* We create a new stock object for the player hand.
* As parameters of the "create" method, we provide the width/height of an item (=a card), and the container div "myhand" - which is a simple void "div" element defines in our HTML template (.tpl).
* As parameters of the "create" method, we provide the width/height of an item (a card), and the div container "myhand" - which is a simple empty "div" element defined in our HTML template (.tpl).
 
Then, we must tell the stock what items it is going to display during its life: the 52 cards of a standard card game. Of course, we did not create 52 different images, but a "CSS sprite" image named "cards.jpg" with the cards arranged in 4 rows and 13 columns.


Then, we must tell the stock what are the items it is going to display during its life: the 52 cards of a standard card game. Of course, we did not create 52 different images, but create a "CSS sprite" image named "cards.jpg" with all the cards arranged in 4 rows and 13 columns.
Here's how we tell stock what items to display:


Here's how we tell stock what are the items type to display:
<pre>
<pre>
     // Explain there are 13 images per row in the CSS sprite image
     // Specify that there are 13 images per row in the CSS sprite image
     this.playerHand.image_items_per_row = 13;
     this.playerHand.image_items_per_row = 13;


     // Create cards types:
     // Create card types:
     for( var color=1;color<=4;color++ )
     for( var color=1;color<=4;color++ )
     {
     {
Dòng 44: Dòng 58:
             // Build card type id
             // Build card type id
             var card_type_id = this.getCardUniqueId( color, value );
             var card_type_id = this.getCardUniqueId( color, value );
             this.playerHand.addItemType( card_type_id, card_type_id, g_themeurl+'img/hearts/cards.jpg', card_type_id );
             this.playerHand.addItemType( card_type_id, card_type_id, g_gamethemeurl+'img/cards.jpg', card_type_id );
         }
         }
     }
     }
</pre>
</pre>


Explanations:
Explanation:
* At first, we tell the stock component that our CSS sprite contains 13 items per row. This way, it can find the correct image for each card type id.
 
* Then for the 4x13 cards, we call "addItemType" method that create the type. The arguments are the type id, the weight of the card (for sorting purpose), the URL of our CSS sprite, and the position of our card image in the CSS sprite.
* First, we tell the stock component that our CSS sprite contains 13 items per row. This way, it can find the correct image for each card type id.
* Then for the 4x13 cards, we call the "addItemType" method that creates the type. The arguments are the type id, the weight of the card (for sorting purpose), the URL of our CSS sprite, and the position of our card image in the CSS sprite.
 
Note: In this specific example we need to generate a unique ID for each type of card based on its color and value. This is the only purpose of "getCardUniqueId".


Note: in this specific example we need to generate a unique ID for each type of card based on its color and value. This is the only purpose of "getCardUniqueId".
From now on, if we need to add a card - for example, the 5 of Hearts - to a player's hand, we can do this:


From now, if we need to add - for example - the 5 of Heart to player's hand, we can do this.
<pre>
this.playerHand.addToStock( this.getCardUniqueId( 2 /* 2=hearts */, 5 ) );
this.playerHand.addToStock( this.getCardUniqueId( 2 /* 2=hearts */, 5 ) );
</pre>


In reality, cards have some IDs, which are useful to manipulate them. This is the reason we are using "addToStockWithId" instead:
In reality, cards have some IDs, which are useful to manipulate them. This is the reason we are using "addToStockWithId" instead:
this.playerHand.addToStock( this.getCardUniqueId( 2 /* 2=hearts */, 5 ), my_card_id );
 
<pre>
this.playerHand.addToStockWithId( this.getCardUniqueId( 2 /* 2=hearts */, 5 ), my_card_id );
</pre>


If afterwards we want to remove this card from the stock:
If afterwards we want to remove this card from the stock:
<pre>
this.playerHand.removeFromStockById( my_card_id );
this.playerHand.removeFromStockById( my_card_id );
</pre>


== Complete stock component reference ==
== Complete stock component reference ==
Dòng 69: Dòng 93:


With create, you create a new stock component.
With create, you create a new stock component.
Parameters:
Parameters:
* page: the container page. Usually: "this".
* page: the container page. Usually: "this".
* container_div: the container "<div>" element (a void <div> element in your template, with an id).
* container_div: the container "div" element (a void div element in your template, with an id).
* a stock item width and height, in pixels.
* width and height (in pixels) for the stock component.
 
(See "Hearts" example above).


(See ''Hearts'' example above).


'''count():'''
'''count():'''
Dòng 83: Dòng 107:
'''addItemType( type, weight, image, image_position ):'''
'''addItemType( type, weight, image, image_position ):'''


Define a new type of item to the stock.
Define a new type of item and add it to the stock.


 
This is mandatory to define a new item type before adding it to the stock. Example: if you want to have a stock contain cubes of 3 different colors, you must add 3 item types (one for each color).
This is mandatory to define a new item type before adding it to the stock. Example: if you want to have a stock control that can contains cubes of 3 different colors, you must add 3 item types (one for each color).


Parameters:
Parameters:
* type: ID of the type to add. You can choose any positive integer. All item types must have distinct IDs.
* type: ID of the type to add. You can choose any positive integer. All item types must have distinct IDs.
* weight: weight of items of this type. Weight value is used to sort items of the stock during the display. Note that you can specify the same weight for all items (in this case they are not sorted).
* weight: weight of items of this type. Weight value is used to sort items of the stock during the display. Note that you can specify the same weight for all items; in this case, they are not sorted and their order might change randomly at any time.
* image: URL of item image. Most of the time, you will use a CSS sprite for stock item, so you have to specify CSS sprite image here.
* image: URL of item image. Most of the time, you will use a CSS sprite for stock items, so you have to specify CSS sprite image here.


Be careful: you must specify the image url as this:
Be careful: you must specify the image url as this:
<pre>
<pre>
   g_themeurl+'img/your_game_name/yourimage.png'
   g_gamethemeurl+'img/yourimage.png'
</pre>
</pre>
_ image_position: if "image" specify the URL of a CSS sprite, you must specify the position of the item image in this CSS sprite. For example, if you have a CSS sprite with 3 cubes with a size of 20x20 pixels each (so your CSS image has for example a size of 20x60 or 60x20), you specify "0" for the first cube image, 1 for the second, 2 for the third.
 
Important: there are more than one line of items in your CSS sprite,  you must specify how many items per line you have in your CSS sprite like this:
* image_position: if "image" specify the URL of a CSS sprite, you must specify the position of the item image in this CSS sprite. For example, if you have a CSS sprite with 3 cubes with a size of 20x20 pixels each (so your CSS image has for example a size of 20x60 or 60x20), you specify "0" for the first cube image, 1 for the second, 2 for the third.
 
''Important'': if there is more than one line of items in your CSS sprite,  you must specify how many items per line you have in your CSS sprite like this:
 
<pre>
<pre>
     // Specify that there is 10 image items per row in images used in "myStockObject" control.
     // Specify that there are 10 image items per row in images used in "myStockObject" control.
     this.myStockObject.image_items_per_row = 10;
     this.myStockObject.image_items_per_row = 10;
</pre>
</pre>
Dòng 106: Dòng 133:
'''addToStock( type, from )'''
'''addToStock( type, from )'''


Add an item to the stock, with the specified type.
Add an item to the stock, with the specified type, but without a unique ID.


To make your life easy, in most of the case we suggest you to use "addToStockWithId" in order to give an ID to the item added. "addToStock" is perfect when you are using a stock controls with items that are generic game material that does not need to be addressed individually (ex: a bunch of money tokens).
To make your life easier, in most cases we suggest you use '''addToStockWithId''' in order to give an ID to the item added. '''addToStock''' is suitable when you are using a stock to control items that are generic game materials that don't need to be addressed individually (example: a bunch of money tokens).


Parameters:
Parameters:
* type: ID of the item type to use (as specified in "addItemType")
* type: ID of the item type to use (as specified in "addItemType")
* from: OPTIONNAL: if you specify a HTML item here, the item will appear on this item and will be slided to its position on the stock item.
* from: OPTIONAL: if you specify an HTML item here, the item will appear on this item and will be slid to its position on the stock item.


Example:
Example:
Dòng 121: Dòng 148:
</pre>
</pre>


Important: for a given stock control, you must use either addToStock or addToStockWithId, but NEVER BOTH OF THEM.
Important: for a given stock control, you must use either '''addToStock''' or '''addToStockWithId''', but NEVER BOTH OF THEM.


'''addToStockWithId( type, id, from )'''
'''addToStockWithId( type, id, from )'''


This is exactly the same method than "addToStock", except that you associate an ID to the newly created item.
This is the same method as '''addToStock''', except that it also associates an ID with the newly created item.


This is especially useful:
This is especially useful:
* When you need to know which item(s) has been selected by the user (see "getSelectedItems").
* When you need to remove a specific item from the stock with "removeFromStockById"


Important: for a given stock control, you must use either addToStock or addToStockWithId, but NEVER BOTH OF THEM.
* When you need to know which item(s) have been selected by the user (see '''getSelectedItems''').
* When you need to remove a specific item from the stock with '''removeFromStockById'''.
 
Important: for a given stock control, you must use either '''addToStock''' or '''addToStockWithId''', but NEVER BOTH OF THEM.


'''removeFromStock( type, to )'''
'''removeFromStock( type, to )'''
Dòng 167: Dòng 195:
'''resetItemsPosition()'''
'''resetItemsPosition()'''


If you moved an item from the stock control manually (ex: after a drag'n'drop) and want to reset their position to their original ones, you can call this method.
If you moved an item from the stock control manually (ex: after a drag'n'drop) and want to reset their positions to their original ones, you can call this method.


'''item_margin'''
'''item_margin'''
Dòng 198: Dòng 226:
For each stock control, you can specify a selection mode:
For each stock control, you can specify a selection mode:
* 0: no item can be selected by the player.
* 0: no item can be selected by the player.
* 1: a maximum of one item can be selected by the player at the same time.
* 1: a maximum of one item can be selected by the player at a time.
* 2: several items can be selected by the player at the same time.
* 2 (default): multiple items can be selected by the player at the same time.
 
'''setSelectionAppearance( type )'''
 
For each stock control, you can specify a selection highlighting type:
* 'border': there will be a red border around selected items (this is the default). The attribute 'apparenceBorderWidth' can be used to manage the width of the border (in pixels).
* 'disappear': the selected item will fade out and disappear. This is useful when the selection has the effect of destroying the item.
* 'class': there will be an extra '''stockitem_selected''' css class added to the element when it is selected (and removed when unselected). You can override this class in the css file for your game.
 
By default this class definition is:
<pre>
.stockitem_selected {
border: 2px solid red ! important;
}
</pre>
 
If you want to override it (for example, to change the border color) add this in your <game>.css file:
<pre>
.stockitem_selected {
border: 2px solid orange ! important;
}
</pre>


'''isSelected( id )'''
'''isSelected( id )'''


Return true/false wether the specified item id has been selected or not.
Return a boolean indicating whether the specified item id has been selected.


'''selectItem( id )'''
'''selectItem( id )'''
Dòng 219: Dòng 268:
'''onChangeSelection'''
'''onChangeSelection'''


This callback method is called when the player select/unselect an item of the stock.
This callback method is called when the player selects/unselects an item of the stock.
 
You can connect this to one of your methods like this:


You can connect this to one of your method like this:
<pre>
<pre>
     dojo.connect( this.myStockControl, 'onChangeSelection', this, 'onMyMethodToCall' );
     dojo.connect( this.myStockControl, 'onChangeSelection', this, 'onMyMethodToCall' );
Dòng 236: Dòng 286:
</pre>
</pre>


Note: The "control_name" argument is the ID (the "DOM" id) of the <div> container of your stock control. Using "control_name", you can use the same callback method for different Stock control and see which one trigger the method.
Note: The "control_name" argument is the ID (the "DOM" id) of the "div" container of your stock control. Using "control_name", you can use the same callback method for different Stock control and see which one trigger the method.


'''getSelectedItems()'''
'''getSelectedItems()'''
Dòng 256: Dòng 306:
'''getAllItems()'''
'''getAllItems()'''


Get all items (same format than getSelectedItems and getUnselectedItems).
Get all items (same format as getSelectedItems and getUnselectedItems).
 
'''getItemDivId(id)'''
 
Get the div id using the stock item id (to manipulate element properties directly).


'''setOverlap( horizontal_percent, vertical_percent )'''
'''setOverlap( horizontal_percent, vertical_percent )'''


Make items on the stock control "overlap" on each other, to save space.
Make items of the stock control "overlap" each other, to save space.


By default, horizontal_overlap and vertical_overlap are 0.
By default, horizontal_overlap and vertical_overlap are 0.


When horizontal_overlap=20, it means that a stock item must overlap on 20% of the width of the previous item. horizontal_overlap can't be over 100.
When horizontal_overlap=20, it means that a stock item will overlap to only show 20% of the width of all the previous items. horizontal_overlap can't be greater than 100.


vertical_overlap works differently: one items on two are shifted up.
vertical_overlap works differently: one items on two are shifted up.


See "Jaipur" game to see an example to use of this function.
See the games "Jaipur" or "Koryŏ" to see examples of use of this function.


'''onItemCreate'''
'''onItemCreate'''


Using onItemCreate, you can trigger a method each time a new item is added to the Stock, in order you can customize it.
Using onItemCreate, you can trigger a method each time a new item is added to the Stock, in order to customize it.


Complete example:
Complete example:
Dòng 299: Dòng 353:
== Tips when adding/removing items to/from Stock components ==
== Tips when adding/removing items to/from Stock components ==


The usual way is the following:
Most cases will be one of the following situations:


'''Situation A''':
'''Situation A''':


When you add a card to a stock item, and this card is '''not''' coming from another stock: use "addToStockWithId" with a "from" argument set to the element of your interface where card should come from.
When you add a card to a stock item, and this card is '''not''' coming from another stock:
 
* Use '''addToStockWithId''' with a "from" argument set to the element of your interface where the card should come from.


'''Situation B''':
'''Situation B''':


When you add a card to a stock item, and this card is coming from another stock:
When you add a card to a stock item, and this card is coming from another stock:
* on the destination Stock, use "addToStockWithId" with a "from" equals to the HTML id of the corresponding item in the source Stock. For example, If the source stock id is "myHand", then the HTML id of card 48 is "myHand_item_48".
 
* then, remove the source item with "removeFromStockById".
* On the destination Stock, use '''addToStockWithId''' with a "from" argument which is the HTML id of the corresponding item in the source Stock. For example, if the source stock id is "myHand", then the HTML id of card 48 is "myHand_item_48".
(note that it's important to do things in this order, because source item must still exists when you use it as the origin of the slide).
* Then, remove the source item with '''removeFromStockById'''.
 
(Note that it's important to do things in this order, because the source item must still exist when you use it as the origin of the slide.)


'''Situation C''':
'''Situation C''':


When you move a card from a stock item to something that is not a stock item:
When you move a card from a stock item to something that is not a stock item:
* insert the card as a classic HTML template (dojo.place / this.format_block).
* place it on the Stock item with "this.placeOnObject", using Stock item HTML id (see above).
* slide it to its new position with "this.slideToObject"
* remove the card from the Stock item with "removeFromStockById".


Using the methods above, your cards should slided to, from and between your Stock controls smoothly
* Insert the card as a classic HTML template (dojo.place / this.format_block).
* Place it on the Stock item with '''this.placeOnObject''', using the Stock item HTML id (see above).
* Slide it to its new position with '''this.slideToObject'''.
* Remove the card from the Stock item with '''removeFromStockById'''.
 
Using the methods above, your cards should slide to, from, and between your Stock controls smoothly.
 
 
You can customize this (showing the default value):
<pre>
 
this.mystock.jstpl_stock_item= "<div id=\"${id}\" class=\"stockitem\" style=\"top:${top}px;left:${left}px;width:${width}px;height:${height}px;z-index:${position};background-image:url('${image}');\"></div>";
</pre>
To produce a different type of stock item

Bản mới nhất lúc 12:15, ngày 5 tháng 4 năm 2019

Stock is a javascript component that you can use in your game interface to display a set of elements of the same size that need to be arranged in single or multiple lines.

Stock is very flexible and is the most used component in BGA games.

Examples of stock use cases:

  • Display a set of cards, typically hands (examples: Hearts, Seasons, The Boss, Race for the Galaxy).
  • Display items in player panels (examples: Takenoko, Amyitis, ...)
  • ... Many other situations. For example, black dice and cubes on cards in Troyes are displayed with stock components.

Using stock:

  • Your items are arranged nicely and sorted by type.
  • When adding or removing items to a set, all items slide smoothly to their new position in the set.
  • Selecting and unselecting items are built-in functions.
  • You don't have to worry about inserting/removing HTML code; the entire life cycle of the stock is managed by the component.

Using stock: a simple example

Let's have a look on how the stock is used in the game Hearts to display a hand of standard cards.

First, don't forget to add "ebg/stock" as a dependency in your js file:

define([
    "dojo","dojo/_base/declare",
    "ebg/core/gamegui",
    "ebg/counter",
    "ebg/stock"     /// <==== HERE
],

The stock is initialized in the Javascript "setup" method like this:

    // Player hand
    this.playerHand = new ebg.stock();
    this.playerHand.create( this, $('myhand'), this.cardwidth, this.cardheight );

Explanations:

  • We create a new stock object for the player hand.
  • As parameters of the "create" method, we provide the width/height of an item (a card), and the div container "myhand" - which is a simple empty "div" element defined in our HTML template (.tpl).

Then, we must tell the stock what items it is going to display during its life: the 52 cards of a standard card game. Of course, we did not create 52 different images, but a "CSS sprite" image named "cards.jpg" with the cards arranged in 4 rows and 13 columns.

Here's how we tell stock what items to display:

    // Specify that there are 13 images per row in the CSS sprite image
    this.playerHand.image_items_per_row = 13;

    // Create card types:
    for( var color=1;color<=4;color++ )
    {
        for( var value=2;value<=14;value++ )
        {
            // Build card type id
            var card_type_id = this.getCardUniqueId( color, value );
            this.playerHand.addItemType( card_type_id, card_type_id, g_gamethemeurl+'img/cards.jpg', card_type_id );
        }
    }

Explanation:

  • First, we tell the stock component that our CSS sprite contains 13 items per row. This way, it can find the correct image for each card type id.
  • Then for the 4x13 cards, we call the "addItemType" method that creates the type. The arguments are the type id, the weight of the card (for sorting purpose), the URL of our CSS sprite, and the position of our card image in the CSS sprite.

Note: In this specific example we need to generate a unique ID for each type of card based on its color and value. This is the only purpose of "getCardUniqueId".

From now on, if we need to add a card - for example, the 5 of Hearts - to a player's hand, we can do this:

this.playerHand.addToStock( this.getCardUniqueId( 2 /* 2=hearts */, 5 ) );

In reality, cards have some IDs, which are useful to manipulate them. This is the reason we are using "addToStockWithId" instead:

this.playerHand.addToStockWithId( this.getCardUniqueId( 2 /* 2=hearts */, 5 ), my_card_id );

If afterwards we want to remove this card from the stock:

this.playerHand.removeFromStockById( my_card_id );

Complete stock component reference

create( page, container_div, item_width, item_height ):

With create, you create a new stock component.

Parameters:

  • page: the container page. Usually: "this".
  • container_div: the container "div" element (a void div element in your template, with an id).
  • width and height (in pixels) for the stock component.

(See Hearts example above).

count():

Return the total number of items in the stock right now.

addItemType( type, weight, image, image_position ):

Define a new type of item and add it to the stock.

This is mandatory to define a new item type before adding it to the stock. Example: if you want to have a stock contain cubes of 3 different colors, you must add 3 item types (one for each color).

Parameters:

  • type: ID of the type to add. You can choose any positive integer. All item types must have distinct IDs.
  • weight: weight of items of this type. Weight value is used to sort items of the stock during the display. Note that you can specify the same weight for all items; in this case, they are not sorted and their order might change randomly at any time.
  • image: URL of item image. Most of the time, you will use a CSS sprite for stock items, so you have to specify CSS sprite image here.

Be careful: you must specify the image url as this:

  g_gamethemeurl+'img/yourimage.png'
  • image_position: if "image" specify the URL of a CSS sprite, you must specify the position of the item image in this CSS sprite. For example, if you have a CSS sprite with 3 cubes with a size of 20x20 pixels each (so your CSS image has for example a size of 20x60 or 60x20), you specify "0" for the first cube image, 1 for the second, 2 for the third.

Important: if there is more than one line of items in your CSS sprite, you must specify how many items per line you have in your CSS sprite like this:

    // Specify that there are 10 image items per row in images used in "myStockObject" control.
    this.myStockObject.image_items_per_row = 10;

addToStock( type, from )

Add an item to the stock, with the specified type, but without a unique ID.

To make your life easier, in most cases we suggest you use addToStockWithId in order to give an ID to the item added. addToStock is suitable when you are using a stock to control items that are generic game materials that don't need to be addressed individually (example: a bunch of money tokens).

Parameters:

  • type: ID of the item type to use (as specified in "addItemType")
  • from: OPTIONAL: if you specify an HTML item here, the item will appear on this item and will be slid to its position on the stock item.

Example:

  // Add a money token to the "player money" stock.
  // The money token will appear on "player_id" player panel and will move to its position.
  this.playerMoney.addToStock( MONEY_TOKEN, 'overall_player_board_'+player_id );

Important: for a given stock control, you must use either addToStock or addToStockWithId, but NEVER BOTH OF THEM.

addToStockWithId( type, id, from )

This is the same method as addToStock, except that it also associates an ID with the newly created item.

This is especially useful:

  • When you need to know which item(s) have been selected by the user (see getSelectedItems).
  • When you need to remove a specific item from the stock with removeFromStockById.

Important: for a given stock control, you must use either addToStock or addToStockWithId, but NEVER BOTH OF THEM.

removeFromStock( type, to )

Remove an item of the specific type from the stock.

"to" is an optional parameter. If "to" contains the ID of an HTML element, the item removed from the stock is slided to this HTML element before it disappear.

removeFromStockById( id, to )

Remove an item with a specific ID from the stock.

"to" is an optional parameter. If "to" contains the ID of an HTML element, the item removed from the stock is slided to this HTML element before it disappear.

removeAll()

Remove all items from the stock.

getPresentTypeList()

Return an array with all the types of items present in the stock right now.

Example:

    this.myStockControl.removeAll();
    this.myStockControl.addToStock( 65 );
    this.myStockControl.addToStock( 34 );
    this.myStockControl.addToStock( 89 );
    this.myStockControl.addToStock( 65 );
    
    // The following returns: { 34:1,  65:1,  89:1  }
    var item_types = this.myStockControl.getPresentTypeList();

resetItemsPosition()

If you moved an item from the stock control manually (ex: after a drag'n'drop) and want to reset their positions to their original ones, you can call this method.

item_margin

By default, there is a margin of 5px between the items of a stock. You can change the member variable "item_margin" to change this.

Example:

     this.myStockControl.item_margin=5;

changeItemsWeight( newWeights )

With this method you can change dynamically the weight of the item types in a stock control.

Items are immediately re-sorted with the new weight.

Example: with a stock control that contains classic cards, you can order them by value or by color. Using changeItemsWeight you can switch from one sort method to another when a player request this.

newWeights is an associative array: item type id => new weight.

Example:

    // Item type 1 gets a new weight of 10, 2 a new weight of 20, 3 a new weight of 30.
    this.myStockControl.changeItemsWeight( { 1: 10, 2: 20, 3: 30 } );

setSelectionMode( mode )

For each stock control, you can specify a selection mode:

  • 0: no item can be selected by the player.
  • 1: a maximum of one item can be selected by the player at a time.
  • 2 (default): multiple items can be selected by the player at the same time.

setSelectionAppearance( type )

For each stock control, you can specify a selection highlighting type:

  • 'border': there will be a red border around selected items (this is the default). The attribute 'apparenceBorderWidth' can be used to manage the width of the border (in pixels).
  • 'disappear': the selected item will fade out and disappear. This is useful when the selection has the effect of destroying the item.
  • 'class': there will be an extra stockitem_selected css class added to the element when it is selected (and removed when unselected). You can override this class in the css file for your game.

By default this class definition is:

.stockitem_selected {
	border: 2px solid red ! important;
}

If you want to override it (for example, to change the border color) add this in your <game>.css file:

.stockitem_selected {
	border: 2px solid orange ! important;
}

isSelected( id )

Return a boolean indicating whether the specified item id has been selected.

selectItem( id )

Select the specified item.

unselectItem( id )

Unselect the specified item.

unselectAll()

Unselect all items of the stock.

onChangeSelection

This callback method is called when the player selects/unselects an item of the stock.

You can connect this to one of your methods like this:

    dojo.connect( this.myStockControl, 'onChangeSelection', this, 'onMyMethodToCall' );
    
    (...)
    
    onMyMethodToCall: function( control_name )
    {
        // This method is called when myStockControl selected items changed
        var items = this.myStockControl.getSelectedItems();
        
        // (do something)
    },

Note: The "control_name" argument is the ID (the "DOM" id) of the "div" container of your stock control. Using "control_name", you can use the same callback method for different Stock control and see which one trigger the method.

getSelectedItems()

Return the list of selected items, as an array with the following format:

[
   { type:1,  id:  1001 },
   { type:1,  id:  1002 },
   { type:3,  id:  1003 }
   ...
]

getUnselectedItems()

Same as the previous one, but return unselected item instead of seleted ones.

getAllItems()

Get all items (same format as getSelectedItems and getUnselectedItems).

getItemDivId(id)

Get the div id using the stock item id (to manipulate element properties directly).

setOverlap( horizontal_percent, vertical_percent )

Make items of the stock control "overlap" each other, to save space.

By default, horizontal_overlap and vertical_overlap are 0.

When horizontal_overlap=20, it means that a stock item will overlap to only show 20% of the width of all the previous items. horizontal_overlap can't be greater than 100.

vertical_overlap works differently: one items on two are shifted up.

See the games "Jaipur" or "Koryŏ" to see examples of use of this function.

onItemCreate

Using onItemCreate, you can trigger a method each time a new item is added to the Stock, in order to customize it.

Complete example:

    // During "setup" phase, we associate our method "setupNewCard" with the creation of a new stock item:
    this.myStockItem.onItemCreate = dojo.hitch( this, 'setupNewCard' ); 

     (...)

    // And here is our "setupNewCard":
    setupNewCard: function( card_div, card_type_id, card_id )
    {
       // Add a special tooltip on the card:
       this.addTooltip( card_div.id, _("Some nice tooltip for this item"), '' );

       // Note that "card_type_id" contains the type of the item, so you can do special actions depending on the item type

       // Add some custom HTML content INSIDE the Stock item:
       dojo.place( this.format_block( 'jstpl_my_card_content', {
                                ....
                           } ), card_div.id );
    }

Tips when adding/removing items to/from Stock components

Most cases will be one of the following situations:

Situation A:

When you add a card to a stock item, and this card is not coming from another stock:

  • Use addToStockWithId with a "from" argument set to the element of your interface where the card should come from.

Situation B:

When you add a card to a stock item, and this card is coming from another stock:

  • On the destination Stock, use addToStockWithId with a "from" argument which is the HTML id of the corresponding item in the source Stock. For example, if the source stock id is "myHand", then the HTML id of card 48 is "myHand_item_48".
  • Then, remove the source item with removeFromStockById.

(Note that it's important to do things in this order, because the source item must still exist when you use it as the origin of the slide.)

Situation C:

When you move a card from a stock item to something that is not a stock item:

  • Insert the card as a classic HTML template (dojo.place / this.format_block).
  • Place it on the Stock item with this.placeOnObject, using the Stock item HTML id (see above).
  • Slide it to its new position with this.slideToObject.
  • Remove the card from the Stock item with removeFromStockById.

Using the methods above, your cards should slide to, from, and between your Stock controls smoothly.


You can customize this (showing the default value):


this.mystock.jstpl_stock_item= "<div id=\"${id}\" class=\"stockitem\" style=\"top:${top}px;left:${left}px;width:${width}px;height:${height}px;z-index:${position};background-image:url('${image}');\"></div>";

To produce a different type of stock item